ExeEffectMeshes.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 <cstdlib>
  17. #include <map>
  18. #include <ostream>
  19. #include <string>
  20. #include <vector>
  21. #include "ExeEffectCameras.h"
  22. /**
  23. * Reads the meshes of the battle effects that are in ff7.exe, like the ground of Ifrit or the
  24. * mountain of Ramuh.
  25. *
  26. * The initialization code of the effect copies a block of the data of the executable to the place it
  27. * is used from (mov ecx, size/4 ; mov esi, source ; mov edi, destination ; rep movsd), and the block
  28. * is a mesh: the size of its vertices in bytes (u32), the vertices (x, y, z and a padding, signed 16
  29. * bits each, y goes down), and four lists of polygons: textured triangles (16 bytes each), textured
  30. * quads (20), triangles with colours (20) and quads with colours (24). Every list begins with the
  31. * number of polygons and a word of flags (both s16). The polygons use the vertices by their position
  32. * in bytes. The blocks that fit exactly (the lists end where the copied block ends) are meshes.
  33. *
  34. * The effect moves the vertices to key shapes: arrays of vertices of the same size, one after the
  35. * other, in a table of pointers somewhere in the data. The 16 words that follow the table are
  36. * kept too, because some effects have the durations of their moves there.
  37. *
  38. * Found by reading the functions 0x594022 (Ifrit) and 0x598f74 (Ramuh) of the 1998 executable.
  39. */
  40. namespace ExeEffectMeshes{
  41. /**
  42. * A mesh of the executable.
  43. */
  44. struct Mesh{
  45. std::vector<int16_t> vertices; ///< x, y, z of every vertex.
  46. std::vector<std::vector<int16_t>> keys; ///< x, y, z of the vertices of every key shape.
  47. std::vector<int16_t> tail; ///< 16 words after the table of key shapes.
  48. std::vector<int16_t> triangles; ///< 10 numbers each: 3 vertices, 3 pairs of UV, grey level.
  49. std::vector<int16_t> gouraud; ///< 12 numbers each: 3 vertices, 3 colours (r, g, b).
  50. size_t shards = 0; ///< Shard meshes: how many of the triangles are the ones of the first list.
  51. };
  52. /**
  53. * Reads a block as a mesh.
  54. *
  55. * @return False if it doesn't fit exactly.
  56. */
  57. inline bool ParseBlock(
  58. const ExeEffectCameras::Image& image, const uint32_t address, const uint32_t bytes, Mesh& mesh
  59. ){
  60. using ExeEffectCameras::Read;
  61. const std::vector<uint8_t>& data = image.data;
  62. const int64_t start = ExeEffectCameras::FileOffset(image.sections, address);
  63. if (start < 0 || static_cast<size_t>(start) + bytes > data.size()) return false;
  64. const uint32_t vertex_bytes = Read<uint32_t>(data, start);
  65. if (vertex_bytes == 0 || vertex_bytes % 8 != 0 || vertex_bytes + 4 > bytes) return false;
  66. mesh = Mesh();
  67. for (uint32_t i = 0; i < vertex_bytes / 8; ++ i){
  68. for (int c = 0; c < 3; ++ c) mesh.vertices.push_back(Read<int16_t>(data, start + 4 + 8 * i + 2 * c));
  69. }
  70. size_t position = start + 4 + vertex_bytes;
  71. const size_t end = start + bytes;
  72. static const int strides[4] = {16, 20, 20, 24};
  73. for (int list = 0; list < 4; ++ list){
  74. if (position + 4 > end) return false;
  75. const int16_t count = Read<int16_t>(data, position);
  76. position += 4;
  77. if (count < 0 || position + static_cast<size_t>(count) * strides[list] > end) return false;
  78. for (int i = 0; i < count; ++ i, position += strides[list]){
  79. // The vertices are positions in bytes of the array, of 8 bytes each.
  80. int vertices[4];
  81. const int number = (list == 0 || list == 2) ? 3 : 4;
  82. for (int v = 0; v < number; ++ v){
  83. vertices[v] = Read<int16_t>(data, position + 2 * v);
  84. if (vertices[v] < 0 || vertices[v] % 8 != 0 || vertices[v] / 8 >= static_cast<int>(vertex_bytes / 8)) return false;
  85. vertices[v] /= 8;
  86. }
  87. if (list == 0){
  88. // 3 vertices, grey at +6, UV of the vertices at +8, +12 and +14.
  89. mesh.triangles.insert(mesh.triangles.end(), {
  90. static_cast<int16_t>(vertices[0]), static_cast<int16_t>(vertices[1]), static_cast<int16_t>(vertices[2]),
  91. data[position + 8], data[position + 9], data[position + 12], data[position + 13],
  92. data[position + 14], data[position + 15], data[position + 6]
  93. });
  94. }
  95. else if (list == 1){
  96. // 4 vertices, UV at +8, +12, +14 and +16, grey at +18. Two triangles.
  97. const int16_t uv[4][2] = {
  98. {data[position + 8], data[position + 9]}, {data[position + 12], data[position + 13]},
  99. {data[position + 14], data[position + 15]}, {data[position + 16], data[position + 17]}
  100. };
  101. const int order[2][3] = {{0, 1, 2}, {1, 3, 2}};
  102. for (int t = 0; t < 2; ++ t){
  103. mesh.triangles.push_back(static_cast<int16_t>(vertices[order[t][0]]));
  104. mesh.triangles.push_back(static_cast<int16_t>(vertices[order[t][1]]));
  105. mesh.triangles.push_back(static_cast<int16_t>(vertices[order[t][2]]));
  106. for (int v = 0; v < 3; ++ v){
  107. mesh.triangles.push_back(uv[order[t][v]][0]);
  108. mesh.triangles.push_back(uv[order[t][v]][1]);
  109. }
  110. mesh.triangles.push_back(data[position + 18]);
  111. }
  112. }
  113. else{
  114. // Colours (r, g, b and a code) at +8, +12, +16 (and +20 for quads).
  115. const int offsets[4] = {8, 12, 16, 20};
  116. auto colour = [&](const int v){
  117. for (int c = 0; c < 3; ++ c) mesh.gouraud.push_back(data[position + offsets[v] + c]);
  118. };
  119. const int order[2][3] = {{0, 1, 2}, {1, 3, 2}};
  120. const int triangles = list == 2 ? 1 : 2;
  121. for (int t = 0; t < triangles; ++ t){
  122. for (int v = 0; v < 3; ++ v) mesh.gouraud.push_back(static_cast<int16_t>(vertices[order[t][v]]));
  123. for (int v = 0; v < 3; ++ v) colour(order[t][v]);
  124. }
  125. }
  126. }
  127. }
  128. // The lists have to end where the block ends.
  129. return position == end;
  130. }
  131. /**
  132. * Reads a block of the shards of Shiva's ice (the ones the ice breaks into, at 0x8aa590 and the
  133. * next ones of the 1998 executable): the size of the vertices, the vertices, a list of
  134. * triangles with texture of 20 bytes (flags 172) and a list of quads with texture of 24 bytes
  135. * (flags 0). Triangles: 3 vertices (positions in bytes), the colour word at +8 (r, g, b and a
  136. * code), the UV of the vertices at +12, +16 and +18. Quads: 4 vertices, the colour word at +8,
  137. * the UV at +12, +16, +18 and +20. The effect breaks only the triangles (function 0x591455).
  138. *
  139. * @param size Size in bytes of the block, if it is one.
  140. * @return False if it isn't a block of this kind.
  141. */
  142. inline bool ParseShardBlock(
  143. const ExeEffectCameras::Image& image, const uint32_t address, Mesh& mesh, uint32_t& size
  144. ){
  145. using ExeEffectCameras::Read;
  146. const std::vector<uint8_t>& data = image.data;
  147. const int64_t start = ExeEffectCameras::FileOffset(image.sections, address);
  148. if (start < 0 || static_cast<size_t>(start) + 16 > data.size()) return false;
  149. const uint32_t vertex_bytes = Read<uint32_t>(data, start);
  150. if (vertex_bytes == 0 || vertex_bytes % 8 != 0 || vertex_bytes < 64 || vertex_bytes > 64000) return false;
  151. const uint32_t vertices = vertex_bytes / 8;
  152. size_t position = static_cast<size_t>(start) + 4 + vertex_bytes;
  153. if (position + 4 > data.size()) return false;
  154. const int16_t triangles = Read<int16_t>(data, position);
  155. if (triangles < 8 || triangles > 1500 || Read<int16_t>(data, position + 2) != 172) return false;
  156. position += 4;
  157. if (position + static_cast<size_t>(triangles) * 20 + 4 > data.size()) return false;
  158. mesh = Mesh();
  159. for (uint32_t i = 0; i < vertices; ++ i){
  160. for (int c = 0; c < 3; ++ c) mesh.vertices.push_back(Read<int16_t>(data, start + 4 + 8 * i + 2 * c));
  161. }
  162. auto vertex = [&](const size_t at, const int number, int* out){
  163. for (int v = 0; v < number; ++ v){
  164. const int value = Read<int16_t>(data, at + 2 * v);
  165. if (value < 0 || value % 8 != 0 || value / 8 >= static_cast<int>(vertices)) return false;
  166. out[v] = value / 8;
  167. }
  168. return true;
  169. };
  170. for (int i = 0; i < triangles; ++ i, position += 20){
  171. int v[3];
  172. if (!vertex(position, 3, v)) return false;
  173. mesh.triangles.insert(mesh.triangles.end(), {
  174. static_cast<int16_t>(v[0]), static_cast<int16_t>(v[1]), static_cast<int16_t>(v[2]),
  175. data[position + 12], data[position + 13], data[position + 16], data[position + 17],
  176. data[position + 18], data[position + 19], data[position + 8]
  177. });
  178. }
  179. mesh.shards = triangles;
  180. const int16_t quads = Read<int16_t>(data, position);
  181. if (quads < 0 || quads > 400 || Read<int16_t>(data, position + 2) != 0) return false;
  182. position += 4;
  183. if (position + static_cast<size_t>(quads) * 24 > data.size()) return false;
  184. for (int i = 0; i < quads; ++ i, position += 24){
  185. int v[4];
  186. if (!vertex(position, 4, v)) return false;
  187. const int16_t uv[4][2] = {
  188. {data[position + 12], data[position + 13]}, {data[position + 16], data[position + 17]},
  189. {data[position + 18], data[position + 19]}, {data[position + 20], data[position + 21]}
  190. };
  191. const int order[2][3] = {{0, 1, 2}, {1, 3, 2}};
  192. for (int t = 0; t < 2; ++ t){
  193. for (int k = 0; k < 3; ++ k) mesh.triangles.push_back(static_cast<int16_t>(v[order[t][k]]));
  194. for (int k = 0; k < 3; ++ k){
  195. mesh.triangles.push_back(uv[order[t][k]][0]);
  196. mesh.triangles.push_back(uv[order[t][k]][1]);
  197. }
  198. mesh.triangles.push_back(data[position + 8]);
  199. }
  200. }
  201. size = static_cast<uint32_t>(position - start);
  202. return true;
  203. }
  204. /**
  205. * Gets the size of a block of a mesh that starts at an address, walking its lists of polygons.
  206. *
  207. * @return 0 if it isn't a mesh (the sizes or the vertices of the polygons are not valid).
  208. */
  209. inline uint32_t BlockSize(const ExeEffectCameras::Image& image, const uint32_t address){
  210. using ExeEffectCameras::Read;
  211. const std::vector<uint8_t>& data = image.data;
  212. const int64_t start = ExeEffectCameras::FileOffset(image.sections, address);
  213. if (start < 0 || static_cast<size_t>(start) + 8 > data.size()) return 0;
  214. const uint32_t vertex_bytes = Read<uint32_t>(data, start);
  215. if (vertex_bytes == 0 || vertex_bytes % 8 != 0 || vertex_bytes > 64000) return 0;
  216. const int vertices = vertex_bytes / 8;
  217. size_t position = static_cast<size_t>(start) + 4 + vertex_bytes;
  218. static const int strides[4] = {16, 20, 20, 24};
  219. static const int numbers[4] = {3, 4, 3, 4};
  220. int total = 0;
  221. for (int list = 0; list < 4; ++ list){
  222. if (position + 4 > data.size()) return 0;
  223. const int16_t count = Read<int16_t>(data, position);
  224. position += 4;
  225. if (count < 0 || count > 4000 || position + static_cast<size_t>(count) * strides[list] > data.size()) return 0;
  226. for (int i = 0; i < count; ++ i){
  227. for (int v = 0; v < numbers[list]; ++ v){
  228. const int vertex = Read<int16_t>(data, position + static_cast<size_t>(i) * strides[list] + 2 * v);
  229. if (vertex < 0 || vertex % 8 != 0 || vertex / 8 >= vertices) return 0;
  230. }
  231. }
  232. position += static_cast<size_t>(count) * strides[list];
  233. total += count;
  234. }
  235. // Small blocks are too easy to find by chance.
  236. if (vertices < 8 || total < 8) return 0;
  237. return static_cast<uint32_t>(position - start);
  238. }
  239. /**
  240. * Gets the data of an image, to know if it looks like the vertices of a mesh.
  241. */
  242. inline bool LooksLike(
  243. const ExeEffectCameras::Image& image, const Mesh& mesh, const uint32_t address
  244. ){
  245. using ExeEffectCameras::Read;
  246. const int64_t start = ExeEffectCameras::FileOffset(image.sections, address);
  247. const size_t count = mesh.vertices.size() / 3;
  248. if (start < 0 || static_cast<size_t>(start) + count * 8 > image.data.size()) return false;
  249. size_t matching = 0;
  250. for (size_t i = 0; i < count; ++ i){
  251. const int x = Read<int16_t>(image.data, start + 8 * i);
  252. const int z = Read<int16_t>(image.data, start + 8 * i + 4);
  253. const int pad = Read<int16_t>(image.data, start + 8 * i + 6);
  254. if (pad == 0 && std::abs(x - mesh.vertices[3 * i]) < 300 && std::abs(z - mesh.vertices[3 * i + 2]) < 500) ++ matching;
  255. }
  256. return matching * 2 >= count;
  257. }
  258. /**
  259. * Extracts the meshes of the effects.
  260. *
  261. * @param[in] path Path to ff7.exe.
  262. * @param[out] meshes The meshes, by the address of their block in the executable.
  263. * @return False if the file couldn't be read or it isn't a 32 bit executable.
  264. */
  265. inline bool Extract(const std::string& path, std::map<uint32_t, Mesh>& meshes){
  266. using ExeEffectCameras::Read;
  267. meshes.clear();
  268. ExeEffectCameras::Image image;
  269. if (!ExeEffectCameras::LoadImage(path, image)) return false;
  270. const std::vector<uint8_t>& data = image.data;
  271. const ExeEffectCameras::Section& code = image.code;
  272. // mov ecx, N ; mov esi, S ; mov edi, D ; rep movsd
  273. std::vector<uint32_t> copies; // The places the blocks are copied to: working copies, not meshes.
  274. for (size_t i = 0; i + 17 <= code.file_size; ++ i){
  275. const size_t o = code.file_offset + i;
  276. if (data[o] != 0xb9 || data[o + 5] != 0xbe || data[o + 10] != 0xbf || data[o + 15] != 0xf3 || data[o + 16] != 0xa5) continue;
  277. const uint32_t count = Read<uint32_t>(data, o + 1);
  278. const uint32_t source = Read<uint32_t>(data, o + 6);
  279. copies.push_back(Read<uint32_t>(data, o + 11));
  280. Mesh mesh;
  281. if (count < 64 || !ParseBlock(image, source, count * 4, mesh) || mesh.vertices.empty()) continue;
  282. // The table of the key shapes, in a section of data that isn't code.
  283. const size_t vertices = mesh.vertices.size() / 3;
  284. const uint32_t vertex_bytes = static_cast<uint32_t>(vertices * 8);
  285. bool found = false;
  286. for (const ExeEffectCameras::Section& section : image.sections){
  287. if (section.code || found) continue;
  288. for (uint32_t k = 0; k + 8 <= section.file_size && !found; k += 4){
  289. const size_t entry = section.file_offset + k;
  290. const uint32_t first = Read<uint32_t>(data, entry);
  291. if (first == source || !LooksLike(image, mesh, first)) continue;
  292. // The shapes are one after the other.
  293. std::vector<uint32_t> table = {first};
  294. while (entry + 4 * (table.size() + 1) <= data.size()){
  295. const uint32_t next = Read<uint32_t>(data, entry + 4 * table.size());
  296. // Only the first one has to be like the mesh: some shapes aren't used.
  297. if (next != table.back() + vertex_bytes) break;
  298. const int64_t at_next = ExeEffectCameras::FileOffset(image.sections, next);
  299. if (at_next < 0 || static_cast<size_t>(at_next) + vertex_bytes > data.size()) break;
  300. table.push_back(next);
  301. }
  302. if (table.size() < 2) continue;
  303. // The first one can be followed by shapes that aren't in the table: the table is
  304. // what is a pointer at a position of the section.
  305. found = true;
  306. for (const uint32_t pointer : table){
  307. const int64_t at = ExeEffectCameras::FileOffset(image.sections, pointer);
  308. std::vector<int16_t> shape;
  309. for (size_t v = 0; v < vertices; ++ v){
  310. for (int c = 0; c < 3; ++ c) shape.push_back(Read<int16_t>(data, at + 8 * v + 2 * c));
  311. }
  312. mesh.keys.push_back(shape);
  313. }
  314. for (int w = 0; w < 16; ++ w) mesh.tail.push_back(Read<int16_t>(data, entry + 4 * table.size() + 2 * w));
  315. }
  316. }
  317. if (found) meshes[source] = mesh;
  318. }
  319. // The meshes that don't change, that aren't copied by the code: blocks of the data that fit
  320. // exactly. The working copies of the others have the same polygons as their template.
  321. for (const ExeEffectCameras::Section& section : image.sections){
  322. if (section.code) continue;
  323. for (uint32_t k = 0; k + 16 <= section.file_size;){
  324. const uint32_t address = section.address + k;
  325. const uint32_t size = BlockSize(image, address);
  326. if (size == 0){
  327. k += 4;
  328. continue;
  329. }
  330. bool is_copy = false;
  331. for (const uint32_t copy : copies) is_copy = is_copy || copy == address;
  332. Mesh mesh;
  333. if (!is_copy && meshes.count(address) == 0 && ParseBlock(image, address, size, mesh)) meshes[address] = mesh;
  334. k += size;
  335. }
  336. }
  337. // The shards of the ice of Shiva.
  338. for (const ExeEffectCameras::Section& section : image.sections){
  339. if (section.code) continue;
  340. for (uint32_t k = 0; k + 16 <= section.file_size; k += 4){
  341. const uint32_t address = section.address + k;
  342. if (meshes.count(address) != 0) continue;
  343. Mesh mesh;
  344. uint32_t size = 0;
  345. if (ParseShardBlock(image, address, mesh, size)) meshes[address] = mesh;
  346. }
  347. }
  348. return true;
  349. }
  350. /**
  351. * Writes the meshes as Lua tables: Game.EffectMeshes["0x8af9b0"] = {...}.
  352. */
  353. inline void WriteLua(std::ostream& out, const std::map<uint32_t, Mesh>& meshes){
  354. auto list = [&](const std::vector<int16_t>& values, const size_t per_line){
  355. for (size_t i = 0; i < values.size(); ++ i){
  356. if (i % per_line == 0) out << "\n ";
  357. out << values[i] << ", ";
  358. }
  359. };
  360. out << "-- Meshes of the battle effects that are in the data of ff7.exe, with the vertices of the key shapes\n"
  361. << "-- they change through. Game.EffectMeshes[address] has the vertices of the first shape (x, y, z, in\n"
  362. << "-- the units of the effects, y goes down), the key shapes, the 16 words that follow the table of\n"
  363. << "-- shapes (some effects have the durations of the moves there), the triangles with texture (3 vertex\n"
  364. << "-- numbers, the UV of each vertex in bytes of the texture page and the grey level, 128 is 1.0) and the\n"
  365. << "-- triangles with colours (3 vertex numbers and the r, g, b of each vertex).\n"
  366. << "Game.EffectMeshes = Game.EffectMeshes or {}\n";
  367. for (const auto& item : meshes){
  368. const Mesh& m = item.second;
  369. out << "Game.EffectMeshes[\"0x" << std::hex << item.first << std::dec << "\"] = {\n vertices = {";
  370. list(m.vertices, 24);
  371. out << "\n },\n keys = {\n";
  372. for (const std::vector<int16_t>& key : m.keys){
  373. out << " {";
  374. list(key, 24);
  375. out << "\n },\n";
  376. }
  377. out << " },\n tail = {";
  378. list(m.tail, 16);
  379. out << "\n },\n triangles = {";
  380. list(m.triangles, 20);
  381. out << "\n },\n gouraud = {";
  382. list(m.gouraud, 24);
  383. out << "\n },\n";
  384. if (m.shards > 0) out << " shards = " << m.shards << ",\n";
  385. out << "}\n";
  386. }
  387. }
  388. }