DataInstaller.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 <OgreLog.h>
  20. #include "app/VGearsApplication.h"
  21. #include "installer/formats/DiskImage.h"
  22. #include "installer/extractors/FieldDataInstaller.h"
  23. #include "installer/extractors/KernelDataInstaller.h"
  24. #include "installer/extractors/MediaDataInstaller.h"
  25. #include "installer/extractors/BattleDataInstaller.h"
  26. #include "installer/extractors/WorldInstaller.h"
  27. #include "installer/extractors/ModelsAndAnimationsDb.h"
  28. /**
  29. * The data installer.
  30. */
  31. class DataInstaller{
  32. public:
  33. /**
  34. * Advanced options available in the installer.
  35. */
  36. struct AdvancedOptions{
  37. /**
  38. * Option to install battle scenes.
  39. */
  40. bool battle_data;
  41. /**
  42. * Option to install battle models.
  43. */
  44. bool battle_models;
  45. /**
  46. * Option to install kernel data.
  47. */
  48. bool kernel;
  49. /**
  50. * Option to install images.
  51. */
  52. bool images;
  53. /**
  54. * Option to install sound effects.
  55. */
  56. bool sounds;
  57. /**
  58. * Option to install music tracks.
  59. */
  60. bool music;
  61. /**
  62. * Option to install FMV videos.
  63. */
  64. bool videos;
  65. /**
  66. * Option to install field maps.
  67. */
  68. bool fields;
  69. /**
  70. * Option to install field models.
  71. */
  72. bool field_models;
  73. /**
  74. * Option to install world map data.
  75. */
  76. bool wm;
  77. /**
  78. * Option to install world map models.
  79. */
  80. bool wm_models;
  81. /**
  82. * Option to keep the original data after installation: the extracted install disk, the temporary
  83. * files and the original files of the converted sounds, music and images.
  84. */
  85. bool keep_originals;
  86. };
  87. /**
  88. * The scale factor for line point coordinates.
  89. *
  90. * When a LINE opcode is found in the game scripts, the line points X
  91. * and Y coordinates must be scaled down by this factor.
  92. */
  93. static float LINE_SCALE_FACTOR;
  94. /**
  95. * Installer constructor
  96. *
  97. * @param[in] disk_image Disk image with the original data to parse.
  98. * @param[in] output_dir Path to the directory to write generated data.
  99. * @param[in] options Advanced options for the installer.
  100. * @param[in] write_output_line Pointer to function to write output.
  101. */
  102. DataInstaller(
  103. const DiskImage& disk_image, const std::string output_dir, AdvancedOptions options,
  104. std::function<void(std::string, int, bool)> write_output_line
  105. );
  106. /**
  107. * Installer destructor.
  108. */
  109. ~DataInstaller();
  110. /**
  111. * Handle the installation progress.
  112. *
  113. * @return Installation progress [0-100].
  114. */
  115. float Progress();
  116. private:
  117. /**
  118. * Calculates the installation progress.
  119. *
  120. * @return Installation progress [0-100]
  121. */
  122. const float CalcProgress();
  123. /**
  124. * Creates a directory in the data path (<installation dir>/data/).
  125. *
  126. * @param[in] dir Path of the directory to create, relative to the data path.
  127. * @param[in] in_root True to make the path relative to the installation dir instead.
  128. * @throws std::runtime_error If the directory can't be created.
  129. */
  130. void CreateDir(const std::string& dir, bool in_root = false);
  131. /**
  132. * Creates all directories required for the installation.
  133. *
  134. * It also adds certain location to the resource group manager.
  135. */
  136. void CreateDirectories();
  137. /**
  138. * Removes the original data once the installation is complete: the extracted install disk and the
  139. * temporary files the game doesn't read.
  140. */
  141. void RemoveOriginals();
  142. /**
  143. * Installation steps.
  144. */
  145. enum InstallationSteps{
  146. /**
  147. * Installation not started.
  148. */
  149. IDLE = 0,
  150. /**
  151. * Create all directories required for the installation.
  152. */
  153. CREATE_DIRECTORIES,
  154. /**
  155. * Initialize data for installation.
  156. */
  157. INITIALIZE,
  158. /**
  159. * Initializes the battle installer for scene processing.
  160. */
  161. BATTLE_SCENES_INIT,
  162. /**
  163. * Extracts battle scenes.
  164. */
  165. BATTLE_SCENES_PROCESS,
  166. /**
  167. * Writes attack data.
  168. */
  169. BATTLE_SCENES_WRITE_ATTACKS,
  170. /**
  171. * Writes enemy data.
  172. */
  173. BATTLE_SCENES_WRITE_ENEMIES,
  174. /**
  175. * Writes enemy formation data.
  176. */
  177. BATTLE_SCENES_WRITE_FORMATIONS,
  178. /**
  179. * Initializes the battle installer for 3D model processing.
  180. */
  181. BATTLE_MODELS_INIT,
  182. /**
  183. * Extracts 3D battle models.
  184. */
  185. BATTLE_MODELS_PROCESS,
  186. /**
  187. * Prepares for 3D battle model conversion.
  188. */
  189. BATTLE_MODELS_CONVERT_INIT,
  190. /**
  191. * Converts 3D battle models.
  192. */
  193. BATTLE_MODELS_CONVERT,
  194. /**
  195. * Writes playable character model info.
  196. */
  197. BATTLE_MODELS_WRITE_CHARACTERS,
  198. /**
  199. * Writes battle scene model info.
  200. */
  201. BATTLE_MODELS_WRITE_SCENES,
  202. /**
  203. * Initializes the battle installer for 3D spell processing.
  204. */
  205. SPELL_MODELS_INIT,
  206. /**
  207. * Extracts 3D spell models.
  208. */
  209. SPELL_MODELS_PROCESS,
  210. /**
  211. * Prepares for 3D spell model conversion.
  212. */
  213. SPELL_MODELS_CONVERT_INIT,
  214. /**
  215. * Converts 3D spell models.
  216. */
  217. SPELL_MODELS_CONVERT,
  218. /**
  219. * Extracts the meshes and textures of the battle effects.
  220. */
  221. EFFECT_MODELS_INIT,
  222. /**
  223. * Converts the battle effect meshes.
  224. */
  225. EFFECT_MODELS_CONVERT,
  226. /**
  227. * Parses item and materia prices from ff7.exe.
  228. */
  229. KERNEL_PRICES,
  230. /**
  231. * Parses command data from KERNEL.BIN.
  232. */
  233. KERNEL_COMMANDS,
  234. /**
  235. * Parses attack data from KERNEL.BIN.
  236. */
  237. KERNEL_ATTACKS,
  238. /**
  239. * Parses characters data from KERNEL.BIN.
  240. */
  241. KERNEL_CHARACTERS,
  242. /**
  243. * Parses item data from KERNEL.BIN.
  244. */
  245. KERNEL_ITEMS,
  246. /**
  247. * Parses growth data from KERNEL.BIN.
  248. */
  249. KERNEL_GROWTH,
  250. /**
  251. * Parses weapon data from KERNEL.BIN.
  252. */
  253. KERNEL_WEAPONS,
  254. /**
  255. * Parses armor data from KERNEL.BIN.
  256. */
  257. KERNEL_ARMORS,
  258. /**
  259. * Parses accessory data from KERNEL.BIN.
  260. */
  261. KERNEL_ACCESSORIES,
  262. /**
  263. * Parses materia data from KERNEL.BIN.
  264. */
  265. KERNEL_MATERIA,
  266. /**
  267. * Parses key item data data from KERNEL.BIN.
  268. */
  269. KERNEL_KEY_ITEMS,
  270. /**
  271. * Parses summon names data from KERNEL.BIN.
  272. */
  273. KERNEL_SUMMON_NAMES,
  274. /**
  275. * Parses the initial savemap from KERNEL.BIN.
  276. */
  277. KERNEL_SAVEMAP,
  278. /**
  279. * Extract game images and icons from menu.lgp.
  280. */
  281. MEDIA_IMAGES,
  282. /**
  283. * Prepares the installer for sound extraction.
  284. */
  285. MEDIA_SOUNDS_INIT,
  286. /**
  287. * Extract game sounds.
  288. */
  289. MEDIA_SOUNDS,
  290. /**
  291. * Build sounds index.
  292. */
  293. MEDIA_SOUNDS_INDEX,
  294. /**
  295. * Prepares the installer for music extraction.
  296. */
  297. MEDIA_MUSICS_INIT,
  298. /**
  299. * Extracts the game music.
  300. */
  301. MEDIA_MUSICS,
  302. /**
  303. * Extracts the game high quality music.
  304. */
  305. MEDIA_MUSICS_HQ,
  306. /**
  307. * Build the music index.
  308. */
  309. MEDIA_MUSICS_INDEX,
  310. /**
  311. * Copies the FMV videos.
  312. */
  313. MEDIA_VIDEOS,
  314. /**
  315. * Initializer for {@see SPAWN_POINTS_AND_SCALE_FACTORS}.
  316. */
  317. FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT,
  318. /**
  319. * Step that collects spawn points and sclae factors.
  320. */
  321. FIELD_SPAWN_POINTS_AND_SCALE_FACTORS,
  322. /**
  323. * Step that convets fields to V-Gears format.
  324. */
  325. FIELD_CONVERT,
  326. /**
  327. * Initializer for {@see WRITE_MAPS}.
  328. */
  329. FIELD_WRITE_INIT,
  330. /**
  331. * Step that writes XML maps.
  332. */
  333. FIELD_WRITE,
  334. /**
  335. * Clean up after {@see WRITE_MAPS}.
  336. */
  337. FIELD_WRITE_END,
  338. /**
  339. * Initializer for {@see CONVERT_FIELD_MODELS}.
  340. */
  341. FIELD_CONVERT_MODELS_INIT,
  342. /**
  343. * Step that converts field models.
  344. */
  345. FIELD_CONVERT_MODELS,
  346. /**
  347. * Prepares the installer for world map data installation.
  348. */
  349. WM_INIT,
  350. /**
  351. * Generates the materials for the world map terrain.
  352. */
  353. WM_MATERIALS,
  354. /**
  355. * Step that extracts world map terrain data.
  356. */
  357. WM_MAPS,
  358. /**
  359. * Step that extracts world map models.
  360. */
  361. WM_MODELS,
  362. /**
  363. * Cleans up after the installation.
  364. */
  365. CLEAN,
  366. /**
  367. * Number of installation steps.
  368. */
  369. STATE_COUNT,
  370. /**
  371. * Installation finished.
  372. */
  373. DONE = 100,
  374. };
  375. /**
  376. * Current installation status.
  377. */
  378. InstallationSteps installation_state_ = IDLE;
  379. /**
  380. * Gets the name of the part of the installation a step belongs to.
  381. *
  382. * @param[in] state An installation step.
  383. */
  384. static const char* StepGroup(int state);
  385. /**
  386. * Prepares the resources for the conversion of a kind of models.
  387. *
  388. * The parts of the models of every kind (field, battle, spells, effects, world map) are in the same resource
  389. * group, and many have the same names in different kinds (the field part cbba.rsd and the battle part
  390. * cbba.rsd). Ogre keeps every loaded resource by name, so a model would get the part of another kind of
  391. * model, loaded before, or the part of another kind left in its temporary folder by a previous installation
  392. * in the same directory. This releases the parts loaded before, and leaves in the group only the temporary
  393. * folder of the models that will be converted.
  394. *
  395. * @param[in] temp_dir Temporary folder of the models, relative to the installation directory
  396. * ("temp/char/").
  397. */
  398. void UseModelParts(const std::string& temp_dir);
  399. /**
  400. * Substeps in the current installation step.
  401. */
  402. int substeps_ = 0;
  403. /**
  404. * Current substep in the current installation step.
  405. */
  406. int cur_substep_ = 0;
  407. /**
  408. * Weight of each installation step.
  409. *
  410. * Used to calculate current progress.
  411. */
  412. int step_weight_[STATE_COUNT];
  413. /**
  414. * List of field model names.
  415. */
  416. std::vector<std::string> field_model_names_;
  417. /**
  418. * The disk image from which to read the PC game data.
  419. */
  420. DiskImage disk_image_;
  421. /**
  422. * The path to the directory where to save the V-Gears data.
  423. */
  424. std::string output_dir_;
  425. /**
  426. * Directory where the game data is installed (<install dir>/data/).
  427. */
  428. std::string data_dir_;
  429. /**
  430. * Name of the part of the installation being done, used to report problems per part.
  431. */
  432. const char* current_group_ = nullptr;
  433. /**
  434. * Advanced options.
  435. */
  436. AdvancedOptions options_;
  437. /**
  438. * The installer application.
  439. *
  440. * It's a singleton so it can't be recreated. It will crash the second
  441. * time round.
  442. */
  443. VGears::Application application_;
  444. /**
  445. * Passes the problems that V-Gears writes to Ogre's log to the installation log.
  446. */
  447. class OgreProblems : public Ogre::LogListener{
  448. public:
  449. void messageLogged(
  450. const Ogre::String& message, Ogre::LogMessageLevel lml, bool mask_debug,
  451. const Ogre::String& log_name, bool& skip_message
  452. ) override;
  453. };
  454. /**
  455. * Listener of Ogre's log. Declared after the application, so it is destroyed before Ogre.
  456. */
  457. OgreProblems ogre_problems_;
  458. /**
  459. * LGP archive with field data.
  460. */
  461. std::unique_ptr<ScopedLgp> fields_lgp_;
  462. /**
  463. * The installer for fiel maps data.
  464. */
  465. std::unique_ptr<FieldDataInstaller> field_installer_;
  466. /**
  467. * The installer for kernel data.
  468. */
  469. std::unique_ptr<KernelDataInstaller> kernel_installer_;
  470. /**
  471. * The installer for media files.
  472. */
  473. std::unique_ptr<MediaDataInstaller> media_installer_;
  474. /**
  475. * The installer for battle data.
  476. */
  477. std::unique_ptr<BattleDataInstaller> battle_installer_;
  478. /**
  479. * The installer for world map data.
  480. */
  481. std::unique_ptr<WorldInstaller> world_installer_;
  482. /**
  483. * Function used to print text to the log output, line by line.
  484. *
  485. * @param[in] msg the message to write.
  486. * @param[in] level Log level for the message.
  487. * @param[in] as_progress If true, the text will also be set as the
  488. * current text behind the progress bar.
  489. */
  490. std::function<void(std::string msg, int level, bool progress)> write_output_line_;
  491. };