DataInstaller.h 9.0 KB

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