Advertisement
IMarvinTPA

Doom IWad Merge Program

Jan 5th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 30.16 KB | None | 0 0
  1. //Merge CPP by Andy Bay 1/11/1998
  2. //Purpose:
  3. //Take all Doom*.wad files and make one big one.
  4. //Reason:  So texture artwork is centralized and
  5. //         Easy New-Game capability.
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <iostream.h>
  11. #include "DoomType.h"
  12.  
  13. //Globals
  14. wadinfo_t Src,Mrg,New;//the wad headers
  15. filelump_t *SrcDat,*MrgDat,*NewDat;//The wad data heads
  16. FILE *InFile,*OutFile,*TempFile;
  17. //end Globals
  18.  
  19. //Prototypes
  20. int LoadWad(wadinfo_t *,filelump_t **,FILE*);
  21. int FindLumpByName(char *,filelump_t **,int num);
  22. void MergeWads();
  23. char* GenPNames(char*,char*);
  24. char* GenTextures(char*,char*);
  25. char* GenTextures(char*,char*);
  26. //End prototypes
  27.  
  28. int gamemode = 0;//Doom or Doom2
  29. int main(int argc,char **argv){
  30.     //(in file, out file, special)
  31.     char *in,*merge,*special;
  32.     if(argc<3){
  33.         cout<<"You need to do this:\n"<<argv[0]<<" original.wad merge.wad [-tnt] [-plutonia]\n"<<flush;
  34.         return 1;
  35.     }
  36.     in = argv[1];
  37.     merge = argv[2];
  38.     if(argc>3)
  39.         special = argv[3];
  40.     else
  41.         special = "";
  42.     if(!strcmpi(special,"-tnt")) gamemode = 1;
  43.     if(!strcmpi(special,"-plutonia")) gamemode = 2;
  44.     cout<<"in: "<<in<<" merge: "<<merge<<" special: "<<special<<"\n"<<flush;
  45.     InFile=fopen(in,"rb");
  46.     OutFile=fopen(merge,"r+b");
  47.     //TempFile = fopen("~atbtmp.wad","w+b");
  48.     if(!InFile){
  49.         cout<<"1 File open error!\n"<<flush;
  50.         return 2;
  51.     }
  52.     if(!OutFile){
  53.         cout<<"2 File open error!\n"<<flush;
  54.         return 2;
  55.     }
  56.     /*if(!TempFile){
  57.         cout<<"3 File open error!\n"<<flush;
  58.         return 2;
  59.     } never use it.*/
  60.  
  61.     //If a conflict of wad enteries ever exists, the merge.wad will win.
  62.     //They both must be IWAD's
  63.     //Lets setup the database:
  64.     cout<<"Reading "<<in<<"\n"<<flush;
  65.     if(LoadWad(&Src,&SrcDat,InFile)){
  66.         cout<<"Bad source file.\n"<<flush;
  67.         return 3;
  68.     }
  69.     cout<<"Reading "<<merge<<"\n"<<flush;
  70.     LoadWad(&Mrg,&MrgDat,OutFile);
  71.     //cout<<"bug"<<flush<<SrcDat<<flush;
  72.     MergeWads();
  73.     if(SrcDat)
  74.         delete SrcDat;
  75.     if(MrgDat)
  76.         delete MrgDat;
  77.     if(NewDat)
  78.         delete NewDat;
  79.     return 0;
  80. }
  81.  
  82.  
  83. int LoadWad(wadinfo_t * Wad,filelump_t **Data,FILE* File){
  84.     char name[9];
  85.     fseek(File,0,SEEK_SET);
  86.     fread(Wad,sizeof(wadinfo_t),1,File);
  87.     if(strncmp("IWAD",Wad->identification,4)){
  88.         cout<<"Not an IWad\n"<<flush;
  89.         return 1;//not an IWad
  90.     }
  91.     cout<<"There are "<<Wad->numlumps<<" enteries in this wad file.\n"<<flush;
  92.     *Data =  new filelump_t[Wad->numlumps];
  93.     //cout<<*Data<<"\n";
  94.     fseek(File,Wad->infotableofs,SEEK_SET);
  95.     fread(*Data,Wad->numlumps,sizeof(filelump_t),File);
  96.     //cout<<*Data<<"\n";
  97.     /*for(int i=0;i<Wad->numlumps;i++){
  98.         strncpy(name,Data[i].name,8);
  99.         name[8]=0;
  100.         cout<<"Wad entry #"<<i<<" is "<<name<<" and is located at "<<Data[i].filepos<<" and is "<<Data[i].size<<" bytes long.\n";
  101.     }*/ //Testing purposes.
  102.     //cout<<*Data<<"\n";
  103.  
  104.     return 0;
  105. }
  106.  
  107. //This searches the lump list and returns the index where it shows up.
  108. //returns -1 if not found.
  109. int FindLumpByName(char * what,filelump_t * Data,int num){
  110.     char Search[9],name[9];
  111.     int i;
  112.     strncpy(Search,what,8);
  113.     Search[8]=0;
  114.     //cout<<"Looking for: "<<Search<<"\n"<<flush;
  115.     for(i=0;i<num;i++){
  116.         memcpy(name,Data[i].name,8);
  117.         name[8]=0;
  118.         //cout<<"Compared to: "<<name<<"\n"<<flush;
  119.         if(!stricmp(Search,name))
  120.             return i;
  121.     }
  122.     //we didn't find it.
  123.     return -1;
  124. }
  125. char *LoadLumpByName(char * what,filelump_t * Data,wadinfo_t Wad,FILE* File){
  126.     char *Hold=0;
  127.     int i;
  128.     if((i=FindLumpByName(what,Data,Wad.numlumps))>-1){
  129.         Hold=new char[Data[i].size];
  130.         fseek(File,Data[i].filepos,SEEK_SET);
  131.         fread(Hold,Data[i].size,1,File);
  132.     }
  133.     return Hold;
  134. }
  135.  
  136.  
  137. //This processes the wad merger.
  138. void MergeWads(){
  139.     char *Tex1Mrg,*Tex2Mrg,*PNamesMrg,*Hold,Name[9],
  140.         *Tex1Src,*Tex2Src,*PNamesSrc,
  141.         *Tex1New,*Tex2New,*PNamesNew;
  142.     int i,Current=0,workSrc,workMrg,TexFlag=0;
  143.     int WriteWhere=12;//writing pointer.
  144.     int P_Stuff=0,F_Stuff=0,S_Stuff=0,MapStuff=0;
  145.     Name[8]=0;
  146.     //setup the new file header.
  147.     New.identification[0]='I';
  148.     New.identification[1]='W';
  149.     New.identification[2]='A';
  150.     New.identification[3]='D';
  151.     New.numlumps = Src.numlumps+Mrg.numlumps;//Max possible.
  152.     New.infotableofs = -1;//needs to be set later.
  153.     NewDat= new filelump_t[New.numlumps];
  154.     //Clear the new index.
  155.     memset(NewDat,0,New.numlumps*sizeof(filelump_t));
  156.     //memcpy(NewDat,MrgDat,Mrg.numlumps*sizeof(filelump_t));
  157.  
  158.     //Texture1, Texture2, and PNames will be the last enteries
  159.     //physically in the file we write and will be processed last but read into
  160.     //memory first for destination file.
  161.     //The idea is to never re-write data or move it around in the
  162.     //Merged file.
  163.     Tex1Mrg = LoadLumpByName("TEXTURE1",MrgDat,Mrg,OutFile);
  164.     Tex2Mrg = LoadLumpByName("TEXTURE2",MrgDat,Mrg,OutFile);
  165.     PNamesMrg = LoadLumpByName("PNAMES",MrgDat,Mrg,OutFile);
  166.     Tex1Src = LoadLumpByName("TEXTURE1",SrcDat,Src,InFile);
  167.     Tex2Src = LoadLumpByName("TEXTURE2",SrcDat,Src,InFile);
  168.     PNamesSrc = LoadLumpByName("PNAMES",SrcDat,Src,InFile);
  169.  
  170.     //Now, lets spin through the wad for the last used byte.
  171.     //If they somehow are not last, at least we won't destroy other data.
  172.     cout<<"Calculating data writing start point.\n"<<flush;
  173.     for(i = 0;i<Mrg.numlumps;i++){
  174.         if(WriteWhere<MrgDat[i].filepos+MrgDat[i].size){
  175.             if(strnicmp(MrgDat[i].name,"TEXTURE1",8)&&
  176.               strnicmp(MrgDat[i].name,"TEXTURE2",8)&&
  177.               strnicmp(MrgDat[i].name,"PNAMES",8)){
  178.                 WriteWhere = MrgDat[i].filepos+MrgDat[i].size;
  179.             }
  180.  
  181.         }
  182.     }
  183.     cout<<"WriteWhere: "<<WriteWhere<<"\n"<<flush;
  184.     //Now, lets spin through the wads.
  185.     //Merge Wad
  186.     cout<<"Updating Merge Wad's Floating Data.\n"<<flush;
  187.     for(i = 0;i<Mrg.numlumps;i++){
  188.         //These will be done separately.
  189.         if(i == FindLumpByName("S_Start",MrgDat,Mrg.numlumps)){
  190.             i = FindLumpByName("S_End",MrgDat,Mrg.numlumps);
  191.             continue;
  192.         }
  193.         if(i == FindLumpByName("P_Start",MrgDat,Mrg.numlumps)){
  194.             i = FindLumpByName("P_End",MrgDat,Mrg.numlumps);
  195.             continue;
  196.         }
  197.         if(i == FindLumpByName("F_Start",MrgDat,Mrg.numlumps)){
  198.             i = FindLumpByName("F_End",MrgDat,Mrg.numlumps);
  199.             continue;
  200.         }
  201.  
  202.  
  203.         if(i == FindLumpByName("TEXTURE1",MrgDat,Mrg.numlumps))
  204.             continue;
  205.         if(i == FindLumpByName("TEXTURE2",MrgDat,Mrg.numlumps))
  206.             continue;
  207.         if(i == FindLumpByName("PNAMES",MrgDat,Mrg.numlumps))
  208.             continue;
  209.  
  210.         //This lump is from the Merged wad.
  211.         //cout<<"Looking "<<strncpy(Name,MrgDat[i].name,8)<<"        \r"<<flush;
  212.         if(!(S_Stuff||P_Stuff||F_Stuff)){
  213.             //cout<<"Copying "<<strncpy(Name,MrgDat[i].name,8)<<"        \n"<<flush;
  214.             //We aren't in our black-out period.
  215.             NewDat[Current]=MrgDat[i];
  216.             Current++;
  217.             if(MrgDat[i].size == 0){
  218.                 if(MrgDat[i].name[0]=='P' ||  //Plutonia
  219.                   MrgDat[i].name[0]=='T' ||   //TNT
  220.                   MrgDat[i].name[0]=='E' ||   //E#M#
  221.                   MrgDat[i].name[0]=='M'){    //Map
  222.                     for(MapStuff=10;MapStuff>0;MapStuff--){//Ten associated lumps to a map.
  223.                         if(i>Mrg.numlumps)
  224.                             break;
  225.                         i++;
  226.                         NewDat[Current]=MrgDat[i];
  227.                         Current++;
  228.                     }//For MapStuff
  229.  
  230.                   }//If it is a Map lump marker.
  231.             }//If it is 0 bytes
  232.         }//Find Lumps
  233.         if(i == FindLumpByName("S_End",MrgDat,Mrg.numlumps))
  234.             S_Stuff = 0;
  235.         if(i == FindLumpByName("P_End",MrgDat,Mrg.numlumps))
  236.             P_Stuff = 0;
  237.         if(i == FindLumpByName("F_End",MrgDat,Mrg.numlumps))
  238.             F_Stuff = 0;
  239.     }//For i
  240.  
  241.  
  242.     //Reset the blackout:  Shouldn't need to though.
  243.      S_Stuff = 0;
  244.      P_Stuff = 0;
  245.      F_Stuff = 0;
  246.  
  247.     //Source Wad
  248.     cout<<"Adding New Floating Data from input wad.\n"<<flush;
  249.     for(i = 0;i<Src.numlumps;i++){
  250.         //These will be done separately.
  251.         if(i == FindLumpByName("S_Start",SrcDat,Src.numlumps)){
  252.             i = FindLumpByName("S_End",SrcDat,Src.numlumps);
  253.             continue;
  254.         }
  255.         if(i == FindLumpByName("P_Start",SrcDat,Src.numlumps)){
  256.             i = FindLumpByName("P_End",SrcDat,Src.numlumps);
  257.             continue;
  258.         }
  259.         if(i == FindLumpByName("F_Start",SrcDat,Src.numlumps)){
  260.             i = FindLumpByName("F_End",SrcDat,Src.numlumps);
  261.             continue;
  262.         }
  263.  
  264.         if(i == FindLumpByName("TEXTURE1",SrcDat,Src.numlumps))
  265.             continue;
  266.         if(i == FindLumpByName("TEXTURE2",SrcDat,Src.numlumps))
  267.             continue;
  268.         if(i == FindLumpByName("PNAMES",SrcDat,Src.numlumps))
  269.             continue;
  270.         if(SrcDat[i].size == 0){
  271.             if(SrcDat[i].name[0]=='M'){   //Map
  272.                 if(gamemode == 1){//tnt
  273.                     SrcDat[i].name[0]='T';
  274.                     SrcDat[i].name[1]='N';
  275.                     SrcDat[i].name[2]='T';
  276.                     cout<<"Tnt Map: "<<SrcDat[i].name<<"\n"<<flush;
  277.                 }
  278.                 if(gamemode == 2){//plutonia
  279.                     SrcDat[i].name[0]='P';
  280.                     SrcDat[i].name[1]='L';
  281.                     SrcDat[i].name[2]='U';
  282.                     cout<<"Plutonia Map: "<<SrcDat[i].name<<"\n"<<flush;
  283.                 }
  284.             }
  285.         }
  286.         else{
  287.             if(SrcDat[i].name[0]=='D' && SrcDat[i].name[1]=='_'){   //Music
  288.                 if(gamemode == 1){//tnt
  289.                     SrcDat[i].name[1]='T';
  290.                     cout<<"Tnt Music: "<<strncpy(Name,SrcDat[i].name,8)<<"\n"<<flush;
  291.                 }
  292.                 if(gamemode == 2){//plutonia
  293.                     SrcDat[i].name[1]='L';
  294.                     cout<<"Plutonia Music: "<<strncpy(Name,SrcDat[i].name,8)<<"\n"<<flush;
  295.                 }
  296.             }
  297.             if(SrcDat[i].name[0]=='C' && SrcDat[i].name[1]=='W'){   //Map Name
  298.                 if(gamemode == 1){//tnt
  299.                     SrcDat[i].name[0]='T';
  300.                     cout<<"Tnt Map Name: "<<SrcDat[i].name<<"\n"<<flush;
  301.                 }
  302.                 if(gamemode == 2){//plutonia
  303.                     SrcDat[i].name[0]='P';
  304.                     cout<<"Plutonia Map Name: "<<SrcDat[i].name<<"\n"<<flush;
  305.                 }
  306.             }
  307.         }
  308.  
  309.  
  310.         if(FindLumpByName(SrcDat[i].name,MrgDat,Mrg.numlumps)==-1 && FindLumpByName(SrcDat[i].name,NewDat,Current)==-1){
  311.             //This lump is NOT in the Merged wad.
  312.             //cout<<"Looking "<<strncpy(Name,SrcDat[i].name,8)<<"        \r"<<flush;
  313.             if(!(S_Stuff||P_Stuff||F_Stuff)){
  314.                 //cout<<"Copying "<<strncpy(Name,SrcDat[i].name,8)<<"        \n"<<flush;
  315.                 //We aren't in our black-out period.
  316.                 NewDat[Current]=SrcDat[i];
  317.                 NewDat[Current].filepos = WriteWhere;
  318.                 fseek(InFile,SrcDat[i].filepos,SEEK_SET);
  319.                 fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  320.                 Hold = new char[SrcDat[i].size];
  321.                 fread(Hold,SrcDat[i].size,1,InFile);
  322.                 fwrite(Hold,SrcDat[i].size,1,OutFile);
  323.                 delete Hold;
  324.                 WriteWhere+=SrcDat[i].size;
  325.                 Current++;
  326.                 if(SrcDat[i].size == 0){
  327.                     if(SrcDat[i].name[0]=='P' ||    //Plutonia
  328.                       SrcDat[i].name[0]=='T' ||   //TNT
  329.                       SrcDat[i].name[0]=='E' ||   //E#M#
  330.                       SrcDat[i].name[0]=='M'){   //Map
  331.                         for(MapStuff=10;MapStuff>0;MapStuff--){
  332.                             if(i>Src.numlumps)
  333.                                 break;
  334.                             i++;
  335.                             NewDat[Current]=SrcDat[i];
  336.                             NewDat[Current].filepos = WriteWhere;
  337.                             fseek(InFile,SrcDat[i].filepos,SEEK_SET);
  338.                             fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  339.                             Hold = new char[SrcDat[i].size];
  340.                             fread(Hold,SrcDat[i].size,1,InFile);
  341.                             fwrite(Hold,SrcDat[i].size,1,OutFile);
  342.                             delete Hold;
  343.                             WriteWhere+=SrcDat[i].size;
  344.                             Current++;
  345.                         }//For MapStuff
  346.  
  347.                       }//If it is a Map lump marker.
  348.                 }//If it is 0 bytes
  349.             }//Find Lumps
  350.         }//If FindLump by Name
  351.         if(i == FindLumpByName("S_End",SrcDat,Src.numlumps))
  352.             S_Stuff = 0;
  353.         if(i == FindLumpByName("P_End",SrcDat,Src.numlumps))
  354.             P_Stuff = 0;
  355.         if(i == FindLumpByName("F_End",SrcDat,Src.numlumps))
  356.             F_Stuff = 0;
  357.     }//For i
  358.  
  359.     //Ok, that crap is done, now we do the S_Stuff
  360.  
  361.     cout<<"Updating Sprite data.\n"<<flush;
  362.  
  363.     workSrc = FindLumpByName("S_Start",SrcDat,Src.numlumps);
  364.     workMrg = FindLumpByName("S_Start",MrgDat,Mrg.numlumps);
  365.     if(workSrc>-1 && workMrg>-1){ //we have both
  366.             //cout<<"Both!\n"<<flush;
  367.             NewDat[Current]=MrgDat[workMrg];
  368.             Current++;
  369.             i=workMrg+1;
  370.             workMrg = FindLumpByName("S_End",MrgDat,Mrg.numlumps);
  371.             for(;i<workMrg;i++){
  372.                 NewDat[Current]=MrgDat[i];
  373.                 Current++;
  374.             }
  375.             i=workSrc+1;
  376.             workSrc = FindLumpByName("S_End",SrcDat,Src.numlumps);
  377.             for(;i<workSrc;i++){
  378.                 if(FindLumpByName(SrcDat[i].name,MrgDat,Mrg.numlumps)==-1 && FindLumpByName(SrcDat[i].name,NewDat,Current)==-1){
  379.                     NewDat[Current]=SrcDat[i];
  380.                     NewDat[Current].filepos = WriteWhere;
  381.                     fseek(InFile,SrcDat[i].filepos,SEEK_SET);
  382.                     fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  383.                     Hold = new char[SrcDat[i].size];
  384.                     fread(Hold,SrcDat[i].size,1,InFile);
  385.                     fwrite(Hold,SrcDat[i].size,1,OutFile);
  386.                     delete Hold;
  387.                     WriteWhere+=SrcDat[i].size;
  388.                     Current++;
  389.                 }
  390.             }
  391.             NewDat[Current]=MrgDat[workMrg];
  392.             Current++;
  393.     }
  394.     else if(workSrc>-1){
  395.             //cout<<"Source!\n"<<flush;
  396.             NewDat[Current]=SrcDat[workSrc];
  397.             NewDat[Current].filepos = WriteWhere;
  398.             Current++;
  399.             i=workSrc+1;
  400.             workSrc = FindLumpByName("S_End",SrcDat,Src.numlumps);
  401.             for(;i<workSrc;i++){
  402.                 NewDat[Current]=SrcDat[i];
  403.                 NewDat[Current].filepos = WriteWhere;
  404.                 fseek(InFile,SrcDat[i].filepos,SEEK_SET);
  405.                 fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  406.                 Hold = new char[SrcDat[i].size];
  407.                 fread(Hold,SrcDat[i].size,1,InFile);
  408.                 fwrite(Hold,SrcDat[i].size,1,OutFile);
  409.                 delete Hold;
  410.                 WriteWhere+=SrcDat[i].size;
  411.                 Current++;
  412.             }
  413.             NewDat[Current]=SrcDat[workSrc];
  414.             NewDat[Current].filepos = WriteWhere;
  415.             Current++;
  416.     }
  417.     else if(workMrg>-1){
  418.             //cout<<"Merge!\n"<<flush;
  419.             NewDat[Current]=MrgDat[workMrg];
  420.             Current++;
  421.             i=workMrg+1;
  422.             workMrg = FindLumpByName("S_End",MrgDat,Mrg.numlumps);
  423.             for(;i<workMrg;i++){
  424.                 NewDat[Current]=MrgDat[i];
  425.                 Current++;
  426.             }
  427.             NewDat[Current]=MrgDat[workMrg];
  428.             Current++;
  429.     }
  430.     //Don't worry about it if we don't have any of them.
  431.  
  432.     cout<<"Updating Wall patch data.\n"<<flush;
  433.  
  434.     //Ok, that crap is done, now we do the P_Stuff
  435.     workSrc = FindLumpByName("P_Start",SrcDat,Src.numlumps);
  436.     workMrg = FindLumpByName("P_Start",MrgDat,Mrg.numlumps);
  437.     if(workSrc>-1 && workMrg>-1){ //we have both
  438.             TexFlag = 1;
  439.             //cout<<"Both!\n"<<flush;
  440.             NewDat[Current]=MrgDat[workMrg];
  441.             Current++;
  442.             i=workMrg+1;
  443.             workMrg = FindLumpByName("P_End",MrgDat,Mrg.numlumps);
  444.             for(;i<workMrg;i++){
  445.                 NewDat[Current]=MrgDat[i];
  446.                 Current++;
  447.             }
  448.             i=workSrc+1;
  449.             workSrc = FindLumpByName("P_End",SrcDat,Src.numlumps);
  450.             for(;i<workSrc;i++){
  451.                 if(FindLumpByName(SrcDat[i].name,MrgDat,Mrg.numlumps)==-1 && FindLumpByName(SrcDat[i].name,NewDat,Current)==-1){
  452.                     NewDat[Current]=SrcDat[i];
  453.                     NewDat[Current].filepos = WriteWhere;
  454.                     fseek(InFile,SrcDat[i].filepos,SEEK_SET);
  455.                     fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  456.                     Hold = new char[SrcDat[i].size];
  457.                     fread(Hold,SrcDat[i].size,1,InFile);
  458.                     fwrite(Hold,SrcDat[i].size,1,OutFile);
  459.                     delete Hold;
  460.                     WriteWhere+=SrcDat[i].size;
  461.                     Current++;
  462.                 }
  463.             }
  464.             NewDat[Current]=MrgDat[workMrg];
  465.             Current++;
  466.     }
  467.     else if(workSrc>-1){
  468.             TexFlag = 2;
  469.             //cout<<"Source!\n"<<flush;
  470.             NewDat[Current]=SrcDat[workSrc];
  471.             NewDat[Current].filepos = WriteWhere;
  472.             Current++;
  473.             i=workSrc+1;
  474.             workSrc = FindLumpByName("P_End",SrcDat,Src.numlumps);
  475.             for(;i<workSrc;i++){
  476.                 NewDat[Current]=SrcDat[i];
  477.                 NewDat[Current].filepos = WriteWhere;
  478.                 fseek(InFile,SrcDat[i].filepos,SEEK_SET);
  479.                 fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  480.                 Hold = new char[SrcDat[i].size];
  481.                 fread(Hold,SrcDat[i].size,1,InFile);
  482.                 fwrite(Hold,SrcDat[i].size,1,OutFile);
  483.                 delete Hold;
  484.                 WriteWhere+=SrcDat[i].size;
  485.                 Current++;
  486.             }
  487.             NewDat[Current]=SrcDat[workSrc];
  488.             NewDat[Current].filepos = WriteWhere;
  489.             Current++;
  490.     }
  491.     else if(workMrg>-1){
  492.             TexFlag = 3;
  493.             //cout<<"Merge!\n"<<flush;
  494.             NewDat[Current]=MrgDat[workMrg];
  495.             Current++;
  496.             i=workMrg+1;
  497.             workMrg = FindLumpByName("P_End",MrgDat,Mrg.numlumps);
  498.             for(;i<workMrg;i++){
  499.                 NewDat[Current]=MrgDat[i];
  500.                 Current++;
  501.             }
  502.             NewDat[Current]=MrgDat[workMrg];
  503.             Current++;
  504.     }
  505.     //Don't worry about it if we don't have any of them.
  506.  
  507.     cout<<"Updating Floor data.\n"<<flush;
  508.  
  509.     //Ok, that crap is done, now we do the F_Stuff
  510.     workSrc = FindLumpByName("F_Start",SrcDat,Src.numlumps);
  511.     workMrg = FindLumpByName("F_Start",MrgDat,Mrg.numlumps);
  512.     if(workSrc>-1 && workMrg>-1){ //we have both
  513.             //cout<<"Both!\n"<<flush;
  514.             NewDat[Current]=MrgDat[workMrg];
  515.             Current++;
  516.             i=workMrg+1;
  517.             workMrg = FindLumpByName("F_End",MrgDat,Mrg.numlumps);
  518.             for(;i<workMrg;i++){
  519.                 NewDat[Current]=MrgDat[i];
  520.                 Current++;
  521.             }
  522.             i=workSrc+1;
  523.             workSrc = FindLumpByName("F_End",SrcDat,Src.numlumps);
  524.             for(;i<workSrc;i++){
  525.                 if(FindLumpByName(SrcDat[i].name,MrgDat,Mrg.numlumps)==-1 && FindLumpByName(SrcDat[i].name,NewDat,Current)==-1){
  526.                     NewDat[Current]=SrcDat[i];
  527.                     NewDat[Current].filepos = WriteWhere;
  528.                     fseek(InFile,SrcDat[i].filepos,SEEK_SET);
  529.                     fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  530.                     Hold = new char[SrcDat[i].size];
  531.                     fread(Hold,SrcDat[i].size,1,InFile);
  532.                     fwrite(Hold,SrcDat[i].size,1,OutFile);
  533.                     delete Hold;
  534.                     WriteWhere+=SrcDat[i].size;
  535.                     Current++;
  536.                 }
  537.             }
  538.             NewDat[Current]=MrgDat[workMrg];
  539.             Current++;
  540.     }
  541.     else if(workSrc>-1){
  542.             //cout<<"Source!\n"<<flush;
  543.             NewDat[Current]=SrcDat[workSrc];
  544.             NewDat[Current].filepos = WriteWhere;
  545.             Current++;
  546.             i=workSrc+1;
  547.             workSrc = FindLumpByName("F_End",SrcDat,Src.numlumps);
  548.             for(;i<workSrc;i++){
  549.                 NewDat[Current]=SrcDat[i];
  550.                 NewDat[Current].filepos = WriteWhere;
  551.                 fseek(InFile,SrcDat[i].filepos,SEEK_SET);
  552.                 fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  553.                 Hold = new char[SrcDat[i].size];
  554.                 fread(Hold,SrcDat[i].size,1,InFile);
  555.                 fwrite(Hold,SrcDat[i].size,1,OutFile);
  556.                 delete Hold;
  557.                 WriteWhere+=SrcDat[i].size;
  558.                 Current++;
  559.             }
  560.             NewDat[Current]=SrcDat[workSrc];
  561.             NewDat[Current].filepos = WriteWhere;
  562.             Current++;
  563.     }
  564.     else if(workMrg>-1){
  565.             //cout<<"Merge!\n"<<flush;
  566.             NewDat[Current]=MrgDat[workMrg];
  567.             Current++;
  568.             i=workMrg+1;
  569.             workMrg = FindLumpByName("F_End",MrgDat,Mrg.numlumps);
  570.             for(;i<workMrg;i++){
  571.                 NewDat[Current]=MrgDat[i];
  572.                 Current++;
  573.             }
  574.             NewDat[Current]=MrgDat[workMrg];
  575.             Current++;
  576.     }
  577.     //Don't worry about it if we don't have any of them.
  578.     //At this point, we need a new texture resources.
  579.     PNamesNew=GenPNames(PNamesSrc,PNamesMrg);
  580.     Tex1New = 0;
  581.     Tex2New = 0;
  582.     Tex1New=GenTextures(Tex1Src,Tex1Mrg);
  583.     Hold = 0;
  584.     if(Tex2Mrg)
  585.         Tex2New=GenTextures(Hold,Tex2Mrg);//If I have one, continue having it.
  586.     if(Tex2Src)
  587.         Tex1New=GenTextures(Tex2Src,Tex1New+4);//Import it to Texture1 for conversion purposes.
  588.     if(PNamesNew){
  589.         memcpy(&i,PNamesNew,4);
  590.         PNamesNew+=4;
  591.         NewDat[Current].size=i;
  592.         NewDat[Current].filepos = WriteWhere;
  593.         strncpy(NewDat[Current].name,"PNAMES",8);
  594.         fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  595.         fwrite(PNamesNew,NewDat[Current].size,1,OutFile);
  596.         PNamesNew-=4;
  597.         WriteWhere+=NewDat[Current].size;
  598.         Current++;
  599.     }
  600.     if(Tex1New){
  601.         memcpy(&i,Tex1New,4);
  602.         Tex1New+=4;
  603.         NewDat[Current].size=i;
  604.         NewDat[Current].filepos = WriteWhere;
  605.         strncpy(NewDat[Current].name,"TEXTURE1",8);
  606.         fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  607.         fwrite(Tex1New,NewDat[Current].size,1,OutFile);
  608.         Tex1New-=4;
  609.         WriteWhere+=NewDat[Current].size;
  610.         Current++;
  611.     }
  612.     if(Tex2New){
  613.         memcpy(&i,Tex2New,4);
  614.         Tex2New+=4;
  615.         NewDat[Current].size=i;
  616.         NewDat[Current].filepos = WriteWhere;
  617.         strncpy(NewDat[Current].name,"TEXTURE2",8);
  618.         fseek(OutFile,NewDat[Current].filepos,SEEK_SET);
  619.         fwrite(Tex2New,NewDat[Current].size,1,OutFile);
  620.         Tex2New-=4;
  621.         WriteWhere+=NewDat[Current].size;
  622.         Current++;
  623.     }
  624.  
  625.     New.numlumps = Current;
  626.     New.infotableofs = WriteWhere;//needs to be set later.
  627.     fseek(OutFile,WriteWhere,SEEK_SET);
  628.     fwrite(NewDat,Current,sizeof(filelump_t),OutFile);
  629.     fseek(OutFile,0,SEEK_SET);
  630.     fwrite(&New,sizeof(wadinfo_t),1,OutFile);
  631. }//End of function
  632.  
  633. int PNamesFind(char*what,char*data,int num){
  634.     int i=num-1;
  635.     char name[9];
  636.     name[8]=0;
  637.     //cout<<"What: "<<strncpy(name,what,8);
  638.     //cout<<"\nData "<<i<<": "<<strncpy(name,data+8*i,8)<<"\n"<<flush;
  639.     for(i = 0;i<num;i++){
  640.  
  641.         if(!strnicmp(what,data,8))
  642.             return i;
  643.         data+=8;
  644.     }
  645.     return -1;
  646. }
  647. char * PNamesToUse,*PNamesSrc,*PNamesMrg;
  648.  
  649. char* GenPNames(char*src,char*mrg){
  650.     char *Hold=0,*start,names[9];
  651.     int numSrc,numMrg,num;
  652.     int i,j,k;
  653.     names[8]=0;
  654.     PNamesSrc=src;
  655.     PNamesMrg=mrg;
  656.     cout<<"Combining PNames.\n"<<flush;
  657.     if(src && mrg){
  658.         memcpy(&numSrc,src,4);
  659.         memcpy(&numMrg,mrg,4);
  660.         num = numSrc+numMrg;
  661.         i = num*8;
  662.         start=Hold=new char[i+8];//4 for lenght and 4 for number of them.
  663.         src+=4;
  664.         mrg+=4;
  665.         Hold+=8;
  666.         num = numMrg;
  667.         memcpy(Hold,mrg,numMrg*8);
  668.         Hold+=num*8;
  669.         for(j=0;j<numSrc;j++){
  670.             if(PNamesFind(src,mrg,numMrg)==-1){
  671.                 memcpy(Hold,src,8);
  672.                 Hold+=8;
  673.                 memcpy(names,src,8);
  674.                 //cout<<"Adding: "<<names<<" to PNames\n"<<flush;
  675.                 num++;
  676.             }
  677.             src+=8;
  678.         }
  679.         i=num*8+4;
  680.         Hold = start;
  681.         PNamesToUse=Hold+4;
  682.         memcpy(Hold,&i,4);
  683.         Hold+=4;
  684.         //cout<<"Find me: "<<num<<flush;
  685.         memcpy(Hold,&num,4);
  686.         //for(i = 0;i<num;i++)
  687.         //    cout<<"\nData "<<i<<": "<<strncpy(names,Hold+4+8*i,8)<<"\n"<<flush;
  688.  
  689.         return start;
  690.     }
  691.     else if(src){
  692.         memcpy(&numSrc,src,4);
  693.         i=numSrc*8+4;
  694.         Hold = new char[i+4];
  695.         memcpy(Hold,&i,4);
  696.         memcpy(Hold+4,src,i);
  697.         PNamesToUse=Hold+4;
  698.         //cout<<"PNames is: "<<i<<" bytes long\n"<<flush;
  699.         return Hold;
  700.     }
  701.     else if(mrg){
  702.         memcpy(&numMrg,mrg,4);
  703.         i=numMrg*8+4;
  704.         Hold = new char[i+4];
  705.         memcpy(Hold,&i,4);
  706.         memcpy(Hold+4,mrg,i);
  707.         PNamesToUse=Hold+4;
  708.         //cout<<"PNames is: "<<i<<" bytes long\n"<<flush;
  709.         return Hold;
  710.     }
  711.     return Hold;
  712.  
  713. }
  714.  
  715. int TNamesFind(char*what, myTex_t *data,int num){
  716.     int i;
  717.     for(i = 0;i<num;i++){
  718.         if(!strnicmp(what,data[i].name,8))
  719.             return i;
  720.     }
  721.     return -1;
  722. }
  723.  
  724. int GetTexture(myTex_t *Tex,char* data){
  725.     memcpy(Tex,data,22);
  726.     Tex->patches = (patches_t*)(data+22);
  727.     /*
  728.     //When in doubt, print it all!
  729.     cout<<"Name: "<<Tex->name<<"\n"<<flush;
  730.     cout<<"zero1: "<<Tex->zero1<<"\n"<<flush;
  731.     cout<<"zero2: "<<Tex->zero2<<"\n"<<flush;
  732.     cout<<"width: "<<Tex->width<<"\n"<<flush;
  733.     cout<<"height: "<<Tex->height<<"\n"<<flush;
  734.     cout<<"zero3: "<<Tex->zero3<<"\n"<<flush;
  735.     cout<<"zero4: "<<Tex->zero4<<"\n"<<flush;
  736.     cout<<"Num patches: "<<Tex->numPatches<<"\n"<<flush;
  737.     */
  738.     return Tex->numPatches*sizeof(patches_t)+22;
  739. }
  740.  
  741. char* GenTextures(char*src,char*mrg){
  742.     char*newbuf=0,*start=0,name[9];
  743.     int inew,*offsnew,*isrc,*offssrc,*imrg,*offsmrg,j,k,len,pat,pos;
  744.     int PNTU=0,PNS=0,PNM=0;
  745.     myTex_t *TextNew,*TextSrc,*TextMrg;
  746.     cout<<"Combining Textures\n"<<flush;
  747.     if(PNamesToUse)
  748.         memcpy(&PNTU,PNamesToUse,4);
  749.     if(PNamesSrc)
  750.         memcpy(&PNS,PNamesSrc,4);
  751.     if(PNamesMrg)
  752.         memcpy(&PNM,PNamesMrg,4);
  753.     //cout<<PNTU<<" "<<PNS<<" "<<PNM<<"\n";
  754.     name[8]=0;
  755.     if(src && mrg){
  756.         isrc=(int*)src;
  757.         imrg=(int*)mrg;
  758.         inew=*isrc+*imrg;
  759.         offsnew=new int[inew];
  760.         offssrc=(int*)(src+4);
  761.         offsmrg=(int*)(mrg+4);
  762.         TextNew=new myTex_t[inew];
  763.         TextSrc=new myTex_t[*isrc];
  764.         TextMrg=new myTex_t[*imrg];
  765.         for(j=0;j<*isrc;j++){
  766.             GetTexture(&TextSrc[j],src+offssrc[j]);
  767.             //cout<<"Src: "<<TextSrc[j].name<<"\n"<<flush;
  768.         }
  769.         len = 0;
  770.         for(j=0;j<*imrg;j++){
  771.             len+=GetTexture(&TextMrg[j],mrg+offsmrg[j]);
  772.             //cout<<"Mrg: "<<TextMrg[j].name<<"\n"<<flush;
  773.             TextNew[j]=TextMrg[j];
  774.         }
  775.  
  776.         for(k=0;k<*isrc;k++){
  777.             if(TNamesFind(TextSrc[k].name,TextMrg,*imrg)==-1){
  778.                 TextNew[j]=TextSrc[k];
  779.                 //cout<<"Added: "<<TextNew[j].name<<"\n"<<flush;
  780.                 for(pat=0;pat<TextNew[j].numPatches;pat++){
  781.                     //cout<<"OldPName: "<<TextNew[j].patches[pat].PName<< " "<<strncpy(name,(PNamesSrc+4+TextNew[j].patches[pat].PName*8),8)<<"\n";
  782.                     TextNew[j].patches[pat].PName=PNamesFind((PNamesSrc+4+TextNew[j].patches[pat].PName*8),PNamesToUse+4,PNTU);
  783.                     //cout<<"\nNewPName: "<<TextNew[j].patches[pat].PName<< " "<<strncpy(name,(PNamesToUse+4+TextNew[j].patches[pat].PName*8),8)<<"\n"<<flush;
  784.                 }
  785.                 len+=TextNew[j].numPatches*10+22;
  786.                 j++;
  787.  
  788.             }
  789.         }
  790.         len += 4+j*4;
  791.         start = newbuf = new char[len+4];//4 for the length we send back.
  792.         k=len+4;
  793.         memcpy(newbuf,&k,4);
  794.         newbuf+=4;
  795.         memcpy(newbuf,&j,4);
  796.         pos=j*4+4;
  797.         newbuf+=4;
  798.         for(k=0;k<j;k++){
  799.             memcpy(newbuf,&pos,4);
  800.             newbuf+=4;
  801.             memcpy(start+pos+4,&TextNew[k],22);
  802.             memcpy(start+pos+26,TextNew[k].patches,TextNew[k].numPatches*10);
  803.             pos+=(TextNew[k].numPatches*10+22);
  804.         }
  805.         newbuf=start;
  806.  
  807.     }
  808.     else if(src){
  809.         isrc=(int*)src;
  810.         len = 0;
  811.         offssrc=(int*)(src+4);
  812.         TextSrc=new myTex_t;
  813.         for(j = 0;j<*isrc;j++){
  814.             if(len < GetTexture(TextSrc,src+offssrc[j])+offssrc[j])
  815.                 len = GetTexture(TextSrc,src+offssrc[j])+offssrc[j];
  816.                 //cout<<offssrc[j]<<" "<<GetTexture(TextSrc,src+offssrc[j])<<" "<<len<<"\n"<<flush;
  817.         }
  818.         newbuf = new char[len+4];
  819.         memcpy(newbuf,&len,4);
  820.         memcpy(newbuf+4,src,len);
  821.         //cout<<*isrc<<" "<<TextSrc[0].name<<" "<<len<<"\n"<<flush;
  822.     }
  823.     else if(mrg){
  824.         len = 0;
  825.         imrg=(int*)mrg;
  826.         offsmrg=(int*)(mrg+4);
  827.         TextMrg=new myTex_t;
  828.         for(j = 0;j<*imrg;j++){
  829.             if(len < GetTexture(TextMrg,mrg+offsmrg[j])+offsmrg[j])
  830.                 len = GetTexture(TextMrg,mrg+offsmrg[j])+offsmrg[j];
  831.             //cout<<len<<"\n"<<flush;
  832.         }
  833.         newbuf = new char[len+4];
  834.         memcpy(newbuf,&len,4);
  835.         memcpy(newbuf+4,mrg,len);
  836.         //cout<<*imrg<<" "<<TextMrg[0].name<<" "<<len<<"\n"<<flush;
  837.     }
  838.     return newbuf;
  839. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement