-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathminirconfig.cpp
1476 lines (1299 loc) · 37.5 KB
/
minirconfig.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "minir.h"
#include "file.h"
#include "io.h"
#include <string.h>
//#include <strings.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
//force some year-old C code to compile properly as C++ - I decided to switch long ago but still haven't finished.
#define this This
#define MINIZ_HEADER_FILE_ONLY
#include "miniz.c"
#define count(array) (sizeof(array)/sizeof(*(array)))
#define nop(x) (x)
#define strdup_s(x) ((x) ? strdup(x) : NULL)
//TODO: store some version info in the config
//git commit, date, public version
struct minirconfig_impl {
struct minirconfig i;
struct configdata global;//we can be reasonably sure that this one does not have an unaligned size
struct configdata * bycore;
struct configdata * bygame;
unsigned int numbycore;
unsigned int numbygame;
const char * autoload;
char * originalconfig;
char * basepath;
bool firstrun;
//char padding[7];
};
//nonstatic so I can tell Valgrind to suppress _Z22initialize_to_defaultsP10configdata
void initialize_to_defaults(struct configdata * this);//because -Wmissing-declarations
void initialize_to_defaults(struct configdata * this)
{
memset(this, 0, sizeof(struct configdata));
for (unsigned int i=0;i<count(this->_scopes);i++) this->_scopes[i]=cfgsc_default;
#define CONFIG_CLEAR_DEFAULTS
#include "obj/generated.cpp"
#undef CONFIG_CLEAR_DEFAULTS
}
static void initialize_to_parent(struct configdata * this)
{
memset(this, 0, sizeof(struct configdata));
for (unsigned int i=0;i<count(this->_scopes);i++) this->_scopes[i]=cfgsc_default;
}
static void join_config(struct configdata * parent, struct configdata * child)
{
if (!child) return;
#undef JOIN
#define JOIN(groupname, scopegroupname, deleteold, clone) \
for (unsigned int i=0;i<count(parent->groupname);i++) \
{ \
if (child->scopegroupname[i] > parent->scopegroupname[i]) \
{ \
(void)deleteold(parent->groupname[i]); \
parent->groupname[i]=child->groupname[i]; \
} \
}
JOIN(inputs,_scopes_input,free,strdup_s);
JOIN(_strings,_scopes_str,free,strdup_s);
JOIN(_ints,_scopes_int,nop,nop);
JOIN(_uints,_scopes_uint,nop,nop);
JOIN(_enums,_scopes_enum,nop,nop);
JOIN(_bools,_scopes_bool,nop,nop);
#undef JOIN
}
static size_t create_core(struct minirconfig_impl * this)
{
if ((this->numbycore & (this->numbycore-1)) == 0)
{
this->bycore=realloc(this->bycore, sizeof(struct configdata)*this->numbycore*2);
}
initialize_to_parent(&this->bycore[this->numbycore]);
this->numbycore++;
return this->numbycore-1;
}
static size_t find_core(struct minirconfig_impl * this, const char * core)
{
size_t i;
for (i=1;i<this->numbycore;i++)
{
if (!strcmp(core, this->bycore[i]._corepath)) break;
}
return i;
}
static size_t find_or_create_core(struct minirconfig_impl * this, const char * core)
{
if (core==NULL) return 0;
size_t i=find_core(this, core);
if (i==this->numbycore)
{
create_core(this);
this->bycore[i]._corepath=strdup(core);
this->bycore[i].support=malloc(sizeof(char*));
this->bycore[i].support[0]=NULL;
this->bycore[i].primary=malloc(sizeof(char*));
this->bycore[i].primary[0]=NULL;
return i;
}
return i;
}
static size_t create_game(struct minirconfig_impl * this)
{
if ((this->numbygame & (this->numbygame-1)) == 0)
{
this->bygame=realloc(this->bygame, sizeof(struct configdata)*this->numbygame*2);
}
initialize_to_parent(&this->bygame[this->numbygame]);
this->numbygame++;
return this->numbygame-1;
}
static size_t find_game(struct minirconfig_impl * this, const char * game)
{
size_t i;
for (i=1;i<this->numbygame;i++)
{
if (!strcmp(game, this->bygame[i]._gamepath)) break;
}
return i;
}
static size_t find_or_create_game(struct minirconfig_impl * this, const char * game)
{
if (game==NULL) return 0;
size_t i=find_game(this, game);
if (i==this->numbygame)
{
create_game(this);
this->bygame[i]._gamepath=strdup(game);
return i;
}
return i;
}
static void delete_conf(struct configdata * this)
{
free(this->_corepath);
free(this->_gamepath);
free(this->corename);
free(this->gamename);
for (unsigned int i=0;i<count(this->inputs);i++) free(this->inputs[i]);
for (unsigned int i=0;i<count(this->_strings);i++) free(this->_strings[i]);
if (this->support) for (unsigned int i=0;this->support[i];i++) free(this->support[i]);
if (this->primary) for (unsigned int i=0;this->primary[i];i++) free(this->primary[i]);
free(this->support);
free(this->primary);
memset(this, 0, sizeof(*this));
}
static int find_place(char* * start, unsigned int len, char* newitem)
{
unsigned int jumpsize=bitround(len+1)/2;
signed int pos=0;
while (jumpsize)
{
if (pos<0) pos+=jumpsize;
else if ((unsigned)pos>=len) pos-=jumpsize;
else if (strcmp(start[pos], newitem)<0) pos+=jumpsize;
else pos-=jumpsize;
jumpsize/=2;
}
if (pos<0) pos+=1;
else if ((unsigned)pos>=len) pos-=0;
else if (strcmp(start[pos], newitem)<0) pos+=1;
else pos-=0;
return pos;
}
static void sort_and_clean_core_support(struct configdata * core)
{
char* * unsorted;
unsigned int numunsorted;
char* * sorted;
unsigned int numsorted;
//sort/uniq support
unsorted=core->support;
for (numunsorted=0;unsorted[numunsorted];numunsorted++) {}
sorted=unsorted;
numsorted=0;
while (numunsorted)
{
char* this=*unsorted;
unsigned int newpos=find_place(sorted, numsorted, this);
if (newpos==numsorted || strcmp(this, sorted[newpos])!=0)
{
memmove(sorted+newpos+1, sorted+newpos, sizeof(char*)*(numsorted-newpos));
sorted[newpos]=this;
numsorted++;
}
else free(this);
unsorted++;
numunsorted--;
}
//sort/uniq primary, delete non-support
char* * support=core->support;
unsigned int numsupport=numsorted;
unsorted=core->primary;
for (numunsorted=0;unsorted[numunsorted];numunsorted++) {}
sorted=unsorted;
numsorted=0;
while (numunsorted)
{
char* this=*unsorted;
unsigned int newpos=find_place(sorted, numsorted, this);
unsigned int supportpos=find_place(support, numsupport, this);
if ((supportpos<numsupport && strcmp(this, support[supportpos])==0) &&
(newpos==numsorted || strcmp(this, sorted[newpos])!=0))
{
memmove(sorted+newpos+1, sorted+newpos, sizeof(char*)*(numsorted-newpos));
sorted[newpos]=this;
numsorted++;
}
else free(this);
unsorted++;
numunsorted--;
}
}
static void split_config(struct configdata * Public, struct configdata * global,
struct configdata * bycore, struct configdata * bygame)
{
#define SPLIT(groupname, scopegroupname, deleteold, clone) \
for (unsigned int i=0;i<count(Public->groupname);i++) \
{ \
if(0); \
else if (Public->scopegroupname[i] >= cfgsc_game) \
{ \
(void)deleteold(bygame->groupname[i]); \
bygame->groupname[i]=clone(Public->groupname[i]); \
} \
else if (Public->scopegroupname[i] >= cfgsc_core) \
{ \
(void)deleteold(bygame->groupname[i]); \
bycore->groupname[i]=clone(Public->groupname[i]); \
} \
else \
{ \
(void)deleteold(global->groupname[i]); \
global->groupname[i]=clone(Public->groupname[i]); \
} \
}
SPLIT(inputs,_scopes_input,free,strdup_s);
SPLIT(_strings,_scopes_str,free,strdup_s);
SPLIT(_ints,_scopes_int,nop,nop);
SPLIT(_uints,_scopes_uint,nop,nop);
SPLIT(_enums,_scopes_enum,nop,nop);
SPLIT(_bools,_scopes_bool,nop,nop);
#undef SPLIT
}
static void set_support(struct minirconfig_impl * this, unsigned int id, char * * support_in, char * * primary_in)
{
//check if the support list is unchanged (it should be unless this core is fresh)
if (support_in)
{
for (unsigned int i=0;true;i++)
{
if (!this->bycore[id].support[i] && !support_in[i])
{
support_in=NULL;
break;
}
if (!this->bycore[id].support[i] || !support_in[i] || strcmp(this->bycore[id].support[i], support_in[i])) break;
}
}
//same for primary
if (primary_in)
{
for (unsigned int i=0;true;i++)
{
if (!this->bycore[id].primary[i] && !primary_in[i])
{
primary_in=NULL;
break;
}
if (!this->bycore[id].primary[i] || !primary_in[i] || strcmp(this->bycore[id].primary[i], primary_in[i])) break;
}
}
if (!support_in && !primary_in) return;
for (unsigned int i=0;this->bycore[id].primary[i];i++) free(this->bycore[id].primary[i]);
this->bycore[id].primary[0]=NULL;
size_t primary_buflen=2;
char* * primary=malloc(sizeof(char*)*primary_buflen);
size_t primary_count=0;
if (support_in)
{
for (unsigned int i=0;this->bycore[id].support[i];i++) free(this->bycore[id].support[i]);
this->bycore[id].support[0]=NULL;
size_t support_buflen=2;
char* * support=malloc(sizeof(char*)*support_buflen);
size_t support_count=0;
for (size_t i=0;support_in[i];i++)
{
//check if it's already known by this core; if it is, discard it
for (size_t j=0;j<support_count;j++)
{
if (!strcmp(support[j], support_in[i])) goto nope;
}
support[support_count]=strdup(support_in[i]);
support_count++;
if (support_count==support_buflen)
{
support_buflen*=2;
support=realloc(support, sizeof(char*)*support_buflen);
}
//check if it's known by other cores; if it is, do not set it as primary
for (size_t j=1;j<this->numbycore;j++)
{
for (size_t k=0;this->bycore[j].primary[k];k++)
{
if (!strcmp(this->bycore[j].primary[k], support_in[i])) goto nope;
}
}
primary[primary_count]=strdup(support_in[i]);
primary_count++;
if (primary_count==primary_buflen)
{
primary_buflen*=2;
primary=realloc(primary, sizeof(char*)*primary_buflen);
}
nope: ;
}
support[support_count]=NULL;
free(this->bycore[id].support);
this->bycore[id].support=support;
}
if (primary_in)
{
for (size_t i=0;primary_in[i];i++)
{
//if we're already primary for this one, we don't need to set anything
for (size_t j=0;j<primary_count;j++)
{
if (!strcmp(primary[j], primary_in[i])) goto nope2;
}
//someone else has claimed this extension as their primary, throw them out
for (size_t j=1;j<this->numbycore;j++)
{
for (size_t k=0;this->bycore[j].primary[k];k++)
{
if (!strcmp(this->bycore[j].primary[k], primary_in[i]))
{
//found it; let's shift the others
while (this->bycore[j].primary[k])
{
this->bycore[j].primary[k]=this->bycore[j].primary[k+1];
k++;
}
break;
}
}
}
primary[primary_count]=strdup(primary_in[i]);
primary_count++;
if (primary_count==primary_buflen)
{
primary_buflen*=2;
primary=realloc(primary, sizeof(char*)*primary_buflen);
}
nope2: ;
}
}
primary[primary_count]=NULL;
free(this->bycore[id].primary);
this->bycore[id].primary=primary;
sort_and_clean_core_support(&this->bycore[id]);
}
static const char * get_autoload(struct minirconfig * this_)
{
struct minirconfig_impl * this=(struct minirconfig_impl*)this_;
return this->autoload;
}
static const char * * get_supported_extensions(struct minirconfig * this_)
{
struct minirconfig_impl * this=(struct minirconfig_impl*)this_;
unsigned int numret=0;
for (unsigned int i=1;i<this->numbycore;i++)
{
unsigned int j;
for (j=0;this->bycore[i].primary[j];j++) {}
numret+=j;
}
const char * * ret=malloc(sizeof(const char*)*(numret+1));
ret[numret]=NULL;
numret=0;
for (unsigned int i=1;i<this->numbycore;i++)
{
unsigned int j;
for (j=0;this->bycore[i].primary[j];j++) {}
memcpy(ret+numret, this->bycore[i].primary, sizeof(const char*)*j);
numret+=j;
}
return ret;
}
static struct configcorelist * get_core_for(struct minirconfig * this_, const char * gamepath, unsigned int * count)
{
struct minirconfig_impl * this=(struct minirconfig_impl*)this_;
size_t gameid=find_game(this, gamepath);
if (gameid!=this->numbygame)
{
if (this->bygame[gameid]._forcecore)
{
size_t coreid=find_core(this, this->bygame[gameid]._forcecore);
//size_t coreid=find_or_create_core(this->bygame[gameid]._forcecore);
if (coreid!=this->numbycore)
{
struct configcorelist * ret=malloc(sizeof(struct configcorelist)*2);
ret[0].path=this->bygame[gameid]._forcecore;
ret[0].name=this->bycore[coreid].corename;
ret[1].path=NULL;
ret[1].name=NULL;
if (count) *count=1;
return ret;
}
else
{
free(this->bygame[gameid]._forcecore);
this->bygame[gameid]._forcecore=NULL;
}
}
}
const char * extension=strrchr(gamepath, '.');
size_t retbuflen=1;
size_t numret=0;
struct configcorelist * ret=malloc(sizeof(struct configcorelist)*retbuflen);
if (extension && !strchr(extension, '/'))
{
extension++;
for (unsigned int i=1;i<this->numbycore;i++)
{
bool thisprimary=false;
for (int j=0;this->bycore[i].primary[j];j++)
{
if (!strcmp(extension, this->bycore[i].primary[j]))
{
thisprimary=true;
}
}
for (int j=0;this->bycore[i].support[j];j++)
{
if (!strcmp(extension, this->bycore[i].support[j]))
{
if (thisprimary && numret!=0)
{
//memmove(ret+1, ret, sizeof(struct configcorelist)*(numret-1));
ret[numret].path=ret[0].path;
ret[numret].name=ret[0].name;
ret[0].path=this->bycore[i]._corepath;
ret[0].name=this->bycore[i].corename;
}
else
{
ret[numret].path=this->bycore[i]._corepath;
ret[numret].name=this->bycore[i].corename;
}
numret++;
if (numret==retbuflen)
{
retbuflen*=2;
ret=realloc(ret, sizeof(struct configcorelist)*retbuflen);
}
}
}
}
}
ret[numret].path=NULL;
ret[numret].name=NULL;
if (count) *count=numret;
return ret;
}
static void data_load(struct minirconfig * this_, struct configdata * config,
bool free_old, const char * corepath, const char * gamepath)
{
struct minirconfig_impl * this=(struct minirconfig_impl*)this_;
if (free_old) delete_conf(config);
initialize_to_defaults(config);
join_config(config, &this->global);
if (corepath)
{
char * truecore=window_get_absolute_path(this->basepath, corepath, true);
unsigned int id=find_or_create_core(this, truecore);
free(truecore);
config->corename=this->bycore[id].corename;
config->_corepath=this->bycore[id]._corepath;
config->support=this->bycore[id].support;
config->primary=this->bycore[id].primary;
join_config(config, &this->bycore[id]);
}
else
{
config->corename=NULL;
config->support=malloc(sizeof(char*));
config->support[0]=NULL;
config->primary=malloc(sizeof(char*));
config->primary[0]=NULL;
}
if (gamepath)
{
char * truegame=window_get_absolute_path(this->basepath, gamepath, true);
unsigned int id=find_or_create_game(this, truegame);
free(truegame);
join_config(config, &this->bygame[id]);
config->gamename=this->bygame[id].gamename;
config->_gamepath=this->bygame[id]._gamepath;
}
else config->gamename=NULL;
for (unsigned int i=0;i<count(config->inputs);i++) config->inputs[i]=strdup_s(config->inputs[i]);
for (unsigned int i=0;i<count(config->_strings);i++) config->_strings[i]=strdup_s(config->_strings[i]);
unsigned int i;
for (i=0;config->support[i];i++) {}
i++;//for the NULL
char * * oldsupport=config->support;
config->support=malloc(sizeof(char*)*i);
for (i=0;oldsupport[i];i++) config->support[i]=strdup(oldsupport[i]);
config->support[i]=NULL;
for (i=0;config->primary[i];i++) {}
i++;
char * * oldprimary=config->primary;
config->primary=malloc(sizeof(char*)*i);
for (i=0;oldprimary[i];i++) config->primary[i]=strdup(oldprimary[i]);
config->primary[i]=NULL;
if (config->corename) config->corename=strdup(config->corename);
if (config->gamename) config->gamename=strdup(config->gamename);
if (config->_corepath) config->_corepath=strdup(config->_corepath);
if (config->_gamepath) config->_gamepath=strdup(config->_gamepath);
config->firstrun=this->firstrun;
}
static void data_save(struct minirconfig * this_, struct configdata * config)
{
struct minirconfig_impl * this=(struct minirconfig_impl*)this_;
size_t coreid=find_or_create_core(this, config->_corepath);
size_t gameid=find_or_create_game(this, config->_gamepath);
split_config(config, &this->global, &this->bycore[coreid], &this->bygame[gameid]);
if (coreid)
{
set_support(this, coreid, config->support, config->primary);
free(this->bycore[coreid].corename);
this->bycore[coreid].corename=strdup_s(config->corename);
}
if (gameid)
{
free(this->bygame[gameid].gamename);
this->bygame[gameid].gamename=strdup_s(config->gamename);
//free(this->bygame[gameid]._forcecore);
//this->bygame[gameid]._forcecore=strdup(config->_forcecore);
}
}
static void data_free(struct minirconfig * this_, struct configdata * config)
{
delete_conf(config);
}
static void data_destroy(struct minirconfig * this_, const char * item)
{
struct minirconfig_impl * this=(struct minirconfig_impl*)this_;
size_t id=find_core(this, item);
if (id!=this->numbycore)
{
delete_conf(&this->bycore[id]);
memmove(&this->bycore[id], &this->bycore[id+1], sizeof(struct configdata)*(this->numbycore-id));
this->numbycore--;
}
id=find_game(this, item);
if (id!=this->numbygame)
{
free(this->bygame[id]._forcecore);
this->bygame[id]._forcecore=NULL;
delete_conf(&this->bygame[id]);
memmove(&this->bygame[id], &this->bygame[id+1], sizeof(struct configdata)*(this->numbygame-id));
this->numbygame--;
}
}
enum {
CFGB_END,
CFGB_COMMENT,
CFGB_LINEBREAK,//Linebreaks that appear only in the root node are in comments.
CFGB_GLOBAL,
CFGB_ARRAY,
CFGB_ARRAY_SHUFFLED,
CFGB_ARRAY_SAME,
CFGB_INPUT,
CFGB_STR,
CFGB_INT,
CFGB_UINT,
CFGB_ENUM,
CFGB_BOOL,
CFGB_STR_MULTI,
CFGB_STR_MAP,
};
static const unsigned char config_bytecode_comp[]={
#define CONFIG_BYTECODE
#include "obj/generated.cpp"
#undef CONFIG_BYTECODE
};
static unsigned char config_bytecode[CONFIG_BYTECODE_LEN];
static char * outstart;
static char * outat;
static char * outend;
static void reserve(unsigned int size)
{
if (outat+size>outend)
{
int buflen=(outend-outstart)*2;
int bufat=outat-outstart;
outstart=realloc(outstart, buflen);
outat=outstart+bufat;
outend=outstart+buflen;
}
}
static void appenddat(const char * str, int len)
{
reserve(len);
memcpy(outat, str, len);
outat+=len;
}
static void appendstr(const char * str)
{
appenddat(str, strlen(str));
}
static void appendlf()
{
appenddat("\n", 1);
}
static void print_config_file(struct configdata * this, unsigned char minscope, bool fullarrays, bool comments)
{
const unsigned char * thisone=config_bytecode;
const unsigned char * arrayshuffle;
const char * arraynames;
int arraynamelen;
while (*thisone!=CFGB_END)
{
const unsigned char * at=thisone;
int arraylen=1;
int arrayend=1;
if (*at==CFGB_LINEBREAK)
{
if (outat[-2]!='\n') appendlf();
thisone=at+1;
continue;
}
if (*at==CFGB_COMMENT)
{
if (comments)
{
appenddat((char*)at+2, at[1]);
}
thisone=at+2+at[1];
continue;
}
if (*at==CFGB_GLOBAL) at++;
if (*at==CFGB_ARRAY)
{
arrayshuffle=NULL;
arraynames=(char*)at+4;
arraylen=at[1];
arraynamelen=at[2];
arrayend=at[3];
at+=4+at[1]*at[2];
}
if (*at==CFGB_ARRAY_SHUFFLED)
{
arrayshuffle=at+4;
arraynames=(char*)at+4+at[1];
arraylen=at[1];
arraynamelen=at[2];
arrayend=at[3];
at+=4+at[1]+at[1]*at[2];
}
if (*at==CFGB_ARRAY_SAME)
{
arraylen=at[1];
arrayend=at[3];
at+=4;
}
int baseoffset=((at[1]<<8)|(at[2]));
unsigned char itemtype=*at;
if(0);
else if (*at==CFGB_INPUT) at+=3;
else if (*at==CFGB_STR) at+=3;
else if (*at==CFGB_INT) at+=11;
else if (*at==CFGB_UINT) at+=11;
//else if (*at==CFGB_ENUM) at++;
else if (*at==CFGB_BOOL) at+=3;
else if (*at==CFGB_STR_MULTI) at+=3;
else if (*at==CFGB_STR_MAP) at+=3;
for (int i=0;i<arraylen;i++)
{
if (itemtype==CFGB_INPUT && this->_scopes_input[baseoffset+i]<minscope) continue;
if (itemtype==CFGB_STR && this->_scopes_str[baseoffset+i]<minscope) continue;
if (itemtype==CFGB_INT && this->_scopes_int[baseoffset+i]<minscope) continue;
if (itemtype==CFGB_UINT && this->_scopes_uint[baseoffset+i]<minscope) continue;
if (itemtype==CFGB_BOOL && this->_scopes_bool[baseoffset+i]<minscope) continue;
if (itemtype==CFGB_STR_MULTI && this->_scopes_strlist[baseoffset+i]<minscope) continue;
if (itemtype==CFGB_STR_MAP && this->_scopes_strmap[baseoffset+i]<minscope) continue;
if (i>=arrayend && !fullarrays)
{
if (itemtype==CFGB_INPUT && !this->inputs[baseoffset+i]) continue;
if (itemtype==CFGB_STR && !this->_strings[baseoffset+i]) continue;
if (itemtype==CFGB_INT && !this->_ints[baseoffset+i]) continue;
if (itemtype==CFGB_UINT && !this->_uints[baseoffset+i]) continue;
if (itemtype==CFGB_BOOL && !this->_bools[baseoffset+i]) continue;
if (itemtype==CFGB_STR_MULTI && !this->_strlists[baseoffset+i]) continue;
if (itemtype==CFGB_STR_MAP && !this->_strmaps[baseoffset+i]) continue;
}
char varname[256];
unsigned int varnamepos=0;
int arrayoffset=0;
memcpy(varname+varnamepos, at+1, *at); varnamepos+=*at;
if (arraylen!=1)
{
int dynlen=arraynamelen;
while (arraynames[arraynamelen*i + dynlen-1]=='\0') dynlen--;
memcpy(varname+varnamepos, arraynames + arraynamelen*i, dynlen); varnamepos+=dynlen;
arrayoffset=arrayshuffle?arrayshuffle[i]:i;
}
int offset=baseoffset+arrayoffset;
varname[varnamepos++]='=';
if (itemtype!=CFGB_STR_MULTI && itemtype!=CFGB_STR_MAP)
{
appenddat(varname, varnamepos);
if (itemtype==CFGB_INPUT)
{
appendstr(this->inputs[offset]?this->inputs[offset]:"");
}
if (itemtype==CFGB_STR)
{
appendstr(this->_strings[offset]?this->_strings[offset]:"");
}
if (itemtype==CFGB_INT)
{
char buf[32];
sprintf(buf, "%i", this->_ints[offset]);
appendstr(buf);
}
if (itemtype==CFGB_UINT)
{
char buf[32];
sprintf(buf, "%u", this->_uints[offset]);
appendstr(buf);
}
if (itemtype==CFGB_BOOL)
{
appendstr(this->_bools[offset]?"true":"false");
}
appendlf();
}
else
{
char** tmp;
if (itemtype==CFGB_STR_MULTI)
{
tmp=this->_strlists[offset];
}
else
{
tmp=this->_strmaps[offset];
varname[varnamepos-1]='_';
}
if (!tmp || !*tmp)
{
appenddat(varname, varnamepos);//set the list to an empty string
if (itemtype==CFGB_STR_MAP) appendstr("=");
appendlf();
continue;
}
while (*tmp)
{
appenddat(varname, varnamepos);
appendstr(*tmp);
appendlf();
tmp++;
}
}
}
thisone=at+1+at[0];
}
}
static void write(struct minirconfig * this_, const char * path)
{
struct minirconfig_impl * this=(struct minirconfig_impl*)this_;
outstart=malloc(8192);
outat=outstart;
outend=outstart+8192;
appendstr("[global]\n");
print_config_file(&this->global, (this->global.verbosity>=cfgv_default ? cfgsc_default : cfgsc_global),
this->global.verbosity>=cfgv_maximum, this->global.verbosity>=cfgv_default);
for (unsigned int i=1;i<this->numbycore;i++)
{
if (outat[-2]!='\n') appendlf();
appendstr("[core]\n");
if (this->global.verbosity>=cfgv_default && i==1)
{
appendstr("#minir will only set core-specific entries here by default,"
"but you can copy any setting from [global] if you want to.\n"
"#You will, of course, still be able to change it from within minir.\n"
"#However, you can't change whether something is core-specific from within minir.\n"
"#Anything that starts core-specific stays that way, "
"even if it's set to the same value as the global one.\n"
"#Additionally, you can't copy anything originating from here into other sections; "
"that wouldn't make sense.\n"
);
}
if (this->bycore[i].corename)
{
appendstr("name="); appendstr(this->bycore[i].corename); appendlf();
}
appendstr("path="); appendstr(this->bycore[i]._corepath); appendlf();
for (unsigned int j=0;this->bycore[i].primary[j];j++)
{
appendstr("primary=");
appendstr(this->bycore[i].primary[j]);
appendlf();
}
for (unsigned int j=0;this->bycore[i].support[j];j++)
{
appendstr("support=");
appendstr(this->bycore[i].support[j]);
appendlf();
}
//print core options HERE
print_config_file(&this->bycore[i], cfgsc_core, false, false);
}
for (unsigned int i=1;i<this->numbygame;i++)
{
if (outat[-2]!='\n') appendstr("\n");
appendstr("[game]\n");
if (this->global.verbosity>=cfgv_default && i==1)
{
appendstr("#You can copypaste global settings to here too,"
" and they will override the global or core-specific settings.\n"
"#You can also set core=C:/path/to/core_libretro.dll to pick another"
" core for that ROM specifically.\n"
#ifdef _WIN32
"#Use forward slashes.\n"
#endif
);
}
if (this->bygame[i].gamename)
{
appendstr("name="); appendstr(this->bygame[i].gamename); appendlf();
}
appendstr("path="); appendstr(this->bygame[i]._gamepath); appendlf();
if (this->bygame[i]._forcecore) { appendstr("core="); appendstr(this->bygame[i]._forcecore); appendlf(); }
if (this->bygame[i]._autoload) { appendstr("autoload=true\n"); }
//print core options HERE if any
print_config_file(&this->bygame[i], cfgsc_game, false, false);
}
if (outat[-2]=='\n') outat--;
//and a null terminator
reserve(1);
*outat='\0';
#ifdef _WIN32
size_t len=0;
for (char * outlen=outstart;*outlen;outlen++)
{
if (*outlen=='\n') len++;
len++;
}
char * newout=malloc(len+1);
char * newoutat=newout;
for (char * inat=outstart;*inat;inat++)
{
if (*inat=='\n') *(newoutat++)='\r';
*(newoutat++)=*inat;
}
*newoutat='\0';
free(outstart);
outstart=newout;
#else
size_t len=outat-outstart;
#endif
if (!this->originalconfig || strcmp(outstart, this->originalconfig))
{
file_write(path, outstart, len);
free(this->originalconfig);
this->originalconfig=outstart;
}
else free(outstart);
}
static void free_(struct minirconfig * this_)
{