FieldEngine.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * V-Gears
  3. * Copyright (C) 2022 V-Gears Team
  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. #pragma once
  19. #include <string>
  20. #include <vector>
  21. #include "FieldScriptFormatter.h"
  22. #include "decompiler/Engine.h"
  23. #include "decompiler/field/FieldDecompiler.h"
  24. namespace FF7{
  25. /**
  26. * Represents the FF7 Field engine.
  27. */
  28. class FieldEngine : public Engine{
  29. public:
  30. /**
  31. * Represents an entity.
  32. *
  33. * An entity can be almost anything in a field map: the playable
  34. * character, an NPC, an item, a line...
  35. */
  36. class Entity{
  37. public:
  38. /**
  39. * Entity constructor.
  40. *
  41. * It doesn't initialize any of the fields.
  42. */
  43. Entity() = default;
  44. /**
  45. * Entity constructor.
  46. *
  47. * Instantiates an entity with a name.
  48. *
  49. * @param name[in] Entity name.
  50. */
  51. Entity(const std::string& name, size_t index);
  52. /**
  53. * Retrieves the entity name.
  54. *
  55. * @return The entity name.
  56. */
  57. std::string GetName() const;
  58. /**
  59. * Retrieves the entity index.
  60. *
  61. * The index is the one at which appears in the original
  62. * game script.
  63. *
  64. * @return The entity index.
  65. */
  66. size_t GetIndex() const;
  67. /**
  68. * Retrieves a function.
  69. *
  70. * Retrieves the name of a function from it's index.
  71. *
  72. * @param index[in] Function index.
  73. * @return Function name.
  74. * @throws DecompilerException if there is no function
  75. * with the specified index.
  76. * @todo What is a function here? An Opcode?
  77. */
  78. std::string FunctionByIndex(size_t index) const;
  79. /**
  80. * Adds a function to the engine.
  81. *
  82. * Must be added by name and index.
  83. *
  84. * @param name[in] Function name. If the entity is a line,
  85. * the name will be overridden.
  86. * @param index[in] Function index.
  87. * @todo What is a function here? An Opcode?
  88. */
  89. void AddFunction(const std::string& name, size_t index);
  90. /**
  91. * Marks the entity as a line.
  92. *
  93. * @param line[in] True to mark the entity as a line,
  94. * false to unmark it.
  95. * @param point_a[in] First point of the line. Can be null
  96. * if line is false.
  97. * @param point_b[in] Second point of the line. Can be
  98. * null if line is false.
  99. */
  100. void MarkAsLine(
  101. bool line, std::vector<float> point_a, std::vector<float> point_b
  102. );
  103. /**
  104. * Checks if the entity is a line.
  105. *
  106. * Note that an entity is not considered to be a line
  107. * until a function has been found containing the opcode
  108. * LINE and {@see MarkAsLine} has been called.
  109. *
  110. * @return true if the entity is a line.
  111. */
  112. bool IsLine();
  113. /**
  114. * Retrieves the first point of the line entity.
  115. *
  116. * If the entity is not a line, the behavior is undefined.
  117. *
  118. * @return The first point of the line entity.
  119. */
  120. std::vector<float> GetLinePointA();
  121. /**
  122. * Retrieves the second point of the line entity.
  123. *
  124. * If the entity is not a line, the behavior is undefined.
  125. *
  126. * @return The second point of the line entity.
  127. */
  128. std::vector<float> GetLinePointB();
  129. private:
  130. /**
  131. * Entity name.
  132. */
  133. std::string name_;
  134. /**
  135. * Entity index.
  136. */
  137. size_t index_;
  138. /**
  139. * Entity function (script) list.
  140. */
  141. std::map< size_t, std::string > functions_;
  142. /**
  143. * Indicates if the entity is a line.
  144. */
  145. bool is_line_;
  146. /**
  147. * The first point of a line entity.
  148. *
  149. * If the entity is not a line, it may not be initializer.
  150. */
  151. std::vector<float> point_a_;
  152. /**
  153. * The second point of a line entity.
  154. *
  155. * If the entity is not a line, it may not be initializer.
  156. */
  157. std::vector<float> point_b_;
  158. };
  159. /**
  160. * Constructor.
  161. *
  162. * @param formatter[in] The formatter to be used by the engine.
  163. * @param script_name[in] The script name.
  164. */
  165. FieldEngine(FieldScriptFormatter& formatter, std::string script_name);
  166. /**
  167. * Copy constructor, disabled.
  168. *
  169. * @param engine[in] The engine to copy.
  170. */
  171. FieldEngine(const FieldEngine& engine) = delete;
  172. /**
  173. * Copy constructor, disabled.
  174. *
  175. * @param engine[in] The engine to copy.
  176. */
  177. FieldEngine& operator = (const FieldEngine& engine) = delete;
  178. /**
  179. * Retrieves the disasembler.
  180. *
  181. * @param insts[in] List of instructions.
  182. * @param raw_script_data[in] Script data, raw format.
  183. * @return Pointer to the disasembler.
  184. */
  185. virtual std::unique_ptr<Disassembler> GetDisassembler(
  186. InstVec &insts, const std::vector<unsigned char>& raw_script_data
  187. ) override;
  188. /**
  189. * Retrieves the dissasembler.
  190. *
  191. * @param insts[in] List of instructions.
  192. * @return Pointer to the dissasembler.
  193. */
  194. virtual std::unique_ptr<Disassembler> GetDisassembler(InstVec &insts) override;
  195. /**
  196. * Retrieves the code generator.
  197. *
  198. * @param insts[in] List of instructions.
  199. * @param output[in] Pointer to the output (file, stream...).
  200. * @return Pointer to the generator.
  201. */
  202. virtual std::unique_ptr<CodeGenerator> GetCodeGenerator(
  203. const InstVec& insts, std::ostream &output
  204. ) override;
  205. /**
  206. * Post-processing actions to apply to the scripts.
  207. *
  208. * It actually does nothing. CFG stands for control flow group.
  209. *
  210. * @param insts[in] Instruction list.
  211. * @param graph[in] Code graph.
  212. */
  213. virtual void PostCFG(InstVec &insts, Graph graph) override;
  214. /**
  215. * Indicates if instructions are purely grouped.
  216. *
  217. * @return True if instructions are purely grouped. Always false.
  218. * @todo What is pure grouping?
  219. */
  220. virtual bool UsePureGrouping() const override;
  221. /**
  222. * Retrieves all entities in the map.
  223. *
  224. * @return A map of entities, with the name and index.
  225. */
  226. std::map<std::string, int> GetEntities() const;
  227. /**
  228. * Retrieves all non-line entities in the map.
  229. *
  230. * @return A list of non-line entities.
  231. */
  232. std::vector<FieldDecompiler::FieldEntity> GetEntityList() const;
  233. /**
  234. * Retrieves all line entities in the map.
  235. *
  236. * @param A list of line entities.
  237. */
  238. std::vector<FieldDecompiler::Line> GetLineList() const;
  239. /**
  240. * Retrieves all entities in the map.
  241. *
  242. * @return A map of entities, with the name and index.
  243. */
  244. std::map<size_t, Entity> GetEntityIndexMap() const{return entity_index_map_;}
  245. /**
  246. * Adds a function to an entity.
  247. *
  248. * @param entity_name Name of the entity.
  249. * @param entity_index Index of the entity.
  250. * @param func_name Name of the function.
  251. * @param func_index Index of the function.
  252. */
  253. void AddEntityFunction(
  254. const std::string& entity_name, size_t entity_index,
  255. const std::string& func_name, size_t func_index
  256. );
  257. /**
  258. * Marks an entity as a line.
  259. *
  260. * @param entity_index Index of the entity.
  261. * @param line[in] True to mark the entity as a line, false to
  262. * unmark it.
  263. * @param point_a[in] First point of the line. Can be null if line
  264. * is false.
  265. * @param point_b[in] Second point of the line. Can be null if
  266. * line is false.
  267. */
  268. void MarkEntityAsLine(
  269. size_t entity_index, bool line,
  270. std::vector<float> point_a, std::vector<float> point_b
  271. );
  272. /**
  273. * Checks if an entity has been marked as a line.
  274. *
  275. * @param entity_index[in] Index of the entity to check.
  276. * @return True if the entity is a line. False if it isn't, or if
  277. * there is no such entity.
  278. */
  279. bool EntityIsLine(size_t entity_index);
  280. /**
  281. * Retrieves an entity.
  282. *
  283. * @param index[in] Index of the entity to retrieve.
  284. * @throws DecompilerException if there is no entity at the
  285. * specified index.
  286. */
  287. const Entity& EntityByIndex(size_t index) const;
  288. /**
  289. * Retrieves the scale factor for the map.
  290. *
  291. * @return Map scale factor.
  292. */
  293. float GetScaleFactor() const;
  294. /**
  295. * Retrieves the script name.
  296. *
  297. * @return The script name.
  298. */
  299. const std::string& GetScriptName() const;
  300. private:
  301. /**
  302. * Removes extraneous return statements.
  303. *
  304. * Useful for scripts that only contain one one return statement.
  305. *
  306. * @param insts[in|out] List of instructions to process.
  307. * Extraneous return statements will be deleted from the
  308. * instructions.
  309. * @param graph[in] Code graph. Unused.
  310. */
  311. void RemoveExtraneousReturnStatements(InstVec& insts, Graph graph);
  312. /**
  313. * Removes trailing infinite loops.
  314. *
  315. * In FF7 some scripts ends with an infinite loop to keep it
  316. * alive. in VGears this isn't required, and can cause infinite
  317. * loops, so they can be removed.
  318. *
  319. * @param insts[in|out] List of instructions to proccess. Trailing
  320. * infinite loops will be deleted from the instructions.
  321. * @param g[in] Code graph.
  322. * @todo What is the graph used to?
  323. */
  324. void RemoveTrailingInfiniteLoops(InstVec& insts, Graph graph);
  325. /**
  326. * Tries to detect scripts with trailing infinite loops.
  327. *
  328. * In FF7 some scripts ends with an infinite loop to keep it
  329. * alive. in VGears this isn't required, and can cause infinite
  330. * loops, so they can be removed. This function marks them, so
  331. * they can be deleted with
  332. * @{see FieldEngine::RemoveTrailingInfiniteLoops}.
  333. */
  334. void MarkInfiniteLoopGroups(InstVec& insts, Graph graph);
  335. /**
  336. * The script formatter.
  337. */
  338. FieldScriptFormatter& formatter_;
  339. /**
  340. * The entity index map for the field.
  341. */
  342. std::map<size_t, Entity> entity_index_map_;
  343. /**
  344. * The map scale factor.
  345. */
  346. float scale_factor_;
  347. /**
  348. * The script name.
  349. */
  350. std::string script_name_;
  351. };
  352. }