OpenEmu: Cheat support

This commit is contained in:
Jeffrey Pfau 2016-01-24 14:24:53 -08:00
parent 15dadb8387
commit a53c3e0628
2 changed files with 44 additions and 5 deletions

View File

@ -32,6 +32,8 @@
<integer>0</integer> <integer>0</integer>
<key>OEGameCoreSupportsRewinding</key> <key>OEGameCoreSupportsRewinding</key>
<true/> <true/>
<key>OEGameCoreSupportsCheatCode</key>
<true/>
</dict> </dict>
</dict> </dict>
<key>OEGameCorePlayerCount</key> <key>OEGameCorePlayerCount</key>

View File

@ -27,6 +27,7 @@
#include "util/common.h" #include "util/common.h"
#include "gba/cheats.h" #include "gba/cheats.h"
#include "gba/cheats/gameshark.h"
#include "gba/renderers/video-software.h" #include "gba/renderers/video-software.h"
#include "gba/serialize.h" #include "gba/serialize.h"
#include "gba/context/context.h" #include "gba/context/context.h"
@ -45,7 +46,7 @@
struct GBAContext context; struct GBAContext context;
struct GBAVideoSoftwareRenderer renderer; struct GBAVideoSoftwareRenderer renderer;
struct GBACheatDevice cheats; struct GBACheatDevice cheats;
struct GBACheatSet cheatSet; NSMutableDictionary *cheatSets;
uint16_t keys; uint16_t keys;
} }
@end @end
@ -70,8 +71,7 @@
GBAAudioResizeBuffer(&context.gba->audio, SAMPLES); GBAAudioResizeBuffer(&context.gba->audio, SAMPLES);
GBACheatDeviceCreate(&cheats); GBACheatDeviceCreate(&cheats);
GBACheatAttachDevice(context.gba, &cheats); GBACheatAttachDevice(context.gba, &cheats);
GBACheatSetInit(&cheatSet, "openemu"); cheatSets = [[NSMutableDictionary alloc] init];
GBACheatAddSet(&cheats, &cheatSet);
keys = 0; keys = 0;
} }
@ -81,9 +81,18 @@
- (void)dealloc - (void)dealloc
{ {
GBAContextDeinit(&context); GBAContextDeinit(&context);
GBACheatRemoveSet(&cheats, &cheatSet); [cheatSets enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
UNUSED(key);
UNUSED(stop);
GBACheatRemoveSet(&cheats, [obj pointerValue]);
}];
GBACheatDeviceDestroy(&cheats); GBACheatDeviceDestroy(&cheats);
GBACheatSetDeinit(&cheatSet); [cheatSets enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
UNUSED(key);
UNUSED(stop);
GBACheatSetDeinit([obj pointerValue]);
}];
[cheatSets release];
free(renderer.outputBuffer); free(renderer.outputBuffer);
[super dealloc]; [super dealloc];
@ -275,5 +284,33 @@ const int GBAMap[] = {
keys &= ~(1 << GBAMap[button]); keys &= ~(1 << GBAMap[button]);
} }
#pragma mark - Cheats
- (void)setCheat:(NSString *)code setType:(NSString *)type setEnabled:(BOOL)enabled
{
code = [code stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
code = [code stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *codeId = [code stringByAppendingFormat:@"/%@", type];
struct GBACheatSet* cheatSet = [[cheatSets objectForKey:codeId] pointerValue];
if (cheatSet) {
cheatSet->enabled = enabled;
return;
}
cheatSet = malloc(sizeof(*cheatSet));
GBACheatSetInit(cheatSet, [codeId UTF8String]);
if ([type isEqual:@"GameShark"]) {
GBACheatSetGameSharkVersion(cheatSet, 1);
} else if ([type isEqual:@"Action Replay"]) {
GBACheatSetGameSharkVersion(cheatSet, 3);
}
NSArray *codeSet = [code componentsSeparatedByString:@"+"];
for (id c in codeSet) {
GBACheatAddLine(cheatSet, [c UTF8String]);
}
cheatSet->enabled = enabled;
[cheatSets setObject:[NSValue valueWithPointer:cheatSet] forKey:codeId];
GBACheatAddSet(&cheats, cheatSet);
}
@end @end