/* * ============================================================================ * Simple test program for resource manipulation routines J. Zbiciak * * WARNING: THIS PROGRAM IS A MAJOR, MAJOR QUICK AND DIRTY HACK. :-) * * Reads a resource file and replaces cart images with the contents of * eggs1.bin in the current directory. * ============================================================================ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include "res_file.h" #include "file_util.h" #include "macros.h" /* * ============================================================================ * Syntax: re_res [-o] file.res file2.res * ============================================================================ */ /* * ============================================================================ * ============================================================================ * UTILITY FUNCTIONS * ============================================================================ * ============================================================================ */ /* * ============================================================================ * MAIN * ============================================================================ */ int main(int argc, char *argv[]) { int overwrite = 0, raw_images = 0, i, ok; res_rec_t *rec; FILE *f; void *raw_img; size_t file_len; while (argc > 3) { if (argv[1][0] != '-') break; switch (argv[1][1]) { case 'o' : overwrite = 1; ok = 1; break; case 'r' : raw_images = 1; ok = 1; break; default : fprintf(stderr,"Invalid flag '%s'\n", argv[1]); ok = 0; } if (!ok) break; argc -= ok; argv += ok; } if (argc != 3) { fprintf(stderr,"Usage: %s [-o] [-r] file.res file2.res\n", argv[0]?argv[0]:"re_res"); exit(1); } f = fopen(argv[1], "r"); if (!f) { perror("fopen()"); fprintf(stderr,"Error opening resource archive '%s'\n",argv[1]); exit(1); } fseek(f, 0, SEEK_END); file_len = ftell(f); raw_img = malloc(file_len); if (!raw_img) { perror("malloc()"); fprintf(stderr,"Error allocating %d bytes for memory image\n", file_len); exit(1); } fseek(f, 0, SEEK_SET); fread(raw_img, 1, file_len, f); fclose(f); /* XXX: TODO: This really should be in the res_file library. */ rec = malloc(sizeof(res_rec_t)); res_init_rec(rec, NULL, NULL, NULL); rec->rec_state = RES_s_encoded; rec->type = RES_t_archive; rec->res.data = raw_img; rec->len = file_len; if (res_decode(rec, (RES_RECURSE | RES_ALL) & (raw_images ? ~(MASK_BIT(RES_t_image)| MASK_BIT(RES_t_font)) : ~0), NULL, NULL )) { fprintf(stderr,"Decode aborted!\n"); for (i = 0; ilib_state->num_err; i++) { fprintf(stderr,"Decode error: %s\n",rec->lib_state->err_msg[i]); } exit(1); } /* HACK: Substitute eggs1.bin for each of the known games' GAME resource */ if (1) { FILE *egg; char *data; int len, drop = 0; res_rec_t *r1, *r2; egg = fopen("eggs1.bin","r"); data = malloc(16384); len = fread(data,1,16384,egg); fclose(egg); r1 = rec->res.arch->res_rec; while (r1) { while (r1) { if (!strncmp(r1->ident,"NITESTLK.RES",12) || !strncmp(r1->ident,"SPCSPART.RES",12) || !strncmp(r1->ident,"POOL.RES",12) || !strncmp(r1->ident,"ASTRSMSH.RES",12) || !strncmp(r1->ident,"SKIING.RES",12) || !strncmp(r1->ident,"UTOPIA.RES",12)) break; r1 = r1->next; } if (!r1) { if (drop < 1) { fprintf(stderr,"Eggdrop procedure failed.\n"); exit(1); } break; } printf("Attempting eggdrop in %-12s ... ", r1->ident); r2 = r1->res.arch->res_rec; while (r2 && strncmp(r2->ident,"GAME",12)) r2 = r2->next; if (!r2) { printf("Eggdrop failed at step 2\n"); continue; } free(r2->res.data); r2->res.data = malloc(len); r2->len = len; memcpy(r2->res.data, data, len); printf("Eggdrop succeeded.\n"); drop++; r1 = r1->next; } if (drop > 0) printf("Performed %d eggdrops\n", drop); } if (res_encode(rec)) { fprintf(stderr,"Encode aborted!\n"); for (i = 0; ilib_state->num_err; i++) { fprintf(stderr,"Encode error: %s\n",rec->lib_state->err_msg[i]); } exit(1); } f = fopen(argv[2],"w"); if (!f) { fprintf(stderr,"couldn't open output file\n"); exit(1); } fwrite(rec->res.data, 1, rec->len, f); fclose(f); printf("Wrote %d bytes to %s\n",rec->len,argv[2]); return 0; } /* * ============================================================================ * Copyright (c) 1998, Joseph Zbiciak. * ============================================================================ */