DataInstaller.cpp 28 KB

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