diff options
Diffstat (limited to 'tools/assign_ids/main.cpp')
| -rw-r--r-- | tools/assign_ids/main.cpp | 253 |
1 files changed, 210 insertions, 43 deletions
| diff --git a/tools/assign_ids/main.cpp b/tools/assign_ids/main.cpp index e65e5e4..4a48b86 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> |
| @@ -40,6 +41,10 @@ class AssignIds { | |||
| 40 | ReadIds(ids_path); | 41 | ReadIds(ids_path); |
| 41 | 42 | ||
| 42 | 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"); | ||
| 43 | 48 | ||
| 44 | WriteIds(ids_path); | 49 | WriteIds(ids_path); |
| 45 | 50 | ||
| @@ -54,50 +59,31 @@ class AssignIds { | |||
| 54 | id_mappings_ = ReadIdsFromYaml(path.string()); | 59 | id_mappings_ = ReadIdsFromYaml(path.string()); |
| 55 | 60 | ||
| 56 | for (const auto& [_, map] : id_mappings_.maps()) { | 61 | for (const auto& [_, map] : id_mappings_.maps()) { |
| 57 | for (const auto& [_, id] : map.doors()) { | 62 | UpdateNextId(map.doors()); |
| 58 | if (id > next_id_) { | ||
| 59 | next_id_ = id; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | 63 | ||
| 63 | for (const auto& [_, room] : map.rooms()) { | 64 | for (const auto& [_, room] : map.rooms()) { |
| 64 | for (const auto& [_, id] : room.panels()) { | 65 | UpdateNextId(room.panels()); |
| 65 | if (id > next_id_) { | 66 | UpdateNextId(room.masteries()); |
| 66 | next_id_ = id; | 67 | UpdateNextId(room.keyholders()); |
| 67 | } | 68 | UpdateNextId(room.ports()); |
| 68 | } | ||
| 69 | |||
| 70 | for (const auto& [_, id] : room.masteries()) { | ||
| 71 | if (id > next_id_) { | ||
| 72 | next_id_ = id; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | 69 | } |
| 76 | } | ||
| 77 | 70 | ||
| 78 | for (const auto& [_, id] : id_mappings_.special()) { | 71 | if (map.has_rte()) { |
| 79 | if (id > next_id_) { | 72 | UpdateNextId(map.rte()); |
| 80 | next_id_ = id; | ||
| 81 | } | 73 | } |
| 82 | } | 74 | } |
| 83 | 75 | ||
| 84 | for (const auto& [_, id] : id_mappings_.letters()) { | 76 | UpdateNextId(id_mappings_.special()); |
| 85 | if (id > next_id_) { | 77 | UpdateNextId(id_mappings_.letters()); |
| 86 | next_id_ = id; | 78 | UpdateNextId(id_mappings_.endings()); |
| 87 | } | 79 | UpdateNextId(id_mappings_.progressives()); |
| 88 | } | 80 | UpdateNextId(id_mappings_.door_groups()); |
| 89 | |||
| 90 | for (const auto& [_, id] : id_mappings_.endings()) { | ||
| 91 | if (id > next_id_) { | ||
| 92 | next_id_ = id; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | 81 | ||
| 96 | next_id_++; | 82 | next_id_++; |
| 97 | } | 83 | } |
| 98 | 84 | ||
| 99 | void WriteIds(std::filesystem::path path) { | 85 | void WriteIds(std::filesystem::path path) { |
| 100 | WriteIdsAsYaml(id_mappings_, path.string()); | 86 | WriteIdsAsYaml(output_, path.string()); |
| 101 | } | 87 | } |
| 102 | 88 | ||
| 103 | void ProcessMaps(std::filesystem::path path) { | 89 | void ProcessMaps(std::filesystem::path path) { |
| @@ -111,10 +97,31 @@ class AssignIds { | |||
| 111 | void ProcessMap(std::filesystem::path path) { | 97 | void ProcessMap(std::filesystem::path path) { |
| 112 | std::string map_name = path.filename().string(); | 98 | std::string map_name = path.filename().string(); |
| 113 | 99 | ||
| 100 | ProcessMapMetadata(path / "metadata.txtpb", map_name); | ||
| 114 | ProcessDoorsFile(path / "doors.txtpb", map_name); | 101 | ProcessDoorsFile(path / "doors.txtpb", map_name); |
| 115 | ProcessRooms(path / "rooms", map_name); | 102 | ProcessRooms(path / "rooms", map_name); |
| 116 | } | 103 | } |
| 117 | 104 | ||
| 105 | void ProcessMapMetadata(std::filesystem::path path, | ||
| 106 | const std::string& current_map_name) { | ||
| 107 | if (!std::filesystem::exists(path)) { | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | auto metadata = ReadMessageFromFile<HumanMap>(path.string()); | ||
| 112 | auto& maps = *output_.mutable_maps(); | ||
| 113 | |||
| 114 | if (metadata.has_rte_room()) { | ||
| 115 | if (!id_mappings_.maps().contains(current_map_name) || | ||
| 116 | !id_mappings_.maps().at(current_map_name).has_rte()) { | ||
| 117 | maps[current_map_name].set_rte(next_id_++); | ||
| 118 | } else { | ||
| 119 | maps[current_map_name].set_rte( | ||
| 120 | id_mappings_.maps().at(current_map_name).rte()); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 118 | void ProcessDoorsFile(std::filesystem::path path, | 125 | void ProcessDoorsFile(std::filesystem::path path, |
| 119 | const std::string& current_map_name) { | 126 | const std::string& current_map_name) { |
| 120 | if (!std::filesystem::exists(path)) { | 127 | if (!std::filesystem::exists(path)) { |
| @@ -130,18 +137,23 @@ class AssignIds { | |||
| 130 | 137 | ||
| 131 | void ProcessDoor(const HumanDoor& h_door, | 138 | void ProcessDoor(const HumanDoor& h_door, |
| 132 | const std::string& current_map_name) { | 139 | const std::string& current_map_name) { |
| 133 | if (h_door.type() == DoorType::EVENT) { | 140 | if (h_door.type() == DoorType::EVENT && !h_door.latch() && |
| 141 | !h_door.legacy_location()) { | ||
| 134 | return; | 142 | return; |
| 135 | } | 143 | } |
| 136 | 144 | ||
| 145 | auto& maps = *output_.mutable_maps(); | ||
| 146 | auto& doors = *maps[current_map_name].mutable_doors(); | ||
| 147 | |||
| 137 | if (!id_mappings_.maps().contains(current_map_name) || | 148 | if (!id_mappings_.maps().contains(current_map_name) || |
| 138 | !id_mappings_.maps() | 149 | !id_mappings_.maps() |
| 139 | .at(current_map_name) | 150 | .at(current_map_name) |
| 140 | .doors() | 151 | .doors() |
| 141 | .contains(h_door.name())) { | 152 | .contains(h_door.name())) { |
| 142 | auto& maps = *id_mappings_.mutable_maps(); | ||
| 143 | auto& doors = *maps[current_map_name].mutable_doors(); | ||
| 144 | doors[h_door.name()] = next_id_++; | 153 | doors[h_door.name()] = next_id_++; |
| 154 | } else { | ||
| 155 | doors[h_door.name()] = | ||
| 156 | id_mappings_.maps().at(current_map_name).doors().at(h_door.name()); | ||
| 145 | } | 157 | } |
| 146 | } | 158 | } |
| 147 | 159 | ||
| @@ -156,6 +168,10 @@ class AssignIds { | |||
| 156 | void ProcessRoom(const HumanRoom& h_room, | 168 | void ProcessRoom(const HumanRoom& h_room, |
| 157 | const std::string& current_map_name) { | 169 | const std::string& current_map_name) { |
| 158 | for (const HumanPanel& h_panel : h_room.panels()) { | 170 | for (const HumanPanel& h_panel : h_room.panels()) { |
| 171 | auto& maps = *output_.mutable_maps(); | ||
| 172 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 173 | auto& panels = *rooms[h_room.name()].mutable_panels(); | ||
| 174 | |||
| 159 | if (!id_mappings_.maps().contains(current_map_name) || | 175 | if (!id_mappings_.maps().contains(current_map_name) || |
| 160 | !id_mappings_.maps() | 176 | !id_mappings_.maps() |
| 161 | .at(current_map_name) | 177 | .at(current_map_name) |
| @@ -167,23 +183,33 @@ class AssignIds { | |||
| 167 | .at(h_room.name()) | 183 | .at(h_room.name()) |
| 168 | .panels() | 184 | .panels() |
| 169 | .contains(h_panel.name())) { | 185 | .contains(h_panel.name())) { |
| 170 | auto& maps = *id_mappings_.mutable_maps(); | ||
| 171 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 172 | auto& panels = *rooms[h_room.name()].mutable_panels(); | ||
| 173 | panels[h_panel.name()] = next_id_++; | 186 | panels[h_panel.name()] = next_id_++; |
| 187 | } else { | ||
| 188 | panels[h_panel.name()] = id_mappings_.maps() | ||
| 189 | .at(current_map_name) | ||
| 190 | .rooms() | ||
| 191 | .at(h_room.name()) | ||
| 192 | .panels() | ||
| 193 | .at(h_panel.name()); | ||
| 174 | } | 194 | } |
| 175 | } | 195 | } |
| 176 | 196 | ||
| 177 | for (const HumanLetter& h_letter : h_room.letters()) { | 197 | for (const HumanLetter& h_letter : h_room.letters()) { |
| 178 | std::string lettername = GetLetterName(h_letter.key(), h_letter.level2()); | 198 | std::string lettername = GetLetterName(h_letter.key(), h_letter.level2()); |
| 179 | 199 | ||
| 200 | auto& letters = *output_.mutable_letters(); | ||
| 180 | if (!id_mappings_.letters().contains(lettername)) { | 201 | if (!id_mappings_.letters().contains(lettername)) { |
| 181 | auto& letters = *id_mappings_.mutable_letters(); | ||
| 182 | letters[lettername] = next_id_++; | 202 | letters[lettername] = next_id_++; |
| 203 | } else { | ||
| 204 | letters[lettername] = id_mappings_.letters().at(lettername); | ||
| 183 | } | 205 | } |
| 184 | } | 206 | } |
| 185 | 207 | ||
| 186 | for (const HumanMastery& h_mastery : h_room.masteries()) { | 208 | for (const HumanMastery& h_mastery : h_room.masteries()) { |
| 209 | auto& maps = *output_.mutable_maps(); | ||
| 210 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 211 | auto& masteries = *rooms[h_room.name()].mutable_masteries(); | ||
| 212 | |||
| 187 | if (!id_mappings_.maps().contains(current_map_name) || | 213 | if (!id_mappings_.maps().contains(current_map_name) || |
| 188 | !id_mappings_.maps() | 214 | !id_mappings_.maps() |
| 189 | .at(current_map_name) | 215 | .at(current_map_name) |
| @@ -195,27 +221,168 @@ class AssignIds { | |||
| 195 | .at(h_room.name()) | 221 | .at(h_room.name()) |
| 196 | .masteries() | 222 | .masteries() |
| 197 | .contains(h_mastery.name())) { | 223 | .contains(h_mastery.name())) { |
| 198 | auto& maps = *id_mappings_.mutable_maps(); | ||
| 199 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 200 | auto& masteries = *rooms[h_room.name()].mutable_masteries(); | ||
| 201 | masteries[h_mastery.name()] = next_id_++; | 224 | masteries[h_mastery.name()] = next_id_++; |
| 225 | } else { | ||
| 226 | masteries[h_mastery.name()] = id_mappings_.maps() | ||
| 227 | .at(current_map_name) | ||
| 228 | .rooms() | ||
| 229 | .at(h_room.name()) | ||
| 230 | .masteries() | ||
| 231 | .at(h_mastery.name()); | ||
| 202 | } | 232 | } |
| 203 | } | 233 | } |
| 204 | 234 | ||
| 205 | for (const HumanEnding& h_ending : h_room.endings()) { | 235 | for (const HumanEnding& h_ending : h_room.endings()) { |
| 236 | auto& endings = *output_.mutable_endings(); | ||
| 237 | |||
| 206 | if (!id_mappings_.endings().contains(h_ending.name())) { | 238 | if (!id_mappings_.endings().contains(h_ending.name())) { |
| 207 | auto& endings = *id_mappings_.mutable_endings(); | ||
| 208 | endings[h_ending.name()] = next_id_++; | 239 | endings[h_ending.name()] = next_id_++; |
| 240 | } else { | ||
| 241 | endings[h_ending.name()] = id_mappings_.endings().at(h_ending.name()); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | for (const HumanKeyholder& h_keyholder : h_room.keyholders()) { | ||
| 246 | if (!h_keyholder.has_key()) { | ||
| 247 | continue; | ||
| 248 | } | ||
| 249 | |||
| 250 | auto& maps = *output_.mutable_maps(); | ||
| 251 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 252 | auto& keyholders = *rooms[h_room.name()].mutable_keyholders(); | ||
| 253 | |||
| 254 | if (!id_mappings_.maps().contains(current_map_name) || | ||
| 255 | !id_mappings_.maps() | ||
| 256 | .at(current_map_name) | ||
| 257 | .rooms() | ||
| 258 | .contains(h_room.name()) || | ||
| 259 | !id_mappings_.maps() | ||
| 260 | .at(current_map_name) | ||
| 261 | .rooms() | ||
| 262 | .at(h_room.name()) | ||
| 263 | .keyholders() | ||
| 264 | .contains(h_keyholder.name())) { | ||
| 265 | keyholders[h_keyholder.name()] = next_id_++; | ||
| 266 | } else { | ||
| 267 | keyholders[h_keyholder.name()] = id_mappings_.maps() | ||
| 268 | .at(current_map_name) | ||
| 269 | .rooms() | ||
| 270 | .at(h_room.name()) | ||
| 271 | .keyholders() | ||
| 272 | .at(h_keyholder.name()); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | for (const HumanPort& h_port : h_room.ports()) { | ||
| 277 | if (h_port.no_shuffle()) { | ||
| 278 | continue; | ||
| 279 | } | ||
| 280 | |||
| 281 | auto& maps = *output_.mutable_maps(); | ||
| 282 | auto& rooms = *maps[current_map_name].mutable_rooms(); | ||
| 283 | auto& ports = *rooms[h_room.name()].mutable_ports(); | ||
| 284 | |||
| 285 | if (!id_mappings_.maps().contains(current_map_name) || | ||
| 286 | !id_mappings_.maps() | ||
| 287 | .at(current_map_name) | ||
| 288 | .rooms() | ||
| 289 | .contains(h_room.name()) || | ||
| 290 | !id_mappings_.maps() | ||
| 291 | .at(current_map_name) | ||
| 292 | .rooms() | ||
| 293 | .at(h_room.name()) | ||
| 294 | .ports() | ||
| 295 | .contains(h_port.name())) { | ||
| 296 | ports[h_port.name()] = next_id_++; | ||
| 297 | } else { | ||
| 298 | ports[h_port.name()] = id_mappings_.maps() | ||
| 299 | .at(current_map_name) | ||
| 300 | .rooms() | ||
| 301 | .at(h_room.name()) | ||
| 302 | .ports() | ||
| 303 | .at(h_port.name()); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | void ProcessSpecialIds() { | ||
| 309 | auto& specials = *output_.mutable_special(); | ||
| 310 | |||
| 311 | for (const auto& [special_name, ap_id] : id_mappings_.special()) { | ||
| 312 | specials[special_name] = ap_id; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | void ProcessProgressivesFile(std::filesystem::path path) { | ||
| 317 | if (!std::filesystem::exists(path)) { | ||
| 318 | return; | ||
| 319 | } | ||
| 320 | |||
| 321 | auto h_progs = ReadMessageFromFile<HumanProgressives>(path.string()); | ||
| 322 | auto& progs = *output_.mutable_progressives(); | ||
| 323 | |||
| 324 | for (const HumanProgressive& h_prog : h_progs.progressives()) { | ||
| 325 | if (!id_mappings_.progressives().contains(h_prog.name())) { | ||
| 326 | progs[h_prog.name()] = next_id_++; | ||
| 327 | } else { | ||
| 328 | progs[h_prog.name()] = id_mappings_.progressives().at(h_prog.name()); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | void ProcessDoorGroupsFile(std::filesystem::path path) { | ||
| 334 | if (!std::filesystem::exists(path)) { | ||
| 335 | return; | ||
| 336 | } | ||
| 337 | |||
| 338 | auto h_groups = ReadMessageFromFile<HumanDoorGroups>(path.string()); | ||
| 339 | auto& groups = *output_.mutable_door_groups(); | ||
| 340 | |||
| 341 | for (const HumanDoorGroup& h_group : h_groups.door_groups()) { | ||
| 342 | if (!id_mappings_.door_groups().contains(h_group.name())) { | ||
| 343 | groups[h_group.name()] = next_id_++; | ||
| 344 | } else { | ||
| 345 | groups[h_group.name()] = id_mappings_.door_groups().at(h_group.name()); | ||
| 346 | } | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | void ProcessGlobalMetadataFile(std::filesystem::path path) { | ||
| 351 | if (!std::filesystem::exists(path)) { | ||
| 352 | return; | ||
| 353 | } | ||
| 354 | |||
| 355 | auto h_metadata = ReadMessageFromFile<HumanGlobalMetadata>(path.string()); | ||
| 356 | auto& specials = *output_.mutable_special(); | ||
| 357 | |||
| 358 | for (const std::string& h_special : h_metadata.special_names()) { | ||
| 359 | if (!id_mappings_.special().contains(h_special)) { | ||
| 360 | specials[h_special] = next_id_++; | ||
| 361 | } else { | ||
| 362 | specials[h_special] = id_mappings_.special().at(h_special); | ||
| 209 | } | 363 | } |
| 210 | } | 364 | } |
| 211 | } | 365 | } |
| 212 | 366 | ||
| 213 | private: | 367 | private: |
| 368 | void UpdateNextId(const google::protobuf::Map<std::string, uint64_t>& ids) { | ||
| 369 | for (const auto& [_, id] : ids) { | ||
| 370 | UpdateNextId(id); | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | void UpdateNextId(uint64_t id) { | ||
| 375 | if (id > next_id_) { | ||
| 376 | next_id_ = id; | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 214 | std::string mapdir_; | 380 | std::string mapdir_; |
| 215 | 381 | ||
| 216 | uint64_t next_id_ = 1; | 382 | uint64_t next_id_ = 1; |
| 217 | 383 | ||
| 218 | IdMappings id_mappings_; | 384 | IdMappings id_mappings_; |
| 385 | IdMappings output_; | ||
| 219 | }; | 386 | }; |
| 220 | 387 | ||
| 221 | } // namespace | 388 | } // namespace |
