home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 4966 / main.7z / main.cpp
Encoding:
C/C++ Source or Header  |  2012-01-08  |  17.6 KB  |  370 lines

  1. //transform from doa3 to doa2u model format
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <io.h>
  5. #include <fcntl.h>
  6. #include <string>
  7. #include <stdio.h>
  8. #include <set>
  9. #include <algorithm>
  10. #include <fstream>
  11. #include <iomanip>
  12. #include <map>
  13. #include <vector>
  14. using namespace std;
  15.  
  16. int main()
  17. {
  18.     ofstream logFile2("MyLog2.log");
  19. //    ofstream logFile3("MyLog3.log");
  20.     cout << hex << setfill('0');
  21.     logFile2 << hex << setfill('0');
  22. //    logFile3 << hex << setfill('0');
  23.     int iterato1 = 0, iterato23 = 0;
  24.  
  25.     WIN32_FIND_DATAA findS;
  26.     HANDLE findH = FindFirstFile("*.xpr", &findS);
  27.     if(findH == INVALID_HANDLE_VALUE){
  28.         MessageBox(0, "bin File not found", 0, 0);
  29.         return 0;
  30.     }
  31.     try{
  32.         do{
  33.             //open emp
  34.             string fName(findS.cFileName);
  35.             logFile2 << fName.c_str() << " ";
  36.             cout << fName.c_str() << " ";
  37.             int fh = _open(fName.c_str(), O_RDONLY | O_BINARY);
  38.             if(fh == -1){
  39.                 MessageBox(0, "Can't open the bin file", "Error", MB_OK);
  40.                 return 0;
  41.             }
  42.             int bufSize2 = _lseek(fh, 0, SEEK_END);
  43.             byte* buf2 = (byte*)malloc(bufSize2);
  44.             int* ibuf2 = (int*)buf2;
  45.             WORD* Wbuf2 = (WORD*)buf2;
  46.             _lseek(fh, 0, SEEK_SET);
  47.             _read(fh, buf2, bufSize2);
  48.             close(fh);
  49.  
  50.  
  51.             const int dwsz4 = 4;
  52.             const int wrdsz2 = 2;
  53.             const int iobjsOffset10 = 10;
  54.             int someIntOffset1 = 27; // delete this if it's not needed
  55.  
  56.  
  57.             DWORD shit1[4] = {'JBO', 0, 4, 0};//first line of obj {OBJ, jointTypeX, someOffset4, unusedX};
  58.             DWORD shit2[4] = {0x42480000, 1, 1, -1};//?texture shit {brightnes, items, type, FFs};{type1 has no brightness}
  59.             DWORD shit3[4] = {0x20, 0, 2, 1};//?{WORD vertexSize,WORD bufferNo, indexBuffer start offset, number of primitives, ?2}
  60.             DWORD xV4Head = 0x12345678;
  61.             DWORD defaultFloat = 0x3F800000;
  62.  
  63.             byte* newMdl2 = (byte*)malloc(bufSize2);
  64.             int* inewMdl2 = (int*)newMdl2;
  65.             byte* obj2Offset;
  66.             int sizeOfObj = 0;
  67.  
  68.             memset(newMdl2, 0, bufSize2);
  69.             memcpy(newMdl2, buf2, ibuf2[iobjsOffset10]);//copy the header of xpr0 with mdl header wich size = offset of first OBJ
  70.  
  71.             int vtfCounter = 0;
  72.             if(ibuf2[5] == 'LDM'){
  73.                 for(int i = 0; i < ibuf2[6]; i++){ // for each OBJ
  74.                     sizeOfObj = 0;
  75.                     obj2Offset = newMdl2 + inewMdl2[i + iobjsOffset10];
  76.  
  77.                     int fullSize = 0;
  78.                     for(int j = 0; j < 4; j++){ // for each vtf
  79.                         int vtfDeclaredSize = *(DWORD*)(buf2 + ibuf2[i + iobjsOffset10] + 0x10 + j * 0x10);
  80.                         if(vtfDeclaredSize != 0){//has vtfDeclaredSize
  81.                             int vtfSize = *(DWORD*)(buf2 + ibuf2[4] + 20 + vtfCounter * 12  + 16) - *(DWORD*)(buf2 + ibuf2[4] + 20 + vtfCounter * 12  + 4);
  82.                             if((vtfSize%vtfDeclaredSize == 0) && (vtfSize/vtfDeclaredSize != 0x20) && (vtfSize/vtfDeclaredSize != 0x18) && (vtfSize/vtfDeclaredSize != 0x30)){
  83.                                 cout << " " << i << "_" << fName.c_str() << " " << vtfSize << "/" << vtfDeclaredSize <<  "=" << (vtfSize/vtfDeclaredSize) << "\r\n";
  84.                                 logFile2 << " " << i << "_" << fName.c_str() << " " << (vtfSize/vtfDeclaredSize) << "\r\n";
  85.                             }
  86.                             vtfCounter++;
  87.                             fullSize += vtfDeclaredSize;
  88.                         }
  89.                     }
  90.  
  91.                     memcpy(obj2Offset + sizeOfObj, shit1, 0x10);//1 line is shit1
  92.                     sizeOfObj += 0x10;
  93.                     memcpy(obj2Offset + sizeOfObj, buf2 + ibuf2[i + iobjsOffset10] + 0x50, 0x10);//2 4floats1
  94.                     sizeOfObj += 0x10;
  95.                     memcpy(obj2Offset + sizeOfObj, buf2 + ibuf2[i + iobjsOffset10] + 0x10, 0x40);//3-4 vtxOffnSz; need to fix later the xV4 offset
  96.                     sizeOfObj += 0x80; // also jump over the bunch of nulls
  97.  
  98.                     int txtr3Offset = ibuf2[i + iobjsOffset10] + 0x60;//texture offset in begining of texture; relate all offsets to it
  99.  
  100.             int allSecondXV2sizes = 0;
  101.  
  102.                     while(*(int*)(buf2 + txtr3Offset) != 0){//convert textures while it's aviable(header's not null)
  103.         //                memcpy(obj2Offset + sizeOfObj, buf2 + txtr3Offset + 0, 0x60);//11-16 txtType+floats+4nulls has some wrong shit
  104.         //                memcpy(obj2Offset + sizeOfObj + 0x20, &defaultFloat, 0x4);//try to fix the missing part of float field
  105.         //                *(int*)&obj2Offset[sizeOfObj + 4] = 0x90;//need to find more abou this unknown 90
  106.         //                sizeOfObj += 0x60;//some shit fixed before this
  107.                         memcpy(obj2Offset + sizeOfObj, buf2 + txtr3Offset + 0, 0x20);//11-16 txtType+floats+4nulls has some wrong shit
  108.                         //if(buf2[txtr3Offset + 0x68] == 1){obj2Offset[sizeOfObj] = 1;}//?? add transparency(sometimes is wrong)//////////////////
  109.                         int array1size = *(int*)(buf2 + txtr3Offset + 0x68);//the amount of items in array is right before the array
  110.                         int transparency = 0;
  111.                         for(int l=0; l < array1size; l++){
  112.                             int textureNo = *(int*)(buf2 + txtr3Offset + 0x6C + l*0x10);
  113.                             if(textureNo == 0x888)continue;
  114.                             if(*(int*)(buf2 + txtr3Offset + 0x78 + l*0x10) == 0x1000400){
  115.                                 transparency = 0;
  116.                                 break;
  117.                             }
  118.                             if(buf2[ibuf2[4]+20 +ibuf2[9]*12 + textureNo*20 +12 +1] != 0xC)transparency = 1;
  119.                         }
  120.                         obj2Offset[sizeOfObj] = transparency;//patch transparency
  121.  
  122.                         *(int*)&obj2Offset[sizeOfObj + 4] = 0x80 + array1size * 0x10;//the size of the entire textured item
  123.                         *(int*)&obj2Offset[sizeOfObj + 8] = 0;//just checking something; erase me
  124.                         *(int*)&obj2Offset[sizeOfObj + 0xC] = 0;//just checking something; erase me
  125.                         sizeOfObj += 0x20;
  126.                         memcpy(obj2Offset + sizeOfObj, buf2 + txtr3Offset + 0x24, 0x2C);//11-16 txtType+floats+4nulls has some wrong shit
  127.                         sizeOfObj += 0x2C;
  128.                         memcpy(obj2Offset + sizeOfObj, &defaultFloat, 0x4);//try to fix the missing part of float field
  129.                         sizeOfObj += 0x4 + 0x10; //jump over nulls
  130.  
  131.                         shit2[0] = *(int*)(buf2 + txtr3Offset + 0x64);//fix the brightness
  132.                         shit2[1] = array1size;//fix the amount of items in linked array1
  133.                        //shit2[2] = 3;//change texture type to 3////////////////////use 1 for clear look
  134.                         memcpy(obj2Offset + sizeOfObj, shit2, 0x10);//17 ?texture shit
  135.                         sizeOfObj += 0x10;
  136.                         txtr3Offset += 0x6C;//move the texture offset to it's array1
  137.                         for(int j = 0; j < array1size; j++){//18 copy every item of array1 coz they're linked
  138.                             memcpy(obj2Offset + sizeOfObj, buf2 + txtr3Offset + j * 0x10, 0x10);
  139.                             sizeOfObj += 0x10;
  140.                         }
  141.                         txtr3Offset += array1size * 0x10 + 4;//move the texture offset to it's array2
  142.  
  143.                         array1size = *(int*)(buf2 + txtr3Offset - 4);//the amount of items in array is right before the array
  144.          //               int secondXV2size = 0;
  145.         //                for(int j = 0; j < array1size; j++){//resolve secondXV2size
  146.         //                    secondXV2size += *(int*)(buf2 + txtr3Offset + j*0x10 + 3*4);
  147.         //                    secondXV2size += 4;
  148.         //                }
  149.         //                secondXV2size -= 4;
  150.  
  151.                         shit3[1] = *(int*)(buf2 + txtr3Offset + 0x8);//vertex start
  152.                         shit3[2] = *(int*)(buf2 + txtr3Offset + 0x8 + (array1size - 1)*0x10) + *(int*)(buf2 + txtr3Offset + 0xC + (array1size - 1)*0x10) - shit3[1];//vertex end
  153.                         if(array1size > 1)shit3[2] += 2;
  154.                         memcpy(obj2Offset + sizeOfObj, shit3, 0x10);//19 ?unknown shit with 20
  155.         //                *(int*)&obj2Offset[sizeOfObj + 8] = secondXV2size;//the ammount of vertexes to draw
  156.                         sizeOfObj += 0x10;
  157.  
  158.                         txtr3Offset += array1size * 0x10 + 4;//move the texture offset to begining of the next texture
  159.                allSecondXV2sizes += shit3[2] + 2;
  160.                     }
  161.  
  162.                     sizeOfObj += 4; //jump over the null "header for next texture" //USE ONLY FOR THE LAST TEXTURE
  163.                     memcpy(obj2Offset + sizeOfObj, &xV4Head, 4);//header of vertex buffer
  164.                     sizeOfObj += 4;
  165.  
  166.                     for(int j = 0; j < 4; j++){//for each possible vector offset n size
  167.                         int xV4Offset = *(DWORD*)(obj2Offset + 11 * dwsz4 + j * 0x10);
  168.                         int xV4Size = *(DWORD*)(obj2Offset + 10 * dwsz4 + j * 0x10);
  169.  
  170.                         if(xV4Offset != 0){
  171.                             *(int*)&obj2Offset[11 * dwsz4 + j * 0x10] = obj2Offset - newMdl2 + sizeOfObj;//fix the xV4 offset in the obj for DOA2
  172.                             memcpy(obj2Offset + sizeOfObj, buf2 + xV4Offset, xV4Size * wrdsz2);//copy each short of xV4
  173.                             sizeOfObj += xV4Size * wrdsz2;
  174.  
  175.                             allSecondXV2sizes -= xV4Size;
  176.                         }
  177.                     }
  178.         //     if(allSecondXV2sizes != 0)cout << i << " do not match " << allSecondXV2sizes << endl;//check if all vertexes are used
  179.                     inewMdl2[i + iobjsOffset10 + 1] = obj2Offset - newMdl2 + sizeOfObj;//fix the offset of the next obj in newMdl
  180.                 }
  181.             }else{
  182.                 continue;
  183.             }
  184.             inewMdl2[4] = sizeOfObj - 20 + obj2Offset - newMdl2;
  185.             int sizeOfDirHead = ibuf2[2] - ibuf2[4] - 20;
  186.             memcpy(obj2Offset + sizeOfObj, buf2 + ibuf2[4] + 20, sizeOfDirHead);//copy included directory header
  187.  
  188.             memcpy(buf2, newMdl2, ibuf2[2]);//copy new mdl over the old one
  189.  
  190.             string catName = fName;
  191.  
  192.             DWORD bytesRead;
  193.             fName = "converted//" + fName;// + ".xpr";
  194.             HANDLE h = CreateFile(fName.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
  195.             if(!h){
  196.                 MessageBox(0,"Can't create file", "Error(Already exist?)", MB_OK);
  197.                 return 0;
  198.             }
  199.             WriteFile(h, (byte*)buf2, bufSize2, &bytesRead, 0);//ogg offset 82
  200.             CloseHandle(h);
  201.             free(newMdl2);
  202.             free(buf2);
  203.  
  204.  
  205.  
  206.             ////////////////////// transform the cat file here ///////////////////////
  207.             byte cat2tempBuf[200] = {0};//buffer for new cat header
  208.             //open cat
  209.             catName.resize(catName.size() - 3);
  210.             catName += "cat";
  211.             fh = _open(catName.c_str(), O_RDONLY | O_BINARY);
  212.             if(fh == -1){
  213.                 logFile2 << endl;
  214.                 cout << endl;
  215.                 continue;
  216.             }
  217.             int bufSize3 = _lseek(fh, 0, SEEK_END);
  218.             byte* buf3 = (byte*)malloc(bufSize3);
  219.             int* ibuf3 = (int*)buf3;
  220.             WORD* Wbuf3 = (WORD*)buf3;
  221.             _lseek(fh, 0, SEEK_SET);
  222.             _read(fh, buf3, bufSize3);
  223.             close(fh);
  224.  
  225.             logFile2 << catName.c_str() << " " << endl;
  226.             cout << catName.c_str() << " " << endl;
  227.             if(ibuf3[0] != 1){
  228.                 logFile2 << " the cat header is crypted " << endl;
  229.                 cout << " the cat header is crypted " << endl;
  230.             }
  231. ////////convert bones arrays from 3 to 2
  232.             int pHair = ibuf3[2], pBoobs = ibuf3[3], pJoints = ibuf3[4];
  233.             int cat2Head[19] = {0x20, 0x14,/*hair*/ 0,/*boobs*/ 0,/*joints*/ 0, /*doa3skeletonType*/1, -1, /*high offset*/0x39, /*high*/1, 0,0, 0,0, 0,0, 0,0, 0,0};
  234.  
  235.             vector<byte> vBones;
  236.             vector<DWORD> vBonesHeads;
  237.             int newHeadSize = sizeof(cat2Head), boneOffset = ibuf3[7] + 0x14 + 1;
  238.             //high poly
  239.             vBones.push_back(0xFE);
  240.             vBonesHeads.push_back(sizeof(cat2Head) + vBones.size() - 0x14);
  241.             vBonesHeads.push_back(1);
  242.             for(int highBone = 0; highBone < 0xf; highBone++, boneOffset++){
  243.                 vBones.push_back(1);
  244.                 vBones.push_back(highBone);
  245.                 vBones.push_back(buf3[boneOffset]);
  246.             }
  247.             if(buf3[boneOffset] == 0xFD){//eyes
  248.                 boneOffset++;
  249.                 vBones.push_back(1);
  250.                 vBones.push_back(0x11);
  251.                 vBones.push_back(buf3[boneOffset]);
  252.                 boneOffset++;
  253.                 vBones.push_back(1);
  254.                 vBones.push_back(0x15);
  255.                 vBones.push_back(buf3[boneOffset]);
  256.                 boneOffset++;
  257.             }
  258.             if(buf3[boneOffset] == 0xFB){//wirsts //problem here
  259.                 boneOffset++;
  260.                 vBones.push_back(1);
  261.                 vBones.push_back(0x80);
  262.                 vBones.push_back(buf3[boneOffset]);
  263.                 boneOffset++;
  264.                 vBones.push_back(1);
  265.                 vBones.push_back(0x81);
  266.                 vBones.push_back(buf3[boneOffset]);
  267.                 boneOffset++;
  268.             }
  269.             vBones.push_back(0);
  270.             vBones.push_back(0);
  271.             vBones.push_back(0);
  272.             //low poly
  273.             if(buf3[boneOffset] == 0xF9){
  274.                 vBonesHeads.push_back(sizeof(cat2Head) + vBones.size() - 0x14);
  275.                 vBonesHeads.push_back(9);
  276.                 boneOffset++;
  277.                 for(int lowBone = 0; lowBone < 0xf; lowBone++, boneOffset++){
  278.                     vBones.push_back(1);
  279.                     vBones.push_back(lowBone);
  280.                     vBones.push_back(buf3[boneOffset]);
  281.                 }
  282.                 vBones.push_back(0);
  283.                 vBones.push_back(0);
  284.                 vBones.push_back(0);
  285.             }
  286.             //shadow
  287.             if(buf3[boneOffset] == 0xF8){
  288.                 vBonesHeads.push_back(sizeof(cat2Head) + vBones.size() - 0x14);
  289.                 vBonesHeads.push_back(5);
  290.                 vBones.push_back(0xF0);
  291.                 boneOffset++;
  292.                 for(int shadowBone = 0; shadowBone < 0xf; shadowBone++, boneOffset++){
  293.                     vBones.push_back(buf3[boneOffset]);
  294.                 };
  295.                 vBones.push_back(0);
  296.                 newHeadSize += vBones.size();
  297.             }
  298.             //ninja effect
  299.             if(buf3[boneOffset] == 0xF7){
  300.                 vBones.push_back(0xFE);
  301.                 vBonesHeads.push_back(sizeof(cat2Head) + vBones.size() - 0x14);
  302.                 vBonesHeads.push_back(7);
  303.                 boneOffset++;
  304.                 for(int petal = 0; petal < 0x3; petal++, boneOffset++){
  305.                     vBones.push_back(buf3[boneOffset]);
  306.                 };
  307.                 vBones.push_back(0);
  308.             }
  309. //                //genra weapon effect
  310. //                if(buf3[boneOffset] == 0xF6){
  311. //                }
  312. //                //glowing effect for zack and genra
  313. //                if(buf3[10] == 4){
  314. //                }
  315.  
  316.             int shiftToHex = 0x10 - (sizeof(cat2Head) + vBones.size()) % 0x10;
  317.             for(int i = 0; i < shiftToHex; i++){
  318.                 vBones.push_back(0);
  319.             }
  320.             if(buf3[0] == 1){
  321.                 cat2Head[2] = pHair + sizeof(cat2Head) + vBones.size();
  322.                 cat2Head[4] = pJoints + sizeof(cat2Head) + vBones.size();
  323.  
  324.                 if(pJoints != 0){
  325.                     ////////// find and replace all doa3 wrists with doau wrists
  326.                     int curJoint = 0;
  327.                     while(*(DWORD*)(buf3 + pJoints + curJoint*4)){
  328.                         int temp = pJoints +*(DWORD*)(buf3 +pJoints +curJoint*4) +10;
  329.                         if(buf3[temp] == 0x13)buf3[temp] = 0x80;//80
  330.                         if(buf3[temp] == 0x14)buf3[temp] = 0x81;//81
  331.                         if(buf3[temp+1] != 1)
  332.                             buf3[temp+1] = 1;//joint any type to 1
  333.                         curJoint++;
  334.                     }
  335.                 }
  336.  
  337.             }
  338.             for(int i = 0; i < vBonesHeads.size(); i++){//copy the offsets to temp header
  339.                 cat2Head[7 + i] = vBonesHeads[i];
  340.             }
  341.             memcpy(cat2tempBuf, cat2Head, sizeof(cat2Head));
  342.             for(int i = 0; i < vBones.size(); i++){
  343.                 cat2tempBuf[i + sizeof(cat2Head)] = vBones[i];
  344.             }
  345.  
  346. ////////end convert bones
  347.  
  348.  
  349.             bytesRead;
  350.             catName = "converted//" + catName;// + ".cat";
  351.             h = CreateFile(catName.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
  352.             if(!h){
  353.                 MessageBox(0,"Can't create file", "Error(Already exist?)", MB_OK);
  354.                 return 0;
  355.             }
  356.             WriteFile(h, cat2tempBuf, sizeof(cat2Head) + vBones.size(), &bytesRead, 0);
  357.             WriteFile(h, buf3, bufSize3, &bytesRead, 0);
  358.             CloseHandle(h);
  359.  
  360.             free(buf3);
  361.             ////////////////////// cat transformation ends here ///////////////////////
  362.  
  363.         }while(FindNextFile(findH, &findS));
  364.         FindClose(findH);
  365.     }catch(...){}
  366.  
  367.     system("pause");
  368.     return 0;
  369. }
  370.