FieldEngine.h 12 KB

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