diff options
Diffstat (limited to 'tools/assign_ids/main.cpp')
| -rw-r--r-- | tools/assign_ids/main.cpp | 193 |
1 files changed, 149 insertions, 44 deletions
| diff --git a/tools/assign_ids/main.cpp b/tools/assign_ids/main.cpp index e65e5e4..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> |
| @@ -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,26 @@ 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 | } | ||
| 69 | |||
| 70 | for (const auto& [_, id] : room.masteries()) { | ||
| 71 | if (id > next_id_) { | ||
| 72 | next_id_ = id; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | for (const auto& [_, id] : id_mappings_.special()) { | ||
| 79 | if (id > next_id_) { | ||
| 80 | next_id_ = id; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | for (const auto& [_, id] : id_mappings_.letters()) { | ||
| 85 | if (id > next_id_) { | ||
| 86 | next_id_ = id; | ||
| 87 | } | 68 | } |
| 88 | } | 69 | } |
| 89 | 70 | ||
| 90 | for (const auto& [_, id] : id_mappings_.endings()) { | 71 | UpdateNextId(id_mappings_.special()); |
| 91 | if (id > next_id_) { | 72 | UpdateNextId(id_mappings_.letters()); |
| 92 | next_id_ = id; | 73 | UpdateNextId(id_mappings_.endings()); |
| 93 | } | 74 | UpdateNextId(id_mappings_.progressives()); |
| 94 | } | 75 | UpdateNextId(id_mappings_.door_groups()); |
| 95 | 76 | ||
| 96 | next_id_++; | 77 | next_id_++; |
| 97 | } | 78 | } |
| 98 | 79 | ||
| 99 | void WriteIds(std::filesystem::path path) { | 80 | void WriteIds(std::filesystem::path path) { |
| 100 | WriteIdsAsYaml(id_mappings_, path.string()); | 81 | WriteIdsAsYaml(output_, path.string()); |
| 101 | } | 82 | } |
| 102 | 83 | ||
| 103 | void ProcessMaps(std::filesystem::path path) { | 84 | void ProcessMaps(std::filesystem::path path) { |
| @@ -134,14 +115,18 @@ class AssignIds { | |||
| 134 | return; | 115 | return; |
| 135 | } | 116 | } |
| 136 | 117 | ||
| 118 | auto& maps = *output_.mutable_maps(); | ||
| 119 | auto& doors = *maps[current_map_name].mutable_doors(); | ||
| 120 | |||
| 137 | if (!id_mappings_.maps().contains(current_map_name) || | 121 | if (!id_mappings_.maps().contains(current_map_name) || |
| 138 | !id_mappings_.maps() | 122 | !id_mappings_.maps() |
| 139 | .at(current_map_name) | 123 | .at(current_map_name) |
| 140 | .doors() | 124 | .doors() |
| 141 | .contains(h_door.name())) { | 125 | .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_++; | 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()); | ||
| 145 | } | 130 | } |
| 146 | } | 131 | } |
| 147 | 132 | ||
| @@ -156,6 +141,10 @@ class AssignIds { | |||
| 156 | void ProcessRoom(const HumanRoom& h_room, | 141 | void ProcessRoom(const HumanRoom& h_room, |
| 157 | const std::string& current_map_name) { | 142 | const std::string& current_map_name) { |
| 158 | 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 | |||
| 159 | if (!id_mappings_.maps().contains(current_map_name) || | 148 | if (!id_mappings_.maps().contains(current_map_name) || |
| 160 | !id_mappings_.maps() | 149 | !id_mappings_.maps() |
| 161 | .at(current_map_name) | 150 | .at(current_map_name) |
| @@ -167,23 +156,33 @@ class AssignIds { | |||
| 167 | .at(h_room.name()) | 156 | .at(h_room.name()) |
| 168 | .panels() | 157 | .panels() |
| 169 | .contains(h_panel.name())) { | 158 | .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_++; | 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()); | ||
| 174 | } | 167 | } |
| 175 | } | 168 | } |
| 176 | 169 | ||
| 177 | for (const HumanLetter& h_letter : h_room.letters()) { | 170 | for (const HumanLetter& h_letter : h_room.letters()) { |
| 178 | std::string lettername = GetLetterName(h_letter.key(), h_letter.level2()); | 171 | std::string lettername = GetLetterName(h_letter.key(), h_letter.level2()); |
| 179 | 172 | ||
| 173 | auto& letters = *output_.mutable_letters(); | ||
| 180 | if (!id_mappings_.letters().contains(lettername)) { | 174 | if (!id_mappings_.letters().contains(lettername)) { |
| 181 | auto& letters = *id_mappings_.mutable_letters(); | ||
| 182 | letters[lettername] = next_id_++; | 175 | letters[lettername] = next_id_++; |
| 176 | } else { | ||
| 177 | letters[lettername] = id_mappings_.letters().at(lettername); | ||
| 183 | } | 178 | } |
| 184 | } | 179 | } |
| 185 | 180 | ||
| 186 | 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 | |||
| 187 | if (!id_mappings_.maps().contains(current_map_name) || | 186 | if (!id_mappings_.maps().contains(current_map_name) || |
| 188 | !id_mappings_.maps() | 187 | !id_mappings_.maps() |
| 189 | .at(current_map_name) | 188 | .at(current_map_name) |
| @@ -195,27 +194,133 @@ class AssignIds { | |||
| 195 | .at(h_room.name()) | 194 | .at(h_room.name()) |
| 196 | .masteries() | 195 | .masteries() |
| 197 | .contains(h_mastery.name())) { | 196 | .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_++; | 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()); | ||
| 202 | } | 205 | } |
| 203 | } | 206 | } |
| 204 | 207 | ||
| 205 | for (const HumanEnding& h_ending : h_room.endings()) { | 208 | for (const HumanEnding& h_ending : h_room.endings()) { |
| 209 | auto& endings = *output_.mutable_endings(); | ||
| 210 | |||
| 206 | if (!id_mappings_.endings().contains(h_ending.name())) { | 211 | if (!id_mappings_.endings().contains(h_ending.name())) { |
| 207 | auto& endings = *id_mappings_.mutable_endings(); | ||
| 208 | endings[h_ending.name()] = next_id_++; | 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); | ||
| 209 | } | 305 | } |
| 210 | } | 306 | } |
| 211 | } | 307 | } |
| 212 | 308 | ||
| 213 | 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 | |||
| 214 | std::string mapdir_; | 318 | std::string mapdir_; |
| 215 | 319 | ||
| 216 | uint64_t next_id_ = 1; | 320 | uint64_t next_id_ = 1; |
| 217 | 321 | ||
| 218 | IdMappings id_mappings_; | 322 | IdMappings id_mappings_; |
| 323 | IdMappings output_; | ||
| 219 | }; | 324 | }; |
| 220 | 325 | ||
| 221 | } // namespace | 326 | } // namespace |
