Browse Source

Split the installation steps into a function for each part

- Progress runs the function of the current step: preparation, battle data and models, kernel data, media, fields or world map.
- Reporting the progress of the last step read past the end of the step weights.
- Writing the list of fields never ended if no field had been converted, and converting field models failed without models.
- Skipping the field models said that the field maps were skipped.
- Removed an unused constant.
Iñigo Valentin 4 days ago
parent
commit
41680dbd6e
2 changed files with 438 additions and 333 deletions
  1. 338 325
      src/installer/steps/DataInstaller.cpp
  2. 100 8
      src/installer/steps/DataInstaller.h

+ 338 - 325
src/installer/steps/DataInstaller.cpp

@@ -24,8 +24,6 @@
 #include "formats/model/PFileManager.h"
 #include "formats/model/PFileManager.h"
 #include "formats/model/RSDFileManager.h"
 #include "formats/model/RSDFileManager.h"
 
 
-float DataInstaller::LINE_SCALE_FACTOR = 0.0078124970964f;
-
 DataInstaller::DataInstaller(
 DataInstaller::DataInstaller(
   const DiskImage& disk_image, const std::string output_dir, AdvancedOptions options,
   const DiskImage& disk_image, const std::string output_dir, AdvancedOptions options,
   std::function<void(std::string, int, bool)> write_output_line
   std::function<void(std::string, int, bool)> write_output_line
@@ -104,432 +102,447 @@ float DataInstaller::Progress(){
         InstallLog::Get().BeginStep(group);
         InstallLog::Get().BeginStep(group);
     }
     }
     if (installation_state_ == DONE) InstallLog::Get().EndStep();
     if (installation_state_ == DONE) InstallLog::Get().EndStep();
+    if (installation_state_ == DONE){
+        Say("Installation complete");
+        return 100;
+    }
+    // The steps of each part are together, in the order they are done.
+    if (installation_state_ < BATTLE_SCENES_INIT) PreparationStep();
+    else if (installation_state_ < BATTLE_MODELS_INIT) BattleDataStep();
+    else if (installation_state_ < SPELL_MODELS_INIT) BattleModelStep();
+    else if (installation_state_ < KERNEL_PRICES) SpellModelStep();
+    else if (installation_state_ < MEDIA_IMAGES) KernelStep();
+    else if (installation_state_ < MEDIA_SOUNDS_INIT) ImageStep();
+    else if (installation_state_ < MEDIA_MUSICS_INIT) SoundStep();
+    else if (installation_state_ < FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT) MusicStep();
+    else if (installation_state_ < FIELD_CONVERT_MODELS_INIT) FieldStep();
+    else if (installation_state_ < WM_INIT) FieldModelStep();
+    else if (installation_state_ < CLEAN) WorldStep();
+    else CleanStep();
+    return CalcProgress();
+}
+
+void DataInstaller::Say(const std::string& message){write_output_line_(message, 2, true);}
+
+void DataInstaller::Skip(const std::string& part, const InstallationSteps next){
+    Say("Skipping " + part + " installation...");
+    installation_state_ = next;
+}
+
+void DataInstaller::Repeat(const int substeps, const InstallationSteps next){
+    substeps_ = substeps;
+    cur_substep_ = 0;
+    installation_state_ = next;
+}
+
+bool DataInstaller::NextSubstep(){
+    ++ cur_substep_;
+    return cur_substep_ >= substeps_;
+}
+
+void DataInstaller::PreparationStep(){
     switch (installation_state_){
     switch (installation_state_){
         case IDLE:
         case IDLE:
             installation_state_ = CREATE_DIRECTORIES;
             installation_state_ = CREATE_DIRECTORIES;
-            return CalcProgress();
+            break;
         case CREATE_DIRECTORIES:
         case CREATE_DIRECTORIES:
-            write_output_line_("Creating directories...", 2, true);
+            Say("Creating directories...");
             CreateDirectories();
             CreateDirectories();
             installation_state_ = INITIALIZE;
             installation_state_ = INITIALIZE;
-            return CalcProgress();
-        case INITIALIZE:{
-            write_output_line_("Initializing installers...", 2, true);
-            const auto input_dir = disk_image_.getContentPath();
-            if (input_dir.empty()) throw std::runtime_error("Input data directory is empty");
-            kernel_installer_ = std::make_unique<KernelDataInstaller>(disk_image_);
-            media_installer_ = std::make_unique<MediaDataInstaller>(
-              disk_image_, output_dir_, options_.keep_originals
-            );
-                        field_installer_ = std::make_unique<FieldDataInstaller>(
-                            disk_image_, output_dir_, [this](std::string log_message){
-                                    write_output_line_(log_message, 2, true);
-                            }
-                        );
-            battle_installer_ = std::make_unique<BattleDataInstaller>(disk_image_, output_dir_, application_.ResMgr());
-            world_installer_ = std::make_unique<WorldInstaller>(
-              disk_image_, output_dir_, application_.ResMgr()
-            );
+            break;
+        default:
+            Say("Initializing installers...");
+            CreateInstallers();
             installation_state_ = BATTLE_SCENES_INIT;
             installation_state_ = BATTLE_SCENES_INIT;
-            return CalcProgress();
-        }
+    }
+}
+
+void DataInstaller::CreateInstallers(){
+    if (disk_image_.getContentPath().empty())
+        throw std::runtime_error("Input data directory is empty");
+    kernel_installer_ = std::make_unique<KernelDataInstaller>(disk_image_);
+    media_installer_ = std::make_unique<MediaDataInstaller>(
+      disk_image_, output_dir_, options_.keep_originals
+    );
+    field_installer_ = std::make_unique<FieldDataInstaller>(
+      disk_image_, output_dir_, [this](std::string message){Say(message);}
+    );
+    battle_installer_ = std::make_unique<BattleDataInstaller>(
+      disk_image_, output_dir_, application_.ResMgr()
+    );
+    world_installer_ = std::make_unique<WorldInstaller>(
+      disk_image_, output_dir_, application_.ResMgr()
+    );
+}
+
+void DataInstaller::BattleDataStep(){
+    switch (installation_state_){
         case BATTLE_SCENES_INIT:
         case BATTLE_SCENES_INIT:
-            // Skip kernel data if option is set.
             if (!options_.battle_data){
             if (!options_.battle_data){
-                write_output_line_("Skipping battle data installation...", 2, true);
-                installation_state_ = BATTLE_MODELS_INIT;
-                return CalcProgress();
+                Skip("battle data", BATTLE_MODELS_INIT);
+                break;
             }
             }
-            write_output_line_("Parsing battle scenes...", 2, true);
-            substeps_ = battle_installer_->InitializeScenes();
-            cur_substep_ = 0;
-            installation_state_ = BATTLE_SCENES_PROCESS;
-            return CalcProgress();
+            Say("Parsing battle scenes...");
+            Repeat(battle_installer_->InitializeScenes(), BATTLE_SCENES_PROCESS);
+            break;
         case BATTLE_SCENES_PROCESS:
         case BATTLE_SCENES_PROCESS:
             cur_substep_ = battle_installer_->ProcessScene();
             cur_substep_ = battle_installer_->ProcessScene();
             if (cur_substep_ >= substeps_) installation_state_ = BATTLE_SCENES_WRITE_ATTACKS;
             if (cur_substep_ >= substeps_) installation_state_ = BATTLE_SCENES_WRITE_ATTACKS;
-            return CalcProgress();
+            break;
         case BATTLE_SCENES_WRITE_ATTACKS:
         case BATTLE_SCENES_WRITE_ATTACKS:
-            write_output_line_("Saving attacks...", 2, true);
-            cur_substep_ = 0;
-            substeps_ = 0;
+            Say("Saving attacks...");
             battle_installer_->WriteAttacks();
             battle_installer_->WriteAttacks();
-            installation_state_ = BATTLE_SCENES_WRITE_ENEMIES;
-            return CalcProgress();
+            Repeat(0, BATTLE_SCENES_WRITE_ENEMIES);
+            break;
         case BATTLE_SCENES_WRITE_ENEMIES:
         case BATTLE_SCENES_WRITE_ENEMIES:
-            write_output_line_("Saving enemies...", 2, true);
-            cur_substep_ = 0;
-            substeps_ = 0;
+            Say("Saving enemies...");
             battle_installer_->WriteEnemies();
             battle_installer_->WriteEnemies();
-            installation_state_ = BATTLE_SCENES_WRITE_FORMATIONS;
-            return CalcProgress();
-        case BATTLE_SCENES_WRITE_FORMATIONS:
-            write_output_line_("Saving enemy formations...", 2, true);
-            cur_substep_ = 0;
-            substeps_ = 0;
+            Repeat(0, BATTLE_SCENES_WRITE_FORMATIONS);
+            break;
+        default:
+            Say("Saving enemy formations...");
             battle_installer_->WriteFormations();
             battle_installer_->WriteFormations();
-            // TODO: DEBUG
-            installation_state_ = BATTLE_MODELS_INIT; // NORMAL
-            //installation_state_ = SPELL_MODELS_INIT; // SKIP BATTLE MODELS
-            return CalcProgress();
+            Repeat(0, BATTLE_MODELS_INIT);
+    }
+}
+
+void DataInstaller::BattleModelStep(){
+    switch (installation_state_){
         case BATTLE_MODELS_INIT:
         case BATTLE_MODELS_INIT:
-            // Skip kernel data if option is set.
+            // The spell and effect models are part of the battle models.
             if (!options_.battle_models){
             if (!options_.battle_models){
-                write_output_line_("Skipping battle models installation...", 2, true);
-                installation_state_ = KERNEL_PRICES;
-                return CalcProgress();
+                Skip("battle models", KERNEL_PRICES);
+                break;
             }
             }
-            write_output_line_("Extracting battle models...", 2, true);
-            substeps_ = battle_installer_->InitializeBattleModels();
-            cur_substep_ = 0;
-            installation_state_ = BATTLE_MODELS_PROCESS;
-            return CalcProgress();
+            Say("Extracting battle models...");
+            Repeat(battle_installer_->InitializeBattleModels(), BATTLE_MODELS_PROCESS);
+            break;
         case BATTLE_MODELS_PROCESS:
         case BATTLE_MODELS_PROCESS:
             cur_substep_ = battle_installer_->ProcessBattleModel();
             cur_substep_ = battle_installer_->ProcessBattleModel();
             if (cur_substep_ >= substeps_) installation_state_ = BATTLE_MODELS_CONVERT_INIT;
             if (cur_substep_ >= substeps_) installation_state_ = BATTLE_MODELS_CONVERT_INIT;
-            return CalcProgress();
+            break;
         case BATTLE_MODELS_CONVERT_INIT:
         case BATTLE_MODELS_CONVERT_INIT:
             UseModelParts("temp/battle_models/");
             UseModelParts("temp/battle_models/");
-            cur_substep_ = 0;
-            substeps_ =  battle_installer_->ConvertBattleModelsInit();
-            installation_state_ = BATTLE_MODELS_CONVERT;
-            return CalcProgress();
+            Repeat(battle_installer_->ConvertBattleModelsInit(), BATTLE_MODELS_CONVERT);
+            break;
         case BATTLE_MODELS_CONVERT:
         case BATTLE_MODELS_CONVERT:
             cur_substep_ = battle_installer_->ConvertBattleModel();
             cur_substep_ = battle_installer_->ConvertBattleModel();
             if (cur_substep_ >= substeps_) installation_state_ = BATTLE_MODELS_WRITE_CHARACTERS;
             if (cur_substep_ >= substeps_) installation_state_ = BATTLE_MODELS_WRITE_CHARACTERS;
-            return CalcProgress();
+            break;
         case BATTLE_MODELS_WRITE_CHARACTERS:
         case BATTLE_MODELS_WRITE_CHARACTERS:
-            write_output_line_("Writing character data...", 2, true);
+            Say("Writing character data...");
             battle_installer_->WriteCharacterData();
             battle_installer_->WriteCharacterData();
             installation_state_ = BATTLE_MODELS_WRITE_SCENES;
             installation_state_ = BATTLE_MODELS_WRITE_SCENES;
-            return CalcProgress();
-        case BATTLE_MODELS_WRITE_SCENES:
-            write_output_line_("Writing battle scene data...", 2, true);
+            break;
+        default:
+            Say("Writing battle scene data...");
             battle_installer_->WriteSceneData();
             battle_installer_->WriteSceneData();
             installation_state_ = SPELL_MODELS_INIT;
             installation_state_ = SPELL_MODELS_INIT;
-            return CalcProgress();
+    }
+}
+
+void DataInstaller::SpellModelStep(){
+    switch (installation_state_){
         case SPELL_MODELS_INIT:
         case SPELL_MODELS_INIT:
-            write_output_line_("Extracting attack models...", 2, true);
-            substeps_ = battle_installer_->InitializeSpellModels();
-            cur_substep_ = 0;
-            installation_state_ = SPELL_MODELS_PROCESS;
-            return CalcProgress();
+            Say("Extracting attack models...");
+            Repeat(battle_installer_->InitializeSpellModels(), SPELL_MODELS_PROCESS);
+            break;
         case SPELL_MODELS_PROCESS:
         case SPELL_MODELS_PROCESS:
             cur_substep_ = battle_installer_->ProcessSpellModel();
             cur_substep_ = battle_installer_->ProcessSpellModel();
             if (cur_substep_ >= substeps_) installation_state_ = SPELL_MODELS_CONVERT_INIT;
             if (cur_substep_ >= substeps_) installation_state_ = SPELL_MODELS_CONVERT_INIT;
-            return CalcProgress();
+            break;
         case SPELL_MODELS_CONVERT_INIT:
         case SPELL_MODELS_CONVERT_INIT:
             UseModelParts("temp/spell_models/");
             UseModelParts("temp/spell_models/");
-            cur_substep_ = 0;
-            substeps_ =  battle_installer_->ConvertSpellModelsInit();
-            installation_state_ = SPELL_MODELS_CONVERT;
-            return CalcProgress();
+            Repeat(battle_installer_->ConvertSpellModelsInit(), SPELL_MODELS_CONVERT);
+            break;
         case SPELL_MODELS_CONVERT:
         case SPELL_MODELS_CONVERT:
             cur_substep_ = battle_installer_->ConvertSpellModel();
             cur_substep_ = battle_installer_->ConvertSpellModel();
             if (cur_substep_ >= substeps_) installation_state_ = EFFECT_MODELS_INIT;
             if (cur_substep_ >= substeps_) installation_state_ = EFFECT_MODELS_INIT;
-            return CalcProgress();
+            break;
         case EFFECT_MODELS_INIT:
         case EFFECT_MODELS_INIT:
-            write_output_line_("Extracting battle effects...", 2, true);
+            Say("Extracting battle effects...");
             UseModelParts("temp/effects/");
             UseModelParts("temp/effects/");
-            substeps_ = battle_installer_->InitializeEffectModels();
-            cur_substep_ = 0;
-            installation_state_ = EFFECT_MODELS_CONVERT;
-            return CalcProgress();
-        case EFFECT_MODELS_CONVERT:
+            Repeat(battle_installer_->InitializeEffectModels(), EFFECT_MODELS_CONVERT);
+            break;
+        default:
             cur_substep_ = battle_installer_->ConvertEffectModel();
             cur_substep_ = battle_installer_->ConvertEffectModel();
             if (cur_substep_ >= substeps_){
             if (cur_substep_ >= substeps_){
                 battle_installer_->WriteEffectModels();
                 battle_installer_->WriteEffectModels();
                 installation_state_ = KERNEL_PRICES;
                 installation_state_ = KERNEL_PRICES;
             }
             }
-            return CalcProgress();
+    }
+}
+
+void DataInstaller::KernelStep(){
+    const std::string dir = data_dir_ + "kernel/";
+    KernelDataInstaller* kernel = kernel_installer_.get();
+    switch (installation_state_){
         case KERNEL_PRICES:
         case KERNEL_PRICES:
-            // Skip kernel data if option is set.
             if (!options_.kernel){
             if (!options_.kernel){
-                write_output_line_("Skipping kernel data installation...", 2, true);
-                installation_state_ = MEDIA_IMAGES;
-                return CalcProgress();
+                Skip("kernel data", MEDIA_IMAGES);
+                return;
             }
             }
-            write_output_line_("Parsing item and materia prices...", 2, true);
-            kernel_installer_->ReadPrices();
-            kernel_installer_->WriteShops(data_dir_ + "kernel/shops.lua");
-            installation_state_ = KERNEL_COMMANDS;
-            return CalcProgress();
+            Say("Parsing item and materia prices...");
+            kernel->ReadPrices();
+            kernel->WriteShops(dir + "shops.lua");
+            break;
         case KERNEL_COMMANDS:
         case KERNEL_COMMANDS:
-            write_output_line_("Extracting command data...", 2, true);
-            kernel_installer_->ReadCommands();
-            kernel_installer_->WriteCommands(data_dir_ + "kernel/commands.lua");
-            installation_state_ = KERNEL_ATTACKS;
-            return CalcProgress();
+            Say("Extracting command data...");
+            kernel->ReadCommands();
+            kernel->WriteCommands(dir + "commands.lua");
+            break;
         case KERNEL_ATTACKS:
         case KERNEL_ATTACKS:
-            write_output_line_("Extracting attack data...", 2, true);
-            kernel_installer_->ReadAttacks();
-            kernel_installer_->WriteAttacks(data_dir_ + "kernel/attacks.lua");
-            installation_state_ = KERNEL_CHARACTERS;
-            return CalcProgress();
+            Say("Extracting attack data...");
+            kernel->ReadAttacks();
+            kernel->WriteAttacks(dir + "attacks.lua");
+            break;
         case KERNEL_CHARACTERS:
         case KERNEL_CHARACTERS:
-            write_output_line_("Extracting character data...", 2, true);
-            kernel_installer_->ReadCharacters();
-            kernel_installer_->WriteCharacters(data_dir_ + "kernel/characters.lua");
-            installation_state_ = KERNEL_ITEMS;
-            return CalcProgress();
+            Say("Extracting character data...");
+            kernel->ReadCharacters();
+            kernel->WriteCharacters(dir + "characters.lua");
+            break;
         case KERNEL_ITEMS:
         case KERNEL_ITEMS:
-            write_output_line_("Extracting item data...", 2, true);
-            kernel_installer_->ReadItems();
-            kernel_installer_->WriteItems(data_dir_ + "kernel/items.lua");
-            installation_state_ = KERNEL_GROWTH;
-            return CalcProgress();
+            Say("Extracting item data...");
+            kernel->ReadItems();
+            kernel->WriteItems(dir + "items.lua");
+            break;
         case KERNEL_GROWTH:
         case KERNEL_GROWTH:
-            write_output_line_("Extracting growth data...", 2, true);
-            kernel_installer_->ReadGrowth();
-            kernel_installer_->WriteGrowth(data_dir_ + "kernel/growth.lua");
-            installation_state_ = KERNEL_WEAPONS;
-            return CalcProgress();
+            Say("Extracting growth data...");
+            kernel->ReadGrowth();
+            kernel->WriteGrowth(dir + "growth.lua");
+            break;
         case KERNEL_WEAPONS:
         case KERNEL_WEAPONS:
-            write_output_line_("Extracting weapon data...", 2, true);
-            kernel_installer_->ReadWeapons();
-            kernel_installer_->WriteWeapons(data_dir_ + "kernel/weapons.lua");
-            installation_state_ = KERNEL_ARMORS;
-            return CalcProgress();
+            Say("Extracting weapon data...");
+            kernel->ReadWeapons();
+            kernel->WriteWeapons(dir + "weapons.lua");
+            break;
         case KERNEL_ARMORS:
         case KERNEL_ARMORS:
-            write_output_line_("Extracting armor data...", 2, true);
-            kernel_installer_->ReadArmors();
-            kernel_installer_->WriteArmors(data_dir_ + "kernel/armors.lua");
-            installation_state_ = KERNEL_ACCESSORIES;
-            return CalcProgress();
+            Say("Extracting armor data...");
+            kernel->ReadArmors();
+            kernel->WriteArmors(dir + "armors.lua");
+            break;
         case KERNEL_ACCESSORIES:
         case KERNEL_ACCESSORIES:
-            write_output_line_("Extracting accessory data...", 2, true);
-            kernel_installer_->ReadAccessories();
-            kernel_installer_->WriteAccessories(data_dir_ + "kernel/accessories.lua");
-            installation_state_ = KERNEL_MATERIA;
-            return CalcProgress();
+            Say("Extracting accessory data...");
+            kernel->ReadAccessories();
+            kernel->WriteAccessories(dir + "accessories.lua");
+            break;
         case KERNEL_MATERIA:
         case KERNEL_MATERIA:
-            write_output_line_("Extracting materia data...", 2, true);
-            kernel_installer_->ReadMateria();
-            kernel_installer_->WriteMateria(data_dir_ + "kernel/materia.lua");
-            installation_state_ = KERNEL_KEY_ITEMS;
-            return CalcProgress();
+            Say("Extracting materia data...");
+            kernel->ReadMateria();
+            kernel->WriteMateria(dir + "materia.lua");
+            break;
         case KERNEL_KEY_ITEMS:
         case KERNEL_KEY_ITEMS:
-            write_output_line_("Extracting key item data...", 2, true);
-            kernel_installer_->ReadKeyItems();
-            kernel_installer_->WriteKeyItems(data_dir_ + "kernel/key_items.lua");
-            installation_state_ = KERNEL_SUMMON_NAMES;
-            return CalcProgress();
+            Say("Extracting key item data...");
+            kernel->ReadKeyItems();
+            kernel->WriteKeyItems(dir + "key_items.lua");
+            break;
         case KERNEL_SUMMON_NAMES:
         case KERNEL_SUMMON_NAMES:
-            write_output_line_("Extracting summon attack names data...", 2, true);
-            kernel_installer_->ReadSummonNames();
-            kernel_installer_->WriteSummonNames(data_dir_ + "kernel/summons.lua");
-            installation_state_ = KERNEL_SAVEMAP;
-            return CalcProgress();
-        case KERNEL_SAVEMAP:
-            write_output_line_("Extracting the initial savemap...", 2, true);
-            kernel_installer_->ReadInitialSaveMap();
-            {
-                auto exe_path = disk_image_.filePath("ff7.exe");
-                if (exe_path) kernel_installer_->ReadRecruitRecords(*exe_path);
-                else write_output_line_("ff7.exe not found, Cait Sith and Vincent won't have their data.", 2, true);
-            }
-            kernel_installer_->WriteInitialSaveMap(data_dir_ + "kernel/initial_savemap.lua");
-            installation_state_ = MEDIA_IMAGES;
-            return CalcProgress();
-        case MEDIA_IMAGES:
-            // The fonts are always needed, even if the rest of the images are skipped.
-            write_output_line_("Extracting fonts...", 2, true);
-            if (!media_installer_->InstallFonts())
-                write_output_line_("WARNING: Couldn't read WINDOW.BIN, the game won't be able to show text.", 2, true);
-            // Skip images if option is set.
-            if (!options_.images){
-                write_output_line_("Skipping images installation...", 2, true);
-                installation_state_ = MEDIA_SOUNDS_INIT;
-                return CalcProgress();
-            }
-            write_output_line_("Extracting game images...", 2, true);
-            media_installer_->InstallSprites();
-            installation_state_ = MEDIA_SOUNDS_INIT;
-            return CalcProgress();
+            Say("Extracting summon attack names data...");
+            kernel->ReadSummonNames();
+            kernel->WriteSummonNames(dir + "summons.lua");
+            break;
+        default:
+            WriteInitialSaveMap();
+    }
+    // The kernel steps are done one after the other.
+    installation_state_ = static_cast<InstallationSteps>(installation_state_ + 1);
+}
+
+void DataInstaller::WriteInitialSaveMap(){
+    Say("Extracting the initial savemap...");
+    kernel_installer_->ReadInitialSaveMap();
+    const auto exe_path = disk_image_.filePath("ff7.exe");
+    if (exe_path) kernel_installer_->ReadRecruitRecords(*exe_path);
+    else Say("ff7.exe not found, Cait Sith and Vincent won't have their data.");
+    kernel_installer_->WriteInitialSaveMap(data_dir_ + "kernel/initial_savemap.lua");
+}
+
+void DataInstaller::ImageStep(){
+    // The fonts are always needed, even if the rest of the images are skipped.
+    Say("Extracting fonts...");
+    if (!media_installer_->InstallFonts())
+        Say("WARNING: Couldn't read WINDOW.BIN, the game won't be able to show text.");
+    if (!options_.images){
+        Skip("images", MEDIA_SOUNDS_INIT);
+        return;
+    }
+    Say("Extracting game images...");
+    media_installer_->InstallSprites();
+    installation_state_ = MEDIA_SOUNDS_INIT;
+}
+
+void DataInstaller::SoundStep(){
+    switch (installation_state_){
         case MEDIA_SOUNDS_INIT:
         case MEDIA_SOUNDS_INIT:
-            // Skip sounds if option is set.
             if (!options_.sounds){
             if (!options_.sounds){
-                write_output_line_("Skipping sound effects installation...", 2, true);
-                installation_state_ = MEDIA_MUSICS_INIT;
-                return CalcProgress();
+                Skip("sound effects", MEDIA_MUSICS_INIT);
+                break;
             }
             }
-            write_output_line_("Extracting sounds...", 2, true);
-            substeps_ = media_installer_->InstallSoundsInit();
-            installation_state_ = MEDIA_SOUNDS;
-            cur_substep_ = 0;
-            return CalcProgress();
+            Say("Extracting sounds...");
+            Repeat(media_installer_->InstallSoundsInit(), MEDIA_SOUNDS);
+            break;
         case MEDIA_SOUNDS:
         case MEDIA_SOUNDS:
-            if (media_installer_->InstallSounds() == true) installation_state_ = MEDIA_SOUNDS_INDEX;
-            else cur_substep_ ++;
-            return CalcProgress();
-        case MEDIA_SOUNDS_INDEX:
-            // The index is written when the conversions, which run in the background, have finished.
+            if (media_installer_->InstallSounds()) installation_state_ = MEDIA_SOUNDS_INDEX;
+            else ++ cur_substep_;
+            break;
+        default:
+            // The index is written when the conversions, which run in the background, have
+            // finished.
             installation_state_ = MEDIA_MUSICS_INIT;
             installation_state_ = MEDIA_MUSICS_INIT;
-            return CalcProgress();
+    }
+}
+
+void DataInstaller::MusicStep(){
+    switch (installation_state_){
         case MEDIA_MUSICS_INIT:
         case MEDIA_MUSICS_INIT:
-            // Skip music if option is set.
             if (!options_.music){
             if (!options_.music){
-                write_output_line_("Skipping music tracks installation...", 2, true);
-                installation_state_ = FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT;
-                return CalcProgress();
+                Skip("music tracks", FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT);
+                break;
             }
             }
-            write_output_line_("Extracting music...", 2, true);
-            substeps_ = media_installer_->InstallMusicsInit();
-            installation_state_ = MEDIA_MUSICS;
-            cur_substep_ = 0;
-            return CalcProgress();
+            Say("Extracting music...");
+            Repeat(media_installer_->InstallMusicsInit(), MEDIA_MUSICS);
+            break;
         case MEDIA_MUSICS:
         case MEDIA_MUSICS:
-            if (media_installer_->InstallMusics() == true) installation_state_ = MEDIA_MUSICS_HQ;
-            else cur_substep_ ++;
-            return CalcProgress();
+            if (media_installer_->InstallMusics()) installation_state_ = MEDIA_MUSICS_HQ;
+            else ++ cur_substep_;
+            break;
         case MEDIA_MUSICS_HQ:
         case MEDIA_MUSICS_HQ:
-            substeps_ = 0;
-            cur_substep_ = 0;
             media_installer_->InstallHQMusics();
             media_installer_->InstallHQMusics();
-            installation_state_ = MEDIA_MUSICS_INDEX;
-            return CalcProgress();
-        case MEDIA_MUSICS_INDEX:
-            // The index is written when the conversions, which run in the background, have finished.
+            Repeat(0, MEDIA_MUSICS_INDEX);
+            break;
+        default:
+            // The index is written when the conversions, which run in the background, have
+            // finished.
             installation_state_ = FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT;
             installation_state_ = FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT;
-            return CalcProgress();
+    }
+}
+
+void DataInstaller::FieldStep(){
+    switch (installation_state_){
         case FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT:
         case FIELD_SPAWN_POINTS_AND_SCALE_FACTORS_INIT:
-            // Skip fields if option is set.
             if (!options_.fields && !options_.wm){
             if (!options_.fields && !options_.wm){
-                write_output_line_("Skipping field maps installation...", 2, true);
-                installation_state_ = WM_INIT;
-                return CalcProgress();
+                Skip("field maps", WM_INIT);
+                break;
             }
             }
-            // The world map scripts need the field list and scale factors even if the fields aren't converted.
-            if (!options_.fields) write_output_line_("Collecting field data for the world map...", 2, true);
-            else write_output_line_("Collecting spawn points and scale factors...", 2, true);
-            substeps_ = field_installer_->CollectSpawnAndScaleFactorsInit(application_.ResMgr());
-            cur_substep_ = 0;
-            installation_state_ = FIELD_SPAWN_POINTS_AND_SCALE_FACTORS;
-            return CalcProgress();
+            // The world map scripts need the field list and scale factors even if the fields
+            // aren't converted.
+            if (!options_.fields) Say("Collecting field data for the world map...");
+            else Say("Collecting spawn points and scale factors...");
+            Repeat(
+              field_installer_->CollectSpawnAndScaleFactorsInit(application_.ResMgr()),
+              FIELD_SPAWN_POINTS_AND_SCALE_FACTORS
+            );
+            break;
         case FIELD_SPAWN_POINTS_AND_SCALE_FACTORS:
         case FIELD_SPAWN_POINTS_AND_SCALE_FACTORS:
             field_installer_->CollectSpawnAndScaleFactors(cur_substep_);
             field_installer_->CollectSpawnAndScaleFactors(cur_substep_);
-            cur_substep_ ++;
-            if (cur_substep_ == substeps_){
-                cur_substep_ = 0;
-                if (options_.fields) installation_state_ = FIELD_CONVERT;
-                else{
-                    // Fields are not converted: use the ones already installed.
-                    write_output_line_("Skipping field maps installation...", 2, true);
-                    field_installer_->LoadInstalledFields();
-                    installation_state_ = WM_INIT;
-                }
+            if (!NextSubstep()) break;
+            cur_substep_ = 0;
+            if (options_.fields) installation_state_ = FIELD_CONVERT;
+            else{
+                // Fields are not converted: use the ones already installed.
+                field_installer_->LoadInstalledFields();
+                Skip("field maps", WM_INIT);
             }
             }
-            return CalcProgress();
+            break;
         case FIELD_CONVERT:
         case FIELD_CONVERT:
             field_installer_->Convert(cur_substep_);
             field_installer_->Convert(cur_substep_);
-            cur_substep_ ++;
-            if (cur_substep_ == substeps_){
-                installation_state_ = FIELD_WRITE_INIT;
+            if (NextSubstep()){
                 cur_substep_ = 0;
                 cur_substep_ = 0;
+                installation_state_ = FIELD_WRITE_INIT;
             }
             }
-            return CalcProgress();
+            break;
         case FIELD_WRITE_INIT:
         case FIELD_WRITE_INIT:
-            write_output_line_("Writing fields...", 2, true);
-            substeps_ = field_installer_->WriteInit();
-            cur_substep_ = 0;
-            installation_state_ = FIELD_WRITE;
-            return CalcProgress();
+            Say("Writing fields...");
+            Repeat(field_installer_->WriteInit(), FIELD_WRITE);
+            break;
         case FIELD_WRITE:
         case FIELD_WRITE:
             field_installer_->Write(cur_substep_);
             field_installer_->Write(cur_substep_);
-            cur_substep_ ++;
-            if (cur_substep_ == substeps_){
-                installation_state_ = FIELD_WRITE_END;
+            if (NextSubstep()){
                 cur_substep_ = 0;
                 cur_substep_ = 0;
+                installation_state_ = FIELD_WRITE_END;
             }
             }
-            return CalcProgress();
-        case FIELD_WRITE_END:
+            break;
+        default:
             field_installer_->WriteEnd();
             field_installer_->WriteEnd();
             installation_state_ = FIELD_CONVERT_MODELS_INIT;
             installation_state_ = FIELD_CONVERT_MODELS_INIT;
-            return CalcProgress();
-        case FIELD_CONVERT_MODELS_INIT:
-            // Skip fields if option is set.
-            if (!options_.field_models){
-                write_output_line_("Skipping field maps installation...", 2, true);
-                installation_state_ = WM_INIT;
-                return CalcProgress();
-            }
-            write_output_line_("Converting field models...", 2, true);
-            UseModelParts("temp/char/");
-            field_model_names_ = field_installer_->ConvertModelsInit();
-            substeps_ = field_model_names_.size();
-            cur_substep_ = 0;
-            installation_state_ = FIELD_CONVERT_MODELS;
-            return CalcProgress();
-        case FIELD_CONVERT_MODELS:
-            field_installer_->ConvertModels(field_model_names_[cur_substep_]);
-            cur_substep_ ++;
-            if (cur_substep_ == substeps_){
-                installation_state_ = WM_INIT;
-                cur_substep_ = 0;
-            }
-            return CalcProgress();
+    }
+}
+
+void DataInstaller::FieldModelStep(){
+    if (installation_state_ == FIELD_CONVERT_MODELS_INIT){
+        if (!options_.field_models){
+            Skip("field models", WM_INIT);
+            return;
+        }
+        Say("Converting field models...");
+        UseModelParts("temp/char/");
+        field_model_names_ = field_installer_->ConvertModelsInit();
+        Repeat(static_cast<int>(field_model_names_.size()), FIELD_CONVERT_MODELS);
+        return;
+    }
+    // There are no models if no field was converted.
+    if (cur_substep_ < substeps_)
+        field_installer_->ConvertModels(field_model_names_[cur_substep_]);
+    if (NextSubstep()){
+        cur_substep_ = 0;
+        installation_state_ = WM_INIT;
+    }
+}
+
+void DataInstaller::WorldStep(){
+    switch (installation_state_){
         case WM_INIT:
         case WM_INIT:
-            // Skip world map if option set
             if (!options_.wm){
             if (!options_.wm){
-                write_output_line_("Skipping world map data installation...", 2, true);
-                installation_state_ = CLEAN;
-                return CalcProgress();
+                Skip("world map data", CLEAN);
+                break;
             }
             }
-            write_output_line_("Extracting world map data...", 2, true);
+            Say("Extracting world map data...");
             UseModelParts("temp/world_models/");
             UseModelParts("temp/world_models/");
             world_installer_->InstallMaps();
             world_installer_->InstallMaps();
-            substeps_ = 0;
-            cur_substep_ = 0;
-            installation_state_ = WM_SCRIPTS;
-            return CalcProgress();
-        case WM_SCRIPTS:
-            {
-                std::string script_error;
-                if (world_installer_->GenerateScripts(
-                  field_installer_->GetMapList(), field_installer_->GetFieldScaleFactors(),
-                  field_installer_->GetConvertedMapList(), script_error
-                )){
-                    write_output_line_("Generated world-map scripts.", 2, true);
-                }
-                else{
-                    write_output_line_("Could not generate world-map scripts: " + script_error, 2, true);
-                }
-                installation_state_ = WM_MODELS;
-                return CalcProgress();
-            }
-        case WM_MODELS:
-            if (!options_.wm_models) write_output_line_("Skipping world map model installation...", 2, true);
-            else world_installer_->ProcessModels();
-            installation_state_ = CLEAN;
-            return CalcProgress();
-        case CLEAN:
-            // The audio conversions run in the background while the other steps are done.
-            if (media_installer_){
-                write_output_line_("Waiting for the audio conversions to finish...", 2, true);
-                media_installer_->WaitForConversions();
-                // Now the files that exist are known, so the indexes list only those.
-                if (options_.sounds){
-                    write_output_line_("Building sound index...", 2, true);
-                    media_installer_->WriteSoundIndex();
-                }
-                if (options_.music){
-                    write_output_line_("Building music track index...", 2, true);
-                    media_installer_->WriteMusicsIndex();
-                }
-            }
-            write_output_line_("Cleaning up...", 2, true);
-            if (!options_.keep_originals) RemoveOriginals();
-            installation_state_ = DONE;
-            return CalcProgress();
-        case DONE:
-            write_output_line_("Installation complete", 2, true);
-            return 100;
+            Repeat(0, WM_SCRIPTS);
+            break;
+        case WM_SCRIPTS:{
+            std::string error;
+            if (world_installer_->GenerateScripts(
+              field_installer_->GetMapList(), field_installer_->GetFieldScaleFactors(),
+              field_installer_->GetConvertedMapList(), error
+            )) Say("Generated world-map scripts.");
+            else Say("Could not generate world-map scripts: " + error);
+            installation_state_ = WM_MODELS;
+            break;
+        }
         default:
         default:
-            return 100;
+            if (!options_.wm_models) Skip("world map model", CLEAN);
+            else{
+                world_installer_->ProcessModels();
+                installation_state_ = CLEAN;
+            }
+    }
+}
+
+void DataInstaller::CleanStep(){
+    // The audio conversions run in the background while the other steps are done.
+    if (media_installer_){
+        Say("Waiting for the audio conversions to finish...");
+        media_installer_->WaitForConversions();
+        // Now the files that exist are known, so the indexes list only those.
+        if (options_.sounds){
+            Say("Building sound index...");
+            media_installer_->WriteSoundIndex();
+        }
+        if (options_.music){
+            Say("Building music track index...");
+            media_installer_->WriteMusicsIndex();
+        }
     }
     }
+    Say("Cleaning up...");
+    if (!options_.keep_originals) RemoveOriginals();
+    installation_state_ = DONE;
 }
 }
 
 
 const float DataInstaller::CalcProgress(){
 const float DataInstaller::CalcProgress(){
+    // The end is reported when the next call finds the installation done.
+    if (installation_state_ >= STATE_COUNT) return 99.9f;
     float weight_total = 0; // Sum of every step weight.
     float weight_total = 0; // Sum of every step weight.
     float weight_done = 0; // Sum of every previous step weight.
     float weight_done = 0; // Sum of every previous step weight.
     float weight = static_cast<float>(step_weight_[installation_state_]); // Weight of current step.
     float weight = static_cast<float>(step_weight_[installation_state_]); // Weight of current step.

+ 100 - 8
src/installer/steps/DataInstaller.h

@@ -104,14 +104,6 @@ class DataInstaller{
 
 
         };
         };
 
 
-        /**
-         * The scale factor for line point coordinates.
-         *
-         * When a LINE opcode is found in the game scripts, the line points X
-         * and Y coordinates must be scaled down by this factor.
-         */
-        static float LINE_SCALE_FACTOR;
-
         /**
         /**
          * Installer constructor
          * Installer constructor
          *
          *
@@ -449,6 +441,106 @@ class DataInstaller{
             DONE = 100,
             DONE = 100,
         };
         };
 
 
+        /**
+         * Writes a message to the installation output.
+         *
+         * @param[in] message The message.
+         */
+        void Say(const std::string& message);
+
+        /**
+         * Skips a part of the installation.
+         *
+         * @param[in] part What is skipped.
+         * @param[in] next The step to go on with.
+         */
+        void Skip(const std::string& part, const InstallationSteps next);
+
+        /**
+         * Goes to a step that is done several times, from its first substep.
+         *
+         * @param[in] substeps Number of times.
+         * @param[in] next The step.
+         */
+        void Repeat(const int substeps, const InstallationSteps next);
+
+        /**
+         * Counts a substep as done.
+         *
+         * @return True if it was the last one.
+         */
+        bool NextSubstep();
+
+        /**
+         * Runs a step of the preparation: the folders and the installers of each part.
+         */
+        void PreparationStep();
+
+        /**
+         * Creates the installers of each part.
+         */
+        void CreateInstallers();
+
+        /**
+         * Runs a step of the battle data: scenes, attacks, enemies and formations.
+         */
+        void BattleDataStep();
+
+        /**
+         * Runs a step of the battle models.
+         */
+        void BattleModelStep();
+
+        /**
+         * Runs a step of the spell and effect models.
+         */
+        void SpellModelStep();
+
+        /**
+         * Runs a step of the kernel data.
+         */
+        void KernelStep();
+
+        /**
+         * Extracts the initial savemap, with the data of the characters that join later.
+         */
+        void WriteInitialSaveMap();
+
+        /**
+         * Installs the fonts and the images.
+         */
+        void ImageStep();
+
+        /**
+         * Runs a step of the sound effects.
+         */
+        void SoundStep();
+
+        /**
+         * Runs a step of the music.
+         */
+        void MusicStep();
+
+        /**
+         * Runs a step of the field maps.
+         */
+        void FieldStep();
+
+        /**
+         * Runs a step of the field models.
+         */
+        void FieldModelStep();
+
+        /**
+         * Runs a step of the world map.
+         */
+        void WorldStep();
+
+        /**
+         * Waits for the conversions, writes the audio indexes and removes the temporary files.
+         */
+        void CleanStep();
+
         /**
         /**
          * Current installation status.
          * Current installation status.
          */
          */