about summary refs log tree commit diff stats
path: root/tools/validator/human_processor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/validator/human_processor.cpp')
-rw-r--r--tools/validator/human_processor.cpp640
1 files changed, 640 insertions, 0 deletions
diff --git a/tools/validator/human_processor.cpp b/tools/validator/human_processor.cpp new file mode 100644 index 0000000..2c978bf --- /dev/null +++ b/tools/validator/human_processor.cpp
@@ -0,0 +1,640 @@
1#include "human_processor.h"
2
3#include <fmt/core.h>
4#include <google/protobuf/message.h>
5#include <google/protobuf/text_format.h>
6
7#include <filesystem>
8#include <fstream>
9#include <iostream>
10#include <map>
11#include <optional>
12#include <sstream>
13#include <string>
14
15#include "structs.h"
16#include "util/ids_yaml_format.h"
17
18namespace com::fourisland::lingo2_archipelago {
19namespace {
20
21template <typename T>
22T ReadMessageFromFile(const std::string& path) {
23 std::cout << "Processing " << path << std::endl;
24
25 std::ifstream file(path);
26 std::stringstream buffer;
27 buffer << file.rdbuf();
28
29 T message;
30 google::protobuf::TextFormat::ParseFromString(buffer.str(), &message);
31
32 return message;
33}
34
35class HumanProcessor {
36 public:
37 HumanProcessor(const std::string& mapdir, CollectedInfo& info)
38 : mapdir_(mapdir), info_(info) {}
39
40 void Run() {
41 std::filesystem::path datadir_path = mapdir_;
42
43 ProcessConnectionsFile(datadir_path / "connections.txtpb", std::nullopt);
44 ProcessMaps(datadir_path);
45 ProcessProgressivesFile(datadir_path / "progressives.txtpb");
46 ProcessDoorGroupsFile(datadir_path / "door_groups.txtpb");
47 ProcessIdsFile(datadir_path / "ids.yaml");
48 }
49
50 private:
51 void ProcessMaps(std::filesystem::path path) {
52 std::filesystem::path maps_dir = path / "maps";
53 for (auto const& dir_entry :
54 std::filesystem::directory_iterator(maps_dir)) {
55 ProcessMap(dir_entry.path());
56 }
57 }
58
59 void ProcessMap(std::filesystem::path path) {
60 std::string map_name = path.filename().string();
61
62 ProcessMetadataFile(path / "metadata.txtpb", map_name);
63 ProcessConnectionsFile(path / "connections.txtpb", map_name);
64 ProcessDoorsFile(path / "doors.txtpb", map_name);
65 ProcessRooms(path / "rooms", map_name);
66 }
67
68 void ProcessMetadataFile(std::filesystem::path path,
69 const std::string& current_map_name) {
70 if (!std::filesystem::exists(path)) {
71 return;
72 }
73
74 MapInfo& map_info = info_.maps[current_map_name];
75
76 auto metadata = ReadMessageFromFile<HumanMap>(path.string());
77 for (const std::string& path : metadata.excluded_nodes()) {
78 map_info.game_nodes[path].uses++;
79 }
80 }
81
82 void ProcessRooms(std::filesystem::path path,
83 const std::string& current_map_name) {
84 for (auto const& dir_entry : std::filesystem::directory_iterator(path)) {
85 auto room = ReadMessageFromFile<HumanRoom>(dir_entry.path().string());
86 ProcessRoom(room, current_map_name);
87 }
88 }
89
90 void ProcessRoom(const HumanRoom& h_room,
91 const std::string& current_map_name) {
92 RoomIdentifier room_identifier;
93 room_identifier.set_map(current_map_name);
94 room_identifier.set_name(h_room.name());
95
96 RoomInfo& room_info = info_.rooms[room_identifier];
97 room_info.definitions.push_back(h_room);
98
99 for (const HumanPanel& h_panel : h_room.panels()) {
100 ProcessPanel(h_panel, current_map_name, h_room);
101 }
102
103 for (const HumanPainting& h_painting : h_room.paintings()) {
104 ProcessPainting(h_painting, current_map_name, h_room.name());
105 }
106
107 for (const HumanPort& h_port : h_room.ports()) {
108 ProcessPort(h_port, current_map_name, h_room.name());
109 }
110
111 for (const HumanLetter& h_letter : h_room.letters()) {
112 ProcessLetter(h_letter, current_map_name, h_room.name());
113 }
114
115 for (const HumanMastery& h_mastery : h_room.masteries()) {
116 ProcessMastery(h_mastery, current_map_name, h_room.name());
117 }
118
119 for (const HumanKeyholder& h_keyholder : h_room.keyholders()) {
120 ProcessKeyholder(h_keyholder, current_map_name, h_room.name());
121 }
122
123 for (const HumanEnding& h_ending : h_room.endings()) {
124 ProcessEnding(h_ending, current_map_name, h_room.name());
125 }
126 }
127
128 void ProcessPanel(const HumanPanel& h_panel,
129 const std::string& current_map_name,
130 const HumanRoom& h_room) {
131 PanelIdentifier panel_identifier;
132 panel_identifier.set_map(current_map_name);
133 panel_identifier.set_room(h_room.name());
134 panel_identifier.set_name(h_panel.name());
135
136 PanelInfo& panel_info = info_.panels[panel_identifier];
137 panel_info.definitions.push_back(h_panel);
138
139 MapInfo& map_info = info_.maps[current_map_name];
140 map_info.game_nodes[h_panel.path()].uses++;
141
142 for (const Proxy& h_proxy : h_panel.proxies()) {
143 ProxyInfo& proxy_info = panel_info.proxies[h_proxy.answer()];
144 proxy_info.definitions.push_back(h_proxy);
145
146 map_info.game_nodes[h_proxy.path()].uses++;
147 }
148
149 if (h_panel.has_required_door()) {
150 DoorIdentifier required_door_identifier =
151 *GetCompleteDoorIdentifier(h_panel.required_door(), current_map_name);
152 DoorInfo& required_door_info = info_.doors[required_door_identifier];
153 required_door_info.panels_referenced_by.push_back(panel_identifier);
154 }
155
156 if (h_panel.has_required_room()) {
157 RoomIdentifier required_room_identifier =
158 *GetCompleteRoomIdentifier(h_panel.required_room(), current_map_name);
159 RoomInfo& required_room_info = info_.rooms[required_room_identifier];
160 required_room_info.panels_referenced_by.push_back(panel_identifier);
161 }
162
163 std::string map_area_name = current_map_name;
164 if (h_room.has_panel_display_name()) {
165 map_area_name =
166 fmt::format("{} ({})", current_map_name, h_room.panel_display_name());
167 }
168
169 panel_info.map_area_name = map_area_name;
170
171 std::string panelsanity_name;
172 if (h_panel.has_display_name()) {
173 panelsanity_name =
174 fmt::format("{} - {}", map_area_name, h_panel.display_name());
175 } else {
176 panelsanity_name = fmt::format("{} - {}", map_area_name, h_panel.name());
177 }
178 info_.panel_names[panelsanity_name].panels_used_by.push_back(
179 panel_identifier);
180 }
181
182 void ProcessPainting(const HumanPainting& h_painting,
183 const std::string& current_map_name,
184 const std::string& current_room_name) {
185 PaintingIdentifier painting_identifier;
186 painting_identifier.set_map(current_map_name);
187 painting_identifier.set_room(current_room_name);
188 painting_identifier.set_name(h_painting.name());
189
190 PaintingInfo& painting_info = info_.paintings[painting_identifier];
191 painting_info.definitions.push_back(h_painting);
192
193 MapInfo& map_info = info_.maps[current_map_name];
194 map_info.game_nodes[h_painting.path()].uses++;
195
196 if (h_painting.has_required_door()) {
197 DoorIdentifier required_door_identifier = *GetCompleteDoorIdentifier(
198 h_painting.required_door(), current_map_name);
199 DoorInfo& required_door_info = info_.doors[required_door_identifier];
200 required_door_info.paintings_referenced_by.push_back(painting_identifier);
201 }
202 }
203
204 void ProcessPort(const HumanPort& h_port, const std::string& current_map_name,
205 const std::string& current_room_name) {
206 PortIdentifier port_identifier;
207 port_identifier.set_map(current_map_name);
208 port_identifier.set_room(current_room_name);
209 port_identifier.set_name(h_port.name());
210
211 PortInfo& port_info = info_.ports[port_identifier];
212 port_info.definitions.push_back(h_port);
213
214 MapInfo& map_info = info_.maps[current_map_name];
215 map_info.game_nodes[h_port.path()].uses++;
216
217 if (h_port.has_required_door()) {
218 DoorIdentifier required_door_identifier =
219 *GetCompleteDoorIdentifier(h_port.required_door(), current_map_name);
220 DoorInfo& required_door_info = info_.doors[required_door_identifier];
221 required_door_info.ports_referenced_by.push_back(port_identifier);
222 }
223 }
224
225 void ProcessLetter(const HumanLetter& h_letter,
226 const std::string& current_map_name,
227 const std::string& current_room_name) {
228 LetterIdentifier letter_identifier =
229 std::make_tuple(h_letter.key()[0], h_letter.level2());
230 LetterInfo& letter_info = info_.letters[letter_identifier];
231
232 RoomIdentifier room_identifier;
233 room_identifier.set_map(current_map_name);
234 room_identifier.set_name(current_room_name);
235 letter_info.defined_in.push_back(room_identifier);
236
237 MapInfo& map_info = info_.maps[current_map_name];
238 map_info.game_nodes[h_letter.path()].uses++;
239 }
240
241 void ProcessMastery(const HumanMastery& h_mastery,
242 const std::string& current_map_name,
243 const std::string& current_room_name) {
244 MapInfo& map_info = info_.maps[current_map_name];
245 map_info.game_nodes[h_mastery.path()].uses++;
246 }
247
248 void ProcessKeyholder(const HumanKeyholder& h_keyholder,
249 const std::string& current_map_name,
250 const std::string& current_room_name) {
251 KeyholderIdentifier keyholder_identifier;
252 keyholder_identifier.set_map(current_map_name);
253 keyholder_identifier.set_room(current_room_name);
254 keyholder_identifier.set_name(h_keyholder.name());
255
256 KeyholderInfo& keyholder_info = info_.keyholders[keyholder_identifier];
257 keyholder_info.definitions.push_back(h_keyholder);
258
259 MapInfo& map_info = info_.maps[current_map_name];
260 map_info.game_nodes[h_keyholder.path()].uses++;
261 }
262
263 void ProcessEnding(const HumanEnding& h_ending,
264 const std::string& current_map_name,
265 const std::string& current_room_name) {
266 EndingInfo& ending_info = info_.endings[h_ending.name()];
267
268 RoomIdentifier room_identifier;
269 room_identifier.set_map(current_map_name);
270 room_identifier.set_name(current_room_name);
271 ending_info.defined_in.push_back(room_identifier);
272
273 MapInfo& map_info = info_.maps[current_map_name];
274 map_info.game_nodes[h_ending.path()].uses++;
275 }
276
277 void ProcessDoorsFile(std::filesystem::path path,
278 const std::string& current_map_name) {
279 if (!std::filesystem::exists(path)) {
280 return;
281 }
282
283 auto doors = ReadMessageFromFile<HumanDoors>(path.string());
284
285 for (const HumanDoor& door : doors.doors()) {
286 ProcessDoor(door, current_map_name);
287 }
288 }
289
290 void ProcessDoor(const HumanDoor& h_door,
291 const std::string& current_map_name) {
292 DoorIdentifier door_identifier;
293 door_identifier.set_map(current_map_name);
294 door_identifier.set_name(h_door.name());
295
296 DoorInfo& door_info = info_.doors[door_identifier];
297 door_info.definitions.push_back(h_door);
298
299 if (h_door.has_location_room()) {
300 RoomIdentifier location_room_identifier;
301 location_room_identifier.set_map(current_map_name);
302 location_room_identifier.set_name(h_door.location_room());
303 info_.rooms[location_room_identifier].doors_referenced_by.push_back(
304 door_identifier);
305 }
306
307 for (const PaintingIdentifier& pi : h_door.move_paintings()) {
308 auto complete_painting_identifier =
309 GetCompletePaintingIdentifier(pi, current_map_name, std::nullopt);
310 if (complete_painting_identifier) {
311 PaintingInfo& move_painting_info =
312 info_.paintings[*complete_painting_identifier];
313 move_painting_info.doors_referenced_by.push_back(door_identifier);
314 } else {
315 door_info.malformed_identifiers.paintings.push_back(pi);
316 }
317 }
318
319 for (const PanelIdentifier& pi : h_door.panels()) {
320 auto complete_panel_identifier = GetCompletePanelIdentifierWithoutAnswer(
321 pi, current_map_name, std::nullopt);
322 if (complete_panel_identifier) {
323 PanelInfo& panel_info = info_.panels[*complete_panel_identifier];
324 panel_info.doors_referenced_by.push_back(door_identifier);
325
326 if (pi.has_answer()) {
327 panel_info.proxies[pi.answer()].doors_referenced_by.push_back(
328 door_identifier);
329 }
330 } else {
331 door_info.malformed_identifiers.panels.push_back(pi);
332 }
333 }
334
335 for (const KeyholderIdentifier& ki : h_door.keyholders()) {
336 auto complete_keyholder_identifier =
337 GetCompleteKeyholderIdentifierWithoutKey(ki, current_map_name,
338 std::nullopt);
339 if (complete_keyholder_identifier) {
340 KeyholderInfo& keyholder_info =
341 info_.keyholders[*complete_keyholder_identifier];
342 keyholder_info.doors_referenced_by.push_back(door_identifier);
343 } else {
344 door_info.malformed_identifiers.keyholders.push_back(ki);
345 }
346 }
347
348 for (const RoomIdentifier& ri : h_door.rooms()) {
349 RoomIdentifier complete_room_identifier =
350 *GetCompleteRoomIdentifier(ri, current_map_name);
351 RoomInfo& room_info = info_.rooms[complete_room_identifier];
352 room_info.doors_referenced_by.push_back(door_identifier);
353 }
354
355 for (const DoorIdentifier& di : h_door.doors()) {
356 DoorIdentifier complete_door_identifier =
357 *GetCompleteDoorIdentifier(di, current_map_name);
358 DoorInfo& other_door_info = info_.doors[complete_door_identifier];
359 other_door_info.doors_referenced_by.push_back(door_identifier);
360 }
361
362 for (const std::string& ei : h_door.endings()) {
363 EndingInfo& ending_info = info_.endings[ei];
364 ending_info.doors_referenced_by.push_back(door_identifier);
365 }
366 }
367
368 void ProcessConnectionsFile(std::filesystem::path path,
369 std::optional<std::string> current_map_name) {
370 if (!std::filesystem::exists(path)) {
371 return;
372 }
373
374 auto connections = ReadMessageFromFile<HumanConnections>(path.string());
375
376 for (const HumanConnection& connection : connections.connections()) {
377 ProcessConnection(connection, current_map_name);
378 }
379 }
380
381 void ProcessConnection(const HumanConnection& human_connection,
382 const std::optional<std::string>& current_map_name) {
383 if (human_connection.has_from_room()) {
384 if (current_map_name) {
385 RoomIdentifier room_identifier;
386 room_identifier.set_map(*current_map_name);
387 room_identifier.set_name(human_connection.from_room());
388
389 RoomInfo& room_info = info_.rooms[room_identifier];
390 room_info.connections_referenced_by.push_back(human_connection);
391 } else {
392 // Not sure where else to store this right now.
393 std::cout << "A global connection used from_room." << std::endl;
394 }
395 } else if (human_connection.has_from()) {
396 ProcessSingleConnection(human_connection, human_connection.from(),
397 current_map_name,
398 /*is_target=*/!human_connection.oneway() &&
399 !human_connection.bypass_target_door());
400 }
401
402 if (human_connection.has_to_room()) {
403 if (current_map_name) {
404 RoomIdentifier room_identifier;
405 room_identifier.set_map(*current_map_name);
406 room_identifier.set_name(human_connection.to_room());
407
408 RoomInfo& room_info = info_.rooms[room_identifier];
409 room_info.connections_referenced_by.push_back(human_connection);
410 } else {
411 // Not sure where else to store this right now.
412 std::cout << "A global connection used to_room." << std::endl;
413 }
414 } else if (human_connection.has_to()) {
415 ProcessSingleConnection(
416 human_connection, human_connection.to(), current_map_name,
417 /*is_target=*/!human_connection.bypass_target_door());
418 }
419
420 if (human_connection.has_door()) {
421 auto door_identifier =
422 GetCompleteDoorIdentifier(human_connection.door(), current_map_name);
423 if (door_identifier) {
424 DoorInfo& door_info = info_.doors[*door_identifier];
425 door_info.connections_referenced_by.push_back(human_connection);
426 } else {
427 // Not sure where else to store this right now.
428 std::cout
429 << "A connection used the following malformed door identifier: "
430 << human_connection.door().ShortDebugString() << std::endl;
431 }
432 }
433 }
434
435 void ProcessSingleConnection(
436 const HumanConnection& human_connection,
437 const HumanConnection::Endpoint& endpoint,
438 const std::optional<std::string>& current_map_name, bool is_target) {
439 if (endpoint.has_room()) {
440 auto room_identifier =
441 GetCompleteRoomIdentifier(endpoint.room(), current_map_name);
442 if (room_identifier) {
443 RoomInfo& room_info = info_.rooms[*room_identifier];
444 room_info.connections_referenced_by.push_back(human_connection);
445 } else {
446 // Not sure where else to store this right now.
447 std::cout
448 << "A connection used the following malformed room identifier: "
449 << endpoint.room().ShortDebugString() << std::endl;
450 }
451 } else if (endpoint.has_painting()) {
452 auto painting_identifier = GetCompletePaintingIdentifier(
453 endpoint.painting(), current_map_name, std::nullopt);
454 if (painting_identifier) {
455 PaintingInfo& painting_info = info_.paintings[*painting_identifier];
456 painting_info.connections_referenced_by.push_back(human_connection);
457
458 if (is_target) {
459 painting_info.target_connections_referenced_by.push_back(
460 human_connection);
461 }
462 } else {
463 // Not sure where else to store this right now.
464 std::cout
465 << "A connection used the following malformed painting identifier: "
466 << endpoint.painting().ShortDebugString() << std::endl;
467 }
468 } else if (endpoint.has_port()) {
469 auto port_identifier = GetCompletePortIdentifier(
470 endpoint.port(), current_map_name, std::nullopt);
471 if (port_identifier) {
472 PortInfo& port_info = info_.ports[*port_identifier];
473 port_info.connections_referenced_by.push_back(human_connection);
474
475 if (is_target) {
476 port_info.target_connections_referenced_by.push_back(
477 human_connection);
478 }
479 } else {
480 // Not sure where else to store this right now.
481 std::cout
482 << "A connection used the following malformed port identifier: "
483 << endpoint.port().ShortDebugString() << std::endl;
484 }
485 } else if (endpoint.has_panel()) {
486 auto panel_identifier = GetCompletePanelIdentifierWithoutAnswer(
487 endpoint.panel(), current_map_name, std::nullopt);
488 if (panel_identifier) {
489 PanelInfo& panel_info = info_.panels[*panel_identifier];
490 panel_info.connections_referenced_by.push_back(human_connection);
491
492 if (endpoint.panel().has_answer()) {
493 panel_info.proxies[endpoint.panel().answer()]
494 .connections_referenced_by.push_back(human_connection);
495 }
496
497 if (is_target) {
498 panel_info.target_connections_referenced_by.push_back(
499 human_connection);
500 }
501 }
502 }
503 }
504
505 void ProcessProgressivesFile(std::filesystem::path path) {
506 if (!std::filesystem::exists(path)) {
507 return;
508 }
509
510 auto h_progs = ReadMessageFromFile<HumanProgressives>(path.string());
511
512 for (const HumanProgressive& h_prog : h_progs.progressives()) {
513 ProcessProgressive(h_prog);
514 }
515 }
516
517 void ProcessProgressive(const HumanProgressive& h_prog) {
518 ProgressiveInfo& prog_info = info_.progressives[h_prog.name()];
519 prog_info.definitions.push_back(h_prog);
520
521 for (const DoorIdentifier& di : h_prog.doors()) {
522 if (!di.has_map()) {
523 prog_info.malformed_doors.push_back(di);
524 continue;
525 }
526
527 DoorInfo& door_info = info_.doors[di];
528 door_info.progressives_referenced_by.push_back(h_prog.name());
529 }
530 }
531
532 void ProcessDoorGroupsFile(std::filesystem::path path) {
533 if (!std::filesystem::exists(path)) {
534 return;
535 }
536
537 auto h_groups = ReadMessageFromFile<HumanDoorGroups>(path.string());
538
539 for (const HumanDoorGroup& h_group : h_groups.door_groups()) {
540 ProcessDoorGroup(h_group);
541 }
542 }
543
544 void ProcessDoorGroup(const HumanDoorGroup& h_group) {
545 DoorGroupInfo& group_info = info_.door_groups[h_group.name()];
546 group_info.definitions.push_back(h_group);
547
548 for (const DoorIdentifier& di : h_group.doors()) {
549 if (!di.has_map()) {
550 group_info.malformed_doors.push_back(di);
551 continue;
552 }
553
554 DoorInfo& door_info = info_.doors[di];
555 door_info.door_groups_referenced_by.push_back(h_group.name());
556 }
557 }
558
559 void ProcessIdsFile(std::filesystem::path path) {
560 auto ids = ReadIdsFromYaml(path.string());
561
562 DoorIdentifier di;
563 PanelIdentifier pi;
564 KeyholderIdentifier ki;
565
566 for (const auto& [map_name, map] : ids.maps()) {
567 di.set_map(map_name);
568 pi.set_map(map_name);
569 ki.set_map(map_name);
570
571 for (const auto& [door_name, ap_id] : map.doors()) {
572 di.set_name(door_name);
573
574 DoorInfo& door_info = info_.doors[di];
575 door_info.has_id = true;
576 }
577
578 for (const auto& [room_name, room] : map.rooms()) {
579 pi.set_room(room_name);
580 ki.set_room(room_name);
581
582 for (const auto& [panel_name, ap_id] : room.panels()) {
583 pi.set_name(panel_name);
584
585 PanelInfo& panel_info = info_.panels[pi];
586 panel_info.has_id = true;
587 }
588
589 for (const auto& [mastery_name, ap_id] : room.masteries()) {
590 // TODO: Mastery
591 }
592
593 for (const auto& [keyholder_name, ap_id] : room.keyholders()) {
594 ki.set_name(keyholder_name);
595
596 KeyholderInfo& keyholder_info = info_.keyholders[ki];
597 keyholder_info.has_id = true;
598 }
599 }
600 }
601
602 for (const auto& [tag, id] : ids.special()) {
603 // TODO: Specials
604 }
605
606 for (const auto& [letter_name, ap_id] : ids.letters()) {
607 LetterIdentifier li =
608 std::make_tuple(letter_name[0], letter_name[1] == '2');
609 LetterInfo& letter_info = info_.letters[li];
610 letter_info.has_id = true;
611 }
612
613 for (const auto& [ending_name, ap_id] : ids.endings()) {
614 EndingInfo& ending_info = info_.endings[ending_name];
615 ending_info.has_id = true;
616 }
617
618 for (const auto& [prog_name, ap_id] : ids.progressives()) {
619 ProgressiveInfo& prog_info = info_.progressives[prog_name];
620 prog_info.has_id = true;
621 }
622
623 for (const auto& [group_name, ap_id] : ids.door_groups()) {
624 DoorGroupInfo& group_info = info_.door_groups[group_name];
625 group_info.has_id = true;
626 }
627 }
628
629 std::string mapdir_;
630 CollectedInfo& info_;
631};
632
633} // namespace
634
635void ProcessHumanData(const std::string& mapdir, CollectedInfo& info) {
636 HumanProcessor human_processor(mapdir, info);
637 human_processor.Run();
638}
639
640} // namespace com::fourisland::lingo2_archipelago