From a53c3e0628461b179ed2b605339c314617d1aa66 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sun, 24 Jan 2016 14:24:53 -0800 Subject: [PATCH] OpenEmu: Cheat support --- src/platform/openemu/Info.plist.in | 2 ++ src/platform/openemu/mGBAGameCore.m | 47 ++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/platform/openemu/Info.plist.in b/src/platform/openemu/Info.plist.in index 9ab3b28f5..0642d81f1 100644 --- a/src/platform/openemu/Info.plist.in +++ b/src/platform/openemu/Info.plist.in @@ -32,6 +32,8 @@ 0 OEGameCoreSupportsRewinding + OEGameCoreSupportsCheatCode + OEGameCorePlayerCount diff --git a/src/platform/openemu/mGBAGameCore.m b/src/platform/openemu/mGBAGameCore.m index 5f220fb95..a5749d6d8 100644 --- a/src/platform/openemu/mGBAGameCore.m +++ b/src/platform/openemu/mGBAGameCore.m @@ -27,6 +27,7 @@ #include "util/common.h" #include "gba/cheats.h" +#include "gba/cheats/gameshark.h" #include "gba/renderers/video-software.h" #include "gba/serialize.h" #include "gba/context/context.h" @@ -45,7 +46,7 @@ struct GBAContext context; struct GBAVideoSoftwareRenderer renderer; struct GBACheatDevice cheats; - struct GBACheatSet cheatSet; + NSMutableDictionary *cheatSets; uint16_t keys; } @end @@ -70,8 +71,7 @@ GBAAudioResizeBuffer(&context.gba->audio, SAMPLES); GBACheatDeviceCreate(&cheats); GBACheatAttachDevice(context.gba, &cheats); - GBACheatSetInit(&cheatSet, "openemu"); - GBACheatAddSet(&cheats, &cheatSet); + cheatSets = [[NSMutableDictionary alloc] init]; keys = 0; } @@ -81,9 +81,18 @@ - (void)dealloc { GBAContextDeinit(&context); - GBACheatRemoveSet(&cheats, &cheatSet); + [cheatSets enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + UNUSED(key); + UNUSED(stop); + GBACheatRemoveSet(&cheats, [obj pointerValue]); + }]; GBACheatDeviceDestroy(&cheats); - GBACheatSetDeinit(&cheatSet); + [cheatSets enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + UNUSED(key); + UNUSED(stop); + GBACheatSetDeinit([obj pointerValue]); + }]; + [cheatSets release]; free(renderer.outputBuffer); [super dealloc]; @@ -275,5 +284,33 @@ const int GBAMap[] = { 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