Skip to content

Commit

Permalink
Plug in installation & savegame version verification from EU4ToVic3. (#…
Browse files Browse the repository at this point in the history
…643)

* Plug in savegame  version verification from EU4ToVic3.

* clang
  • Loading branch information
Zemurin authored Sep 8, 2024
1 parent 6224862 commit aa9f709
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 81 deletions.
2 changes: 1 addition & 1 deletion external/Fronter
Submodule Fronter updated 1 files
+1 −1 external/commonItems
2 changes: 1 addition & 1 deletion external/commonItems
82 changes: 82 additions & 0 deletions src/configuration/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <string>

#include "external/fmt/include/fmt/format.h"


namespace configuration
{
Expand All @@ -20,11 +22,91 @@ struct Configuration
bool debug = false;
std::string output_name;
bool dynamic_resources = false;

void verifyVic3Path()
{
if (!commonItems::DoesFolderExist(vic3_directory))
{
throw std::runtime_error(fmt::format("Victoria 3 path {} doesn't exist.", vic3_directory));
}
if (!commonItems::DoesFileExist(vic3_directory + "/binaries/victoria3.exe") &&
!commonItems::DoesFileExist(vic3_directory + "/binaries/victoria3"))
{
throw std::runtime_error(fmt::format("{} does not contain Victoria 3.", vic3_directory));
}
}

void verifyHOI4Path()
{
if (!commonItems::DoesFolderExist(hoi4_directory))
{
throw std::runtime_error(fmt::format("Hearts of Iron 4 path {} doesn't exist.", hoi4_directory));
}
if (!commonItems::DoesFileExist(hoi4_directory + "/hoi4.exe") &&
!commonItems::DoesFileExist(hoi4_directory + "/hoi4"))
{
throw std::runtime_error(fmt::format("{} does not contain Hearts of Iron 4.", hoi4_directory));
}
}

void verifyVic3Version(const commonItems::ConverterVersion& converterVersion) const
{
const auto V3Version =
GameVersion::extractVersionFromLauncher(vic3_directory + "/launcher/launcher-settings.json");
if (!V3Version)
{
Log(LogLevel::Error) << "Vic3 version could not be determined, proceeding blind!";
return;
}

Log(LogLevel::Info) << "Vic3 version: " << V3Version->toShortString();

if (converterVersion.getMinSource() > *V3Version)
{
Log(LogLevel::Error) << "Vic3 version is v" << V3Version->toShortString() << ", converter requires minimum v"
<< converterVersion.getMinSource().toShortString() << "!";
throw std::runtime_error("Converter vs Vic3 installation mismatch!");
}

if (!converterVersion.getMaxSource().isLargerishThan(*V3Version))
{
Log(LogLevel::Error) << "Vic3 version is v" << V3Version->toShortString() << ", converter requires maximum v"
<< converterVersion.getMaxSource().toShortString() << "!";
throw std::runtime_error("Converter vs Vic3 installation mismatch!");
}
}

void verifyHOI4Version(const commonItems::ConverterVersion& converterVersion) const
{
const auto HOI4Version = GameVersion::extractVersionFromLauncher(hoi4_directory + "/launcher-settings.json");
if (!HOI4Version)
{
Log(LogLevel::Error) << "HOI4 version could not be determined, proceeding blind!";
return;
}

Log(LogLevel::Info) << "HOI4 version: " << HOI4Version->toShortString();

if (converterVersion.getMinTarget() > *HOI4Version)
{
Log(LogLevel::Error) << "HOI4 version is v" << HOI4Version->toShortString() << ", converter requires minimum v"
<< converterVersion.getMinTarget().toShortString() << "!";
throw std::runtime_error("Converter vs HOI4 installation mismatch!");
}

if (!converterVersion.getMaxTarget().isLargerishThan(*HOI4Version))
{
Log(LogLevel::Error) << "HOI4 version is v" << HOI4Version->toShortString() << ", converter requires maximum v"
<< converterVersion.getMaxTarget().toShortString() << "!";
throw std::runtime_error("Converter vs HOI4 installation mismatch!");
}
}
};

constexpr Configuration defaultConfig = {};
constexpr Configuration debugConfig = {.debug = true};


} // namespace configuration


