FieldEngine.h 12 KB

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