sudm.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 Field{
  128. /**
  129. * A line.
  130. */
  131. struct Line{
  132. /**
  133. * Name of the line entity.
  134. */
  135. std::string name;
  136. /**
  137. * First point of the line.
  138. */
  139. std::vector<float> point_a;
  140. /**
  141. * Second point of the line.
  142. */
  143. std::vector<float> point_b;
  144. };
  145. /**
  146. * An entity.
  147. */
  148. struct FieldEntity{
  149. /**
  150. * Character identifier of the entity.
  151. */
  152. uint char_id;
  153. /**
  154. * Index of the entity in the field.
  155. */
  156. uint index;
  157. /**
  158. * Name of the entity.
  159. */
  160. std::string name;
  161. };
  162. /**
  163. * The field decompiled script.
  164. */
  165. struct DecompiledScript{
  166. /**
  167. * The LUA script for the field.
  168. */
  169. std::string luaScript;
  170. /**
  171. * The field entities.
  172. *
  173. * Lines are not included.
  174. */
  175. std::vector<FieldEntity> entities;
  176. /**
  177. * Lines in the field.
  178. */
  179. std::vector<Line> lines;
  180. };
  181. /**
  182. * Retrieves the scale factor of a field.
  183. *
  184. * @param script_bytes[in] Vector of raw byte data that makes up
  185. * the script.
  186. * @return The scale fctor.
  187. */
  188. float ScaleFactor(const std::vector<unsigned char>& script_bytes);
  189. /**
  190. * Decompiles a field script.
  191. *
  192. * @param script_name[in] Name of the script to be converted,
  193. * should match file name.
  194. * @param script_bytes[in] Vector of raw byte data that makes up
  195. * the script.
  196. * @param formatter[in] Formatter used to rename variables, drop
  197. * functions...
  198. * @param text_after[in] Raw text that is added at to the end of
  199. * the decompiled output.
  200. * @param text_before[in] Raw text that is added at to the start of
  201. * the decompiled output.
  202. * @return A string with the decompiled script.
  203. * @throws InternalDecompilerError on failure.
  204. */
  205. DecompiledScript Decompile(
  206. std::string script_name, const std::vector<unsigned char>& script_bytes,
  207. IScriptFormatter& formatter, std::string text_after = "", std::string text_before = ""
  208. );
  209. }
  210. }