1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#include "Define.hpp"
#include <cassert>
#include <cstdlib>
#include <memory>
#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp>
#include "openvic-simulation/dataloader/NodeTools.hpp"
#include "openvic-simulation/types/Date.hpp"
#include "openvic-simulation/types/IdentifierRegistry.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
using namespace OpenVic;
using namespace OpenVic::NodeTools;
Define::Define(std::string_view new_identifier, std::string&& new_value, Type new_type)
: HasIdentifier { new_identifier }, value { std::move(new_value) }, type { new_type } {}
fixed_point_t Define::get_value_as_fp() const {
return fixed_point_t::parse(value);
}
int64_t Define::get_value_as_int() const {
return std::strtoll(value.data(), nullptr, 10);
}
uint64_t Define::get_value_as_uint() const {
return std::strtoull(value.data(), nullptr, 10);
}
bool DefineManager::add_define(std::string_view name, std::string&& value, Define::Type type) {
if (name.empty()) {
Logger::error("Invalid define identifier - empty!");
return false;
}
return defines.add_item({ name, std::move(value), type }, duplicate_warning_callback);
}
Date DefineManager::get_start_date() const {
return start_date ? *start_date : Date {};
}
Date DefineManager::get_end_date() const {
return end_date ? *end_date : Date {};
}
bool DefineManager::in_game_period(Date date) const {
if (start_date && end_date) {
return date.in_range(*start_date, *end_date);
} else {
return false;
}
}
bool DefineManager::add_date_define(std::string_view name, Date date) {
if (name == "start_date") {
start_date = date;
} else if (name == "end_date") {
end_date = date;
} else {
Logger::error("Invalid date define identifier - \"", name, "\" (must be start_date or end_date)");
return false;
}
return defines.add_item({ name, date.to_string(), Define::Type::Date });
}
bool DefineManager::load_defines_file(ast::NodeCPtr root) {
bool ret = expect_dictionary_keys(
"defines", ONE_EXACTLY, expect_dictionary([this](std::string_view key, ast::NodeCPtr value) -> bool {
using enum Define::Type;
static const string_map_t<Define::Type> type_map {
{ "country", Country },
{ "economy", Economy },
{ "military", Military },
{ "diplomacy", Diplomacy },
{ "pops", Pops },
{ "ai", Ai },
{ "graphics", Graphics },
};
const string_map_t<Define::Type>::const_iterator type_it = type_map.find(key);
if (type_it != type_map.end()) {
return expect_dictionary_reserve_length(
defines,
[this, &key, type = type_it->second](std::string_view inner_key, ast::NodeCPtr value) -> bool {
std::string str_val;
bool ret = expect_identifier_or_string(assign_variable_callback_string(str_val))(value);
ret &= add_define(inner_key, std::move(str_val), type);
return ret;
}
)(value);
} else if (key == "start_date" || key == "end_date") {
return expect_date_identifier_or_string(std::bind_front(&DefineManager::add_date_define, this, key))(value);
} else {
return false;
}
})
)(root);
lock_defines();
return ret;
}
|