home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the DOOM Programming Gurus / Tricks_of_the_Doom_Programming_Gurus.iso / bonus / editors / deth / source / wads.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-02  |  18.7 KB  |  672 lines

  1. /*
  2.    Doom Editor Utility, by Brendon Wyber and RaphaĆ«l Quinet.
  3.    
  4.    You are allowed to use any parts of this code in another program, as
  5.    long as you give credits to the authors in the documentation and in
  6.    the program itself.  Read the file README.1ST for more information.
  7.    
  8.    This program comes with absolutely no warranty.
  9.    
  10.    WAD.C - Wad files routines.
  11.    */
  12.  
  13. /* the includes */
  14. #include "deu.h"
  15.  
  16. /* global variables */
  17. WadPtr WadFileList = NULL;       /* linked list of wad files */
  18. MDirPtr MasterDir = NULL;        /* the master directory */
  19. SList LevelNames = NULL; /* list of levels for the current game */
  20.  
  21. /*
  22.    open the main wad file, read in its directory and create the
  23.    master directory
  24.    */
  25.  
  26. void OpenMainWad( char *filename)
  27. {
  28.     MDirPtr lastp, newp;
  29.     long n;
  30.     WadPtr wad;
  31.     
  32.     /* open the wad file */
  33.     printf( "Loading main WAD file: %s...\n", filename);
  34.     wad = BasicWadOpen( filename);
  35.     if (strncmp( wad->type, "IWAD", 4))
  36.         ProgError( "\"%s\" is not the main WAD file", filename);
  37.     
  38.     /* create the master directory */
  39.     lastp = NULL;
  40.     for (n = 0; n < wad->dirsize; n++) {
  41.         newp = (MDirPtr) GetMemory( sizeof( struct MasterDirectory));
  42.         newp->next = NULL;
  43.         newp->wadfile = wad;
  44.         memcpy( &(newp->dir), &(wad->directory[ n]), sizeof( struct Directory));
  45.         if (MasterDir)
  46.             lastp->next = newp;
  47.         else
  48.             MasterDir = newp;
  49.         lastp = newp;
  50.     }
  51.     
  52.     if(FindMasterDir( MasterDir, "MAP01"))
  53.         Doom2 = TRUE;
  54.     else
  55.         Doom2 = FALSE;
  56.     
  57.     /* check if registered version */
  58.     Registered = FALSE; /* If you remove this, bad things will happen to you... */
  59.     if ( RegTest && FindMasterDir( MasterDir, RegTest))
  60.         Registered = TRUE;
  61.     
  62.     if(!Registered) {
  63.         printf( "   *-------------------------------------------------*\n");
  64.         printf( "   |     Warning: this is the shareware version.     |\n");
  65.         printf( "   |   You won't be allowed to save your changes.    |\n");
  66.         printf( "   |     PLEASE REGISTER YOUR COPY OF THE GAME.      |\n");
  67.         printf( "   *-------------------------------------------------*\n");
  68.     }
  69. }
  70.  
  71. /* short helper function for the next fn */
  72. int isalev(char *s)
  73. {
  74.     SList p;
  75.  
  76.     for(p = LevelNames; p; p = p->next)
  77.         if(!strcmp(p->string, s))
  78.             return 1;
  79.  
  80.     return 0;
  81. }
  82.  
  83.  
  84. /*
  85.    open a patch wad file, read in its directory and alter the master
  86.    directory
  87.    */
  88.  
  89. void OpenPatchWad( char *filename)
  90. {
  91.     WadPtr wad;
  92.     MDirPtr mdir = NULL;
  93.     BCINT n, l;
  94.     char entryname[9];
  95.     
  96.     /* ignore the file if it doesn't exist */
  97.     if (! Exists( filename)) {
  98.         printf( "Warning: patch WAD file \"%s\" doesn't exist.  Ignored.\n", filename);
  99.         return;
  100.     }
  101.  
  102.     /* if we have loaded the TEXTURE1 & TEXTURE2 resources, now 
  103.        would be a good time to get rid of them, in case the
  104.        wad we are now opening contains its own versions of them */
  105.  
  106.     ForgetAllResources();
  107.     
  108.     /* open the wad file */
  109.     printf( "Loading patch WAD file: %s...\n", filename);
  110.     wad = BasicWadOpen( filename);
  111.     if (strncmp( wad->type, "PWAD", 4))
  112.         ProgError( "\"%s\" is not a patch WAD file", filename);
  113.     
  114.     /* alter the master directory */
  115.     l = 0;
  116.     for (n = 0; n < wad->dirsize; n++) {
  117.         strncpy( entryname, wad->directory[ n].name, 8);
  118.         entryname[8] = '\0';
  119.         if (l == 0) {
  120.             mdir = FindMasterDir( MasterDir, wad->directory[ n].name);
  121.             /* if this entry is not in the master directory, then add it */
  122.             if (mdir == NULL) {
  123.                 printf( "   [Adding new entry %s]\n", entryname);
  124.                 mdir = MasterDir;
  125.                 while (mdir->next)
  126.                     mdir = mdir->next;
  127.                 mdir->next = (MDirPtr) GetMemory( sizeof( struct MasterDirectory));
  128.                 mdir = mdir->next;
  129.                 mdir->next = NULL;
  130.             }
  131.             /* if this is a level, then copy this entry and the next 10 */
  132.             else if (isalev(wad->directory[ n].name)) {
  133.                 printf( "   [Updating level %s]\n", entryname);
  134.                 l = 10;
  135.             }
  136.             else
  137.                 printf( "   [Updating entry %s]\n", entryname);
  138.         }
  139.         else {
  140.             mdir = mdir->next;
  141.             /* the level data should replace an existing level */
  142.             if (mdir == NULL || strncmp(mdir->dir.name, wad->directory[ n].name, 8))
  143.                 ProgError( "\\%s\" is not an understandable PWAD file (error with %s)", filename, entryname);
  144.             l--;
  145.         }
  146.         mdir->wadfile = wad;
  147.         memcpy( &(mdir->dir), &(wad->directory[ n]), sizeof( struct Directory));
  148.     }
  149. }
  150.  
  151.  
  152.  
  153. /*
  154.    close all the wad files, deallocating the WAD file structures
  155.    */
  156.  
  157. void CloseWadFiles()
  158. {
  159.     WadPtr curw, nextw;
  160.     MDirPtr curd, nextd;
  161.     
  162.     /* close the wad files */
  163.     curw = WadFileList;
  164.     WadFileList = NULL;
  165.     while (curw) {
  166.         nextw = curw->next;
  167.         fclose( curw->fileinfo);
  168.         FreeMemory( curw->directory);
  169.         FreeMemory( curw);
  170.         curw = nextw;
  171.     }
  172.     
  173.     /* delete the master directory */
  174.     curd = MasterDir;
  175.     MasterDir = NULL;
  176.     while (curd) {
  177.         nextd = curd->next;
  178.         FreeMemory( curd);
  179.         curd = nextd;
  180.     }
  181. }
  182.  
  183.  
  184.  
  185. /*
  186.    forget unused patch wad files
  187.    */
  188.  
  189. void CloseUnusedWadFiles()
  190. {
  191.     WadPtr curw, prevw;
  192.     MDirPtr mdir;
  193.     
  194.     prevw = NULL;
  195.     curw = WadFileList;
  196.     while (curw) {
  197.         /* check if the wad file is used by a directory entry */
  198.         mdir = MasterDir;
  199.         while (mdir && mdir->wadfile != curw)
  200.             mdir = mdir->next;
  201.         if (mdir)
  202.             prevw = curw;
  203.         else {
  204.             /* if this wad file is never used, close it */
  205.             if (prevw)
  206.                 prevw->next = curw->next;
  207.             else
  208.                 WadFileList = curw->next;
  209.             fclose( curw->fileinfo);
  210.             FreeMemory( curw->directory);
  211.             FreeMemory( curw);
  212.         }
  213.         curw = prevw->next;
  214.     }
  215. }
  216.  
  217.  
  218.  
  219. /*
  220.    basic opening of WAD file and creation of node in Wad linked list
  221.    */
  222.  
  223. WadPtr BasicWadOpen( char *filename)
  224. {
  225.     WadPtr curw, prevw;
  226.     
  227.     /* find the wad file in the wad file list */
  228.     prevw = WadFileList;
  229.     if (prevw) {
  230.         curw = prevw->next;
  231.         while (curw && strcmp( filename, curw->filename)) {
  232.             prevw = curw;
  233.             curw = prevw->next;
  234.         }
  235.     }
  236.     else
  237.         curw = NULL;
  238.     
  239.     /* if this entry doesn't exist, add it to the WadFileList */
  240.     if (curw == NULL) {
  241.         curw = (WadPtr) GetMemory( sizeof( struct WadFileInfo));
  242.         if (prevw == NULL)
  243.             WadFileList = curw;
  244.         else
  245.             prevw->next = curw;
  246.         curw->next = NULL;
  247.         curw->filename = filename;
  248.     }
  249.     
  250.     /* open the file */
  251.     if ((curw->fileinfo = fopen( filename, "rb")) == NULL) {
  252.         if (!prevw)  {                
  253.             WadFileList = NULL;
  254.         }
  255.         else {
  256.             prevw->next = curw->next;
  257.         }
  258.         FreeMemory( curw);
  259.         ProgError( "error opening \"%s\"", filename);
  260.     }
  261.     /* read in the WAD directory info */
  262.     BasicWadRead( curw, curw->type, 4);
  263.     if (strncmp( curw->type, "IWAD", 4) && strncmp( curw->type, "PWAD", 4))
  264.         ProgError( "\"%s\" is not a valid WAD file", filename);
  265.     BasicWadRead( curw, &curw->dirsize, sizeof( curw->dirsize));
  266.     BasicWadRead( curw, &curw->dirstart, sizeof( curw->dirstart));
  267.     
  268.     /* read in the WAD directory itself */
  269.     curw->directory = (DirPtr) GetMemory( sizeof( struct Directory) * curw->dirsize);
  270.     BasicWadSeek( curw, curw->dirstart);
  271.     BasicWadRead( curw, curw->directory, sizeof( struct Directory) * curw->dirsize);
  272.     
  273.     /* all done */
  274.     return curw;
  275. }
  276.  
  277.  
  278.  
  279. /*
  280.    read bytes from a file and store it into an address with error checking
  281.    */
  282.  
  283. void BasicWadRead( WadPtr wadfile, void huge *addr, long size)
  284. {
  285.     if (fread( addr, 1, size, wadfile->fileinfo) != size)
  286.         ProgError( "error reading from \"%s\", size %d", wadfile->filename,
  287.                   size);
  288. }
  289.  
  290.  
  291.  
  292. /*
  293.    go to offset of wad file with error checking
  294.    */
  295.  
  296. void BasicWadSeek( WadPtr wadfile, long offset)
  297. {
  298.     if (fseek( wadfile->fileinfo, offset, 0))
  299.         ProgError( "error reading from \"%s\"", wadfile->filename);
  300. }
  301.  
  302.  
  303.  
  304. /*
  305.    find an entry in the master directory
  306.    */
  307.  
  308. MDirPtr FindMasterDir( MDirPtr from, char *name)
  309. {
  310.     while (from) {
  311.         if (!strncmp( from->dir.name, name, 8))
  312.             break;
  313.         from = from->next;
  314.     }
  315.     return from;
  316. }
  317.  
  318.  
  319.  
  320. /*
  321.    list the master directory
  322.    */
  323.  
  324. void ListMasterDirectory( FILE *file)
  325. {
  326.     char dataname[ 9];
  327.     MDirPtr dir;
  328.     char key;
  329.     BCINT lines = 3;
  330.     
  331.     dataname[ 8] = '\0';
  332.     fprintf( file, "The Master Directory\n");
  333.     fprintf( file, "====================\n\n");
  334.     fprintf( file, "NAME____  FILE________________  SIZE__  START____\n");
  335.     for (dir = MasterDir; dir; dir = dir->next) {
  336.         strncpy( dataname, dir->dir.name, 8);
  337.         fprintf( file, "%-8s  %-20s  %6ld  x%08lx\n", dataname, dir->wadfile->filename, dir->dir.size, dir->dir.start);
  338.         if (file == stdout && lines++ > 21) {
  339.             lines = 0;
  340.             printf( "[Q to abort, any other key to continue]");
  341.             key = bioskey( 0);
  342.             printf( "\r                                       \r");
  343.             if (key == 'Q' || key == 'q')
  344.                 break;
  345.         }
  346.     }
  347. }
  348.  
  349.  
  350.  
  351. /*
  352.    list the directory of a file
  353.    */
  354.  
  355. void ListFileDirectory( FILE *file, WadPtr wad)
  356. {
  357.     char dataname[ 9];
  358.     char key;
  359.     BCINT lines = 5;
  360.     long n;
  361.     
  362.     dataname[ 8] = '\0';
  363.     fprintf( file, "WAD File Directory\n");
  364.     fprintf( file, "==================\n\n");
  365.     fprintf( file, "Wad File: %s\n\n", wad->filename);
  366.     fprintf( file, "NAME____  SIZE__  START____  END______\n");
  367.     for (n = 0; n < wad->dirsize; n++) {
  368.         strncpy( dataname, wad->directory[n].name, 8);
  369.         fprintf( file, "%-8s  %6ld  x%08lx  x%08lx\n", dataname, wad->directory[n].size, wad->directory[n].start, wad->directory[n].size + wad->directory[n].start - 1);
  370.         if (file == stdout && lines++ > 21) {
  371.             lines = 0;
  372.             printf( "[Q to abort, any other key to continue]");
  373.             key = bioskey( 0);
  374.             printf( "\r                                       \r");
  375.             if (key == 'Q' || key == 'q')
  376.                 break;
  377.         }
  378.     }
  379. }
  380.  
  381.  
  382.  
  383. /*
  384.    build a new wad file from master dictionary
  385.    */
  386.  
  387. void BuildNewMainWad( char *filename, Bool patchonly)
  388. {
  389.     FILE *file;
  390.     long counter = 12;
  391.     MDirPtr cur;
  392.     long size;
  393.     long dirstart;
  394.     long dirnum;
  395.     
  396.     /* open the file and store signatures */
  397.     if (patchonly)
  398.         printf( "Building a compound Patch Wad file \"%s\".\n", filename);
  399.     else
  400.         printf( "Building a new Main Wad file \"%s\" (size approx 10000K)\n", filename);
  401.     if (!(Doom2 || Registered))
  402.         ProgError( "You were warned: you are not allowed to do this.");
  403.     if ((file = fopen( filename, "wb")) == NULL)
  404.         ProgError( "unable to open file \"%s\"", filename);
  405.     if (patchonly)
  406.         WriteBytes( file, "PWAD", 4);
  407.     else
  408.         WriteBytes( file, "IWAD", 4);
  409.     WriteBytes( file, &counter, 4L);      /* put true value in later */
  410.     WriteBytes( file, &counter, 4L);      /* put true value in later */
  411.     
  412.     /* output the directory data chuncks */
  413.     for (cur = MasterDir; cur; cur = cur->next) {
  414.         if (patchonly && cur->wadfile == WadFileList)
  415.             continue;
  416.         size = cur->dir.size;
  417.         counter += size;
  418.         BasicWadSeek( cur->wadfile, cur->dir.start);
  419.         CopyBytes( file, cur->wadfile->fileinfo, size);
  420.         printf( "Size: %ldK\r", counter / 1024);
  421.     }
  422.     
  423.     /* output the directory */
  424.     dirstart = counter;
  425.     counter = 12;
  426.     dirnum = 0;
  427.     for (cur = MasterDir; cur; cur = cur->next) {
  428.         if (patchonly && cur->wadfile == WadFileList)
  429.             continue;
  430.         if (dirnum % 100 == 0)
  431.             printf( "Outputting directory %04ld...\r", dirnum);
  432.         if (cur->dir.start)
  433.             WriteBytes( file, &counter, 4L);
  434.         else
  435.             WriteBytes( file, &(cur->dir.start), 4L);
  436.         WriteBytes( file, &(cur->dir.size), 4L);
  437.         WriteBytes( file, &(cur->dir.name), 8L);
  438.         counter += cur->dir.size;
  439.         dirnum++;
  440.     }
  441.     
  442.     /* fix up the number of entries and directory start information */
  443.     if (fseek( file, 4L, 0))
  444.         ProgError( "error writing to file");
  445.     WriteBytes( file, &dirnum, 4L);
  446.     WriteBytes( file, &dirstart, 4L);
  447.     
  448.     /* close the file */
  449.     printf( "                            \r");
  450.     fclose( file);
  451. }
  452.  
  453.  
  454.  
  455. /*
  456.    output bytes to a binary file with error checking
  457.    */
  458.  
  459. void WriteBytes( FILE *file, void huge *addr, long size)
  460. {
  461.     if (!(Registered || Doom2))
  462.         return;
  463.     while (size > 0x8000) {
  464.         if (fwrite( addr, 1, 0x8000, file) != 0x8000)
  465.             ProgError( "error writing to file");
  466.         addr = (char huge *)addr + 0x8000;
  467.         size -= 0x8000;
  468.     }
  469.     if (fwrite( addr, 1, size, file) != size)
  470.         ProgError( "error writing to file");
  471. }
  472.  
  473.  
  474.  
  475. /*
  476.    copy bytes from a binary file to another with error checking
  477.    */
  478.  
  479. void CopyBytes( FILE *dest, FILE *source, long size)
  480. {
  481.     void huge *data;
  482.     
  483.     if (!(Registered || Doom2))
  484.         return;
  485.     data = GetFarMemory( 0x8000 + 2);
  486.     while (size > 0x8000) {
  487.         if (fread( data, 1, 0x8000, source) != 0x8000)
  488.             ProgError( "error reading from file");
  489.         if (fwrite( data, 1, 0x8000, dest) != 0x8000)
  490.             ProgError( "error writing to file");
  491.         size -= 0x8000;
  492.     }
  493.     if (fread( data, 1, size, source) != size)
  494.         ProgError( "error reading from file");
  495.     if (fwrite( data, 1, size, dest) != size)
  496.         ProgError( "error writing to file");
  497.     FreeFarMemory( data);
  498. }
  499.  
  500.  
  501.  
  502. /*
  503.    check if a file exists and is readable
  504.    */
  505.  
  506. Bool Exists( char *filename)
  507. {
  508.     FILE *test;
  509.     
  510.     if ((test = fopen( filename, "rb")) == NULL)
  511.         return FALSE;
  512.     fclose( test);
  513.     return TRUE;
  514. }
  515.  
  516.  
  517.  
  518. /*
  519.    dump a directory entry in hex
  520.    */
  521.  
  522. void DumpDirectoryEntry( FILE *file, char *entryname)
  523. {
  524.     MDirPtr entry;
  525.     char dataname[ 9];
  526.     char key;
  527.     BCINT lines = 5;
  528.     long n, c, i;
  529.     unsigned char buf[16];
  530.     
  531.     
  532.     c = 0;
  533.     entry = MasterDir;
  534.     while (entry) {
  535.         if (!strnicmp( entry->dir.name, entryname, 8)) {
  536.             strncpy( dataname, entry->dir.name, 8);
  537.             dataname[ 8] = '\0';
  538.             fprintf( file, "Contents of entry %s (size = %ld bytes):\n", dataname, entry->dir.size);
  539.             BasicWadSeek( entry->wadfile, entry->dir.start);
  540.             n = 0;
  541.             i = -1;
  542.             for (c = 0; c < entry->dir.size; c += i) {
  543.                 fprintf( file, "%04lX: ", n);
  544.                 for (i = 0; i < 16; i++) {
  545.                     BasicWadRead( entry->wadfile, &(buf[ i]), 1);
  546.                     fprintf( file, " %02X", buf[ i]);
  547.                     n++;
  548.                 }
  549.                 fprintf( file, "   ");
  550.                 for (i = 0; i < 16; i++) {
  551.                     if (buf[ i] >= 32)
  552.                         fprintf( file, "%c", buf[ i]);
  553.                     else
  554.                         fprintf( file, " ");
  555.                 }
  556.                 fprintf( file, "\n");
  557.                 if (file == stdout && lines++ > 21) {
  558.                     lines = 0;
  559.                     printf( "[%ld%% - Q to abort, S to skip this entry, any other key to continue]", n * 100 / entry->dir.size);
  560.                     key = bioskey( 0);
  561.                     printf( "\r                                                                    \r");
  562.                     if (key == 'S' || key == 's')
  563.                         break;
  564.                     if (key == 'Q' || key == 'q')
  565.                         return;
  566.                 }
  567.             }
  568.         }
  569.         entry = entry->next;
  570.     }
  571.     if (! c) {
  572.         printf( "[Entry not in master directory]\n");
  573.         return;
  574.     }
  575. }
  576.  
  577.  
  578.  
  579. /*
  580.    save a directory entry to disk
  581.    */
  582.  
  583. void SaveDirectoryEntry( FILE *file, char *entryname)
  584. {
  585.     MDirPtr entry;
  586.     long    counter;
  587.     long    size;
  588.     
  589.     for (entry = MasterDir; entry; entry = entry->next)
  590.         if (!strnicmp( entry->dir.name, entryname, 8))
  591.             break;
  592.     if (entry) {
  593.         WriteBytes( file, "PWAD", 4L);     /* PWAD file */
  594.         counter = 1L;
  595.         WriteBytes( file, &counter, 4L);   /* 1 entry */
  596.         counter = 12L;
  597.         WriteBytes( file, &counter, 4L);
  598.         counter = 28L;
  599.         WriteBytes( file, &counter, 4L);
  600.         size = entry->dir.size;
  601.         WriteBytes( file, &size, 4L);
  602.         WriteBytes( file, &(entry->dir.name), 8L);
  603.         BasicWadSeek( entry->wadfile, entry->dir.start);
  604.         CopyBytes( file, entry->wadfile->fileinfo, size);
  605.     }
  606.     else {
  607.         printf( "[Entry not in master directory]\n");
  608.         return;
  609.     }
  610. }
  611.  
  612.  
  613.  
  614. /*
  615.    save a directory entry to disk, without a PWAD header
  616.    */
  617.  
  618. void SaveEntryToRawFile( FILE *file, char *entryname)
  619. {
  620.     MDirPtr entry;
  621.     
  622.     for (entry = MasterDir; entry; entry = entry->next)
  623.         if (!strnicmp( entry->dir.name, entryname, 8))
  624.             break;
  625.     if (entry) {
  626.         BasicWadSeek( entry->wadfile, entry->dir.start);
  627.         CopyBytes( file, entry->wadfile->fileinfo, entry->dir.size);
  628.     }
  629.     else {
  630.         printf( "[Entry not in master directory]\n");
  631.         return;
  632.     }
  633. }
  634.  
  635.  
  636.  
  637. /*
  638.    encapsulate a raw file in a PWAD file
  639.    */
  640.  
  641. void SaveEntryFromRawFile( FILE *file, FILE *raw, char *entryname)
  642. {
  643.     long    counter;
  644.     long    size;
  645.     char    name8[ 8];
  646.     
  647.     for (counter = 0L; counter < 8L; counter++)
  648.         name8[ counter] = '\0';
  649.     strncpy( name8, entryname, 8);
  650.     WriteBytes( file, "PWAD", 4L);     /* PWAD file */
  651.     counter = 1L;
  652.     WriteBytes( file, &counter, 4L);   /* 1 entry */
  653.     counter = 12L;
  654.     WriteBytes( file, &counter, 4L);
  655.     counter = 28L;
  656.     WriteBytes( file, &counter, 4L);
  657.     if (fseek( raw, 0L, SEEK_END) != 0)
  658.         ProgError( "error reading from raw file");
  659.     size = ftell( raw);
  660.     if (size < 0)
  661.         ProgError( "error reading from raw file");
  662.     if (fseek( raw, 0L, SEEK_SET) != 0)
  663.         ProgError( "error reading from raw file");
  664.     WriteBytes( file, &size, 4L);
  665.     WriteBytes( file, name8, 8L);
  666.     CopyBytes( file, raw, size);
  667. }
  668.  
  669.  
  670.  
  671. /* end of file */
  672.