X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;ds=sidebyside;f=hacks.cpp;h=191c704f8556ec2b16cd6563818f37b417026300;hb=3315a1d5bdaef093a055d312dd3d4b3c611518e3;hp=69ba90d54fd550ce95791726fd435441f09360db;hpb=909f5635bffc4c3516ddbba4f9c63c2a7333afc7;p=drnoksnes diff --git a/hacks.cpp b/hacks.cpp index 69ba90d..191c704 100644 --- a/hacks.cpp +++ b/hacks.cpp @@ -24,12 +24,18 @@ static unsigned long getGameCrc32() static int loadHacks(char * line) { int count = 0; + bool end_of_line = false; char *pos = strchr(line, '|'), *start = line; - // Skip: Title[start..pos] + // Title[start..pos] + *pos = '\0'; + printf("Hacks: detected \"%s\"\n", start); start = pos + 1; pos = strchr(start, '|'); - if (!pos) return -1; + if (!pos) { + // If there are no flags, jump directly to hacks + goto parse_hacks; + } // Skip: Flags1[start..pos] start = pos + 1; @@ -59,8 +65,8 @@ static int loadHacks(char * line) if (!pos) return 0; // No patches! start = pos + 1; - bool end_of_line = false; - printf("Loading patches: %s", start); + +parse_hacks: do { char *end; unsigned long addr; @@ -106,10 +112,12 @@ static int loadHacks(char * line) char valStr[3] = { pos[0], pos[1], '\0' }; unsigned char val = strtoul(valStr, 0, 16); +#ifdef DEBUG printf("ROM[0x%lx..0x%lx]=0x%hhx 0x%hhx 0x%hhx\n", addr + i - 1, addr + i + 1, ROM[addr + i - 1], ROM[addr + i], ROM[addr + i + 1]); printf("--> ROM[0x%lx]=0x%hhx\n", addr + i, val); +#endif ROM[addr + i] = val; count++; @@ -124,19 +132,24 @@ static int loadHacks(char * line) void S9xHacksLoadFile(const char * file) { + unsigned long gameCrc; + char * line; + FILE * fp; + if (!Settings.HacksEnabled) goto no_hacks; if (!file) goto no_hacks; // At this point, the ROM is already loaded. - FILE * fp = fopen(file, "r"); + fp = fopen(file, "r"); if (!fp) { fprintf(stderr, "Can't open hacks file %s: %s\n", file, strerror(errno)); goto no_hacks; } - const unsigned long gameCrc = getGameCrc32(); - char * line = (char*) malloc(kLineBufferSize + 1); + // Get current ROM CRC + gameCrc = getGameCrc32(); + line = (char*) malloc(kLineBufferSize + 1); do { fgets(line, kLineBufferSize, fp); @@ -145,16 +158,16 @@ void S9xHacksLoadFile(const char * file) *pos = '\0'; if (gameCrc == parseCrc32(line)) { - // A hit! :) + // Hit! This line's CRC matches our current ROM CRC. int res = loadHacks(pos + 1); if (res > 0) { - printf("Hacks: searched %s for crc32 %lx, %d hacks loaded\n", - file, gameCrc, res); + printf("Hacks: searched %s for crc %lX, %d byte%s patched\n", + file, gameCrc, res, (res == 1 ? "" : "s")); } else if (res < 0) { - printf("Hacks: searched %s for crc32 %lx, error parsing line\n", + printf("Hacks: searched %s for crc %lX, error parsing line\n", file, gameCrc); } else { - printf("Hacks: searched %s for crc32 %lx, no hacks\n", + printf("Hacks: searched %s for crc %lX, no hacks\n", file, gameCrc); } goto hacks_found; @@ -162,10 +175,10 @@ void S9xHacksLoadFile(const char * file) } while (!feof(fp) && !ferror(fp)); if (ferror(fp)) { - fprintf(stderr, "Error reading hacks file: %s\n"); + fprintf(stderr, "Error reading hacks file: %s\n", file); } - printf("Hacks: searched %s for crc %lu; nothing found\n", file, gameCrc); + printf("Hacks: searched %s for crc %lX; nothing found\n", file, gameCrc); hacks_found: free(line);