X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fio%2FWorldSave.cpp;h=5cfb7f9d7fd7ec24bcdfc2cdccb18db18e049348;hb=13e676a6e49128ebc6c63b8dd08bef51d360e8e9;hp=d90bce1654c4c5b0e1745043e328e8746d3e92c2;hpb=ede25c0a2f59e21521d1cd962e6ea9d78169ca12;p=blank.git diff --git a/src/io/WorldSave.cpp b/src/io/WorldSave.cpp index d90bce1..5cfb7f9 100644 --- a/src/io/WorldSave.cpp +++ b/src/io/WorldSave.cpp @@ -3,6 +3,7 @@ #include "filesystem.hpp" #include +#include #include #include #include @@ -16,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::digits10) , chunk_buf(new char[chunk_bufsiz]) { @@ -25,14 +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 { - cout << "reading world save" << endl; - - ifstream in(conf_path); + ifstream in(world_conf_path); if (!in) { throw runtime_error("failed to open world config"); } @@ -59,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"); @@ -71,14 +72,12 @@ void WorldSave::Read(World::Config &conf) const { } void WorldSave::Write(const World::Config &conf) const { - cout << "writing world save" << endl; - if (!make_dirs(root_path)) { 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) { @@ -87,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)); }