Permalink
Browse files

Initial support for loading text cheat file. Currently does not work.

1 parent 761e081 commit c2640e50e19218a6415ebec404b1b2f17c0bda75 @root670 committed Jun 23, 2015
Showing with 320 additions and 2 deletions.
  1. +1 −1 CheatDevicePS2.ini
  2. +1 −0 Makefile
  3. +6 −0 cheats.c
  4. +15 −0 cheats.txt
  5. +2 −1 main.c
  6. +271 −0 textcheats.c
  7. +24 −0 textcheats.h
View
@@ -1,5 +1,5 @@
[CheatDevicePS2]
-database = cb10.cdb
+database = cb10.txt
boot1 = mass:BOOT/BOOT.ELF
boot2 = mass:BOOT/ESR.ELF
boot3 = mc0:BOOT/BOOT.ELF
View
@@ -14,6 +14,7 @@ OBJS += libraries/ini.o
OBJS += util.o
OBJS += startgame.o
OBJS += database.o
+OBJS += textcheats.o
OBJS += cheats.o
OBJS += graphics.o
OBJS += storage.o
View
@@ -1,5 +1,6 @@
#include "cheats.h"
#include "database.h"
+#include "textcheats.h"
#include "menus.h"
#include "graphics.h"
#include <debug.h>
@@ -76,6 +77,9 @@ int cheatsOpenDatabase(const char* path, cheatDatabaseType_t t)
switch(dbType)
{
case TEXT:
+ numGames = textCheatsOpenFile(path);
+ gamesHead = textCheatsGetCheatStruct();
+ textCheatsClose();
break;
case BINARY:
@@ -134,10 +138,12 @@ cheatsGame_t* cheatsLoadCheatMenu(const cheatsGame_t* game)
{
if(node == game)
{
+ printf("Found game.\n");
/* Build the menu */
cheatsCheat_t *cheat = node->cheats;
menuItem_t *items = calloc(node->numCheats, sizeof(menuItem_t));
menuItem_t *item = items;
+ printf("%d cheats\n", node->numCheats);
while(cheat != NULL)
{
if(cheat->type == CHEATNORMAL)
View
@@ -0,0 +1,15 @@
+"Test Game"
+Cheat 1
+12345678 12345678
+98765432 24841811
+Cheat 2
+
+
+
+Cheat 3
+12345678 12345678
+
+"Game 2"
+Some Code
+12345677 77777777
+"Game 3"
View
@@ -24,7 +24,8 @@ int main(int argc, char *argv[])
initCheatMan();
initMenuMan();
- if(!cheatsOpenDatabase(settingsGetDatabasePath(), 1))
+ //if(!cheatsOpenDatabase(settingsGetDatabasePath(), 0))
+ if(!cheatsOpenDatabase(settingsGetDatabasePath(), 0))
{
char error[255];
sprintf(error, "Error loading cheat database \"%s\"!", settingsGetDatabasePath());
View
@@ -0,0 +1,271 @@
+#include "textcheats.h"
+#include "cheats.h"
+#include "storage.h"
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <time.h>
+
+#define TOKEN_TITLE (1 << 0)
+#define TOKEN_CHEAT (1 << 1)
+#define TOKEN_CODE (1 << 2)
+#define TOKEN_VALID TOKEN_TITLE|TOKEN_CHEAT|TOKEN_CODE
+#define TOKEN_START TOKEN_TITLE
+
+static cheatsGame_t *gamesHead = NULL;
+static cheatsGame_t *game = NULL;
+static cheatsCheat_t *cheatsHead = NULL;
+static int usedCheats = 0;
+static cheatsCheat_t *cheat = NULL;
+static u64 *codeLines = NULL;
+static u8 *text;
+static int tokenNext;
+
+static int numGames = 0;
+static int numCheats = 0;
+static int numCodeLines = 0;
+
+void countTokens(const char *text, int *numGames, int *numCheats, int *numCodeLines)
+{
+ char line[255];
+ int len = 0;
+ if(!text || !numGames || !numCheats || !numCodeLines)
+ return;
+
+ while(*text)
+ {
+ char *off1 = strchr(text, '\n');
+ len = off1 - text;
+
+ if(len)
+ {
+ strncpy(line, text, 255);
+ strtok(line, "\r");
+ line[len] = '\0';
+
+ int token = getToken(line);
+
+ switch(token)
+ {
+ case TOKEN_TITLE:
+ *numGames += 1;
+ break;
+ case TOKEN_CHEAT:
+ *numCheats += 1;
+ break;
+ case TOKEN_CODE:
+ *numCodeLines += 1;
+ break;
+ default:
+ break;
+ }
+
+ text += len + 1;
+ }
+ }
+}
+
+// Returns number of games in cheat file.
+int textCheatsOpenFile(const char *path)
+{
+ FILE *txtFile;
+ char *text;
+ char *buff;
+ char *c;
+ char line[255];
+ int numLines = 0, textSize;
+
+ if(!path)
+ return 0;
+
+ txtFile = fopen(path, "r");
+ fseek(txtFile, 0, SEEK_END);
+ int len = ftell(txtFile);
+ fseek(txtFile, 0, SEEK_SET);
+
+ buff = malloc(len);
+ text = buff;
+ fread(text, 1, len, txtFile);
+
+ printf(" *** Counting tokens ***\n");
+ countTokens(text, &numGames, &numCheats, &numCodeLines);
+ fseek(txtFile, 0, SEEK_SET);
+
+ printf("Games: %d\nCheats: %d\nLines: %d\n", numGames, numCheats, numCodeLines);
+
+ printf(" *** Allocating structs\n");
+ gamesHead = calloc(numGames, sizeof(cheatsGame_t));
+ cheatsHead = calloc(numCheats, sizeof(cheatsCheat_t));
+
+ tokenNext = TOKEN_START;
+
+ while(*text)
+ {
+ char line[255];
+ char *off1 = strchr(text, '\n');
+ int len = off1 - text;
+
+ if(len > 1)
+ {
+ strncpy(line, text, 255);
+ strtok(line, "\r");
+ line[len] = '\0';
+
+ parseLine(line);
+ }
+
+ text += len + 1;
+ }
+
+ free(buff);
+
+ return numGames;
+}
+
+// Returns number og games in cheat file.
+int textCheatsOpenBuffer(unsigned char *buff)
+{
+ return 0;
+}
+// Get data structure of loaded cheat file. Returns null on error.
+cheatsGame_t *textCheatsGetCheatStruct()
+{
+ return gamesHead;
+}
+// Free cheat file from memory.
+int textCheatsClose()
+{
+ return 1;
+}
+
+// Determine token type for line.
+int getToken(const char *line)
+{
+ //printf("line = %s\n", line);
+ const char *c;
+ int len, numDigits = 0, ret;
+
+ if (!line || strlen(line) <= 0)
+ return 0;
+
+ len = strlen(line);
+
+ if(len == 17 && line[8] == ' ')
+ {
+ c = line;
+ while(*c)
+ {
+ if(isxdigit(*c++))
+ numDigits++;
+ }
+
+ if(numDigits == 16)
+ ret = TOKEN_CODE;
+ }
+
+ else if(line[0] == '"' && line[len-1] == '"')
+ ret = TOKEN_TITLE;
+
+ else if(line[0] == '/' && line[1] == '/')
+ ret = 0;
+
+ else
+ ret = TOKEN_CHEAT;
+
+ return ret;
+}
+
+// Find next expected token.
+int getNextToken(int tokenCurrent)
+{
+ if(!(tokenCurrent & TOKEN_VALID))
+ return 0;
+
+ switch(tokenCurrent)
+ {
+ case TOKEN_TITLE:
+ return TOKEN_TITLE|TOKEN_CHEAT;
+ case TOKEN_CHEAT:
+ case TOKEN_CODE:
+ return TOKEN_TITLE|TOKEN_CHEAT|TOKEN_CODE;
+ default:
+ return 0;
+ }
+}
+
+// Parse line and process token.
+int parseLine(const char *line)
+{
+ int token, len;
+
+ token = getToken(line);
+
+ switch(token)
+ {
+ case TOKEN_TITLE:
+ // create new game
+ printf("%s\n", line);
+ if(!game)
+ {
+ //printf(">>>> first game\n");
+ game = gamesHead;
+ }
+ else
+ {
+ //printf(">>>> not first game\n");
+ game->next = ++game;
+ }
+ strncpy(game->title, strtok(line+1, "\""), 81);
+ game->next = NULL;
+
+ break;
+ case TOKEN_CHEAT:
+ // create new cheat
+ //printf(" %s\n", line);
+ game->numCheats++;
+ if(!game)
+ return 0;
+ if(game->cheats == NULL)
+ {
+ //printf(" >>>> first cheat in game\n");
+ game->cheats = &cheatsHead[usedCheats++];
+ cheat = game->cheats;
+ }
+ else
+ {
+ //printf(" >>>> not first cheat in game\n");
+ cheat->next = &cheatsHead[usedCheats++];
+ cheat = cheat->next;
+ }
+ strncpy(cheat->title, line, 81);
+ cheat->type = CHEATHEADER;
+ cheat->next = NULL;
+
+ break;
+ case TOKEN_CODE:
+ // add code to cheat
+ //printf("Code: %s\n", line);
+ //printf(" %s\n", line);
+ if(!game || !cheat)
+ return 0;
+ if(!cheat->codeLines)
+ {
+ //printf("Allocating space for code lines");
+ cheat->codeLines = calloc(1, sizeof(u64));
+ }
+ else
+ //cheat->codeLines = realloc(cheat->codeLines, (cheat->numCodeLines + 1) * sizeof(u64));
+ //cheat->codeLines[0] = 5;
+ cheat->type = CHEATNORMAL;
+ cheat->numCodeLines++;
+
+ break;
+
+ default:
+ //printf("skip\n");
+ break;
+ }
+
+ //printf("Next token type :%d\n", tokenNext);
+ return 1;
+}
View
@@ -0,0 +1,24 @@
+#ifndef TEXTCHEATS_H
+#define TEXTCHEATS_H
+#include "cheats.h"
+#include <stdio.h>
+
+// Returns number of games in cheat file.
+int textCheatsOpenFile(const char *path);
+// Returns number og games in cheat file.
+int textCheatsOpenBuffer(unsigned char *buff);
+// Get data structure of loaded cheat file. Returns null on error.
+cheatsGame_t *textCheatsGetCheatStruct();
+// Free cheat file from memory.
+int textCheatsClose();
+
+// Determine token type for line.
+int getToken(const char *line);
+// Find next expected token.
+int getNextToken(int tokenCurrent);
+// Parse line and process token.
+int parseLine(const char *line);
+// Determine number of each token type
+void countTokens(const char *text, int *numGames, int *numCheats, int *numCodeLines);
+
+#endif

0 comments on commit c2640e5

Please sign in to comment.