diff options
author | Wolfgang Aigner <wolfgang.aigner@yahoo.de> | 2023-04-06 17:10:13 +0200 |
---|---|---|
committer | Wolfgang Aigner <wolfgang.aigner@yahoo.de> | 2023-04-06 17:42:27 +0200 |
commit | 383583733b2f4cd8c5e632c160afdb987b8615af (patch) | |
tree | 2425f372a655e8f87bc5d0d55663daa53141d615 /extension/src/openvic2 | |
parent | 0c110a5de255d7fbddf77b549cf019dcfef88751 (diff) |
Basic loading of goods defined in goods.json
Diffstat (limited to 'extension/src/openvic2')
-rw-r--r-- | extension/src/openvic2/Good.cpp | 19 | ||||
-rw-r--r-- | extension/src/openvic2/Good.hpp | 23 | ||||
-rw-r--r-- | extension/src/openvic2/LoadGoods.cpp | 87 | ||||
-rw-r--r-- | extension/src/openvic2/LoadGoods.hpp | 15 |
4 files changed, 144 insertions, 0 deletions
diff --git a/extension/src/openvic2/Good.cpp b/extension/src/openvic2/Good.cpp new file mode 100644 index 0000000..3424099 --- /dev/null +++ b/extension/src/openvic2/Good.cpp @@ -0,0 +1,19 @@ +#include "Good.hpp" + +using namespace OpenVic2;; + +Good::Good() = default; + +Good::Good(const godot::String& identifier, const godot::String& category, float_t cost, const godot::String& colour, + bool isAvailable, bool isTradable, bool isMoney, bool hasOverseasPenalty) { + this->identifier = identifier; + this->category = category; + this->cost = cost; + this->colour = colour; + this->isAvailableAtStart = isAvailable; + this->isTradable = isTradable; + this->isMoney = isMoney; + this->hasOverseasPenalty = hasOverseasPenalty; +} + +Good::~Good() = default;
\ No newline at end of file diff --git a/extension/src/openvic2/Good.hpp b/extension/src/openvic2/Good.hpp new file mode 100644 index 0000000..615b82c --- /dev/null +++ b/extension/src/openvic2/Good.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include <godot_cpp/variant/string.hpp> +#include <string> + +namespace OpenVic2 { + class Good { + public: + godot::String identifier; + godot::String category; + float_t cost; + godot::String colour; + bool isAvailableAtStart; + bool isTradable; + bool isMoney; + bool hasOverseasPenalty; + + Good(); + Good(const godot::String& identifier, const godot::String& category, float_t cost, const godot::String& colour, + bool isAvailable, bool isTradable, bool isMoney, bool hasOverseasPenalty); + ~Good(); + }; +}
\ No newline at end of file diff --git a/extension/src/openvic2/LoadGoods.cpp b/extension/src/openvic2/LoadGoods.cpp new file mode 100644 index 0000000..a47f03f --- /dev/null +++ b/extension/src/openvic2/LoadGoods.cpp @@ -0,0 +1,87 @@ +#include "LoadGoods.hpp" + +#include <godot_cpp/variant/utility_functions.hpp> +#include <godot_cpp/classes/file_access.hpp> +#include <godot_cpp/classes/json.hpp> + +using namespace OpenVic2; +using namespace godot; + +#define JSON_PROPERTY_NAME(x) godot::StringName(x) + +std::vector<Good> LoadGoods::load_goods_from_disk(godot::String const& file_path) { + std::vector<Good> goods; + Ref<FileAccess> file = FileAccess::open(file_path, FileAccess::ModeFlags::READ); + Error err = FileAccess::get_open_error(); + if (err != OK || file.is_null()) { + UtilityFunctions::push_error("Failed to open good configuration file: ", file_path); + return std::vector<Good>(); + } + godot::String file_content = file->get_as_text(); + JSON parsed_goods; + err = parsed_goods.parse(file_content); + if (err != OK) { + UtilityFunctions::push_error("Failed to parse goods.json"); + return std::vector<Good>(); + } + Variant v_goods = parsed_goods.get_data().get("goods", nullptr); + if (v_goods.get_type() != Variant::ARRAY) { + UtilityFunctions::push_error("Failed to parse goods.json: Top level property is not an array"); + return std::vector<Good>(); + } + godot::Array v_goods_array = (godot::Array)v_goods; + int32_t count = v_goods_array.size(); + goods.resize(count); + for (size_t i = 0; i < count; i++) { + if(!extract_property_from_json(v_goods_array[i], goods, i)) { + return std::vector<Good>(); + } + } + return goods; +} + +bool LoadGoods::extract_property_from_json(const godot::Variant& variant, std::vector<Good>& goods, int32_t index) { + bool valid; + String id = variant.get_named(JSON_PROPERTY_NAME("id"), valid); + if(!valid) { + UtilityFunctions::push_error("Could not extract property id of type string from goods.json"); + return false; + } + String category = variant.get_named(JSON_PROPERTY_NAME("category"), valid); + if(!valid) { + UtilityFunctions::push_error("Could not extract property category of type string from goods.json"); + return false; + } + float_t cost = (float_t)variant.get_named(JSON_PROPERTY_NAME("cost"), valid); + if(!valid) { + UtilityFunctions::push_error("Could not extract property cost of type float from goods.json"); + return false; + } + String colour = variant.get_named(JSON_PROPERTY_NAME("colour"), valid); + if(!valid) { + UtilityFunctions::push_error("Could not extract property colour of type string from goods.json"); + return false; + } + bool isAvailableAtStart = (bool)variant.get_named(JSON_PROPERTY_NAME("isAvailableAtStart"), valid); + if(!valid) { + UtilityFunctions::push_error("Could not extract property isAvailableAtStart of type bool from goods.json"); + return false; + } + bool isTradable = (bool)variant.get_named(JSON_PROPERTY_NAME("isTradeable"), valid); + if(!valid) { + UtilityFunctions::push_error("Could not extract property isTradable of type bool from goods.json"); + return false; + } + bool isMoney = (bool)variant.get_named(JSON_PROPERTY_NAME("isMoney"), valid); + if(!valid) { + UtilityFunctions::push_error("Could not extract property isMoney of type bool from goods.json"); + return false; + } + bool hasOverseasPenalty = (bool)variant.get_named(JSON_PROPERTY_NAME("hasOverseasPenalty"), valid); + if(!valid) { + UtilityFunctions::push_error("Could not extract property hasOverseaPenalty of type bool from goods.json"); + return false; + } + goods.at(index) = Good(id, category, cost, colour, isAvailableAtStart, isTradable, isMoney, hasOverseasPenalty); + return true; +}
\ No newline at end of file diff --git a/extension/src/openvic2/LoadGoods.hpp b/extension/src/openvic2/LoadGoods.hpp new file mode 100644 index 0000000..41dd75e --- /dev/null +++ b/extension/src/openvic2/LoadGoods.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include <godot_cpp/classes/object.hpp> +#include <vector> +#include "Good.hpp" + +namespace OpenVic2 { + class LoadGoods { + private: + static bool extract_property_from_json(const godot::Variant& variant, std::vector<Good>& goods, int32_t index); + + public: + static std::vector<Good> load_goods_from_disk(godot::String const& file_path); + }; +}
\ No newline at end of file |