-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwiz_msgs.h
200 lines (167 loc) · 4.46 KB
/
wiz_msgs.h
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
#pragma once
#include <Windows.h>
#include <string>
#include <vector>
#include "wiz_wad.h"
#include "rapidxml.hpp"
#include <map>
#include <algorithm>
using namespace rapidxml;
struct field {
std::string name;
std::string type;
std::string value;
};
struct xml_message {
std::string msg_name;
std::string msg_description;
std::string msg_handler;
std::string msg_access_level;
int msg_order;
std::vector<field> params;
};
struct protocol_info {
byte service_id;
std::string protocol_type;
int protocol_version;
std::string protocol_description;
std::vector<xml_message> messages;
};
xml_message get_msg_from_xml(xml_node<>* node)
{
xml_message ret;
const std::string node_name = node->name();
if (node_name != "RECORD")
{
printf("Expected <RECORD> node but got <%s>.\n", node->name());
}
for (auto* field_node = node->first_node();
field_node; field_node = field_node->next_sibling())
{
if (field_node->type() != node_element)
continue;
const auto type = field_node->first_attribute("TYPE");
if (!type)
{
printf("Type required but not found on %s??\n", field_node->name());
continue;
}
const auto name = std::string(field_node->name());
if (name == "_MsgName")
{
ret.msg_name = field_node->value();
continue;
}
if (name == "_MsgDescription")
{
ret.msg_description = field_node->value();
continue;
}
if (name == "_MsgHandler")
{
ret.msg_handler = field_node->value();
continue;
}
if (name == "_MsgOrder")
{
ret.msg_order = reinterpret_cast<int>(field_node->value());
continue;
}
if (name == "_MsgAccessLvl")
{
ret.msg_access_level = field_node->value();
continue;
}
ret.params.push_back({ field_node->name(), std::string(type->value()) });
}
return ret;
}
int has_msg_order(xml_node<>* node)
{
for (auto* field_node = node->first_node();
field_node; field_node = field_node->next_sibling())
{
if (field_node->type() != node_element)
continue;
const auto name = std::string(field_node->name());
if (name == "_MsgOrder")
return atoi(field_node->value());
}
return 0;
}
std::vector<protocol_info> get_protocols() {
std::vector<protocol_info> protocols;
std::vector<file_dat> messages;
get_wad("Messages.xml", ".xml", messages);
for (auto message : messages) {
std::map<std::string, xml_message> msgs;
message.file.push_back(0x00);
const auto data = reinterpret_cast<char*>(message.file.data());
xml_document<> doc;
try
{
doc.parse<0>(data);
}
catch (parse_error & e)
{
printf("Failed to parse\n");
return {};
}
auto has_record_order = false;
auto* root = doc.first_node();
for (auto* node = root->first_node();
node; node = node->next_sibling())
{
auto* record_node = node->first_node();
if (!record_node)
continue;
const auto record_order = has_msg_order(record_node);
if (record_order > 0)
has_record_order = true;
}
std::vector<xml_message> sorted_messages;
if (has_record_order)
sorted_messages.resize(254);
for (auto* node = root->first_node();
node; node = node->next_sibling())
{
auto* record_node = node->first_node();
if (!record_node)
continue;
auto record = get_msg_from_xml(record_node);
if (has_record_order)
{
sorted_messages[has_msg_order(record_node)] = record;
}
else
{
msgs.insert(std::pair<std::string, xml_message>(record.msg_name, record));
}
}
bool is_done = false;
if (!has_record_order) {
for (auto it = msgs.begin();
it != msgs.end(); ++it)
{
sorted_messages.push_back(it->second);
if (std::string(root->first_node()->first_node()->first_node("ProtocolType")->value()) == "GAME")
{
if (sorted_messages.size() == 50 && !is_done) {
sorted_messages.pop_back(); // IS KI's ALPHABETICAL SORT WRONG?????????
is_done = true;
}
}
}
}
protocol_info message_module =
{
static_cast<uint8_t>(atoi(root->first_node()->first_node()->first_node("ServiceID")->value())),
std::string(root->first_node()->first_node()->first_node("ProtocolType")->value()),
atoi(root->first_node()->first_node()->first_node("ProtocolVersion")->value()),
std::string(root->first_node()->first_node()->first_node("ProtocolDescription")->value()),
sorted_messages
};
protocols.push_back(message_module);
}
return protocols;
}