-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcore_option_manager.c
1899 lines (1616 loc) · 54.8 KB
/
core_option_manager.c
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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <string/stdstring.h>
#ifdef HAVE_CHEEVOS
#include "cheevos/cheevos.h"
#endif
#ifdef HAVE_MENU
#include "menu/menu_driver.h"
#endif
#include "core_option_manager.h"
#include "msg_hash.h"
/*********************/
/* Option Conversion */
/*********************/
/**
* core_option_manager_convert_v1:
*
* @options_v1 : an array of retro_core_option_definition
* structs
*
* Converts an array of core option v1 definitions into
* a v2 core options struct. Returned pointer must be
* freed using core_option_manager_free_converted().
*
* Returns: Valid pointer to a new v2 core options struct
* if successful, otherwise NULL.
**/
struct retro_core_options_v2 *core_option_manager_convert_v1(
const struct retro_core_option_definition *options_v1)
{
size_t i;
size_t num_options = 0;
struct retro_core_options_v2 *options_v2 = NULL;
struct retro_core_option_v2_definition *option_v2_defs = NULL;
if (!options_v1)
return NULL;
/* Determine number of options */
for (;;)
{
if (string_is_empty(options_v1[num_options].key))
break;
num_options++;
}
if (num_options < 1)
return NULL;
/* Allocate output retro_core_options_v2 struct */
if (!(options_v2 = (struct retro_core_options_v2 *)
malloc(sizeof(*options_v2))))
return NULL;
/* Note: v1 options have no concept of
* categories, so this field will be left
* as NULL */
options_v2->categories = NULL;
options_v2->definitions = NULL;
/* Allocate output option_v2_defs array
* > One extra entry required for terminating NULL entry
* > Note that calloc() sets terminating NULL entry and
* correctly 'nullifies' each values array */
if (!(option_v2_defs = (struct retro_core_option_v2_definition *)
calloc(num_options + 1, sizeof(*option_v2_defs))))
{
free(options_v2);
return NULL;
}
options_v2->definitions = option_v2_defs;
/* Loop through options... */
for (i = 0; i < num_options; i++)
{
size_t j;
size_t num_values = 0;
/* Set key */
option_v2_defs[i].key = options_v1[i].key;
/* Set default value */
option_v2_defs[i].default_value = options_v1[i].default_value;
/* Set desc and info strings */
option_v2_defs[i].desc = options_v1[i].desc;
option_v2_defs[i].info = options_v1[i].info;
/* v1 options have no concept of categories
* (Note: These are already nullified by
* the preceding calloc(), but we do it
* explicitly here for code clarity) */
option_v2_defs[i].desc_categorized = NULL;
option_v2_defs[i].info_categorized = NULL;
option_v2_defs[i].category_key = NULL;
/* Determine number of values */
for (;;)
{
if (string_is_empty(options_v1[i].values[num_values].value))
break;
num_values++;
}
/* Copy values */
for (j = 0; j < num_values; j++)
{
/* Set value string */
option_v2_defs[i].values[j].value = options_v1[i].values[j].value;
/* Set value label string */
option_v2_defs[i].values[j].label = options_v1[i].values[j].label;
}
}
return options_v2;
}
/**
* core_option_manager_convert_v1_intl:
*
* @options_v1_intl : pointer to a retro_core_options_intl
* struct
*
* Converts a v1 'international' core options definition
* struct into a v2 core options struct. Returned pointer
* must be freed using core_option_manager_free_converted().
*
* Returns: Valid pointer to a new v2 core options struct
* if successful, otherwise NULL.
**/
struct retro_core_options_v2 *core_option_manager_convert_v1_intl(
const struct retro_core_options_intl *options_v1_intl)
{
size_t i;
size_t num_options = 0;
struct retro_core_option_definition *option_defs_us = NULL;
struct retro_core_option_definition *option_defs_local = NULL;
struct retro_core_options_v2 *options_v2 = NULL;
struct retro_core_option_v2_definition *option_v2_defs = NULL;
if (!options_v1_intl)
return NULL;
option_defs_us = options_v1_intl->us;
option_defs_local = options_v1_intl->local;
if (!option_defs_us)
return NULL;
/* Determine number of options */
for (;;)
{
if (string_is_empty(option_defs_us[num_options].key))
break;
num_options++;
}
if (num_options < 1)
return NULL;
/* Allocate output retro_core_options_v2 struct */
if (!(options_v2 = (struct retro_core_options_v2 *)
malloc(sizeof(*options_v2))))
return NULL;
/* Note: v1 options have no concept of
* categories, so this field will be left
* as NULL */
options_v2->categories = NULL;
options_v2->definitions = NULL;
/* Allocate output option_v2_defs array
* > One extra entry required for terminating NULL entry
* > Note that calloc() sets terminating NULL entry and
* correctly 'nullifies' each values array */
if (!(option_v2_defs = (struct retro_core_option_v2_definition *)
calloc(num_options + 1, sizeof(*option_v2_defs))))
{
core_option_manager_free_converted(options_v2);
return NULL;
}
options_v2->definitions = option_v2_defs;
/* Loop through options... */
for (i = 0; i < num_options; i++)
{
size_t j;
size_t num_values = 0;
const char *key = option_defs_us[i].key;
const char *local_desc = NULL;
const char *local_info = NULL;
struct retro_core_option_value *local_values = NULL;
/* Key is always taken from us english defs */
option_v2_defs[i].key = key;
/* Default value is always taken from us english defs */
option_v2_defs[i].default_value = option_defs_us[i].default_value;
/* Try to find corresponding entry in local defs array */
if (option_defs_local)
{
size_t index = 0;
for (;;)
{
const char *local_key = option_defs_local[index].key;
if (string_is_empty(local_key))
break;
if (string_is_equal(key, local_key))
{
local_desc = option_defs_local[index].desc;
local_info = option_defs_local[index].info;
local_values = option_defs_local[index].values;
break;
}
index++;
}
}
/* Set desc and info strings */
option_v2_defs[i].desc = string_is_empty(local_desc) ?
option_defs_us[i].desc : local_desc;
option_v2_defs[i].info = string_is_empty(local_info) ?
option_defs_us[i].info : local_info;
/* v1 options have no concept of categories
* (Note: These are already nullified by
* the preceding calloc(), but we do it
* explicitly here for code clarity) */
option_v2_defs[i].desc_categorized = NULL;
option_v2_defs[i].info_categorized = NULL;
option_v2_defs[i].category_key = NULL;
/* Determine number of values
* (always taken from us english defs) */
for (;;)
{
if (string_is_empty(option_defs_us[i].values[num_values].value))
break;
num_values++;
}
/* Copy values */
for (j = 0; j < num_values; j++)
{
const char *value = option_defs_us[i].values[j].value;
const char *local_label = NULL;
/* Value string is always taken from us english defs */
option_v2_defs[i].values[j].value = value;
/* Try to find corresponding entry in local defs values array */
if (local_values)
{
size_t value_index = 0;
for (;;)
{
const char *local_value = local_values[value_index].value;
if (string_is_empty(local_value))
break;
if (string_is_equal(value, local_value))
{
local_label = local_values[value_index].label;
break;
}
value_index++;
}
}
/* Set value label string */
option_v2_defs[i].values[j].label = string_is_empty(local_label) ?
option_defs_us[i].values[j].label : local_label;
}
}
return options_v2;
}
/**
* core_option_manager_convert_v2_intl:
*
* @options_v2_intl : pointer to a retro_core_options_v2_intl
* struct
*
* Converts a v2 'international' core options struct
* into a regular v2 core options struct. Returned pointer
* must be freed using core_option_manager_free_converted().
*
* Returns: Valid pointer to a new v2 core options struct
* if successful, otherwise NULL.
**/
struct retro_core_options_v2 *core_option_manager_convert_v2_intl(
const struct retro_core_options_v2_intl *options_v2_intl)
{
size_t i;
size_t num_categories = 0;
size_t num_options = 0;
struct retro_core_options_v2 *options_v2_us = NULL;
struct retro_core_options_v2 *options_v2_local = NULL;
struct retro_core_options_v2 *options_v2 = NULL;
struct retro_core_option_v2_category *option_v2_cats = NULL;
struct retro_core_option_v2_definition *option_v2_defs = NULL;
if (!options_v2_intl)
return NULL;
options_v2_us = options_v2_intl->us;
options_v2_local = options_v2_intl->local;
if (!options_v2_us ||
!options_v2_us->definitions)
return NULL;
/* Determine number of categories
* (Note: zero categories are permitted) */
if (options_v2_us->categories)
{
for (;;)
{
if (string_is_empty(options_v2_us->categories[num_categories].key))
break;
num_categories++;
}
}
/* Determine number of options */
for (;;)
{
if (string_is_empty(options_v2_us->definitions[num_options].key))
break;
num_options++;
}
if (num_options < 1)
return NULL;
/* Allocate output retro_core_options_v2 struct */
if (!(options_v2 = (struct retro_core_options_v2 *)
malloc(sizeof(*options_v2))))
return NULL;
options_v2->categories = NULL;
options_v2->definitions = NULL;
/* Allocate output option_v2_cats array
* > One extra entry required for terminating NULL entry
* > Note that calloc() sets terminating NULL entry */
if (num_categories > 0)
{
if (!(option_v2_cats = (struct retro_core_option_v2_category *)
calloc(num_categories + 1, sizeof(*option_v2_cats))))
{
core_option_manager_free_converted(options_v2);
return NULL;
}
}
options_v2->categories = option_v2_cats;
/* Allocate output option_v2_defs array
* > One extra entry required for terminating NULL entry
* > Note that calloc() sets terminating NULL entry and
* correctly 'nullifies' each values array */
if (!(option_v2_defs = (struct retro_core_option_v2_definition *)
calloc(num_options + 1, sizeof(*option_v2_defs))))
{
core_option_manager_free_converted(options_v2);
return NULL;
}
options_v2->definitions = option_v2_defs;
/* Loop through categories...
* (Note: This loop will not execute if
* options_v2_us->categories is NULL) */
for (i = 0; i < num_categories; i++)
{
const char *key = options_v2_us->categories[i].key;
const char *local_desc = NULL;
const char *local_info = NULL;
/* Key is always taken from us english
* categories */
option_v2_cats[i].key = key;
/* Try to find corresponding entry in local
* categories array */
if (options_v2_local &&
options_v2_local->categories)
{
size_t index = 0;
for (;;)
{
const char *local_key = options_v2_local->categories[index].key;
if (string_is_empty(local_key))
break;
if (string_is_equal(key, local_key))
{
local_desc = options_v2_local->categories[index].desc;
local_info = options_v2_local->categories[index].info;
break;
}
index++;
}
}
/* Set desc and info strings */
option_v2_cats[i].desc = string_is_empty(local_desc) ?
options_v2_us->categories[i].desc : local_desc;
option_v2_cats[i].info = string_is_empty(local_info) ?
options_v2_us->categories[i].info : local_info;
}
/* Loop through options... */
for (i = 0; i < num_options; i++)
{
size_t j;
size_t num_values = 0;
const char *key = options_v2_us->definitions[i].key;
const char *local_desc = NULL;
const char *local_desc_categorized = NULL;
const char *local_info = NULL;
const char *local_info_categorized = NULL;
struct retro_core_option_value *local_values = NULL;
/* Key is always taken from us english defs */
option_v2_defs[i].key = key;
/* Default value is always taken from us english defs */
option_v2_defs[i].default_value = options_v2_us->definitions[i].default_value;
/* Try to find corresponding entry in local defs array */
if (options_v2_local &&
options_v2_local->definitions)
{
size_t index = 0;
for (;;)
{
const char *local_key = options_v2_local->definitions[index].key;
if (string_is_empty(local_key))
break;
if (string_is_equal(key, local_key))
{
local_desc = options_v2_local->definitions[index].desc;
local_desc_categorized = options_v2_local->definitions[index].desc_categorized;
local_info = options_v2_local->definitions[index].info;
local_info_categorized = options_v2_local->definitions[index].info_categorized;
local_values = options_v2_local->definitions[index].values;
break;
}
index++;
}
}
/* Set desc and info strings */
option_v2_defs[i].desc = string_is_empty(local_desc) ?
options_v2_us->definitions[i].desc : local_desc;
option_v2_defs[i].desc_categorized = string_is_empty(local_desc_categorized) ?
options_v2_us->definitions[i].desc_categorized : local_desc_categorized;
option_v2_defs[i].info = string_is_empty(local_info) ?
options_v2_us->definitions[i].info : local_info;
option_v2_defs[i].info_categorized = string_is_empty(local_info_categorized) ?
options_v2_us->definitions[i].info_categorized : local_info_categorized;
/* Category key is always taken from us english defs */
option_v2_defs[i].category_key = options_v2_us->definitions[i].category_key;
/* Determine number of values
* (always taken from us english defs) */
for (;;)
{
if (string_is_empty(
options_v2_us->definitions[i].values[num_values].value))
break;
num_values++;
}
/* Copy values */
for (j = 0; j < num_values; j++)
{
const char *value = options_v2_us->definitions[i].values[j].value;
const char *local_label = NULL;
/* Value string is always taken from us english defs */
option_v2_defs[i].values[j].value = value;
/* Try to find corresponding entry in local defs values array */
if (local_values)
{
size_t value_index = 0;
for (;;)
{
const char *local_value = local_values[value_index].value;
if (string_is_empty(local_value))
break;
if (string_is_equal(value, local_value))
{
local_label = local_values[value_index].label;
break;
}
value_index++;
}
}
/* Set value label string */
option_v2_defs[i].values[j].label = string_is_empty(local_label) ?
options_v2_us->definitions[i].values[j].label : local_label;
}
}
return options_v2;
}
/**
* core_option_manager_convert_v2_intl:
*
* @options_v2 : pointer to a retro_core_options_v2
* struct
*
* Frees the pointer returned by any
* core_option_manager_convert_*() function.
**/
void core_option_manager_free_converted(
struct retro_core_options_v2 *options_v2)
{
if (!options_v2)
return;
if (options_v2->categories)
{
free(options_v2->categories);
options_v2->categories = NULL;
}
if (options_v2->definitions)
{
free(options_v2->definitions);
options_v2->definitions = NULL;
}
free(options_v2);
}
/**************************************/
/* Initialisation / De-Initialisation */
/**************************************/
/* Generates a hash key for the specified string */
static uint32_t core_option_manager_hash_string(const char *str)
{
unsigned char c;
uint32_t hash = (uint32_t)0x811c9dc5;
while ((c = (unsigned char)*(str++)) != '\0')
hash = ((hash * (uint32_t)0x01000193) ^ (uint32_t)c);
return (hash ? hash : 1);
}
/* Sanitises a core option value label, handling the case
* where an explicit label is not provided and performing
* conversion of various true/false identifiers to
* ON/OFF strings */
static const char *core_option_manager_parse_value_label(
const char *value, const char *value_label)
{
/* 'value_label' may be NULL */
const char *label = string_is_empty(value_label) ?
value : value_label;
if (string_is_empty(label))
return NULL;
/* Any label starting with a digit (or +/-)
* cannot be a boolean string, and requires
* no further processing */
if (ISDIGIT((unsigned char)*label) ||
(*label == '+') ||
(*label == '-'))
return label;
/* Core devs have a habit of using arbitrary
* strings to label boolean values (i.e. enabled,
* Enabled, on, On, ON, true, True, TRUE, disabled,
* Disabled, off, Off, OFF, false, False, FALSE).
* These should all be converted to standard ON/OFF
* strings
* > Note: We require some duplication here
* (e.g. MENU_ENUM_LABEL_ENABLED *and*
* MENU_ENUM_LABEL_VALUE_ENABLED) in order
* to match both localised and non-localised
* strings. This function is not performance
* critical, so these extra comparisons do
* no harm */
if (string_is_equal_noncase(label, msg_hash_to_str(MENU_ENUM_LABEL_ENABLED)) ||
string_is_equal_noncase(label, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_ENABLED)) ||
string_is_equal_noncase(label, "enable") ||
string_is_equal_noncase(label, "on") ||
string_is_equal_noncase(label, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_ON)) ||
string_is_equal_noncase(label, "true") ||
string_is_equal_noncase(label, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_TRUE)))
label = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_ON);
else if (string_is_equal_noncase(label, msg_hash_to_str(MENU_ENUM_LABEL_DISABLED)) ||
string_is_equal_noncase(label, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISABLED)) ||
string_is_equal_noncase(label, "disable") ||
string_is_equal_noncase(label, "off") ||
string_is_equal_noncase(label, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_OFF)) ||
string_is_equal_noncase(label, "false") ||
string_is_equal_noncase(label, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_FALSE)))
label = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_OFF);
return label;
}
/* Parses a single legacy core options interface
* variable, extracting all present core_option
* information */
static bool core_option_manager_parse_variable(
core_option_manager_t *opt, size_t idx,
const struct retro_variable *var,
config_file_t *config_src)
{
size_t i;
union string_list_elem_attr attr;
const char *val_start = NULL;
char *value = NULL;
char *desc_end = NULL;
struct core_option *option = (struct core_option*)&opt->opts[idx];
struct config_entry_list
*entry = NULL;
/* Record option index (required to facilitate
* option map handling) */
option->opt_idx = idx;
/* All options are visible by default */
option->visible = true;
if (!string_is_empty(var->key))
{
option->key = strdup(var->key);
option->key_hash = core_option_manager_hash_string(var->key);
}
if (!string_is_empty(var->value))
value = strdup(var->value);
if (!string_is_empty(value))
desc_end = strstr(value, "; ");
if (!desc_end)
goto error;
*desc_end = '\0';
if (!string_is_empty(value))
option->desc = strdup(value);
val_start = desc_end + 2;
option->vals = string_split(val_start, "|");
if (!option->vals)
goto error;
/* Legacy core option interface has no concept
* of value labels
* > Use actual values for display purposes */
attr.i = 0;
option->val_labels = string_list_new();
if (!option->val_labels)
goto error;
/* > Loop over values and:
* - Set value hashes
* - 'Extract' labels */
for (i = 0; i < option->vals->size; i++)
{
const char *value = option->vals->elems[i].data;
uint32_t *value_hash = (uint32_t *)malloc(sizeof(uint32_t));
const char *value_label = core_option_manager_parse_value_label(
value, NULL);
/* Set value hash */
*value_hash = core_option_manager_hash_string(value);
option->vals->elems[i].userdata = (void*)value_hash;
/* Redundant safely check... */
if (string_is_empty(value_label))
value_label = value;
/* Append value label string */
string_list_append(option->val_labels, value_label, attr);
}
/* Legacy core option interface always uses first
* defined value as the default */
option->default_index = 0;
option->index = 0;
if (config_src)
entry = config_get_entry(config_src, option->key);
else
entry = config_get_entry(opt->conf, option->key);
/* Set current config value */
if (entry && !string_is_empty(entry->value))
{
uint32_t entry_value_hash = core_option_manager_hash_string(entry->value);
for (i = 0; i < option->vals->size; i++)
{
const char *value = option->vals->elems[i].data;
uint32_t value_hash = *((uint32_t*)option->vals->elems[i].userdata);
if ((value_hash == entry_value_hash) &&
string_is_equal(value, entry->value))
{
option->index = i;
break;
}
}
}
/* Legacy core option interface has no concept
* of categories */
option->desc_categorized = NULL;
option->info_categorized = NULL;
option->category_key = NULL;
free(value);
return true;
error:
free(value);
return false;
}
/**
* core_option_manager_new_vars:
*
* @conf_path : Filesystem path to write core option
* config file to
* @src_conf_path : Filesystem path from which to load
* initial config settings.
* @vars : Pointer to core option variable array
* handle
*
* Legacy version of core_option_manager_new().
* Creates and initializes a core manager handle.
*
* Returns: handle to new core manager handle if successful,
* otherwise NULL.
**/
core_option_manager_t *core_option_manager_new_vars(
const char *conf_path, const char *src_conf_path,
const struct retro_variable *vars)
{
const struct retro_variable *var = NULL;
size_t size = 0;
config_file_t *config_src = NULL;
core_option_manager_t *opt = NULL;
if (!vars)
return NULL;
if (!(opt = (core_option_manager_t*)malloc(sizeof(*opt))))
return NULL;
opt->conf = NULL;
opt->conf_path[0] = '\0';
/* Legacy core option interface has no concept
* of categories, so leave opt->cats as NULL
* and opt->cats_size as zero */
opt->cats = NULL;
opt->cats_size = 0;
opt->opts = NULL;
opt->size = 0;
opt->option_map = nested_list_init();
opt->updated = false;
if (!opt->option_map)
goto error;
/* Open 'output' config file */
if (!string_is_empty(conf_path))
if (!(opt->conf = config_file_new_from_path_to_string(conf_path)))
if (!(opt->conf = config_file_new_alloc()))
goto error;
strlcpy(opt->conf_path, conf_path, sizeof(opt->conf_path));
/* Load source config file, if required */
if (!string_is_empty(src_conf_path))
config_src = config_file_new_from_path_to_string(src_conf_path);
/* Get number of variables */
for (var = vars; var->key && var->value; var++)
size++;
if (size == 0)
goto error;
/* Create options array */
if (!(opt->opts = (struct core_option*)calloc(size, sizeof(*opt->opts))))
goto error;
opt->size = size;
size = 0;
/* Parse each variable */
for (var = vars; var->key && var->value; size++, var++)
{
if (core_option_manager_parse_variable(opt, size, var, config_src))
{
size_t _len = 0;
/* If variable is read correctly, add it to
* the map */
char address[256];
address[ _len] = '#';
address[++_len] = '\0';
/* Address string is normally:
* <category_key><delim><tag><option_key>
* ...where <tag> is prepended to the option
* key in order to avoid category/option key
* collisions. Legacy options have no categories,
* so we could just set the address to
* <option_key> - but for consistency with
* 'modern' options, we apply the tag regardless */
strlcpy(address + _len, var->key, sizeof(address) - _len);
if (!nested_list_add_item(opt->option_map,
address, NULL, (const void*)&opt->opts[size]))
goto error;
}
else
goto error;
}
if (config_src)
config_file_free(config_src);
return opt;
error:
if (config_src)
config_file_free(config_src);
core_option_manager_free(opt);
return NULL;
}
/* Parses a single v2 core options interface
* option, extracting all present core_option
* information */
static bool core_option_manager_parse_option(
core_option_manager_t *opt, size_t idx,
const struct retro_core_option_v2_definition *option_def,
config_file_t *config_src)
{
size_t i;
union string_list_elem_attr attr;
struct config_entry_list
*entry = NULL;
size_t num_vals = 0;
struct core_option *option = (struct core_option*)&opt->opts[idx];
const char *key = option_def->key;
const char *category_key = option_def->category_key;
const struct retro_core_option_value
*values = option_def->values;
/* Record option index (required to facilitate
* option map handling) */
option->opt_idx = idx;
/* All options are visible by default */
option->visible = true;
if (!string_is_empty(option_def->desc))
option->desc = strdup(option_def->desc);
if (!string_is_empty(option_def->info))
option->info = strdup(option_def->info);
/* Set category-related parameters
* > Ignore if specified category key does not
* match an entry in the categories array
* > Category key cannot contain a map delimiter
* character */
if (opt->cats &&
!string_is_empty(category_key) &&
!strstr(category_key, ":"))
{
for (i = 0; i < opt->cats_size; i++)
{
const char *search_key = opt->cats[i].key;
if (string_is_empty(search_key))
break;
if (string_is_equal(search_key, category_key))
{
option->category_key = strdup(category_key);
if (!string_is_empty(option_def->desc_categorized))
option->desc_categorized = strdup(option_def->desc_categorized);
if (!string_is_empty(option_def->info_categorized))
option->info_categorized = strdup(option_def->info_categorized);
break;
}
}
}
/* Have to set key *after* checking for option
* categories */
if (!string_is_empty(option_def->key))
{
/* If option has a category, option key
* cannot contain a map delimiter character */
if (!string_is_empty(option->category_key) &&
strstr(key, ":"))
return false;
option->key = strdup(key);
option->key_hash = core_option_manager_hash_string(key);
}
/* Get number of values */
for (;;)
{
if (string_is_empty(values[num_vals].value))
break;
num_vals++;
}
if (num_vals < 1)
return false;
/* Initialise string lists */
attr.i = 0;
option->vals = string_list_new();
option->val_labels = string_list_new();
if (!option->vals || !option->val_labels)
return false;
/* Initialise default value */
option->default_index = 0;
option->index = 0;
/* Extract value/label pairs */
for (i = 0; i < num_vals; i++)
{
const char *value = values[i].value;
uint32_t *value_hash = (uint32_t *)malloc(sizeof(uint32_t));
const char *value_label = values[i].label;