all repos — mgba @ 67905d281bfecbb06f51f2ca5ac939df378734a5

mGBA Game Boy Advance Emulator

src/core/serialize.c (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "serialize.h"
  7
  8#include "core/core.h"
  9#include "core/cheats.h"
 10#include "core/sync.h"
 11#include "util/memory.h"
 12#include "util/vfs.h"
 13
 14#ifdef USE_PNG
 15#include "util/png-io.h"
 16#include <png.h>
 17#include <zlib.h>
 18#endif
 19
 20mLOG_DEFINE_CATEGORY(SAVESTATE, "Savestate");
 21
 22struct mBundledState {
 23	size_t stateSize;
 24	void* state;
 25	struct mStateExtdata* extdata;
 26};
 27
 28struct mStateExtdataHeader {
 29	uint32_t tag;
 30	int32_t size;
 31	int64_t offset;
 32};
 33
 34bool mStateExtdataInit(struct mStateExtdata* extdata) {
 35	memset(extdata->data, 0, sizeof(extdata->data));
 36	return true;
 37}
 38
 39void mStateExtdataDeinit(struct mStateExtdata* extdata) {
 40	size_t i;
 41	for (i = 1; i < EXTDATA_MAX; ++i) {
 42		if (extdata->data[i].data && extdata->data[i].clean) {
 43			extdata->data[i].clean(extdata->data[i].data);
 44		}
 45	}
 46}
 47
 48void mStateExtdataPut(struct mStateExtdata* extdata, enum mStateExtdataTag tag, struct mStateExtdataItem* item) {
 49	if (tag == EXTDATA_NONE || tag >= EXTDATA_MAX) {
 50		return;
 51	}
 52
 53	if (extdata->data[tag].data && extdata->data[tag].clean) {
 54		extdata->data[tag].clean(extdata->data[tag].data);
 55	}
 56	extdata->data[tag] = *item;
 57}
 58
 59bool mStateExtdataGet(struct mStateExtdata* extdata, enum mStateExtdataTag tag, struct mStateExtdataItem* item) {
 60	if (tag == EXTDATA_NONE || tag >= EXTDATA_MAX) {
 61		return false;
 62	}
 63
 64	*item = extdata->data[tag];
 65	return true;
 66}
 67
 68bool mStateExtdataSerialize(struct mStateExtdata* extdata, struct VFile* vf) {
 69	ssize_t position = vf->seek(vf, 0, SEEK_CUR);
 70	ssize_t size = sizeof(struct mStateExtdataHeader);
 71	size_t i = 0;
 72	for (i = 1; i < EXTDATA_MAX; ++i) {
 73		if (extdata->data[i].data) {
 74			size += sizeof(struct mStateExtdataHeader);
 75		}
 76	}
 77	if (size == sizeof(struct mStateExtdataHeader)) {
 78		return true;
 79	}
 80	struct mStateExtdataHeader* header = malloc(size);
 81	position += size;
 82
 83	size_t j;
 84	for (i = 1, j = 0; i < EXTDATA_MAX; ++i) {
 85		if (extdata->data[i].data) {
 86			STORE_32LE(i, offsetof(struct mStateExtdataHeader, tag), &header[j]);
 87			STORE_32LE(extdata->data[i].size, offsetof(struct mStateExtdataHeader, size), &header[j]);
 88			STORE_64LE(position, offsetof(struct mStateExtdataHeader, offset), &header[j]);
 89			position += extdata->data[i].size;
 90			++j;
 91		}
 92	}
 93	header[j].tag = 0;
 94	header[j].size = 0;
 95	header[j].offset = 0;
 96
 97	if (vf->write(vf, header, size) != size) {
 98		free(header);
 99		return false;
100	}
101	free(header);
102
103	for (i = 1; i < EXTDATA_MAX; ++i) {
104		if (extdata->data[i].data) {
105			if (vf->write(vf, extdata->data[i].data, extdata->data[i].size) != extdata->data[i].size) {
106				return false;
107			}
108		}
109	}
110	return true;
111}
112
113bool mStateExtdataDeserialize(struct mStateExtdata* extdata, struct VFile* vf) {
114	while (true) {
115		struct mStateExtdataHeader buffer, header;
116		if (vf->read(vf, &buffer, sizeof(buffer)) != sizeof(buffer)) {
117			return false;
118		}
119		LOAD_32LE(header.tag, 0, &buffer.tag);
120		LOAD_32LE(header.size, 0, &buffer.size);
121		LOAD_64LE(header.offset, 0, &buffer.offset);
122
123		if (header.tag == EXTDATA_NONE) {
124			break;
125		}
126		if (header.tag >= EXTDATA_MAX) {
127			continue;
128		}
129		ssize_t position = vf->seek(vf, 0, SEEK_CUR);
130		if (vf->seek(vf, header.offset, SEEK_SET) < 0) {
131			return false;
132		}
133		struct mStateExtdataItem item = {
134			.data = malloc(header.size),
135			.size = header.size,
136			.clean = free
137		};
138		if (!item.data) {
139			continue;
140		}
141		if (vf->read(vf, item.data, header.size) != header.size) {
142			free(item.data);
143			continue;
144		}
145		mStateExtdataPut(extdata, header.tag, &item);
146		vf->seek(vf, position, SEEK_SET);
147	};
148	return true;
149}
150
151
152
153
154
155
156
157#ifdef USE_PNG
158static bool _savePNGState(struct mCore* core, struct VFile* vf, struct mStateExtdata* extdata) {
159	size_t stride;
160	color_t* pixels = 0;
161
162	core->getVideoBuffer(core, &pixels, &stride);
163	if (!pixels) {
164		return false;
165	}
166
167	size_t stateSize = core->stateSize(core);
168	void* state = anonymousMemoryMap(stateSize);
169	if (!state) {
170		return false;
171	}
172	core->saveState(core, state);
173
174	uLongf len = compressBound(stateSize);
175	void* buffer = malloc(len);
176	if (!buffer) {
177		mappedMemoryFree(state, stateSize);
178		return false;
179	}
180	compress(buffer, &len, (const Bytef*) state, stateSize);
181	mappedMemoryFree(state, stateSize);
182
183	unsigned width, height;
184	core->desiredVideoDimensions(core, &width, &height);
185	png_structp png = PNGWriteOpen(vf);
186	png_infop info = PNGWriteHeader(png, width, height);
187	if (!png || !info) {
188		PNGWriteClose(png, info);
189		free(buffer);
190		return false;
191	}
192	PNGWritePixels(png, width, height, stride, pixels);
193	PNGWriteCustomChunk(png, "gbAs", len, buffer);
194	if (extdata) {
195		uint32_t i;
196		for (i = 1; i < EXTDATA_MAX; ++i) {
197			if (!extdata->data[i].data) {
198				continue;
199			}
200			uLongf len = compressBound(extdata->data[i].size) + sizeof(uint32_t) * 2;
201			uint32_t* data = malloc(len);
202			if (!data) {
203				continue;
204			}
205			STORE_32LE(i, 0, data);
206			STORE_32LE(extdata->data[i].size, sizeof(uint32_t), data);
207			compress((Bytef*) (data + 2), &len, extdata->data[i].data, extdata->data[i].size);
208			PNGWriteCustomChunk(png, "gbAx", len + sizeof(uint32_t) * 2, data);
209			free(data);
210		}
211	}
212	PNGWriteClose(png, info);
213	free(buffer);
214	return true;
215}
216
217static int _loadPNGChunkHandler(png_structp png, png_unknown_chunkp chunk) {
218	struct mBundledState* bundle = png_get_user_chunk_ptr(png);
219	if (!bundle) {
220		return 0;
221	}
222	if (!strcmp((const char*) chunk->name, "gbAs")) {
223		void* state = bundle->state;
224		if (!state) {
225			return 0;
226		}
227		uLongf len = bundle->stateSize;
228		uncompress((Bytef*) state, &len, chunk->data, chunk->size);
229		return 1;
230	}
231	if (!strcmp((const char*) chunk->name, "gbAx")) {
232		struct mStateExtdata* extdata = bundle->extdata;
233		if (!extdata) {
234			return 0;
235		}
236		struct mStateExtdataItem item;
237		if (chunk->size < sizeof(uint32_t) * 2) {
238			return 0;
239		}
240		uint32_t tag;
241		LOAD_32LE(tag, 0, chunk->data);
242		LOAD_32LE(item.size, sizeof(uint32_t), chunk->data);
243		uLongf len = item.size;
244		if (item.size < 0 || tag == EXTDATA_NONE || tag >= EXTDATA_MAX) {
245			return 0;
246		}
247		item.data = malloc(item.size);
248		item.clean = free;
249		if (!item.data) {
250			return 0;
251		}
252		const uint8_t* data = chunk->data;
253		data += sizeof(uint32_t) * 2;
254		uncompress((Bytef*) item.data, &len, data, chunk->size);
255		item.size = len;
256		mStateExtdataPut(extdata, tag, &item);
257		return 1;
258	}
259	return 0;
260}
261
262static void* _loadPNGState(struct mCore* core, struct VFile* vf, struct mStateExtdata* extdata) {
263	png_structp png = PNGReadOpen(vf, PNG_HEADER_BYTES);
264	png_infop info = png_create_info_struct(png);
265	png_infop end = png_create_info_struct(png);
266	if (!png || !info || !end) {
267		PNGReadClose(png, info, end);
268		return false;
269	}
270	unsigned width, height;
271	core->desiredVideoDimensions(core, &width, &height);
272	uint32_t* pixels = malloc(width * height * 4);
273	if (!pixels) {
274		PNGReadClose(png, info, end);
275		return false;
276	}
277
278	size_t stateSize = core->stateSize(core);
279	void* state = anonymousMemoryMap(stateSize);
280	struct mBundledState bundle = {
281		.stateSize = stateSize,
282		.state = state,
283		.extdata = extdata
284	};
285
286	PNGInstallChunkHandler(png, &bundle, _loadPNGChunkHandler, "gbAs gbAx");
287	bool success = PNGReadHeader(png, info);
288	success = success && PNGReadPixels(png, info, pixels, width, height, width);
289	success = success && PNGReadFooter(png, end);
290	PNGReadClose(png, info, end);
291
292	if (success) {
293		struct mStateExtdataItem item = {
294			.size = width * height * 4,
295			.data = pixels,
296			.clean = free
297		};
298		mStateExtdataPut(extdata, EXTDATA_SCREENSHOT, &item);
299	} else {
300		free(pixels);
301		mappedMemoryFree(state, stateSize);
302		return 0;
303	}
304	return state;
305}
306#endif
307
308bool mCoreSaveStateNamed(struct mCore* core, struct VFile* vf, int flags) {
309	struct mStateExtdata extdata;
310	mStateExtdataInit(&extdata);
311	size_t stateSize = core->stateSize(core);
312	if (flags & SAVESTATE_SAVEDATA) {
313		void* sram = NULL;
314		size_t size = core->savedataClone(core, &sram);
315		if (size) {
316			struct mStateExtdataItem item = {
317				.size = size,
318				.data = sram,
319				.clean = free
320			};
321			mStateExtdataPut(&extdata, EXTDATA_SAVEDATA, &item);
322		}
323	}
324	struct VFile* cheatVf = 0;
325	struct mCheatDevice* device;
326	if (flags & SAVESTATE_CHEATS && (device = core->cheatDevice(core))) {
327		cheatVf = VFileMemChunk(0, 0);
328		if (cheatVf) {
329			mCheatSaveFile(device, cheatVf);
330			struct mStateExtdataItem item = {
331				.size = cheatVf->size(cheatVf),
332				.data = cheatVf->map(cheatVf, cheatVf->size(cheatVf), MAP_READ),
333				.clean = 0
334			};
335			mStateExtdataPut(&extdata, EXTDATA_CHEATS, &item);
336		}
337	}
338#ifdef USE_PNG
339	if (!(flags & SAVESTATE_SCREENSHOT)) {
340#else
341	UNUSED(flags);
342#endif
343		vf->truncate(vf, stateSize);
344		struct GBASerializedState* state = vf->map(vf, stateSize, MAP_WRITE);
345		if (!state) {
346			mStateExtdataDeinit(&extdata);
347			if (cheatVf) {
348				cheatVf->close(cheatVf);
349			}
350			return false;
351		}
352		core->saveState(core, state);
353		vf->unmap(vf, state, stateSize);
354		vf->seek(vf, stateSize, SEEK_SET);
355		mStateExtdataSerialize(&extdata, vf);
356		mStateExtdataDeinit(&extdata);
357		if (cheatVf) {
358			cheatVf->close(cheatVf);
359		}
360		return true;
361#ifdef USE_PNG
362	}
363	else {
364		bool success = _savePNGState(core, vf, &extdata);
365		mStateExtdataDeinit(&extdata);
366		return success;
367	}
368#endif
369	mStateExtdataDeinit(&extdata);
370	return false;
371}
372
373void* mCoreExtractState(struct mCore* core, struct VFile* vf, struct mStateExtdata* extdata) {
374#ifdef USE_PNG
375	if (isPNG(vf)) {
376		return _loadPNGState(core, vf, extdata);
377	}
378#endif
379	ssize_t stateSize = core->stateSize(core);
380	vf->seek(vf, 0, SEEK_SET);
381	if (vf->size(vf) < stateSize) {
382		return false;
383	}
384	void* state = anonymousMemoryMap(stateSize);
385	if (vf->read(vf, state, stateSize) != stateSize) {
386		mappedMemoryFree(state, stateSize);
387		return 0;
388	}
389	if (extdata) {
390		mStateExtdataDeserialize(extdata, vf);
391	}
392	return state;
393}
394
395bool mCoreLoadStateNamed(struct mCore* core, struct VFile* vf, int flags) {
396	struct mStateExtdata extdata;
397	mStateExtdataInit(&extdata);
398	void* state = mCoreExtractState(core, vf, &extdata);
399	if (!state) {
400		return false;
401	}
402	bool success = core->loadState(core, state);
403	mappedMemoryFree(state, core->stateSize(core));
404
405	unsigned width, height;
406	core->desiredVideoDimensions(core, &width, &height);
407
408	struct mStateExtdataItem item;
409	if (flags & SAVESTATE_SCREENSHOT && mStateExtdataGet(&extdata, EXTDATA_SCREENSHOT, &item)) {
410		if (item.size >= (int) (width * height) * 4) {
411			/*gba->video.renderer->putPixels(gba->video.renderer, width, item.data);
412			mCoreSyncForceFrame(core->sync);*/
413		} else {
414			mLOG(SAVESTATE, WARN, "Savestate includes invalid screenshot");
415		}
416	}
417	if (flags & SAVESTATE_SAVEDATA && mStateExtdataGet(&extdata, EXTDATA_SAVEDATA, &item)) {
418		if (item.data) {
419			core->savedataLoad(core, item.data, item.size);
420		}
421	}
422	struct mCheatDevice* device;
423	if (flags & SAVESTATE_CHEATS && (device = core->cheatDevice(core)) && mStateExtdataGet(&extdata, EXTDATA_CHEATS, &item)) {
424		if (item.size) {
425			struct VFile* svf = VFileFromMemory(item.data, item.size);
426			if (svf) {
427				mCheatDeviceClear(device);
428				mCheatParseFile(device, svf);
429				svf->close(svf);
430			}
431		}
432	}
433	mStateExtdataDeinit(&extdata);
434	return success;
435}
436