diff --git a/src/mappers/provinces/province_mapper_importer.cpp b/src/mappers/provinces/province_mapper_importer.cpp index e39c4632..bfff7090 100644 --- a/src/mappers/provinces/province_mapper_importer.cpp +++ b/src/mappers/provinces/province_mapper_importer.cpp @@ -62,13 +62,12 @@ std::map GenerateProvinceToStateMap( } void AreVic3ProvincesFromSameState(const std::vector& provinces_from_map, - std::map province_to_state_map) + const std::map& province_to_state_map) { std::set state_names; for (const auto& province: provinces_from_map) { - auto linked_state = province_to_state_map.find(province); - if (linked_state != province_to_state_map.end()) + if (auto linked_state = province_to_state_map.find(province); linked_state != province_to_state_map.end()) { state_names.insert(linked_state->second); } @@ -78,11 +77,30 @@ void AreVic3ProvincesFromSameState(const std::vector& provinces_fro { for (const auto& province_from_map: provinces_from_map) { - Log(LogLevel::Warning) << fmt::format( + Log(LogLevel::Debug) << fmt::format( "Province {} is designated as part of {} in Vic3 data and is placed in a mapping with provinces from " "other states.", province_from_map, - province_to_state_map[province_from_map]); + province_to_state_map.at(province_from_map)); + } + } +} + + +void IsMappingInWrongRegion(std::string_view current_region, + const std::vector& provinces_from_map, + const std::map& province_to_state_map) +{ + for (const auto& province: provinces_from_map) + { + auto linked_state = province_to_state_map.find(province); + if (linked_state != province_to_state_map.end() && linked_state->second != current_region) + { + Log(LogLevel::Debug) << fmt::format( + "Province {} is designated as part of {} in Vic3 data and is placed in a mapping in the {} region.", + province, + province_to_state_map.at(province), + current_region); } } } @@ -136,6 +154,10 @@ mappers::ProvinceMapperImporter::ProvinceMapperImporter(const commonItems::ModFi mapping_parser_.registerKeyword("link", [this, vic3_state_regions](std::istream& input_stream) { const auto the_mapping = mapping_importer_.ImportProvinceMapping(input_stream); + if (the_mapping.comment.has_value()) + { + current_region_ = the_mapping.comment.value(); + } if (the_mapping.vic3_provinces.empty() && the_mapping.hoi4_provinces.empty()) { return; @@ -143,11 +165,10 @@ mappers::ProvinceMapperImporter::ProvinceMapperImporter(const commonItems::ModFi if (!vic3_state_regions.empty()) { - auto vic3_province = the_mapping.vic3_provinces; - AreVic3ProvincesFromSameState(vic3_province, province_to_state_map_); + AreVic3ProvincesFromSameState(the_mapping.vic3_provinces, province_to_state_map_); + IsMappingInWrongRegion(current_region_, the_mapping.vic3_provinces, province_to_state_map_); } - for (auto color: the_mapping.vic3_provinces) { const auto [unused, success] = vic3_to_hoi4_province_map_.emplace(color, the_mapping.hoi4_provinces); @@ -171,6 +192,7 @@ mappers::ProvinceMapperImporter::ProvinceMapperImporter(const commonItems::ModFi mappers::ProvinceMapper mappers::ProvinceMapperImporter::ImportProvinceMappings() { + Log(LogLevel::Info) << "Importing province mappings."; vic3_to_hoi4_province_map_.clear(); hoi4_to_vic3_province_map_.clear(); diff --git a/src/mappers/provinces/province_mapper_importer.h b/src/mappers/provinces/province_mapper_importer.h index 84a4a66a..b2a475a4 100644 --- a/src/mappers/provinces/province_mapper_importer.h +++ b/src/mappers/provinces/province_mapper_importer.h @@ -33,6 +33,8 @@ class ProvinceMapperImporter commonItems::parser mapping_parser_; ProvinceMappingImporter mapping_importer_; + std::string current_region_; + Vic3ToHoi4ProvinceMapping vic3_to_hoi4_province_map_; Hoi4ToVic3ProvinceMapping hoi4_to_vic3_province_map_; std::map province_to_state_map_; diff --git a/src/mappers/provinces/province_mapping.h b/src/mappers/provinces/province_mapping.h index badc976a..63f572c5 100644 --- a/src/mappers/provinces/province_mapping.h +++ b/src/mappers/provinces/province_mapping.h @@ -3,6 +3,8 @@ +#include +#include #include @@ -14,6 +16,7 @@ struct ProvinceMapping { std::vector vic3_provinces; std::vector hoi4_provinces; + std::optional comment; }; } // namespace mappers diff --git a/src/mappers/provinces/province_mapping_importer.cpp b/src/mappers/provinces/province_mapping_importer.cpp index c225ce73..c0d268d7 100644 --- a/src/mappers/provinces/province_mapping_importer.cpp +++ b/src/mappers/provinces/province_mapping_importer.cpp @@ -18,7 +18,17 @@ mappers::ProvinceMappingImporter::ProvinceMappingImporter() parser_.registerKeyword("hoi4", [this](std::istream& theStream) { hoi4_provinces_.push_back(commonItems::getInt(theStream)); }); - parser_.registerKeyword("comment", commonItems::ignoreItem); + parser_.registerKeyword("comment", [this](std::istream& the_stream) { + const std::string raw_string = commonItems::getString(the_stream); + if (const size_t start = raw_string.find_first_not_of("* "); start != std::string::npos) + { + comment_ = raw_string.substr(start, raw_string.length()); + if (const size_t last = comment_.value().find_last_not_of("* "); last != std::string::npos) + { + comment_ = comment_.value().substr(0, last + 1); + } + } + }); parser_.IgnoreAndLogUnregisteredItems(); } @@ -27,8 +37,9 @@ mappers::ProvinceMapping mappers::ProvinceMappingImporter::ImportProvinceMapping { vic3_provinces_.clear(); hoi4_provinces_.clear(); + comment_.reset(); parser_.parseStream(input_stream); - return ProvinceMapping{vic3_provinces_, hoi4_provinces_}; + return ProvinceMapping{.vic3_provinces = vic3_provinces_, .hoi4_provinces = hoi4_provinces_, .comment = comment_}; } \ No newline at end of file diff --git a/src/mappers/provinces/province_mapping_importer.h b/src/mappers/provinces/province_mapping_importer.h index e7cc8bc1..e698a9f8 100644 --- a/src/mappers/provinces/province_mapping_importer.h +++ b/src/mappers/provinces/province_mapping_importer.h @@ -3,7 +3,9 @@ -#include +#include +#include +#include #include "external/commonItems/Parser.h" #include "src/mappers/provinces/province_mapping.h" @@ -24,6 +26,7 @@ class ProvinceMappingImporter std::vector vic3_provinces_; std::vector hoi4_provinces_; + std::optional comment_; }; } // namespace mappers diff --git a/src/mappers/provinces/province_mapping_importer_tests.cpp b/src/mappers/provinces/province_mapping_importer_tests.cpp index 25d3dc78..b4dafb80 100644 --- a/src/mappers/provinces/province_mapping_importer_tests.cpp +++ b/src/mappers/provinces/province_mapping_importer_tests.cpp @@ -51,4 +51,25 @@ TEST(MappersProvincesProvinceMappingImporterTests, HoI4ProvincesCanBeAdded) EXPECT_THAT(mapping.hoi4_provinces, testing::ElementsAre(42, 144)); } + +TEST(MappersProvincesProvinceMappingImporterTests, CommentDefaultsToNullopt) +{ + std::stringstream input; + + const auto mapping = ProvinceMappingImporter{}.ImportProvinceMapping(input); + + EXPECT_FALSE(mapping.comment.has_value()); +} + + +TEST(MappersProvincesProvinceMappingImporterTests, CommentCanBeSet) +{ + std::stringstream input; + input << R"(= { comment = "test_comment" })"; + + const auto mapping = ProvinceMappingImporter{}.ImportProvinceMapping(input); + + EXPECT_EQ(mapping.comment.value_or(""), "test_comment"); +} + } // namespace mappers \ No newline at end of file