Skip to content

Commit

Permalink
Revamping cheats support.
Browse files Browse the repository at this point in the history
  • Loading branch information
bearoso committed Apr 26, 2018
1 parent c727189 commit 0d102b7
Show file tree
Hide file tree
Showing 12 changed files with 462 additions and 301 deletions.
59 changes: 35 additions & 24 deletions bml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ static inline bml_node *bml_node_new(void)
node->data = NULL;
node->name = NULL;
node->depth = -1;

return node;
}

static char *strndup_trim (char *str, int len)
Expand Down Expand Up @@ -45,14 +47,14 @@ static inline unsigned int bml_read_depth (char *data)
return depth;
}

static unsigned int bml_parse_depth (bml_node *node, char **data)
static void bml_parse_depth (bml_node *node, char **data)
{
unsigned int depth = bml_read_depth (*data);
*data += depth;
node->depth = depth;
}

static char *bml_parse_name (bml_node *node, char **data)
static void bml_parse_name (bml_node *node, char **data)
{
int len;

Expand All @@ -70,7 +72,7 @@ static void bml_parse_data (bml_node *node, char **data)
if (p[0] == '=' && p[1] == '\"')
{
len = 2;
while (p[len] && !islf (p[len]))
while (p[len] && p[len] != '\"' && !islf (p[len]))
len++;
if (p[len] != '\"')
return;
Expand Down Expand Up @@ -177,7 +179,8 @@ static void bml_parse_attr (bml_node *node, char **data)
n->name = strndup_trim (p, len);
p += len;
bml_parse_data (n, &p);
node->attr.push_back (n);
n->depth = bml_attr_type;
node->child.push_back (n);
}

*data = p;
Expand All @@ -196,10 +199,12 @@ static int contains_space (char *str)

static void bml_print_node (bml_node *node, int depth)
{
int i;

if (!node)
return;

for (int i = 0; i < depth * 2; i++)
for (i = 0; i < depth * 2; i++)
{
printf (" ");
}
Expand All @@ -210,30 +215,29 @@ static void bml_print_node (bml_node *node, int depth)
if (node->data)
{
if (contains_space (node->data))
printf (": \"%s\"", node->data);
printf ("=\"%s\"", node->data);
else
printf (": %s", node->data);
}

for (int i = 0; i < node->attr.size(); i++)
for (i = 0; i < (int) node->child.size () && node->child[i]->depth == bml_attr_type; i++)
{
if (node->attr[i]->name)
if (node->child[i]->name)
{
printf (" %s", node->attr[i]->name);
if (node->attr[i]->data)
printf (" %s", node->child[i]->name);
if (node->child[i]->data)
{
if (contains_space (node->attr[i]->data))
printf ("=\"%s\"", node->attr[i]->data);
if (contains_space (node->child[i]->data))
printf ("=\"%s\"", node->child[i]->data);
else
printf ("=%s", node->attr[i]->data);
printf ("=%s", node->child[i]->data);
}
}
}

if (depth >= 0)
printf ("\n");

for (int i = 0; i < node->child.size(); i++)
for (; i < (int) node->child.size(); i++)
{
bml_print_node (node->child[i], depth + 1);
}
Expand Down Expand Up @@ -265,7 +269,7 @@ static bml_node *bml_parse_node (char **doc)
return NULL;

bml_skip_empty (doc);
while (*doc && bml_read_depth (*doc) > node->depth)
while (*doc && (int) bml_read_depth (*doc) > node->depth)
{
bml_node *child = bml_parse_node (doc);

Expand All @@ -283,18 +287,12 @@ void bml_free_node (bml_node *node)
delete[] (node->name);
delete[] (node->data);

for (int i = 0; i < node->child.size(); i++)
for (unsigned int i = 0; i < node->child.size(); i++)
{
bml_free_node (node->child[i]);
delete node->child[i];
}

for (int i = 0; i < node->attr.size(); i++)
{
bml_free_node (node->attr[i]);
delete node->attr[i];
}

return;
}

Expand All @@ -320,7 +318,20 @@ bml_node *bml_parse (char **doc)
return root;
}

bml_node *bml_parse_file (char *filename)
bml_node *bml_find_sub (bml_node *n, const char *name)
{
unsigned int i;

for (i = 0; i < n->child.size (); i++)
{
if (!strcasecmp (n->child[i]->name, name))
return n->child[i];
}

return NULL;
}

bml_node *bml_parse_file (const char *filename)
{
FILE *file = NULL;
char *buffer = NULL;
Expand Down
7 changes: 5 additions & 2 deletions bml.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
#define __BML_H
#include <vector>

const int bml_attr_type = -2;

typedef struct bml_node
{
char *name;
char *data;

int depth;

std::vector<bml_node *> attr;
std::vector<bml_node *> child;

} bml_node;

bml_node *bml_parse_file (char *filename);
bml_node *bml_find_sub (bml_node *node, const char *name);

bml_node *bml_parse_file (const char *filename);

/* Parse character array into BML tree. Destructive to input. */
bml_node *bml_parse (char **buffer);
Expand Down
40 changes: 24 additions & 16 deletions cheats.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,30 @@
#ifndef _CHEATS_H_
#define _CHEATS_H_

#define MAX_CHEATS 150
#include "port.h"
#include <vector>

struct SCheat
{
uint32 address;
uint8 byte;
uint8 saved_byte;
bool8 conditional;
bool8 cond_true;
uint8 cond_byte;
bool8 enabled;
bool8 saved;
char name[22];
};

struct SCheatGroup
{
char *name;
bool8 enabled;
std::vector<struct SCheat> c;
};

struct SCheatData
{
struct SCheat c[MAX_CHEATS];
uint32 num_cheats;
std::vector<struct SCheatGroup> g;
uint8 CWRAM[0x20000];
uint8 CSRAM[0x10000];
uint8 CIRAM[0x2000];
Expand Down Expand Up @@ -250,20 +258,20 @@ typedef enum
extern SCheatData Cheat;
extern Watch watches[16];

void S9xApplyCheat (uint32);
void S9xApplyCheats (void);
void S9xRemoveCheat (uint32);
void S9xRemoveCheats (void);
void S9xDeleteCheat (uint32);
int S9xAddCheatGroup (const char *name, const char *cheat);
int S9xModifyCheatGroup (uint32 index, const char *name, const char *cheat);
void S9xEnableCheatGroup (uint32 index);
void S9xDisableCheatGroup (uint32 index);
void S9xDeleteCheats (void);
void S9xEnableCheat (uint32);
void S9xDisableCheat (uint32);
void S9xAddCheat (bool8, bool8, uint32, uint8);
char *S9xCheatGroupToText (uint32 index);
void S9xDeleteCheatGroup (uint32 index);
bool8 S9xLoadCheatFile (const char *filename);
bool8 S9xSaveCheatFile (const char *filename);
void S9xUpdateCheatsInMemory (void);
bool8 S9xImportCheatsFromDatabase (const char *filename);

void S9xInitCheatData (void);
void S9xInitWatchedAddress (void);
bool8 S9xLoadCheatFile (const char *);
bool8 S9xSaveCheatFile (const char *);

void S9xStartCheatSearch (SCheatData *);
void S9xSearchForChange (SCheatData *, S9xCheatComparisonType, S9xCheatDataSize, bool8, bool8);
void S9xSearchForValue (SCheatData *, S9xCheatComparisonType, S9xCheatDataSize, uint32, bool8, bool8);
Expand Down
Loading

0 comments on commit 0d102b7

Please sign in to comment.