DataInstaller.h 8.4 KB

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