snes9x/macosx/mac-cheat.mm

179 lines
4.1 KiB
Plaintext
Raw Normal View History

/*****************************************************************************\
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
This file is licensed under the Snes9x License.
For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/
/***********************************************************************************
SNES9X for Mac OS (c) Copyright John Stiles
Snes9x for Mac OS X
(c) Copyright 2001 - 2011 zones
(c) Copyright 2002 - 2005 107
(c) Copyright 2002 PB1400c
(c) Copyright 2004 Alexander and Sander
(c) Copyright 2004 - 2005 Steven Seeger
(c) Copyright 2005 Ryan Vogt
2022-02-07 00:03:46 +01:00
(c) Copyright 2019 - 2022 Michael Donald Buckley
***********************************************************************************/
#include "port.h"
#include "cheats.h"
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include "mac-prefix.h"
#include "mac-dialog.h"
#include "mac-os.h"
#include "mac-stringtools.h"
#include "mac-cheat.h"
#define kDataBrowser 'BRSR'
#define kCmCheckBox 'CHK_'
#define kCmAddress 'ADDR'
#define kCmValue 'VALU'
#define kCmDescription 'DESC'
#define kNewButton 'NEW_'
#define kDelButton 'DEL_'
#define kAllButton 'ALL_'
extern SCheatData Cheat;
2022-02-07 00:03:46 +01:00
@implementation S9xCheatItem
- (void)setAddress:(uint32)address value:(uint8)value cheatDescription:(const char *)cheatDescription
{
2022-02-07 00:03:46 +01:00
bool enabled = self.enabled.boolValue;
char code[256];
sprintf(code, "%x=%x", address, value);
S9xModifyCheatGroup(self.cheatID, cheatDescription, code);
if (enabled)
{
2022-02-07 00:03:46 +01:00
S9xEnableCheatGroup(self.cheatID);
}
}
2022-02-07 00:03:46 +01:00
@dynamic address;
- (NSNumber *)address
{
2022-02-07 00:03:46 +01:00
return @(Cheat.g[self.cheatID].c[0].address);
}
- (void)setAddress:(NSNumber *)address
{
[self setAddress:address.unsignedIntValue value:Cheat.g[self.cheatID].c[0].byte cheatDescription:self.cheatDescription.UTF8String];
}
@dynamic value;
- (NSNumber *)value
{
return @(Cheat.g[self.cheatID].c[0].byte);
}
- (void)setValue:(NSNumber *)value
{
[self setAddress:Cheat.g[self.cheatID].c[0].address value:value.unsignedCharValue cheatDescription:self.cheatDescription.UTF8String];
}
2022-02-07 00:03:46 +01:00
@dynamic enabled;
- (NSNumber *)enabled
{
2022-02-07 00:03:46 +01:00
return @(Cheat.g[self.cheatID].enabled);
}
2022-02-07 00:03:46 +01:00
- (void)setEnabled:(NSNumber *)enabled
{
if (enabled.boolValue)
{
2022-02-07 00:03:46 +01:00
S9xEnableCheatGroup(self.cheatID);
}
else
{
S9xDisableCheatGroup(self.cheatID);
}
}
2022-02-07 00:03:46 +01:00
@dynamic cheatDescription;
- (NSString *)cheatDescription
{
2022-02-07 00:03:46 +01:00
return [NSString stringWithUTF8String:Cheat.g[self.cheatID].name];
}
2022-02-07 00:03:46 +01:00
- (void)setCheatDescription:(NSString *)cheatDescription
{
2022-02-07 00:03:46 +01:00
[self setAddress:Cheat.g[self.cheatID].c[0].address value:Cheat.g[self.cheatID].c[0].byte cheatDescription:cheatDescription.UTF8String];
}
2022-02-07 00:03:46 +01:00
- (void)delete
{
2022-02-07 00:03:46 +01:00
S9xDeleteCheatGroup(self.cheatID);
}
2022-02-07 00:03:46 +01:00
@end
extern "C"
{
bool CheatValuesFromCode(NSString *code, uint32 *address, uint8 *value)
{
2022-02-07 00:03:46 +01:00
const char *text = code.UTF8String;
if (!S9xGameGenieToRaw (text, *address, *value))
{
return true;
}
if (!S9xProActionReplayToRaw (text, *address, *value))
{
return true;
}
else if (sscanf (text, "%x = %x", (unsigned int *)address, (unsigned int *)value) == 2)
{
return true;
}
else if (sscanf (text, "%x / %x", (unsigned int *)address, (unsigned int *)value) == 2)
{
return true;
}
return false;
}
2022-02-07 00:03:46 +01:00
void CreateCheatFromAddress(NSNumber *address, NSNumber *value, NSString *cheatDescription)
{
2022-02-07 00:03:46 +01:00
char code[256];
sprintf(code, "%x=%x", address.unsignedIntValue, value.unsignedCharValue);
S9xAddCheatGroup(cheatDescription.UTF8String, code);
S9xEnableCheatGroup(Cheat.g.size() - 1);
}
2022-02-07 00:03:46 +01:00
void CreateCheatFromCode(NSString *code, NSString *cheatDescription)
{
S9xAddCheatGroup(cheatDescription.UTF8String, code.UTF8String);
S9xEnableCheatGroup(Cheat.g.size() - 1);
}
2022-02-07 00:03:46 +01:00
NSArray<S9xCheatItem *> *GetAllCheats(void)
{
2022-02-07 00:03:46 +01:00
NSMutableArray *cheats = [NSMutableArray new];
uint32 cheatID = 0;
for (auto it : Cheat.g)
{
S9xCheatItem *cheat = [S9xCheatItem new];
cheat.cheatID = cheatID++;
[cheats addObject:cheat];
}
return cheats;
}
}