diff options
author | Spartan322 <Megacake1234@gmail.com> | 2023-10-09 18:02:14 +0200 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2023-10-13 16:43:34 +0200 |
commit | a93b0d8f268fbf04c09ee73e5b2923a610667fcf (patch) | |
tree | 01e8f55c5327ee3b0796bb1d1e6e616331abee13 /src/openvic-simulation | |
parent | 32fdf7c38b2e56339a2fffa71a7a61a854759e2c (diff) |
Add Bookmark Loading
Diffstat (limited to 'src/openvic-simulation')
-rw-r--r-- | src/openvic-simulation/GameManager.hpp | 3 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.cpp | 5 | ||||
-rw-r--r-- | src/openvic-simulation/history/Bookmark.cpp | 95 | ||||
-rw-r--r-- | src/openvic-simulation/history/Bookmark.hpp | 53 | ||||
-rw-r--r-- | src/openvic-simulation/history/HistoryManager.hpp | 18 |
5 files changed, 174 insertions, 0 deletions
diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index 6e6c0b8..09771c6 100644 --- a/src/openvic-simulation/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -3,6 +3,7 @@ #include "openvic-simulation/GameAdvancementHook.hpp" #include "openvic-simulation/Modifier.hpp" #include "openvic-simulation/economy/EconomyManager.hpp" +#include "openvic-simulation/history/HistoryManager.hpp" #include "openvic-simulation/map/Map.hpp" #include "openvic-simulation/military/MilitaryManager.hpp" #include "openvic-simulation/politics/PoliticsManager.hpp" @@ -17,6 +18,7 @@ namespace OpenVic { MilitaryManager military_manager; ModifierManager modifier_manager; PoliticsManager politics_manager; + HistoryManager history_manager; PopManager pop_manager; GameAdvancementHook clock; @@ -37,6 +39,7 @@ namespace OpenVic { REF_GETTERS(military_manager) REF_GETTERS(modifier_manager) REF_GETTERS(politics_manager) + REF_GETTERS(history_manager) REF_GETTERS(pop_manager) REF_GETTERS(clock) diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 718218f..b9d7496 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -595,6 +595,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const { static const fs::path issues_file = "common/issues.txt"; static const fs::path production_types_file = "common/production_types.txt"; static const fs::path religion_file = "common/religion.txt"; + static const fs::path bookmark_file = "common/bookmarks.txt"; bool ret = true; @@ -662,6 +663,10 @@ bool Dataloader::load_defines(GameManager& game_manager) const { Logger::error("Failed to load units!"); ret = false; } + if (!game_manager.get_history_manager().load_bookmark_file(parse_defines(lookup_file(bookmark_file)).get_file_node())) { + Logger::error("Failed to load bookmarks!"); + ret = false; + } return ret; } diff --git a/src/openvic-simulation/history/Bookmark.cpp b/src/openvic-simulation/history/Bookmark.cpp new file mode 100644 index 0000000..7ab96b4 --- /dev/null +++ b/src/openvic-simulation/history/Bookmark.cpp @@ -0,0 +1,95 @@ +#include "Bookmark.hpp" + +#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> + +#include "openvic-simulation/dataloader/NodeTools.hpp" + +#include "types/Date.hpp" +#include "types/IdentifierRegistry.hpp" +#include "utility/Logger.hpp" + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +Bookmark::Bookmark( + std::string_view new_identifier, + std::string_view new_name, + std::string_view new_description, + Date new_date, + uint32_t new_initial_camera_x, + uint32_t new_initial_camera_y +) + : HasIdentifier { new_identifier }, + name { new_name }, + description { new_description }, + date { new_date }, + initial_camera_x { new_initial_camera_x }, + initial_camera_y { new_initial_camera_y } { +} + +std::string_view Bookmark::get_name() const { + return name; +} + +std::string_view Bookmark::get_description() const { + return description; +} + +Date const& Bookmark::get_date() const { + return date; +} + +uint32_t Bookmark::get_initial_camera_x() const { + return initial_camera_x; +} + +uint32_t Bookmark::get_initial_camera_y() const { + return initial_camera_y; +} + +BookmarkManager::BookmarkManager() : bookmarks { "bookmarks" } {} + +bool BookmarkManager::add_bookmark( + std::string_view identifier, + std::string_view name, + std::string_view description, + Date date, + uint32_t initial_camera_x, + uint32_t initial_camera_y +) { + if (identifier.empty()) { + Logger::error("Invalid bookmark identifier - empty!"); + return false; + } + + return bookmarks.add_item({ identifier, name, description, date, initial_camera_x, initial_camera_y }); +} + +bool BookmarkManager::load_bookmark_file(ast::NodeCPtr root) { + bool ret = expect_dictionary( + [this](std::string_view key, ast::NodeCPtr value) -> bool { + if (key != "bookmark") { + Logger::error("Invalid bookmark declaration ", key); + return false; + } + + std::string_view name, description; + Date date; + uint32_t initial_camera_x, initial_camera_y; + + bool ret = expect_dictionary_keys( + "name", ONE_EXACTLY, expect_string(assign_variable_callback(name)), + "desc", ONE_EXACTLY, expect_string(assign_variable_callback(description)), + "date", ONE_EXACTLY, expect_date(assign_variable_callback(date)), + "cameraX", ONE_EXACTLY, expect_uint(assign_variable_callback(initial_camera_x)), + "cameraY", ONE_EXACTLY, expect_uint(assign_variable_callback(initial_camera_y)) + )(value); + + ret &= add_bookmark(date.to_string(), name, description, date, initial_camera_x, initial_camera_y); + return ret; + } + )(root); + lock_bookmarks(); + + return ret; +}
\ No newline at end of file diff --git a/src/openvic-simulation/history/Bookmark.hpp b/src/openvic-simulation/history/Bookmark.hpp new file mode 100644 index 0000000..ec230ca --- /dev/null +++ b/src/openvic-simulation/history/Bookmark.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include <cstdint> + +#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> + +#include "openvic-simulation/types/IdentifierRegistry.hpp" + +namespace OpenVic { + struct BookmarkManager; + + struct Bookmark : HasIdentifier { + friend struct BookmarkManager; + + private: + const std::string name; + const std::string description; + const Date date; + const uint32_t initial_camera_x; + const uint32_t initial_camera_y; + + Bookmark( + std::string_view new_identifier, + std::string_view new_name, + std::string_view new_description, + Date new_date, + uint32_t new_initial_camera_x, + uint32_t new_initial_camera_y + ); + + public: + Bookmark(Bookmark&&) = default; + + std::string_view get_name() const; + std::string_view get_description() const; + Date const& get_date() const; + uint32_t get_initial_camera_x() const; + uint32_t get_initial_camera_y() const; + }; + + struct BookmarkManager { + private: + IdentifierRegistry<Bookmark> bookmarks; + + public: + BookmarkManager(); + + bool add_bookmark(std::string_view identifier, std::string_view name, std::string_view description, Date date, uint32_t initial_camera_x, uint32_t initial_camera_y); + IDENTIFIER_REGISTRY_ACCESSORS(bookmark); + + bool load_bookmark_file(ast::NodeCPtr root); + }; +}
\ No newline at end of file diff --git a/src/openvic-simulation/history/HistoryManager.hpp b/src/openvic-simulation/history/HistoryManager.hpp new file mode 100644 index 0000000..bec5359 --- /dev/null +++ b/src/openvic-simulation/history/HistoryManager.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "openvic-simulation/history/Bookmark.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" + +namespace OpenVic { + struct HistoryManager { + private: + BookmarkManager bookmark_manager; + + public: + REF_GETTERS(bookmark_manager) + + inline bool load_bookmark_file(ast::NodeCPtr root) { + return bookmark_manager.load_bookmark_file(root); + } + }; +} |