summary refs log tree commit diff stats
path: root/tools/datapacker/main.cpp
blob: b2ec068bdbce68c2112429863053bb1b406b2b11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <google/protobuf/message.h>
#include <google/protobuf/text_format.h>

#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <optional>
#include <sstream>
#include <string>

#include "container.h"
#include "proto/data.pb.h"
#include "proto/human.pb.h"

namespace com::fourisland::lingo2_archipelago {
namespace {

template <typename T>
T ReadMessageFromFile(const std::string& path) {
  std::ifstream file(path);
  std::stringstream buffer;
  buffer << file.rdbuf();

  T message;
  google::protobuf::TextFormat::ParseFromString(buffer.str(), &message);

  return message;
}

class DataPacker {
 public:
  DataPacker(const std::string& mapdir, const std::string& outputpath)
      : mapdir_(mapdir), outputpath_(outputpath) {}

  void Run() {
    std::filesystem::path datadir_path = mapdir_;

    ProcessConnectionsFile(datadir_path / "connections.txtpb", std::nullopt);

    {
      std::ofstream outputfile(outputpath_);
      container_.all_objects().SerializeToOstream(&outputfile);
    }

    std::string output;
    google::protobuf::TextFormat::PrintToString(container_.all_objects(),
                                                &output);
    std::cout << output << std::endl;
  }

 private:
  void ProcessConnectionsFile(std::filesystem::path path,
                              std::optional<std::string> current_map_name) {
    if (!std::filesystem::exists(path)) {
      return;
    }

    auto connections = ReadMessageFromFile<HumanConnections>(path.string());

    for (const HumanConnection& connection : connections.connections()) {
      ProcessConnection(connection, current_map_name);
    }
  }

  void ProcessConnection(const HumanConnection& human_connection,
                         const std::optional<std::string>& current_map_name) {
    Connection f_connection;

    if (human_connection.has_from_room()) {
      f_connection.set_from_room(container_.FindOrAddRoom(
          std::nullopt, human_connection.from_room(), current_map_name));
    } else if (human_connection.has_from()) {
      ProcessSingleConnection(human_connection.from(), current_map_name,
                              f_connection);
    }

    Connection r_connection;
    r_connection.set_to_room(f_connection.from_room());

    if (human_connection.has_to_room()) {
      r_connection.set_to_room(container_.FindOrAddRoom(
          std::nullopt, human_connection.to_room(), current_map_name));
    } else if (human_connection.has_to()) {
      ProcessSingleConnection(human_connection.to(), current_map_name,
                              r_connection);
    }

    f_connection.set_to_room(r_connection.from_room());

    if (human_connection.has_door()) {
      std::optional<std::string> map_name =
          human_connection.door().has_map()
              ? std::optional<std::string>(human_connection.door().map())
              : std::nullopt;
      uint64_t door_id = container_.FindOrAddDoor(
          map_name, human_connection.door().name(), current_map_name);
      f_connection.set_required_door(door_id);
      r_connection.set_required_door(door_id);
    }

    container_.AddConnection(f_connection);
    if (!human_connection.oneway()) {
      container_.AddConnection(r_connection);
    }
  }

  void ProcessSingleConnection(
      const HumanConnection::Endpoint& endpoint,
      const std::optional<std::string>& current_map_name,
      Connection& connection) {
    if (endpoint.has_room()) {
      std::optional<std::string> map_name =
          endpoint.room().has_map()
              ? std::optional<std::string>(endpoint.room().map())
              : std::nullopt;
      connection.set_from_room(container_.FindOrAddRoom(
          map_name, endpoint.room().name(), current_map_name));
    } else if (endpoint.has_painting()) {
      std::optional<std::string> map_name =
          endpoint.painting().has_map()
              ? std::optional<std::string>(endpoint.painting().map())
              : std::nullopt;

      std::string room_name;
      if (!endpoint.painting().has_room()) {
        std::cout << "Missing room name for painting "
                  << endpoint.painting().name() << std::endl;
        room_name = "default";
      } else {
        room_name = endpoint.painting().room();
      }

      connection.set_from_room(
          container_.FindOrAddRoom(map_name, room_name, current_map_name));
      connection.set_painting(container_.FindOrAddPainting(
          map_name, room_name, endpoint.painting().name(), current_map_name,
          std::nullopt));
    } else if (endpoint.has_port()) {
      std::optional<std::string> map_name =
          endpoint.port().has_map()
              ? std::optional<std::string>(endpoint.port().map())
              : std::nullopt;

      std::string room_name;
      if (!endpoint.port().has_room()) {
        std::cout << "Missing room name for port " << endpoint.port().name()
                  << std::endl;
        room_name = "default";
      } else {
        room_name = endpoint.port().room();
      }

      connection.set_from_room(
          container_.FindOrAddRoom(map_name, room_name, current_map_name));
      connection.set_port(
          container_.FindOrAddPort(map_name, room_name, endpoint.port().name(),
                                   current_map_name, std::nullopt));
    } else if (endpoint.has_panel()) {
      std::optional<std::string> map_name =
          endpoint.panel().has_map()
              ? std::optional<std::string>(endpoint.panel().map())
              : std::nullopt;

      std::string room_name;
      if (!endpoint.panel().has_room()) {
        std::cout << "Missing room name for panel " << endpoint.panel().name()
                  << std::endl;
        room_name = "default";
      } else {
        room_name = endpoint.panel().room();
      }

      connection.set_from_room(
          container_.FindOrAddRoom(map_name, room_name, current_map_name));
      connection.mutable_panel()->set_panel(container_.FindOrAddPanel(
          map_name, room_name, endpoint.panel().name(), current_map_name,
          std::nullopt));
      if (endpoint.panel().has_answer()) {
        connection.mutable_panel()->set_answer(endpoint.panel().answer());
      }
    }
  }

  std::string mapdir_;
  std::string outputpath_;

  Container container_;
};

}  // namespace
}  // namespace com::fourisland::lingo2_archipelago

int main(int argc, char** argv) {
  if (argc != 3) {
    std::cout << "Incorrect argument count." << std::endl;
    std::cout << "Usage: datapacker [path to map directory] [output file]"
              << std::endl;
    return 1;
  }

  std::string mapdir = argv[1];
  std::string outputpath = argv[2];

  com::fourisland::lingo2_archipelago::DataPacker data_packer(mapdir,
                                                              outputpath);
  data_packer.Run();

  return 0;
}