|
1 | 1 | #define PROGRAM_NAME "make_patch"
|
2 |
| -#define USAGE_OPTS "labels.sym constants.sym patched.gbc original.gbc vc.patch.template vc.patch" |
| 2 | +#define USAGE_OPTS "labels.sym patched.gbc original.gbc vc.patch.template vc.patch" |
3 | 3 |
|
4 | 4 | #include "common.h"
|
5 | 5 |
|
@@ -113,21 +113,22 @@ void parse_symbol_value(char *input, int *restrict bank, int *restrict address)
|
113 | 113 | }
|
114 | 114 | }
|
115 | 115 |
|
116 |
| -void parse_symbols(const char *filename, struct Symbol **symbols) { |
| 116 | +struct Symbol *parse_symbols(const char *filename) { |
117 | 117 | FILE *file = xfopen(filename, 'r');
|
118 | 118 | struct Buffer *buffer = buffer_create(1);
|
119 | 119 |
|
120 | 120 | enum { SYM_PRE, SYM_VALUE, SYM_SPACE, SYM_NAME } state = SYM_PRE;
|
121 | 121 | int bank = 0;
|
122 | 122 | int address = 0;
|
| 123 | + struct Symbol *symbols = NULL; |
123 | 124 |
|
124 | 125 | for (;;) {
|
125 | 126 | int c = getc(file);
|
126 | 127 | if (c == EOF || c == '\n' || c == '\r' || c == ';' || (state == SYM_NAME && (c == ' ' || c == '\t'))) {
|
127 | 128 | if (state == SYM_NAME) {
|
128 | 129 | // The symbol name has ended; append the buffered symbol
|
129 | 130 | buffer_append(buffer, &(char []){'\0'});
|
130 |
| - symbol_append(symbols, buffer->data, bank, address); |
| 131 | + symbol_append(&symbols, buffer->data, bank, address); |
131 | 132 | }
|
132 | 133 | // Skip to the next line, ignoring anything after the symbol value and name
|
133 | 134 | state = SYM_PRE;
|
@@ -156,6 +157,7 @@ void parse_symbols(const char *filename, struct Symbol **symbols) {
|
156 | 157 |
|
157 | 158 | fclose(file);
|
158 | 159 | buffer_free(buffer);
|
| 160 | + return symbols; |
159 | 161 | }
|
160 | 162 |
|
161 | 163 | int strfind(const char *s, const char *list[], int count) {
|
@@ -342,6 +344,12 @@ struct Buffer *process_template(const char *template_filename, const char *patch
|
342 | 344 |
|
343 | 345 | // The ROM checksum will always differ
|
344 | 346 | buffer_append(patches, &(struct Patch){0x14e, 2});
|
| 347 | + // The Stadium data (see stadium.c) will always differ |
| 348 | + unsigned int rom_size = (unsigned int)xfsize("", orig_rom); |
| 349 | + if (rom_size == 128 * 0x4000) { |
| 350 | + unsigned int stadium_size = 24 + 6 + 2 + 128 * 2 * 2; |
| 351 | + buffer_append(patches, &(struct Patch){rom_size - stadium_size, stadium_size}); |
| 352 | + } |
345 | 353 |
|
346 | 354 | // Fill in the template
|
347 | 355 | const struct Symbol *current_hook = NULL;
|
@@ -411,7 +419,7 @@ struct Buffer *process_template(const char *template_filename, const char *patch
|
411 | 419 | int compare_patch(const void *patch1, const void *patch2) {
|
412 | 420 | unsigned int offset1 = ((const struct Patch *)patch1)->offset;
|
413 | 421 | unsigned int offset2 = ((const struct Patch *)patch2)->offset;
|
414 |
| - return offset1 > offset2 ? 1 : offset1 < offset2 ? -1 : 0; |
| 422 | + return (offset1 > offset2) - (offset1 < offset2); |
415 | 423 | }
|
416 | 424 |
|
417 | 425 | bool verify_completeness(FILE *restrict orig_rom, FILE *restrict new_rom, struct Buffer *patches) {
|
@@ -443,20 +451,18 @@ bool verify_completeness(FILE *restrict orig_rom, FILE *restrict new_rom, struct
|
443 | 451 | }
|
444 | 452 |
|
445 | 453 | int main(int argc, char *argv[]) {
|
446 |
| - if (argc != 7) { |
| 454 | + if (argc != 6) { |
447 | 455 | usage_exit(1);
|
448 | 456 | }
|
449 | 457 |
|
450 |
| - struct Symbol *symbols = NULL; |
451 |
| - parse_symbols(argv[1], &symbols); |
452 |
| - parse_symbols(argv[2], &symbols); |
| 458 | + struct Symbol *symbols = parse_symbols(argv[1]); |
453 | 459 |
|
454 |
| - FILE *new_rom = xfopen(argv[3], 'r'); |
455 |
| - FILE *orig_rom = xfopen(argv[4], 'r'); |
456 |
| - struct Buffer *patches = process_template(argv[5], argv[6], new_rom, orig_rom, symbols); |
| 460 | + FILE *new_rom = xfopen(argv[2], 'r'); |
| 461 | + FILE *orig_rom = xfopen(argv[3], 'r'); |
| 462 | + struct Buffer *patches = process_template(argv[4], argv[5], new_rom, orig_rom, symbols); |
457 | 463 |
|
458 | 464 | if (!verify_completeness(orig_rom, new_rom, patches)) {
|
459 |
| - fprintf(stderr, PROGRAM_NAME ": Warning: Not all ROM differences are defined by \"%s\"\n", argv[6]); |
| 465 | + fprintf(stderr, PROGRAM_NAME ": Warning: Not all ROM differences are defined by \"%s\"\n", argv[5]); |
460 | 466 | }
|
461 | 467 |
|
462 | 468 | symbol_free(symbols);
|
|
0 commit comments