forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.c
1775 lines (1488 loc) · 46 KB
/
content.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-2016 - 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 <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#ifdef _WIN32
#ifdef _XBOX
#include <xtl.h>
#define setmode _setmode
#define INVALID_FILE_ATTRIBUTES -1
#else
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#endif
#endif
#include <boolean.h>
#include <encodings/utf.h>
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <file/file_path.h>
#ifdef HAVE_COMPRESSION
#include <file/file_archive.h>
#endif
#include <string/stdstring.h>
#include <retro_miscellaneous.h>
#include <retro_file.h>
#include <retro_stat.h>
#include <retro_assert.h>
#include <string/string_list.h>
#include <string/stdstring.h>
#include "msg_hash.h"
#include "content.h"
#include "general.h"
#include "dynamic.h"
#include "movie.h"
#include "patch.h"
#include "system.h"
#include "retroarch.h"
#include "command_event.h"
#include "libretro_version_1.h"
#include "verbosity.h"
#ifdef HAVE_7ZIP
#include "deps/7zip/7z.h"
#include "deps/7zip/7zAlloc.h"
#include "deps/7zip/7zCrc.h"
#include "deps/7zip/7zFile.h"
#endif
#ifdef HAVE_MENU
#include "menu/menu_driver.h"
#endif
#ifdef HAVE_CHEEVOS
#include "cheevos.h"
#endif
#define MAX_ARGS 32
struct sram_block
{
unsigned type;
void *data;
size_t size;
};
#ifdef HAVE_COMPRESSION
#ifdef HAVE_7ZIP
static bool utf16_to_char(uint8_t **utf_data,
size_t *dest_len, const uint16_t *in)
{
unsigned len = 0;
while (in[len] != '\0')
len++;
utf16_conv_utf8(NULL, dest_len, in, len);
*dest_len += 1;
*utf_data = (uint8_t*)malloc(*dest_len);
if (*utf_data == 0)
return false;
return utf16_conv_utf8(*utf_data, dest_len, in, len);
}
static bool utf16_to_char_string(const uint16_t *in, char *s, size_t len)
{
size_t dest_len = 0;
uint8_t *utf16_data = NULL;
bool ret = utf16_to_char(&utf16_data, &dest_len, in);
if (ret)
{
utf16_data[dest_len] = 0;
strlcpy(s, (const char*)utf16_data, len);
}
free(utf16_data);
utf16_data = NULL;
return ret;
}
/* Extract the relative path (needle) from a 7z archive
* (path) and allocate a buf for it to write it in.
* If optional_outfile is set, extract to that instead
* and don't allocate buffer.
*/
static int content_7zip_file_read(
const char *path,
const char *needle, void **buf,
const char *optional_outfile)
{
CFileInStream archiveStream;
CLookToRead lookStream;
CSzArEx db;
ISzAlloc allocImp;
ISzAlloc allocTempImp;
uint8_t *output = 0;
long outsize = -1;
/*These are the allocation routines.
* Currently using the non-standard 7zip choices. */
allocImp.Alloc = SzAlloc;
allocImp.Free = SzFree;
allocTempImp.Alloc = SzAllocTemp;
allocTempImp.Free = SzFreeTemp;
if (InFile_Open(&archiveStream.file, path))
{
RARCH_ERR("Could not open %s as 7z archive\n.", path);
return -1;
}
FileInStream_CreateVTable(&archiveStream);
LookToRead_CreateVTable(&lookStream, False);
lookStream.realStream = &archiveStream.s;
LookToRead_Init(&lookStream);
CrcGenerateTable();
SzArEx_Init(&db);
if (SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp) == SZ_OK)
{
uint32_t i;
bool file_found = false;
uint16_t *temp = NULL;
size_t temp_size = 0;
uint32_t block_index = 0xFFFFFFFF;
SRes res = SZ_OK;
for (i = 0; i < db.db.NumFiles; i++)
{
size_t len;
char infile[PATH_MAX_LENGTH];
size_t offset = 0;
size_t outSizeProcessed = 0;
const CSzFileItem *f = db.db.Files + i;
/* We skip over everything which is not a directory.
* FIXME: Why continue then if f->IsDir is true?*/
if (f->IsDir)
continue;
len = SzArEx_GetFileNameUtf16(&db, i, NULL);
if (len > temp_size)
{
free(temp);
temp_size = len;
temp = (uint16_t *)malloc(temp_size * sizeof(temp[0]));
if (temp == 0)
{
res = SZ_ERROR_MEM;
break;
}
}
SzArEx_GetFileNameUtf16(&db, i, temp);
res = utf16_to_char_string(temp, infile, sizeof(infile))
? SZ_OK : SZ_ERROR_FAIL;
if (string_is_equal(infile, needle))
{
size_t output_size = 0;
RARCH_LOG_OUTPUT("Opened archive %s. Now trying to extract %s\n",
path, needle);
/* C LZMA SDK does not support chunked extraction - see here:
* sourceforge.net/p/sevenzip/discussion/45798/thread/6fb59aaf/
* */
file_found = true;
res = SzArEx_Extract(&db, &lookStream.s, i, &block_index,
&output, &output_size, &offset, &outSizeProcessed,
&allocImp, &allocTempImp);
if (res != SZ_OK)
break; /* This goes to the error section. */
outsize = outSizeProcessed;
if (optional_outfile != NULL)
{
const void *ptr = (const void*)(output + offset);
if (!retro_write_file(optional_outfile, ptr, outsize))
{
RARCH_ERR("Could not open outfilepath %s.\n",
optional_outfile);
res = SZ_OK;
file_found = true;
outsize = -1;
}
}
else
{
/*We could either use the 7Zip allocated buffer,
* or create our own and use it.
* We would however need to realloc anyways, because RetroArch
* expects a \0 at the end, therefore we allocate new,
* copy and free the old one. */
*buf = malloc(outsize + 1);
((char*)(*buf))[outsize] = '\0';
memcpy(*buf,output + offset,outsize);
}
break;
}
}
free(temp);
IAlloc_Free(&allocImp, output);
if (!(file_found && res == SZ_OK))
{
/* Error handling */
if (!file_found)
RARCH_ERR("File %s not found in %s\n", needle, path);
RARCH_ERR("Failed to open compressed file inside 7zip archive.\n");
outsize = -1;
}
}
SzArEx_Free(&db, &allocImp);
File_Close(&archiveStream.file);
return outsize;
}
static struct string_list *compressed_7zip_file_list_new(
const char *path, const char* ext)
{
CFileInStream archiveStream;
CLookToRead lookStream;
CSzArEx db;
ISzAlloc allocImp;
ISzAlloc allocTempImp;
size_t temp_size = 0;
struct string_list *list = NULL;
/* These are the allocation routines - currently using
* the non-standard 7zip choices. */
allocImp.Alloc = SzAlloc;
allocImp.Free = SzFree;
allocTempImp.Alloc = SzAllocTemp;
allocTempImp.Free = SzFreeTemp;
if (InFile_Open(&archiveStream.file, path))
{
RARCH_ERR("Could not open %s as 7z archive.\n",path);
return NULL;
}
list = string_list_new();
if (!list)
{
File_Close(&archiveStream.file);
return NULL;
}
FileInStream_CreateVTable(&archiveStream);
LookToRead_CreateVTable(&lookStream, False);
lookStream.realStream = &archiveStream.s;
LookToRead_Init(&lookStream);
CrcGenerateTable();
SzArEx_Init(&db);
if (SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp) == SZ_OK)
{
uint32_t i;
struct string_list *ext_list = ext ? string_split(ext, "|"): NULL;
SRes res = SZ_OK;
uint16_t *temp = NULL;
for (i = 0; i < db.db.NumFiles; i++)
{
union string_list_elem_attr attr;
char infile[PATH_MAX_LENGTH];
const char *file_ext = NULL;
size_t len = 0;
bool supported_by_core = false;
const CSzFileItem *f = db.db.Files + i;
/* we skip over everything, which is a directory. */
if (f->IsDir)
continue;
len = SzArEx_GetFileNameUtf16(&db, i, NULL);
if (len > temp_size)
{
free(temp);
temp_size = len;
temp = (uint16_t *)malloc(temp_size * sizeof(temp[0]));
if (temp == 0)
{
res = SZ_ERROR_MEM;
break;
}
}
SzArEx_GetFileNameUtf16(&db, i, temp);
res = utf16_to_char_string(temp, infile, sizeof(infile))
? SZ_OK : SZ_ERROR_FAIL;
file_ext = path_get_extension(infile);
if (string_list_find_elem_prefix(ext_list, ".", file_ext))
supported_by_core = true;
/*
* Currently we only support files without subdirs in the archives.
* Folders are not supported (differences between win and lin.
* Archives within archives should imho never be supported.
*/
if (!supported_by_core)
continue;
attr.i = RARCH_COMPRESSED_FILE_IN_ARCHIVE;
if (!string_list_append(list, infile, attr))
{
res = SZ_ERROR_MEM;
break;
}
}
string_list_free(ext_list);
free(temp);
if (res != SZ_OK)
{
/* Error handling */
RARCH_ERR("Failed to open compressed_file: \"%s\"\n", path);
string_list_free(list);
list = NULL;
}
}
SzArEx_Free(&db, &allocImp);
File_Close(&archiveStream.file);
return list;
}
#endif
#ifdef HAVE_ZLIB
struct decomp_state
{
char *opt_file;
char *needle;
void **buf;
size_t size;
bool found;
};
static bool content_zip_file_decompressed_handle(
file_archive_file_handle_t *handle,
const uint8_t *cdata, uint32_t csize,
uint32_t size, uint32_t crc32)
{
int ret = 0;
handle->backend = file_archive_get_default_file_backend();
if (!handle->backend)
goto error;
if (!handle->backend->stream_decompress_data_to_file_init(
handle, cdata, csize, size))
return false;
do{
ret = handle->backend->stream_decompress_data_to_file_iterate(
handle->stream);
}while(ret == 0);
handle->real_checksum = handle->backend->stream_crc_calculate(0,
handle->data, size);
if (handle->real_checksum != crc32)
{
RARCH_ERR("Inflated checksum did not match CRC32!\n");
goto error;
}
if (handle->stream)
free(handle->stream);
return true;
error:
if (handle->stream)
free(handle->stream);
if (handle->data)
free(handle->data);
return false;
}
/* Extract the relative path (needle) from a
* ZIP archive (path) and allocate a buffer for it to write it in.
*
* optional_outfile if not NULL will be used to extract the file to.
* buf will be 0 then.
*/
static int content_zip_file_decompressed(
const char *name, const char *valid_exts,
const uint8_t *cdata, unsigned cmode,
uint32_t csize, uint32_t size,
uint32_t crc32, void *userdata)
{
struct decomp_state *st = (struct decomp_state*)userdata;
/* Ignore directories. */
if (name[strlen(name) - 1] == '/' || name[strlen(name) - 1] == '\\')
return 1;
RARCH_LOG("[deflate] Path: %s, CRC32: 0x%x\n", name, crc32);
if (strstr(name, st->needle))
{
bool goto_error = false;
file_archive_file_handle_t handle = {0};
st->found = true;
if (content_zip_file_decompressed_handle(&handle,
cdata, csize, size, crc32))
{
if (st->opt_file != 0)
{
/* Called in case core has need_fullpath enabled. */
char *buf = (char*)malloc(size);
if (buf)
{
RARCH_LOG("Extracting file : %s\n", st->opt_file);
memcpy(buf, handle.data, size);
if (!retro_write_file(st->opt_file, buf, size))
goto_error = true;
}
free(buf);
st->size = 0;
}
else
{
/* Called in case core has need_fullpath disabled.
* Will copy decompressed content directly into
* RetroArch's ROM buffer. */
*st->buf = malloc(size);
memcpy(*st->buf, handle.data, size);
st->size = size;
}
}
if (handle.data)
free(handle.data);
if (goto_error)
return 0;
}
return 1;
}
static int content_zip_file_read(
const char *path,
const char *needle, void **buf,
const char* optional_outfile)
{
file_archive_transfer_t zlib;
struct decomp_state st;
bool returnerr = true;
int ret = 0;
zlib.type = ZLIB_TRANSFER_INIT;
st.needle = NULL;
st.opt_file = NULL;
st.found = false;
st.buf = buf;
if (needle)
st.needle = strdup(needle);
if (optional_outfile)
st.opt_file = strdup(optional_outfile);
do
{
ret = file_archive_parse_file_iterate(&zlib, &returnerr, path,
"", content_zip_file_decompressed, &st);
if (!returnerr)
break;
}while(ret == 0 && !st.found);
file_archive_parse_file_iterate_stop(&zlib);
if (st.opt_file)
free(st.opt_file);
if (st.needle)
free(st.needle);
if (!st.found)
return -1;
return st.size;
}
#endif
#endif
#ifdef HAVE_COMPRESSION
/* Generic compressed file loader.
* Extracts to buf, unless optional_filename != 0
* Then extracts to optional_filename and leaves buf alone.
*/
static int content_file_compressed_read(
const char * path, void **buf,
const char* optional_filename, ssize_t *length)
{
int ret = 0;
const char* file_ext = NULL;
struct string_list *str_list = string_split(path, "#");
/* Safety check.
* If optional_filename and optional_filename
* exists, we simply return 0,
* hoping that optional_filename is the
* same as requested.
*/
if (optional_filename && path_file_exists(optional_filename))
{
*length = 0;
return 1;
}
/* We assure that there is something after the '#' symbol.
*
* This error condition happens for example, when
* path = /path/to/file.7z, or
* path = /path/to/file.7z#
*/
if (str_list->size <= 1)
goto error;
#if defined(HAVE_7ZIP) || defined(HAVE_ZLIB)
file_ext = path_get_extension(str_list->elems[0].data);
#endif
#ifdef HAVE_7ZIP
if (string_is_equal_noncase(file_ext, "7z"))
{
*length = content_7zip_file_read(str_list->elems[0].data,
str_list->elems[1].data, buf, optional_filename);
if (*length != -1)
ret = 1;
}
#endif
#ifdef HAVE_ZLIB
if (string_is_equal_noncase(file_ext, "zip"))
{
*length = content_zip_file_read(str_list->elems[0].data,
str_list->elems[1].data, buf, optional_filename);
if (*length != -1)
ret = 1;
}
string_list_free(str_list);
#endif
return ret;
error:
RARCH_ERR("Could not extract string and substring from "
": %s.\n", path);
string_list_free(str_list);
*length = 0;
return 0;
}
#endif
/**
* content_file_read:
* @path : path to file.
* @buf : buffer to allocate and read the contents of the
* file into. Needs to be freed manually.
* @length : Number of items read, -1 on error.
*
* Read the contents of a file into @buf. Will call content_file_compressed_read
* if path contains a compressed file, otherwise will call retro_read_file().
*
* Returns: 1 if file read, 0 on error.
*/
static int content_file_read(const char *path, void **buf, ssize_t *length)
{
#ifdef HAVE_COMPRESSION
if (path_contains_compressed_file(path))
{
if (content_file_compressed_read(path, buf, NULL, length))
return 1;
}
#endif
return retro_read_file(path, buf, length);
}
struct string_list *compressed_file_list_new(const char *path,
const char* ext)
{
#if defined(HAVE_ZLIB) || defined(HAVE_7ZIP)
const char* file_ext = path_get_extension(path);
#endif
#ifdef HAVE_7ZIP
if (string_is_equal_noncase(file_ext, "7z"))
return compressed_7zip_file_list_new(path,ext);
#endif
#ifdef HAVE_ZLIB
if (string_is_equal_noncase(file_ext, "zip"))
return file_archive_get_file_list(path, ext);
#endif
return NULL;
}
static void check_defaults_dir_create_dir(const char *path)
{
if (path_is_directory(path))
return;
path_mkdir(path);
}
static void check_defaults_dirs(void)
{
if (*g_defaults.dir.core_assets)
check_defaults_dir_create_dir(g_defaults.dir.core_assets);
if (*g_defaults.dir.remap)
check_defaults_dir_create_dir(g_defaults.dir.remap);
if (*g_defaults.dir.screenshot)
check_defaults_dir_create_dir(g_defaults.dir.screenshot);
if (*g_defaults.dir.core)
check_defaults_dir_create_dir(g_defaults.dir.core);
if (*g_defaults.dir.autoconfig)
check_defaults_dir_create_dir(g_defaults.dir.autoconfig);
if (*g_defaults.dir.audio_filter)
check_defaults_dir_create_dir(g_defaults.dir.audio_filter);
if (*g_defaults.dir.video_filter)
check_defaults_dir_create_dir(g_defaults.dir.video_filter);
if (*g_defaults.dir.assets)
check_defaults_dir_create_dir(g_defaults.dir.assets);
if (*g_defaults.dir.playlist)
check_defaults_dir_create_dir(g_defaults.dir.playlist);
if (*g_defaults.dir.core)
check_defaults_dir_create_dir(g_defaults.dir.core);
if (*g_defaults.dir.core_info)
check_defaults_dir_create_dir(g_defaults.dir.core_info);
if (*g_defaults.dir.overlay)
check_defaults_dir_create_dir(g_defaults.dir.overlay);
if (*g_defaults.dir.port)
check_defaults_dir_create_dir(g_defaults.dir.port);
if (*g_defaults.dir.shader)
check_defaults_dir_create_dir(g_defaults.dir.shader);
if (*g_defaults.dir.savestate)
check_defaults_dir_create_dir(g_defaults.dir.savestate);
if (*g_defaults.dir.sram)
check_defaults_dir_create_dir(g_defaults.dir.sram);
if (*g_defaults.dir.system)
check_defaults_dir_create_dir(g_defaults.dir.system);
if (*g_defaults.dir.resampler)
check_defaults_dir_create_dir(g_defaults.dir.resampler);
if (*g_defaults.dir.menu_config)
check_defaults_dir_create_dir(g_defaults.dir.menu_config);
if (*g_defaults.dir.content_history)
check_defaults_dir_create_dir(g_defaults.dir.content_history);
if (*g_defaults.dir.cache)
check_defaults_dir_create_dir(g_defaults.dir.cache);
if (*g_defaults.dir.database)
check_defaults_dir_create_dir(g_defaults.dir.database);
if (*g_defaults.dir.cursor)
check_defaults_dir_create_dir(g_defaults.dir.cursor);
if (*g_defaults.dir.cheats)
check_defaults_dir_create_dir(g_defaults.dir.cheats);
}
void content_push_to_history_playlist(bool do_push,
const char *path, void *data)
{
settings_t *settings = config_get_ptr();
struct retro_system_info *info = (struct retro_system_info*)data;
/* If the history list is not enabled, early return. */
if (!settings->history_list_enable)
return;
if (!g_defaults.history)
return;
if (!do_push)
return;
content_playlist_push(g_defaults.history,
path,
NULL,
settings->libretro,
info->library_name,
NULL,
NULL);
}
/**
* content_load_init_wrap:
* @args : Input arguments.
* @argc : Count of arguments.
* @argv : Arguments.
*
* Generates an @argc and @argv pair based on @args
* of type rarch_main_wrap.
**/
static void content_load_init_wrap(
const struct rarch_main_wrap *args,
int *argc, char **argv)
{
#ifdef HAVE_FILE_LOGGER
int i;
#endif
*argc = 0;
argv[(*argc)++] = strdup("retroarch");
if (!args->no_content)
{
if (args->content_path)
{
RARCH_LOG("Using content: %s.\n", args->content_path);
argv[(*argc)++] = strdup(args->content_path);
}
#ifdef HAVE_MENU
else
{
RARCH_LOG("No content, starting dummy core.\n");
argv[(*argc)++] = strdup("--menu");
}
#endif
}
if (args->sram_path)
{
argv[(*argc)++] = strdup("-s");
argv[(*argc)++] = strdup(args->sram_path);
}
if (args->state_path)
{
argv[(*argc)++] = strdup("-S");
argv[(*argc)++] = strdup(args->state_path);
}
if (args->config_path)
{
argv[(*argc)++] = strdup("-c");
argv[(*argc)++] = strdup(args->config_path);
}
#ifdef HAVE_DYNAMIC
if (args->libretro_path)
{
argv[(*argc)++] = strdup("-L");
argv[(*argc)++] = strdup(args->libretro_path);
}
#endif
if (args->verbose)
argv[(*argc)++] = strdup("-v");
#ifdef HAVE_FILE_LOGGER
for (i = 0; i < *argc; i++)
RARCH_LOG("arg #%d: %s\n", i, argv[i]);
#endif
}
/**
* content_load:
*
* Loads content file and starts up RetroArch.
* If no content file can be loaded, will start up RetroArch
* as-is.
*
* Returns: false (0) if rarch_main_init failed, otherwise true (1).
**/
static bool content_load(content_ctx_info_t *info)
{
unsigned i;
bool retval = true;
int rarch_argc = 0;
char *rarch_argv[MAX_ARGS] = {NULL};
char *argv_copy [MAX_ARGS] = {NULL};
char **rarch_argv_ptr = (char**)info->argv;
int *rarch_argc_ptr = (int*)&info->argc;
struct rarch_main_wrap *wrap_args = (struct rarch_main_wrap*)
calloc(1, sizeof(*wrap_args));
if (!wrap_args)
return false;
retro_assert(wrap_args);
if (info->environ_get)
info->environ_get(rarch_argc_ptr,
rarch_argv_ptr, info->args, wrap_args);
if (wrap_args->touched)
{
content_load_init_wrap(wrap_args, &rarch_argc, rarch_argv);
memcpy(argv_copy, rarch_argv, sizeof(rarch_argv));
rarch_argv_ptr = (char**)rarch_argv;
rarch_argc_ptr = (int*)&rarch_argc;
}
rarch_ctl(RARCH_CTL_MAIN_DEINIT, NULL);
wrap_args->argc = *rarch_argc_ptr;
wrap_args->argv = rarch_argv_ptr;
if (!rarch_ctl(RARCH_CTL_MAIN_INIT, wrap_args))
{
retval = false;
goto error;
}
#ifdef HAVE_MENU
menu_driver_ctl(RARCH_MENU_CTL_SHADER_MANAGER_INIT, NULL);
#endif
event_cmd_ctl(EVENT_CMD_HISTORY_INIT, NULL);
event_cmd_ctl(EVENT_CMD_RESUME, NULL);
event_cmd_ctl(EVENT_CMD_VIDEO_SET_ASPECT_RATIO, NULL);
check_defaults_dirs();
frontend_driver_process_args(rarch_argc_ptr, rarch_argv_ptr);
frontend_driver_content_loaded();
error:
for (i = 0; i < ARRAY_SIZE(argv_copy); i++)
free(argv_copy[i]);
free(wrap_args);
return retval;
}
/**
* read_content_file:
* @path : buffer of the content file.
* @buf : size of the content file.
* @length : size of the content file that has been read from.
*
* Read the content file. If read into memory, also performs soft patching
* (see patch_content function) in case soft patching has not been
* blocked by the enduser.
*
* Returns: true if successful, false on error.
**/
static bool read_content_file(unsigned i, const char *path, void **buf,
ssize_t *length)
{
#ifdef HAVE_ZLIB
content_stream_t stream_info;
uint32_t *content_crc_ptr = NULL;
#endif
uint8_t *ret_buf = NULL;
global_t *global = global_get_ptr();
RARCH_LOG("%s: %s.\n",
msg_hash_to_str(MSG_LOADING_CONTENT_FILE), path);
if (!content_file_read(path, (void**) &ret_buf, length))
return false;
if (*length < 0)
return false;
if (i != 0)
return true;
/* Attempt to apply a patch. */
if (!global->patch.block_patch)
patch_content(&ret_buf, length);
#ifdef HAVE_ZLIB
content_ctl(CONTENT_CTL_GET_CRC, &content_crc_ptr);
stream_info.a = 0;
stream_info.b = ret_buf;
stream_info.c = *length;
content_ctl(CONTENT_CTL_STREAM_CRC_CALCULATE, &stream_info);
*content_crc_ptr = stream_info.crc;
RARCH_LOG("CRC32: 0x%x .\n", (unsigned)*content_crc_ptr);
#endif
*buf = ret_buf;
return true;
}
/**
* dump_to_file_desperate:
* @data : pointer to data buffer.
* @size : size of @data.
* @type : type of file to be saved.
*
* Attempt to save valuable RAM data somewhere.
**/
static bool dump_to_file_desperate(const void *data,
size_t size, unsigned type)
{
time_t time_;
char timebuf[256];
char path[PATH_MAX_LENGTH];
#if defined(_WIN32) && !defined(_XBOX)
const char *base = getenv("APPDATA");
#elif defined(__CELLOS_LV2__) || defined(_XBOX)
const char *base = NULL;
#else
const char *base = getenv("HOME");
#endif
if (!base)
return false;
snprintf(path, sizeof(path), "%s/RetroArch-recovery-%u", base, type);
time(&time_);
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d-%H-%M-%S", localtime(&time_));
strlcat(path, timebuf, sizeof(path));
if (!retro_write_file(path, data, size))
return false;
RARCH_WARN("Succeeded in saving RAM data to \"%s\".\n", path);
return true;
}
/**
* save_state:
* @path : path of saved state that shall be written to.
*
* Save a state from memory to disk.
*