From 7604bd4ff2948680b113eb67eff930ee8d681737 Mon Sep 17 00:00:00 2001 From: clobber Date: Sat, 12 Dec 2015 17:01:35 -0800 Subject: [PATCH] Clean up state saving and serialization. --- GenPlusGameCore.m | 71 ++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 44 deletions(-) diff --git a/GenPlusGameCore.m b/GenPlusGameCore.m index 59a6649..b2946f7 100644 --- a/GenPlusGameCore.m +++ b/GenPlusGameCore.m @@ -367,25 +367,19 @@ static __weak GenPlusGameCore *_current; - (NSData *)serializeStateWithError:(NSError **)outError { size_t length = STATE_SIZE; - void *bytes = malloc(length); - if(state_save(bytes)) - { - return [NSData dataWithBytesNoCopy:bytes length:length]; - } - else - { - NSError *error = [NSError errorWithDomain:OEGameCoreErrorDomain - code:OEGameCoreCouldNotSaveStateError - userInfo:@{ - NSLocalizedDescriptionKey : @"Save state data could not be written", - NSLocalizedRecoverySuggestionErrorKey : @"The emulator could not write the state data." - }]; - if(outError) - { - *outError = error; - } - return nil; + NSMutableData *data = [NSMutableData dataWithLength:length]; + + if(state_save(data.mutableBytes)) + return data; + + if (outError) { + *outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotSaveStateError userInfo:@{ + NSLocalizedDescriptionKey : @"Save state data could not be written", + NSLocalizedRecoverySuggestionErrorKey : @"The emulator could not write the state data." + }]; } + + return nil; } - (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError @@ -394,39 +388,28 @@ static __weak GenPlusGameCore *_current; size_t length = [state length]; size_t serialSize = STATE_SIZE; - if(serialSize != length) - { - NSError *error = [NSError errorWithDomain:OEGameCoreErrorDomain - code:OEGameCoreStateHasWrongSizeError - userInfo:@{ - NSLocalizedDescriptionKey : @"Save state has wrong file size.", - NSLocalizedRecoverySuggestionErrorKey : [NSString stringWithFormat:@"The size of the save state does not have the right size, %lu expected, got: %ld.", serialSize, [state length]], - }]; - if(outError) - { - *outError = error; + if(serialSize != length) { + if (outError) { + *outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreStateHasWrongSizeError userInfo:@{ + NSLocalizedDescriptionKey : @"Save state has wrong file size.", + NSLocalizedRecoverySuggestionErrorKey : [NSString stringWithFormat:@"The size of the save state does not have the right size, %lu expected, got: %ld.", serialSize, [state length]], + }]; } + return NO; } if(state_load((uint8_t *)bytes)) - { return YES; + + if (outError) { + *outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadStateError userInfo:@{ + NSLocalizedDescriptionKey : @"The save state data could not be read", + NSLocalizedRecoverySuggestionErrorKey : @"Could not load data from the save state" + }]; } - else - { - NSError *error = [NSError errorWithDomain:OEGameCoreErrorDomain - code:OEGameCoreCouldNotLoadStateError - userInfo:@{ - NSLocalizedDescriptionKey : @"The save state data could not be read", - NSLocalizedRecoverySuggestionErrorKey : @"Could not load data from the save state" - }]; - if(outError) - { - *outError = error; - } - return NO; - } + + return NO; } # pragma mark - Input