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