DataInstaller.h 9.9 KB

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