FieldEngine.h 12 KB

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