1 #include "WorldSave.hpp"
3 #include "../app/io.hpp"
15 WorldSave::WorldSave(const string &path)
17 , conf_path(path + "world.conf") {
22 bool WorldSave::Exists() const noexcept {
23 return is_dir(root_path) && is_file(conf_path);
27 void WorldSave::Create(const World::Config &conf) const {
28 cout << "creating world save" << endl;
30 if (!make_dirs(root_path)) {
31 throw runtime_error("failed to create world save directory");
34 ofstream out(conf_path);
35 out << "seed = " << conf.gen.seed << endl;
39 throw runtime_error("failed to write world config");
43 void WorldSave::Read(World::Config &conf) const {
44 cout << "reading world save" << endl;
46 ifstream in(conf_path);
48 throw runtime_error("failed to open world config");
51 constexpr char spaces[] = "\n\r\t ";
54 while (getline(in, line)) {
55 if (line.empty() || line[0] == '#') continue;
56 auto equals_pos = line.find_first_of('=');
58 auto name_begin = line.find_first_not_of(spaces, 0, sizeof(spaces));
59 auto name_end = equals_pos - 1;
60 while (name_end > name_begin && isspace(line[name_end])) {
64 auto value_begin = line.find_first_not_of(spaces, equals_pos + 1, sizeof(spaces));
65 auto value_end = line.length() - 1;
66 while (value_end > value_begin && isspace(line[value_end])) {
70 string name(line, name_begin, name_end - name_begin + 1);
71 string value(line, value_begin, value_end - value_begin + 1);
74 conf.gen.seed = stoul(value);
76 throw runtime_error("unknown world option: " + name);
80 throw runtime_error("IO error reading world config");