diff options
author | Hop311 <hop3114@gmail.com> | 2023-09-16 17:27:41 +0200 |
---|---|---|
committer | Hop311 <hop3114@gmail.com> | 2023-09-16 17:28:01 +0200 |
commit | f95902fdd85f86c10b9c300099c67312fc81f11b (patch) | |
tree | 1655119602f39a366de8248cb4651321b6dfdda0 /src/openvic-simulation/dataloader | |
parent | 64d134ad20c333afa60373526d6fab27a07c6adc (diff) |
Scaffolding for `positions.txt` + modifier loading
Diffstat (limited to 'src/openvic-simulation/dataloader')
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.cpp | 28 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.hpp | 6 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.hpp | 1 |
3 files changed, 21 insertions, 14 deletions
diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index e85f499..eae3f84 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -50,7 +50,6 @@ fs::path Dataloader::lookup_file(fs::path const& path) const { } static bool contains_file_with_name(Dataloader::path_vector_t const& paths, fs::path const& name) { - for (fs::path const& path : paths) { if (path.filename() == name) return true; } @@ -76,8 +75,7 @@ Dataloader::path_vector_t Dataloader::lookup_files_in_dir(fs::path const& path, return ret; } -bool Dataloader::apply_to_files_in_dir(fs::path const& path, fs::path const& extension, std::function<bool(fs::path const&)> callback) const { - +bool Dataloader::apply_to_files_in_dir(fs::path const& path, fs::path const& extension, callback_t<fs::path const&> callback) const { bool ret = true; for (fs::path const& file : lookup_files_in_dir(path, extension)) { ret &= callback(file); @@ -140,6 +138,12 @@ static csv::Windows1252Parser _parse_csv(fs::path const& path) { return _run_ovdl_parser<csv::Windows1252Parser, &_csv_parse>(path); } +static callback_t<fs::path const&> _parse_defines_callback(node_callback_t callback) { + return [callback](fs::path const& path) -> bool { + return callback(_parse_defines(path).get_file_node()); + }; +} + bool Dataloader::_load_pop_types(PopManager& pop_manager, fs::path const& pop_type_directory) const { const bool ret = apply_to_files_in_dir(pop_type_directory, ".txt", [&pop_manager](fs::path const& file) -> bool { @@ -226,6 +230,11 @@ bool Dataloader::_load_map_dir(Map& map, fs::path const& map_directory) const { ret = false; } + if (!map.load_province_positions(_parse_defines(lookup_file(map_directory / positions)).get_file_node())) { + Logger::error("Failed to load province positions file!"); + ret = false; + } + if (!map.load_region_file(_parse_defines(lookup_file(map_directory / region)).get_file_node())) { Logger::error("Failed to load region file!"); ret = false; @@ -281,16 +290,11 @@ bool Dataloader::load_defines(GameManager& game_manager) const { bool Dataloader::load_pop_history(GameManager& game_manager, fs::path const& path) const { return apply_to_files_in_dir(path, ".txt", [&game_manager](fs::path const& file) -> bool { - return expect_dictionary( - [&game_manager](std::string_view province_key, ast::NodeCPtr province_node) -> bool { - Province* province = game_manager.map.get_province_by_identifier(province_key); - if (province == nullptr) { - Logger::error("Invalid province id: ", province_key); - return false; - } - return province->load_pop_list(game_manager.pop_manager, province_node); + return _parse_defines_callback(game_manager.map.expect_province_dictionary( + [&game_manager](Province& province, ast::NodeCPtr value) -> bool { + return province.load_pop_list(game_manager.pop_manager, value); } - )(_parse_defines(file).get_file_node()); + ))(file); } ); } diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index 2b358b6..b07248e 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -4,6 +4,8 @@ #include <functional> #include <vector> +#include "openvic-simulation/dataloader/NodeTools.hpp" + namespace OpenVic { namespace fs = std::filesystem; @@ -33,7 +35,7 @@ namespace OpenVic { fs::path lookup_file(fs::path const& path) const; path_vector_t lookup_files_in_dir(fs::path const& path, fs::path const& extension) const; bool apply_to_files_in_dir(fs::path const& path, fs::path const& extension, - std::function<bool(fs::path const&)> callback) const; + NodeTools::callback_t<fs::path const&> callback) const; bool load_defines(GameManager& game_manager) const; bool load_pop_history(GameManager& game_manager, fs::path const& path) const; @@ -46,7 +48,7 @@ namespace OpenVic { }; /* Args: key, locale, localisation */ - using localisation_callback_t = std::function<bool(std::string_view, locale_t, std::string_view)>; + using localisation_callback_t = NodeTools::callback_t<std::string_view, locale_t, std::string_view>; bool load_localisation_files(localisation_callback_t callback, fs::path const& localisation_dir = "localisation"); }; } diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index a68e922..8508087 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -20,6 +20,7 @@ namespace OpenVic { constexpr bool success_callback(ast::NodeCPtr) { return true; } using key_value_callback_t = callback_t<std::string_view, ast::NodeCPtr>; + constexpr bool key_value_success_callback(std::string_view, ast::NodeCPtr) { return true; } node_callback_t expect_identifier(callback_t<std::string_view> callback); node_callback_t expect_string(callback_t<std::string_view> callback); |