stoneharry

Untitled

Jan 28th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.24 KB | None | 0 0
  1. /* Author : Mjollnà, 2012-08-12 */
  2.  
  3. /* Help/documentation :
  4.  
  5. http://www.pxr.dk/wowdev/wiki/
  6. http://modcraft.superparanoid.de/viewtopic.php?f=59&t=828 (010 templates)
  7. http://modcraft.superparanoid.de/viewtopic.php?f=20&t=937 (Tutorial for M2 Cata -> LK)
  8.  
  9. Thanks to Schlumpf, Zim4ik and Morfium :)
  10.  
  11.  */
  12.  
  13. #include <iostream>
  14. #include <string>
  15. #include <cstring>
  16. #include <fstream>
  17. #include <vector>
  18.  
  19. struct Vec3F
  20. {
  21.   float x;
  22.   float y;
  23.   float z;
  24. };
  25.  
  26. struct cataHeader
  27. {
  28.   char magic[4];
  29.   int version;
  30.   int lName;
  31.   int ofsName;
  32.   int GlobalModelFlags;
  33.   int nGlobalSequences;
  34.   int ofsGlobalSequences;
  35.   int nAnimations;
  36.   int ofsAnimations;
  37.   int nAnimationLookup;
  38.   int ofsAnimationLookup;
  39.   int nBones;
  40.   int ofsBones;
  41.   int nKeyBoneLookup;
  42.   int ofsKeyBoneLookup;
  43.   int nVertices;
  44.   int ofsVertices;
  45.   int nViews;
  46.   int nColors;
  47.   int ofsColors;
  48.   int nTextures;
  49.   int ofsTextures;
  50.   int nTransparency;
  51.   int ofsTransparency;
  52.   int nUVAnimation;
  53.   int ofsUVAnimation;
  54.   int nTexReplace;
  55.   int ofsTexReplace;
  56.   int nRenderFlags;
  57.   int ofsRenderFlags;
  58.   int nBoneLookupTable;
  59.   int ofsBoneLookupTable;
  60.   int nTexLookup;
  61.   int ofsTexLookup;
  62.   int nTexUnits;
  63.   int ofsTexUnits;
  64.   int nTransLookup;
  65.   int ofsTransLookup;
  66.   int nUVAnimLookup;
  67.   int ofsUVAnimLookup; 
  68.   Vec3F VertexBox[2];
  69.   float VertexRadius;  
  70.   Vec3F BoundingBox[2];
  71.   float BoundingRadius;
  72.   int nBoundingTriangles;
  73.   int ofsBoundingTriangles;
  74.   int nBoundingVertices;
  75.   int ofsBoundingVertices;
  76.   int nBoundingNormals;
  77.   int ofsBoundingNormals;
  78.   int nAttachments;
  79.   int ofsAttachments;
  80.   int nAttachLookup;
  81.   int ofsAttachLookup;
  82.   int nEvents;
  83.   int ofsEvents;
  84.   int nLights;
  85.   int ofsLights;
  86.   int nCameras;
  87.   int ofsCameras;
  88.   int nCameraLookup;
  89.   int ofsCameraLookup;
  90.   int nRibbonEmitters;
  91.   int ofsRibbonEmitters;
  92.   int nParticleEmitters;
  93.   int ofsParticleEmitters;
  94. };
  95.  
  96. struct lkCamera // Total size = 0x64
  97. {
  98.   int Type;
  99.   float FOV;  
  100.   char otherStuff[0x5C]; // 0x64 - (Type + FOV)
  101. };
  102.  
  103. struct cataCamera // Total size = 0x74
  104. {
  105.   int Type;
  106.   char otherStuff[0x5C];
  107.   char AnimBlock4[0x14]; // Stuff we're gonna get rid of
  108. };
  109.  
  110. struct skinHeader
  111. {
  112.   int magic;
  113.   int nIndices;    
  114.   int ofsIndices;
  115.   int nTriangles;      
  116.   int ofsTriangles;
  117.   int nProperties;     
  118.   int ofsProperties;
  119.   int nSubmeshes;      
  120.   int ofsSubmeshes;
  121.   int nTextureUnits;   
  122.   int ofsTextureUnits;
  123.   int LOD;
  124. };
  125.  
  126. struct submesh
  127. {
  128.   int ID;      
  129.   short StartVertex;       
  130.   short nVertices; 
  131.   short StartTriangle; 
  132.   short nTriangles;
  133.   short nBones;
  134.   short StartBones;
  135.   short Unknown;       
  136.   short RootBone;  
  137.   float Position[3];
  138.   float Floats[4];
  139. };
  140.  
  141. struct textureUnit
  142. {
  143.   char flags;
  144.   char flags2;
  145.   unsigned short order;
  146.   short submesh;
  147.   short submesh2;
  148.   short colorIndex;
  149.   short renderFlags;
  150.   short texunit;
  151.   short d4;
  152.   short textureid;
  153.   short texunit2;
  154.   short transid;
  155.   short texanimid;
  156. };
  157.  
  158. void fixM2File(char * filename)
  159. {
  160.   std::ifstream cataM2File;
  161.   cataM2File.open( filename, std::ios::binary );
  162.  
  163.   cataHeader header;
  164.   cataM2File.read( reinterpret_cast<char*>(&header), sizeof(header) );
  165.  
  166.   std::vector<cataCamera> cataCams (0);  
  167.   cataM2File.seekg( header.ofsCameras );
  168.  
  169.   for ( int numCams = 0 ; numCams < header.nCameras ; ++numCams )
  170.   {
  171.     cataCamera singleCam;
  172.     cataM2File.read( reinterpret_cast<char*>(&singleCam), sizeof(cataCamera) );
  173.     cataCams.push_back( singleCam );
  174.   }  
  175.  
  176.   const int headerSize (0x130);
  177.  
  178.   cataM2File.seekg( 0, std::ios::end );
  179.   const long fullFileSize ( cataM2File.tellg() );
  180.   const long restOfFileSize (fullFileSize - headerSize);
  181.   cataM2File.seekg( headerSize, std::ios::beg );
  182.  
  183.   char * restOfFile;
  184.   restOfFile = new char[restOfFileSize];
  185.  
  186.   cataM2File.read( restOfFile, restOfFileSize );
  187.  
  188.   cataM2File.close();
  189.  
  190.   /* Start of tutorial, Zim4ik part */
  191.   header.version = 264;
  192.   header.nTexUnits = 1;
  193.   header.ofsTexUnits = header.ofsTransLookup;
  194.  
  195.   /* Cameras fix, Schlumpf's help */
  196.   std::vector<lkCamera> lkCams (0);
  197.  
  198.   if ( header.nCameras > 0 )
  199.   {
  200.     header.ofsCameras = fullFileSize;
  201.    
  202.     for ( int numCams = 0 ; numCams < header.nCameras ; ++numCams )
  203.     {
  204.       lkCamera singleLkCam;
  205.      
  206.       singleLkCam.Type = cataCams[numCams].Type;
  207.      
  208.       for (int i = 0 ; i < 0x5C ; ++i)
  209.         singleLkCam.otherStuff[i] = cataCams[numCams].otherStuff[i];
  210.      
  211.       if ( cataCams[numCams].Type == 0 )
  212.         singleLkCam.FOV = 0.7;
  213.       else
  214.         singleLkCam.FOV = 0.97;
  215.  
  216.       lkCams.push_back( singleLkCam );
  217.     }
  218.   }
  219.  
  220.   std::ofstream lkOutput;
  221.   std::string outputFileName ( filename );
  222.   outputFileName.append( "_lk" );
  223.   lkOutput.open( outputFileName.c_str(), std::ios::binary );
  224.  
  225.   lkOutput.write( reinterpret_cast<char*>(&header), sizeof(header) );
  226.   lkOutput.write( restOfFile, restOfFileSize );
  227.   lkOutput.write( reinterpret_cast<char*>(&lkCams[0]), sizeof(lkCamera) * lkCams.size() );
  228.  
  229.   lkOutput.close();
  230.  
  231.   delete[] restOfFile;
  232. }
  233.  
  234. void fixSkinFile(const std::string & filename)
  235. {
  236.   std::ifstream skinFile;
  237.   skinFile.open( filename.c_str(), std::ios::binary );
  238.  
  239.   skinFile.seekg( 0, std::ios::end );
  240.   const long fullFileSize ( skinFile.tellg() );
  241.  
  242.   if ( fullFileSize > 0 )
  243.   {
  244.     skinFile.seekg( 0, std::ios::beg );
  245.     skinHeader skinHdr;
  246.     skinFile.read( reinterpret_cast<char*>(&skinHdr), sizeof(skinHdr) );
  247.  
  248.     skinFile.seekg( 0, std::ios::beg );
  249.    
  250.     char * startOfFile;
  251.     const long startOfFileSize ( skinHdr.ofsSubmeshes );
  252.    
  253.     startOfFile = new char[startOfFileSize];
  254.     skinFile.read( startOfFile, startOfFileSize );
  255.    
  256.     std::vector<submesh> subs (0);  
  257.  
  258.     for ( int numSubs = 0 ; numSubs < skinHdr.nSubmeshes ; ++numSubs )
  259.     {
  260.       submesh singleSubmesh;
  261.       skinFile.read( reinterpret_cast<char*>(&singleSubmesh), sizeof(submesh) );
  262.       subs.push_back( singleSubmesh );
  263.     }  
  264.    
  265.     std::vector<textureUnit> texUnits (0);  
  266.  
  267.     for ( int numTexUnits = 0 ; numTexUnits < skinHdr.nTextureUnits ; ++numTexUnits )
  268.     {
  269.       textureUnit singleTexUnit;
  270.       skinFile.read( reinterpret_cast<char*>(&singleTexUnit), sizeof(textureUnit) );
  271.       texUnits.push_back( singleTexUnit );
  272.     }  
  273.    
  274.     const long endOfFileSize ( fullFileSize - skinFile.tellg() );  
  275.     char * endOfFile;
  276.    
  277.     endOfFile = new char[endOfFileSize];
  278.     skinFile.read( endOfFile, endOfFileSize );  
  279.    
  280.     skinFile.close();  
  281.  
  282.     /* My addition for dynamic shadows */
  283.    
  284.     for ( int i = 0 ; i < subs.size() ; ++i )
  285.     {
  286.       if ( subs[i].Unknown == 0 )
  287.         subs[i].Unknown = 1;
  288.     }
  289.    
  290.     /* Morfium's post on page 2 : http://modcraft.superparanoid.de/posting.php?mode=quote&f=20&p=14081 */  
  291.  
  292.     for ( int i = 0 ; i < texUnits.size() ; ++i )
  293.     {
  294.       texUnits[i].d4 = 1;
  295.      
  296.       if ( (texUnits[i].flags != 0) || (texUnits[i].flags != 16) )
  297.         texUnits[i].flags = 16;
  298.        
  299.       if ( texUnits[i].order > 50 ) // I hope that one is ok...
  300.         texUnits[i].order = 1;     
  301.     }  
  302.    
  303.     std::ofstream lkOutput;
  304.     std::string outputFileName ( filename );
  305.     outputFileName.append( "_lk" );
  306.     lkOutput.open( outputFileName.c_str(), std::ios::binary );
  307.  
  308.     lkOutput.write( startOfFile, startOfFileSize );
  309.     lkOutput.write( reinterpret_cast<char*>(&subs[0]), sizeof(submesh) * subs.size() );
  310.     lkOutput.write( reinterpret_cast<char*>(&texUnits[0]), sizeof(textureUnit) * texUnits.size() );
  311.     lkOutput.write( endOfFile, endOfFileSize );
  312.  
  313.     lkOutput.close();  
  314.    
  315.     delete[] startOfFile;
  316.     delete[] endOfFile;
  317.   }
  318.   else
  319.   {
  320.     skinFile.close();
  321.   }
  322. }
  323.  
  324. int main( int argc, char **argv )
  325. {
  326.   fixM2File( argv[1] );
  327.  
  328.   std::string skinGenFileName ( argv[1] );
  329.   skinGenFileName = skinGenFileName.substr( 0, skinGenFileName.size() - 3 );
  330.  
  331.   std::string skinFileNameZero ( skinGenFileName );
  332.   skinFileNameZero.append( "00.skin" );
  333.  
  334.   std::string skinFileNameOne ( skinGenFileName );
  335.   skinFileNameOne.append( "01.skin" );
  336.  
  337.   std::string skinFileNameTwo ( skinGenFileName );
  338.   skinFileNameTwo.append( "02.skin" );
  339.  
  340.   std::string skinFileNameThree ( skinGenFileName );
  341.   skinFileNameThree.append( "03.skin" );    
  342.  
  343.   fixSkinFile( skinFileNameZero );
  344.   fixSkinFile( skinFileNameOne );
  345.   fixSkinFile( skinFileNameTwo );
  346.   fixSkinFile( skinFileNameThree );
  347.  
  348.   return 0;
  349. }
Advertisement
Add Comment
Please, Sign In to add comment