Expand Down
29 changes: 8 additions & 21 deletions src/configuration/configuration_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,17 @@ std::string DetermineOutputName(std::string_view save_name)
} // namespace


configuration::Configuration configuration::ImportConfiguration(std::string_view configuration_file)
configuration::Configuration configuration::ImportConfiguration(std::string_view configuration_file,
const commonItems::ConverterVersion& converter_version)
{
commonItems::parser configuration_parser;
Configuration configuration;

configuration_parser.registerKeyword("Vic3directory", [&configuration](std::istream& stream) {
configuration_parser.registerKeyword("Vic3directory", [&configuration, &converter_version](std::istream& stream) {
configuration.vic3_directory = commonItems::getString(stream);
if (!commonItems::DoesFolderExist(configuration.vic3_directory))
{
throw std::runtime_error(fmt::format("Victoria 3 path {} doesn't exist.", configuration.vic3_directory));
}
if (!commonItems::DoesFileExist(configuration.vic3_directory + "/binaries/victoria3.exe") &&
!commonItems::DoesFileExist(configuration.vic3_directory + "/binaries/victoria3"))
{
throw std::runtime_error(fmt::format("{} does not contain Victoria 3.", configuration.vic3_directory));
}
Log(LogLevel::Info) << "\tVictoria 3 install path is " << configuration.vic3_directory;
configuration.verifyVic3Path();
configuration.verifyVic3Version(converter_version);
});
configuration_parser.registerKeyword("Vic3SteamModDirectory", [&configuration](std::istream& stream) {
configuration.vic3_steam_mod_path = commonItems::getString(stream);
Expand All @@ -53,18 +47,11 @@ configuration::Configuration configuration::ImportConfiguration(std::string_view
configuration.vic3_mod_path = commonItems::getString(stream);
Log(LogLevel::Info) << "\tVictoria 3 mod path is " << configuration.vic3_mod_path;
});
configuration_parser.registerKeyword("HoI4directory", [&configuration](std::istream& stream) {
configuration_parser.registerKeyword("HoI4directory", [&configuration, &converter_version](std::istream& stream) {
configuration.hoi4_directory = commonItems::getString(stream);
if (!commonItems::DoesFolderExist(configuration.hoi4_directory))
{
throw std::runtime_error(fmt::format("Hearts of Iron 4 path {} doesn't exist.", configuration.hoi4_directory));
}
if (!commonItems::DoesFileExist(configuration.hoi4_directory + "/hoi4.exe") &&
!commonItems::DoesFileExist(configuration.hoi4_directory + "/hoi4"))
{
throw std::runtime_error(fmt::format("{} does not contain Hearts of Iron 4.", configuration.hoi4_directory));
}
Log(LogLevel::Info) << "\tHearts of Iron 4 install path is " << configuration.hoi4_directory;
configuration.verifyHOI4Path();
configuration.verifyHOI4Version(converter_version);
});
configuration_parser.registerKeyword("targetGameModPath", [&configuration](std::istream& stream) {
configuration.hoi4_mod_path = commonItems::getString(stream);
Expand Down
3 changes: 2 additions & 1 deletion src/configuration/configuration_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
namespace configuration
{

[[nodiscard]] Configuration ImportConfiguration(std::string_view configuration_file);
[[nodiscard]] Configuration ImportConfiguration(std::string_view configuration_file,
const commonItems::ConverterVersion& converter_version);

} // namespace configuration

Expand Down
30 changes: 20 additions & 10 deletions src/configuration/configuration_importer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace configuration

TEST(ConfigurationTest, DefaultsAreDefaulted)
{
const auto configuration = ImportConfiguration("test_files/configuration/blank_configuration.txt");
const auto configuration =
ImportConfiguration("test_files/configuration/blank_configuration.txt", commonItems::ConverterVersion());

EXPECT_TRUE(configuration.vic3_directory.empty());
EXPECT_TRUE(configuration.vic3_steam_mod_path.empty());
Expand All @@ -23,35 +24,40 @@ TEST(ConfigurationTest, DefaultsAreDefaulted)

TEST(ConfigurationTest, ExceptionForMissingVic3Directory)
{
EXPECT_THROW(const auto _ = ImportConfiguration("test_files/configuration/missing_vic3_directory.txt"),
EXPECT_THROW(const auto _ = ImportConfiguration("test_files/configuration/missing_vic3_directory.txt",
commonItems::ConverterVersion()),
std::runtime_error);
}


TEST(ConfigurationTest, ExceptionForBadVic3Directory)
{
EXPECT_THROW(const auto _ = ImportConfiguration("test_files/configuration/bad_vic3_directory.txt"),
EXPECT_THROW(const auto _ = ImportConfiguration("test_files/configuration/bad_vic3_directory.txt",
commonItems::ConverterVersion()),
std::runtime_error);
}


TEST(ConfigurationTest, ExceptionForMissingHoI4Directory)
{
EXPECT_THROW(const auto _ = ImportConfiguration("test_files/configuration/missing_hoi4_directory.txt"),
EXPECT_THROW(const auto _ = ImportConfiguration("test_files/configuration/missing_hoi4_directory.txt",
commonItems::ConverterVersion()),
std::runtime_error);
}


TEST(ConfigurationTest, ExceptionForBadHoI4Directory)
{
EXPECT_THROW(const auto _ = ImportConfiguration("test_files/configuration/bad_hoi4_directory.txt"),
EXPECT_THROW(const auto _ = ImportConfiguration("test_files/configuration/bad_hoi4_directory.txt",
commonItems::ConverterVersion()),
std::runtime_error);
}


TEST(ConfigurationTest, ItemsCanBeImported)
{
const auto configuration = ImportConfiguration("test_files/configuration/test_configuration.txt");
const auto configuration =
ImportConfiguration("test_files/configuration/test_configuration.txt", commonItems::ConverterVersion());

EXPECT_EQ(configuration.vic3_directory, R"(test_files/test_folders/vic3_folder)");
EXPECT_EQ(configuration.vic3_steam_mod_path, "vic3_steam_mod_directory");
Expand All @@ -70,7 +76,8 @@ TEST(ConfigurationTest, ItemsAreLoggedWhenImported)
std::streambuf* cout_buffer = std::cout.rdbuf();
std::cout.rdbuf(log.rdbuf());

const auto _ = ImportConfiguration("test_files/configuration/test_configuration.txt");
const auto _ =
ImportConfiguration("test_files/configuration/test_configuration.txt", commonItems::ConverterVersion());

EXPECT_THAT(log.str(), testing::HasSubstr(R"(Victoria 3 install path is test_files/test_folders/vic3_folder)"));
EXPECT_THAT(log.str(), testing::HasSubstr(R"(Victoria 3 Steam mod path is vic3_steam_mod_directory)"));
Expand All @@ -88,22 +95,25 @@ TEST(ConfigurationTest, ItemsAreLoggedWhenImported)

TEST(ConfigurationTest, BadSaveNameThrowsException)
{
EXPECT_THROW(const auto _ = ImportConfiguration("test_files/configuration/bad_save_name.txt"),
EXPECT_THROW(const auto _ =
ImportConfiguration("test_files/configuration/bad_save_name.txt", commonItems::ConverterVersion()),
std::invalid_argument);
}


TEST(ConfigurationTest, OutputNameIsFromSave)
{
const auto configuration = ImportConfiguration("test_files/configuration/output_name_from_save_configuration.txt");
const auto configuration = ImportConfiguration("test_files/configuration/output_name_from_save_configuration.txt",
commonItems::ConverterVersion());

EXPECT_EQ(configuration.output_name, "test_save_with_spaces");
}


TEST(ConfigurationTest, CustomOutputOverridesSaveOutputName)
{
const auto configuration = ImportConfiguration("test_files/configuration/output_name_override.txt");
const auto configuration =
ImportConfiguration("test_files/configuration/output_name_override.txt", commonItems::ConverterVersion());

EXPECT_EQ(configuration.output_name, "path_has_not__been__removed__override_name_with_spaces");
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ int main()
converterVersion.loadVersion("../version.txt");
Log(LogLevel::Info) << converterVersion;

const auto configuration = configuration::ImportConfiguration("configuration.txt");
const auto configuration = configuration::ImportConfiguration("configuration.txt", converterVersion);

Log(LogLevel::Progress) << "0%";
ConvertVic3ToHoi4(configuration, converterVersion.getMaxTarget());
ConvertVic3ToHoi4(configuration, converterVersion);

return 0;
}
Expand Down
7 changes: 4 additions & 3 deletions src/vic3_to_hoi4_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@



void ConvertVic3ToHoi4(const configuration::Configuration& configuration, const GameVersion& game_version)
void ConvertVic3ToHoi4(const configuration::Configuration& configuration,
const commonItems::ConverterVersion& converter_version)
{
const commonItems::ModFilesystem hoi4_mod_filesystem(configuration.hoi4_directory, {});

auto hoi4_framework = std::async(std::launch::async, [&hoi4_mod_filesystem]() {
return hoi4::WorldFrameworkBuilder::CreateDefaultWorldFramework(hoi4_mod_filesystem).Build();
});

const auto source_world = vic3::ImportWorld(configuration);
const auto source_world = vic3::ImportWorld(configuration, converter_version);
auto world_mapper = mappers::WorldMapperBuilder::CreateDefaultMapper(hoi4_mod_filesystem, source_world).Build();
world_mapper.province_mapper.CheckAllVic3ProvincesMapped(
source_world.GetProvinceDefinitions().GetProvinceDefinitions());
const hoi4::World destination_world =
hoi4::ConvertWorld(hoi4_mod_filesystem, source_world, world_mapper, std::move(hoi4_framework), configuration);

out::ClearOutputFolder(configuration.output_name);
out::OutputMod(configuration.output_name, game_version);
out::OutputMod(configuration.output_name, converter_version.getMaxTarget());
out::OutputFlags(configuration.output_name, destination_world.GetCountries(), hoi4_mod_filesystem);
out::OutputWorld(configuration.output_name, destination_world);
Log(LogLevel::Progress) << "100%";
Expand Down
3 changes: 2 additions & 1 deletion src/vic3_to_hoi4_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@



void ConvertVic3ToHoi4(const configuration::Configuration& configuration, const GameVersion& game_version);
void ConvertVic3ToHoi4(const configuration::Configuration& configuration,
const commonItems::ConverterVersion& converter_version);



Expand Down
21 changes: 20 additions & 1 deletion src/vic3_world/world/vic3_world_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ void CheckProvinceTerrainsGrouping(const std::vector<std::string>& all_provinces
} // namespace


vic3::World vic3::ImportWorld(const configuration::Configuration& configuration)
vic3::World vic3::ImportWorld(const configuration::Configuration& configuration,
const commonItems::ConverterVersion& converter_version)
{
WorldOptions world_options;
Log(LogLevel::Info) << "*** Hello Vic3, loading World. ***";
Expand All @@ -390,6 +391,24 @@ vic3::World vic3::ImportWorld(const configuration::Configuration& configuration)
meta_parser.registerKeyword("mods", [&mod_names](std::istream& input_stream) {
mod_names = commonItems::stringList(input_stream).getStrings();
});
meta_parser.registerKeyword("version", [&converter_version](std::istream& input_stream) {
const auto str_version = commonItems::getString(input_stream);
GameVersion version = GameVersion(str_version);
Log(LogLevel::Info) << "Savegame version: " << version;

if (converter_version.getMinSource() > version)
{
Log(LogLevel::Error) << "Converter requires a minimum save from v"
<< converter_version.getMinSource().toShortString();
throw std::runtime_error("Savegame vs converter version mismatch!");
}
if (!converter_version.getMaxSource().isLargerishThan(version))
{
Log(LogLevel::Error) << "Converter requires a maximum save from v"
<< converter_version.getMaxSource().toShortString();
throw std::runtime_error("Savegame vs converter version mismatch!");
}
});
meta_parser.IgnoreUnregisteredItems();
meta_parser.parseStream(meta_stream);

Expand Down
3 changes: 2 additions & 1 deletion src/vic3_world/world/vic3_world_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace vic3
{


World ImportWorld(const configuration::Configuration& configuration);
World ImportWorld(const configuration::Configuration& configuration,
const commonItems::ConverterVersion& converter_version);


} // namespace vic3
Expand Down
Loading

0 comments on commit aa9f709

Please sign in to comment.