about summary refs log tree commit diff stats
path: root/tools/assign_ids/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/assign_ids/main.cpp')
-rw-r--r--tools/assign_ids/main.cpp229
1 files changed, 190 insertions, 39 deletions
diff --git a/tools/assign_ids/main.cpp b/tools/assign_ids/main.cpp index 2471bc5..4cf7c3f 100644 --- a/tools/assign_ids/main.cpp +++ b/tools/assign_ids/main.cpp
@@ -2,6 +2,7 @@
2#include <google/protobuf/text_format.h> 2#include <google/protobuf/text_format.h>
3 3
4#include <cstdint> 4#include <cstdint>
5#include <filesystem>
5#include <fstream> 6#include <fstream>
6#include <iostream> 7#include <iostream>
7#include <map> 8#include <map>
@@ -40,6 +41,10 @@ class AssignIds {
40 ReadIds(ids_path); 41 ReadIds(ids_path);
41 42
42 ProcessMaps(datadir_path); 43 ProcessMaps(datadir_path);
44 ProcessSpecialIds();
45 ProcessProgressivesFile(datadir_path / "progressives.txtpb");
46 ProcessDoorGroupsFile(datadir_path / "door_groups.txtpb");
47 ProcessGlobalMetadataFile(datadir_path / "metadata.txtpb");
43 48
44 WriteIds(ids_path); 49 WriteIds(ids_path);
45 50
@@ -54,44 +59,27 @@ class AssignIds {
54 id_mappings_ = ReadIdsFromYaml(path.string()); 59 id_mappings_ = ReadIdsFromYaml(path.string());
55 60
56 for (const auto& [_, map] : id_mappings_.maps()) { 61 for (const auto& [_, map] : id_mappings_.maps()) {
57 for (const auto& [_, id] : map.doors()) { 62 UpdateNextId(map.doors());
58 if (id > next_id_) {
59 next_id_ = id;
60 }
61 }
62 63
63 for (const auto& [_, room] : map.rooms()) { 64 for (const auto& [_, room] : map.rooms()) {
64 for (const auto& [_, id] : room.panels()) { 65 UpdateNextId(room.panels());
65 if (id > next_id_) { 66 UpdateNextId(room.masteries());
66 next_id_ = id; 67 UpdateNextId(room.keyholders());
67 } 68 UpdateNextId(room.ports());
68 }
69
70 for (const auto& [_, id] : room.masteries()) {
71 if (id > next_id_) {
72 next_id_ = id;
73 }
74 }
75 } 69 }
76 } 70 }
77 71
78 for (const auto& [_, id] : id_mappings_.special()) { 72 UpdateNextId(id_mappings_.special());
79 if (id > next_id_) { 73 UpdateNextId(id_mappings_.letters());
80 next_id_ = id; 74 UpdateNextId(id_mappings_.endings());
81 } 75 UpdateNextId(id_mappings_.progressives());
82 } 76 UpdateNextId(id_mappings_.door_groups());
83
84 for (const auto& [_, id] : id_mappings_.letters()) {
85 if (id > next_id_) {
86 next_id_ = id;
87 }
88 }
89 77
90 next_id_++; 78 next_id_++;
91 } 79 }
92 80
93 void WriteIds(std::filesystem::path path) { 81 void WriteIds(std::filesystem::path path) {
94 WriteIdsAsYaml(id_mappings_, path.string()); 82 WriteIdsAsYaml(output_, path.string());
95 } 83 }
96 84
97 void ProcessMaps(std::filesystem::path path) { 85 void ProcessMaps(std::filesystem::path path) {
@@ -124,18 +112,23 @@ class AssignIds {
124 112
125 void ProcessDoor(const HumanDoor& h_door, 113 void ProcessDoor(const HumanDoor& h_door,
126 const std::string& current_map_name) { 114 const std::string& current_map_name) {
127 if (h_door.type() == DoorType::EVENT) { 115 if (h_door.type() == DoorType::EVENT && !h_door.latch() &&
116 !h_door.legacy_location()) {
128 return; 117 return;
129 } 118 }
130 119
120 auto& maps = *output_.mutable_maps();
121 auto& doors = *maps[current_map_name].mutable_doors();
122
131 if (!id_mappings_.maps().contains(current_map_name) || 123 if (!id_mappings_.maps().contains(current_map_name) ||
132 !id_mappings_.maps() 124 !id_mappings_.maps()
133 .at(current_map_name) 125 .at(current_map_name)
134 .doors() 126 .doors()
135 .contains(h_door.name())) { 127 .contains(h_door.name())) {
136 auto& maps = *id_mappings_.mutable_maps();
137 auto& doors = *maps[current_map_name].mutable_doors();
138 doors[h_door.name()] = next_id_++; 128 doors[h_door.name()] = next_id_++;
129 } else {
130 doors[h_door.name()] =
131 id_mappings_.maps().at(current_map_name).doors().at(h_door.name());
139 } 132 }
140 } 133 }
141 134
@@ -150,6 +143,10 @@ class AssignIds {
150 void ProcessRoom(const HumanRoom& h_room, 143 void ProcessRoom(const HumanRoom& h_room,
151 const std::string& current_map_name) { 144 const std::string& current_map_name) {
152 for (const HumanPanel& h_panel : h_room.panels()) { 145 for (const HumanPanel& h_panel : h_room.panels()) {
146 auto& maps = *output_.mutable_maps();
147 auto& rooms = *maps[current_map_name].mutable_rooms();
148 auto& panels = *rooms[h_room.name()].mutable_panels();
149
153 if (!id_mappings_.maps().contains(current_map_name) || 150 if (!id_mappings_.maps().contains(current_map_name) ||
154 !id_mappings_.maps() 151 !id_mappings_.maps()
155 .at(current_map_name) 152 .at(current_map_name)
@@ -161,23 +158,33 @@ class AssignIds {
161 .at(h_room.name()) 158 .at(h_room.name())
162 .panels() 159 .panels()
163 .contains(h_panel.name())) { 160 .contains(h_panel.name())) {
164 auto& maps = *id_mappings_.mutable_maps();
165 auto& rooms = *maps[current_map_name].mutable_rooms();
166 auto& panels = *rooms[h_room.name()].mutable_panels();
167 panels[h_panel.name()] = next_id_++; 161 panels[h_panel.name()] = next_id_++;
162 } else {
163 panels[h_panel.name()] = id_mappings_.maps()
164 .at(current_map_name)
165 .rooms()
166 .at(h_room.name())
167 .panels()
168 .at(h_panel.name());
168 } 169 }
169 } 170 }
170 171
171 for (const HumanLetter& h_letter : h_room.letters()) { 172 for (const HumanLetter& h_letter : h_room.letters()) {
172 std::string lettername = GetLetterName(h_letter.key(), h_letter.level2()); 173 std::string lettername = GetLetterName(h_letter.key(), h_letter.level2());
173 174
175 auto& letters = *output_.mutable_letters();
174 if (!id_mappings_.letters().contains(lettername)) { 176 if (!id_mappings_.letters().contains(lettername)) {
175 auto& letters = *id_mappings_.mutable_letters();
176 letters[lettername] = next_id_++; 177 letters[lettername] = next_id_++;
178 } else {
179 letters[lettername] = id_mappings_.letters().at(lettername);
177 } 180 }
178 } 181 }
179 182
180 for (const HumanMastery& h_mastery : h_room.masteries()) { 183 for (const HumanMastery& h_mastery : h_room.masteries()) {
184 auto& maps = *output_.mutable_maps();
185 auto& rooms = *maps[current_map_name].mutable_rooms();
186 auto& masteries = *rooms[h_room.name()].mutable_masteries();
187
181 if (!id_mappings_.maps().contains(current_map_name) || 188 if (!id_mappings_.maps().contains(current_map_name) ||
182 !id_mappings_.maps() 189 !id_mappings_.maps()
183 .at(current_map_name) 190 .at(current_map_name)
@@ -189,20 +196,164 @@ class AssignIds {
189 .at(h_room.name()) 196 .at(h_room.name())
190 .masteries() 197 .masteries()
191 .contains(h_mastery.name())) { 198 .contains(h_mastery.name())) {
192 auto& maps = *id_mappings_.mutable_maps();
193 auto& rooms = *maps[current_map_name].mutable_rooms();
194 auto& masteries = *rooms[h_room.name()].mutable_masteries();
195 masteries[h_mastery.name()] = next_id_++; 199 masteries[h_mastery.name()] = next_id_++;
200 } else {
201 masteries[h_mastery.name()] = id_mappings_.maps()
202 .at(current_map_name)
203 .rooms()
204 .at(h_room.name())
205 .masteries()
206 .at(h_mastery.name());
207 }
208 }
209
210 for (const HumanEnding& h_ending : h_room.endings()) {
211 auto& endings = *output_.mutable_endings();
212
213 if (!id_mappings_.endings().contains(h_ending.name())) {
214 endings[h_ending.name()] = next_id_++;
215 } else {
216 endings[h_ending.name()] = id_mappings_.endings().at(h_ending.name());
217 }
218 }
219
220 for (const HumanKeyholder& h_keyholder : h_room.keyholders()) {
221 if (!h_keyholder.has_key()) {
222 continue;
223 }
224
225 auto& maps = *output_.mutable_maps();
226 auto& rooms = *maps[current_map_name].mutable_rooms();
227 auto& keyholders = *rooms[h_room.name()].mutable_keyholders();
228
229 if (!id_mappings_.maps().contains(current_map_name) ||
230 !id_mappings_.maps()
231 .at(current_map_name)
232 .rooms()
233 .contains(h_room.name()) ||
234 !id_mappings_.maps()
235 .at(current_map_name)
236 .rooms()
237 .at(h_room.name())
238 .keyholders()
239 .contains(h_keyholder.name())) {
240 keyholders[h_keyholder.name()] = next_id_++;
241 } else {
242 keyholders[h_keyholder.name()] = id_mappings_.maps()
243 .at(current_map_name)
244 .rooms()
245 .at(h_room.name())
246 .keyholders()
247 .at(h_keyholder.name());
248 }
249 }
250
251 for (const HumanPort& h_port : h_room.ports()) {
252 if (h_port.no_shuffle()) {
253 continue;
254 }
255
256 auto& maps = *output_.mutable_maps();
257 auto& rooms = *maps[current_map_name].mutable_rooms();
258 auto& ports = *rooms[h_room.name()].mutable_ports();
259
260 if (!id_mappings_.maps().contains(current_map_name) ||
261 !id_mappings_.maps()
262 .at(current_map_name)
263 .rooms()
264 .contains(h_room.name()) ||
265 !id_mappings_.maps()
266 .at(current_map_name)
267 .rooms()
268 .at(h_room.name())
269 .ports()
270 .contains(h_port.name())) {
271 ports[h_port.name()] = next_id_++;
272 } else {
273 ports[h_port.name()] = id_mappings_.maps()
274 .at(current_map_name)
275 .rooms()
276 .at(h_room.name())
277 .ports()
278 .at(h_port.name());
279 }
280 }
281 }
282
283 void ProcessSpecialIds() {
284 auto& specials = *output_.mutable_special();
285
286 for (const auto& [special_name, ap_id] : id_mappings_.special()) {
287 specials[special_name] = ap_id;
288 }
289 }
290
291 void ProcessProgressivesFile(std::filesystem::path path) {
292 if (!std::filesystem::exists(path)) {
293 return;
294 }
295
296 auto h_progs = ReadMessageFromFile<HumanProgressives>(path.string());
297 auto& progs = *output_.mutable_progressives();
298
299 for (const HumanProgressive& h_prog : h_progs.progressives()) {
300 if (!id_mappings_.progressives().contains(h_prog.name())) {
301 progs[h_prog.name()] = next_id_++;
302 } else {
303 progs[h_prog.name()] = id_mappings_.progressives().at(h_prog.name());
304 }
305 }
306 }
307
308 void ProcessDoorGroupsFile(std::filesystem::path path) {
309 if (!std::filesystem::exists(path)) {
310 return;
311 }
312
313 auto h_groups = ReadMessageFromFile<HumanDoorGroups>(path.string());
314 auto& groups = *output_.mutable_door_groups();
315
316 for (const HumanDoorGroup& h_group : h_groups.door_groups()) {
317 if (!id_mappings_.door_groups().contains(h_group.name())) {
318 groups[h_group.name()] = next_id_++;
319 } else {
320 groups[h_group.name()] = id_mappings_.door_groups().at(h_group.name());
321 }
322 }
323 }
324
325 void ProcessGlobalMetadataFile(std::filesystem::path path) {
326 if (!std::filesystem::exists(path)) {
327 return;
328 }
329
330 auto h_metadata = ReadMessageFromFile<HumanGlobalMetadata>(path.string());
331 auto& specials = *output_.mutable_special();
332
333 for (const std::string& h_special : h_metadata.special_names()) {
334 if (!id_mappings_.special().contains(h_special)) {
335 specials[h_special] = next_id_++;
336 } else {
337 specials[h_special] = id_mappings_.special().at(h_special);
196 } 338 }
197 } 339 }
198 } 340 }
199 341
200 private: 342 private:
343 void UpdateNextId(const google::protobuf::Map<std::string, uint64_t>& ids) {
344 for (const auto& [_, id] : ids) {
345 if (id > next_id_) {
346 next_id_ = id;
347 }
348 }
349 }
350
201 std::string mapdir_; 351 std::string mapdir_;
202 352
203 uint64_t next_id_ = 0; 353 uint64_t next_id_ = 1;
204 354
205 IdMappings id_mappings_; 355 IdMappings id_mappings_;
356 IdMappings output_;
206}; 357};
207 358
208} // namespace 359} // namespace