forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retroarch.c
6009 lines (5390 loc) · 200 KB
/
retroarch.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-2021 - Daniel De Matteis
* Copyright (C) 2012-2015 - Michael Lelli
* Copyright (C) 2014-2017 - Jean-Andr� Santoni
* Copyright (C) 2016-2019 - Brad Parker
*
* 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/>.
*/
#ifdef _WIN32
#ifdef _XBOX
#include <xtl.h>
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#if defined(DEBUG) && defined(HAVE_DRMINGW)
#include "exchndl.h"
#endif
#endif
#if defined(DINGUX)
#include <sys/types.h>
#include <unistd.h>
#endif
#if (defined(__linux__) || defined(__unix__) || defined(DINGUX)) && !defined(EMSCRIPTEN)
#include <signal.h>
#endif
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX)
#ifndef LEGACY_WIN32
#define LEGACY_WIN32
#endif
#endif
#if defined(_WIN32) && !defined(_XBOX) && !defined(__WINRT__)
#include <objbase.h>
#include <process.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <locale.h>
#include <boolean.h>
#include <clamping.h>
#include <string/stdstring.h>
#include <dynamic/dylib.h>
#include <file/config_file.h>
#include <lists/string_list.h>
#include <memalign.h>
#include <retro_math.h>
#include <retro_timers.h>
#include <encodings/utf.h>
#include <time/rtime.h>
#include <libretro.h>
#define VFS_FRONTEND
#include <vfs/vfs_implementation.h>
#include <features/features_cpu.h>
#include <compat/strl.h>
#include <compat/strcasestr.h>
#include <compat/getopt.h>
#include <compat/posix_string.h>
#include <file/file_path.h>
#include <retro_assert.h>
#include <retro_miscellaneous.h>
#include <lists/dir_list.h>
#ifdef EMSCRIPTEN
#include <emscripten/emscripten.h>
#endif
#ifdef HAVE_LIBNX
#include <switch.h>
#endif
#if defined(HAVE_LAKKA) || defined(HAVE_LIBNX)
#include "switch_performance_profiles.h"
#endif
#if defined(ANDROID)
#include "play_feature_delivery/play_feature_delivery.h"
#endif
#ifdef HAVE_DISCORD
#include "network/discord.h"
#endif
#include "config.def.h"
#include "runloop.h"
#include "camera/camera_driver.h"
#include "location_driver.h"
#include "record/record_driver.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_NETWORKING
#include <net/net_compat.h>
#include <net/net_socket.h>
#endif
#include <audio/audio_resampler.h>
#include "audio/audio_driver.h"
#ifdef HAVE_GFX_WIDGETS
#include "gfx/gfx_widgets.h"
#endif
#include "input/input_remapping.h"
#ifdef HAVE_CHEEVOS
#include "cheevos/cheevos.h"
#include "cheevos/cheevos_menu.h"
#endif
#ifdef HAVE_TRANSLATE
#include <encodings/base64.h>
#include <formats/rbmp.h>
#include <formats/rpng.h>
#include <formats/rjson.h>
#include "translation_defines.h"
#endif
#ifdef HAVE_NETWORKING
#include "network/netplay/netplay.h"
#include "network/netplay/netplay_private.h"
#ifdef HAVE_WIFI
#include "network/wifi_driver.h"
#endif
#endif
#ifdef HAVE_THREADS
#include <rthreads/rthreads.h>
#endif
#include "autosave.h"
#include "config.features.h"
#include "content.h"
#include "core_info.h"
#include "dynamic.h"
#include "defaults.h"
#include "driver.h"
#include "msg_hash.h"
#include "paths.h"
#include "file_path_special.h"
#include "ui/ui_companion_driver.h"
#include "verbosity.h"
#include "gfx/video_display_server.h"
#ifdef HAVE_BLUETOOTH
#include "bluetooth/bluetooth_driver.h"
#endif
#include "misc/cpufreq/cpufreq.h"
#include "midi_driver.h"
#include "core.h"
#include "configuration.h"
#include "list_special.h"
#ifdef HAVE_CHEATS
#include "cheat_manager.h"
#endif
#include "tasks/task_content.h"
#include "tasks/tasks_internal.h"
#include "version.h"
#include "version_git.h"
#include "retroarch.h"
#include "accessibility.h"
#if defined(HAVE_SDL) || defined(HAVE_SDL2) || defined(HAVE_SDL_DINGUX)
#include "SDL.h"
#endif
#ifdef HAVE_LAKKA
#include "lakka.h"
#endif
#define _PSUPP(var, name, desc) printf(" %s:\n\t\t%s: %s\n", name, desc, var ? "yes" : "no")
#define FAIL_CPU(simd_type) do { \
RARCH_ERR(simd_type " code is compiled in, but CPU does not support this feature. Cannot continue.\n"); \
retroarch_fail(1, "validate_cpu_features()"); \
} while (0)
#define FFMPEG_RECORD_ARG "r:"
#ifdef HAVE_DYNAMIC
#define DYNAMIC_ARG "L:"
#else
#define DYNAMIC_ARG
#endif
#ifdef HAVE_NETWORKING
#define NETPLAY_ARG "HC:F:"
#else
#define NETPLAY_ARG
#endif
#ifdef HAVE_CONFIGFILE
#define CONFIG_FILE_ARG "c:"
#else
#define CONFIG_FILE_ARG
#endif
#ifdef HAVE_BSV_MOVIE
#define BSV_MOVIE_ARG "P:R:M:"
#else
#define BSV_MOVIE_ARG
#endif
#define _PSUPP_BUF(buf, var, name, desc) \
strlcat(buf, " ", sizeof(buf)); \
strlcat(buf, name, sizeof(buf)); \
strlcat(buf, ":\n\t\t", sizeof(buf)); \
strlcat(buf, desc, sizeof(buf)); \
strlcat(buf, ": ", sizeof(buf)); \
strlcat(buf, var ? "yes\n" : "no\n", sizeof(buf))
/* Griffin hack */
#ifdef HAVE_QT
#ifndef HAVE_MAIN
#define HAVE_MAIN
#endif
#endif
/* Descriptive names for options without short variant.
*
* Please keep the name in sync with the option name.
* Order does not matter. */
enum
{
RA_OPT_MENU = 256, /* must be outside the range of a char */
RA_OPT_STATELESS,
RA_OPT_CHECK_FRAMES,
RA_OPT_PORT,
RA_OPT_SPECTATE,
RA_OPT_NICK,
RA_OPT_COMMAND,
RA_OPT_APPENDCONFIG,
RA_OPT_BPS,
RA_OPT_IPS,
RA_OPT_NO_PATCH,
RA_OPT_RECORDCONFIG,
RA_OPT_SUBSYSTEM,
RA_OPT_SIZE,
RA_OPT_FEATURES,
RA_OPT_VERSION,
RA_OPT_EOF_EXIT,
RA_OPT_LOG_FILE,
RA_OPT_MAX_FRAMES,
RA_OPT_MAX_FRAMES_SCREENSHOT,
RA_OPT_MAX_FRAMES_SCREENSHOT_PATH,
RA_OPT_SET_SHADER,
RA_OPT_ACCESSIBILITY,
RA_OPT_LOAD_MENU_ON_ERROR
};
/* DRIVERS */
#ifdef HAVE_BLUETOOTH
extern const bluetooth_driver_t *bluetooth_drivers[];
#endif
/* MAIN GLOBAL VARIABLES */
struct rarch_state
{
char *connect_host; /* Netplay hostname passed from CLI */
struct retro_perf_counter *perf_counters_rarch[MAX_COUNTERS];
#ifdef HAVE_THREAD_STORAGE
sthread_tls_t rarch_tls; /* unsigned alignment */
#endif
unsigned perf_ptr_rarch;
char launch_arguments[4096];
char path_default_shader_preset[PATH_MAX_LENGTH];
char path_content[PATH_MAX_LENGTH];
char path_libretro[PATH_MAX_LENGTH];
char path_config_file[PATH_MAX_LENGTH];
char path_config_append_file[256];
char path_core_options_file[PATH_MAX_LENGTH];
char dir_system[PATH_MAX_LENGTH];
char dir_savefile[PATH_MAX_LENGTH];
char dir_savestate[PATH_MAX_LENGTH];
bool has_set_username;
bool has_set_verbosity;
bool has_set_libretro;
bool has_set_libretro_directory;
bool has_set_save_path;
bool has_set_state_path;
#ifdef HAVE_PATCH
bool has_set_ups_pref;
bool has_set_bps_pref;
bool has_set_ips_pref;
#endif
bool has_set_log_to_file;
bool rarch_ups_pref;
bool rarch_bps_pref;
bool rarch_ips_pref;
#ifdef HAVE_CONFIGFILE
bool rarch_block_config_read;
#endif
};
/* Forward declarations */
#ifdef HAVE_LIBNX
void libnx_apply_overclock(void);
#endif
static struct rarch_state rarch_st = {0};
#ifdef HAVE_THREAD_STORAGE
static const void *MAGIC_POINTER = (void*)(uintptr_t)0x0DEFACED;
#endif
static access_state_t access_state_st = {0};
static struct global global_driver_st = {0}; /* retro_time_t alignment */
access_state_t *access_state_get_ptr(void)
{
return &access_state_st;
}
/* GLOBAL POINTER GETTERS */
global_t *global_get_ptr(void)
{
return &global_driver_st;
}
struct retro_perf_counter **retro_get_perf_counter_rarch(void)
{
struct rarch_state *p_rarch = &rarch_st;
return p_rarch->perf_counters_rarch;
}
unsigned retro_get_perf_count_rarch(void)
{
struct rarch_state *p_rarch = &rarch_st;
return p_rarch->perf_ptr_rarch;
}
void rarch_perf_register(struct retro_perf_counter *perf)
{
struct rarch_state *p_rarch = &rarch_st;
runloop_state_t *runloop_st = runloop_state_get_ptr();
if (
!runloop_st->perfcnt_enable
|| perf->registered
|| p_rarch->perf_ptr_rarch >= MAX_COUNTERS
)
return;
p_rarch->perf_counters_rarch[p_rarch->perf_ptr_rarch++] = perf;
perf->registered = true;
}
struct string_list *dir_list_new_special(const char *input_dir,
enum dir_list_type type, const char *filter,
bool show_hidden_files)
{
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
char ext_shaders[255];
#endif
char ext_name[255];
const char *exts = NULL;
bool recursive = false;
switch (type)
{
case DIR_LIST_AUTOCONFIG:
exts = filter;
break;
case DIR_LIST_CORES:
ext_name[0] = '\0';
if (!frontend_driver_get_core_extension(ext_name, sizeof(ext_name)))
return NULL;
exts = ext_name;
break;
case DIR_LIST_RECURSIVE:
recursive = true;
/* fall-through */
case DIR_LIST_CORE_INFO:
{
core_info_list_t *list = NULL;
core_info_get_list(&list);
if (list)
exts = list->all_ext;
}
break;
case DIR_LIST_SHADERS:
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
{
union string_list_elem_attr attr;
struct string_list str_list;
if (!string_list_initialize(&str_list))
return NULL;
ext_shaders[0] = '\0';
attr.i = 0;
if (video_shader_is_supported(RARCH_SHADER_CG))
{
string_list_append(&str_list, "cgp", attr);
string_list_append(&str_list, "cg", attr);
}
if (video_shader_is_supported(RARCH_SHADER_GLSL))
{
string_list_append(&str_list, "glslp", attr);
string_list_append(&str_list, "glsl", attr);
}
if (video_shader_is_supported(RARCH_SHADER_SLANG))
{
string_list_append(&str_list, "slangp", attr);
string_list_append(&str_list, "slang", attr);
}
string_list_join_concat(ext_shaders, sizeof(ext_shaders), &str_list, "|");
string_list_deinitialize(&str_list);
exts = ext_shaders;
}
break;
#else
return NULL;
#endif
case DIR_LIST_COLLECTIONS:
exts = "lpl";
break;
case DIR_LIST_DATABASES:
exts = "rdb";
break;
case DIR_LIST_PLAIN:
exts = filter;
break;
case DIR_LIST_NONE:
default:
return NULL;
}
return dir_list_new(input_dir, exts, false,
show_hidden_files,
type == DIR_LIST_CORE_INFO, recursive);
}
struct string_list *string_list_new_special(enum string_list_type type,
void *data, unsigned *len, size_t *list_size)
{
union string_list_elem_attr attr;
unsigned i;
struct string_list *s = string_list_new();
if (!s || !len)
goto error;
attr.i = 0;
*len = 0;
switch (type)
{
case STRING_LIST_MENU_DRIVERS:
#ifdef HAVE_MENU
for (i = 0; menu_ctx_drivers[i]; i++)
{
const char *opt = menu_ctx_drivers[i]->ident;
*len += strlen(opt) + 1;
/* Don't allow the user to set menu driver to "null" using the UI.
* Can prevent the user from locking him/herself out of the program. */
if (string_is_not_equal(opt, "null"))
string_list_append(s, opt, attr);
}
break;
#endif
case STRING_LIST_CAMERA_DRIVERS:
for (i = 0; camera_drivers[i]; i++)
{
const char *opt = camera_drivers[i]->ident;
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
}
break;
case STRING_LIST_BLUETOOTH_DRIVERS:
#ifdef HAVE_BLUETOOTH
for (i = 0; bluetooth_drivers[i]; i++)
{
const char *opt = bluetooth_drivers[i]->ident;
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
}
break;
#endif
case STRING_LIST_WIFI_DRIVERS:
#ifdef HAVE_WIFI
for (i = 0; wifi_drivers[i]; i++)
{
const char *opt = wifi_drivers[i]->ident;
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
}
break;
#endif
case STRING_LIST_LOCATION_DRIVERS:
for (i = 0; location_drivers[i]; i++)
{
const char *opt = location_drivers[i]->ident;
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
}
break;
case STRING_LIST_AUDIO_DRIVERS:
for (i = 0; audio_drivers[i]; i++)
{
const char *opt = audio_drivers[i]->ident;
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
}
break;
case STRING_LIST_AUDIO_RESAMPLER_DRIVERS:
for (i = 0; audio_resampler_driver_find_handle(i); i++)
{
const char *opt = audio_resampler_driver_find_ident(i);
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
}
break;
case STRING_LIST_VIDEO_DRIVERS:
for (i = 0; video_drivers[i]; i++)
{
const char *opt = video_drivers[i]->ident;
*len += strlen(opt) + 1;
/* Don't allow the user to set video driver to "null" using the UI.
* Can prevent the user from locking him/herself out of the program. */
if (string_is_not_equal(opt, "null"))
string_list_append(s, opt, attr);
}
break;
case STRING_LIST_INPUT_DRIVERS:
for (i = 0; input_drivers[i]; i++)
{
const char *opt = input_drivers[i]->ident;
*len += strlen(opt) + 1;
/* Don't allow the user to set input driver to "null" using the UI.
* Can prevent the user from locking him/herself out of the program. */
if (string_is_not_equal(opt, "null"))
string_list_append(s, opt, attr);
}
break;
case STRING_LIST_INPUT_HID_DRIVERS:
#ifdef HAVE_HID
for (i = 0; hid_drivers[i]; i++)
{
const char *opt = hid_drivers[i]->ident;
*len += strlen(opt) + 1;
/* Don't allow the user to set input HID driver to "null" using the UI.
* Can prevent the user from locking him/herself out of the program. */
if (string_is_not_equal(opt, "null"))
string_list_append(s, opt, attr);
}
#endif
break;
case STRING_LIST_INPUT_JOYPAD_DRIVERS:
for (i = 0; joypad_drivers[i]; i++)
{
const char *opt = joypad_drivers[i]->ident;
*len += strlen(opt) + 1;
/* Don't allow the user to set input joypad driver to "null" using the UI.
* Can prevent the user from locking him/herself out of the program. */
if (string_is_not_equal(opt, "null"))
string_list_append(s, opt, attr);
}
break;
case STRING_LIST_RECORD_DRIVERS:
for (i = 0; record_drivers[i]; i++)
{
const char *opt = record_drivers[i]->ident;
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
}
break;
case STRING_LIST_MIDI_DRIVERS:
for (i = 0; midi_driver_find_handle(i); i++)
{
const char *opt = midi_drivers[i]->ident;
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
}
break;
#ifdef HAVE_LAKKA
case STRING_LIST_TIMEZONES:
{
const char *opt = DEFAULT_TIMEZONE;
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
FILE *zones_file = popen("grep -v ^# /usr/share/zoneinfo/zone.tab | "
"cut -f3 | "
"sort", "r");
if (zones_file != NULL)
{
char zone_desc[TIMEZONE_LENGTH];
while (fgets(zone_desc, TIMEZONE_LENGTH, zones_file))
{
size_t zone_desc_len = strlen(zone_desc);
if (zone_desc_len > 0)
if (zone_desc[--zone_desc_len] == '\n')
zone_desc[zone_desc_len] = '\0';
if (strlen(zone_desc) > 0)
{
const char *opt = zone_desc;
*len += strlen(opt) + 1;
string_list_append(s, opt, attr);
}
}
pclose(zones_file);
}
}
break;
#endif
case STRING_LIST_NONE:
default:
goto error;
}
return s;
error:
string_list_free(s);
s = NULL;
return NULL;
}
const char *char_list_new_special(enum string_list_type type, void *data)
{
unsigned len = 0;
size_t list_size;
struct string_list *s = string_list_new_special(type, data, &len, &list_size);
char *options = (len > 0) ? (char*)calloc(len, sizeof(char)): NULL;
if (options && s)
string_list_join_concat(options, len, s, "|");
string_list_free(s);
s = NULL;
return options;
}
void retroarch_path_set_redirect(settings_t *settings)
{
char content_dir_name[PATH_MAX_LENGTH];
char new_savefile_dir[PATH_MAX_LENGTH];
char new_savestate_dir[PATH_MAX_LENGTH];
struct rarch_state *p_rarch = &rarch_st;
const char *old_savefile_dir = p_rarch->dir_savefile;
const char *old_savestate_dir = p_rarch->dir_savestate;
runloop_state_t *runloop_st = runloop_state_get_ptr();
struct retro_system_info *system = &runloop_st->system.info;
bool sort_savefiles_enable = settings->bools.sort_savefiles_enable;
bool sort_savefiles_by_content_enable = settings->bools.sort_savefiles_by_content_enable;
bool sort_savestates_enable = settings->bools.sort_savestates_enable;
bool sort_savestates_by_content_enable = settings->bools.sort_savestates_by_content_enable;
bool savefiles_in_content_dir = settings->bools.savefiles_in_content_dir;
bool savestates_in_content_dir = settings->bools.savestates_in_content_dir;
content_dir_name[0] = '\0';
new_savefile_dir[0] = '\0';
new_savestate_dir[0] = '\0';
/* Initialize current save directories
* with the values from the config. */
strlcpy(new_savefile_dir, old_savefile_dir, sizeof(new_savefile_dir));
strlcpy(new_savestate_dir, old_savestate_dir, sizeof(new_savestate_dir));
/* Get content directory name, if per-content-directory
* saves/states are enabled */
if ((sort_savefiles_by_content_enable ||
sort_savestates_by_content_enable) &&
!string_is_empty(runloop_st->runtime_content_path_basename))
fill_pathname_parent_dir_name(content_dir_name,
runloop_st->runtime_content_path_basename,
sizeof(content_dir_name));
if (system && !string_is_empty(system->library_name))
{
#ifdef HAVE_MENU
if (!string_is_equal(system->library_name,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NO_CORE)))
#endif
{
/* Per-core and/or per-content-directory saves */
if ((sort_savefiles_enable || sort_savefiles_by_content_enable)
&& !string_is_empty(old_savefile_dir))
{
/* Append content directory name to save location */
if (sort_savefiles_by_content_enable)
fill_pathname_join(
new_savefile_dir,
old_savefile_dir,
content_dir_name,
sizeof(new_savefile_dir));
/* Append library_name to the save location */
if (sort_savefiles_enable)
fill_pathname_join(
new_savefile_dir,
new_savefile_dir,
system->library_name,
sizeof(new_savefile_dir));
/* If path doesn't exist, try to create it,
* if everything fails revert to the original path. */
if (!path_is_directory(new_savefile_dir))
if (!path_mkdir(new_savefile_dir))
{
RARCH_LOG("%s %s\n",
msg_hash_to_str(MSG_REVERTING_SAVEFILE_DIRECTORY_TO),
old_savefile_dir);
strlcpy(new_savefile_dir, old_savefile_dir, sizeof(new_savefile_dir));
}
}
/* Per-core and/or per-content-directory savestates */
if ((sort_savestates_enable || sort_savestates_by_content_enable)
&& !string_is_empty(old_savestate_dir))
{
/* Append content directory name to savestate location */
if (sort_savestates_by_content_enable)
fill_pathname_join(
new_savestate_dir,
old_savestate_dir,
content_dir_name,
sizeof(new_savestate_dir));
/* Append library_name to the savestate location */
if (sort_savestates_enable)
{
fill_pathname_join(
new_savestate_dir,
new_savestate_dir,
system->library_name,
sizeof(new_savestate_dir));
}
/* If path doesn't exist, try to create it.
* If everything fails, revert to the original path. */
if (!path_is_directory(new_savestate_dir))
if (!path_mkdir(new_savestate_dir))
{
RARCH_LOG("%s %s\n",
msg_hash_to_str(MSG_REVERTING_SAVESTATE_DIRECTORY_TO),
old_savestate_dir);
strlcpy(new_savestate_dir,
old_savestate_dir,
sizeof(new_savestate_dir));
}
}
}
}
/* Set savefile directory if empty to content directory */
if (string_is_empty(new_savefile_dir) || savefiles_in_content_dir)
{
strlcpy(new_savefile_dir,
runloop_st->runtime_content_path_basename,
sizeof(new_savefile_dir));
path_basedir(new_savefile_dir);
if (string_is_empty(new_savefile_dir))
RARCH_LOG("Cannot resolve save file path.\n",
msg_hash_to_str(MSG_REVERTING_SAVEFILE_DIRECTORY_TO),
new_savefile_dir);
else if (sort_savefiles_enable || sort_savefiles_by_content_enable)
RARCH_LOG("Saving files in content directory is set. This overrides other save file directory settings.\n");
}
/* Set savestate directory if empty based on content directory */
if (string_is_empty(new_savestate_dir) || savestates_in_content_dir)
{
strlcpy(new_savestate_dir,
runloop_st->runtime_content_path_basename,
sizeof(new_savestate_dir));
path_basedir(new_savestate_dir);
if (string_is_empty(new_savestate_dir))
RARCH_LOG("Cannot resolve save state file path.\n",
msg_hash_to_str(MSG_REVERTING_SAVESTATE_DIRECTORY_TO),
new_savestate_dir);
else if (sort_savestates_enable || sort_savestates_by_content_enable)
RARCH_LOG("Saving save states in content directory is set. This overrides other save state file directory settings.\n");
}
if (system && !string_is_empty(system->library_name))
{
bool savefile_is_dir = path_is_directory(new_savefile_dir);
bool savestate_is_dir = path_is_directory(new_savestate_dir);
if (savefile_is_dir)
strlcpy(runloop_st->name.savefile, new_savefile_dir,
sizeof(runloop_st->name.savefile));
else
savefile_is_dir = path_is_directory(runloop_st->name.savefile);
if (savestate_is_dir)
strlcpy(runloop_st->name.savestate, new_savestate_dir,
sizeof(runloop_st->name.savestate));
else
savestate_is_dir = path_is_directory(runloop_st->name.savestate);
if (savefile_is_dir)
{
fill_pathname_dir(runloop_st->name.savefile,
!string_is_empty(runloop_st->runtime_content_path_basename)
? runloop_st->runtime_content_path_basename
: system->library_name,
FILE_PATH_SRM_EXTENSION,
sizeof(runloop_st->name.savefile));
RARCH_LOG("[Overrides]: %s \"%s\".\n",
msg_hash_to_str(MSG_REDIRECTING_SAVEFILE_TO),
runloop_st->name.savefile);
}
if (savestate_is_dir)
{
fill_pathname_dir(runloop_st->name.savestate,
!string_is_empty(runloop_st->runtime_content_path_basename)
? runloop_st->runtime_content_path_basename
: system->library_name,
FILE_PATH_STATE_EXTENSION,
sizeof(runloop_st->name.savestate));
RARCH_LOG("[Overrides]: %s \"%s\".\n",
msg_hash_to_str(MSG_REDIRECTING_SAVESTATE_TO),
runloop_st->name.savestate);
}
#ifdef HAVE_CHEATS
if (path_is_directory(runloop_st->name.cheatfile))
{
fill_pathname_dir(runloop_st->name.cheatfile,
!string_is_empty(runloop_st->runtime_content_path_basename)
? runloop_st->runtime_content_path_basename
: system->library_name,
FILE_PATH_CHT_EXTENSION,
sizeof(runloop_st->name.cheatfile));
RARCH_LOG("[Overrides]: %s \"%s\".\n",
msg_hash_to_str(MSG_REDIRECTING_CHEATFILE_TO),
runloop_st->name.cheatfile);
}
#endif
}
dir_set(RARCH_DIR_CURRENT_SAVEFILE, new_savefile_dir);
dir_set(RARCH_DIR_CURRENT_SAVESTATE, new_savestate_dir);
}
void path_set_special(char **argv, unsigned num_content)
{
unsigned i;
char str[PATH_MAX_LENGTH];
union string_list_elem_attr attr;
bool is_dir = false;
struct string_list subsystem_paths = {0};
runloop_state_t *runloop_st = runloop_state_get_ptr();
const char *savestate_dir = runloop_st->savestate_dir;
/* First content file is the significant one. */
runloop_path_set_basename(argv[0]);
string_list_initialize(&subsystem_paths);
runloop_st->subsystem_fullpaths = string_list_new();
retro_assert(runloop_st->subsystem_fullpaths);
attr.i = 0;
for (i = 0; i < num_content; i++)
{
string_list_append(runloop_st->subsystem_fullpaths, argv[i], attr);
strlcpy(str, argv[i], sizeof(str));
path_remove_extension(str);
string_list_append(&subsystem_paths, path_basename(str), attr);
}
str[0] = '\0';
string_list_join_concat(str, sizeof(str), &subsystem_paths, " + ");
string_list_deinitialize(&subsystem_paths);
/* We defer SRAM path updates until we can resolve it.
* It is more complicated for special content types. */
is_dir = path_is_directory(savestate_dir);
if (is_dir)
strlcpy(runloop_st->name.savestate, savestate_dir,
sizeof(runloop_st->name.savestate));
else
is_dir = path_is_directory(runloop_st->name.savestate);
if (is_dir)
{
fill_pathname_dir(runloop_st->name.savestate,
str,
".state",
sizeof(runloop_st->name.savestate));
RARCH_LOG("%s \"%s\".\n",
msg_hash_to_str(MSG_REDIRECTING_SAVESTATE_TO),
runloop_st->name.savestate);
}
}
char *path_get_ptr(enum rarch_path_type type)
{
struct rarch_state *p_rarch = &rarch_st;
runloop_state_t *runloop_st = runloop_state_get_ptr();
switch (type)
{
case RARCH_PATH_CONTENT:
return p_rarch->path_content;
case RARCH_PATH_DEFAULT_SHADER_PRESET:
return p_rarch->path_default_shader_preset;
case RARCH_PATH_BASENAME:
return runloop_st->runtime_content_path_basename;
case RARCH_PATH_CORE_OPTIONS:
if (!path_is_empty(RARCH_PATH_CORE_OPTIONS))
return p_rarch->path_core_options_file;
break;
case RARCH_PATH_SUBSYSTEM:
return runloop_st->subsystem_path;
case RARCH_PATH_CONFIG:
if (!path_is_empty(RARCH_PATH_CONFIG))
return p_rarch->path_config_file;
break;
case RARCH_PATH_CONFIG_APPEND:
if (!path_is_empty(RARCH_PATH_CONFIG_APPEND))
return p_rarch->path_config_append_file;
break;
case RARCH_PATH_CORE:
return p_rarch->path_libretro;
case RARCH_PATH_NONE:
case RARCH_PATH_NAMES:
break;
}
return NULL;
}
const char *path_get(enum rarch_path_type type)
{
struct rarch_state *p_rarch = &rarch_st;
runloop_state_t *runloop_st = runloop_state_get_ptr();
switch (type)
{