Permalink
Browse files

Remember previously used cheats and enable them next time Cheat Devic…

…e is run.
  • Loading branch information...
1 parent 1489bfb commit f2bf3eedc2726807abd0296f6107bbec04238705 @root670 committed Mar 22, 2016
Showing with 113 additions and 4 deletions.
  1. +1 −0 Makefile
  2. +98 −4 cheats.c
  3. +2 −0 cheats.h
  4. +1 −0 main.c
  5. +10 −0 menus.c
  6. +1 −0 menus.h
View
@@ -10,6 +10,7 @@ OBJS += libraries/upng.o
OBJS += libraries/ini.o
# Main
+OBJS += hash.o
OBJS += util.o
OBJS += startgame.o
OBJS += database.o
View
@@ -4,6 +4,7 @@
#include "menus.h"
#include "graphics.h"
#include "util.h"
+#include "hash.h"
#include <debug.h>
#include <kernel.h>
#include <stdio.h>
@@ -14,6 +15,9 @@
static cheatsGame_t *gamesHead = NULL;
static cheatsGame_t *activeGame = NULL;
static cheatDatabaseType_t dbType;
+static hashTable_t *gameHashes;
+static hashTable_t *cheatHashes;
+static FILE *historyFile;
static int numGames = 0;
static int numCheats = 0;
static int numEnabledCheats = 0;
@@ -69,8 +73,78 @@ int killCheatMan()
return 1;
}
-// CheatDB --> Game --> Cheat --> Code
+static void populateCheatHashTable()
+{
+ if(!cheatHashes)
+ return;
+
+ cheatsCheat_t *cheat = activeGame->cheats;
+
+ while(cheat != NULL)
+ {
+ unsigned int hash = hashFunction(cheat->codeLines, cheat->numCodeLines * 8);
+ hashAdd(cheatHashes, cheat, hash);
+
+ cheat = cheat->next;
+ }
+}
+
+int cheatsLoadHistory()
+{
+ int i;
+ FILE *historyFile;
+ size_t historyLength;
+ unsigned int lastGameHash, cheatHash;
+ menuItem_t *lastGameMenu;
+ cheatsCheat_t *cheat;
+
+ if(initialized)
+ {
+ if(!gameHashes)
+ return 0;
+
+ historyFile = fopen("CheatHistory.bin", "rb");
+
+ if(historyFile)
+ {
+ fseek(historyFile, 0, SEEK_END);
+ historyLength = ftell(historyFile);
+ fseek(historyFile, 0, SEEK_SET);
+
+ fread(&lastGameHash, 4, 1, historyFile);
+ lastGameMenu = (menuItem_t *)hashFind(gameHashes, lastGameHash);
+
+ if(lastGameMenu != NULL)
+ {
+ cheatsSetActiveGame((cheatsGame_t *) lastGameMenu->extra);
+ cheatHashes = hashNewTable(activeGame->numCheats * 1.6);
+ populateCheatHashTable();
+
+ for(i = 0; i < historyLength - 4; i+= 4)
+ {
+ fread(&cheatHash, 4, 1, historyFile);
+ cheat = (cheatsCheat_t *)hashFind(cheatHashes, cheatHash);
+
+ if(cheat != NULL)
+ cheatsToggleCheat(cheat);
+ }
+
+ menuSetActiveItem(lastGameMenu);
+
+ free(cheatHashes);
+ free(gameHashes);
+ }
+
+ fclose(historyFile);
+ }
+
+ return 1;
+ }
+
+ return 0;
+}
+// CheatDB --> Game --> Cheat --> Code
int cheatsOpenDatabase(const char* path)
{
const char *ext;
@@ -117,6 +191,9 @@ int cheatsLoadGameMenu()
cheatsGame_t *node = gamesHead;
menuItem_t *items = calloc(numGames, sizeof(menuItem_t));
menuItem_t *item = items;
+ unsigned int hash;
+
+ gameHashes = hashNewTable(numGames * 1.6);
while(node)
{
@@ -125,6 +202,9 @@ int cheatsLoadGameMenu()
strcpy(item->text, node->title);
item->extra = node;
+ hash = hashFunction(item->text, strlen(item->text));
+ hashAdd(gameHashes, item, hash);
+
menuAppendItem(item);
node = node->next;
item++;
@@ -140,9 +220,6 @@ cheatsGame_t* cheatsLoadCheatMenu(const cheatsGame_t* game)
{
if(initialized && gamesHead!=NULL && game)
{
- cheatsGame_t *node = gamesHead;
- printf("Loading cheat menu for %s\n", game->title);
-
/* Build the menu */
cheatsCheat_t *cheat = game->cheats;
menuItem_t *items = calloc(game->numCheats, sizeof(menuItem_t));
@@ -355,6 +432,13 @@ static void readCodes(cheatsCheat_t *cheats)
{
if(cheat->enabled)
{
+ if(historyFile)
+ {
+ // Save cheat's hash
+ unsigned int cheatHash = hashFunction(cheat->codeLines, cheat->numCodeLines * 8);
+ fwrite(&cheatHash, 4, 1, historyFile);
+ }
+
for(i = 0; i < cheat->numCodeLines; ++i)
{
addr = (u32)*((u32 *)cheat->codeLines + 2*i);
@@ -388,7 +472,17 @@ void cheatsInstallCodesForEngine()
{
SetupERL();
+ historyFile = fopen("CheatHistory.bin", "wb");
+ if(historyFile)
+ {
+ unsigned int gameHash = hashFunction(activeGame->title, strlen(activeGame->title));
+ fwrite(&gameHash, 4, 1, historyFile);
+ }
+
readCodes(activeGame->enableCheat);
readCodes(activeGame->cheats);
+
+ if(historyFile)
+ fclose(historyFile);
}
}
View
@@ -47,6 +47,8 @@ int killCheatMan();
int cheatsOpenDatabase(const char* path);
// TODO: Save all cheats to a file.
int cheatsSaveDatabase(const char* path, cheatDatabaseType_t t);
+// Re-enable previously used cheats
+int cheatsLoadHistory();
// Create a menu with game titles.
int cheatsLoadGameMenu();
View
@@ -31,6 +31,7 @@ int main(int argc, char *argv[])
}
cheatsLoadGameMenu();
+ cheatsLoadHistory();
graphicsClearScreen(128, 128, 128);
/* Main Loop */
View
@@ -116,6 +116,16 @@ int menuRemoveAllItems()
return 1;
}
+int menuSetActiveItem(menuItem_t *item)
+{
+ if(!item)
+ return 0;
+
+ activeMenu->current = item;
+
+ return 1;
+}
+
menuID_t menuGetActive()
{
return activeMenu->identifier;
View
@@ -39,6 +39,7 @@ menuID_t menuGetActive();
int menuAppendItem(menuItem_t *item);
int menuRemoveActiveItem();
int menuRemoveAllItems();
+int menuSetActiveItem(menuItem_t *item);
int menuUp();
int menuDown();

0 comments on commit f2bf3ee

Please sign in to comment.