WorldSave::WorldSave(const string &path)
: root_path(path)
-, conf_path(path + "world.conf")
+, world_conf_path(path + "world.conf")
+, gen_conf_path(path + "gen.conf")
, chunk_path(path + "chunks/%d/%d/%d.gz")
, chunk_bufsiz(chunk_path.length() + 3 * std::numeric_limits<int>::digits10)
, chunk_buf(new char[chunk_bufsiz]) {
bool WorldSave::Exists() const noexcept {
- return is_dir(root_path) && is_file(conf_path);
+ return is_dir(root_path) && is_file(world_conf_path);
}
+// TODO: better implementation of config files
void WorldSave::Read(World::Config &conf) const {
- ifstream in(conf_path);
+ ifstream in(world_conf_path);
if (!in) {
throw runtime_error("failed to open world config");
}
string name(line, name_begin, name_end - name_begin + 1);
string value(line, value_begin, value_end - value_begin + 1);
- if (name == "seed") {
- conf.gen.seed = stoul(value);
- } else {
+ // if (name == "seed") {
+ // conf.gen.seed = stoul(value);
+ // } else {
throw runtime_error("unknown world option: " + name);
- }
+ // }
}
if (in.bad()) {
throw runtime_error("IO error reading world config");
throw runtime_error("failed to create world save directory");
}
- ofstream out(conf_path);
- out << "seed = " << conf.gen.seed << endl;
+ ofstream out(world_conf_path);
+ //out << "seed = " << conf.gen.seed << endl;
out.close();
if (!out) {
}
+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");
+ }
+}
+
+
bool WorldSave::Exists(const Chunk::Pos &pos) const noexcept {
return is_file(ChunkPath(pos));
}