ff7DataInstaller.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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/make_unique.h"
  20. #include "common/QGearsApplication.h"
  21. #include "common/FinalFantasy7/FF7NameLookup.h"
  22. #include "data/QGearsTriggersFile.h"
  23. #include "common/QGearsStringUtil.h"
  24. #include "data/QGearsFLevelFile.h"
  25. #include "ff7FieldTextWriter.h"
  26. // TODO: Separate classes in files.
  27. // TODO: Move implementations to cpp file.
  28. /**
  29. * Handles a scoped LGP archive.
  30. */
  31. class ScopedLgp{
  32. public:
  33. /**
  34. * Constructor.
  35. *
  36. * Don't use directly.
  37. */
  38. ScopedLgp(const ScopedLgp&) = delete;
  39. /**
  40. * Copy constructor.
  41. *
  42. * Don't use directly.
  43. */
  44. ScopedLgp& operator = (const ScopedLgp&) = delete;
  45. /**
  46. * Constructor.
  47. */
  48. ScopedLgp(
  49. Ogre::Root* root, std::string full_path,
  50. std::string type, std::string group
  51. ) : root_(root), full_path_(full_path), group_(group) {
  52. if (root_){
  53. std::cout << "[RESOURCE] " << full_path_ << ", "
  54. << type << ", " << group_ << std::endl;
  55. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  56. full_path_, type, group_
  57. );
  58. }
  59. }
  60. /**
  61. * Destructor.
  62. */
  63. ~ScopedLgp(){
  64. if (root_){
  65. Ogre::ResourceGroupManager::getSingleton()
  66. .removeResourceLocation(full_path_, group_);
  67. }
  68. }
  69. private:
  70. /**
  71. * The Ogre root system.
  72. */
  73. Ogre::Root* root_;
  74. /**
  75. * Full path to the LGP archive.
  76. */
  77. std::string full_path_;
  78. /**
  79. * Group for the resource.
  80. */
  81. std::string group_;
  82. };
  83. typedef std::set<std::string> MapCollection;
  84. /**
  85. * A database of spawn points.
  86. */
  87. class SpawnPointDb{
  88. public:
  89. /**
  90. * Field IDs.
  91. *
  92. * ID of the field the gateways records from N other number of fields
  93. * are linking to.
  94. */
  95. u16 target_field_id = 0;
  96. /**
  97. * A spawn point database record.
  98. */
  99. class Record{
  100. public:
  101. /**
  102. * Field that links to {@see SpawnPointDb::target_field_id}.
  103. */
  104. u16 field_id = 0;
  105. /**
  106. * Index of the gateway in {@see field_id}.
  107. */
  108. u32 gateway_index_or_map_jump_address = 0;
  109. /**
  110. * Gateway data.
  111. */
  112. QGears::TriggersFile::Gateway gateway;
  113. /**
  114. * Indicates if the gateway is a map jump from a script.
  115. */
  116. bool from_script = false;
  117. /**
  118. * @todo Understand and document.
  119. *
  120. * Only used from script calls.
  121. */
  122. std::string entity_name;
  123. /**
  124. * @todo Understand and document.
  125. *
  126. * Only used from script calls.
  127. */
  128. std::string script_function_name;
  129. };
  130. /**
  131. * List of gateways to this field.
  132. */
  133. std::vector<Record> gateways_to_this_field;
  134. };
  135. typedef std::map<u16, SpawnPointDb> FieldSpawnPointsMap;
  136. typedef std::map<u16, float> FieldScaleFactorMap;
  137. typedef std::map<std::string, std::set<std::string>> ModelAnimationMap;
  138. /**
  139. * Database of model animations.
  140. */
  141. class ModelsAndAnimationsDb{
  142. public:
  143. /**
  144. * Normalizes an animation name.
  145. *
  146. * Generates a normalized file name, without path, lowercase, and an
  147. * '.a' extension.
  148. *
  149. * @param name[in] Current name.
  150. * @return The normalized name.
  151. */
  152. std::string NormalizeAnimationName(const std::string& name){
  153. Ogre::String base_name;
  154. QGears::StringUtil::splitBase(name, base_name);
  155. std::transform(
  156. base_name.begin(), base_name.end(), base_name.begin(), ::tolower
  157. );
  158. return base_name + ".a";
  159. }
  160. /**
  161. * @todo Understand and document.
  162. *
  163. * @param model[in] @todo Understand and document.
  164. * @return @todo Understand and document.
  165. */
  166. std::set<std::string>& ModelAnimations(const std::string model){
  167. // HACK FIX LGP READING
  168. std::string model_lower = model;
  169. std::transform(
  170. model_lower.begin(), model_lower.end(),
  171. model_lower.begin(), ::tolower
  172. );
  173. return map[model_lower];
  174. }
  175. /**
  176. * Retrieves the metadata file name associated to a model.
  177. *
  178. * @param model_name[in] The model name.
  179. * @return Name of the metadata file.
  180. */
  181. std::string ModelMetaDataName(const std::string& model_name){
  182. // If not in meta data then just replace .hrc with .mesh.
  183. Ogre::String base_name;
  184. QGears::StringUtil::splitBase(model_name, base_name);
  185. return QGears::FF7::NameLookup::model(base_name) + ".mesh";
  186. }
  187. //private:
  188. ModelAnimationMap map;
  189. };
  190. /**
  191. * The data installer.
  192. */
  193. class FF7DataInstaller{
  194. public:
  195. /**
  196. * Installer constructor
  197. *
  198. * @param input_dir[in] Path to the directory containing the original
  199. * data to parse.
  200. * @param output_dir[in] Path to the directory to write generated data
  201. * to.
  202. * @param write_output_line Pointer to function to write output.
  203. */
  204. FF7DataInstaller(
  205. std::string input_dir, std::string output_dir,
  206. std::function<void(std::string)> write_output_line
  207. );
  208. /**
  209. * Installer destructor.
  210. */
  211. ~FF7DataInstaller();
  212. /**
  213. * Handle the installation progress.
  214. *
  215. * @return Installation progress [0-100].
  216. */
  217. int Progress();
  218. private:
  219. /**
  220. * Calculates the installation progress.
  221. *
  222. * @return Installation progress [0-100]
  223. */
  224. int CalcProgress();
  225. /**
  226. * Creates a directory in the outputh path.
  227. *
  228. * @param dir[in] Path of the directory to create, relative to the
  229. * output path..
  230. * @throws std::runtime_error If the directory can't be created.
  231. */
  232. void CreateDir(const std::string& dir);
  233. /**
  234. * Initializer for {@see CollectionFieldSpawnAndScaleFactors}.
  235. *
  236. * Installation step 1.
  237. */
  238. void InitCollectSpawnAndScaleFactors();
  239. /**
  240. * Reads spawn points and scale factors from flevel files.
  241. *
  242. * Installation step 2.
  243. */
  244. void CollectionFieldSpawnAndScaleFactors();
  245. /**
  246. * Converts FFVII PC fields to V-Gears format.
  247. */
  248. void ConvertFieldsIteration();
  249. /**
  250. * Initializer for {@see WriteMapsXmlIteration}.
  251. *
  252. * Installation step 3.
  253. */
  254. void WriteMapsXmlBegin();
  255. /**
  256. * Saves the game maps to XML files.
  257. *
  258. * Installation step 4.
  259. */
  260. void WriteMapsXmlIteration();
  261. /**
  262. * Cleans up after {@see WriteMapsXmlIteration}.
  263. *
  264. * Installation step 5.
  265. */
  266. void EndWriteMapsXml();
  267. /**
  268. * Initializer for {@see ConvertFieldModelsIteration}.
  269. *
  270. * Installation step 6.
  271. */
  272. void ConvertFieldModelsBegin();
  273. /**
  274. * Converts the field models to V-Gears format.
  275. *
  276. * Installation step 7 and final.
  277. */
  278. void ConvertFieldModelsIteration();
  279. /**
  280. * Installation steps.
  281. */
  282. enum InstallationSteps{
  283. /**
  284. * Installation not started.
  285. */
  286. IDLE = 0,
  287. /**
  288. * Initializer for {@see SPAWN_POINTS_AND_SCALE_FACTORS}.
  289. */
  290. SPAWN_POINTS_AND_SCALE_FACTORS_INIT = 1,
  291. /**
  292. * Step that collects spawn points and sclae factors.
  293. */
  294. SPAWN_POINTS_AND_SCALE_FACTORS = 2,
  295. /**
  296. * Step that convets fields to V-Gears format.
  297. */
  298. CONVERT_FIELDS = 3,
  299. /**
  300. * Initializer for {@see WRITE_MAPS}.
  301. */
  302. WRITE_MAPS_INIT = 4,
  303. /**
  304. * Step that writes XML maps.
  305. */
  306. WRITE_MAPS = 5,
  307. /**
  308. * Clean up after {@see WRITE_MAPS}.
  309. */
  310. WRITE_MAPS_CLEAN = 6,
  311. /**
  312. * Initializer for {@see CONVERT_FIELD_MODELS}.
  313. */
  314. CONVERT_FIELD_MODELS_INIT = 7,
  315. /**
  316. * Step that converts field models.
  317. */
  318. CONVERT_FIELD_MODELS = 8,
  319. /**
  320. * Number of installation steps.
  321. */
  322. STATE_COUNT,
  323. /**
  324. * Installation finished.
  325. */
  326. DONE = 100,
  327. };
  328. /**
  329. * Current installation status.
  330. */
  331. InstallationSteps installation_state_ = IDLE;
  332. /**
  333. * The path to the directory from which to read the PC game data.
  334. */
  335. std::string input_dir_;
  336. /**
  337. * The path to the directory where to sve the V-Gears data.
  338. */
  339. std::string output_dir_;
  340. /**
  341. * The installer application.
  342. *
  343. * It's a singleton so it can't be recreated. It will crash the second
  344. * time round.
  345. */
  346. QGears::Application application_;
  347. /**
  348. * LGP archive with field data.
  349. */
  350. std::unique_ptr<ScopedLgp> fields_lgp_;
  351. /**
  352. * LGP archive with texture data.
  353. */
  354. std::unique_ptr<ScopedLgp> textures_lgp_;
  355. /**
  356. * LGP archive with field model data.
  357. */
  358. std::unique_ptr<ScopedLgp> field_models_lgp_;
  359. /**
  360. * List of flevel files.
  361. */
  362. Ogre::StringVectorPtr flevel_file_list_;
  363. /**
  364. * The list of maps.
  365. */
  366. std::vector<std::string> map_list_;
  367. /**
  368. * Map of the collected spawn points.
  369. */
  370. FieldSpawnPointsMap spawn_points_;
  371. /**
  372. * Map of th collected scale factors.
  373. */
  374. FieldScaleFactorMap scale_factors_;
  375. /**
  376. * ModelsAndAnimationsUsedByConvertedFields
  377. */
  378. ModelsAndAnimationsDb used_models_and_anims_;
  379. /**
  380. * List of converted maps.
  381. */
  382. MapCollection converted_map_list_;
  383. /**
  384. * Iterator for {@see converted_map_list}.
  385. */
  386. MapCollection::iterator converted_map_list_iterator_;
  387. /**
  388. * Iterator counter.
  389. */
  390. size_t iterator_counter_;
  391. /**
  392. * Helper variable to indicate internal progress of installation steps.
  393. */
  394. size_t conversion_step_;
  395. /**
  396. * Helper variable to indicate internal progress of installation steps.
  397. */
  398. size_t progress_step_num_elements_;
  399. /**
  400. * Field currently being processed.
  401. */
  402. QGears::FLevelFilePtr field_;
  403. /**
  404. * List of model files.
  405. */
  406. Ogre::StringVectorPtr field_model_file_list_;
  407. /**
  408. * An XML document.
  409. *
  410. * Internally used during some installation steps.
  411. */
  412. std::unique_ptr<TiXmlDocument> doc_;
  413. /**
  414. * An XML element.
  415. *
  416. * Internally used during some installation steps.
  417. */
  418. std::unique_ptr<TiXmlElement> element_;
  419. /**
  420. * Iterator for {@see used_models_and_anims_}.
  421. */
  422. ModelAnimationMap::iterator model_animation_map_iterator_;
  423. /**
  424. * Function used to print text.
  425. */
  426. std::function<void(std::string)> write_output_line;
  427. /**
  428. * Field text writer.
  429. */
  430. FF7FieldTextWriter field_text_writer_;
  431. /**
  432. * Written materials.
  433. */
  434. std::vector<std::string> materials_;
  435. };