sudm.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears 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. #pragma once
  16. #include <map>
  17. #include <vector>
  18. #include <string>
  19. #include "unknown_opcode_exception.h"
  20. namespace SUDM{
  21. class IScriptFormatter{
  22. public:
  23. virtual ~IScriptFormatter() = default;
  24. /**
  25. * Adds an spawn point.
  26. *
  27. * If the spawn is new, a new record will be added to the spawn
  28. * point database. If it was already there, update the record to
  29. * add the origin.
  30. *
  31. * @param map_id[in] The target map ID.
  32. * @param entity[in] The entity that acts as the spawn point.
  33. * @param function_name[in] The spawn function name.
  34. * @param address[in] @todo Understand and document.
  35. * @param x[in] X coordinate of the field at which to spawn.
  36. * @param y[in] Y coordinate of the field at which to spawn.
  37. * @param z[in] Z coordinate of the field at which to spawn.
  38. * @param angle[in] Orientation at which to spawn.
  39. */
  40. virtual void AddSpawnPoint(
  41. unsigned int map_id, const std::string& entity, const std::string& function_name,
  42. unsigned int address, int x, int y, int triangle_id, int angle
  43. ){}
  44. /**
  45. * Retrieves the name of a spawn point.
  46. *
  47. * @param map_id[in] The target map ID.
  48. * @param entity[in] The entity that acts as the spawn point.
  49. * @param function_name[in] The spawn function name.
  50. * @param address[in] @todo Understand and document.
  51. * @return The name of the spawn point.
  52. */
  53. virtual std::string SpawnPointName(
  54. unsigned int map_id, const std::string& entity,
  55. const std::string& function_name, unsigned int address
  56. ){
  57. return MapName(map_id)
  58. + "_" + entity + "_" + function_name + "_" + std::to_string(address);
  59. }
  60. /**
  61. * Retrieves the name of a map.
  62. *
  63. * The name of a map is usually it's ID.
  64. *
  65. * @param map_id[in] The map ID.
  66. * @return The map name.
  67. */
  68. virtual std::string MapName(unsigned int map_id) { return std::to_string(map_id);}
  69. /**
  70. * Retrieves a friendly name for a variable.
  71. *
  72. * @param bank[in] Variable memory bank.
  73. * @param address[in] Variable memory address.
  74. * @return Friendly name assigned to the variable, or an empty
  75. * string if there is none.
  76. */
  77. virtual std::string VarName(unsigned int bank, unsigned int addr){return "";}
  78. /**
  79. * Retrieves a friendly name for an entity.
  80. *
  81. * @param entity_name[in] Name of the entity.
  82. * @return Friendly name assigned to the entity, or <entity_name>
  83. * if there is none.
  84. */
  85. virtual std::string EntityName(const std::string& entity_name){return entity_name;}
  86. /**
  87. * Retrieves a friendly name for an animation.
  88. *
  89. * @param animation_id[in] ID of the animation.
  90. * @return Friendly name assigned to the animation. If there is
  91. * no one, the ID in string format.
  92. */
  93. virtual std::string AnimationName(int animation_id, int id){
  94. return std::to_string(animation_id);
  95. }
  96. /**
  97. * Retrieves a friendly name for a character.
  98. *
  99. * @param char_id[in] ID of the character.
  100. * @return Friendly name assigned to the character. If there is
  101. * no one, the ID in string format.
  102. */
  103. virtual std::string CharName(int char_id){return std::to_string(char_id);}
  104. /**
  105. * Retrieves a friendly name for a function.
  106. *
  107. * @param entity_name[in] Name of the entity.
  108. * @param function_name[in] Name of the function.
  109. * @return Friendly name assigned to the entity, or
  110. * <function_name> if there is none.
  111. */
  112. virtual std::string FunctionName(
  113. const std::string& entity_name, const std::string& function_name
  114. ){return function_name;}
  115. /**
  116. * Retrieves the header comment for a function in an entity.
  117. *
  118. * @param entity_name[in] Name of the entity.
  119. * @param function_name[in] Name of the function.
  120. * @return The function comment. An empty string if the entity or
  121. * the function don't exist.
  122. */
  123. virtual std::string FunctionComment(
  124. const std::string& entity_name, const std::string& function_name
  125. ){return "";}
  126. };
  127. namespace FF7{
  128. namespace Field{
  129. /**
  130. * A line.
  131. */
  132. struct Line{
  133. /**
  134. * Name of the line entity.
  135. */
  136. std::string name;
  137. /**
  138. * First point of the line.
  139. */
  140. std::vector<float> point_a;
  141. /**
  142. * Second point of the line.
  143. */
  144. std::vector<float> point_b;
  145. };
  146. /**
  147. * An entity.
  148. */
  149. struct FieldEntity{
  150. /**
  151. * Character identifier of the entity.
  152. */
  153. uint char_id;
  154. /**
  155. * Index of the entity in the field.
  156. */
  157. uint index;
  158. /**
  159. * Name of the entity.
  160. */
  161. std::string name;
  162. };
  163. /**
  164. * The field decompiled script.
  165. */
  166. struct DecompiledScript{
  167. /**
  168. * The LUA script for the field.
  169. */
  170. std::string luaScript;
  171. /**
  172. * The field entities.
  173. *
  174. * Lines are not included.
  175. */
  176. std::vector<FieldEntity> entities;
  177. /**
  178. * Lines in the field.
  179. */
  180. std::vector<Line> lines;
  181. };
  182. /**
  183. * Retrieves the scale factor of a field.
  184. *
  185. * @param script_bytes[in] Vector of raw byte data that makes up
  186. * the script.
  187. * @return The scale fctor.
  188. */
  189. float ScaleFactor(const std::vector<unsigned char>& script_bytes);
  190. /**
  191. * Decompiles a field script.
  192. *
  193. * @param script_name[in] Name of the script to be converted,
  194. * should match file name.
  195. * @param script_bytes[in] Vector of raw byte data that makes up
  196. * the script.
  197. * @param formatter[in] Formatter used to rename variables, drop
  198. * functions...
  199. * @param text_after[in] Raw text that is added at to the end of
  200. * the decompiled output.
  201. * @param text_before[in] Raw text that is added at to the start of
  202. * the decompiled output.
  203. * @return A string with the decompiled script.
  204. * @throws InternalDecompilerError on failure.
  205. */
  206. DecompiledScript Decompile(
  207. std::string script_name, const std::vector<unsigned char>& script_bytes,
  208. IScriptFormatter& formatter, std::string text_after = "", std::string text_before = ""
  209. );
  210. }
  211. }
  212. }