workaround a problem with the harmattan gcc
[drnoksnes] / hacks.cpp
index 69ba90d..7226738 100644 (file)
--- 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,37 +132,43 @@ 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);
+               line = fgets(line, kLineBufferSize, fp);
+               if (!line) break;
 
                char *pos = strchr(line, '|');
                if (!pos) continue;
                *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 +176,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);