Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
hjsonloader.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2025 Conquer Space
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <hjson.h>
20
21#include <map>
22#include <string_view>
23
24#include <glm/glm.hpp>
25
26#include "core/universe.h"
27
28namespace cqsp::core::loading {
30 public:
32 virtual ~HjsonLoader() = default;
33 virtual const Hjson::Value& GetDefaultValues() = 0;
34 int LoadHjson(const Hjson::Value& values);
35 virtual bool LoadValue(const Hjson::Value& values, Node& node) = 0;
36 virtual void PostLoad(const Node& node) {}
37 virtual bool NeedIdentifier() { return true; }
38
39 protected:
41
42 glm::vec3 LoadColor(const Hjson::Value& value, std::string_view identifier);
43 glm::vec3 LoadColor(const Hjson::Value& value);
44 uint32_t StringHash(std::string_view);
45};
46
47class TagLoader {
48 public:
49 using Handler = std::function<void(Node&)>;
50 void Register(std::string_view tag, Handler handler) { handles.emplace(std::string(tag), handler); }
51
55 template <typename Comp>
56 void Register(std::string_view tag) {
57 handles.emplace(std::string(tag), [](Node& node) { node.emplace<Comp>(); });
58 }
59
60 void Apply(std::string_view tag, Node& node) const;
61
62 void ParseTags(const Hjson::Value& tags, Node& node) const;
63
64 private:
65 std::map<std::string, Handler> handles;
66};
67} // namespace cqsp::core::loading
Definition: universe.h:39
Definition: universe.h:59
Definition: hjsonloader.h:29
virtual void PostLoad(const Node &node)
Definition: hjsonloader.h:36
glm::vec3 LoadColor(const Hjson::Value &value, std::string_view identifier)
Definition: hjsonloader.cpp:106
Universe & universe
Definition: hjsonloader.h:40
int LoadHjson(const Hjson::Value &values)
Definition: hjsonloader.cpp:32
virtual bool LoadValue(const Hjson::Value &values, Node &node)=0
HjsonLoader(Universe &universe)
Definition: hjsonloader.h:31
uint32_t StringHash(std::string_view)
Definition: hjsonloader.cpp:94
virtual const Hjson::Value & GetDefaultValues()=0
virtual bool NeedIdentifier()
Definition: hjsonloader.h:37
Definition: hjsonloader.h:47
std::function< void(Node &)> Handler
Definition: hjsonloader.h:49
void Apply(std::string_view tag, Node &node) const
Definition: hjsonloader.cpp:85
void ParseTags(const Hjson::Value &tags, Node &node) const
Definition: hjsonloader.cpp:79
std::map< std::string, Handler > handles
Definition: hjsonloader.h:65
void Register(std::string_view tag)
Definition: hjsonloader.h:56
void Register(std::string_view tag, Handler handler)
Definition: hjsonloader.h:50
Definition: economyloader.cpp:24