toml format is now used for settings file
This commit is contained in:
parent
3723cf491f
commit
69b309dc60
@ -4,6 +4,8 @@
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
|
||||
#include "commons.h"
|
||||
|
||||
using namespace json;
|
||||
|
||||
using std::string;
|
||||
@ -13,83 +15,6 @@ using std::unordered_map;
|
||||
using std::stringstream;
|
||||
using std::make_pair;
|
||||
|
||||
inline bool is_digit(int c) {
|
||||
return (c >= '0' && c <= '9');
|
||||
}
|
||||
|
||||
inline bool is_whitespace(int c) {
|
||||
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f';
|
||||
}
|
||||
|
||||
inline bool is_identifier_start(int c) {
|
||||
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || c == '-' || c == '.';
|
||||
}
|
||||
|
||||
inline bool is_identifier_part(int c) {
|
||||
return is_identifier_start(c) || is_digit(c);
|
||||
}
|
||||
|
||||
inline int is_box(int c) {
|
||||
switch (c) {
|
||||
case 'B':
|
||||
case 'b':
|
||||
return 2;
|
||||
case 'O':
|
||||
case 'o':
|
||||
return 8;
|
||||
case 'X':
|
||||
case 'x':
|
||||
return 16;
|
||||
}
|
||||
return 10;
|
||||
}
|
||||
|
||||
inline int char2int(int c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if (c >= 'a' && c <= 'f') {
|
||||
return 10 + c - 'a';
|
||||
}
|
||||
if (c >= 'A' && c <= 'F') {
|
||||
return 10 + c - 'A';
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
inline number_t power(number_t base, int64_t power) {
|
||||
number_t result = 1.0;
|
||||
for (int64_t i = 0; i < power; i++) {
|
||||
result *= base;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
string json::escape(string s) {
|
||||
std::stringstream ss;
|
||||
ss << '"';
|
||||
for (char c : s) {
|
||||
switch (c) {
|
||||
case '\n': ss << "\\n"; break;
|
||||
case '\r': ss << "\\r"; break;
|
||||
case '\t': ss << "\\t"; break;
|
||||
case '\f': ss << "\\f"; break;
|
||||
case '\b': ss << "\\b"; break;
|
||||
case '"': ss << "\\\""; break;
|
||||
case '\\': ss << "\\\\"; break;
|
||||
default:
|
||||
if (c < ' ') {
|
||||
ss << "\\" << std::oct << (int)c;
|
||||
break;
|
||||
}
|
||||
ss << c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ss << '"';
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
inline void newline(std::stringstream& ss, bool nice, uint indent, const std::string indentstr) {
|
||||
if (nice) {
|
||||
ss << "\n";
|
||||
@ -134,7 +59,7 @@ void stringify(Value* value, stringstream& ss, int indent, string indentstr, boo
|
||||
} else if (value->type == valtype::number) {
|
||||
ss << value->value.num;
|
||||
} else if (value->type == valtype::string) {
|
||||
ss << escape(*value->value.str);
|
||||
ss << escape_string(*value->value.str);
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,7 +76,7 @@ void stringifyObj(JObject* obj, stringstream& ss, int indent, string indentstr,
|
||||
newline(ss, nice, indent, indentstr);
|
||||
}
|
||||
Value* value = entry.second;
|
||||
ss << escape(key) << ": ";
|
||||
ss << escape_string(key) << ": ";
|
||||
stringify(value, ss, indent+1, indentstr, nice);
|
||||
index++;
|
||||
if (index < obj->map.size()) {
|
||||
@ -171,34 +96,6 @@ string json::stringify(JObject* obj, bool nice, string indent) {
|
||||
}
|
||||
|
||||
|
||||
parsing_error::parsing_error(string message,
|
||||
string filename,
|
||||
string source,
|
||||
uint pos,
|
||||
uint line,
|
||||
uint linestart)
|
||||
: std::runtime_error(message), filename(filename), source(source),
|
||||
pos(pos), line(line), linestart(linestart) {
|
||||
}
|
||||
|
||||
string parsing_error::errorLog() const {
|
||||
std::stringstream ss;
|
||||
uint linepos = pos - linestart;
|
||||
ss << "parsing error in file '" << filename;
|
||||
ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n";
|
||||
size_t end = source.find("\n", linestart);
|
||||
if (end == string::npos) {
|
||||
end = source.length();
|
||||
}
|
||||
ss << source.substr(linestart, end-linestart) << "\n";
|
||||
for (uint i = 0; i < linepos; i++) {
|
||||
ss << " ";
|
||||
}
|
||||
ss << "^";
|
||||
return ss.str();
|
||||
|
||||
}
|
||||
|
||||
JArray::~JArray() {
|
||||
for (auto value : values) {
|
||||
delete value;
|
||||
@ -209,7 +106,7 @@ std::string JArray::str(size_t index) const {
|
||||
return *values[index]->value.str;
|
||||
}
|
||||
|
||||
number_t JArray::num(size_t index) const {
|
||||
double JArray::num(size_t index) const {
|
||||
return values[index]->value.num;
|
||||
}
|
||||
|
||||
@ -233,14 +130,14 @@ JArray& JArray::put(string value) {
|
||||
}
|
||||
|
||||
JArray& JArray::put(uint value) {
|
||||
return put((number_t)value);
|
||||
return put((double)value);
|
||||
}
|
||||
|
||||
JArray& JArray::put(int value) {
|
||||
return put((number_t)value);
|
||||
return put((double)value);
|
||||
}
|
||||
|
||||
JArray& JArray::put(number_t value) {
|
||||
JArray& JArray::put(double value) {
|
||||
valvalue val;
|
||||
val.num = value;
|
||||
values.push_back(new Value(valtype::number, val));
|
||||
@ -287,7 +184,7 @@ void JObject::str(std::string key, std::string& dst) const {
|
||||
dst = *found->second->value.str;
|
||||
}
|
||||
|
||||
void JObject::num(std::string key, number_t& dst) const {
|
||||
void JObject::num(std::string key, double& dst) const {
|
||||
auto found = map.find(key);
|
||||
if (found != map.end())
|
||||
dst = found->second->value.num;
|
||||
@ -332,18 +229,18 @@ void JObject::flag(std::string key, bool& dst) const {
|
||||
}
|
||||
|
||||
JObject& JObject::put(string key, uint value) {
|
||||
return put(key, (number_t)value);
|
||||
return put(key, (double)value);
|
||||
}
|
||||
|
||||
JObject& JObject::put(string key, int value) {
|
||||
return put(key, (number_t)value);
|
||||
return put(key, (double)value);
|
||||
}
|
||||
|
||||
JObject& JObject::put(string key, float value) {
|
||||
return put(key, (number_t)value);
|
||||
return put(key, (double)value);
|
||||
}
|
||||
|
||||
JObject& JObject::put(string key, number_t value) {
|
||||
JObject& JObject::put(string key, double value) {
|
||||
auto found = map.find(key);
|
||||
if (found != map.end()) delete found->second;
|
||||
valvalue val;
|
||||
@ -401,7 +298,7 @@ Value::~Value() {
|
||||
}
|
||||
}
|
||||
|
||||
Parser::Parser(string filename, string source) : filename(filename), source(source) {
|
||||
Parser::Parser(string filename, string source) : BasicParser(filename, source) {
|
||||
}
|
||||
|
||||
JObject* Parser::parse() {
|
||||
@ -412,189 +309,6 @@ JObject* Parser::parse() {
|
||||
return parseObject();
|
||||
}
|
||||
|
||||
bool Parser::hasNext() {
|
||||
return pos < source.length();
|
||||
}
|
||||
|
||||
char Parser::nextChar() {
|
||||
if (!hasNext()) {
|
||||
throw error("unexpected end");
|
||||
}
|
||||
return source[pos++];
|
||||
}
|
||||
|
||||
void Parser::expect(char expected) {
|
||||
char c = peek();
|
||||
if (c != expected) {
|
||||
throw error("'"+string({expected})+"' expected");
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
|
||||
char Parser::peek() {
|
||||
skipWhitespace();
|
||||
if (pos >= source.length()) {
|
||||
throw error("unexpected end");
|
||||
}
|
||||
return source[pos];
|
||||
}
|
||||
|
||||
parsing_error Parser::error(std::string message) {
|
||||
return parsing_error(message, filename, source, pos, line, linestart);
|
||||
}
|
||||
|
||||
void Parser::skipWhitespace() {
|
||||
while (hasNext()) {
|
||||
char next = source[pos];
|
||||
if (next == '\n') {
|
||||
line++;
|
||||
linestart = ++pos;
|
||||
continue;
|
||||
}
|
||||
if (is_whitespace(next)) {
|
||||
pos++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string Parser::parseName() {
|
||||
char c = peek();
|
||||
if (!is_identifier_start(c)) {
|
||||
if (c == '"') {
|
||||
pos++;
|
||||
return parseString(c);
|
||||
}
|
||||
throw error("identifier expected");
|
||||
}
|
||||
int start = pos;
|
||||
while (hasNext() && is_identifier_part(source[pos])) {
|
||||
pos++;
|
||||
}
|
||||
return source.substr(start, pos-start);
|
||||
}
|
||||
|
||||
int64_t Parser::parseSimpleInt(int base) {
|
||||
char c = peek();
|
||||
int index = char2int(c);
|
||||
if (index == -1 || index >= base) {
|
||||
throw error("invalid number literal");
|
||||
}
|
||||
int64_t value = index;
|
||||
pos++;
|
||||
while (hasNext()) {
|
||||
c = source[pos];
|
||||
while (c == '_') {
|
||||
c = source[++pos];
|
||||
}
|
||||
index = char2int(c);
|
||||
if (index == -1 || index >= base) {
|
||||
return value;
|
||||
}
|
||||
value *= base;
|
||||
value += index;
|
||||
pos++;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
number_t Parser::parseNumber(int sign) {
|
||||
char c = peek();
|
||||
int base = 10;
|
||||
if (c == '0' && pos + 1 < source.length() &&
|
||||
(base = is_box(source[pos+1])) != 10) {
|
||||
pos += 2;
|
||||
return parseSimpleInt(base);
|
||||
}
|
||||
int64_t value = parseSimpleInt(base);
|
||||
if (!hasNext()) {
|
||||
return value * sign;
|
||||
}
|
||||
c = source[pos];
|
||||
if (c == 'e' || c == 'E') {
|
||||
pos++;
|
||||
int s = 1;
|
||||
if (peek() == '-') {
|
||||
s = -1;
|
||||
pos++;
|
||||
} else if (peek() == '+'){
|
||||
pos++;
|
||||
}
|
||||
return sign * value * power(10.0, s * parseSimpleInt(10));
|
||||
}
|
||||
if (c == '.') {
|
||||
pos++;
|
||||
int64_t expo = 1;
|
||||
while (hasNext() && source[pos] == '0') {
|
||||
expo *= 10;
|
||||
pos++;
|
||||
}
|
||||
int64_t afterdot = 0;
|
||||
if (hasNext() && is_digit(source[pos])) {
|
||||
afterdot = parseSimpleInt(10);
|
||||
}
|
||||
expo *= power(10, fmax(0, log10(afterdot) + 1));
|
||||
c = source[pos];
|
||||
|
||||
number_t dvalue = (value + (afterdot / (number_t)expo));
|
||||
if (c == 'e' || c == 'E') {
|
||||
pos++;
|
||||
int s = 1;
|
||||
if (peek() == '-') {
|
||||
s = -1;
|
||||
pos++;
|
||||
} else if (peek() == '+'){
|
||||
pos++;
|
||||
}
|
||||
return sign * dvalue * power(10.0, s * parseSimpleInt(10));
|
||||
}
|
||||
return dvalue;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
string Parser::parseString(char quote) {
|
||||
std::stringstream ss;
|
||||
while (hasNext()) {
|
||||
char c = source[pos];
|
||||
if (c == quote) {
|
||||
pos++;
|
||||
return ss.str();
|
||||
}
|
||||
if (c == '\\') {
|
||||
pos++;
|
||||
c = nextChar();
|
||||
if (c >= '0' && c <= '7') {
|
||||
pos--;
|
||||
ss << (char)parseSimpleInt(8);
|
||||
continue;
|
||||
}
|
||||
switch (c) {
|
||||
case 'n': ss << '\n'; break;
|
||||
case 'r': ss << '\r'; break;
|
||||
case 'b': ss << '\b'; break;
|
||||
case 't': ss << '\t'; break;
|
||||
case 'f': ss << '\f'; break;
|
||||
case '\'': ss << '\\'; break;
|
||||
case '"': ss << '"'; break;
|
||||
case '\\': ss << '\\'; break;
|
||||
case '/': ss << '/'; break;
|
||||
case '\n': pos++; continue;
|
||||
default:
|
||||
throw error("'\\" + string({c}) + "' is an illegal escape");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c == '\n') {
|
||||
throw error("non-closed string literal");
|
||||
}
|
||||
ss << c;
|
||||
pos++;
|
||||
}
|
||||
throw error("unexpected end");
|
||||
}
|
||||
|
||||
JObject* Parser::parseObject() {
|
||||
expect('{');
|
||||
unique_ptr<JObject> obj(new JObject());
|
||||
@ -648,10 +362,15 @@ Value* Parser::parseValue() {
|
||||
if (literal == "true") {
|
||||
val.boolean = true;
|
||||
return new Value(valtype::boolean, val);
|
||||
}
|
||||
if (literal == "false") {
|
||||
} else if (literal == "false") {
|
||||
val.boolean = false;
|
||||
return new Value(valtype::boolean, val);
|
||||
} else if (literal == "inf") {
|
||||
val.num = INFINITY;
|
||||
return new Value(valtype::number, val);
|
||||
} else if (literal == "nan") {
|
||||
val.num = NAN;
|
||||
return new Value(valtype::number, val);
|
||||
}
|
||||
throw error("invalid literal");
|
||||
}
|
||||
|
||||
@ -7,33 +7,16 @@
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "commons.h"
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
namespace json {
|
||||
typedef double number_t;
|
||||
class JObject;
|
||||
class JArray;
|
||||
class Value;
|
||||
|
||||
extern std::string escape(std::string s);
|
||||
extern std::string stringify(JObject* obj, bool nice, std::string indent);
|
||||
class parsing_error : public std::runtime_error {
|
||||
public:
|
||||
std::string filename;
|
||||
std::string source;
|
||||
uint pos;
|
||||
uint line;
|
||||
uint linestart;
|
||||
|
||||
parsing_error(std::string message,
|
||||
std::string filename,
|
||||
std::string source,
|
||||
uint pos,
|
||||
uint line,
|
||||
uint linestart);
|
||||
|
||||
std::string errorLog() const;
|
||||
};
|
||||
|
||||
enum class valtype {
|
||||
object, array, number, string, boolean
|
||||
@ -43,7 +26,7 @@ namespace json {
|
||||
JObject* obj;
|
||||
JArray* arr;
|
||||
std::string* str;
|
||||
number_t num;
|
||||
double num;
|
||||
bool boolean;
|
||||
};
|
||||
|
||||
@ -61,7 +44,7 @@ namespace json {
|
||||
~JArray();
|
||||
|
||||
std::string str(size_t index) const;
|
||||
number_t num(size_t index) const;
|
||||
double num(size_t index) const;
|
||||
JObject* obj(size_t index) const;
|
||||
JArray* arr(size_t index) const;
|
||||
bool flag(size_t index) const;
|
||||
@ -73,7 +56,7 @@ namespace json {
|
||||
JArray& put(uint value);
|
||||
JArray& put(int value);
|
||||
JArray& put(float value);
|
||||
JArray& put(number_t value);
|
||||
JArray& put(double value);
|
||||
JArray& put(std::string value);
|
||||
JArray& put(JObject* value);
|
||||
JArray& put(JArray* value);
|
||||
@ -89,7 +72,7 @@ namespace json {
|
||||
void num(std::string key, int& dst) const;
|
||||
void num(std::string key, float& dst) const;
|
||||
void num(std::string key, uint& dst) const;
|
||||
void num(std::string key, number_t& dst) const;
|
||||
void num(std::string key, double& dst) const;
|
||||
JObject* obj(std::string key) const;
|
||||
JArray* arr(std::string key) const;
|
||||
void flag(std::string key, bool& dst) const;
|
||||
@ -97,37 +80,17 @@ namespace json {
|
||||
JObject& put(std::string key, uint value);
|
||||
JObject& put(std::string key, int value);
|
||||
JObject& put(std::string key, float value);
|
||||
JObject& put(std::string key, number_t value);
|
||||
JObject& put(std::string key, double value);
|
||||
JObject& put(std::string key, std::string value);
|
||||
JObject& put(std::string key, JObject* value);
|
||||
JObject& put(std::string key, JArray* value);
|
||||
JObject& put(std::string key, bool value);
|
||||
};
|
||||
|
||||
class Parser {
|
||||
std::string filename;
|
||||
std::string source;
|
||||
uint pos = 0;
|
||||
uint line = 0;
|
||||
uint linestart = 0;
|
||||
|
||||
void skipWhitespace();
|
||||
void expect(char expected);
|
||||
char peek();
|
||||
char nextChar();
|
||||
bool hasNext();
|
||||
|
||||
std::string parseName();
|
||||
int64_t parseSimpleInt(int base);
|
||||
number_t parseNumber(int sign);
|
||||
std::string parseString(char chr);
|
||||
|
||||
class Parser : public BasicParser {
|
||||
JArray* parseArray();
|
||||
JObject* parseObject();
|
||||
Value* parseValue();
|
||||
|
||||
parsing_error error(std::string message);
|
||||
|
||||
public:
|
||||
Parser(std::string filename, std::string source);
|
||||
|
||||
|
||||
@ -43,9 +43,8 @@ struct EngineSettings {
|
||||
Use values in range [1.0 - 2.0] where 1.0 is linear, 2.0 is quadratic
|
||||
*/
|
||||
float fogCurve = 1.6f;
|
||||
|
||||
|
||||
};
|
||||
|
||||
void load_settings(EngineSettings& settings, std::string filename);
|
||||
void save_settings(EngineSettings& settings, std::string filename);
|
||||
|
||||
#endif // SRC_SETTINGS_H_
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
#include "../typedefs.h"
|
||||
|
||||
#define SETTINGS_FILE "settings.json"
|
||||
#define SETTINGS_FILE "settings.toml"
|
||||
#define SCREENSHOTS_FOLDER "screenshots"
|
||||
|
||||
using std::string;
|
||||
|
||||
@ -10,18 +10,47 @@
|
||||
#include "util/platform.h"
|
||||
#include "engine.h"
|
||||
|
||||
#include "coders/toml.h"
|
||||
#include "files/files.h"
|
||||
|
||||
inline toml::Wrapper create_wrapper(EngineSettings& settings) {
|
||||
toml::Wrapper wrapper;
|
||||
toml::Section& display = wrapper.add("display");
|
||||
display.add("width", &settings.display.width);
|
||||
display.add("height", &settings.display.height);
|
||||
display.add("samples", &settings.display.samples);
|
||||
display.add("swap-interval", &settings.display.swapInterval);
|
||||
|
||||
toml::Section& chunks = wrapper.add("chunks");
|
||||
chunks.add("load-distance", &settings.chunks.loadDistance);
|
||||
chunks.add("load-speed", &settings.chunks.loadSpeed);
|
||||
chunks.add("padding", &settings.chunks.padding);
|
||||
|
||||
toml::Section& camera = wrapper.add("camera");
|
||||
camera.add("fov-effects", &settings.camera.fovEvents);
|
||||
camera.add("shaking", &settings.camera.shaking);
|
||||
|
||||
toml::Section& graphics = wrapper.add("graphics");
|
||||
graphics.add("fog-curve", &settings.fogCurve);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
int main() {
|
||||
platform::configure_encoding();
|
||||
setup_definitions();
|
||||
try {
|
||||
EngineSettings settings;
|
||||
toml::Wrapper wrapper = create_wrapper(settings);
|
||||
|
||||
std::string settings_file = platform::get_settings_file();
|
||||
if (std::filesystem::is_regular_file(settings_file)) {
|
||||
std::cout << "-- loading settings" << std::endl;
|
||||
load_settings(settings, settings_file);
|
||||
std::string content = files::read_string(settings_file);
|
||||
toml::Reader reader(&wrapper, settings_file, content);
|
||||
reader.read();
|
||||
} else {
|
||||
std::cout << "-- creating settings file " << settings_file << std::endl;
|
||||
save_settings(settings, settings_file);
|
||||
files::write_string(settings_file, wrapper.write());
|
||||
}
|
||||
Engine engine(settings);
|
||||
engine.mainloop();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user