diff options
Diffstat (limited to 'src/openvic-simulation/dataloader/Dataloader.cpp')
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.cpp | 103 |
1 files changed, 72 insertions, 31 deletions
diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 82957fc..a30983b 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -49,18 +49,14 @@ fs::path Dataloader::lookup_file(fs::path const& path) const { return {}; } -const fs::path Dataloader::TXT = ".txt"; - 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; } return false; } -Dataloader::path_vector_t Dataloader::lookup_files_in_dir(fs::path const& path, fs::path const* extension) const { - +Dataloader::path_vector_t Dataloader::lookup_files_in_dir(fs::path const& path, fs::path const& extension) const { path_vector_t ret; for (fs::path const& root : roots) { const fs::path composed = root / path; @@ -68,7 +64,7 @@ Dataloader::path_vector_t Dataloader::lookup_files_in_dir(fs::path const& path, for (fs::directory_entry const& entry : fs::directory_iterator { composed, ec }) { if (entry.is_regular_file()) { const fs::path file = entry; - if (extension == nullptr || file.extension() == *extension) { + if (extension.empty() || file.extension() == extension) { if (!contains_file_with_name(ret, file.filename())) { ret.push_back(file); } @@ -79,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, std::function<bool(fs::path const&)> callback, fs::path const* extension) 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); @@ -88,11 +83,11 @@ bool Dataloader::apply_to_files_in_dir(fs::path const& path, std::function<bool( return ret; } -template<std::derived_from<detail::BasicParser> Parser, bool (Parser::*parse_func)()> +template<std::derived_from<detail::BasicParser> Parser, bool (*parse_func)(Parser&)> static Parser _run_ovdl_parser(fs::path const& path) { Parser parser; std::string buffer; - auto error_log_stream = ovdl::detail::CallbackStream { + auto error_log_stream = detail::CallbackStream { [](void const* s, std::streamsize n, void* user_data) -> std::streamsize { if (s != nullptr && n > 0 && user_data != nullptr) { static_cast<std::string*>(user_data)->append(static_cast<char const*>(s), n); @@ -107,18 +102,18 @@ static Parser _run_ovdl_parser(fs::path const& path) { parser.set_error_log_to(error_log_stream); parser.load_from_file(path); if (!buffer.empty()) { - Logger::error("Parser load errors:\n\n", buffer, "\n"); + Logger::error("Parser load errors for ", path, ":\n\n", buffer, "\n"); buffer.clear(); } if (parser.has_fatal_error() || parser.has_error()) { Logger::error("Parser errors while loading ", path); return parser; } - if (!(parser.*parse_func)()) { - Logger::error("Parse function returned false!"); + if (!parse_func(parser)) { + Logger::error("Parse function returned false for ", path, "!"); } if (!buffer.empty()) { - Logger::error("Parser parse errors:\n\n", buffer, "\n"); + Logger::error("Parser parse errors for ", path, ":\n\n", buffer, "\n"); buffer.clear(); } if (parser.has_fatal_error() || parser.has_error()) { @@ -127,16 +122,30 @@ static Parser _run_ovdl_parser(fs::path const& path) { return parser; } +static bool _v2script_parse(v2script::Parser& parser) { + return parser.simple_parse(); +} + static v2script::Parser _parse_defines(fs::path const& path) { - return _run_ovdl_parser<v2script::Parser, &v2script::Parser::simple_parse>(path); + return _run_ovdl_parser<v2script::Parser, &_v2script_parse>(path); +} + +static bool _csv_parse(csv::Windows1252Parser& parser) { + return parser.parse_csv(); } static csv::Windows1252Parser _parse_csv(fs::path const& path) { - return _run_ovdl_parser<csv::Windows1252Parser, &csv::Windows1252Parser::parse_csv>(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, + const bool ret = apply_to_files_in_dir(pop_type_directory, ".txt", [&pop_manager](fs::path const& file) -> bool { return pop_manager.load_pop_type_file(file.stem().string(), _parse_defines(file).get_file_node()); } @@ -148,7 +157,9 @@ bool Dataloader::_load_pop_types(PopManager& pop_manager, fs::path const& pop_ty return ret; } -bool Dataloader::_load_map_dir(Map& map, fs::path const& map_directory) const { +bool Dataloader::_load_map_dir(GameManager& game_manager, fs::path const& map_directory) const { + Map& map = game_manager.map; + static const fs::path defaults_filename = "default.map"; static const std::string default_definitions = "definition.csv"; static const std::string default_provinces = "provinces.bmp"; @@ -221,6 +232,16 @@ bool Dataloader::_load_map_dir(Map& map, fs::path const& map_directory) const { ret = false; } + if (!map.load_province_positions(game_manager.building_manager, _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; + } + if (!map.set_water_province_list(water_province_identifiers)) { Logger::error("Failed to set water provinces!"); ret = false; @@ -231,7 +252,7 @@ bool Dataloader::_load_map_dir(Map& map, fs::path const& map_directory) const { } bool Dataloader::load_defines(GameManager& game_manager) const { - static const fs::path good_file = "common/goods.txt"; + static const fs::path goods_file = "common/goods.txt"; static const fs::path pop_type_directory = "poptypes"; static const fs::path graphical_culture_type_file = "common/graphicalculturetype.txt"; static const fs::path culture_file = "common/cultures.txt"; @@ -240,7 +261,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const { bool ret = true; - if (!game_manager.good_manager.load_good_file(_parse_defines(lookup_file(good_file)).get_file_node())) { + if (!game_manager.good_manager.load_goods_file(_parse_defines(lookup_file(goods_file)).get_file_node())) { Logger::error("Failed to load goods!"); ret = false; } @@ -260,7 +281,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load religions!"); ret = false; } - if (!_load_map_dir(game_manager.map, map_directory)) { + if (!_load_map_dir(game_manager, map_directory)) { Logger::error("Failed to load map!"); ret = false; } @@ -269,18 +290,38 @@ 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, + 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); + } + ); +} + +static bool _load_localisation_file(Dataloader::localisation_callback_t callback, std::vector<csv::LineObject> const& lines) { + bool ret = true; + for (csv::LineObject const& line : lines) { + const std::string_view key = line.get_value_for(0); + if (!key.empty()) { + const size_t max_entry = std::min<size_t>(line.value_count() - 1, Dataloader::_LocaleCount); + for (size_t i = 0; i < max_entry; ++i) { + const std::string_view entry = line.get_value_for(i + 1); + if (!entry.empty()) { + ret &= callback(key, static_cast<Dataloader::locale_t>(i), entry); + } + } + } + } + return ret; +} + +bool Dataloader::load_localisation_files(localisation_callback_t callback, fs::path const& localisation_dir) { + return apply_to_files_in_dir(localisation_dir, ".csv", + [callback](fs::path path) -> bool { + return _load_localisation_file(callback, _parse_csv(path).get_lines()); } ); } |