1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | * Mupen64plus - savestates.c * |
3 | * Mupen64Plus homepage: https://mupen64plus.org/ * |
4 | * Copyright (C) 2012 CasualJames * |
5 | * Copyright (C) 2009 Olejl Tillin9 * |
6 | * Copyright (C) 2008 Richard42 Tillin9 * |
7 | * Copyright (C) 2002 Hacktarux * |
8 | * * |
9 | * This program is free software; you can redistribute it and/or modify * |
10 | * it under the terms of the GNU General Public License as published by * |
11 | * the Free Software Foundation; either version 2 of the License, or * |
12 | * (at your option) any later version. * |
13 | * * |
14 | * This program is distributed in the hope that it will be useful, * |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
17 | * GNU General Public License for more details. * |
18 | * * |
19 | * You should have received a copy of the GNU General Public License * |
20 | * along with this program; if not, write to the * |
21 | * Free Software Foundation, Inc., * |
22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
23 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
24 | |
25 | #include <SDL.h> |
26 | #include <SDL_thread.h> |
27 | #include <stddef.h> |
28 | #include <stdint.h> |
29 | #include <stdio.h> |
30 | #include <stdlib.h> |
31 | #include <string.h> |
32 | #include <sys/types.h> |
33 | #include <zlib.h> |
34 | #include <unzip.h> |
35 | #include <zip.h> |
36 | |
37 | #define M64P_CORE_PROTOTYPES 1 |
38 | #include "api/callbacks.h" |
39 | #include "api/config.h" |
40 | #include "api/m64p_config.h" |
41 | #include "api/m64p_types.h" |
42 | #include "backends/api/storage_backend.h" |
43 | #include "device/device.h" |
44 | #include "main/list.h" |
45 | #include "main/main.h" |
46 | #include "osal/preproc.h" |
47 | #include "osd/osd.h" |
48 | #include "plugin/plugin.h" |
49 | #include "rom.h" |
50 | #include "savestates.h" |
51 | #include "util.h" |
52 | #include "workqueue.h" |
53 | |
54 | enum { GB_CART_FINGERPRINT_SIZE = 0x1c }; |
55 | enum { GB_CART_FINGERPRINT_OFFSET = 0x134 }; |
56 | |
57 | enum { DD_DISK_ID_OFFSET = 0x43670 }; |
58 | |
59 | static const char* savestate_magic = "M64+SAVE" ; |
60 | static const int savestate_latest_version = 0x00010500; /* 1.5 */ |
61 | static const unsigned char pj64_magic[4] = { 0xC8, 0xA6, 0xD8, 0x23 }; |
62 | |
63 | static savestates_job job = savestates_job_nothing; |
64 | static savestates_type type = savestates_type_unknown; |
65 | static char *fname = NULL; |
66 | |
67 | static unsigned int slot = 0; |
68 | static int autoinc_save_slot = 0; |
69 | |
70 | static SDL_mutex *savestates_lock; |
71 | |
72 | struct savestate_work { |
73 | char *filepath; |
74 | char *data; |
75 | size_t size; |
76 | struct work_struct work; |
77 | }; |
78 | |
79 | /* Returns the malloc'd full path of the currently selected savestate. */ |
80 | static char *savestates_generate_path(savestates_type type) |
81 | { |
82 | if(fname != NULL) /* A specific path was given. */ |
83 | { |
84 | return strdup(fname); |
85 | } |
86 | else /* Use the selected savestate slot */ |
87 | { |
88 | char *filename; |
89 | switch (type) |
90 | { |
91 | case savestates_type_m64p: |
92 | filename = formatstr("%s.st%d" , ROM_SETTINGS.goodname, slot); |
93 | break; |
94 | case savestates_type_pj64_zip: |
95 | filename = formatstr("%s.pj%d.zip" , ROM_PARAMS.headername, slot); |
96 | break; |
97 | case savestates_type_pj64_unc: |
98 | filename = formatstr("%s.pj%d" , ROM_PARAMS.headername, slot); |
99 | break; |
100 | default: |
101 | filename = NULL; |
102 | break; |
103 | } |
104 | |
105 | if (filename != NULL) |
106 | { |
107 | char *filepath = formatstr("%s%s" , get_savestatepath(), filename); |
108 | free(filename); |
109 | return filepath; |
110 | } |
111 | else |
112 | return NULL; |
113 | } |
114 | } |
115 | |
116 | void savestates_select_slot(unsigned int s) |
117 | { |
118 | if(s>9||s==slot) |
119 | return; |
120 | slot = s; |
121 | ConfigSetParameter(g_CoreConfig, "CurrentStateSlot" , M64TYPE_INT, &s); |
122 | StateChanged(M64CORE_SAVESTATE_SLOT, slot); |
123 | |
124 | main_message(M64MSG_STATUS, OSD_BOTTOM_LEFT, "Selected state slot: %d" , slot); |
125 | } |
126 | |
127 | /* Returns the currently selected save slot. */ |
128 | unsigned int savestates_get_slot(void) |
129 | { |
130 | return slot; |
131 | } |
132 | |
133 | /* Sets save state slot autoincrement on or off. */ |
134 | void savestates_set_autoinc_slot(int b) |
135 | { |
136 | autoinc_save_slot = b; |
137 | } |
138 | |
139 | void savestates_inc_slot(void) |
140 | { |
141 | if(++slot>9) |
142 | slot = 0; |
143 | StateChanged(M64CORE_SAVESTATE_SLOT, slot); |
144 | } |
145 | |
146 | savestates_job savestates_get_job(void) |
147 | { |
148 | return job; |
149 | } |
150 | |
151 | void savestates_set_job(savestates_job j, savestates_type t, const char *fn) |
152 | { |
153 | if (fname != NULL) |
154 | { |
155 | free(fname); |
156 | fname = NULL; |
157 | } |
158 | |
159 | job = j; |
160 | type = t; |
161 | if (fn != NULL) |
162 | fname = strdup(fn); |
163 | } |
164 | |
165 | static void savestates_clear_job(void) |
166 | { |
167 | savestates_set_job(savestates_job_nothing, savestates_type_unknown, NULL); |
168 | } |
169 | |
170 | #define GETARRAY(buff, type, count) \ |
171 | (to_little_endian_buffer(buff, sizeof(type),count), \ |
172 | buff += count*sizeof(type), \ |
173 | (type *)(buff-count*sizeof(type))) |
174 | #define COPYARRAY(dst, buff, type, count) \ |
175 | memcpy(dst, GETARRAY(buff, type, count), sizeof(type)*count) |
176 | #define GETDATA(buff, type) *GETARRAY(buff, type, 1) |
177 | #define PUTARRAY(src, buff, type, count) \ |
178 | memcpy(buff, src, sizeof(type)*count); \ |
179 | to_little_endian_buffer(buff, sizeof(type), count); \ |
180 | buff += count*sizeof(type); |
181 | |
182 | #define PUTDATA(buff, type, value) \ |
183 | do { type |
---|