diff options
Diffstat (limited to 'tools/assign_ids/main.cpp')
| -rw-r--r-- | tools/assign_ids/main.cpp | 211 |
1 files changed, 164 insertions, 47 deletions
| diff --git a/tools/assign_ids/main.cpp b/tools/assign_ids/main.cpp index 2617cf7..3e16f78 100644 --- a/tools/assign_ids/main.cpp +++ b/tools/assign_ids/main.cpp | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | #include <google/protobuf/text_format.h> | 2 | #include <google/protobuf/text_format.h> |
| 3 | 3 | ||
| 4 | #include <cstdint> | 4 | #include <cstdint> |
| 5 | #include <filesystem> | ||
| 5 | #include <fstream> | 6 | #include <fstream> |
| 6 | #include <iostream> | 7 | #include <iostream> |
| 7 | #include <map> | 8 | #include <map> |
| @@ -9,6 +10,7 @@ | |||
| 9 | #include <string> | 10 | #include <string> |
| 10 | 11 | ||
| 11 | #include "proto/human.pb.h" | 12 | #include "proto/human.pb.h" |
| 13 | #include "util/ids_yaml_format.h" | ||
| 12 | #include "util/naming.h" | 14 | #include "util/naming.h" |
| 13 | 15 | ||
| 14 | namespace com::fourisland::lingo2_archipelago { | 16 | namespace com::fourisland::lingo2_archipelago { |
| @@ -34,11 +36,15 @@ class AssignIds { | |||
| 34 | 36 | ||
| 35 | void Run() { | 37 | void Run() { |
| 36 | std::filesystem::path datadir_path = mapdir_; | 38 | std::filesystem::path datadir_path = mapdir_; |
| 37 | std::filesystem::path ids_path = datadir_path / "ids.txtpb"; | 39 | std::filesystem::path ids_path = datadir_path / "ids.yaml"; |
| 38 | 40 | ||
| 39 | ReadIds(ids_path); | 41 | ReadIds(ids_path); |
| 40 | 42 | ||
| 41 | ProcessMaps(datadir_path); | 43 | ProcessMaps(datadir_path); |
| 44 | ProcessSpecialIds(); | ||
| 45 | ProcessProgressivesFile(datadir_path / "progressives.txtpb"); | ||
| 46 | ProcessDoorGroupsFile(datadir_path / "door_groups.txtpb"); | ||
| 47 | ProcessGlobalMetadataFile(datadir_path / "metadata.txtpb"); | ||
| 42 | 48 | ||
| 43 | WriteIds(ids_path); | 49 | WriteIds(ids_path); |
| 44 | 50 | ||
| @@ -46,53 +52,33 @@ class AssignIds { | |||
| 46 | } | 52 | } |
| 47 | 53 | ||
| 48 | void ReadIds(std::filesystem::path path) { | 54 | void ReadIds(std::filesystem::path path) { |
| 49 | id_mappings_ = ReadMessageFromFile<IdMappings>(path.string()); | 55 | if (!std::filesystem::exists(path)) { |
| 56 | return; | ||
| 57 | } | ||
| 58 | |||
| 59 | id_mappings_ = ReadIdsFromYaml(path.string()); | ||
| 50 | 60 | ||
| 51 | for (const auto& [_, map] : id_mappings_.maps()) { | 61 | for (const auto& [_, map] : id_mappings_.maps()) { |
| 52 | for (const auto& [_, id] : map.doors()) { | 62 | UpdateNextId(map.doors()); |
| 53 | if (id > next_id_) { | ||
| 54 | next_id_ = id; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | 63 | ||
| 58 | for (const auto& [_, room] : map.rooms()) { | 64 | for (const auto& [_, room] : map.rooms()) { |
| 59 | for (const auto& [_, id] : room.panels()) { | 65 | UpdateNextId(room.panels()); |
| 60 | if (id > next_id_) { | 66 | UpdateNextId(room.masteries()); |
| 61 | next_id_ = id; | 67 | UpdateNextId(room.keyholders()); |
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | for (const auto& [_, id] : room.masteries()) { | ||
| 66 | if (id > next_id_) { | ||
| 67 | next_id_ = id; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | 68 | } |
| 71 | } | 69 | } |
| 72 | 70 | ||
| 73 | for (const auto& [_, id] : id_mappings_.special()) { | 71 | UpdateNextId(id_mappings_.special()); |
| 74 | if (id > next_id_) { | 72 | UpdateNextId(id_mappings_.letters()); |
| 75 | next_id_ = id; | 73 | UpdateNextId(id_mappings_.endings()); |
| 76 | } | 74 | UpdateNextId(id_mappings_.progressives()); |
| 77 | } | 75 | UpdateNextId(id_mappings_.door_groups()); |
| 78 | |||
| 79 | for (const auto& [_, id] : id_mappings_.letters()) { | ||
| 80 | if (id > next_id_) { | ||
| 81 | next_id_ = id; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | 76 | ||
| 85 | next_id_++; | 77 | next_id_++; |
| 86 | } | 78 | } |
| 87 | 79 | ||
| 88 | void WriteIds(std::filesystem::path path) { | 80 | void WriteIds(std::filesystem::path path) { |
| 89 | std::string output; | 81 | WriteIdsAsYaml(output_, path.string()); |
| 90 | google::protobuf::TextFormat::PrintToString(id_mappings_, &output); | ||
| 91 | |||
| 92 | { | ||
| 93 | std::ofstream outputfile(path.string()); | ||
| 94 | outputfile << output; | ||
| 95 | } | ||
| 96 | } | 82 | } |
| 97 | 83 | ||
| 98 | void ProcessMaps(std::filesystem::path path) { | 84 | void ProcessMaps(std::filesystem::path path) { |
| @@ -104,7 +90,7 @@ class AssignIds { | |||
| 104 | } | 90 | } |
| 105 | 91 | ||
| 106 | void ProcessMap(std::filesystem::path path) { | 92 | void ProcessMap(std::filesystem::path path) { |
| 107 | std::string map_name = path.filename(); | 93 | std::string map_name = path.filename().string(); |
| 108 | 94 | ||
| 109 | ProcessDoorsFile(path / "doors.txtpb", map_name); | 95 | ProcessDoorsFile(path / "doors.txtpb", map_name); |
| 110 | ProcessRooms(path / "rooms", map_name); | 96 | ProcessRooms(path / "rooms", map_name); |
| @@ -129,14 +115,18 @@ class AssignIds { | |||
| 129 | return; | 115 | return; |
| 130 | } | 116 | } |
| 131 | 117 | ||
| 118 | auto& maps = *output_.mutable_maps(); | ||
| 119 | auto& doors = *maps[current_map_name].mutable_doors(); | ||
| 120 | |||
| 132 | if (!id_mappings_.maps().contains(current_map_name) || | 121 | if (!id_mappings_.maps().contains(current_map_name) || |
| 133 | !id_mappings_.maps() | 122 | !id_mappings_.maps() |
| 134 | .at(current_map_name) | 123 | .at(current_map_name) |
| 135 | .doors() | 124 | .doors() |
| 136 | .contains(h_door.name())) { | 125 | .contains(h_door.name())) { |
| 137 | auto& maps = *id_mappings_.mutable_maps(); | ||
| 138 | auto& doors = *maps[current_map_name].mutable_doors(); | ||
| 139 | doors[h_door.name()] = next_id_++; | 126 | doors[h_door.name()] = next_id_++; |
| 127 | } else { | ||
| 128 | doors[h_door.name()] = | ||
| 129 | id_mappings_.maps().at(current_map_name).doors().at(h_door.name()); | ||
| 140 | } | 130 | } |
| 141 | } | 131 | } |
| 142 | 132 | ||
| @@ -151,6 +141,10 @@ class AssignIds { | |||
| 151 | void ProcessRoom(const HumanRoom& h_room, | 141 | void ProcessRoom(const HumanRoom& h_room, |
| 152 | const std::string& current_map_name) { | 142 | const std::string& current_map_name) { |
| 153 | for (const HumanPanel& h_panel : h_room.panels()) { | 143 | for (const HumanPanel& h_panel : h_room.panels()) { |
| 144 | auto& maps = *output_.mutable_maps(); | ||
| 145 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 146 | auto& panels = *rooms[h_room.name()].mutable_panels(); | ||
| 147 | |||
| 154 | if (!id_mappings_.maps().contains(current_map_name) || | 148 | if (!id_mappings_.maps().contains(current_map_name) || |
| 155 | !id_mappings_.maps() | 149 | !id_mappings_.maps() |
| 156 | .at(current_map_name) | 150 | .at(current_map_name) |
| @@ -162,23 +156,33 @@ class AssignIds { | |||
| 162 | .at(h_room.name()) | 156 | .at(h_room.name()) |
| 163 | .panels() | 157 | .panels() |
| 164 | .contains(h_panel.name())) { | 158 | .contains(h_panel.name())) { |
| 165 | auto& maps = *id_mappings_.mutable_maps(); | ||
| 166 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 167 | auto& panels = *rooms[h_room.name()].mutable_panels(); | ||
| 168 | panels[h_panel.name()] = next_id_++; | 159 | panels[h_panel.name()] = next_id_++; |
| 160 | } else { | ||
| 161 | panels[h_panel.name()] = id_mappings_.maps() | ||
| 162 | .at(current_map_name) | ||
| 163 | .rooms() | ||
| 164 | .at(h_room.name()) | ||
| 165 | .panels() | ||
| 166 | .at(h_panel.name()); | ||
| 169 | } | 167 | } |
| 170 | } | 168 | } |
| 171 | 169 | ||
| 172 | for (const HumanLetter& h_letter : h_room.letters()) { | 170 | for (const HumanLetter& h_letter : h_room.letters()) { |
| 173 | std::string lettername = GetLetterName(h_letter.key(), h_letter.level2()); | 171 | std::string lettername = GetLetterName(h_letter.key(), h_letter.level2()); |
| 174 | 172 | ||
| 173 | auto& letters = *output_.mutable_letters(); | ||
| 175 | if (!id_mappings_.letters().contains(lettername)) { | 174 | if (!id_mappings_.letters().contains(lettername)) { |
| 176 | auto& letters = *id_mappings_.mutable_letters(); | ||
| 177 | letters[lettername] = next_id_++; | 175 | letters[lettername] = next_id_++; |
| 176 | } else { | ||
| 177 | letters[lettername] = id_mappings_.letters().at(lettername); | ||
| 178 | } | 178 | } |
| 179 | } | 179 | } |
| 180 | 180 | ||
| 181 | for (const HumanMastery& h_mastery : h_room.masteries()) { | 181 | for (const HumanMastery& h_mastery : h_room.masteries()) { |
| 182 | auto& maps = *output_.mutable_maps(); | ||
| 183 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 184 | auto& masteries = *rooms[h_room.name()].mutable_masteries(); | ||
| 185 | |||
| 182 | if (!id_mappings_.maps().contains(current_map_name) || | 186 | if (!id_mappings_.maps().contains(current_map_name) || |
| 183 | !id_mappings_.maps() | 187 | !id_mappings_.maps() |
| 184 | .at(current_map_name) | 188 | .at(current_map_name) |
| @@ -190,20 +194,133 @@ class AssignIds { | |||
| 190 | .at(h_room.name()) | 194 | .at(h_room.name()) |
| 191 | .masteries() | 195 | .masteries() |
| 192 | .contains(h_mastery.name())) { | 196 | .contains(h_mastery.name())) { |
| 193 | auto& maps = *id_mappings_.mutable_maps(); | ||
| 194 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 195 | auto& masteries = *rooms[h_room.name()].mutable_masteries(); | ||
| 196 | masteries[h_mastery.name()] = next_id_++; | 197 | masteries[h_mastery.name()] = next_id_++; |
| 198 | } else { | ||
| 199 | masteries[h_mastery.name()] = id_mappings_.maps() | ||
| 200 | .at(current_map_name) | ||
| 201 | .rooms() | ||
| 202 | .at(h_room.name()) | ||
| 203 | .masteries() | ||
| 204 | .at(h_mastery.name()); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | for (const HumanEnding& h_ending : h_room.endings()) { | ||
| 209 | auto& endings = *output_.mutable_endings(); | ||
| 210 | |||
| 211 | if (!id_mappings_.endings().contains(h_ending.name())) { | ||
| 212 | endings[h_ending.name()] = next_id_++; | ||
| 213 | } else { | ||
| 214 | endings[h_ending.name()] = id_mappings_.endings().at(h_ending.name()); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | for (const HumanKeyholder& h_keyholder : h_room.keyholders()) { | ||
| 219 | if (!h_keyholder.has_key()) { | ||
| 220 | continue; | ||
| 221 | } | ||
| 222 | |||
| 223 | auto& maps = *output_.mutable_maps(); | ||
| 224 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 225 | auto& keyholders = *rooms[h_room.name()].mutable_keyholders(); | ||
| 226 | |||
| 227 | if (!id_mappings_.maps().contains(current_map_name) || | ||
| 228 | !id_mappings_.maps() | ||
| 229 | .at(current_map_name) | ||
| 230 | .rooms() | ||
| 231 | .contains(h_room.name()) || | ||
| 232 | !id_mappings_.maps() | ||
| 233 | .at(current_map_name) | ||
| 234 | .rooms() | ||
| 235 | .at(h_room.name()) | ||
| 236 | .keyholders() | ||
| 237 | .contains(h_keyholder.name())) { | ||
| 238 | keyholders[h_keyholder.name()] = next_id_++; | ||
| 239 | } else { | ||
| 240 | keyholders[h_keyholder.name()] = id_mappings_.maps() | ||
| 241 | .at(current_map_name) | ||
| 242 | .rooms() | ||
| 243 | .at(h_room.name()) | ||
| 244 | .keyholders() | ||
| 245 | .at(h_keyholder.name()); | ||
| 246 | } | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | void ProcessSpecialIds() { | ||
| 251 | auto& specials = *output_.mutable_special(); | ||
| 252 | |||
| 253 | for (const auto& [special_name, ap_id] : id_mappings_.special()) { | ||
| 254 | specials[special_name] = ap_id; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | void ProcessProgressivesFile(std::filesystem::path path) { | ||
| 259 | if (!std::filesystem::exists(path)) { | ||
| 260 | return; | ||
| 261 | } | ||
| 262 | |||
| 263 | auto h_progs = ReadMessageFromFile<HumanProgressives>(path.string()); | ||
| 264 | auto& progs = *output_.mutable_progressives(); | ||
| 265 | |||
| 266 | for (const HumanProgressive& h_prog : h_progs.progressives()) { | ||
| 267 | if (!id_mappings_.progressives().contains(h_prog.name())) { | ||
| 268 | progs[h_prog.name()] = next_id_++; | ||
| 269 | } else { | ||
| 270 | progs[h_prog.name()] = id_mappings_.progressives().at(h_prog.name()); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | void ProcessDoorGroupsFile(std::filesystem::path path) { | ||
| 276 | if (!std::filesystem::exists(path)) { | ||
| 277 | return; | ||
| 278 | } | ||
| 279 | |||
| 280 | auto h_groups = ReadMessageFromFile<HumanDoorGroups>(path.string()); | ||
| 281 | auto& groups = *output_.mutable_door_groups(); | ||
| 282 | |||
| 283 | for (const HumanDoorGroup& h_group : h_groups.door_groups()) { | ||
| 284 | if (!id_mappings_.door_groups().contains(h_group.name())) { | ||
| 285 | groups[h_group.name()] = next_id_++; | ||
| 286 | } else { | ||
| 287 | groups[h_group.name()] = id_mappings_.door_groups().at(h_group.name()); | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | void ProcessGlobalMetadataFile(std::filesystem::path path) { | ||
| 293 | if (!std::filesystem::exists(path)) { | ||
| 294 | return; | ||
| 295 | } | ||
| 296 | |||
| 297 | auto h_metadata = ReadMessageFromFile<HumanGlobalMetadata>(path.string()); | ||
| 298 | auto& specials = *output_.mutable_special(); | ||
| 299 | |||
| 300 | for (const std::string& h_special : h_metadata.special_names()) { | ||
| 301 | if (!id_mappings_.special().contains(h_special)) { | ||
| 302 | specials[h_special] = next_id_++; | ||
| 303 | } else { | ||
| 304 | specials[h_special] = id_mappings_.special().at(h_special); | ||
| 197 | } | 305 | } |
| 198 | } | 306 | } |
| 199 | } | 307 | } |
| 200 | 308 | ||
| 201 | private: | 309 | private: |
| 310 | void UpdateNextId(const google::protobuf::Map<std::string, uint64_t>& ids) { | ||
| 311 | for (const auto& [_, id] : ids) { | ||
| 312 | if (id > next_id_) { | ||
| 313 | next_id_ = id; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 202 | std::string mapdir_; | 318 | std::string mapdir_; |
| 203 | 319 | ||
| 204 | uint64_t next_id_ = 0; | 320 | uint64_t next_id_ = 1; |
| 205 | 321 | ||
| 206 | IdMappings id_mappings_; | 322 | IdMappings id_mappings_; |
| 323 | IdMappings output_; | ||
| 207 | }; | 324 | }; |
| 208 | 325 | ||
| 209 | } // namespace | 326 | } // namespace |
