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);