Advertisement
Guest User

LightAdder

a guest
Aug 3rd, 2011
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. typedef unsigned int uint32;
  9. typedef int int32;
  10. typedef unsigned short uint16;
  11. typedef short int16;
  12.  
  13. struct Vec3F {
  14. /*0x000*/   float x;
  15. /*0x004*/   float y;
  16. /*0x008*/   float z;
  17. };
  18.  
  19. struct ABlock {
  20. /*0x000*/   uint16  InterpolationType;
  21. /*0x002*/   uint16  GlobalSequenceID;
  22. /*0x004*/   uint32  numberOfTimestampPairs;
  23. /*0x008*/   uint32  offsetToTimestampPairs;
  24. /*0x00C*/   uint32  numberOfKeyFramePairs;
  25. /*0x010*/   uint32  offsetToKeyFramePairs;
  26. };
  27.  
  28. struct Pairs {
  29. /*0x000*/   uint32  Number;
  30. /*0x004*/   uint32  Offset;
  31. };
  32.  
  33. struct M2Header {
  34. /*0x000*/   char    Magic[4];
  35. /*0x004*/   uint32  Version;
  36. /*0x008*/   uint32  lName;
  37. /*0x00C*/   uint32  ofsName;   
  38. /*0x010*/   uint32  GlobalModelFlags;
  39. /*0x014*/   uint32  nGlobalSequences;
  40. /*0x018*/   uint32  ofsGlobalSequences;
  41. /*0x01C*/   uint32  nAnimations;
  42. /*0x020*/   uint32  ofsAnimations;
  43. /*0x024*/   uint32  nAnimationLookup;
  44. /*0x028*/   uint32  ofsAnimationLookup;
  45. /*0x02C*/   uint32  nBones;
  46. /*0x030*/   uint32  ofsBones;
  47. /*0x034*/   uint32  nKeyBoneLookup;
  48. /*0x038*/   uint32  ofsKeyBoneLookup;
  49. /*0x03C*/   uint32  nVertices;
  50. /*0x040*/   uint32  ofsVertices;
  51. /*0x044*/   uint32  nViews;
  52. /*0x048*/   uint32  nColors;
  53. /*0x04C*/   uint32  ofsColors;
  54. /*0x050*/   uint32  nTextures;
  55. /*0x054*/   uint32  ofsTextures;
  56. /*0x058*/   uint32  nTransparency;
  57. /*0x05C*/   uint32  ofsTransparency;
  58. /*0x060*/   uint32  nUVAnimation;
  59. /*0x064*/   uint32  ofsUVAnimation;
  60. /*0x068*/   uint32  nTexReplace;
  61. /*0x06C*/   uint32  ofsTexReplace;
  62. /*0x070*/   uint32  nRenderFlags;
  63. /*0x074*/   uint32  ofsRenderFlags;
  64. /*0x078*/   uint32  nBoneLookupTable;
  65. /*0x07C*/   uint32  ofsBoneLookupTable;
  66. /*0x080*/   uint32  nTexLookup;
  67. /*0x084*/   uint32  ofsTexLookup;
  68. /*0x088*/   uint32  nTexUnits;
  69. /*0x08C*/   uint32  ofsTexUnits;
  70. /*0x090*/   uint32  nTransLookup;
  71. /*0x094*/   uint32  ofsTransLookup;
  72. /*0x098*/   uint32  nUVAnimLookup;
  73. /*0x09C*/   uint32  ofsUVAnimLookup;   
  74. /*0x0A0*/   Vec3F   VertexBox[2];
  75. /*0x0B8*/   float   VertexRadius;  
  76. /*0x0BC*/   Vec3F   BoundingBox[2];    
  77. /*0x0D4*/   float   BoundingRadius;    
  78. /*0x0D8*/   uint32  nBoundingTriangles;
  79. /*0x0DC*/   uint32  ofsBoundingTriangles;
  80. /*0x0E0*/   uint32  nBoundingVertices;
  81. /*0x0E4*/   uint32  ofsBoundingVertices;
  82. /*0x0E8*/   uint32  nBoundingNormals;
  83. /*0x0EC*/   uint32  ofsBoundingNormals;
  84. /*0x0F0*/   uint32  nAttachments;
  85. /*0x0F4*/   uint32  ofsAttachments;
  86. /*0x0F8*/   uint32  nAttachLookup;
  87. /*0x0FC*/   uint32  ofsAttachLookup;
  88. /*0x100*/   uint32  nEvents;
  89. /*0x104*/   uint32  ofsEvents;
  90. /*0x108*/   uint32  nLights;
  91. /*0x10C*/   uint32  ofsLights;
  92. /*0x110*/   uint32  nCameras;
  93. /*0x114*/   uint32  ofsCameras;
  94. /*0x118*/   uint32  nCameraLookup;
  95. /*0x11C*/   uint32  ofsCameraLookup;
  96. /*0x120*/   uint32  nRibbonEmitters;
  97. /*0x124*/   uint32  ofsRibbonEmitters; 
  98. /*0x128*/   uint32  nParticleEmitters;
  99. /*0x12C*/   uint32  ofsParticleEmitters;
  100. };
  101.  
  102. struct Light
  103. {
  104. /*0x00*/    uint16  Type;
  105. /*0x02*/    int16   Bone ;
  106. /*0x04*/    float   Position[3];
  107. /*0x10*/    ABlock  AmbientColor;
  108. /*0x24*/    ABlock  AmbientIntensity;
  109. /*0x38*/    ABlock  DiffuseColor;
  110. /*0x4C*/    ABlock  DiffuseIntensity;
  111. /*0x60*/    ABlock  AttenuationStart;
  112. /*0x74*/    ABlock  AttenuationEnd;
  113. /*0x88*/    ABlock  Unknown;
  114. };
  115.  
  116. const short OFFSET = 0x0;
  117. const short BUFFER = 8;
  118.  
  119. M2Header header;
  120. fstream m2file;
  121. char command;
  122.  
  123. float getABlockFloat(fstream *file, uint32 offset, int index = 0);
  124. void setABlockFloat(fstream *file, float value, uint32 offset, int index = 0);
  125. void setABlockInt(fstream *file, uint32 value, uint32 offset, int index);
  126.  
  127. void setUInt16(fstream *file, unsigned short value, unsigned int offset);
  128. void setInt16(fstream *file, short value, unsigned int offset);
  129. void setFloat(fstream *file, float value, unsigned int offset);
  130.  
  131. int main(int argc, char **argv) {
  132.     if(argc == 1)
  133.     {
  134.         cout << "You need to put a M2 file to get this working.";
  135.         cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  136.         return 0;
  137.     }
  138.     //TODO: Add check for M2 (magic) file.
  139.  
  140.     m2file.open(argv[1], ios::in | ios::out | ios::binary);
  141.  
  142.     m2file.seekg(OFFSET);
  143.     m2file.read(reinterpret_cast<char *>(&header), sizeof(M2Header));
  144.     Light *lights;
  145.  
  146.     lights = new Light[header.nLights];
  147.     if(!header.ofsLights == 0 && !header.nLights == 0)
  148.     {
  149.         m2file.seekg(header.ofsLights);
  150.  
  151.         for(int i=0; i<header.nLights; i++)
  152.         {
  153.             m2file.read(reinterpret_cast<char *>(&(lights[i])), sizeof(Light));
  154.         }
  155.     }
  156.  
  157.     cout << "Done by MegaBigBoss/Garthog. Thanks to Schlumpf and Tigurius for helping me." << endl << "Also, be careful. This tool could corrupt your M2" << endl << endl;
  158.     cout << "h : This message" << endl;
  159.     cout << "i : Print info" << endl;
  160.     cout << "c : Create new Light" << endl;
  161.     cout << "m : Modify existing light" << endl;
  162.     cout << "d : Delete existing light" << endl;
  163.     cout << "x : Exit" << endl;
  164.    
  165.     while(1)
  166.     {
  167.  
  168.         cout << endl << "> ";
  169.         cin >> command;
  170.  
  171.         switch(command)
  172.         {
  173.             case 'c':
  174.             {
  175.                 if(header.nLights > 0)
  176.                 {
  177.                     // il faut copier les lumières existantes, ainsi que leurs données, les zerofiller, et en recréer à la fin
  178.                     // du fichier avec les mêmes valeurs et tout et tout.
  179.                     cout << "This file already have lights. Please, delete them before trying to add new one, because it can only add one light to one model which hasn't got any light." << endl;
  180.                 }
  181.                 else
  182.                 {
  183.                     long size;
  184.                     const char null = NULL;
  185.  
  186.                     m2file.seekg(0,ifstream::end);
  187.                     m2file.seekp(0,ifstream::end);
  188.  
  189.                     size=m2file.tellg();
  190.  
  191.                     header.nLights = 1;
  192.                     header.ofsLights = size; // Formula to add that shit. ( Hm. End of the file ? And then file.put(260) - 1C4 all the ABlocks shits + 9C light size
  193.                    
  194.                     char tmp[0x260];
  195.                     memset(tmp, 0, sizeof(tmp));
  196.                     m2file.write(tmp, sizeof(tmp));
  197.  
  198.                     cout << endl << "DEBUG : " << hex << size << endl;
  199.  
  200.                     // Now, we need to define aaaaall the vars.
  201.                     Light newLight;
  202.                     float ar, ag, ab, ai, dr, dg, db, di;
  203.  
  204.                     cout << "Write the data following this format : [TYPE] [BONE] [X] [Y] [Z]" << endl;
  205.                     cin >> newLight.Type >> newLight.Bone >> newLight.Position[0] >> newLight.Position[1] >> newLight.Position[2];
  206.                     cout << "Now, this format : [AR] [AG] [AB] [AI] [DR] [DG] [DB] [DI]" << endl;
  207.                     cin >> ar >> ag >> ab >> ai >> dr >> dg >> db >> di;
  208.  
  209.                     /*
  210.                     *OFSLIGHT + A0 = OFS_PAIRS[0]
  211.                     *OFS_PAIRS[0].N = 1
  212.                     *OFS_PAIRS[0].OFS = OFS_PAIRS[0] + E0
  213.                     */
  214.                     uint32 one = 1;
  215.                     uint32 offset;
  216.  
  217.                     newLight.AmbientColor.InterpolationType = 0;
  218.                     newLight.AmbientColor.GlobalSequenceID = -1;
  219.                     newLight.AmbientColor.numberOfTimestampPairs = 1;
  220.                     newLight.AmbientColor.offsetToTimestampPairs = header.ofsLights + 0xA0;
  221.                     m2file.seekp(header.ofsLights + 0xA0);
  222.                     offset = header.ofsLights + 0xA0 + 0xE0;
  223.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  224.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  225.  
  226.                     newLight.AmbientColor.numberOfKeyFramePairs = 1;
  227.                     newLight.AmbientColor.offsetToKeyFramePairs = header.ofsLights + 0xB0; 
  228.                     m2file.seekp(header.ofsLights + 0xB0);
  229.                     offset = header.ofsLights + 0xB0 + 0xE0;
  230.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  231.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  232.  
  233.                     newLight.AmbientIntensity.InterpolationType = 0;
  234.                     newLight.AmbientIntensity.GlobalSequenceID = -1;
  235.                     newLight.AmbientIntensity.numberOfTimestampPairs = 1;
  236.                     newLight.AmbientIntensity.offsetToTimestampPairs = header.ofsLights + 0xC0;
  237.                     m2file.seekp(header.ofsLights + 0xC0);
  238.                     offset = header.ofsLights + 0xC0 + 0xE0;
  239.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  240.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  241.  
  242.                     newLight.AmbientIntensity.numberOfKeyFramePairs = 1;
  243.                     newLight.AmbientIntensity.offsetToKeyFramePairs = header.ofsLights + 0xD0; 
  244.                     m2file.seekp(header.ofsLights + 0xD0);
  245.                     offset = header.ofsLights + 0xD0 + 0xE0;
  246.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  247.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  248.  
  249.                     newLight.DiffuseColor.InterpolationType = 0;
  250.                     newLight.DiffuseColor.GlobalSequenceID = -1;
  251.                     newLight.DiffuseColor.numberOfTimestampPairs = 1;
  252.                     newLight.DiffuseColor.offsetToTimestampPairs = header.ofsLights + 0xE0;
  253.                     m2file.seekp(header.ofsLights + 0xE0);
  254.                     offset = header.ofsLights + 0xE0 + 0xE0;
  255.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  256.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  257.  
  258.                     newLight.DiffuseColor.numberOfKeyFramePairs = 1;
  259.                     newLight.DiffuseColor.offsetToKeyFramePairs = header.ofsLights + 0xF0; 
  260.                     m2file.seekp(header.ofsLights + 0xF0);
  261.                     offset = header.ofsLights + 0xF0 + 0xE0;
  262.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  263.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  264.  
  265.                     newLight.DiffuseIntensity.InterpolationType = 0;
  266.                     newLight.DiffuseIntensity.GlobalSequenceID = -1;
  267.                     newLight.DiffuseIntensity.numberOfTimestampPairs = 1;
  268.                     newLight.DiffuseIntensity.offsetToTimestampPairs = header.ofsLights + 0x100;   
  269.                     m2file.seekp(header.ofsLights + 0x100);
  270.                     offset = header.ofsLights + 0x100 + 0xE0;
  271.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  272.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  273.  
  274.                     newLight.DiffuseIntensity.numberOfKeyFramePairs = 1;
  275.                     newLight.DiffuseIntensity.offsetToKeyFramePairs = header.ofsLights + 0x110;
  276.                     m2file.seekp(header.ofsLights + 0x110);
  277.                     offset = header.ofsLights + 0x110 + 0xE0;
  278.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  279.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  280.  
  281.                     newLight.AttenuationStart.InterpolationType = 0;
  282.                     newLight.AttenuationStart.GlobalSequenceID = -1;
  283.                     newLight.AttenuationStart.numberOfTimestampPairs = 1;
  284.                     newLight.AttenuationStart.offsetToTimestampPairs = header.ofsLights + 0x120;
  285.                     m2file.seekp(header.ofsLights + 0x120);
  286.                     offset = header.ofsLights + 0x120 + 0xE0;
  287.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  288.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  289.  
  290.                     newLight.AttenuationStart.numberOfKeyFramePairs = 1;
  291.                     newLight.AttenuationStart.offsetToKeyFramePairs = header.ofsLights + 0x130;
  292.                     m2file.seekp(header.ofsLights + 0x130);
  293.                     offset = header.ofsLights + 0x130 + 0xE0;
  294.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  295.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  296.  
  297.                     newLight.AttenuationEnd.InterpolationType = 0;
  298.                     newLight.AttenuationEnd.GlobalSequenceID = -1;
  299.                     newLight.AttenuationEnd.numberOfTimestampPairs = 1;
  300.                     newLight.AttenuationEnd.offsetToTimestampPairs = header.ofsLights + 0x140; 
  301.                     m2file.seekp(header.ofsLights + 0x140);
  302.                     offset = header.ofsLights + 0x140 + 0xE0;
  303.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  304.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  305.  
  306.                     newLight.AttenuationEnd.numberOfKeyFramePairs = 1;
  307.                     newLight.AttenuationEnd.offsetToKeyFramePairs = header.ofsLights + 0x150;  
  308.                     m2file.seekp(header.ofsLights + 0x150);
  309.                     offset = header.ofsLights + 0x150 + 0xE0;
  310.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  311.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  312.  
  313.                     newLight.Unknown.InterpolationType = 0;
  314.                     newLight.Unknown.GlobalSequenceID = -1;
  315.                     newLight.Unknown.numberOfTimestampPairs = 1;
  316.                     newLight.Unknown.offsetToTimestampPairs = header.ofsLights + 0x160;
  317.                     m2file.seekp(header.ofsLights + 0x160);
  318.                     offset = header.ofsLights + 0x160 + 0xE0;
  319.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  320.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  321.  
  322.                     newLight.Unknown.numberOfKeyFramePairs = 1;
  323.                     newLight.Unknown.offsetToKeyFramePairs = header.ofsLights + 0x170;
  324.                     m2file.seekp(header.ofsLights + 0x170);
  325.                     offset = header.ofsLights + 0x170 + 0xE0;
  326.                     m2file.write(reinterpret_cast<char *>(&(one)), sizeof(uint32));
  327.                     m2file.write(reinterpret_cast<char *>(&(offset)), sizeof(uint32));
  328.                    
  329.                     setABlockFloat(&m2file, ar, newLight.AmbientColor.offsetToKeyFramePairs,0);
  330.                     setABlockFloat(&m2file, ag, newLight.AmbientColor.offsetToKeyFramePairs,1);
  331.                     setABlockFloat(&m2file, ab, newLight.AmbientColor.offsetToKeyFramePairs,2);
  332.                     setABlockFloat(&m2file, ai, newLight.AmbientIntensity.offsetToKeyFramePairs,0);
  333.  
  334.                     setABlockFloat(&m2file, dr, newLight.DiffuseColor.offsetToKeyFramePairs,0);
  335.                     setABlockFloat(&m2file, dg, newLight.DiffuseColor.offsetToKeyFramePairs,1);
  336.                     setABlockFloat(&m2file, db, newLight.DiffuseColor.offsetToKeyFramePairs,2);
  337.                     setABlockFloat(&m2file, di, newLight.DiffuseIntensity.offsetToKeyFramePairs,0);
  338.  
  339.                     float start = 1.2;
  340.                     float end = 2.4;
  341.  
  342.                     setABlockFloat(&m2file, start, newLight.AttenuationStart.offsetToKeyFramePairs,0);
  343.                     setABlockFloat(&m2file, end, newLight.AttenuationEnd.offsetToKeyFramePairs,0);
  344.                     setABlockInt(&m2file, 1, newLight.Unknown.offsetToKeyFramePairs,0);
  345.                    
  346.                     m2file.seekp(header.ofsLights);
  347.                     m2file.write(reinterpret_cast<char *>(&(newLight)), sizeof(newLight));
  348.  
  349.                     m2file.seekp(OFFSET);
  350.                     m2file.write(reinterpret_cast<char *>(&header), sizeof(M2Header));
  351.                 }
  352.             }
  353.             break;
  354.  
  355.             case 'd':
  356.             {
  357.                
  358.             }
  359.             break;
  360.  
  361.             case 'm':
  362.             {
  363.                 int entry = 0;
  364.                 bool menu = false;
  365.                 char subcommand;
  366.                 std::system ( "CLS" );
  367.                 cout << "Light entry > ";
  368.                 cin >> entry;
  369.                
  370.                 if(entry > header.nLights)
  371.                 {
  372.                     cout << "Bad entry";
  373.                 }
  374.                 else
  375.                 {
  376.                     menu = true;
  377.  
  378.                     cout << endl;
  379.                     cout << "r : Return in the previous menu" << endl;
  380.                     cout << "t : Set type of light" << endl;
  381.                     cout << "b : Set bone of light" << endl;
  382.                     cout << "p : Set X, Y, Z of light" << endl << endl;
  383.                     cout << "a : Set RGB of light's AmbientColor" << endl;
  384.                     cout << "z : Set intensity of AmbiantColor" << endl << endl;
  385.                     cout << "d : Set RGB of light's DiffuseColor" << endl;
  386.                     cout << "i : Set intensity of light's DiffuseColor" << endl << endl;
  387.  
  388.                     while(menu == true)
  389.                     {
  390.                         cout << "> ";
  391.                         cin >> subcommand;
  392.                         switch(subcommand)
  393.                         {
  394.                             case 'r':
  395.                             {
  396.                                 menu = false;
  397.                                 std::system ( "CLS" );
  398.                                 cout << "Done by MegaBigBoss/Garthog. Thanks to Schlumpf and Tigurius for helping me." << endl << endl;
  399.                                 cout << "h : This message" << endl;
  400.                                 cout << "i : Print info" << endl;
  401.                                 cout << "c : Create new Light" << endl;
  402.                                 cout << "m : Modify existing light" << endl;
  403.                                 cout << "d : Delete existing light" << endl;
  404.                                 cout << "x : Exit" << endl;
  405.                             }
  406.                             break;
  407.  
  408.                             case 't':
  409.                             {
  410.                                 uint16 newtype;
  411.  
  412.                                 cout << "New type >";
  413.                                 cin >> newtype;
  414.                                
  415.                                 lights[entry].Type = newtype;
  416.                                 setUInt16(&m2file, newtype, (header.ofsLights + (entry * sizeof(Light))));
  417.                             }
  418.                             break;
  419.  
  420.                             case 'b':
  421.                             {
  422.                                 int16 bone;
  423.  
  424.                                 cout << "New bone >";
  425.                                 cin >> bone;
  426.  
  427.                                 lights[entry].Bone = bone;
  428.                                 setInt16(&m2file, bone, (header.ofsLights + sizeof(uint16) + (entry * sizeof(Light))));
  429.                             }
  430.                             break;
  431.  
  432.                             case 'p':
  433.                             {
  434.                                 float x, y, z;
  435.                                 cout << "X >";
  436.                                 cin >> x;
  437.                                 cout << endl << "Y >";
  438.                                 cin >> y;
  439.                                 cout << endl << "Z >";
  440.                                 cin >> z;
  441.  
  442.                                 lights[entry].Position[0] = x;
  443.                                 lights[entry].Position[1] = y;
  444.                                 lights[entry].Position[2] = z;
  445.  
  446.                                 setFloat(&m2file, x, (header.ofsLights + 0x04 + (entry * sizeof(Light))));
  447.                                 setFloat(&m2file, y, (header.ofsLights + 0x08 + (entry * sizeof(Light))));
  448.                                 setFloat(&m2file, z, (header.ofsLights + 0xC  + (entry * sizeof(Light))));
  449.                             }
  450.                             break;
  451.  
  452.                             case 'a':
  453.                             {
  454.                                 float r, g, b;
  455.                                 cout << "R >";
  456.                                 cin >> r;
  457.                                 cout << endl << "G >";
  458.                                 cin >> g;
  459.                                 cout << endl << "B >";
  460.                                 cin >> b;
  461.  
  462.                                 setABlockFloat(&m2file, r, lights[entry].AmbientColor.offsetToKeyFramePairs);
  463.                                 setABlockFloat(&m2file, g, lights[entry].AmbientColor.offsetToKeyFramePairs, 1);
  464.                                 setABlockFloat(&m2file, b, lights[entry].AmbientColor.offsetToKeyFramePairs, 2);
  465.                             }
  466.                             break;
  467.                             case 'z':
  468.                             {
  469.                                 float intensity;
  470.                                 cout << "Intensity >";
  471.                                 cin >> intensity;
  472.  
  473.                                 setABlockFloat(&m2file, intensity, lights[entry].AmbientIntensity.offsetToKeyFramePairs);
  474.                             }
  475.                             break;
  476.  
  477.                             case 'd':
  478.                             {
  479.                                 float r, g, b;
  480.                                 cout << "R >";
  481.                                 cin >> r;
  482.                                 cout << endl << "G >";
  483.                                 cin >> g;
  484.                                 cout << endl << "B >";
  485.                                 cin >> b;
  486.  
  487.                                 setABlockFloat(&m2file, r, lights[entry].DiffuseColor.offsetToKeyFramePairs);
  488.                                 setABlockFloat(&m2file, g, lights[entry].DiffuseColor.offsetToKeyFramePairs, 1);
  489.                                 setABlockFloat(&m2file, b, lights[entry].DiffuseColor.offsetToKeyFramePairs, 2);
  490.                             }
  491.                             break;
  492.                             case 'i':
  493.                             {
  494.                                 float intensity;
  495.                                 cout << "Intensity >";
  496.                                 cin >> intensity;
  497.  
  498.                                 setABlockFloat(&m2file, intensity, lights[entry].DiffuseIntensity.offsetToKeyFramePairs);
  499.                             }
  500.                             break;
  501.                         }
  502.                     }
  503.                 }
  504.             }
  505.             break;
  506.  
  507.             case 'i':
  508.             {
  509.                 cout << "There are : " << header.nLights << " lights in this M2." << endl << endl;
  510.  
  511.                 for(int i=0; i<header.nLights; i++)
  512.                 {
  513.                     cout << "Light " << i << " ( " << std::hex << header.ofsLights + (i * sizeof(Light)) << " )" << endl;
  514.                     cout << "Type : " << lights[i].Type << endl;
  515.                     cout << "Bone : " << lights[i].Bone << endl;
  516.                     cout << "Position X = " << lights[i].Position[0] << " Y = " << lights[i].Position[1] << " Z = " << lights[i].Position[2] << endl;      
  517.                    
  518.                     cout << "Ambient Color ( RGB ) : "  << getABlockFloat(&m2file, lights[i].AmbientColor.offsetToKeyFramePairs) << " " << getABlockFloat(&m2file, lights[i].AmbientColor.offsetToKeyFramePairs, 1) << " " << getABlockFloat(&m2file, lights[i].AmbientColor.offsetToKeyFramePairs, 2) << endl;
  519.                     cout << "Ambient intensity : " << getABlockFloat(&m2file, lights[i].AmbientIntensity.offsetToKeyFramePairs) << endl;
  520.                
  521.                     cout << "Diffuse Color ( RGB ) : "  << getABlockFloat(&m2file, lights[i].DiffuseColor.offsetToKeyFramePairs) << " " << getABlockFloat(&m2file, lights[i].AmbientColor.offsetToKeyFramePairs, 1) << " " << getABlockFloat(&m2file, lights[i].AmbientColor.offsetToKeyFramePairs, 2) << endl;
  522.                     cout << "Diffuse intensity : " << getABlockFloat(&m2file, lights[i].DiffuseIntensity.offsetToKeyFramePairs) << endl;
  523.                 }          
  524.             }
  525.             break;
  526.            
  527.             case 'x':
  528.             {
  529.                 delete[] lights;
  530.                 m2file.close();
  531.                 return 0;
  532.             }
  533.             break;
  534.  
  535.             default:
  536.             {
  537.                 cout << "Done by MegaBigBoss/Garthog. Thanks to Schlumpf and Tigurius for helping me." << endl << endl;
  538.                 cout << "i : Print info" << endl;
  539.                 cout << "c : Create new Light" << endl;
  540.                 cout << "m : Modify existing light" << endl;
  541.                 cout << "d : Delete existing light" << endl;
  542.                 cout << "x : Exit" << endl << endl;
  543.             }
  544.             break;
  545.         }
  546.     }
  547. }
  548.  
  549. float getABlockFloat(fstream *file, uint32 offset, int index)
  550. {
  551.     uint32 location = file->tellg();
  552.     int32 real_offset;
  553.     float value;
  554.  
  555.     file->seekg(offset + sizeof(int32));
  556.     file->read(reinterpret_cast<char *>(&(real_offset)), sizeof(int32));
  557.     file->seekg(real_offset + (index * sizeof(float)));
  558.     file->read(reinterpret_cast<char *>(&(value)), sizeof(float));
  559.  
  560.     file->seekg(location);
  561.    
  562.     return value;
  563. }
  564.  
  565. void setABlockFloat(fstream *file, float value, uint32 offset, int index)
  566. {
  567.     uint32 glocation = file->tellg();
  568.     uint32 plocation = file->tellp();
  569.     int32 real_offset;
  570.  
  571.     file->seekg(offset + sizeof(int32));
  572.     file->read(reinterpret_cast<char *>(&(real_offset)), sizeof(int32));
  573.     file->seekp(real_offset + (index * sizeof(float)));
  574.  
  575.     file->write(reinterpret_cast<char *>(&(value)), sizeof(float));
  576.  
  577.     file->seekg(glocation);
  578.     file->seekp(plocation);
  579. }
  580.  
  581. void setABlockInt(fstream *file, uint32 value, uint32 offset, int index)
  582. {
  583.     uint32 glocation = file->tellg();
  584.     uint32 plocation = file->tellp();
  585.     int32 real_offset;
  586.  
  587.     file->seekg(offset + sizeof(int32));
  588.     file->read(reinterpret_cast<char *>(&(real_offset)), sizeof(int32));
  589.     file->seekp(real_offset + (index * sizeof(uint32)));
  590.  
  591.     file->write(reinterpret_cast<char *>(&(value)), sizeof(uint32));
  592.  
  593.     file->seekg(glocation);
  594.     file->seekp(plocation);
  595. }
  596.  
  597. void setUInt16(fstream *file, unsigned short value, unsigned int offset)
  598. {
  599.     uint32 glocation = file->tellg();
  600.     uint32 plocation = file->tellp();
  601.  
  602.     file->seekp(offset);
  603.     file->write(reinterpret_cast<char *>(&(value)), sizeof(unsigned short));
  604.  
  605.     file->seekg(glocation);
  606.     file->seekp(plocation);
  607. }
  608.  
  609. void setInt16(fstream *file, short value, unsigned int offset)
  610. {
  611.     uint32 glocation = file->tellg();
  612.     uint32 plocation = file->tellp();
  613.  
  614.     file->seekp(offset);
  615.     file->write(reinterpret_cast<char *>(&(value)), sizeof(short));
  616.  
  617.     file->seekg(glocation);
  618.     file->seekp(plocation);
  619. }
  620.  
  621. void setFloat(fstream *file, float value, unsigned int offset)
  622. {
  623.     uint32 glocation = file->tellg();
  624.     uint32 plocation = file->tellp();
  625.  
  626.     file->seekp(offset);
  627.     file->write(reinterpret_cast<char *>(&(value)), sizeof(float));
  628.  
  629.     file->seekg(glocation);
  630.     file->seekp(plocation);
  631. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement