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