+void WorldSave::Read(Generator::Config &conf) const {
+ ifstream in(gen_conf_path);
+ if (!in) {
+ throw runtime_error("failed to open generator config");
+ }
+
+ constexpr char spaces[] = "\n\r\t ";
+
+ string line;
+ while (getline(in, line)) {
+ if (line.empty() || line[0] == '#') continue;
+ auto equals_pos = line.find_first_of('=');
+
+ auto name_begin = line.find_first_not_of(spaces, 0, sizeof(spaces));
+ auto name_end = equals_pos - 1;
+ while (name_end > name_begin && isspace(line[name_end])) {
+ --name_end;
+ }
+
+ auto value_begin = line.find_first_not_of(spaces, equals_pos + 1, sizeof(spaces));
+ auto value_end = line.length() - 1;
+ while (value_end > value_begin && isspace(line[value_end])) {
+ --value_end;
+ }
+
+ string name(line, name_begin, name_end - name_begin + 1);
+ string value(line, value_begin, value_end - value_begin + 1);
+
+ if (name == "seed") {
+ conf.seed = stoul(value);
+ } else {
+ throw runtime_error("unknown generator option: " + name);
+ }
+ }
+ if (in.bad()) {
+ throw runtime_error("IO error reading world config");
+ }
+}
+
+void WorldSave::Write(const Generator::Config &conf) const {
+ if (!make_dirs(root_path)) {
+ throw runtime_error("failed to create world save directory");
+ }
+
+ ofstream out(gen_conf_path);
+ out << "seed = " << conf.seed << endl;
+ out.close();
+
+ if (!out) {
+ throw runtime_error("failed to write generator config");
+ }
+}
+
+