Advertisement
Guest User

madparse

a guest
Feb 16th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. int main()
  5. {
  6.     FILE* f = fopen("mars.mad", "rb");
  7.     if (!f) return 1;
  8.  
  9.     char MadHeader[0x1C];
  10.     if (fread(MadHeader, 0x1C, 1, f) < 1)
  11.         return 2;
  12.  
  13.     if (strncmp(MadHeader, "WORLD&ART&DIRECTORY", 0x13))
  14.     {
  15.         printf("Not a valid MAD file\n");
  16.         return 3;
  17.     }
  18.  
  19.     int MadCount = *(int*)(MadHeader+0x14);
  20.     int MadOffset = *(int*)(MadHeader+0x18);
  21.  
  22.     fseek(f, MadOffset, SEEK_SET);
  23.  
  24.     char* MadTable = new char[MadCount*16];
  25.     fread(MadTable, 16, MadCount, f);
  26.  
  27.     char* v10 = MadTable;
  28.     for (int i = 0; i < MadCount*16; i++)
  29.         *v10++ -= 0x30;
  30.  
  31.     // lets convert this bullshit into a WAD file
  32.     FILE* f2 = fopen("mars.wad", "wb");
  33.     if (!f2) return 4;
  34.  
  35.     int MadCountReal = MadCount;
  36.     int MadOffsetC = MadOffset;
  37.  
  38.     for (int i = 0; i < MadCount; i++)
  39.     {
  40.         int LumpOffset = *(int*)(MadTable+i*16);
  41.         int LumpSize = *(int*)(MadTable+i*16+4);
  42.         char LumpName[9];
  43.         LumpName[8] = 0;
  44.         memcpy(LumpName, MadTable+i*16+8, 8);
  45.  
  46.         if (LumpSize < 0)
  47.             continue;
  48.  
  49.         if (LumpSize)
  50.         {
  51.             char* lump = new char[LumpSize];
  52.             fseek(f, LumpOffset, SEEK_SET);
  53.             fseek(f2, LumpOffset, SEEK_SET);
  54.             fread(lump, LumpSize, 1, f);
  55.             fwrite(lump, LumpSize, 1, f2);
  56.             delete[] lump;
  57.         }
  58.     }
  59.  
  60.     for (int i = 0; i < MadCount; i++)
  61.     {
  62.         int LumpOffset = *(int*)(MadTable+i*16);
  63.         int LumpSize = *(int*)(MadTable+i*16+4);
  64.         char LumpName[9];
  65.         LumpName[8] = 0;
  66.         memcpy(LumpName, MadTable+i*16+8, 8);
  67.  
  68.         if (LumpSize < 0)
  69.         {
  70.             MadCountReal--;
  71.             continue;
  72.         }
  73.  
  74.         fseek(f2, MadOffsetC, SEEK_SET);
  75.         MadOffsetC += 16;
  76.         fwrite(&LumpOffset, 4, 1, f2);
  77.         fwrite(&LumpSize, 4, 1, f2);
  78.         fwrite(LumpName, 8, 1, f2);
  79.     }
  80.  
  81.     fseek(f2, 0, SEEK_SET);
  82.     fwrite("PWAD", 4, 1, f2);
  83.     fwrite(&MadCount, 4, 1, f2);
  84.     fwrite(&MadOffset, 4, 1, f2);
  85.  
  86.     fclose(f2);
  87.     fclose(f);
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement