DataInstaller.h 15 KB

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