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
|
#include <google/protobuf/message.h>
#include <google/protobuf/text_format.h>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include "proto/human.pb.h"
namespace com::fourisland::lingo2_archipelago {
namespace {
template <typename T>
T ReadMessageFromFile(const std::string& path) {
std::cout << "Processing " << path << std::endl;
std::ifstream file(path);
std::stringstream buffer;
buffer << file.rdbuf();
T message;
google::protobuf::TextFormat::ParseFromString(buffer.str(), &message);
return message;
}
class AssignIds {
public:
AssignIds(const std::string& mapdir) : mapdir_(mapdir) {}
void Run() {
std::filesystem::path datadir_path = mapdir_;
std::filesystem::path ids_path = datadir_path / "ids.txtpb";
ReadIds(ids_path);
ProcessMaps(datadir_path);
WriteIds(ids_path);
std::cout << "Next ID: " << next_id_ << std::endl;
}
void ReadIds(std::filesystem::path path) {
id_mappings_ = ReadMessageFromFile<IdMappings>(path.string());
for (const auto& [_, map] : id_mappings_.maps()) {
for (const auto& [_, id] : map.doors()) {
if (id > next_id_) {
next_id_ = id;
}
}
for (const auto& [_, room] : map.rooms()) {
for (const auto& [_, id] : room.panels()) {
if (id > next_id_) {
next_id_ = id;
}
}
}
}
for (const auto& [_, id] : id_mappings_.special()) {
if (id > next_id_) {
next_id_ = id;
}
}
next_id_++;
}
void WriteIds(std::filesystem::path path) {
std::string output;
google::protobuf::TextFormat::PrintToString(id_mappings_, &output);
{
std::ofstream outputfile(path.string());
outputfile << output;
}
}
void ProcessMaps(std::filesystem::path path) {
std::filesystem::path maps_dir = path / "maps";
for (auto const& dir_entry :
std::filesystem::directory_iterator(maps_dir)) {
ProcessMap(dir_entry.path());
}
}
void ProcessMap(std::filesystem::path path) {
std::string map_name = path.filename();
ProcessDoorsFile(path / "doors.txtpb", map_name);
ProcessRooms(path / "rooms", map_name);
}
void ProcessDoorsFile(std::filesystem::path path,
const std::string& current_map_name) {
if (!std::filesystem::exists(path)) {
return;
}
auto doors = ReadMessageFromFile<HumanDoors>(path.string());
for (const HumanDoor& door : doors.doors()) {
ProcessDoor(door, current_map_name);
}
}
void ProcessDoor(const HumanDoor& h_door,
const std::string& current_map_name) {
if (h_door.type() == DoorType::EVENT) {
return;
}
if (!id_mappings_.maps().contains(current_map_name) ||
!id_mappings_.maps()
.at(current_map_name)
.doors()
.contains(h_door.name())) {
auto& maps = *id_mappings_.mutable_maps();
auto& doors = *maps[current_map_name].mutable_doors();
doors[h_door.name()] = next_id_++;
}
}
void ProcessRooms(std::filesystem::path path,
const std::string& current_map_name) {
for (auto const& dir_entry : std::filesystem::directory_iterator(path)) {
auto room = ReadMessageFromFile<HumanRoom>(dir_entry.path().string());
ProcessRoom(room, current_map_name);
}
}
void ProcessRoom(const HumanRoom& h_room,
const std::string& current_map_name) {
for (const HumanPanel& h_panel : h_room.panels()) {
if (!id_mappings_.maps().contains(current_map_name) ||
!id_mappings_.maps()
.at(current_map_name)
.rooms()
.contains(h_room.name()) ||
!id_mappings_.maps()
.at(current_map_name)
.rooms()
.at(h_room.name())
.panels()
.contains(h_panel.name())) {
auto& maps = *id_mappings_.mutable_maps();
auto& rooms = *maps[current_map_name].mutable_rooms();
auto& panels = *rooms[h_room.name()].mutable_panels();
panels[h_panel.name()] = next_id_++;
}
}
}
private:
std::string mapdir_;
uint64_t next_id_ = 0;
IdMappings id_mappings_;
};
} // namespace
} // namespace com::fourisland::lingo2_archipelago
int main(int argc, char** argv) {
if (argc != 2) {
std::cout << "Incorrect argument count." << std::endl;
std::cout << "Usage: assign_ids [path to map directory]" << std::endl;
return 1;
}
std::string mapdir = argv[1];
com::fourisland::lingo2_archipelago::AssignIds assign_ids(mapdir);
assign_ids.Run();
return 0;
}
|