DataInstaller.h 9.6 KB

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