DaFile.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears 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. #pragma once
  16. #include <vector>
  17. #include "common/File.h"
  18. #include "common/TypeDefine.h"
  19. /**
  20. * Represents a compiled animations file.
  21. *
  22. * Da files are bundled into battle.lgp. They contain information about a skeleton animation. They
  23. * are heavily compressed, and involve a lot of bit reading to decompile.
  24. */
  25. class DaFile{
  26. public:
  27. /**
  28. * Constructor.
  29. *
  30. * Reads the file.
  31. *
  32. * @param[in, out] file The file to read from. The file contents will not be altered, but
  33. * it's offset will be changed while reading it.
  34. */
  35. DaFile(File file);
  36. /**
  37. * Generates .a files from the da file.
  38. *
  39. * A file will be generated for each animation in the da file. See {@see VGearsAFile.h} for
  40. * more info about .a files. Files will be created in the specified path, and will be named
  41. * like the model id, followed by an underscore, followed a two-digit animation index
  42. * (starting from 0) followed by the ".a extension". For example, if path is
  43. * "/home/user/.v-gears", the model id is "ac", and the da file contains three animations,
  44. * the following files will be generated: "/home/user/.v-gears/ac_00.a",
  45. * "/home/user/.v-gears/ac_01.a" and "/home/user/.v-gears/ac_02.a".
  46. *
  47. * @param[in] model_id ID of the model the animation belongs to, usually a two letter
  48. * character. Used to generate the file name.
  49. * @param[in] path Path to the directory where the files will be saved.
  50. * @return A list of the generated files. They include the path and the extension.
  51. */
  52. std::vector<std::string> GenerateAFiles(std::string model_id, std::string path);
  53. private:
  54. /**
  55. * A bone rotation, with all it's values calculated.
  56. */
  57. struct Rotation{
  58. /**
  59. * Rotation in the X axis, in degrees.
  60. */
  61. float x;
  62. /**
  63. * Rotation in the Y axis, in degrees.
  64. */
  65. float y;
  66. /**
  67. * Rotation in the Z axis, in degrees.
  68. */
  69. float z;
  70. };
  71. /**
  72. * Processed frame data, to be written to .a files.
  73. */
  74. struct FrameData{
  75. /**
  76. * Position offset for the frame.
  77. */
  78. Rotation position_offset;
  79. /**
  80. * List of bone rotations, one per bone.
  81. */
  82. std::vector<Rotation> rotations;
  83. };
  84. /**
  85. * Processed animation data, to be written to.a files.
  86. */
  87. struct Animation{
  88. /**
  89. * Number of frames in the animation.
  90. */
  91. unsigned int frame_count;
  92. /**
  93. * Number of bones involved in the animation.
  94. */
  95. unsigned int bone_count;
  96. /**
  97. * Each frame in the animation.
  98. */
  99. std::vector<FrameData> frames;
  100. };
  101. /**
  102. * Header for animations. 12 bytes, at the start of each animation.
  103. */
  104. struct AnimationHeader {
  105. /**
  106. * Number of bones + 1 in the animation.
  107. *
  108. * It's not always reliable, the number of bones in the skeleton is the real one. If
  109. * dealing with a weapon animation, the value will always be 1.
  110. */
  111. u32 bone_count;
  112. /**
  113. * Number of frames in the animation.
  114. */
  115. u32 frame_count;
  116. /**
  117. * Size, in bytes, of the animation data.
  118. */
  119. u32 data_size;
  120. };
  121. /**
  122. * Mini header for each animation. 5 bytes, immediately after each main header.
  123. */
  124. struct AnimationMiniHeader {
  125. /**
  126. * Number of frames in the animation.
  127. *
  128. * It may not match {@see AnimationHeader.frame_count}.
  129. */
  130. u16 frame_count;
  131. /**
  132. * Size of the animation data.
  133. *
  134. * It should be 6 bytes less than {@see AnimationHeader.data_size}.
  135. */
  136. u16 data_size;
  137. /**
  138. * Key used for decoding bone data.
  139. *
  140. * It indicates how many bits are to be read to get a bone rotation. Not to be used in
  141. * the first frame, which is not compressed.
  142. *
  143. * 0: 12 bits per bone rotation, 36 bits per bone.
  144. * 2: 10 bits per bone rotation, 30 bits per bone.
  145. * 4: 8 bits per bone rotation, 30 bits per bone.
  146. */
  147. u8 key;
  148. };
  149. /**
  150. * Bone rotation.
  151. */
  152. struct BoneRotation {
  153. /**
  154. * X axis rotation, as read from the da file.
  155. */
  156. u16 s_x;
  157. /**
  158. * Y axis rotation, as read from the da file.
  159. */
  160. u16 s_y;
  161. /**
  162. * Z axis rotation, as read from the da file.
  163. */
  164. u16 s_z; // Signed short versions. 0x00
  165. /**
  166. * X axis rotation, converted to integer. Rotation between 0 and 4096.
  167. */
  168. int i_x;
  169. /**
  170. * Y axis rotation, converted to integer. Rotation between 0 and 4096.
  171. */
  172. int i_y;
  173. /**
  174. * Z axis rotation, converted to integer. Rotation between 0 and 4096.
  175. */
  176. int i_z;
  177. /**
  178. * X axis rotation, in degrees.
  179. */
  180. float f_x;
  181. /**
  182. * Y axis rotation, in degrees.
  183. */
  184. float f_y;
  185. /**
  186. * Z axis rotation, in degrees.
  187. */
  188. float f_z;
  189. };
  190. /**
  191. * A frame of an animation.
  192. *
  193. * Stores all bone rotations for a frame.
  194. */
  195. struct Frame{
  196. /**
  197. * Number of bones in the animation frame.
  198. */
  199. u32 bone_count;
  200. /**
  201. * Position offset.
  202. *
  203. * @todo Verify if it's the previous frame?
  204. */
  205. BoneRotation position_offset;
  206. /**
  207. * Bone rotations for the frame.
  208. */
  209. BoneRotation* rotations;
  210. /**
  211. * Constructor. Initializes data.
  212. */
  213. Frame(){
  214. bone_count = 0;
  215. rotations = NULL;
  216. }
  217. /**
  218. * Destructor.
  219. */
  220. ~Frame(){
  221. bone_count = 0;
  222. delete [] rotations;
  223. rotations = NULL;
  224. }
  225. /**
  226. * Sets the number of bones and initializes data arrays.
  227. *
  228. * @param[in] bones The number of bones.
  229. */
  230. void SetBones(u32 bones){
  231. // Delete the old.
  232. bone_count = 0;
  233. delete [] rotations;
  234. // Create the new.
  235. rotations = new BoneRotation[bones];
  236. if (rotations != NULL) bone_count = bones;
  237. }
  238. };
  239. /**
  240. * Reads the Da file and extracts all the data.
  241. *
  242. * @param[in, out] file The file to read from. The file contents will not be altered, but
  243. * it's offset will be changed while reading it.
  244. */
  245. void Read(File file);
  246. /**
  247. * Retrieves a value of dynamic size from the byte stream.
  248. *
  249. * The first read bit will determine the length of the read data. If it's 1, 7 more bits of
  250. * data will be read and returned. It it isn't, 16 more bits of data will be read and
  251. * returned. In total, either 17 or 8 bits of data will be read.
  252. *
  253. * @param[in] bytes The byte stream to read from.
  254. * @param[in,out] stream_bit_offset The bit at which to start reading. For each bit read,
  255. * it will be advanced by one.
  256. * @return The read value.
  257. */
  258. u16 GetValueFromStream(u8* bytes, u32 &stream_bit_offset);
  259. /**
  260. * Reads 2 bytes from the stream as a little endian value.
  261. *
  262. * @param[in] bytes The byte stream to read from.
  263. * @param[in,out] stream_bit_offset The bit at which to start reading. It will be advanced
  264. * by 16.
  265. * @return The read value.
  266. */
  267. u16 ReadU16LE(u8* bytes, u32 &stream_bit_offset);
  268. /**
  269. * Reads an arbitrary length of bits from the stream.
  270. *
  271. * @param[in] bytes The byte stream to read from.
  272. * @param[in,out] stream_bit_offset The bit at which to start reading. It will be advanced
  273. * by one per read bit.
  274. * @param[in] bits Number of bits to read.
  275. * @return The read value.
  276. */
  277. int GetBitsFromStream(u8* bytes, u32 &stream_bit_offset, int bits);
  278. /**
  279. * Decodes a delta rotation read from the stream.
  280. *
  281. * @param[in] bytes The byte stream to read from.
  282. * @param[in,out] stream_bit_offset The bit at which to start reading. It will be advanced
  283. * by one per read bit.
  284. * @param lowered_precision_bits How many bits to sift the read value.
  285. * @return The decoded value.
  286. */
  287. u16 GetCompressedDeltaFromStream(
  288. u8* bytes, u32& stream_bit_offset, int lowered_precision_bits
  289. );
  290. /**
  291. * Loads the frames in an animation.
  292. *
  293. * @param[out] frame Read frame data will be set here.
  294. * @param[in] bones The number of bones in the animation.
  295. * @param[in] bit_start Bit to start reading from animation_buffer at.
  296. * @param[in] animation_buffer The animation data.
  297. */
  298. u32 LoadFrames(Frame* frame, int bones, int bit_start, u8* animation_buffer);
  299. /**
  300. * List of animations.
  301. */
  302. std::vector<Animation> animations_;
  303. };