SkeletonHack.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // SkeletonHack.cpp : Defines the entry point for the console application.
  19. //
  20. #include "stdafx.h"
  21. class HTreeHackClass
  22. {
  23. public:
  24. HTreeHackClass(void);
  25. ~HTreeHackClass(void);
  26. void Load_W3D(ChunkLoadClass & cload);
  27. void Save_W3D(ChunkSaveClass & csave);
  28. int Num_Pivots(void) { return Header.NumPivots; }
  29. // Skeleton Modification: moves a bone to the end of the array. Use to make
  30. // skeletons compatible with existing anims by moving all new bones to the end of the array.
  31. void Move_Bone_To_End(int bone_index);
  32. protected:
  33. bool read_pivots(ChunkLoadClass & cload);
  34. bool read_pivot_fixups(ChunkLoadClass & cload);
  35. bool write_pivots(ChunkSaveClass & csave);
  36. bool write_pivot_fixups(ChunkSaveClass & csave);
  37. void move_single_bone_to_end(unsigned int bone_index);
  38. int find_first_bad_bone(void);
  39. W3dHierarchyStruct Header;
  40. W3dPivotStruct * Pivots;
  41. W3dPivotFixupStruct * PivotFixups;
  42. };
  43. void Print_Usage(void)
  44. {
  45. printf("USAGE: SkeletonHack <W3D Filename> <BoneIndex>\r\n");
  46. printf("WARNING: This program only works on w3d files that contain a skeleton *ONLY*\r\n");
  47. printf("WARNING: This program modifies the input file.\r\n");
  48. }
  49. int main(int argc, char* argv[])
  50. {
  51. // See if we got the proper number of arguments
  52. if (argc != 3) {
  53. Print_Usage();
  54. return 0;
  55. }
  56. // Try to open the file,
  57. char * filename = argv[1];
  58. int bone_index = atoi(argv[2]);
  59. RawFileClass in_file(filename);
  60. if (!in_file.Is_Available()) {
  61. printf("Unable to open file: %s\r\n",filename);
  62. return 0;
  63. }
  64. // Open the file, read the first chunk, and verify that it is an HTree
  65. in_file.Open();
  66. ChunkLoadClass cload(&in_file);
  67. cload.Open_Chunk();
  68. if (cload.Cur_Chunk_ID() != W3D_CHUNK_HIERARCHY) {
  69. printf("Other chunks encountered in file: %s\r\n",filename);
  70. cload.Close_Chunk();
  71. in_file.Close();
  72. return 0;
  73. }
  74. // Read the hierarchy tree into memory and close the file.
  75. HTreeHackClass htree;
  76. htree.Load_W3D(cload);
  77. cload.Close_Chunk();
  78. in_file.Close();
  79. // Validate the bone_index parameter
  80. if ((bone_index < 0) || (bone_index >= htree.Num_Pivots())) {
  81. printf("Invalid bone index requested: %d\r\n",bone_index);
  82. return 0;
  83. }
  84. // Edit the HTree, moving the specified bone to the end of the tree
  85. htree.Move_Bone_To_End(bone_index);
  86. // Write the hierarchy tree out again.
  87. RawFileClass out_file(filename);
  88. if (out_file.Open(FileClass::WRITE) == false) {
  89. printf("Failed to open %s for writing. Is the file read-only?\r\n",filename);
  90. return 0;
  91. }
  92. ChunkSaveClass csave(&out_file);
  93. csave.Begin_Chunk(W3D_CHUNK_HIERARCHY);
  94. htree.Save_W3D(csave);
  95. csave.End_Chunk();
  96. out_file.Close();
  97. printf("Moved bone %d and all of its children to the end of the bone array.\r\n",bone_index);
  98. return 0;
  99. }
  100. HTreeHackClass::HTreeHackClass(void) :
  101. Pivots(NULL),
  102. PivotFixups(NULL)
  103. {
  104. }
  105. HTreeHackClass::~HTreeHackClass(void)
  106. {
  107. delete[] Pivots;
  108. delete[] PivotFixups;
  109. }
  110. void HTreeHackClass::Load_W3D(ChunkLoadClass & cload)
  111. {
  112. /*
  113. ** Read the first chunk, it should be the hierarchy header
  114. */
  115. cload.Open_Chunk();
  116. assert(cload.Cur_Chunk_ID() == W3D_CHUNK_HIERARCHY_HEADER);
  117. cload.Read(&Header,sizeof(W3dHierarchyStruct));
  118. cload.Close_Chunk();
  119. /*
  120. ** Allocate the array of pivots
  121. */
  122. if (Header.NumPivots > 0) {
  123. Pivots = new W3dPivotStruct[Header.NumPivots];
  124. PivotFixups = new W3dPivotFixupStruct[Header.NumPivots];
  125. }
  126. /*
  127. ** Now, read in all of the other chunks for this hierarchy.
  128. */
  129. while (cload.Open_Chunk()) {
  130. switch (cload.Cur_Chunk_ID()) {
  131. case W3D_CHUNK_PIVOTS:
  132. read_pivots(cload);
  133. break;
  134. case W3D_CHUNK_PIVOT_FIXUPS:
  135. read_pivot_fixups(cload);
  136. break;
  137. default:
  138. break;
  139. }
  140. cload.Close_Chunk();
  141. }
  142. }
  143. bool HTreeHackClass::read_pivots(ChunkLoadClass & cload)
  144. {
  145. for (unsigned int pidx=0; pidx < Header.NumPivots; pidx++) {
  146. if (cload.Read(&Pivots[pidx],sizeof(W3dPivotStruct)) != sizeof(W3dPivotStruct)) {
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. bool HTreeHackClass::read_pivot_fixups(ChunkLoadClass & cload)
  153. {
  154. for (unsigned int pidx=0; pidx < Header.NumPivots; pidx++) {
  155. if (cload.Read(&PivotFixups[pidx],sizeof(W3dPivotFixupStruct)) != sizeof(W3dPivotFixupStruct)) {
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. void HTreeHackClass::Save_W3D(ChunkSaveClass & csave)
  162. {
  163. csave.Begin_Chunk(W3D_CHUNK_HIERARCHY_HEADER);
  164. csave.Write(&Header,sizeof(W3dHierarchyStruct));
  165. csave.End_Chunk();
  166. csave.Begin_Chunk(W3D_CHUNK_PIVOTS);
  167. write_pivots(csave);
  168. csave.End_Chunk();
  169. csave.Begin_Chunk(W3D_CHUNK_PIVOT_FIXUPS);
  170. write_pivot_fixups(csave);
  171. csave.End_Chunk();
  172. }
  173. bool HTreeHackClass::write_pivots(ChunkSaveClass & csave)
  174. {
  175. for (unsigned int pidx=0; pidx < Header.NumPivots; pidx++) {
  176. if (csave.Write(&Pivots[pidx],sizeof(W3dPivotStruct)) != sizeof(W3dPivotStruct)) {
  177. return false;
  178. }
  179. }
  180. return true;
  181. }
  182. bool HTreeHackClass::write_pivot_fixups(ChunkSaveClass & csave)
  183. {
  184. for (unsigned int pidx=0; pidx < Header.NumPivots; pidx++) {
  185. if (csave.Write(&PivotFixups[pidx],sizeof(W3dPivotFixupStruct)) != sizeof(W3dPivotFixupStruct)) {
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191. void HTreeHackClass::Move_Bone_To_End(int bone_index)
  192. {
  193. move_single_bone_to_end(bone_index);
  194. int bad_bone = find_first_bad_bone();
  195. while (bad_bone != -1) {
  196. move_single_bone_to_end(bad_bone);
  197. bad_bone = find_first_bad_bone();
  198. }
  199. }
  200. void HTreeHackClass::move_single_bone_to_end(unsigned int bone_index)
  201. {
  202. if (bone_index >= Header.NumPivots) {
  203. return;
  204. }
  205. // move entries at 'bone_index' to the ends of their arrays
  206. W3dPivotStruct tmp_pivot = Pivots[bone_index];
  207. W3dPivotFixupStruct tmp_fixup = PivotFixups[bone_index];
  208. for (unsigned int i=bone_index; i<Header.NumPivots-1; i++) {
  209. Pivots[i] = Pivots[i+1];
  210. PivotFixups[i] = PivotFixups[i+1];
  211. }
  212. Pivots[Header.NumPivots-1] = tmp_pivot;
  213. PivotFixups[Header.NumPivots-1] = tmp_fixup;
  214. // adjust all parent indices to reflect the change
  215. for (i=bone_index; i<Header.NumPivots-1; i++) {
  216. if (Pivots[i].ParentIdx > bone_index) {
  217. Pivots[i].ParentIdx--;
  218. } else if (Pivots[i].ParentIdx == bone_index) {
  219. Pivots[i].ParentIdx = Header.NumPivots-1;
  220. }
  221. }
  222. }
  223. int HTreeHackClass::find_first_bad_bone(void)
  224. {
  225. for (unsigned int i=1; i<Header.NumPivots; i++) {
  226. if (Pivots[i].ParentIdx > i) {
  227. return i;
  228. }
  229. }
  230. return -1;
  231. }