diff options
Diffstat (limited to 'tools/util/ids_yaml_format.cpp')
| -rw-r--r-- | tools/util/ids_yaml_format.cpp | 190 |
1 files changed, 190 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..71bfd63 --- /dev/null +++ b/tools/util/ids_yaml_format.cpp | |||
| @@ -0,0 +1,190 @@ | |||
| 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 | if (room_it.second["keyholders"]) { | ||
| 61 | for (const auto& keyholder_it : room_it.second["keyholders"]) { | ||
| 62 | (*room_ids.mutable_keyholders())[keyholder_it.first | ||
| 63 | .as<std::string>()] = | ||
| 64 | keyholder_it.second.as<uint64_t>(); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | if (map_it.second["doors"]) { | ||
| 71 | for (const auto& door_it : map_it.second["doors"]) { | ||
| 72 | (*map_ids.mutable_doors())[door_it.first.as<std::string>()] = | ||
| 73 | door_it.second.as<uint64_t>(); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | if (document["letters"]) { | ||
| 80 | for (const auto& letter_it : document["letters"]) { | ||
| 81 | (*result.mutable_letters())[letter_it.first.as<std::string>()] = | ||
| 82 | letter_it.second.as<uint64_t>(); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | if (document["endings"]) { | ||
| 87 | for (const auto& ending_it : document["endings"]) { | ||
| 88 | (*result.mutable_endings())[ending_it.first.as<std::string>()] = | ||
| 89 | ending_it.second.as<uint64_t>(); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | if (document["special"]) { | ||
| 94 | for (const auto& special_it : document["special"]) { | ||
| 95 | (*result.mutable_special())[special_it.first.as<std::string>()] = | ||
| 96 | special_it.second.as<uint64_t>(); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | if (document["progressives"]) { | ||
| 101 | for (const auto& prog_it : document["progressives"]) { | ||
| 102 | (*result.mutable_progressives())[prog_it.first.as<std::string>()] = | ||
| 103 | prog_it.second.as<uint64_t>(); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | if (document["door_groups"]) { | ||
| 108 | for (const auto& group_it : document["door_groups"]) { | ||
| 109 | (*result.mutable_door_groups())[group_it.first.as<std::string>()] = | ||
| 110 | group_it.second.as<uint64_t>(); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | return result; | ||
| 115 | } | ||
| 116 | |||
| 117 | void WriteIdsAsYaml(const IdMappings& ids, const std::string& filename) { | ||
| 118 | YAML::Node result; | ||
| 119 | |||
| 120 | OperateOnSortedMap(ids.maps(), [&result](const std::string& map_name, | ||
| 121 | const IdMappings::MapIds& map_ids) { | ||
| 122 | YAML::Node map_node; | ||
| 123 | |||
| 124 | OperateOnSortedMap( | ||
| 125 | map_ids.rooms(), [&map_node](const std::string& room_name, | ||
| 126 | const IdMappings::RoomIds& room_ids) { | ||
| 127 | YAML::Node room_node; | ||
| 128 | |||
| 129 | OperateOnSortedMap( | ||
| 130 | room_ids.panels(), | ||
| 131 | [&room_node](const std::string& panel_name, uint64_t panel_id) { | ||
| 132 | room_node["panels"][panel_name] = panel_id; | ||
| 133 | }); | ||
| 134 | |||
| 135 | OperateOnSortedMap(room_ids.masteries(), | ||
| 136 | [&room_node](const std::string& mastery_name, | ||
| 137 | uint64_t mastery_id) { | ||
| 138 | room_node["masteries"][mastery_name] = | ||
| 139 | mastery_id; | ||
| 140 | }); | ||
| 141 | |||
| 142 | OperateOnSortedMap(room_ids.keyholders(), | ||
| 143 | [&room_node](const std::string& keyholder_name, | ||
| 144 | uint64_t keyholder_id) { | ||
| 145 | room_node["keyholders"][keyholder_name] = | ||
| 146 | keyholder_id; | ||
| 147 | }); | ||
| 148 | |||
| 149 | map_node["rooms"][room_name] = std::move(room_node); | ||
| 150 | }); | ||
| 151 | |||
| 152 | OperateOnSortedMap( | ||
| 153 | map_ids.doors(), | ||
| 154 | [&map_node](const std::string& door_name, uint64_t door_id) { | ||
| 155 | map_node["doors"][door_name] = door_id; | ||
| 156 | }); | ||
| 157 | |||
| 158 | result["maps"][map_name] = std::move(map_node); | ||
| 159 | }); | ||
| 160 | |||
| 161 | OperateOnSortedMap(ids.letters(), [&result](const std::string& letter_name, | ||
| 162 | uint64_t letter_id) { | ||
| 163 | result["letters"][letter_name] = letter_id; | ||
| 164 | }); | ||
| 165 | |||
| 166 | OperateOnSortedMap(ids.endings(), [&result](const std::string& ending_name, | ||
| 167 | uint64_t ending_id) { | ||
| 168 | result["endings"][ending_name] = ending_id; | ||
| 169 | }); | ||
| 170 | |||
| 171 | OperateOnSortedMap(ids.special(), [&result](const std::string& special_name, | ||
| 172 | uint64_t special_id) { | ||
| 173 | result["special"][special_name] = special_id; | ||
| 174 | }); | ||
| 175 | |||
| 176 | OperateOnSortedMap(ids.progressives(), | ||
| 177 | [&result](const std::string& prog_name, uint64_t prog_id) { | ||
| 178 | result["progressives"][prog_name] = prog_id; | ||
| 179 | }); | ||
| 180 | |||
| 181 | OperateOnSortedMap(ids.door_groups(), [&result](const std::string& group_name, | ||
| 182 | uint64_t group_id) { | ||
| 183 | result["door_groups"][group_name] = group_id; | ||
| 184 | }); | ||
| 185 | |||
| 186 | std::ofstream output_stream(filename); | ||
| 187 | output_stream << result << std::endl; | ||
| 188 | } | ||
| 189 | |||
| 190 | } // namespace com::fourisland::lingo2_archipelago | ||
