summary refs log tree commit diff stats
path: root/tools
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2025-08-19 20:19:48 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2025-08-19 20:19:48 -0400
commit724f8092c4808cdad47316e00949c04ee797acb5 (patch)
tree758d598fbc2d62c4620956b71b2a851791fd2dbf /tools
parent511d813113b6e7986aff665603ca0196799e232d (diff)
downloadlingo2-archipelago-724f8092c4808cdad47316e00949c04ee797acb5.tar.gz
lingo2-archipelago-724f8092c4808cdad47316e00949c04ee797acb5.tar.bz2
lingo2-archipelago-724f8092c4808cdad47316e00949c04ee797acb5.zip
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.
Diffstat (limited to 'tools')
-rw-r--r--tools/assign_ids/main.cpp17
-rw-r--r--tools/datapacker/main.cpp5
-rw-r--r--tools/util/CMakeLists.txt4
-rw-r--r--tools/util/ids_yaml_format.cpp139
-rw-r--r--tools/util/ids_yaml_format.h16
5 files changed, 169 insertions, 12 deletions
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 @@
9#include <string> 9#include <string>
10 10
11#include "proto/human.pb.h" 11#include "proto/human.pb.h"
12#include "util/ids_yaml_format.h"
12#include "util/naming.h" 13#include "util/naming.h"
13 14
14namespace com::fourisland::lingo2_archipelago { 15namespace com::fourisland::lingo2_archipelago {
@@ -34,7 +35,7 @@ class AssignIds {
34 35
35 void Run() { 36 void Run() {
36 std::filesystem::path datadir_path = mapdir_; 37 std::filesystem::path datadir_path = mapdir_;
37 std::filesystem::path ids_path = datadir_path / "ids.txtpb"; 38 std::filesystem::path ids_path = datadir_path / "ids.yaml";
38 39
39 ReadIds(ids_path); 40 ReadIds(ids_path);
40 41
@@ -46,7 +47,11 @@ class AssignIds {
46 } 47 }
47 48
48 void ReadIds(std::filesystem::path path) { 49 void ReadIds(std::filesystem::path path) {
49 id_mappings_ = ReadMessageFromFile<IdMappings>(path.string()); 50 if (!std::filesystem::exists(path)) {
51 return;
52 }
53
54 id_mappings_ = ReadIdsFromYaml(path.string());
50 55
51 for (const auto& [_, map] : id_mappings_.maps()) { 56 for (const auto& [_, map] : id_mappings_.maps()) {
52 for (const auto& [_, id] : map.doors()) { 57 for (const auto& [_, id] : map.doors()) {
@@ -86,13 +91,7 @@ class AssignIds {
86 } 91 }
87 92
88 void WriteIds(std::filesystem::path path) { 93 void WriteIds(std::filesystem::path path) {
89 std::string output; 94 WriteIdsAsYaml(id_mappings_, path.string());
90 google::protobuf::TextFormat::PrintToString(id_mappings_, &output);
91
92 {
93 std::ofstream outputfile(path.string());
94 outputfile << output;
95 }
96 } 95 }
97 96
98 void ProcessMaps(std::filesystem::path path) { 97 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 @@
14#include "container.h" 14#include "container.h"
15#include "proto/data.pb.h" 15#include "proto/data.pb.h"
16#include "proto/human.pb.h" 16#include "proto/human.pb.h"
17#include "util/ids_yaml_format.h"
17 18
18namespace com::fourisland::lingo2_archipelago { 19namespace com::fourisland::lingo2_archipelago {
19namespace { 20namespace {
@@ -42,7 +43,7 @@ class DataPacker {
42 43
43 ProcessConnectionsFile(datadir_path / "connections.txtpb", std::nullopt); 44 ProcessConnectionsFile(datadir_path / "connections.txtpb", std::nullopt);
44 ProcessMaps(datadir_path); 45 ProcessMaps(datadir_path);
45 ProcessIdsFile(datadir_path / "ids.txtpb"); 46 ProcessIdsFile(datadir_path / "ids.yaml");
46 47
47 { 48 {
48 std::ofstream outputfile(outputpath_); 49 std::ofstream outputfile(outputpath_);
@@ -490,7 +491,7 @@ class DataPacker {
490 } 491 }
491 492
492 void ProcessIdsFile(std::filesystem::path path) { 493 void ProcessIdsFile(std::filesystem::path path) {
493 auto ids = ReadMessageFromFile<IdMappings>(path.string()); 494 auto ids = ReadIdsFromYaml(path.string());
494 495
495 for (const auto& [map_name, map] : ids.maps()) { 496 for (const auto& [map_name, map] : ids.maps()) {
496 for (const auto& [door_name, ap_id] : map.doors()) { 497 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 @@
1find_package(Protobuf REQUIRED) 1find_package(Protobuf REQUIRED)
2find_package(yaml-cpp REQUIRED)
2 3
3add_library(util 4add_library(util
4 godot_scene.cpp 5 godot_scene.cpp
5 identifiers.cpp 6 identifiers.cpp
7 ids_yaml_format.cpp
6 naming.cpp 8 naming.cpp
7) 9)
8set_property(TARGET util PROPERTY CXX_STANDARD 20) 10set_property(TARGET util PROPERTY CXX_STANDARD 20)
9set_property(TARGET util PROPERTY CXX_STANDARD_REQUIRED ON) 11set_property(TARGET util PROPERTY CXX_STANDARD_REQUIRED ON)
10target_include_directories(util PUBLIC ${CMAKE_BINARY_DIR}) 12target_include_directories(util PUBLIC ${CMAKE_BINARY_DIR})
11target_link_libraries(util PUBLIC protos protobuf::libprotobuf) 13target_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 @@
1#include "ids_yaml_format.h"
2
3#include <yaml-cpp/yaml.h>
4
5#include <fstream>
6#include <functional>
7
8namespace com::fourisland::lingo2_archipelago {
9namespace {
10
11template <typename T>
12void OperateOnSortedMap(
13 const T& map, std::function<void(const std::string& name,
14 const typename T::mapped_type& value)>
15 callback) {
16 std::vector<std::string> names;
17 for (const auto& it : map) {
18 names.push_back(it.first);
19 }
20
21 std::sort(names.begin(), names.end());
22
23 for (const std::string& name : names) {
24 callback(name, map.at(name));
25 }
26}
27
28} // namespace
29
30IdMappings ReadIdsFromYaml(const std::string& filename) {
31 IdMappings result;
32
33 YAML::Node document = YAML::LoadFile(filename);
34
35 if (document["maps"]) {
36 for (const auto& map_it : document["maps"]) {
37 IdMappings::MapIds& map_ids =
38 (*result.mutable_maps())[map_it.first.as<std::string>()];
39
40 if (map_it.second["rooms"]) {
41 for (const auto& room_it : map_it.second["rooms"]) {
42 IdMappings::RoomIds& room_ids =
43 (*map_ids.mutable_rooms())[room_it.first.as<std::string>()];
44
45 if (room_it.second["panels"]) {
46 for (const auto& panel_it : room_it.second["panels"]) {
47 (*room_ids.mutable_panels())[panel_it.first.as<std::string>()] =
48 panel_it.second.as<uint64_t>();
49 }
50 }
51
52 if (room_it.second["masteries"]) {
53 for (const auto& mastery_it : room_it.second["masteries"]) {
54 (*room_ids
55 .mutable_masteries())[mastery_it.first.as<std::string>()] =
56 mastery_it.second.as<uint64_t>();
57 }
58 }
59 }
60 }
61
62 if (map_it.second["doors"]) {
63 for (const auto& door_it : map_it.second["doors"]) {
64 (*map_ids.mutable_doors())[door_it.first.as<std::string>()] =
65 door_it.second.as<uint64_t>();
66 }
67 }
68 }
69 }
70
71 if (document["letters"]) {
72 for (const auto& letter_it : document["letters"]) {
73 (*result.mutable_letters())[letter_it.first.as<std::string>()] =
74 letter_it.second.as<uint64_t>();
75 }
76 }
77
78 if (document["special"]) {
79 for (const auto& special_it : document["special"]) {
80 (*result.mutable_special())[special_it.first.as<std::string>()] =
81 special_it.second.as<uint64_t>();
82 }
83 }
84
85 return result;
86}
87
88void WriteIdsAsYaml(const IdMappings& ids, const std::string& filename) {
89 YAML::Node result;
90
91 OperateOnSortedMap(ids.maps(), [&result](const std::string& map_name,
92 const IdMappings::MapIds& map_ids) {
93 YAML::Node map_node;
94
95 OperateOnSortedMap(
96 map_ids.rooms(), [&map_node](const std::string& room_name,
97 const IdMappings::RoomIds& room_ids) {
98 YAML::Node room_node;
99
100 OperateOnSortedMap(
101 room_ids.panels(),
102 [&room_node](const std::string& panel_name, uint64_t panel_id) {
103 room_node["panels"][panel_name] = panel_id;
104 });
105
106 OperateOnSortedMap(room_ids.masteries(),
107 [&room_node](const std::string& mastery_name,
108 uint64_t mastery_id) {
109 room_node["masteries"][mastery_name] =
110 mastery_id;
111 });
112
113 map_node["rooms"][room_name] = std::move(room_node);
114 });
115
116 OperateOnSortedMap(
117 map_ids.doors(),
118 [&map_node](const std::string& door_name, uint64_t door_id) {
119 map_node["doors"][door_name] = door_id;
120 });
121
122 result["maps"][map_name] = std::move(map_node);
123 });
124
125 OperateOnSortedMap(ids.letters(), [&result](const std::string& letter_name,
126 uint64_t letter_id) {
127 result["letters"][letter_name] = letter_id;
128 });
129
130 OperateOnSortedMap(ids.special(), [&result](const std::string& special_name,
131 uint64_t special_id) {
132 result["special"][special_name] = special_id;
133 });
134
135 std::ofstream output_stream(filename);
136 output_stream << result << std::endl;
137}
138
139} // 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 @@
1#ifndef TOOLS_UTIL_IDS_YAML_FORMAT_H_
2#define TOOLS_UTIL_IDS_YAML_FORMAT_H_
3
4#include <string>
5
6#include "proto/human.pb.h"
7
8namespace com::fourisland::lingo2_archipelago {
9
10IdMappings ReadIdsFromYaml(const std::string& filename);
11
12void WriteIdsAsYaml(const IdMappings& ids, const std::string& filename);
13
14} // namespace com::fourisland::lingo2_archipelago
15
16#endif /* TOOLS_UTIL_IDS_YAML_FORMAT_H_ */