From 724f8092c4808cdad47316e00949c04ee797acb5 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 19 Aug 2025 20:19:48 -0400 Subject: Store IDs in a yaml file This is much more efficient than the txtpb format, and we only need an interface for it in C++ since the IDs will be packed into the binary proto representation. --- tools/assign_ids/main.cpp | 17 +++-- tools/datapacker/main.cpp | 5 +- tools/util/CMakeLists.txt | 4 +- tools/util/ids_yaml_format.cpp | 139 +++++++++++++++++++++++++++++++++++++++++ tools/util/ids_yaml_format.h | 16 +++++ 5 files changed, 169 insertions(+), 12 deletions(-) create mode 100644 tools/util/ids_yaml_format.cpp create mode 100644 tools/util/ids_yaml_format.h (limited to 'tools') diff --git a/tools/assign_ids/main.cpp b/tools/assign_ids/main.cpp index 92f3ea4..2471bc5 100644 --- a/tools/assign_ids/main.cpp +++ b/tools/assign_ids/main.cpp @@ -9,6 +9,7 @@ #include #include "proto/human.pb.h" +#include "util/ids_yaml_format.h" #include "util/naming.h" namespace com::fourisland::lingo2_archipelago { @@ -34,7 +35,7 @@ class AssignIds { void Run() { std::filesystem::path datadir_path = mapdir_; - std::filesystem::path ids_path = datadir_path / "ids.txtpb"; + std::filesystem::path ids_path = datadir_path / "ids.yaml"; ReadIds(ids_path); @@ -46,7 +47,11 @@ class AssignIds { } void ReadIds(std::filesystem::path path) { - id_mappings_ = ReadMessageFromFile(path.string()); + if (!std::filesystem::exists(path)) { + return; + } + + id_mappings_ = ReadIdsFromYaml(path.string()); for (const auto& [_, map] : id_mappings_.maps()) { for (const auto& [_, id] : map.doors()) { @@ -86,13 +91,7 @@ class AssignIds { } void WriteIds(std::filesystem::path path) { - std::string output; - google::protobuf::TextFormat::PrintToString(id_mappings_, &output); - - { - std::ofstream outputfile(path.string()); - outputfile << output; - } + WriteIdsAsYaml(id_mappings_, path.string()); } void ProcessMaps(std::filesystem::path path) { diff --git a/tools/datapacker/main.cpp b/tools/datapacker/main.cpp index 860d3c0..0beb304 100644 --- a/tools/datapacker/main.cpp +++ b/tools/datapacker/main.cpp @@ -14,6 +14,7 @@ #include "container.h" #include "proto/data.pb.h" #include "proto/human.pb.h" +#include "util/ids_yaml_format.h" namespace com::fourisland::lingo2_archipelago { namespace { @@ -42,7 +43,7 @@ class DataPacker { ProcessConnectionsFile(datadir_path / "connections.txtpb", std::nullopt); ProcessMaps(datadir_path); - ProcessIdsFile(datadir_path / "ids.txtpb"); + ProcessIdsFile(datadir_path / "ids.yaml"); { std::ofstream outputfile(outputpath_); @@ -490,7 +491,7 @@ class DataPacker { } void ProcessIdsFile(std::filesystem::path path) { - auto ids = ReadMessageFromFile(path.string()); + auto ids = ReadIdsFromYaml(path.string()); for (const auto& [map_name, map] : ids.maps()) { for (const auto& [door_name, ap_id] : map.doors()) { 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 @@ find_package(Protobuf REQUIRED) +find_package(yaml-cpp REQUIRED) add_library(util godot_scene.cpp identifiers.cpp + ids_yaml_format.cpp naming.cpp ) set_property(TARGET util PROPERTY CXX_STANDARD 20) set_property(TARGET util PROPERTY CXX_STANDARD_REQUIRED ON) target_include_directories(util PUBLIC ${CMAKE_BINARY_DIR}) -target_link_libraries(util PUBLIC protos protobuf::libprotobuf) +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 @@ +#include "ids_yaml_format.h" + +#include + +#include +#include + +namespace com::fourisland::lingo2_archipelago { +namespace { + +template +void OperateOnSortedMap( + const T& map, std::function + callback) { + std::vector names; + for (const auto& it : map) { + names.push_back(it.first); + } + + std::sort(names.begin(), names.end()); + + for (const std::string& name : names) { + callback(name, map.at(name)); + } +} + +} // namespace + +IdMappings ReadIdsFromYaml(const std::string& filename) { + IdMappings result; + + YAML::Node document = YAML::LoadFile(filename); + + if (document["maps"]) { + for (const auto& map_it : document["maps"]) { + IdMappings::MapIds& map_ids = + (*result.mutable_maps())[map_it.first.as()]; + + if (map_it.second["rooms"]) { + for (const auto& room_it : map_it.second["rooms"]) { + IdMappings::RoomIds& room_ids = + (*map_ids.mutable_rooms())[room_it.first.as()]; + + if (room_it.second["panels"]) { + for (const auto& panel_it : room_it.second["panels"]) { + (*room_ids.mutable_panels())[panel_it.first.as()] = + panel_it.second.as(); + } + } + + if (room_it.second["masteries"]) { + for (const auto& mastery_it : room_it.second["masteries"]) { + (*room_ids + .mutable_masteries())[mastery_it.first.as()] = + mastery_it.second.as(); + } + } + } + } + + if (map_it.second["doors"]) { + for (const auto& door_it : map_it.second["doors"]) { + (*map_ids.mutable_doors())[door_it.first.as()] = + door_it.second.as(); + } + } + } + } + + if (document["letters"]) { + for (const auto& letter_it : document["letters"]) { + (*result.mutable_letters())[letter_it.first.as()] = + letter_it.second.as(); + } + } + + if (document["special"]) { + for (const auto& special_it : document["special"]) { + (*result.mutable_special())[special_it.first.as()] = + special_it.second.as(); + } + } + + return result; +} + +void WriteIdsAsYaml(const IdMappings& ids, const std::string& filename) { + YAML::Node result; + + OperateOnSortedMap(ids.maps(), [&result](const std::string& map_name, + const IdMappings::MapIds& map_ids) { + YAML::Node map_node; + + OperateOnSortedMap( + map_ids.rooms(), [&map_node](const std::string& room_name, + const IdMappings::RoomIds& room_ids) { + YAML::Node room_node; + + OperateOnSortedMap( + room_ids.panels(), + [&room_node](const std::string& panel_name, uint64_t panel_id) { + room_node["panels"][panel_name] = panel_id; + }); + + OperateOnSortedMap(room_ids.masteries(), + [&room_node](const std::string& mastery_name, + uint64_t mastery_id) { + room_node["masteries"][mastery_name] = + mastery_id; + }); + + map_node["rooms"][room_name] = std::move(room_node); + }); + + OperateOnSortedMap( + map_ids.doors(), + [&map_node](const std::string& door_name, uint64_t door_id) { + map_node["doors"][door_name] = door_id; + }); + + result["maps"][map_name] = std::move(map_node); + }); + + OperateOnSortedMap(ids.letters(), [&result](const std::string& letter_name, + uint64_t letter_id) { + result["letters"][letter_name] = letter_id; + }); + + OperateOnSortedMap(ids.special(), [&result](const std::string& special_name, + uint64_t special_id) { + result["special"][special_name] = special_id; + }); + + std::ofstream output_stream(filename); + output_stream << result << std::endl; +} + +} // 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 @@ +#ifndef TOOLS_UTIL_IDS_YAML_FORMAT_H_ +#define TOOLS_UTIL_IDS_YAML_FORMAT_H_ + +#include + +#include "proto/human.pb.h" + +namespace com::fourisland::lingo2_archipelago { + +IdMappings ReadIdsFromYaml(const std::string& filename); + +void WriteIdsAsYaml(const IdMappings& ids, const std::string& filename); + +} // namespace com::fourisland::lingo2_archipelago + +#endif /* TOOLS_UTIL_IDS_YAML_FORMAT_H_ */ -- cgit 1.4.1