DataInstaller.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. #include <QtCore/QDir>
  16. #include <boost/filesystem.hpp>
  17. #include "DataInstaller.h"
  18. #include "InstallLayout.h"
  19. #include "InstallLog.h"
  20. #include "DiskImage.h"
  21. float DataInstaller::LINE_SCALE_FACTOR = 0.0078124970964f;
  22. DataInstaller::DataInstaller(
  23. const DiskImage& disk_image, const std::string output_dir, AdvancedOptions options,
  24. std::function<void(std::string, int, bool)> write_output_line
  25. )
  26. #ifdef _DEBUG
  27. : application_("plugins_d.cfg", "resources_d.cfg", "install_d.log"),
  28. #else
  29. : application_("plugins.cfg", "resources.cfg", "install.log"),
  30. #endif
  31. disk_image_(disk_image), output_dir_(output_dir), data_dir_(InstallLayout::DataDir(output_dir)),
  32. options_(options), write_output_line_(write_output_line)
  33. {
  34. if (!application_.initOgre(true)) throw std::runtime_error("Ogre init failure");
  35. Ogre::Log* default_log(Ogre::LogManager::getSingleton().getDefaultLog());
  36. assert( default_log );
  37. default_log->setLogDetail(Ogre::LL_LOW);
  38. // Assign weights.
  39. for (int i = IDLE; i < STATE_COUNT; i ++) step_weight_[i] = 1;
  40. step_weight_[IDLE] = 0;
  41. step_weight_[BATTLE_MODELS_CONVERT] = 2;
  42. step_weight_[SPELL_MODELS_CONVERT] = 2;
  43. step_weight_[MEDIA_IMAGES] = 3;
  44. step_weight_[MEDIA_SOUNDS] = 8;
  45. step_weight_[MEDIA_MUSICS] = 9;
  46. step_weight_[MEDIA_MUSICS_HQ] = 2;
  47. step_weight_[FIELD_SPAWN_POINTS_AND_SCALE_FACTORS] = 3;
  48. step_weight_[FIELD_CONVERT] = 3;
  49. step_weight_[FIELD_WRITE] = 2;
  50. step_weight_[FIELD_CONVERT_MODELS] = 3;
  51. }
  52. DataInstaller::~DataInstaller(){}
  53. const char* DataInstaller::StepGroup(int state){
  54. if (state >= FIELD_CONVERT_MODELS_INIT && state <= FIELD_CONVERT_MODELS)
  55. return "Field models";
  56. if (state >= FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT && state <= FIELD_WRITE_END)
  57. return "Field maps";
  58. if (state == WM_MODELS) return "World map models";
  59. if (state >= WM_INIT && state <= WM_MAPS) return "World map";
  60. if (state >= MEDIA_VIDEOS && state < FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT)
  61. return "Videos";
  62. if (state >= MEDIA_MUSICS_INIT && state < MEDIA_VIDEOS) return "Music";
  63. if (state >= MEDIA_SOUNDS_INIT && state < MEDIA_MUSICS_INIT) return "Sounds";
  64. if (state == MEDIA_IMAGES) return "Images";
  65. if (state >= KERNEL_PRICES && state < MEDIA_IMAGES) return "Kernel data";
  66. if (state >= SPELL_MODELS_INIT && state < KERNEL_PRICES) return "Spell models";
  67. if (state >= BATTLE_MODELS_INIT && state < SPELL_MODELS_INIT) return "Battle models";
  68. if (state >= BATTLE_SCENES_INIT && state < BATTLE_MODELS_INIT) return "Battle data";
  69. if (state == CLEAN) return "Cleanup";
  70. return "Preparation";
  71. }
  72. float DataInstaller::Progress(){
  73. // Report each part of the installation separately in the log.
  74. const char* group = StepGroup(installation_state_);
  75. if (current_group_ != group){
  76. current_group_ = group;
  77. InstallLog::Get().BeginStep(group);
  78. }
  79. if (installation_state_ == DONE) InstallLog::Get().EndStep();
  80. switch (installation_state_){
  81. case IDLE:
  82. installation_state_ = CREATE_DIRECTORIES;
  83. return CalcProgress();
  84. case CREATE_DIRECTORIES:
  85. write_output_line_("Creating directories...", 2, true);
  86. CreateDirectories();
  87. installation_state_ = INITIALIZE;
  88. return CalcProgress();
  89. case INITIALIZE:{
  90. write_output_line_("Initializing installers...", 2, true);
  91. const auto input_dir = disk_image_.getContentPath();
  92. if (input_dir.empty()) throw std::runtime_error("Input data directory is empty");
  93. kernel_installer_ = std::make_unique<KernelDataInstaller>(disk_image_);
  94. media_installer_ = std::make_unique<MediaDataInstaller>(
  95. disk_image_, output_dir_, options_.keep_originals
  96. );
  97. field_installer_ = std::make_unique<FieldDataInstaller>(
  98. disk_image_, output_dir_, [this](std::string log_message){
  99. write_output_line_(log_message, 2, true);
  100. }
  101. );
  102. battle_installer_ = std::make_unique<BattleDataInstaller>(disk_image_, output_dir_, application_.ResMgr());
  103. world_installer_ = std::make_unique<WorldInstaller>(
  104. disk_image_, output_dir_, options_.keep_originals, application_.ResMgr()
  105. );
  106. installation_state_ = BATTLE_SCENES_INIT;
  107. return CalcProgress();
  108. }
  109. case BATTLE_SCENES_INIT:
  110. // Skip kernel data if option is set.
  111. if (!options_.battle_data){
  112. write_output_line_("Skipping battle data installation...", 2, true);
  113. installation_state_ = BATTLE_MODELS_INIT;
  114. return CalcProgress();
  115. }
  116. write_output_line_("Parsing battle scenes...", 2, true);
  117. substeps_ = battle_installer_->InitializeScenes();
  118. cur_substep_ = 0;
  119. installation_state_ = BATTLE_SCENES_PROCESS;
  120. return CalcProgress();
  121. case BATTLE_SCENES_PROCESS:
  122. cur_substep_ = battle_installer_->ProcessScene();
  123. if (cur_substep_ >= substeps_) installation_state_ = BATTLE_SCENES_WRITE_ATTACKS;
  124. return CalcProgress();
  125. case BATTLE_SCENES_WRITE_ATTACKS:
  126. write_output_line_("Saving attacks...", 2, true);
  127. cur_substep_ = 0;
  128. substeps_ = 0;
  129. battle_installer_->WriteAttacks();
  130. installation_state_ = BATTLE_SCENES_WRITE_ENEMIES;
  131. return CalcProgress();
  132. case BATTLE_SCENES_WRITE_ENEMIES:
  133. write_output_line_("Saving enemies...", 2, true);
  134. cur_substep_ = 0;
  135. substeps_ = 0;
  136. battle_installer_->WriteEnemies();
  137. installation_state_ = BATTLE_SCENES_WRITE_FORMATIONS;
  138. return CalcProgress();
  139. case BATTLE_SCENES_WRITE_FORMATIONS:
  140. write_output_line_("Saving enemy formations...", 2, true);
  141. cur_substep_ = 0;
  142. substeps_ = 0;
  143. battle_installer_->WriteFormations();
  144. // TODO: DEBUG
  145. installation_state_ = BATTLE_MODELS_INIT; // NORMAL
  146. //installation_state_ = SPELL_MODELS_INIT; // SKIP BATTLE MODELS
  147. return CalcProgress();
  148. case BATTLE_MODELS_INIT:
  149. // Skip kernel data if option is set.
  150. if (!options_.battle_models){
  151. write_output_line_("Skipping battle models installation...", 2, true);
  152. installation_state_ = KERNEL_PRICES;
  153. return CalcProgress();
  154. }
  155. write_output_line_("Extracting battle models...", 2, true);
  156. substeps_ = battle_installer_->InitializeBattleModels();
  157. cur_substep_ = 0;
  158. installation_state_ = BATTLE_MODELS_PROCESS;
  159. return CalcProgress();
  160. case BATTLE_MODELS_PROCESS:
  161. cur_substep_ = battle_installer_->ProcessBattleModel();
  162. if (cur_substep_ >= substeps_) installation_state_ = BATTLE_MODELS_CONVERT_INIT;
  163. return CalcProgress();
  164. case BATTLE_MODELS_CONVERT_INIT:
  165. cur_substep_ = 0;
  166. substeps_ = battle_installer_->ConvertBattleModelsInit();
  167. installation_state_ = BATTLE_MODELS_CONVERT;
  168. return CalcProgress();
  169. case BATTLE_MODELS_CONVERT:
  170. cur_substep_ = battle_installer_->ConvertBattleModel();
  171. if (cur_substep_ >= substeps_) installation_state_ = BATTLE_MODELS_WRITE_CHARACTERS;
  172. return CalcProgress();
  173. case BATTLE_MODELS_WRITE_CHARACTERS:
  174. write_output_line_("Writing character data...", 2, true);
  175. battle_installer_->WriteCharacterData();
  176. installation_state_ = BATTLE_MODELS_WRITE_SCENES;
  177. return CalcProgress();
  178. case BATTLE_MODELS_WRITE_SCENES:
  179. write_output_line_("Writing battle scene data...", 2, true);
  180. battle_installer_->WriteSceneData();
  181. installation_state_ = SPELL_MODELS_INIT;
  182. return CalcProgress();
  183. case SPELL_MODELS_INIT:
  184. write_output_line_("Extracting attack models...", 2, true);
  185. substeps_ = battle_installer_->InitializeSpellModels();
  186. cur_substep_ = 0;
  187. installation_state_ = SPELL_MODELS_PROCESS;
  188. return CalcProgress();
  189. case SPELL_MODELS_PROCESS:
  190. cur_substep_ = battle_installer_->ProcessSpellModel();
  191. if (cur_substep_ >= substeps_) installation_state_ = SPELL_MODELS_CONVERT_INIT;
  192. return CalcProgress();
  193. case SPELL_MODELS_CONVERT_INIT:
  194. cur_substep_ = 0;
  195. substeps_ = battle_installer_->ConvertSpellModelsInit();
  196. installation_state_ = SPELL_MODELS_CONVERT;
  197. return CalcProgress();
  198. case SPELL_MODELS_CONVERT:
  199. cur_substep_ = battle_installer_->ConvertSpellModel();
  200. if (cur_substep_ >= substeps_) installation_state_ = KERNEL_PRICES;
  201. return CalcProgress();
  202. case KERNEL_PRICES:
  203. // Skip kernel data if option is set.
  204. if (!options_.kernel){
  205. write_output_line_("Skipping kernel data installation...", 2, true);
  206. installation_state_ = MEDIA_SOUNDS_INIT;
  207. return CalcProgress();
  208. }
  209. write_output_line_("Parsing item and materia prices...", 2, true);
  210. kernel_installer_->ReadPrices();
  211. installation_state_ = KERNEL_COMMANDS;
  212. return CalcProgress();
  213. case KERNEL_COMMANDS:
  214. write_output_line_("Extracting command data...", 2, true);
  215. kernel_installer_->ReadCommands();
  216. kernel_installer_->WriteCommands(data_dir_ + "gamedata/commands.lua");
  217. installation_state_ = KERNEL_ATTACKS;
  218. return CalcProgress();
  219. case KERNEL_ATTACKS:
  220. write_output_line_("Extracting attack data...", 2, true);
  221. kernel_installer_->ReadAttacks();
  222. kernel_installer_->WriteAttacks(data_dir_ + "gamedata/attacks.lua");
  223. installation_state_ = KERNEL_CHARACTERS;
  224. return CalcProgress();
  225. case KERNEL_CHARACTERS:
  226. write_output_line_("Extracting character data...", 2, true);
  227. kernel_installer_->ReadCharacters();
  228. kernel_installer_->WriteCharacters(data_dir_ + "gamedata/characters.lua");
  229. installation_state_ = KERNEL_ITEMS;
  230. return CalcProgress();
  231. case KERNEL_ITEMS:
  232. write_output_line_("Extracting item data...", 2, true);
  233. kernel_installer_->ReadItems();
  234. kernel_installer_->WriteItems(data_dir_ + "gamedata/items.lua");
  235. installation_state_ = KERNEL_GROWTH;
  236. return CalcProgress();
  237. case KERNEL_GROWTH:
  238. write_output_line_("Extracting growth data...", 2, true);
  239. kernel_installer_->ReadGrowth();
  240. kernel_installer_->WriteGrowth(data_dir_ + "gamedata/growth.lua");
  241. installation_state_ = KERNEL_WEAPONS;
  242. return CalcProgress();
  243. case KERNEL_WEAPONS:
  244. write_output_line_("Extracting weapon data...", 2, true);
  245. kernel_installer_->ReadWeapons();
  246. kernel_installer_->WriteWeapons(data_dir_ + "gamedata/weapons.lua");
  247. installation_state_ = KERNEL_ARMORS;
  248. return CalcProgress();
  249. case KERNEL_ARMORS:
  250. write_output_line_("Extracting armor data...", 2, true);
  251. kernel_installer_->ReadArmors();
  252. kernel_installer_->WriteArmors(data_dir_ + "gamedata/armors.lua");
  253. installation_state_ = KERNEL_ACCESSORIES;
  254. return CalcProgress();
  255. case KERNEL_ACCESSORIES:
  256. write_output_line_("Extracting accessory data...", 2, true);
  257. kernel_installer_->ReadAccessories();
  258. kernel_installer_->WriteAccessories(data_dir_ + "gamedata/accessories.lua");
  259. installation_state_ = KERNEL_MATERIA;
  260. return CalcProgress();
  261. case KERNEL_MATERIA:
  262. write_output_line_("Extracting materia data...", 2, true);
  263. kernel_installer_->ReadMateria();
  264. kernel_installer_->WriteMateria(data_dir_ + "gamedata/materia.lua");
  265. installation_state_ = KERNEL_KEY_ITEMS;
  266. return CalcProgress();
  267. case KERNEL_KEY_ITEMS:
  268. write_output_line_("Extracting key item data...", 2, true);
  269. kernel_installer_->ReadKeyItems();
  270. kernel_installer_->WriteKeyItems(data_dir_ + "gamedata/key_items.lua");
  271. installation_state_ = KERNEL_SUMMON_NAMES;
  272. return CalcProgress();
  273. case KERNEL_SUMMON_NAMES:
  274. write_output_line_("Extracting summon attack names data...", 2, true);
  275. kernel_installer_->ReadSummonNames();
  276. kernel_installer_->WriteSummonNames(data_dir_ + "gamedata/summons.lua");
  277. installation_state_ = KERNEL_SAVEMAP;
  278. return CalcProgress();
  279. case KERNEL_SAVEMAP:
  280. write_output_line_("Extracting the initial savemap...", 2, true);
  281. kernel_installer_->ReadInitialSaveMap();
  282. kernel_installer_->WriteInitialSaveMap(data_dir_ + "gamedata/initial_savemap.lua");
  283. installation_state_ = MEDIA_IMAGES;
  284. return CalcProgress();
  285. case MEDIA_IMAGES:
  286. // Skip images if option is set.
  287. if (!options_.images){
  288. write_output_line_("Skipping images installation...", 2, true);
  289. installation_state_ = MEDIA_SOUNDS_INIT;
  290. return CalcProgress();
  291. }
  292. write_output_line_("Extracting game images...", 2, true);
  293. media_installer_->InstallSprites();
  294. installation_state_ = MEDIA_SOUNDS_INIT;
  295. return CalcProgress();
  296. case MEDIA_SOUNDS_INIT:
  297. // Skip sounds if option is set.
  298. if (!options_.sounds){
  299. write_output_line_("Skipping sound effects installation...", 2, true);
  300. installation_state_ = MEDIA_MUSICS_INIT;
  301. return CalcProgress();
  302. }
  303. write_output_line_("Extracting sounds...", 2, true);
  304. substeps_ = media_installer_->InstallSoundsInit();
  305. installation_state_ = MEDIA_SOUNDS;
  306. cur_substep_ = 0;
  307. return CalcProgress();
  308. case MEDIA_SOUNDS:
  309. if (media_installer_->InstallSounds() == true) installation_state_ = MEDIA_SOUNDS_INDEX;
  310. else cur_substep_ ++;
  311. return CalcProgress();
  312. case MEDIA_SOUNDS_INDEX:
  313. // The index is written when the conversions, which run in the background, have finished.
  314. installation_state_ = MEDIA_MUSICS_INIT;
  315. return CalcProgress();
  316. case MEDIA_MUSICS_INIT:
  317. // Skip music if option is set.
  318. if (!options_.music){
  319. write_output_line_("Skipping music tracks installation...", 2, true);
  320. installation_state_ = FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT;
  321. return CalcProgress();
  322. }
  323. write_output_line_("Extracting music...", 2, true);
  324. substeps_ = media_installer_->InstallMusicsInit();
  325. installation_state_ = MEDIA_MUSICS;
  326. cur_substep_ = 0;
  327. return CalcProgress();
  328. case MEDIA_MUSICS:
  329. if (media_installer_->InstallMusics() == true) installation_state_ = MEDIA_MUSICS_HQ;
  330. else cur_substep_ ++;
  331. return CalcProgress();
  332. case MEDIA_MUSICS_HQ:
  333. substeps_ = 0;
  334. cur_substep_ = 0;
  335. media_installer_->InstallHQMusics();
  336. installation_state_ = MEDIA_MUSICS_INDEX;
  337. return CalcProgress();
  338. case MEDIA_MUSICS_INDEX:
  339. // The index is written when the conversions, which run in the background, have finished.
  340. installation_state_ = FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT;
  341. return CalcProgress();
  342. case MEDIA_VIDEOS:
  343. // Skip videos if option is set.
  344. if (!options_.videos){
  345. write_output_line_("Skipping video installation...", 2, true);
  346. installation_state_ = FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT;
  347. return CalcProgress();
  348. }
  349. write_output_line_("Extracting videos...", 2, true);
  350. media_installer_->InstallVideos();
  351. installation_state_ = FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT;
  352. return CalcProgress();
  353. case FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT:
  354. // Skip fields if option is set.
  355. if (!options_.fields && !options_.wm){
  356. write_output_line_("Skipping field maps installation...", 2, true);
  357. installation_state_ = WM_INIT;
  358. return CalcProgress();
  359. }
  360. // The world map scripts need the field list and scale factors even if the fields aren't converted.
  361. if (!options_.fields) write_output_line_("Collecting field data for the world map...", 2, true);
  362. else write_output_line_("Collecting spawn points and scale factors...", 2, true);
  363. substeps_ = field_installer_->CollectSpawnAndScaleFactorsInit(application_.ResMgr());
  364. cur_substep_ = 0;
  365. installation_state_ = FIELD_SPAWN_POINTS_AND_SCALE_FACTORS;
  366. return CalcProgress();
  367. case FIELD_SPAWN_POINTS_AND_SCALE_FACTORS:
  368. field_installer_->CollectSpawnAndScaleFactors(cur_substep_);
  369. cur_substep_ ++;
  370. if (cur_substep_ == substeps_){
  371. cur_substep_ = 0;
  372. if (options_.fields) installation_state_ = FIELD_CONVERT;
  373. else{
  374. // Fields are not converted: use the ones already installed.
  375. write_output_line_("Skipping field maps installation...", 2, true);
  376. field_installer_->LoadInstalledFields();
  377. installation_state_ = WM_INIT;
  378. }
  379. }
  380. return CalcProgress();
  381. case FIELD_CONVERT:
  382. field_installer_->Convert(cur_substep_);
  383. cur_substep_ ++;
  384. if (cur_substep_ == substeps_){
  385. installation_state_ = FIELD_WRITE_INIT;
  386. cur_substep_ = 0;
  387. }
  388. return CalcProgress();
  389. case FIELD_WRITE_INIT:
  390. write_output_line_("Writing fields...", 2, true);
  391. substeps_ = field_installer_->WriteInit();
  392. cur_substep_ = 0;
  393. installation_state_ = FIELD_WRITE;
  394. return CalcProgress();
  395. case FIELD_WRITE:
  396. field_installer_->Write(cur_substep_);
  397. cur_substep_ ++;
  398. if (cur_substep_ == substeps_){
  399. installation_state_ = FIELD_WRITE_END;
  400. cur_substep_ = 0;
  401. }
  402. return CalcProgress();
  403. case FIELD_WRITE_END:
  404. field_installer_->WriteEnd();
  405. installation_state_ = FIELD_CONVERT_MODELS_INIT;
  406. return CalcProgress();
  407. case FIELD_CONVERT_MODELS_INIT:
  408. // Skip fields if option is set.
  409. if (!options_.field_models){
  410. write_output_line_("Skipping field maps installation...", 2, true);
  411. installation_state_ = WM_INIT;
  412. return CalcProgress();
  413. }
  414. write_output_line_("Converting field models...", 2, true);
  415. field_model_names_ = field_installer_->ConvertModelsInit();
  416. substeps_ = field_model_names_.size();
  417. cur_substep_ = 0;
  418. installation_state_ = FIELD_CONVERT_MODELS;
  419. return CalcProgress();
  420. case FIELD_CONVERT_MODELS:
  421. field_installer_->ConvertModels(field_model_names_[cur_substep_]);
  422. cur_substep_ ++;
  423. if (cur_substep_ == substeps_){
  424. installation_state_ = WM_INIT;
  425. cur_substep_ = 0;
  426. }
  427. return CalcProgress();
  428. case WM_INIT:
  429. // Skip world map if option set
  430. if (!options_.wm){
  431. write_output_line_("Skipping world map data installation...", 2, true);
  432. installation_state_ = CLEAN;
  433. return CalcProgress();
  434. }
  435. write_output_line_("Extracting world map data...", 2, true);
  436. substeps_ = world_installer_->Initialize();
  437. cur_substep_ = 0;
  438. installation_state_ = WM_MATERIALS;
  439. return CalcProgress();
  440. case WM_MATERIALS:
  441. world_installer_->GenerateMaterials();
  442. installation_state_ = WM_MAPS;
  443. return CalcProgress();
  444. case WM_MAPS:
  445. if (world_installer_->ProcessMap() == false) cur_substep_ ++;
  446. else{
  447. std::string script_error;
  448. if (world_installer_->GenerateScripts(
  449. field_installer_->GetMapList(), field_installer_->GetFieldScaleFactors(),
  450. field_installer_->GetConvertedMapList(), script_error
  451. )){
  452. write_output_line_("Generated world-map scripts.", 2, true);
  453. }
  454. else{
  455. write_output_line_("Could not generate world-map scripts: " + script_error, 2, true);
  456. }
  457. installation_state_ = WM_MODELS;
  458. cur_substep_ = 0;
  459. }
  460. return CalcProgress();
  461. case WM_MODELS:
  462. if (!options_.wm_models) write_output_line_("Skipping world map model installation...", 2, true);
  463. else world_installer_->ProcessModels();
  464. installation_state_ = CLEAN;
  465. return CalcProgress();
  466. case CLEAN:
  467. // The audio conversions run in the background while the other steps are done.
  468. if (media_installer_){
  469. write_output_line_("Waiting for the audio conversions to finish...", 2, true);
  470. media_installer_->WaitForConversions();
  471. // Now the files that exist are known, so the indexes list only those.
  472. if (options_.sounds){
  473. write_output_line_("Building sound index...", 2, true);
  474. media_installer_->WriteSoundIndex();
  475. }
  476. if (options_.music){
  477. write_output_line_("Building music track index...", 2, true);
  478. media_installer_->WriteMusicsIndex();
  479. }
  480. }
  481. write_output_line_("Cleaning up...", 2, true);
  482. if (!options_.keep_originals) CleanInstall();
  483. installation_state_ = DONE;
  484. return CalcProgress();
  485. case DONE:
  486. write_output_line_("Installation complete", 2, true);
  487. return 100;
  488. default:
  489. return 100;
  490. }
  491. }
  492. const float DataInstaller::CalcProgress(){
  493. float weight_total = 0; // Sum of every step weight.
  494. float weight_done = 0; // Sum of every previous step weight.
  495. float weight = static_cast<float>(step_weight_[installation_state_]); // Weight of current step.
  496. float weight_substeps = 0; // Weight of the already done substeps in current step.
  497. for (int i = IDLE; i < STATE_COUNT; i ++){
  498. weight_total += step_weight_[i];
  499. if (installation_state_ > i) weight_done += step_weight_[i];
  500. }
  501. if (substeps_ > 0 && cur_substep_ < substeps_) weight_substeps = (cur_substep_ * weight) / substeps_;
  502. float progress = 100 * (weight_done + weight_substeps) / weight_total;
  503. if (progress >= 100.0f) progress = 99.9f;
  504. return progress;
  505. }
  506. void DataInstaller::CreateDirectories(){
  507. for (const std::string& dir : InstallLayout::DataSubdirs()) CreateDir(dir);
  508. CreateDir("temp", true);
  509. for (const std::string& dir : InstallLayout::TempSubdirs()) CreateDir("temp/" + dir, true);
  510. application_.ResMgr()->addResourceLocation(output_dir_ + "temp/char/", "FileSystem", "FFVII", true, true);
  511. application_.ResMgr()->addResourceLocation(output_dir_ + "temp/battle_models/", "FileSystem", "FFVII", true, true);
  512. application_.ResMgr()->addResourceLocation(output_dir_ + "temp/world_models/", "FileSystem", "FFVII", true, true);
  513. application_.ResMgr()->addResourceLocation(data_dir_ + "models/", "FileSystem", "FFVII", true, true);
  514. application_.ResMgr()->addResourceLocation(
  515. data_dir_ + "models/fields/", "FileSystem", "FFVIITextures", true, true
  516. );
  517. application_.ResMgr()->addResourceLocation(
  518. data_dir_ + "models/battle/characters/", "FileSystem", "FFVIITextures", true, true
  519. );
  520. application_.ResMgr()->addResourceLocation(
  521. data_dir_ + "models/battle/scenes/", "FileSystem", "FFVIITextures", true, true
  522. );
  523. application_.ResMgr()->addResourceLocation(
  524. data_dir_ + "models/battle/enemies/", "FileSystem", "FFVIITextures", true, true
  525. );
  526. application_.ResMgr()->addResourceLocation(
  527. data_dir_ + "models/battle/attacks/", "FileSystem", "FFVIITextures", true, true
  528. );
  529. application_.ResMgr()->addResourceLocation(
  530. data_dir_ + "models/world/", "FileSystem", "FFVIITextures", true, true
  531. );
  532. const auto fields_lgp_path = disk_image_.filePath("data/field/flevel.lgp");
  533. if (!fields_lgp_path) throw std::runtime_error("Missing required file: data/field/flevel.lgp");
  534. fields_lgp_ = std::make_unique<ScopedLgp>(application_.getRoot(), *fields_lgp_path, "LGP", "FFVIIFields");
  535. }
  536. void DataInstaller::CleanInstall(){
  537. // The game itself reads some of the temp directories, so only the rest are removed.
  538. for (const std::string& dir : InstallLayout::RemovableTempSubdirs())
  539. boost::filesystem::remove_all(InstallLayout::TempDir(output_dir_) + dir + "/");
  540. }
  541. void DataInstaller::CreateDir(const std::string& path, bool in_root){
  542. QString target = QString::fromStdString((in_root ? output_dir_ : data_dir_) + path);
  543. QDir dir(target);
  544. if (!dir.mkpath(".")) throw std::runtime_error("Failed to mkpath");
  545. }