diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2025-08-19 20:19:48 -0400 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2025-08-19 20:19:48 -0400 |
commit | 724f8092c4808cdad47316e00949c04ee797acb5 (patch) | |
tree | 758d598fbc2d62c4620956b71b2a851791fd2dbf /tools/util/ids_yaml_format.cpp | |
parent | 511d813113b6e7986aff665603ca0196799e232d (diff) | |
download | lingo2-archipelago-724f8092c4808cdad47316e00949c04ee797acb5.tar.gz lingo2-archipelago-724f8092c4808cdad47316e00949c04ee797acb5.tar.bz2 lingo2-archipelago-724f8092c4808cdad47316e00949c04ee797acb5.zip |
Store IDs in a yaml file
This is much more efficient than the txtpb format, and we only need an interface for it in C++ since the IDs will be packed into the binary proto representation.
Diffstat (limited to 'tools/util/ids_yaml_format.cpp')
-rw-r--r-- | tools/util/ids_yaml_format.cpp | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/tools/util/ids_yaml_format.cpp b/tools/util/ids_yaml_format.cpp new file mode 100644 index 0000000..99a8890 --- /dev/null +++ b/tools/util/ids_yaml_format.cpp | |||
@@ -0,0 +1,139 @@ | |||
1 | #include "ids_yaml_format.h" | ||
2 | |||
3 | #include <yaml-cpp/yaml.h> | ||
4 | |||
5 | #include <fstream> | ||
6 | #include <functional> | ||
7 | |||
8 | namespace com::fourisland::lingo2_archipelago { | ||
9 | namespace { | ||
10 | |||
11 | template <typename T> | ||
12 | void OperateOnSortedMap( | ||
13 | const T& map, std::function<void(const std::string& name, | ||
14 | const typename T::mapped_type& value)> | ||
15 | callback) { | ||
16 | std::vector<std::string> names; | ||
17 | for (const auto& it : map) { | ||
18 | names.push_back(it.first); | ||
19 | } | ||
20 | |||
21 | std::sort(names.begin(), names.end()); | ||
22 | |||
23 | for (const std::string& name : names) { | ||
24 | callback(name, map.at(name)); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | } // namespace | ||
29 | |||
30 | IdMappings ReadIdsFromYaml(const std::string& filename) { | ||
31 | IdMappings result; | ||
32 | |||
33 | YAML::Node document = YAML::LoadFile(filename); | ||
34 | |||
35 | if (document["maps"]) { | ||
36 | for (const auto& map_it : document["maps"]) { | ||
37 | IdMappings::MapIds& map_ids = | ||
38 | (*result.mutable_maps())[map_it.first.as<std::string>()]; | ||
39 | |||
40 | if (map_it.second["rooms"]) { | ||
41 | for (const auto& room_it : map_it.second["rooms"]) { | ||
42 | IdMappings::RoomIds& room_ids = | ||
43 | (*map_ids.mutable_rooms())[room_it.first.as<std::string>()]; | ||
44 | |||
45 | if (room_it.second["panels"]) { | ||
46 | for (const auto& panel_it : room_it.second["panels"]) { | ||
47 | (*room_ids.mutable_panels())[panel_it.first.as<std::string>()] = | ||
48 | panel_it.second.as<uint64_t>(); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | if (room_it.second["masteries"]) { | ||
53 | for (const auto& mastery_it : room_it.second["masteries"]) { | ||
54 | (*room_ids | ||
55 | .mutable_masteries())[mastery_it.first.as<std::string>()] = | ||
56 | mastery_it.second.as<uint64_t>(); | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | |||
62 | if (map_it.second["doors"]) { | ||
63 | for (const auto& door_it : map_it.second["doors"]) { | ||
64 | (*map_ids.mutable_doors())[door_it.first.as<std::string>()] = | ||
65 | door_it.second.as<uint64_t>(); | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | if (document["letters"]) { | ||
72 | for (const auto& letter_it : document["letters"]) { | ||
73 | (*result.mutable_letters())[letter_it.first.as<std::string>()] = | ||
74 | letter_it.second.as<uint64_t>(); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | if (document["special"]) { | ||
79 | for (const auto& special_it : document["special"]) { | ||
80 | (*result.mutable_special())[special_it.first.as<std::string>()] = | ||
81 | special_it.second.as<uint64_t>(); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | return result; | ||
86 | } | ||
87 | |||
88 | void WriteIdsAsYaml(const IdMappings& ids, const std::string& filename) { | ||
89 | YAML::Node result; | ||
90 | |||
91 | OperateOnSortedMap(ids.maps(), [&result](const std::string& map_name, | ||
92 | const IdMappings::MapIds& map_ids) { | ||
93 | YAML::Node map_node; | ||
94 | |||
95 | OperateOnSortedMap( | ||
96 | map_ids.rooms(), [&map_node](const std::string& room_name, | ||
97 | const IdMappings::RoomIds& room_ids) { | ||
98 | YAML::Node room_node; | ||
99 | |||
100 | OperateOnSortedMap( | ||
101 | room_ids.panels(), | ||
102 | [&room_node](const std::string& panel_name, uint64_t panel_id) { | ||
103 | room_node["panels"][panel_name] = panel_id; | ||
104 | }); | ||
105 | |||
106 | OperateOnSortedMap(room_ids.masteries(), | ||
107 | [&room_node](const std::string& mastery_name, | ||
108 | uint64_t mastery_id) { | ||
109 | room_node["masteries"][mastery_name] = | ||
110 | mastery_id; | ||
111 | }); | ||
112 | |||
113 | map_node["rooms"][room_name] = std::move(room_node); | ||
114 | }); | ||
115 | |||
116 | OperateOnSortedMap( | ||
117 | map_ids.doors(), | ||
118 | [&map_node](const std::string& door_name, uint64_t door_id) { | ||
119 | map_node["doors"][door_name] = door_id; | ||
120 | }); | ||
121 | |||
122 | result["maps"][map_name] = std::move(map_node); | ||
123 | }); | ||
124 | |||
125 | OperateOnSortedMap(ids.letters(), [&result](const std::string& letter_name, | ||
126 | uint64_t letter_id) { | ||
127 | result["letters"][letter_name] = letter_id; | ||
128 | }); | ||
129 | |||
130 | OperateOnSortedMap(ids.special(), [&result](const std::string& special_name, | ||
131 | uint64_t special_id) { | ||
132 | result["special"][special_name] = special_id; | ||
133 | }); | ||
134 | |||
135 | std::ofstream output_stream(filename); | ||
136 | output_stream << result << std::endl; | ||
137 | } | ||
138 | |||
139 | } // namespace com::fourisland::lingo2_archipelago | ||