diff options
Diffstat (limited to 'tools/util')
-rw-r--r-- | tools/util/CMakeLists.txt | 4 | ||||
-rw-r--r-- | tools/util/ids_yaml_format.cpp | 139 | ||||
-rw-r--r-- | tools/util/ids_yaml_format.h | 16 |
3 files changed, 158 insertions, 1 deletions
diff --git a/tools/util/CMakeLists.txt b/tools/util/CMakeLists.txt index 4d19c3b..0859a58 100644 --- a/tools/util/CMakeLists.txt +++ b/tools/util/CMakeLists.txt | |||
@@ -1,11 +1,13 @@ | |||
1 | find_package(Protobuf REQUIRED) | 1 | find_package(Protobuf REQUIRED) |
2 | find_package(yaml-cpp REQUIRED) | ||
2 | 3 | ||
3 | add_library(util | 4 | add_library(util |
4 | godot_scene.cpp | 5 | godot_scene.cpp |
5 | identifiers.cpp | 6 | identifiers.cpp |
7 | ids_yaml_format.cpp | ||
6 | naming.cpp | 8 | naming.cpp |
7 | ) | 9 | ) |
8 | set_property(TARGET util PROPERTY CXX_STANDARD 20) | 10 | set_property(TARGET util PROPERTY CXX_STANDARD 20) |
9 | set_property(TARGET util PROPERTY CXX_STANDARD_REQUIRED ON) | 11 | set_property(TARGET util PROPERTY CXX_STANDARD_REQUIRED ON) |
10 | target_include_directories(util PUBLIC ${CMAKE_BINARY_DIR}) | 12 | target_include_directories(util PUBLIC ${CMAKE_BINARY_DIR}) |
11 | target_link_libraries(util PUBLIC protos protobuf::libprotobuf) | 13 | target_link_libraries(util PUBLIC protos protobuf::libprotobuf yaml-cpp::yaml-cpp) |
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 | ||
diff --git a/tools/util/ids_yaml_format.h b/tools/util/ids_yaml_format.h new file mode 100644 index 0000000..d926369 --- /dev/null +++ b/tools/util/ids_yaml_format.h | |||
@@ -0,0 +1,16 @@ | |||
1 | #ifndef TOOLS_UTIL_IDS_YAML_FORMAT_H_ | ||
2 | #define TOOLS_UTIL_IDS_YAML_FORMAT_H_ | ||
3 | |||
4 | #include <string> | ||
5 | |||
6 | #include "proto/human.pb.h" | ||
7 | |||
8 | namespace com::fourisland::lingo2_archipelago { | ||
9 | |||
10 | IdMappings ReadIdsFromYaml(const std::string& filename); | ||
11 | |||
12 | void WriteIdsAsYaml(const IdMappings& ids, const std::string& filename); | ||
13 | |||
14 | } // namespace com::fourisland::lingo2_archipelago | ||
15 | |||
16 | #endif /* TOOLS_UTIL_IDS_YAML_FORMAT_H_ */ | ||