-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpgmaker_types.cpp
More file actions
222 lines (192 loc) · 7.84 KB
/
rpgmaker_types.cpp
File metadata and controls
222 lines (192 loc) · 7.84 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "rpgmaker_types.hpp"
#include "logger.hpp"
using namespace RPGMaker;
bool Command::is_valid(const json &command_json) const {
if (!command_json.contains("code") || !command_json["code"].is_number_integer()) {
log_err(R"(Command doesn't have a code or it's not an integer!")");
return false;
}
if (!command_json.contains("parameters")) {
log_err(R"(Command doesn't have parameters!)");
return false;
}
return true;
}
Command::Command(const json &command_json) {
if (!is_valid(command_json)) {
return;
}
code = static_cast<CommandCode>(command_json["code"].get<uint32_t>());
for (const auto ¶meter : command_json["parameters"]) {
if (parameter.is_null() || parameter.empty()) {
continue;
}
if (parameter.is_number_integer()) {
parameters.emplace_back(parameter.get<uint32_t>());
continue;
}
if (parameter.is_number_float()) {
parameters.emplace_back(parameter.get<double>());
continue;
}
if (parameter.is_boolean()) {
parameters.emplace_back(parameter.get<bool>());
continue;
}
if (parameter.is_string() && parameter.get<std::string_view>().length() == 1) {
parameters.emplace_back(parameter.get<std::string>()[0]);
continue;
}
if (parameter.is_string()) {
parameters.emplace_back(parameter.get<std::string>());
continue;
}
}
}
bool Condition::is_valid(const json &condition_json) const {
if (!condition_json.contains("switch1Id") || !condition_json["switch1Id"].is_number_integer()) {
log_err(R"(This condition doesn't have a switch 1 id or it's not an integer!)");
return false;
}
if (!condition_json.contains("switch1Valid") || !condition_json["switch1Valid"].is_boolean()) {
log_err(R"(This condition doesn't have a bool to define if switch1Id is active or it's not a boolean!)");
return false;
}
if (!condition_json.contains("switch2Id") || !condition_json["switch2Id"].is_number_integer()) {
log_err(R"(This condition doesn't have a switch 2 id or it's not an integer!)");
return false;
}
if (!condition_json.contains("switch2Valid") || !condition_json["switch2Valid"].is_boolean()) {
log_err(R"(This condition doesn't have a bool to define if switch2Id is active or it's not a boolean!)");
return false;
}
if (!condition_json.contains("variableId") || !condition_json["variableId"].is_number_integer()) {
log_err(R"(This condition doesn't have a variable id or it's not an integer!)");
return false;
}
if (!condition_json.contains("variableValid") || !condition_json["variableValid"].is_boolean()) {
log_err(R"(This condition doesn't have a bool to define if variableId is active or it's not a boolean!)");
return false;
}
if (!condition_json.contains("variableValue") || !condition_json["variableValue"].is_number_integer()) {
log_err(R"(This condition doesn't have a value to compare against or it's not an integer!)");
return false;
}
return true;
}
Condition::Condition(const json &condition_json) {
if (!is_valid(condition_json)) {
return;
}
switch1_id = condition_json["switch1Id"].get<uint32_t>();
switch1_valid = condition_json["switch1Valid"].get<bool>();
switch2_id = condition_json["switch2Id"].get<uint32_t>();
switch2_valid = condition_json["switch2Valid"].get<bool>();
variable_id = condition_json["variableId"].get<uint32_t>();
variable_valid = condition_json["variableValid"].get<bool>();
variable_value = condition_json["variableValue"].get<uint32_t>();
}
bool EventPage::is_valid(const json &event_page_json) const {
if (!event_page_json.contains("conditions")) {
log_err(R"(This event page doesn't have conditions!)");
return false;
}
if (!event_page_json.contains("list")) {
log_err(R"(This event page doesn't have commands!)");
return false;
}
return true;
}
EventPage::EventPage(const json &event_page_json) {
if (!is_valid(event_page_json)) {
return;
}
conditions = Condition(event_page_json["conditions"]);
const auto &command_list = event_page_json["list"];
list.reserve(command_list.size());
for (size_t line = 0, last_line = command_list.size(); line < last_line; ++line) {
list.emplace_back(Command(command_list[line]));
}
}
bool Event::is_valid(const json &event_json) const {
if (!event_json.contains("x") || !event_json["x"].is_number_integer()) {
log_err(R"(Event doesn't have a x position or it's not an integer!)");
return false;
}
if (!event_json.contains("y") || !event_json["y"].is_number_integer()) {
log_err(R"(Event doesn't have a y position or it's not an integer!)");
return false;
}
if (!event_json.contains("name") || !event_json["name"].is_string()) {
log_err(R"(Event doesn't have a name or it's not a string!)");
return false;
}
if (!event_json.contains("note") || !event_json["note"].is_string()) {
log_err(R"(Event doesn't have a note or it's not a string!)");
return false;
}
if (!event_json.contains("id") || !event_json["id"].is_number_integer()) {
log_err(R"(Event doesn't have an id or it's not an integer!)");
return false;
}
if (!event_json.contains("pages")) {
log_err(R"(Event doesn't have pages!)");
return false;
}
return true;
}
Event::Event(const json &event_json) {
if (!is_valid(event_json)) {
return;
}
x = event_json["x"].get<uint32_t>();
y = event_json["y"].get<uint32_t>();
id = event_json["id"].get<uint32_t>();
name = event_json["name"].get<std::string_view>();
note = event_json["note"].get<std::string_view>();
const auto page_count = event_json["pages"].size();
pages.reserve(page_count);
for (size_t page_num = 0; page_num < page_count; ++page_num) {
pages.emplace_back(EventPage(event_json["pages"][page_num]));
}
}
bool CommonEvent::is_valid(const json &common_event_json) const {
if (!common_event_json.contains("id") || !common_event_json["id"].is_number_integer()) {
log_err(R"(CommonEvent doesn't have an id or it's not an integer!)");
return false;
}
if (!common_event_json.contains("name") || !common_event_json["name"].is_string()) {
log_err(R"(CommonEvent doesn't have a name or it's not a string!)");
return false;
}
if (!common_event_json.contains("switchId") || !common_event_json["switchId"].is_number_integer()) {
log_err(R"(CommonEvent doesn't have a switch id or it's not an integer!)");
return false;
}
if (!common_event_json.contains("trigger") || !common_event_json["trigger"].is_number_integer()) {
log_err(R"(CommonEvent doesn't have a trigger or it's not an integer!)");
return false;
}
if (!common_event_json.contains("list")) {
log_err(R"(CommonEvent doesn't have commands!)");
return false;
}
return true;
}
bool CommonEvent::has_trigger() const {
return trigger != CommonEventTrigger::NONE;
}
CommonEvent::CommonEvent(const json &common_event_json) {
if (!is_valid(common_event_json)) {
return;
}
id = common_event_json["id"].get<uint32_t>();
name = common_event_json["name"].get<std::string_view>();
switch_id = common_event_json["switchId"].get<uint32_t>();
trigger = static_cast<CommonEventTrigger>(common_event_json["trigger"].get<uint32_t>());
const auto &command_list = common_event_json["list"];
list.reserve(command_list.size());
for (size_t line = 0, last_line = command_list.size(); line < last_line; ++line) {
list.emplace_back(Command(command_list[line]));
}
}