From 86a2edbdf1b79b0a6a55f3b1e9452d90a4c67999 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Mon, 4 Aug 2014 22:40:11 -0700 Subject: [PATCH] Save/load state with file parameter --- src/gba/gba-serialize.c | 37 ++++++++++++++++++++++++++----------- src/gba/gba-serialize.h | 3 +++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/gba/gba-serialize.c b/src/gba/gba-serialize.c index 2ce09963a..df07688f0 100644 --- a/src/gba/gba-serialize.c +++ b/src/gba/gba-serialize.c @@ -163,15 +163,7 @@ bool GBASaveState(struct GBA* gba, struct VDir* dir, int slot, bool screenshot) if (!vf) { return false; } - bool success = true; - if (!screenshot) { - vf->truncate(vf, sizeof(struct GBASerializedState)); - struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_WRITE); - GBASerialize(gba, state); - vf->unmap(vf, state, sizeof(struct GBASerializedState)); - } else { - _savePNGState(gba, vf); - } + bool success = GBASaveStateNamed(gba, vf, screenshot); vf->close(vf); return success; } @@ -181,14 +173,37 @@ bool GBALoadState(struct GBA* gba, struct VDir* dir, int slot) { if (!vf) { return false; } + bool success = GBALoadStateNamed(gba, vf); + vf->close(vf); + return success; +} + +bool GBASaveStateNamed(struct GBA* gba, struct VFile* vf, bool screenshot) { + if (!screenshot) { + vf->truncate(vf, sizeof(struct GBASerializedState)); + struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_WRITE); + if (!state) { + return false; + } + GBASerialize(gba, state); + vf->unmap(vf, state, sizeof(struct GBASerializedState)); + } else { + return _savePNGState(gba, vf); + } + return true; +} + +bool GBALoadStateNamed(struct GBA* gba, struct VFile* vf) { if (!isPNG(vf)) { struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_READ); + if (!state) { + return false; + } GBADeserialize(gba, state); vf->unmap(vf, state, sizeof(struct GBASerializedState)); } else { - _loadPNGState(gba, vf); + return _loadPNGState(gba, vf); } - vf->close(vf); return true; } diff --git a/src/gba/gba-serialize.h b/src/gba/gba-serialize.h index af73c45f9..0f294f5fc 100644 --- a/src/gba/gba-serialize.h +++ b/src/gba/gba-serialize.h @@ -272,6 +272,9 @@ void GBADeserialize(struct GBA* gba, struct GBASerializedState* state); bool GBASaveState(struct GBA* gba, struct VDir* dir, int slot, bool screenshot); bool GBALoadState(struct GBA* gba, struct VDir* dir, int slot); +bool GBASaveStateNamed(struct GBA* gba, struct VFile* vf, bool screenshot); +bool GBALoadStateNamed(struct GBA* gba, struct VFile* vf); + struct GBASerializedState* GBAAllocateState(void); void GBADeallocateState(struct GBASerializedState* state);