-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathmanual_content_scan.c
1333 lines (1159 loc) · 42.5 KB
/
manual_content_scan.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
/* Copyright (C) 2010-2019 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (manual_content_scan.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <file/file_path.h>
#include <file/archive_file.h>
#include <string/stdstring.h>
#include <lists/dir_list.h>
#include <retro_miscellaneous.h>
#include "msg_hash.h"
#include "list_special.h"
#include "core_info.h"
#include "file_path_special.h"
#include "frontend/frontend_driver.h"
#include "manual_content_scan.h"
/* Holds all configuration parameters associated
* with a manual content scan */
typedef struct
{
enum manual_content_scan_system_name_type system_name_type;
enum manual_content_scan_core_type core_type;
char core_path[PATH_MAX_LENGTH];
char file_exts_core[PATH_MAX_LENGTH];
char file_exts_custom[PATH_MAX_LENGTH];
char dat_file_path[PATH_MAX_LENGTH];
char content_dir[DIR_MAX_LENGTH];
char system_name_content_dir[DIR_MAX_LENGTH];
char system_name_database[NAME_MAX_LENGTH];
char system_name_custom[NAME_MAX_LENGTH];
char core_name[NAME_MAX_LENGTH];
bool search_recursively;
bool search_archives;
bool filter_dat_content;
bool overwrite_playlist;
bool validate_entries;
} scan_settings_t;
/* TODO/FIXME - static public global variables */
/* Static settings object
* > Provides easy access to settings parameters
* when creating associated menu entries
* > We are handling this in almost exactly the same
* way as the regular global 'static settings_t *configuration_settings;'
* object in retroarch.c. This means it is not inherently thread safe,
* but this should not be an issue (i.e. regular configuration_settings
* are not thread safe, but we only access them when pushing a
* task, not in the task thread itself, so all is well) */
static scan_settings_t scan_settings = {
MANUAL_CONTENT_SCAN_SYSTEM_NAME_CONTENT_DIR, /* system_name_type */
MANUAL_CONTENT_SCAN_CORE_DETECT, /* core_type */
"", /* content_dir */
"", /* system_name_content_dir */
"", /* system_name_database */
"", /* system_name_custom */
"", /* core_name */
"", /* core_path */
"", /* file_exts_core */
"", /* file_exts_custom */
"", /* dat_file_path */
true, /* search_recursively */
false, /* search_archives */
false, /* filter_dat_content */
false, /* overwrite_playlist */
false /* validate_entries */
};
/*****************/
/* Configuration */
/*****************/
/* Pointer access */
/* Returns a pointer to the internal
* 'content_dir' string */
char *manual_content_scan_get_content_dir_ptr(void)
{
return scan_settings.content_dir;
}
/* Returns a pointer to the internal
* 'system_name_custom' string */
char *manual_content_scan_get_system_name_custom_ptr(void)
{
return scan_settings.system_name_custom;
}
/* Returns size of the internal
* 'system_name_custom' string */
size_t manual_content_scan_get_system_name_custom_size(void)
{
return sizeof(scan_settings.system_name_custom);
}
/* Returns a pointer to the internal
* 'file_exts_custom' string */
char *manual_content_scan_get_file_exts_custom_ptr(void)
{
return scan_settings.file_exts_custom;
}
/* Returns size of the internal
* 'file_exts_custom' string */
size_t manual_content_scan_get_file_exts_custom_size(void)
{
return sizeof(scan_settings.file_exts_custom);
}
/* Returns a pointer to the internal
* 'dat_file_path' string */
char *manual_content_scan_get_dat_file_path_ptr(void)
{
return scan_settings.dat_file_path;
}
/* Returns size of the internal
* 'dat_file_path' string */
size_t manual_content_scan_get_dat_file_path_size(void)
{
return sizeof(scan_settings.dat_file_path);
}
/* Returns a pointer to the internal
* 'search_recursively' bool */
bool *manual_content_scan_get_search_recursively_ptr(void)
{
return &scan_settings.search_recursively;
}
/* Returns a pointer to the internal
* 'search_archives' bool */
bool *manual_content_scan_get_search_archives_ptr(void)
{
return &scan_settings.search_archives;
}
/* Returns a pointer to the internal
* 'filter_dat_content' bool */
bool *manual_content_scan_get_filter_dat_content_ptr(void)
{
return &scan_settings.filter_dat_content;
}
/* Returns a pointer to the internal
* 'overwrite_playlist' bool */
bool *manual_content_scan_get_overwrite_playlist_ptr(void)
{
return &scan_settings.overwrite_playlist;
}
/* Returns a pointer to the internal
* 'validate_entries' bool */
bool *manual_content_scan_get_validate_entries_ptr(void)
{
return &scan_settings.validate_entries;
}
/* Sanitisation */
/* Sanitises file extensions list string:
* > Removes period (full stop) characters
* > Converts to lower case
* > Trims leading/trailing whitespace */
static void manual_content_scan_scrub_file_exts(char *file_exts)
{
if (string_is_empty(file_exts))
return;
string_remove_all_chars(file_exts, '.');
string_to_lower(file_exts);
string_trim_whitespace_right(file_exts);
string_trim_whitespace_left(file_exts);
}
/* Removes invalid characters from
* 'system_name_custom' string */
void manual_content_scan_scrub_system_name_custom(void)
{
char *scrub_char_pointer = NULL;
if (string_is_empty(scan_settings.system_name_custom))
return;
/* Scrub characters that are not cross-platform
* and/or violate the No-Intro filename standard:
* http://datomatic.no-intro.org/stuff/The%20Official%20No-Intro%20Convention%20(20071030).zip
* Replace these characters with underscores */
while ((scrub_char_pointer =
strpbrk(scan_settings.system_name_custom, "&*/:`\"<>?\\|")))
*scrub_char_pointer = '_';
}
/* Removes period (full stop) characters from
* 'file_exts_custom' string and converts to
* lower case */
void manual_content_scan_scrub_file_exts_custom(void)
{
manual_content_scan_scrub_file_exts(scan_settings.file_exts_custom);
}
/* Checks 'dat_file_path' string and resets it
* if invalid */
enum manual_content_scan_dat_file_path_status
manual_content_scan_validate_dat_file_path(void)
{
enum manual_content_scan_dat_file_path_status dat_file_path_status =
MANUAL_CONTENT_SCAN_DAT_FILE_UNSET;
/* Check if 'dat_file_path' has been set */
if (!string_is_empty(scan_settings.dat_file_path))
{
uint64_t file_size;
/* Check if path itself is valid */
if (logiqx_dat_path_is_valid(scan_settings.dat_file_path, &file_size))
{
uint64_t free_memory = frontend_driver_get_free_memory();
dat_file_path_status = MANUAL_CONTENT_SCAN_DAT_FILE_OK;
/* DAT files can be *very* large...
* Try to enforce sane behaviour by requiring
* the system to have an amount of free memory
* at least twice the size of the DAT file...
* > Note that desktop (and probably mobile)
* platforms should always have enough memory
* for this - we're really only protecting the
* console ports here */
if (free_memory > 0)
{
if (free_memory < (2 * file_size))
dat_file_path_status = MANUAL_CONTENT_SCAN_DAT_FILE_TOO_LARGE;
}
/* This is an annoying condition - it means the
* current platform doesn't have a 'free_memory'
* implementation...
* Have to make some assumptions in this case:
* > Typically the lowest system RAM of a supported
* platform in 32MB
* > Propose that (2 * file_size) should be no more
* than 1/4 of this total RAM value */
else if ((2 * file_size) > (8 * 1048576))
dat_file_path_status = MANUAL_CONTENT_SCAN_DAT_FILE_TOO_LARGE;
}
else
dat_file_path_status = MANUAL_CONTENT_SCAN_DAT_FILE_INVALID;
}
/* Reset 'dat_file_path' if status is anything other
* that 'OK' */
if (dat_file_path_status != MANUAL_CONTENT_SCAN_DAT_FILE_OK)
scan_settings.dat_file_path[0] = '\0';
return dat_file_path_status;
}
/* Menu setters */
/* Sets content directory for next manual scan
* operation.
* Returns true if content directory is valid. */
bool manual_content_scan_set_menu_content_dir(const char *content_dir)
{
size_t _len;
const char *dir_name = NULL;
/* Sanity check */
if (string_is_empty(content_dir))
goto error;
if (!path_is_directory(content_dir))
goto error;
/* Copy directory path to settings struct.
* Remove trailing slash, if required */
if ((_len = strlcpy(
scan_settings.content_dir, content_dir,
sizeof(scan_settings.content_dir))) <= 0)
goto error;
if (scan_settings.content_dir[_len - 1] == PATH_DEFAULT_SLASH_C())
scan_settings.content_dir[_len - 1] = '\0';
/* Handle case where path was a single slash... */
if (string_is_empty(scan_settings.content_dir))
goto error;
/* Get directory name (used as system name
* when scan_settings.system_name_type ==
* MANUAL_CONTENT_SCAN_SYSTEM_NAME_CONTENT_DIR) */
dir_name = path_basename(scan_settings.content_dir);
if (string_is_empty(dir_name))
goto error;
/* Copy directory name to settings struct */
strlcpy(
scan_settings.system_name_content_dir,
dir_name,
sizeof(scan_settings.system_name_content_dir));
return true;
error:
/* Directory is invalid - reset internal
* content directory and associated 'directory'
* system name */
scan_settings.content_dir[0] = '\0';
scan_settings.system_name_content_dir[0] = '\0';
return false;
}
/* Sets system name for the next manual scan
* operation.
* Returns true if system name is valid.
* NOTE:
* > Only sets 'system_name_type' and 'system_name_database'
* > 'system_name_content_dir' and 'system_name_custom' are
* (by necessity) handled elsewhere
* > This may look fishy, but it's not - it's a menu-specific
* function, and this is simply the cleanest way to handle
* the setting... */
bool manual_content_scan_set_menu_system_name(
enum manual_content_scan_system_name_type system_name_type,
const char *system_name)
{
/* Sanity check */
if (system_name_type > MANUAL_CONTENT_SCAN_SYSTEM_NAME_DATABASE)
goto error;
/* Cache system name 'type' */
scan_settings.system_name_type = system_name_type;
/* Check if we are using a non-database name */
if ((scan_settings.system_name_type == MANUAL_CONTENT_SCAN_SYSTEM_NAME_CONTENT_DIR) ||
(scan_settings.system_name_type == MANUAL_CONTENT_SCAN_SYSTEM_NAME_CUSTOM))
scan_settings.system_name_database[0] = '\0';
else
{
/* We are using a database name... */
if (string_is_empty(system_name))
goto error;
/* Copy database name to settings struct */
strlcpy(
scan_settings.system_name_database,
system_name,
sizeof(scan_settings.system_name_database));
}
return true;
error:
/* Input parameters are invalid - reset internal
* 'system_name_type' and 'system_name_database' */
scan_settings.system_name_type = MANUAL_CONTENT_SCAN_SYSTEM_NAME_CONTENT_DIR;
scan_settings.system_name_database[0] = '\0';
return false;
}
/* Sets core name for the next manual scan
* operation (+ core path and other associated
* parameters).
* Returns true if core name is valid. */
bool manual_content_scan_set_menu_core_name(
enum manual_content_scan_core_type core_type,
const char *core_name)
{
/* Sanity check */
if (core_type > MANUAL_CONTENT_SCAN_CORE_SET)
goto error;
/* Cache core 'type' */
scan_settings.core_type = core_type;
/* Check if we are using core autodetection */
if (scan_settings.core_type == MANUAL_CONTENT_SCAN_CORE_DETECT)
{
scan_settings.core_name[0] = '\0';
scan_settings.core_path[0] = '\0';
scan_settings.file_exts_core[0] = '\0';
}
else
{
size_t i;
core_info_list_t *core_info_list = NULL;
core_info_t *core_info = NULL;
bool core_found = false;
/* We are using a manually set core... */
if (string_is_empty(core_name))
goto error;
/* Get core list */
core_info_get_list(&core_info_list);
if (!core_info_list)
goto error;
/* Search for the specified core name */
for (i = 0; i < core_info_list->count; i++)
{
core_info = NULL;
core_info = core_info_get(core_info_list, i);
if (core_info)
{
if (string_is_equal(core_info->display_name, core_name))
{
/* Core has been found */
core_found = true;
/* Copy core path to settings struct */
if (string_is_empty(core_info->path))
goto error;
strlcpy(
scan_settings.core_path,
core_info->path,
sizeof(scan_settings.core_path));
/* Copy core name to settings struct */
strlcpy(
scan_settings.core_name,
core_info->display_name,
sizeof(scan_settings.core_name));
/* Copy supported extensions to settings
* struct, if required */
if (!string_is_empty(core_info->supported_extensions))
{
strlcpy(
scan_settings.file_exts_core,
core_info->supported_extensions,
sizeof(scan_settings.file_exts_core));
/* Core info extensions are delimited by
* vertical bars. For internal consistency,
* replace them with spaces */
string_replace_all_chars(scan_settings.file_exts_core, '|', ' ');
/* Apply standard scrubbing/clean-up
* (should not be required, but must handle the
* case where a core info file is incorrectly
* formatted) */
manual_content_scan_scrub_file_exts(scan_settings.file_exts_core);
}
else
scan_settings.file_exts_core[0] = '\0';
break;
}
}
}
/* Sanity check */
if (!core_found)
goto error;
}
return true;
error:
/* Input parameters are invalid - reset internal
* core values */
scan_settings.core_type = MANUAL_CONTENT_SCAN_CORE_DETECT;
scan_settings.core_name[0] = '\0';
scan_settings.core_path[0] = '\0';
scan_settings.file_exts_core[0] = '\0';
return false;
}
/* Sets all parameters for the next manual scan
* operation according the to recorded values in
* the specified playlist.
* Returns MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_OK
* if playlist contains a valid scan record. */
enum manual_content_scan_playlist_refresh_status
manual_content_scan_set_menu_from_playlist(playlist_t *playlist,
const char *path_content_database, bool show_hidden_files)
{
const char *playlist_path = NULL;
const char *content_dir = NULL;
const char *core_name = NULL;
const char *file_exts = NULL;
const char *dat_file_path = NULL;
bool search_recursively = false;
bool search_archives = false;
bool filter_dat_content = false;
bool overwrite_playlist = false;
#ifdef HAVE_LIBRETRODB
struct string_list *rdb_list = NULL;
#endif
enum manual_content_scan_system_name_type
system_name_type = MANUAL_CONTENT_SCAN_SYSTEM_NAME_CONTENT_DIR;
enum manual_content_scan_core_type
core_type = MANUAL_CONTENT_SCAN_CORE_DETECT;
enum manual_content_scan_playlist_refresh_status
playlist_status = MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_OK;
char system_name[NAME_MAX_LENGTH];
if (!playlist_scan_refresh_enabled(playlist))
{
playlist_status = MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_MISSING_CONFIG;
goto end;
}
/* Read scan parameters from playlist */
playlist_path = playlist_get_conf_path(playlist);
content_dir = playlist_get_scan_content_dir(playlist);
core_name = playlist_get_default_core_name(playlist);
file_exts = playlist_get_scan_file_exts(playlist);
dat_file_path = playlist_get_scan_dat_file_path(playlist);
search_recursively = playlist_get_scan_search_recursively(playlist);
search_archives = playlist_get_scan_search_archives(playlist);
filter_dat_content = playlist_get_scan_filter_dat_content(playlist);
overwrite_playlist = playlist_get_scan_overwrite_playlist(playlist);
/* Determine system name (playlist basename
* without extension) */
if (string_is_empty(playlist_path))
{
/* Cannot happen, but would constitute a
* 'system name' error */
playlist_status = MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_INVALID_SYSTEM_NAME;
goto end;
}
fill_pathname(system_name, path_basename(playlist_path),
"", sizeof(system_name));
if (string_is_empty(system_name))
{
/* Cannot happen, but would constitute a
* 'system name' error */
playlist_status = MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_INVALID_SYSTEM_NAME;
goto end;
}
/* Set content directory */
if (!manual_content_scan_set_menu_content_dir(content_dir))
{
playlist_status = MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_INVALID_CONTENT_DIR;
goto end;
}
/* Set system name */
#ifdef HAVE_LIBRETRODB
/* > If platform has database support, get names
* of all installed database files */
rdb_list = dir_list_new_special(
path_content_database,
DIR_LIST_DATABASES, NULL, show_hidden_files);
if (rdb_list && rdb_list->size)
{
size_t i;
/* Loop over database files */
for (i = 0; i < rdb_list->size; i++)
{
char rdb_name[NAME_MAX_LENGTH];
const char *rdb_path = rdb_list->elems[i].data;
/* Sanity check */
if (string_is_empty(rdb_path))
continue;
fill_pathname(rdb_name, path_basename(rdb_path), "",
sizeof(rdb_name));
if (string_is_empty(rdb_name))
continue;
/* Check whether playlist system name
* matches current database file */
if (string_is_equal(system_name, rdb_name))
{
system_name_type = MANUAL_CONTENT_SCAN_SYSTEM_NAME_DATABASE;
break;
}
}
}
string_list_free(rdb_list);
#endif
/* > If system name does not match a database
* file, then check whether it matches the
* content directory name */
if (system_name_type !=
MANUAL_CONTENT_SCAN_SYSTEM_NAME_DATABASE)
{
/* system_name_type is set to
* MANUAL_CONTENT_SCAN_SYSTEM_NAME_CONTENT_DIR
* by default - so if a match is found just
* reset 'custom name' field */
if (string_is_equal(system_name,
scan_settings.system_name_content_dir))
scan_settings.system_name_custom[0] = '\0';
else
{
/* Playlist is using a custom system name */
system_name_type = MANUAL_CONTENT_SCAN_SYSTEM_NAME_CUSTOM;
strlcpy(scan_settings.system_name_custom, system_name,
sizeof(scan_settings.system_name_custom));
}
}
if (!manual_content_scan_set_menu_system_name(
system_name_type, system_name))
{
playlist_status = MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_INVALID_SYSTEM_NAME;
goto end;
}
/* Set core path/name */
if ( !string_is_empty(core_name)
&& !string_is_equal(core_name, FILE_PATH_DETECT))
core_type = MANUAL_CONTENT_SCAN_CORE_SET;
if (!manual_content_scan_set_menu_core_name(
core_type, core_name))
{
playlist_status = MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_INVALID_CORE;
goto end;
}
/* Set custom file extensions */
if (string_is_empty(file_exts))
scan_settings.file_exts_custom[0] = '\0';
else
{
strlcpy(scan_settings.file_exts_custom, file_exts,
sizeof(scan_settings.file_exts_custom));
/* File extensions read from playlist should
* be correctly formatted, with '|' characters
* as delimiters
* > For menu purposes, must replace these
* delimiters with space characters
* > Additionally scrub the resultant string,
* to handle the case where a user has
* 'corrupted' it by manually tampering with
* the playlist file */
string_replace_all_chars(scan_settings.file_exts_custom, '|', ' ');
manual_content_scan_scrub_file_exts(scan_settings.file_exts_custom);
}
/* Set DAT file path */
if (string_is_empty(dat_file_path))
scan_settings.dat_file_path[0] = '\0';
else
{
strlcpy(scan_settings.dat_file_path, dat_file_path,
sizeof(scan_settings.dat_file_path));
switch (manual_content_scan_validate_dat_file_path())
{
case MANUAL_CONTENT_SCAN_DAT_FILE_INVALID:
playlist_status = MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_INVALID_DAT_FILE;
goto end;
case MANUAL_CONTENT_SCAN_DAT_FILE_TOO_LARGE:
playlist_status = MANUAL_CONTENT_SCAN_PLAYLIST_REFRESH_DAT_FILE_TOO_LARGE;
goto end;
default:
/* No action required */
break;
}
}
/* Set remaining boolean parameters */
scan_settings.search_recursively = search_recursively;
scan_settings.search_archives = search_archives;
scan_settings.filter_dat_content = filter_dat_content;
scan_settings.overwrite_playlist = overwrite_playlist;
/* When refreshing a playlist:
* > We always validate entries in the
* existing file */
scan_settings.validate_entries = true;
end:
return playlist_status;
}
/* Menu getters */
/* Fetches content directory for next manual scan
* operation.
* Returns true if content directory is valid. */
bool manual_content_scan_get_menu_content_dir(const char **content_dir)
{
if (!content_dir)
return false;
if (string_is_empty(scan_settings.content_dir))
return false;
*content_dir = scan_settings.content_dir;
return true;
}
/* Fetches system name for the next manual scan operation.
* Returns true if system name is valid.
* NOTE: This corresponds to the 'System Name' value
* displayed in menus - this is not identical to the
* actual system name used when generating the playlist */
bool manual_content_scan_get_menu_system_name(const char **system_name)
{
if (system_name)
{
switch (scan_settings.system_name_type)
{
case MANUAL_CONTENT_SCAN_SYSTEM_NAME_CONTENT_DIR:
*system_name = msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_MANUAL_CONTENT_SCAN_SYSTEM_NAME_USE_CONTENT_DIR);
return true;
case MANUAL_CONTENT_SCAN_SYSTEM_NAME_CUSTOM:
*system_name = msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_MANUAL_CONTENT_SCAN_SYSTEM_NAME_USE_CUSTOM);
return true;
case MANUAL_CONTENT_SCAN_SYSTEM_NAME_DATABASE:
if (!string_is_empty(scan_settings.system_name_database))
{
*system_name = scan_settings.system_name_database;
return true;
}
break;
default:
break;
}
}
return false;
}
/* Fetches core name for the next manual scan operation.
* Returns true if core name is valid. */
bool manual_content_scan_get_menu_core_name(const char **core_name)
{
if (core_name)
{
switch (scan_settings.core_type)
{
case MANUAL_CONTENT_SCAN_CORE_DETECT:
*core_name = msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_MANUAL_CONTENT_SCAN_CORE_NAME_DETECT);
return true;
case MANUAL_CONTENT_SCAN_CORE_SET:
if (!string_is_empty(scan_settings.core_name))
{
*core_name = scan_settings.core_name;
return true;
}
break;
default:
break;
}
}
return false;
}
/* Menu utility functions */
/* Creates a list of all possible 'system name' menu
* strings, for use in 'menu_displaylist' drop-down
* lists and 'menu_cbs_left/right'
* > Returns NULL in the event of failure
* > Returned string list must be free()'d */
struct string_list *manual_content_scan_get_menu_system_name_list(
const char *path_content_database, bool show_hidden_files)
{
union string_list_elem_attr attr;
struct string_list *name_list = string_list_new();
/* Sanity check */
if (!name_list)
return NULL;
attr.i = 0;
/* Add 'use content directory' entry */
if (!string_list_append(name_list, msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_MANUAL_CONTENT_SCAN_SYSTEM_NAME_USE_CONTENT_DIR), attr))
goto error;
/* Add 'use custom' entry */
if (!string_list_append(name_list, msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_MANUAL_CONTENT_SCAN_SYSTEM_NAME_USE_CUSTOM), attr))
goto error;
#ifdef HAVE_LIBRETRODB
/* If platform has database support, get names
* of all installed database files */
{
/* Note: dir_list_new_special() is well behaved - the
* returned string list will only include database
* files (i.e. don't have to check for directories,
* or verify file extensions) */
struct string_list *rdb_list = dir_list_new_special(
path_content_database,
DIR_LIST_DATABASES, NULL, show_hidden_files);
if (rdb_list && rdb_list->size)
{
unsigned i;
/* Ensure database list is in alphabetical order */
dir_list_sort(rdb_list, true);
/* Loop over database files */
for (i = 0; i < rdb_list->size; i++)
{
char rdb_name[NAME_MAX_LENGTH];
const char *rdb_path = rdb_list->elems[i].data;
/* Sanity check */
if (string_is_empty(rdb_path))
continue;
fill_pathname(rdb_name, path_basename(rdb_path), "",
sizeof(rdb_name));
if (string_is_empty(rdb_name))
continue;
/* Add database name to list */
if (!string_list_append(name_list, rdb_name, attr))
goto error;
}
}
/* Clean up */
string_list_free(rdb_list);
}
#endif
return name_list;
error:
if (name_list)
string_list_free(name_list);
return NULL;
}
/* Creates a list of all possible 'core name' menu
* strings, for use in 'menu_displaylist' drop-down
* lists and 'menu_cbs_left/right'
* > Returns NULL in the event of failure
* > Returned string list must be free()'d */
struct string_list *manual_content_scan_get_menu_core_name_list(void)
{
union string_list_elem_attr attr;
struct string_list *name_list = string_list_new();
core_info_list_t *core_info_list = NULL;
/* Sanity check */
if (!name_list)
return NULL;
attr.i = 0;
/* Add 'DETECT' entry */
if (!string_list_append(name_list, msg_hash_to_str(
MENU_ENUM_LABEL_VALUE_MANUAL_CONTENT_SCAN_CORE_NAME_DETECT), attr))
goto error;
/* Get core list */
core_info_get_list(&core_info_list);
if (core_info_list)
{
size_t i;
core_info_t *core_info = NULL;
/* Sort cores alphabetically */
core_info_qsort(core_info_list, CORE_INFO_LIST_SORT_DISPLAY_NAME);
/* Loop through cores */
for (i = 0; i < core_info_list->count; i++)
{
if ((core_info = core_info_get(core_info_list, i)))
{
if (string_is_empty(core_info->display_name))
continue;
/* Add core name to list */
if (!string_list_append(name_list, core_info->display_name, attr))
goto error;
}
}
}
return name_list;
error:
if (name_list)
string_list_free(name_list);
return NULL;
}
/****************/
/* Task Helpers */
/****************/
/* Parses current manual content scan settings,
* and extracts all information required to configure
* a manual content scan task.
* Returns false if current settings are invalid. */
bool manual_content_scan_get_task_config(
manual_content_scan_task_config_t *task_config,
const char *path_dir_playlist)
{
size_t _len;
if (!task_config)
return false;
/* Ensure all 'task_config' strings are
* correctly initialised */
task_config->playlist_file[0] = '\0';
task_config->content_dir[0] = '\0';
task_config->system_name[0] = '\0';
task_config->database_name[0] = '\0';
task_config->core_name[0] = '\0';
task_config->core_path[0] = '\0';
task_config->file_exts[0] = '\0';
task_config->dat_file_path[0] = '\0';
/* Get content directory */
if (string_is_empty(scan_settings.content_dir))
return false;
if (!path_is_directory(scan_settings.content_dir))
return false;
strlcpy(
task_config->content_dir,
scan_settings.content_dir,
sizeof(task_config->content_dir));
/* Get system name */
switch (scan_settings.system_name_type)
{
case MANUAL_CONTENT_SCAN_SYSTEM_NAME_CONTENT_DIR:
if (string_is_empty(scan_settings.system_name_content_dir))
return false;
strlcpy(
task_config->system_name,
scan_settings.system_name_content_dir,
sizeof(task_config->system_name));
break;
case MANUAL_CONTENT_SCAN_SYSTEM_NAME_CUSTOM:
if (string_is_empty(scan_settings.system_name_custom))
return false;
strlcpy(
task_config->system_name,
scan_settings.system_name_custom,
sizeof(task_config->system_name));
break;
case MANUAL_CONTENT_SCAN_SYSTEM_NAME_DATABASE:
if (string_is_empty(scan_settings.system_name_database))
return false;
strlcpy(
task_config->system_name,
scan_settings.system_name_database,
sizeof(task_config->system_name));
break;
default:
return false;
}
/* Now we have a valid system name, can generate
* a 'database' name... */
_len = strlcpy(
task_config->database_name,
task_config->system_name,
sizeof(task_config->database_name));
strlcpy(task_config->database_name + _len,
".lpl",
sizeof(task_config->database_name) - _len);
/* ...which can in turn be used to generate the
* playlist path */
if (string_is_empty(path_dir_playlist))
return false;
fill_pathname_join_special(
task_config->playlist_file,