DataInstaller.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <string>
  17. #include <vector>
  18. #include <iostream>
  19. #include "common/VGearsApplication.h"
  20. #include "common/FinalFantasy7/FF7NameLookup.h"
  21. #include "data/VGearsTriggersFile.h"
  22. #include "data/VGearsFLevelFile.h"
  23. #include "FieldTextWriter.h"
  24. #include "ModelsAndAnimationsDb.h"
  25. #include "ScopedLgp.h"
  26. #include "KernelFile.h"
  27. #include "SpawnPointDb.h"
  28. // TODO: Separate classes in files.
  29. // TODO: Move implementations to cpp file.
  30. typedef std::set<std::string> MapCollection;
  31. typedef std::map<u16, SpawnPointDb> FieldSpawnPointsMap;
  32. typedef std::map<u16, float> FieldScaleFactorMap;
  33. /**
  34. * The data installer.
  35. */
  36. class DataInstaller{
  37. public:
  38. /**
  39. * The scale factor for line point coordinates.
  40. *
  41. * When a LINE opcode is found in the game scripts, the line points X and Y coordinates
  42. * must be scaled down by this factor.
  43. */
  44. static float LINE_SCALE_FACTOR;
  45. /**
  46. * Installer constructor
  47. *
  48. * @param[in] input_dir Path to the directory containing the original
  49. * data to parse.
  50. * @param[in] output_dir Path to the directory to write generated data
  51. * to.
  52. * @param[in] write_output_line Pointer to function to write output.
  53. */
  54. DataInstaller(
  55. const std::string input_dir, const std::string output_dir,
  56. std::function<void(std::string)> write_output_line
  57. );
  58. /**
  59. * Installer destructor.
  60. */
  61. ~DataInstaller();
  62. /**
  63. * Handle the installation progress.
  64. *
  65. * @return Installation progress [0-100].
  66. */
  67. int Progress();
  68. private:
  69. /**
  70. * Calculates the installation progress.
  71. *
  72. * @return Installation progress [0-100]
  73. */
  74. int CalcProgress();
  75. /**
  76. * Creates a directory in the outputh path.
  77. *
  78. * @param[in] dir Path of the directory to create, relative to the
  79. * output path..
  80. * @throws std::runtime_error If the directory can't be created.
  81. */
  82. void CreateDir(const std::string& dir);
  83. /**
  84. * Initializer for {@see CollectionFieldSpawnAndScaleFactors}.
  85. *
  86. * Installation step 1.
  87. */
  88. void InitCollectSpawnAndScaleFactors();
  89. /**
  90. * Reads spawn points and scale factors from flevel files.
  91. *
  92. * Installation step 2.
  93. */
  94. void CollectionFieldSpawnAndScaleFactors();
  95. /**
  96. * Converts FFVII PC fields to V-Gears format.
  97. */
  98. void ConvertFieldsIteration();
  99. /**
  100. * Initializer for {@see WriteMapsXmlIteration}.
  101. *
  102. * Installation step 3.
  103. */
  104. void WriteMapsXmlBegin();
  105. /**
  106. * Saves the game maps to XML files.
  107. *
  108. * Installation step 4.
  109. */
  110. void WriteMapsXmlIteration();
  111. /**
  112. * Cleans up after {@see WriteMapsXmlIteration}.
  113. *
  114. * Installation step 5.
  115. */
  116. void EndWriteMapsXml();
  117. /**
  118. * Initializer for {@see ConvertFieldModelsIteration}.
  119. *
  120. * Installation step 6.
  121. */
  122. void ConvertFieldModelsBegin();
  123. /**
  124. * Converts the field models to V-Gears format.
  125. *
  126. * Installation step 7 and final.
  127. */
  128. void ConvertFieldModelsIteration();
  129. /**
  130. * Installation steps.
  131. */
  132. enum InstallationSteps{
  133. /**
  134. * Installation not started.
  135. */
  136. IDLE = 0,
  137. /**
  138. * Initializer for {@see SPAWN_POINTS_AND_SCALE_FACTORS}.
  139. */
  140. SPAWN_POINTS_AND_SCALE_FACTORS_INIT = 1,
  141. /**
  142. * Step that collects spawn points and sclae factors.
  143. */
  144. SPAWN_POINTS_AND_SCALE_FACTORS = 2,
  145. /**
  146. * Step that convets fields to V-Gears format.
  147. */
  148. CONVERT_FIELDS = 3,
  149. /**
  150. * Initializer for {@see WRITE_MAPS}.
  151. */
  152. WRITE_MAPS_INIT = 4,
  153. /**
  154. * Step that writes XML maps.
  155. */
  156. WRITE_MAPS = 5,
  157. /**
  158. * Clean up after {@see WRITE_MAPS}.
  159. */
  160. WRITE_MAPS_CLEAN = 6,
  161. /**
  162. * Initializer for {@see CONVERT_FIELD_MODELS}.
  163. */
  164. CONVERT_FIELD_MODELS_INIT = 7,
  165. /**
  166. * Step that converts field models.
  167. */
  168. CONVERT_FIELD_MODELS = 8,
  169. /**
  170. * Number of installation steps.
  171. */
  172. STATE_COUNT,
  173. /**
  174. * Installation finished.
  175. */
  176. DONE = 100,
  177. };
  178. /**
  179. * Current installation status.
  180. */
  181. InstallationSteps installation_state_ = IDLE;
  182. /**
  183. * The path to the directory from which to read the PC game data.
  184. */
  185. std::string input_dir_;
  186. /**
  187. * The path to the directory where to sve the V-Gears data.
  188. */
  189. std::string output_dir_;
  190. /**
  191. * The installer application.
  192. *
  193. * It's a singleton so it can't be recreated. It will crash the second
  194. * time round.
  195. */
  196. VGears::Application application_;
  197. /**
  198. * LGP archive with field data.
  199. */
  200. std::unique_ptr<ScopedLgp> fields_lgp_;
  201. /**
  202. * BIN/GZ Kernel files.
  203. */
  204. std::unique_ptr<KernelFile> kernel_bin_;
  205. /**
  206. * LGP archive with texture data.
  207. */
  208. std::unique_ptr<ScopedLgp> textures_lgp_;
  209. /**
  210. * LGP archive with field model data.
  211. */
  212. std::unique_ptr<ScopedLgp> field_models_lgp_;
  213. /**
  214. * List of flevel files.
  215. */
  216. Ogre::StringVectorPtr flevel_file_list_;
  217. /**
  218. * The list of maps.
  219. */
  220. std::vector<std::string> map_list_;
  221. /**
  222. * Map of the collected spawn points.
  223. */
  224. FieldSpawnPointsMap spawn_points_;
  225. /**
  226. * Map of th collected scale factors.
  227. */
  228. FieldScaleFactorMap scale_factors_;
  229. /**
  230. * ModelsAndAnimationsUsedByConvertedFields
  231. */
  232. ModelsAndAnimationsDb used_models_and_anims_;
  233. /**
  234. * List of converted maps.
  235. */
  236. MapCollection converted_map_list_;
  237. /**
  238. * Iterator for {@see converted_map_list}.
  239. */
  240. MapCollection::iterator converted_map_list_iterator_;
  241. /**
  242. * Iterator counter.
  243. */
  244. size_t iterator_counter_;
  245. /**
  246. * Helper variable to indicate internal progress of installation steps.
  247. */
  248. size_t conversion_step_;
  249. /**
  250. * Helper variable to indicate internal progress of installation steps.
  251. */
  252. size_t progress_step_num_elements_;
  253. /**
  254. * Field currently being processed.
  255. */
  256. VGears::FLevelFilePtr field_;
  257. /**
  258. * List of model files.
  259. */
  260. Ogre::StringVectorPtr field_model_file_list_;
  261. /**
  262. * An XML document.
  263. *
  264. * Internally used during some installation steps.
  265. */
  266. std::unique_ptr<TiXmlDocument> doc_;
  267. /**
  268. * An XML element.
  269. *
  270. * Internally used during some installation steps.
  271. */
  272. std::unique_ptr<TiXmlElement> element_;
  273. /**
  274. * Iterator for {@see used_models_and_anims_}.
  275. */
  276. ModelAnimationMap::iterator model_animation_map_iterator_;
  277. /**
  278. * Function used to print text.
  279. */
  280. std::function<void(std::string)> write_output_line;
  281. /**
  282. * Field text writer.
  283. */
  284. FieldTextWriter field_text_writer_;
  285. /**
  286. * Written materials.
  287. */
  288. std::vector<std::string> materials_;
  289. };