]> git.localhorst.tv Git - blank.git/blobdiff - src/io/WorldSave.cpp
split chunk stuff
[blank.git] / src / io / WorldSave.cpp
index bd041f2e89cf20aba12487b1db2352ea010ebfac..5cfb7f9d7fd7ec24bcdfc2cdccb18db18e049348 100644 (file)
@@ -17,7 +17,8 @@ namespace blank {
 
 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]) {
@@ -26,12 +27,13 @@ WorldSave::WorldSave(const string &path)
 
 
 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");
        }
@@ -58,11 +60,11 @@ void WorldSave::Read(World::Config &conf) const {
                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");
@@ -74,8 +76,8 @@ void WorldSave::Write(const World::Config &conf) const {
                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) {
@@ -84,6 +86,60 @@ void WorldSave::Write(const World::Config &conf) const {
 }
 
 
+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));
 }