FieldEngine.h 14 KB

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