1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - cheat.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2009-2010 Richard Goedeken *
5 * Copyright (C) 2010 Rhett Osborne (spinout) *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
21 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 #include "m64p_types.h"
31 #include "core_interface.h"
33 /* local definitions */
34 #define CHEAT_FILE "mupencheat.txt"
39 char **variable_names;
44 typedef struct _sCheatInfo {
49 const char *Description;
51 struct _sCheatInfo *Next;
55 static m64p_rom_header l_RomHeader;
56 static char *l_IniText = NULL;
57 static char *l_CheatGameName = NULL;
58 static sCheatInfo *l_CheatList = NULL;
59 static int l_CheatCodesFound = 0;
60 static int l_RomFound = 0;
62 /*********************************************************************************************************
63 * Static (Local) functions
66 static int isSpace(char ch)
68 return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
72 static sCheatInfo *CheatFindCode(int Number)
74 sCheatInfo *pCur = l_CheatList;
77 if (pCur->Number == Number) break;
85 static void CheatActivate(sCheatInfo *pCheat)
89 /* Get a m64p_cheat_code object */
90 m64p_cheat_code * code = (m64p_cheat_code*) calloc(pCheat->Count, sizeof(m64p_cheat_code));
93 DebugMessage(M64MSG_WARNING, "could not allocate memory for code '%s'", pCheat->Name);
97 for (i = 0; i < pCheat->Count; i++)
99 code[i].address = pCheat->Codes[i].address;
100 code[i].value = pCheat->Codes[i].variables[pCheat->Codes[i].var_to_use];
103 if (CoreAddCheat(pCheat->Name, code, pCheat->Count) != M64ERR_SUCCESS)
105 DebugMessage(M64MSG_WARNING, "CoreAddCheat() failed for cheat code %i (%s)", pCheat->Number, pCheat->Name);
111 DebugMessage(M64MSG_STATUS, "activated cheat code %i: %s", pCheat->Number, pCheat->Name);
114 static void CheatFreeAll(void)
116 if (l_IniText != NULL)
120 sCheatInfo *pCur = l_CheatList;
123 sCheatInfo *pNext = pCur->Next;
124 if (pCur->Codes != NULL)
127 for (i=0; i < pCur->Count; i++)
129 if (pCur->Codes[i].variables != NULL)
130 free(pCur->Codes[i].variables);
131 if (pCur->Codes[i].variable_names != NULL)
132 free(pCur->Codes[i].variable_names);
143 /* Append new code */
144 static sCheatInfo * NewCode(char *CheatName, int CheatNum)
146 /* allocate memory for a new sCheatInfo struct */
147 sCheatInfo *pNew = (sCheatInfo *) malloc(sizeof(sCheatInfo));
148 if (pNew == NULL) return NULL;
150 /* fill in the data members */
151 pNew->Number = CheatNum;
153 pNew->VariableLine = -1;
154 pNew->Name = CheatName;
155 pNew->Description = NULL;
161 /* stick it at the end of the list */
162 if (l_CheatList == NULL)
167 sCheatInfo *pLast = l_CheatList;
168 while (pLast->Next != NULL) pLast = pLast->Next;
173 static void CheatAddVariables(cheat_code * Code, char *varlist)
175 /* needs to be more verbose? */
176 Code->variables = NULL;
177 Code->variable_names = NULL;
179 while (*varlist != 0)
181 if ((Code->variables = (int*) realloc(Code->variables, sizeof(int) * (Code->var_count + 1))) == NULL)
183 if ((Code->variable_names = (char**) realloc(Code->variable_names, sizeof(char*) * (Code->var_count + 1))) == NULL)
185 if (sscanf(varlist, "%04X", &Code->variables[Code->var_count]) != 1)
186 Code->variables[Code->var_count] = 0;
187 if (strchr(varlist, '"') == NULL)
189 Code->variable_names[Code->var_count] = strchr(varlist, '"') + 1;
190 if ((varlist = strchr(Code->variable_names[Code->var_count], '"')) == NULL)
199 /*********************************************************************************************************
203 static void ReadCheats(char *RomSection)
205 sCheatInfo *curr_code = NULL;
206 const char *romdbpath = ConfigGetSharedDataFilepath(CHEAT_FILE);
207 if (romdbpath == NULL)
209 DebugMessage(M64MSG_WARNING, "cheat code database file '%s' not found.", CHEAT_FILE);
213 /* read the INI file into a new buffer */
215 fPtr = fopen(romdbpath, "rb");
218 DebugMessage(M64MSG_WARNING, "Couldn't open cheat code database file '%s'.", romdbpath);
221 fseek(fPtr, 0L, SEEK_END);
222 long IniLength = ftell(fPtr);
223 fseek(fPtr, 0L, SEEK_SET);
224 l_IniText = (char *) malloc(IniLength + 1);
225 if (l_IniText == NULL)
227 DebugMessage(M64MSG_WARNING, "Couldn't allocate %li bytes of memory to read cheat file.", IniLength);
231 if (fread(l_IniText, 1, IniLength, fPtr) != IniLength)
233 DebugMessage(M64MSG_WARNING, "Couldn't read %li bytes from cheat file.", IniLength);
240 l_IniText[IniLength] = 0; /* null-terminate the text data */
242 /* parse lines from cheat database */
243 char *curline = NULL;
244 char *nextline = l_IniText;
246 while(nextline != NULL && *nextline != 0)
249 /* get pointer to next line and NULL-terminate the current line */
250 nextline = strchr(curline, '\n');
251 if (nextline != NULL)
257 /* remove leading and trailing white space */
258 while(isSpace(*curline)) curline++;
259 char *endptr = curline + strlen(curline) - 1;
260 while(isSpace(*endptr)) *endptr-- = 0;
262 /* ignore line if comment or empty */
263 if (*curline == '#' || strncmp(curline, "//", 2) == 0 || *curline == 0)
266 /* handle beginning of new rom section */
267 if (strncmp(curline, "crc ", 4) == 0)
269 /* if we have already found cheats for the given ROM file, then exit upon encountering a new ROM section */
270 if (l_RomFound && (l_CheatGameName != NULL || l_CheatList != NULL))
272 /* else see if this Rom Section matches */
273 if (strcmp(curline+4, RomSection) == 0)
278 /* if we haven't found the specified ROM section, then continue looking */
283 if (strncmp(curline, "gn ", 3) == 0)
285 l_CheatGameName = curline+3;
290 if (strncmp(curline, "cn ", 3) == 0)
292 curr_code = NewCode(curline + 3, l_CheatCodesFound);
293 if (curr_code == NULL)
294 DebugMessage(M64MSG_WARNING, "error getting new code (%s)", curline+3);
298 /* if curr_code is NULL, don't do these checks */
299 if (curr_code == NULL)
302 /* code description */
303 if (strncmp(curline, "cd ", 3) == 0)
305 curr_code->Description = curline+3;
311 if (sscanf(curline, "%8X %*s", &address) == 1)
313 curr_code->Codes = (cheat_code*) realloc(curr_code->Codes, sizeof(cheat_code) * (curr_code->Count + 1));
314 if (strncmp(curline+9, "????", 4) == 0)
316 curr_code->Codes[curr_code->Count].var_count = 0;
317 CheatAddVariables(&curr_code->Codes[curr_code->Count], curline+14);
318 curr_code->VariableLine = curr_code->Count;
323 curr_code->Codes[curr_code->Count].var_count = 1;
324 curr_code->Codes[curr_code->Count].variables = (int*) malloc(sizeof(int));
325 if(curr_code->Codes[curr_code->Count].variables == NULL)
327 DebugMessage(M64MSG_WARNING, "couldn't allocate memory; ignoring line: '%s'", curline);
330 if (sscanf(curline+9, "%04X", &var) != 1)
332 curr_code->Codes[curr_code->Count].variables[0] = var;
333 curr_code->Codes[curr_code->Count].variable_names = NULL;
335 curr_code->Codes[curr_code->Count].var_to_use = 0;
336 curr_code->Codes[curr_code->Count].address = address;
341 /* otherwise we don't know what this line is */
342 DebugMessage(M64MSG_WARNING, "unrecognized line in cheat file: '%s'", curline);
347 void CheatStart(eCheatMode CheatMode, char *CheatNumList)
349 /* if cheat codes are disabled, then we don't have to do anything */
350 if (CheatMode == CHEAT_DISABLE || (CheatMode == CHEAT_LIST && strlen(CheatNumList) == 0))
352 DebugMessage(M64MSG_STATUS, "Cheat codes disabled.");
356 /* get the ROM header for the currently loaded ROM image from the core */
357 if ((*CoreDoCommand)(M64CMD_ROM_GET_HEADER, sizeof(l_RomHeader), &l_RomHeader) != M64ERR_SUCCESS)
359 DebugMessage(M64MSG_WARNING, "couldn't get ROM header information from core library");
363 /* generate section name from ROM's CRC and country code */
365 sprintf(RomSection, "%08X-%08X-C:%X", sl(l_RomHeader.CRC1), sl(l_RomHeader.CRC2), l_RomHeader.Country_code & 0xff);
367 /* parse through the cheat INI file and load up any cheat codes found for this ROM */
368 ReadCheats(RomSection);
369 if (!l_RomFound || l_CheatCodesFound == 0)
371 DebugMessage(M64MSG_WARNING, "no cheat codes found for ROM image '%.20s'", l_RomHeader.Name);
376 /* handle the list command */
377 if (CheatMode == CHEAT_SHOW_LIST)
379 DebugMessage(M64MSG_INFO, "%i cheat code(s) found for ROM '%s'", l_CheatCodesFound, l_CheatGameName);
380 sCheatInfo *pCur = l_CheatList;
383 if (pCur->Description == NULL)
384 DebugMessage(M64MSG_INFO, " %i: %s", pCur->Number, pCur->Name);
386 DebugMessage(M64MSG_INFO, " %i: %s (%s)", pCur->Number, pCur->Name, pCur->Description);
387 if(pCur->VariableLine != -1)
390 for (i = 0; i < pCur->Codes[pCur->VariableLine].var_count; i++)
391 DebugMessage(M64MSG_INFO, " %i: %s", i, pCur->Codes[pCur->VariableLine].variable_names[i]);
399 /* handle all cheats enabled mode */
400 if (CheatMode == CHEAT_ALL)
402 sCheatInfo *pCur = l_CheatList;
412 /* handle list of cheats enabled mode */
413 if (CheatMode == CHEAT_LIST)
418 while(CheatNumList != NULL && *CheatNumList)
420 if ((cheat_next = strchr(CheatNumList, ',')) != NULL)
426 if (strchr(CheatNumList, '-') != NULL)
428 sscanf(CheatNumList, "%i-%i", &number, &option); /* option */
433 sscanf(CheatNumList, "%i", &number);
436 pCheat = CheatFindCode(number);
438 DebugMessage(M64MSG_WARNING, "invalid cheat code number %i", number);
441 if (pCheat->VariableLine != -1 && pCheat->Count > pCheat->VariableLine && option < pCheat->Codes[pCheat->VariableLine].var_count)
442 pCheat->Codes[pCheat->VariableLine].var_to_use = option;
443 CheatActivate(pCheat);
446 CheatNumList = cheat_next;
453 /* otherwise the mode is invalid */
454 DebugMessage(M64MSG_WARNING, "internal error; invalid CheatMode in CheatStart()");