MapFix

From Wolfire Games Wiki
Revision as of 05:11, 17 June 2011 by Conner36 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This article is a stub. You should help Wolfire by expanding it.
This is the source for the MapFix program. Check the dialogue editing section.

  #include <stdio.h>
  
  #define SKIP(i,n)      fseek(f[i], n, SEEK_CUR)
  
  int quit() {
     printf("(You can close this window.)\n");
     exit(0);
  }
  
  int exeunt(FILE *f, FILE *g) {
     fclose(f);
     fclose(g);
     quit();
  }
  
  FILE *fopenpath(char *path, char *name, char mode[]) {
     char pathname[250];
     strcpy(pathname, path);
     strcat(pathname, name);
     return( fopen(pathname, mode) );
  }
  
  int main (int argc, char *argv[]) {
  //-----------------------------------------------OPEN
     char answer[50], path[200];
     FILE *f[2];
  
     char x, goal[] = ":Data:";
     int goallen = 6;
     int i,j,k, okay = 0, length[2];
  
     strcpy(path, argv[0]);
     path[strlen(path)-6] = (char) 0;
        
     printf("\nWhat map needs to be repaired?\n\n");
     printf("Enter map name: ");
     scanf("%s",answer);
     
     f[0] = fopenpath(path, answer, "r+");
     
     if (f[0] == 0) {
        printf( "Can't see any file named \"%s\" in this folder.\n", answer);
        quit();
     }
  
     printf("\nWhat's your backup?\n\n");
     printf("Enter backup name: ");
     scanf("%s",answer);
     
     f[1] = fopenpath(path, answer, "r");
     
     if (f[1] == 0) {
        printf( "Can't see any file named \"%s\" in this folder.\n", answer);
        fclose(f[0]);
        quit();
     }
  
     printf("\nDo you want to repair the map or just the dialogue?\n\n");
     printf("m) Map. If the map crashes when you try to load it in Lugaru.\n");
     printf("d) Dialogue. If the dialogue is not showing up anymore.\n\n");
     printf("Pick one: ");   
     scanf("%s",answer);
     
     if (answer[0] != 'd') {
  //-----------------------------------------------REPAIR MAP
     
        for (i=0; i<2; i++) {   
           do {
           
              do x = fgetc( f[i] );
              while (x != goal[0]);
  
              okay = 1;
              for (k=1; k<goallen; k++)
                 if (fgetc(f[i]) != goal[k])
                    okay = 0;
              SKIP(i,-goallen+1);
  
           } while(okay == 0);
           
           SKIP(i,-4);
           length[i] = ftell(f[i]);
           
        }
     
        if (length[0] != length[1]) {
           printf("Looks like the backup file is too old. You lose.\n");
           exeunt(f[0],f[1]);
        } else {
           char *c;
           
           c = (char *)malloc( length[0] * sizeof(char) );
           fseek(f[0], 0, SEEK_SET);
           fseek(f[1], 0, SEEK_SET);
           fread(c, sizeof(char), length[0], f[1]);
           fwrite(c, sizeof(char), length[0], f[0]);
           
           printf("> The map has been successfully repaired (supposedly).\n");
           
           exeunt(f[0],f[1]);
        }
        
  //-----------------------------------------------REPAIR DIALOGUE
     } else {
        
        fseek(f[1], 11, SEEK_SET);
        
        if (fgetc(f[1]) ==  0) {
           fseek(f[0], 11, SEEK_SET);
           fputc(0, f[0]);
        }
        
        fseek(f[0], 0x2F, SEEK_SET);
        fseek(f[1], 0x2F, SEEK_SET);
        for (i=0x2F; i<0x41; i++)
           fputc(fgetc(f[1]), f[0]);
        
        printf("> The dialogue has been repaired (supposedly).\n");
        
        exeunt(f[0],f[1]);
     }
  }