mirror of https://github.com/mgba-emu/mgba.git
OpenEmu: Fix some leaks, and improve serialization
This commit is contained in:
parent
6f62852671
commit
7f4320e872
|
@ -208,19 +208,29 @@
|
||||||
|
|
||||||
- (NSData *)serializeStateWithError:(NSError **)outError
|
- (NSData *)serializeStateWithError:(NSError **)outError
|
||||||
{
|
{
|
||||||
UNUSED(outError);
|
struct VFile* vf = VFileMemChunk(nil, 0);
|
||||||
// TODO memory VFile that self-manages memory
|
if (!GBASaveStateNamed(context.gba, vf, 0)) {
|
||||||
return nil;
|
*outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadStateError userInfo:nil];
|
||||||
|
vf->close(vf);
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
size_t size = vf->size(vf);
|
||||||
|
void* data = vf->map(vf, size, MAP_READ);
|
||||||
|
NSData *nsdata = [NSData dataWithBytes:data length:size];
|
||||||
|
vf->unmap(vf, data, size);
|
||||||
|
vf->close(vf);
|
||||||
|
return nsdata;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError
|
- (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError
|
||||||
{
|
{
|
||||||
// TODO VFileFromConstMemory
|
struct VFile* vf = VFileFromConstMemory(state.bytes, state.length);
|
||||||
struct VFile* vf = VFileFromMemory((void*) state.bytes, state.length);
|
|
||||||
if (!GBALoadStateNamed(context.gba, vf, 0)) {
|
if (!GBALoadStateNamed(context.gba, vf, 0)) {
|
||||||
*outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadStateError userInfo:nil];
|
*outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadStateError userInfo:nil];
|
||||||
|
vf->close(vf);
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
vf->close(vf);
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,12 +238,14 @@
|
||||||
{
|
{
|
||||||
struct VFile* vf = VFileOpen([fileName UTF8String], O_CREAT | O_TRUNC | O_RDWR);
|
struct VFile* vf = VFileOpen([fileName UTF8String], O_CREAT | O_TRUNC | O_RDWR);
|
||||||
block(GBASaveStateNamed(context.gba, vf, 0), nil);
|
block(GBASaveStateNamed(context.gba, vf, 0), nil);
|
||||||
|
vf->close(vf);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)loadStateFromFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block
|
- (void)loadStateFromFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block
|
||||||
{
|
{
|
||||||
struct VFile* vf = VFileOpen([fileName UTF8String], O_RDONLY);
|
struct VFile* vf = VFileOpen([fileName UTF8String], O_RDONLY);
|
||||||
block(GBALoadStateNamed(context.gba, vf, 0), nil);
|
block(GBALoadStateNamed(context.gba, vf, 0), nil);
|
||||||
|
vf->close(vf);
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Input
|
#pragma mark - Input
|
||||||
|
|
Loading…
Reference in New Issue