DataInstaller.h 9.8 KB

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