forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
task_save.c
1531 lines (1251 loc) · 37.4 KB
/
task_save.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) 2011-2017 - Daniel De Matteis
* Copyright (C) 2016-2017 - 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/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include <errno.h>
#include <compat/strl.h>
#include <retro_assert.h>
#include <lists/string_list.h>
#include <streams/file_stream.h>
#include <rthreads/rthreads.h>
#include <file/file_path.h>
#include <retro_miscellaneous.h>
#include <string/stdstring.h>
#ifdef HAVE_CONFIG_H
#include "../core.h"
#endif
#ifdef HAVE_NETWORKING
#include "../network/netplay/netplay.h"
#endif
#include "../core.h"
#include "../file_path_special.h"
#include "../configuration.h"
#include "../gfx/video_driver.h"
#include "../msg_hash.h"
#include "../retroarch.h"
#include "../verbosity.h"
#include "tasks_internal.h"
#define SAVE_STATE_CHUNK 4096
static struct string_list *task_save_files = NULL;
struct ram_type
{
const char *path;
int type;
};
struct save_state_buf
{
void* data;
char path[PATH_MAX_LENGTH];
size_t size;
};
struct sram_block
{
unsigned type;
void *data;
size_t size;
};
typedef struct
{
RFILE *file;
char path[PATH_MAX_LENGTH];
void *data;
void *undo_data;
ssize_t size;
ssize_t undo_size;
ssize_t written;
ssize_t bytes_read;
bool load_to_backup_buffer;
bool autoload;
bool autosave;
bool undo_save;
bool mute;
int state_slot;
bool thumbnail_enable;
bool has_valid_framebuffer;
} save_task_state_t;
typedef save_task_state_t load_task_data_t;
/* Holds the previous saved state
* Can be restored to disk with undo_save_state(). */
static struct save_state_buf undo_save_buf;
/* Holds the data from before a load_state() operation
* Can be restored with undo_load_state(). */
static struct save_state_buf undo_load_buf;
#ifdef HAVE_THREADS
typedef struct autosave autosave_t;
/* Autosave support. */
struct autosave_st
{
autosave_t **list;
unsigned num;
};
struct autosave
{
volatile bool quit;
slock_t *lock;
slock_t *cond_lock;
scond_t *cond;
sthread_t *thread;
void *buffer;
const void *retro_buffer;
const char *path;
size_t bufsize;
unsigned interval;
};
static struct autosave_st autosave_state;
/**
* autosave_thread:
* @data : pointer to autosave object
*
* Callback function for (threaded) autosave.
**/
static void autosave_thread(void *data)
{
bool first_log = true;
autosave_t *save = (autosave_t*)data;
while (!save->quit)
{
bool differ;
slock_lock(save->lock);
differ = string_is_not_equal_fast(save->buffer, save->retro_buffer,
save->bufsize);
if (differ)
memcpy(save->buffer, save->retro_buffer, save->bufsize);
slock_unlock(save->lock);
if (differ)
{
/* Should probably deal with this more elegantly. */
RFILE *file = filestream_open(save->path, RFILE_MODE_WRITE, -1);
if (file)
{
bool failed = false;
/* Avoid spamming down stderr ... */
if (first_log)
{
RARCH_LOG("Autosaving SRAM to \"%s\", will continue to check every %u seconds ...\n",
save->path, save->interval);
first_log = false;
}
else
RARCH_LOG("SRAM changed ... autosaving ...\n");
failed |= filestream_write(file, save->buffer, save->bufsize)
!= save->bufsize;
failed |= filestream_flush(file) != 0;
failed |= filestream_close(file) != 0;
if (failed)
RARCH_WARN("Failed to autosave SRAM. Disk might be full.\n");
}
}
slock_lock(save->cond_lock);
if (!save->quit)
#if defined(_MSC_VER) && _MSC_VER <= 1200
scond_wait_timeout(save->cond, save->cond_lock, save->interval * 1000000);
#else
scond_wait_timeout(save->cond, save->cond_lock, save->interval * 1000000LL);
#endif
slock_unlock(save->cond_lock);
}
}
/**
* autosave_new:
* @path : path to autosave file
* @data : pointer to buffer
* @size : size of @data buffer
* @interval : interval at which saves should be performed.
*
* Create and initialize autosave object.
*
* Returns: pointer to new autosave_t object if successful, otherwise
* NULL.
**/
static autosave_t *autosave_new(const char *path,
const void *data, size_t size,
unsigned interval)
{
autosave_t *handle = (autosave_t*)calloc(1, sizeof(*handle));
if (!handle)
goto error;
handle->bufsize = size;
handle->interval = interval;
handle->path = path;
handle->buffer = malloc(size);
handle->retro_buffer = data;
if (!handle->buffer)
goto error;
memcpy(handle->buffer, handle->retro_buffer, handle->bufsize);
handle->lock = slock_new();
handle->cond_lock = slock_new();
handle->cond = scond_new();
handle->thread = sthread_create(autosave_thread, handle);
return handle;
error:
if (handle)
free(handle);
return NULL;
}
/**
* autosave_free:
* @handle : pointer to autosave object
*
* Frees autosave object.
**/
static void autosave_free(autosave_t *handle)
{
slock_lock(handle->cond_lock);
handle->quit = true;
slock_unlock(handle->cond_lock);
scond_signal(handle->cond);
sthread_join(handle->thread);
slock_free(handle->lock);
slock_free(handle->cond_lock);
scond_free(handle->cond);
if (handle->buffer)
free(handle->buffer);
handle->buffer = NULL;
}
bool autosave_init(void)
{
unsigned i;
autosave_t **list = NULL;
settings_t *settings = config_get_ptr();
unsigned autosave_interval = settings->uints.autosave_interval;
if (autosave_interval < 1 || !task_save_files)
return false;
list = (autosave_t**)calloc(task_save_files->size,
sizeof(*autosave_state.list));
if (!list)
return false;
autosave_state.list = list;
autosave_state.num = (unsigned)task_save_files->size;
for (i = 0; i < task_save_files->size; i++)
{
retro_ctx_memory_info_t mem_info;
autosave_t *auto_st = NULL;
const char *path = task_save_files->elems[i].data;
unsigned type = task_save_files->elems[i].attr.i;
mem_info.id = type;
core_get_memory(&mem_info);
if (mem_info.size <= 0)
continue;
auto_st = autosave_new(path,
mem_info.data,
mem_info.size,
autosave_interval);
if (!auto_st)
{
RARCH_WARN("%s\n", msg_hash_to_str(MSG_AUTOSAVE_FAILED));
continue;
}
autosave_state.list[i] = auto_st;
}
return true;
}
void autosave_deinit(void)
{
unsigned i;
for (i = 0; i < autosave_state.num; i++)
{
autosave_t *handle = autosave_state.list[i];
if (handle)
{
autosave_free(handle);
free(autosave_state.list[i]);
}
autosave_state.list[i] = NULL;
}
free(autosave_state.list);
autosave_state.list = NULL;
autosave_state.num = 0;
}
#endif
/**
* autosave_lock:
*
* Lock autosave.
**/
void autosave_lock(void)
{
#ifdef HAVE_THREADS
unsigned i;
for (i = 0; i < autosave_state.num; i++)
{
autosave_t *handle = autosave_state.list[i];
if (handle)
slock_lock(handle->lock);
}
#endif
}
/**
* autosave_unlock:
*
* Unlocks autosave.
**/
void autosave_unlock(void)
{
#ifdef HAVE_THREADS
unsigned i;
for (i = 0; i < autosave_state.num; i++)
{
autosave_t *handle = autosave_state.list[i];
if (handle)
slock_unlock(handle->lock);
}
#endif
}
/**
* undo_load_state:
* Revert to the state before a state was loaded.
*
* Returns: true if successful, false otherwise.
**/
bool content_undo_load_state(void)
{
unsigned i;
retro_ctx_serialize_info_t serial_info;
size_t temp_data_size;
bool ret = false;
unsigned num_blocks = 0;
void* temp_data = NULL;
struct sram_block *blocks = NULL;
settings_t *settings = config_get_ptr();
RARCH_LOG("%s: \"%s\".\n",
msg_hash_to_str(MSG_LOADING_STATE),
undo_load_buf.path);
RARCH_LOG("%s: %u %s.\n",
msg_hash_to_str(MSG_STATE_SIZE),
undo_load_buf.size,
msg_hash_to_str(MSG_BYTES));
/* TODO/FIXME - This checking of SRAM overwrite,
* the backing up of it and
* its flushing could all be in their
* own functions... */
if (settings->bools.block_sram_overwrite && task_save_files
&& task_save_files->size)
{
RARCH_LOG("%s.\n",
msg_hash_to_str(MSG_BLOCKING_SRAM_OVERWRITE));
blocks = (struct sram_block*)
calloc(task_save_files->size, sizeof(*blocks));
if (blocks)
{
num_blocks = (unsigned)task_save_files->size;
for (i = 0; i < num_blocks; i++)
blocks[i].type = task_save_files->elems[i].attr.i;
}
}
for (i = 0; i < num_blocks; i++)
{
retro_ctx_memory_info_t mem_info;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
blocks[i].size = mem_info.size;
}
for (i = 0; i < num_blocks; i++)
if (blocks[i].size)
blocks[i].data = malloc(blocks[i].size);
/* Backup current SRAM which is overwritten by unserialize. */
for (i = 0; i < num_blocks; i++)
{
if (blocks[i].data)
{
retro_ctx_memory_info_t mem_info;
const void *ptr = NULL;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
ptr = mem_info.data;
if (ptr)
memcpy(blocks[i].data, ptr, blocks[i].size);
}
}
/* We need to make a temporary copy of the buffer, to allow the swap below */
temp_data = malloc(undo_load_buf.size);
temp_data_size = undo_load_buf.size;
memcpy(temp_data, undo_load_buf.data, undo_load_buf.size);
serial_info.data_const = temp_data;
serial_info.size = temp_data_size;
/* Swap the current state with the backup state. This way, we can undo
what we're undoing */
content_save_state("RAM", false, false);
ret = core_unserialize(&serial_info);
/* Clean up the temporary copy */
free(temp_data);
temp_data = NULL;
/* Flush back. */
for (i = 0; i < num_blocks; i++)
{
if (blocks[i].data)
{
retro_ctx_memory_info_t mem_info;
void *ptr = NULL;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
ptr = mem_info.data;
if (ptr)
memcpy(ptr, blocks[i].data, blocks[i].size);
}
}
for (i = 0; i < num_blocks; i++)
free(blocks[i].data);
free(blocks);
if (!ret)
{
RARCH_ERR("%s \"%s\".\n",
msg_hash_to_str(MSG_FAILED_TO_UNDO_LOAD_STATE),
undo_load_buf.path);
}
return ret;
}
static void undo_save_state_cb(void *task_data,
void *user_data, const char *error)
{
/* Wipe the save file buffer as it's intended to be one use only */
undo_save_buf.path[0] = '\0';
undo_save_buf.size = 0;
if (undo_save_buf.data)
{
free(undo_save_buf.data);
undo_save_buf.data = NULL;
}
}
/**
* task_save_handler_finished:
* @task : the task to finish
* @state : the state associated with this task
*
* Close the save state file and finish the task.
**/
static void task_save_handler_finished(retro_task_t *task,
save_task_state_t *state)
{
save_task_state_t *task_data = NULL;
task_set_finished(task, true);
filestream_close(state->file);
if (!task_get_error(task) && task_get_cancelled(task))
task_set_error(task, strdup("Task canceled"));
task_data = (save_task_state_t*)calloc(1, sizeof(*task_data));
memcpy(task_data, state, sizeof(*state));
task_set_data(task, task_data);
if (state->data)
{
if (state->undo_save && state->data == undo_save_buf.data)
undo_save_buf.data = NULL;
free(state->data);
state->data = NULL;
}
free(state);
}
/**
* task_save_handler:
* @task : the task being worked on
*
* Write a chunk of data to the save state file.
**/
static void task_save_handler(retro_task_t *task)
{
int written;
ssize_t remaining;
save_task_state_t *state = (save_task_state_t*)task->state;
if (!state->file)
{
state->file = filestream_open(state->path, RFILE_MODE_WRITE, -1);
if (!state->file)
return;
}
remaining = MIN(state->size - state->written, SAVE_STATE_CHUNK);
written = (int)filestream_write(state->file,
(uint8_t*)state->data + state->written, remaining);
state->written += written;
task_set_progress(task, (state->written / (float)state->size) * 100);
if (task_get_cancelled(task) || written != remaining)
{
char err[256];
err[0] = '\0';
if (state->undo_save)
{
RARCH_ERR("%s \"%s\".\n",
msg_hash_to_str(MSG_FAILED_TO_UNDO_SAVE_STATE),
undo_save_buf.path);
snprintf(err, sizeof(err), "%s \"%s\".",
msg_hash_to_str(MSG_FAILED_TO_UNDO_SAVE_STATE),
"RAM");
}
else
snprintf(err, sizeof(err), "%s %s", msg_hash_to_str(MSG_FAILED_TO_SAVE_STATE_TO), state->path);
task_set_error(task, strdup(err));
task_save_handler_finished(task, state);
return;
}
if (state->written == state->size)
{
char *msg = NULL;
task_free_title(task);
if (state->undo_save)
msg = strdup(msg_hash_to_str(MSG_RESTORED_OLD_SAVE_STATE));
else if (state->state_slot < 0)
msg = strdup(msg_hash_to_str(MSG_SAVED_STATE_TO_SLOT_AUTO));
else
{
char new_msg[128];
new_msg[0] = '\0';
snprintf(new_msg, sizeof(new_msg), msg_hash_to_str(MSG_SAVED_STATE_TO_SLOT),
state->state_slot);
msg = strdup(new_msg);
}
if (!task_get_mute(task) && msg)
task_set_title(task, msg);
task_save_handler_finished(task, state);
return;
}
}
/**
* task_push_undo_save_state:
* @path : file path of the save state
* @data : the save state data to write
* @size : the total size of the save state
*
* Create a new task to undo the last save of the content state.
**/
static bool task_push_undo_save_state(const char *path, void *data, size_t size)
{
retro_task_t *task = (retro_task_t*)calloc(1, sizeof(*task));
save_task_state_t *state = (save_task_state_t*)calloc(1, sizeof(*state));
settings_t *settings = config_get_ptr();
if (!task || !state)
goto error;
strlcpy(state->path, path, sizeof(state->path));
state->data = data;
state->size = size;
state->undo_save = true;
state->state_slot = settings->ints.state_slot;
state->has_valid_framebuffer = video_driver_cached_frame_has_valid_framebuffer();
task->type = TASK_TYPE_BLOCKING;
task->state = state;
task->handler = task_save_handler;
task->callback = undo_save_state_cb;
task->title = strdup(msg_hash_to_str(MSG_UNDOING_SAVE_STATE));
task_queue_push(task);
return true;
error:
if (data)
free(data);
if (state)
free(state);
if (task)
free(task);
return false;
}
/**
* undo_save_state:
* Reverts the last save operation
*
* Returns: true if successful, false otherwise.
**/
bool content_undo_save_state(void)
{
return task_push_undo_save_state(undo_save_buf.path,
undo_save_buf.data,
undo_save_buf.size);
}
/**
* task_load_handler_finished:
* @task : the task to finish
* @state : the state associated with this task
*
* Close the loaded state file and finish the task.
**/
static void task_load_handler_finished(retro_task_t *task,
save_task_state_t *state)
{
load_task_data_t *task_data = NULL;
task_set_finished(task, true);
if (state->file)
filestream_close(state->file);
if (!task_get_error(task) && task_get_cancelled(task))
task_set_error(task, strdup("Task canceled"));
task_data = (load_task_data_t*)calloc(1, sizeof(*task_data));
memcpy(task_data, state, sizeof(*task_data));
task_set_data(task, task_data);
free(state);
}
/**
* task_load_handler:
* @task : the task being worked on
*
* Load a chunk of data from the save state file.
**/
static void task_load_handler(retro_task_t *task)
{
ssize_t remaining, bytes_read;
save_task_state_t *state = (save_task_state_t*)task->state;
if (!state->file)
{
state->file = filestream_open(state->path, RFILE_MODE_READ, -1);
if (!state->file)
goto error;
if (filestream_seek(state->file, 0, SEEK_END) != 0)
goto error;
state->size = filestream_tell(state->file);
if (state->size < 0)
goto error;
filestream_rewind(state->file);
state->data = malloc(state->size + 1);
if (!state->data)
goto error;
}
remaining = MIN(state->size - state->bytes_read, SAVE_STATE_CHUNK);
bytes_read = filestream_read(state->file,
(uint8_t*)state->data + state->bytes_read, remaining);
state->bytes_read += bytes_read;
if (state->size > 0)
task_set_progress(task, (state->bytes_read / (float)state->size) * 100);
if (task_get_cancelled(task) || bytes_read != remaining)
{
if (state->autoload)
{
char *msg = (char*)malloc(1024 * sizeof(char));
msg[0] = '\0';
snprintf(msg,
1024 * sizeof(char),
"%s \"%s\" %s.",
msg_hash_to_str(MSG_AUTOLOADING_SAVESTATE_FROM),
state->path,
msg_hash_to_str(MSG_FAILED));
task_set_error(task, strdup(msg));
free(msg);
}
else
task_set_error(task, strdup(msg_hash_to_str(MSG_FAILED_TO_LOAD_STATE)));
free(state->data);
state->data = NULL;
task_load_handler_finished(task, state);
return;
}
if (state->bytes_read == state->size)
{
char *msg = (char*)malloc(1024 * sizeof(char));
msg[0] = '\0';
task_free_title(task);
if (state->autoload)
{
snprintf(msg,
1024 * sizeof(char),
"%s \"%s\" %s.",
msg_hash_to_str(MSG_AUTOLOADING_SAVESTATE_FROM),
state->path,
msg_hash_to_str(MSG_SUCCEEDED));
}
else
{
if (state->state_slot < 0)
strlcpy(msg, msg_hash_to_str(MSG_LOADED_STATE_FROM_SLOT_AUTO),
1024 * sizeof(char)
);
else
snprintf(msg,
1024 * sizeof(char),
msg_hash_to_str(MSG_LOADED_STATE_FROM_SLOT),
state->state_slot);
}
if (!task_get_mute(task))
task_set_title(task, strdup(msg));
free(msg);
task_load_handler_finished(task, state);
return;
}
return;
error:
task_load_handler_finished(task, state);
}
/**
* content_load_state_cb:
* @path : path that state will be loaded from.
* Load a state from disk to memory.
*
**/
static void content_load_state_cb(void *task_data,
void *user_data, const char *error)
{
retro_ctx_serialize_info_t serial_info;
unsigned i;
bool ret;
load_task_data_t *load_data = (load_task_data_t*)task_data;
ssize_t size = load_data->size;
unsigned num_blocks = 0;
void *buf = load_data->data;
struct sram_block *blocks = NULL;
settings_t *settings = config_get_ptr();
RARCH_LOG("%s: \"%s\".\n",
msg_hash_to_str(MSG_LOADING_STATE),
load_data->path);
if (size < 0 || !buf)
goto error;
RARCH_LOG("%s: %u %s.\n",
msg_hash_to_str(MSG_STATE_SIZE),
(unsigned)size,
msg_hash_to_str(MSG_BYTES));
/* This means we're backing up the file in memory, so content_undo_save_state()
can restore it */
if (load_data->load_to_backup_buffer)
{
/* If we were previously backing up a file, let go of it first */
if (undo_save_buf.data)
{
free(undo_save_buf.data);
undo_save_buf.data = NULL;
}
undo_save_buf.data = malloc(size);
if (!undo_save_buf.data)
goto error;
memcpy(undo_save_buf.data, buf, size);
undo_save_buf.size = size;
strlcpy(undo_save_buf.path, load_data->path, sizeof(undo_save_buf.path));
free(buf);
free(load_data);
return;
}
if (settings->bools.block_sram_overwrite && task_save_files
&& task_save_files->size)
{
RARCH_LOG("%s.\n",
msg_hash_to_str(MSG_BLOCKING_SRAM_OVERWRITE));
blocks = (struct sram_block*)
calloc(task_save_files->size, sizeof(*blocks));
if (blocks)
{
num_blocks = (unsigned)task_save_files->size;
for (i = 0; i < num_blocks; i++)
blocks[i].type = task_save_files->elems[i].attr.i;
}
}
for (i = 0; i < num_blocks; i++)
{
retro_ctx_memory_info_t mem_info;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
blocks[i].size = mem_info.size;
}
for (i = 0; i < num_blocks; i++)
if (blocks[i].size)
blocks[i].data = malloc(blocks[i].size);
/* Backup current SRAM which is overwritten by unserialize. */
for (i = 0; i < num_blocks; i++)
{
if (blocks[i].data)
{
retro_ctx_memory_info_t mem_info;
const void *ptr = NULL;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
ptr = mem_info.data;
if (ptr)
memcpy(blocks[i].data, ptr, blocks[i].size);
}
}
serial_info.data_const = buf;
serial_info.size = size;
/* Backup the current state so we can undo this load */
content_save_state("RAM", false, false);
ret = core_unserialize(&serial_info);
/* Flush back. */
for (i = 0; i < num_blocks; i++)
{
if (blocks[i].data)
{
retro_ctx_memory_info_t mem_info;
void *ptr = NULL;
mem_info.id = blocks[i].type;
core_get_memory(&mem_info);
ptr = mem_info.data;
if (ptr)
memcpy(ptr, blocks[i].data, blocks[i].size);
}
}
for (i = 0; i < num_blocks; i++)
free(blocks[i].data);
free(blocks);
if (!ret)
goto error;
free(buf);
free(load_data);
return;
error:
RARCH_ERR("%s \"%s\".\n",
msg_hash_to_str(MSG_FAILED_TO_LOAD_STATE),
load_data->path);
if (buf)
free(buf);
free(load_data);
}
/**
* save_state_cb:
*
* Called after the save state is done. Takes a screenshot if needed.
**/
static void save_state_cb(void *task_data,
void *user_data, const char *error)
{
save_task_state_t *state = (save_task_state_t*)task_data;
char *path = strdup(state->path);
if (state->thumbnail_enable)
take_screenshot(path, true, state->has_valid_framebuffer);
free(path);
}