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/util/CMakeLists.txt | 4 +- tools/util/ids_yaml_format.cpp | 139 +++++++++++++++++++++++++++++++++++++++++ tools/util/ids_yaml_format.h | 16 +++++ 3 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 tools/util/ids_yaml_format.cpp create mode 100644 tools/util/ids_yaml_format.h (limited to 'tools/util') 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