toml::Wrapper removed

This commit is contained in:
MihailRis 2024-04-28 17:23:52 +03:00
parent 184ecd88ba
commit c3b5576c02
22 changed files with 795 additions and 965 deletions

View File

@ -1,320 +1,323 @@
#include "commons.h" #include "commons.h"
#include "../util/stringutil.h" #include "../util/stringutil.h"
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <math.h> #include <math.h>
inline double power(double base, int64_t power) { inline double power(double base, int64_t power) {
double result = 1.0; double result = 1.0;
for (int64_t i = 0; i < power; i++) { for (int64_t i = 0; i < power; i++) {
result *= base; result *= base;
} }
return result; return result;
} }
parsing_error::parsing_error( parsing_error::parsing_error(
std::string message, std::string message,
std::string filename, std::string filename,
std::string source, std::string source,
uint pos, uint pos,
uint line, uint line,
uint linestart) uint linestart)
: std::runtime_error(message), filename(filename), source(source), : std::runtime_error(message), filename(filename), source(source),
pos(pos), line(line), linestart(linestart) { pos(pos), line(line), linestart(linestart) {
} }
std::string parsing_error::errorLog() const { std::string parsing_error::errorLog() const {
std::stringstream ss; std::stringstream ss;
uint linepos = pos - linestart; uint linepos = pos - linestart;
ss << "parsing error in file '" << filename; ss << "parsing error in file '" << filename;
ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n"; ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n";
size_t end = source.find("\n", linestart); size_t end = source.find("\n", linestart);
if (end == std::string::npos) { if (end == std::string::npos) {
end = source.length(); end = source.length();
} }
ss << source.substr(linestart, end-linestart) << "\n"; ss << source.substr(linestart, end-linestart) << "\n";
for (uint i = 0; i < linepos; i++) { for (uint i = 0; i < linepos; i++) {
ss << " "; ss << " ";
} }
ss << "^"; ss << "^";
return ss.str(); return ss.str();
} }
BasicParser::BasicParser(std::string file, std::string source) : filename(file), source(source) { BasicParser::BasicParser(
} const std::string& file,
const std::string& source
void BasicParser::skipWhitespace() { ) : filename(file), source(source) {
while (hasNext()) { }
char next = source[pos];
if (next == '\n') { void BasicParser::skipWhitespace() {
line++; while (hasNext()) {
linestart = ++pos; char next = source[pos];
continue; if (next == '\n') {
} line++;
if (is_whitespace(next)) { linestart = ++pos;
pos++; continue;
} else { }
break; if (is_whitespace(next)) {
} pos++;
} } else {
} break;
}
void BasicParser::skip(size_t n) { }
n = std::min(n, source.length()-pos); }
for (size_t i = 0; i < n; i++) { void BasicParser::skip(size_t n) {
char next = source[pos++]; n = std::min(n, source.length()-pos);
if (next == '\n') {
line++; for (size_t i = 0; i < n; i++) {
linestart = pos; char next = source[pos++];
} if (next == '\n') {
} line++;
} linestart = pos;
}
void BasicParser::skipLine() { }
while (hasNext()) { }
if (source[pos] == '\n') {
pos++; void BasicParser::skipLine() {
linestart = pos; while (hasNext()) {
line++; if (source[pos] == '\n') {
break; pos++;
} linestart = pos;
pos++; line++;
} break;
} }
pos++;
bool BasicParser::skipTo(const std::string& substring) { }
size_t idx = source.find(substring, pos); }
if (idx == std::string::npos) {
skip(source.length()-pos); bool BasicParser::skipTo(const std::string& substring) {
return false; size_t idx = source.find(substring, pos);
} else { if (idx == std::string::npos) {
skip(idx-pos); skip(source.length()-pos);
return true; return false;
} } else {
} skip(idx-pos);
return true;
bool BasicParser::hasNext() { }
return pos < source.length(); }
}
bool BasicParser::hasNext() {
bool BasicParser::isNext(const std::string& substring) { return pos < source.length();
if (source.length() - pos < substring.length()) { }
return false;
} bool BasicParser::isNext(const std::string& substring) {
return source.substr(pos, substring.length()) == substring; if (source.length() - pos < substring.length()) {
} return false;
}
char BasicParser::nextChar() { return source.substr(pos, substring.length()) == substring;
if (!hasNext()) { }
throw error("unexpected end");
} char BasicParser::nextChar() {
return source[pos++]; if (!hasNext()) {
} throw error("unexpected end");
}
void BasicParser::expect(char expected) { return source[pos++];
char c = peek(); }
if (c != expected) {
throw error("'"+std::string({expected})+"' expected"); void BasicParser::expect(char expected) {
} char c = peek();
pos++; if (c != expected) {
} throw error("'"+std::string({expected})+"' expected");
}
void BasicParser::expect(const std::string& substring) { pos++;
if (substring.empty()) }
return;
for (uint i = 0; i < substring.length(); i++) { void BasicParser::expect(const std::string& substring) {
if (source.length() <= pos + i || source[pos+i] != substring[i]) { if (substring.empty())
throw error(util::quote(substring)+" expected"); return;
} for (uint i = 0; i < substring.length(); i++) {
} if (source.length() <= pos + i || source[pos+i] != substring[i]) {
pos += substring.length(); throw error(util::quote(substring)+" expected");
} }
}
void BasicParser::expectNewLine() { pos += substring.length();
while (hasNext()) { }
char next = source[pos];
if (next == '\n') { void BasicParser::expectNewLine() {
line++; while (hasNext()) {
linestart = ++pos; char next = source[pos];
return; if (next == '\n') {
} line++;
if (is_whitespace(next)) { linestart = ++pos;
pos++; return;
} else { }
throw error("line separator expected"); if (is_whitespace(next)) {
} pos++;
} } else {
} throw error("line separator expected");
}
void BasicParser::goBack() { }
if (pos) pos--; }
}
void BasicParser::goBack() {
char BasicParser::peek() { if (pos) pos--;
skipWhitespace(); }
if (pos >= source.length()) {
throw error("unexpected end"); char BasicParser::peek() {
} skipWhitespace();
return source[pos]; if (pos >= source.length()) {
} throw error("unexpected end");
}
std::string BasicParser::parseName() { return source[pos];
char c = peek(); }
if (!is_identifier_start(c)) {
if (c == '"') { std::string BasicParser::parseName() {
pos++; char c = peek();
return parseString(c); if (!is_identifier_start(c)) {
} if (c == '"') {
throw error("identifier expected"); pos++;
} return parseString(c);
int start = pos; }
while (hasNext() && is_identifier_part(source[pos])) { throw error("identifier expected");
pos++; }
} int start = pos;
return source.substr(start, pos-start); while (hasNext() && is_identifier_part(source[pos])) {
} pos++;
}
int64_t BasicParser::parseSimpleInt(int base) { return source.substr(start, pos-start);
char c = peek(); }
int index = hexchar2int(c);
if (index == -1 || index >= base) { int64_t BasicParser::parseSimpleInt(int base) {
throw error("invalid number literal"); char c = peek();
} int index = hexchar2int(c);
int64_t value = index; if (index == -1 || index >= base) {
pos++; throw error("invalid number literal");
while (hasNext()) { }
c = source[pos]; int64_t value = index;
while (c == '_') { pos++;
c = source[++pos]; while (hasNext()) {
} c = source[pos];
index = hexchar2int(c); while (c == '_') {
if (index == -1 || index >= base) { c = source[++pos];
return value; }
} index = hexchar2int(c);
value *= base; if (index == -1 || index >= base) {
value += index; return value;
pos++; }
} value *= base;
return value; value += index;
} pos++;
}
bool BasicParser::parseNumber(int sign, number_u& out) { return value;
char c = peek(); }
int base = 10;
if (c == '0' && pos + 1 < source.length() && bool BasicParser::parseNumber(int sign, number_u& out) {
(base = is_box(source[pos+1])) != 10) { char c = peek();
pos += 2; int base = 10;
out = parseSimpleInt(base); if (c == '0' && pos + 1 < source.length() &&
return true; (base = is_box(source[pos+1])) != 10) {
} else if (c == 'i' && pos + 2 < source.length() && source[pos+1] == 'n' && source[pos+2] == 'f') { pos += 2;
pos += 3; out = parseSimpleInt(base);
out = INFINITY * sign; return true;
return false; } else if (c == 'i' && pos + 2 < source.length() && source[pos+1] == 'n' && source[pos+2] == 'f') {
} else if (c == 'n' && pos + 2 < source.length() && source[pos+1] == 'a' && source[pos+2] == 'n') { pos += 3;
pos += 3; out = INFINITY * sign;
out = NAN * sign; return false;
return false; } else if (c == 'n' && pos + 2 < source.length() && source[pos+1] == 'a' && source[pos+2] == 'n') {
} pos += 3;
int64_t value = parseSimpleInt(base); out = NAN * sign;
if (!hasNext()) { return false;
out = value * sign; }
return true; int64_t value = parseSimpleInt(base);
} if (!hasNext()) {
c = source[pos]; out = value * sign;
if (c == 'e' || c == 'E') { return true;
pos++; }
int s = 1; c = source[pos];
if (peek() == '-') { if (c == 'e' || c == 'E') {
s = -1; pos++;
pos++; int s = 1;
} else if (peek() == '+'){ if (peek() == '-') {
pos++; s = -1;
} pos++;
out = sign * value * power(10.0, s * parseSimpleInt(10)); } else if (peek() == '+'){
return false; pos++;
} }
if (c == '.') { out = sign * value * power(10.0, s * parseSimpleInt(10));
pos++; return false;
int64_t expo = 1; }
while (hasNext() && source[pos] == '0') { if (c == '.') {
expo *= 10; pos++;
pos++; int64_t expo = 1;
} while (hasNext() && source[pos] == '0') {
int64_t afterdot = 0; expo *= 10;
if (hasNext() && is_digit(source[pos])) { pos++;
afterdot = parseSimpleInt(10); }
} int64_t afterdot = 0;
expo *= power(10, fmax(0, log10(afterdot) + 1)); if (hasNext() && is_digit(source[pos])) {
c = source[pos]; afterdot = parseSimpleInt(10);
}
double dvalue = (value + (afterdot / (double)expo)); expo *= power(10, fmax(0, log10(afterdot) + 1));
if (c == 'e' || c == 'E') { c = source[pos];
pos++;
int s = 1; double dvalue = (value + (afterdot / (double)expo));
if (peek() == '-') { if (c == 'e' || c == 'E') {
s = -1; pos++;
pos++; int s = 1;
} else if (peek() == '+'){ if (peek() == '-') {
pos++; s = -1;
} pos++;
out = sign * dvalue * power(10.0, s * parseSimpleInt(10)); } else if (peek() == '+'){
return false; pos++;
} }
out = sign * dvalue; out = sign * dvalue * power(10.0, s * parseSimpleInt(10));
return false; return false;
} }
out = sign * value; out = sign * dvalue;
return true; return false;
} }
out = sign * value;
std::string BasicParser::parseString(char quote, bool closeRequired) { return true;
std::stringstream ss; }
while (hasNext()) {
char c = source[pos]; std::string BasicParser::parseString(char quote, bool closeRequired) {
if (c == quote) { std::stringstream ss;
pos++; while (hasNext()) {
return ss.str(); char c = source[pos];
} if (c == quote) {
if (c == '\\') { pos++;
pos++; return ss.str();
c = nextChar(); }
if (c >= '0' && c <= '7') { if (c == '\\') {
pos--; pos++;
ss << (char)parseSimpleInt(8); c = nextChar();
continue; if (c >= '0' && c <= '7') {
} pos--;
switch (c) { ss << (char)parseSimpleInt(8);
case 'n': ss << '\n'; break; continue;
case 'r': ss << '\r'; break; }
case 'b': ss << '\b'; break; switch (c) {
case 't': ss << '\t'; break; case 'n': ss << '\n'; break;
case 'f': ss << '\f'; break; case 'r': ss << '\r'; break;
case '\'': ss << '\\'; break; case 'b': ss << '\b'; break;
case '"': ss << '"'; break; case 't': ss << '\t'; break;
case '\\': ss << '\\'; break; case 'f': ss << '\f'; break;
case '/': ss << '/'; break; case '\'': ss << '\\'; break;
case '\n': pos++; continue; case '"': ss << '"'; break;
default: case '\\': ss << '\\'; break;
throw error("'\\" + std::string({c}) + case '/': ss << '/'; break;
"' is an illegal escape"); case '\n': pos++; continue;
} default:
continue; throw error("'\\" + std::string({c}) +
} "' is an illegal escape");
if (c == '\n' && closeRequired) { }
throw error("non-closed string literal"); continue;
} }
ss << c; if (c == '\n' && closeRequired) {
pos++; throw error("non-closed string literal");
} }
if (closeRequired) { ss << c;
throw error("unexpected end"); pos++;
} }
return ss.str(); if (closeRequired) {
} throw error("unexpected end");
}
parsing_error BasicParser::error(std::string message) { return ss.str();
return parsing_error(message, filename, source, pos, line, linestart); }
}
parsing_error BasicParser::error(std::string message) {
return parsing_error(message, filename, source, pos, line, linestart);
}

View File

@ -70,8 +70,8 @@ public:
class BasicParser { class BasicParser {
protected: protected:
std::string filename; const std::string& filename;
std::string source; const std::string& source;
uint pos = 0; uint pos = 0;
uint line = 1; uint line = 1;
uint linestart = 0; uint linestart = 0;
@ -96,7 +96,7 @@ protected:
parsing_error error(std::string message); parsing_error error(std::string message);
BasicParser(std::string filename, std::string source); BasicParser(const std::string& file, const std::string& source);
}; };
#endif // CODERS_COMMONS_H_ #endif // CODERS_COMMONS_H_

View File

@ -118,8 +118,8 @@ std::string json::stringify(
return ss.str(); return ss.str();
} }
Parser::Parser(std::string filename, std::string source) Parser::Parser(const std::string& filename, const std::string& source)
: BasicParser(filename, source) { : BasicParser(filename, source) {
} }
Map* Parser::parse() { Map* Parser::parse() {
@ -244,11 +244,11 @@ Value* Parser::parseValue() {
throw error("unexpected character '"+std::string({next})+"'"); throw error("unexpected character '"+std::string({next})+"'");
} }
std::unique_ptr<Map> json::parse(std::string filename, std::string source) { std::unique_ptr<Map> json::parse(const std::string& filename, const std::string& source) {
Parser parser(filename, source); Parser parser(filename, source);
return std::unique_ptr<Map>(parser.parse()); return std::unique_ptr<Map>(parser.parse());
} }
std::unique_ptr<Map> json::parse(std::string source) { std::unique_ptr<Map> json::parse(const std::string& source) {
return parse("<string>", source); return parse("<string>", source);
} }

View File

@ -24,13 +24,13 @@ namespace json {
dynamic::Map* parseObject(); dynamic::Map* parseObject();
dynamic::Value* parseValue(); dynamic::Value* parseValue();
public: public:
Parser(std::string filename, std::string source); Parser(const std::string& filename, const std::string& source);
dynamic::Map* parse(); dynamic::Map* parse();
}; };
extern std::unique_ptr<dynamic::Map> parse(std::string filename, std::string source); extern std::unique_ptr<dynamic::Map> parse(const std::string& filename, const std::string& source);
extern std::unique_ptr<dynamic::Map> parse(std::string source); extern std::unique_ptr<dynamic::Map> parse(const std::string& source);
extern std::string stringify( extern std::string stringify(
const dynamic::Map* obj, const dynamic::Map* obj,
@ -38,4 +38,4 @@ namespace json {
const std::string& indent); const std::string& indent);
} }
#endif // CODERS_JSON_H_ #endif // CODERS_JSON_H_

View File

@ -1,261 +1,127 @@
#include "toml.h" #include "toml.h"
#include "commons.h" #include "commons.h"
#include "../util/stringutil.h" #include "../data/dynamic.h"
#include "../util/stringutil.h"
#include <math.h> #include "../files/settings_io.hpp"
#include <iostream>
#include <iomanip> #include <math.h>
#include <sstream> #include <iostream>
#include <assert.h> #include <iomanip>
#include <sstream>
// FIXME: refactor this monster #include <assert.h>
using namespace toml; // FIXME: refactor this monster
Section::Section(std::string name) : name(name) { using namespace toml;
}
class Reader : public BasicParser {
void Section::add(std::string name, Field field) { SettingsHandler& handler;
if (fields.find(name) != fields.end()) {
throw std::runtime_error("field duplication"); void skipWhitespace() override {
} BasicParser::skipWhitespace();
fields[name] = field; if (hasNext() && source[pos] == '#') {
keyOrder.push_back(name); skipLine();
} if (hasNext() && is_whitespace(peek())) {
skipWhitespace();
void Section::add(std::string name, bool* ptr) { }
add(name, {fieldtype::ftbool, ptr}); }
} }
void readSection(const std::string& section) {
void Section::add(std::string name, int* ptr) { while (hasNext()) {
add(name, {fieldtype::ftint, ptr}); skipWhitespace();
} if (!hasNext()) {
break;
void Section::add(std::string name, uint* ptr) { }
add(name, {fieldtype::ftuint, ptr}); char c = nextChar();
} if (c == '[') {
std::string name = parseName();
void Section::add(std::string name, int64_t* ptr) { pos++;
add(name, {fieldtype::ftint64, ptr}); readSection(name);
} return;
}
void Section::add(std::string name, float* ptr) { pos--;
add(name, {fieldtype::ftfloat, ptr}); std::string name = section+"."+parseName();
} expect('=');
c = peek();
void Section::add(std::string name, double* ptr) { if (is_digit(c)) {
add(name, {fieldtype::ftdouble, ptr}); number_u num;
} parseNumber(1, num);
handler.setValue(name, *dynamic::Value::of(num));
void Section::add(std::string name, std::string* ptr) { } else if (c == '-' || c == '+') {
add(name, {fieldtype::ftstring, ptr}); int sign = c == '-' ? -1 : 1;
} pos++;
number_u num;
const std::string& Section::getName() const { parseNumber(sign, num);
return name; handler.setValue(name, *dynamic::Value::of(num));
} } else if (is_identifier_start(c)) {
std::string identifier = parseName();
const Field* Section::field(const std::string& name) const { if (identifier == "true" || identifier == "false") {
auto found = fields.find(name); bool flag = identifier == "true";
if (found == fields.end()) { handler.setValue(name, *dynamic::Value::boolean(flag));
return nullptr; } else if (identifier == "inf") {
} handler.setValue(name, *dynamic::Value::of(INFINITY));
return &found->second; } else if (identifier == "nan") {
} handler.setValue(name, *dynamic::Value::of(NAN));
}
const std::vector<std::string>& Section::keys() const { } else if (c == '"' || c == '\'') {
return keyOrder; pos++;
} std::string str = parseString(c);
handler.setValue(name, *dynamic::Value::of(str));
Wrapper::~Wrapper() { } else {
for (auto entry : sections) { throw error("feature is not supported");
delete entry.second; }
} expectNewLine();
} }
}
Section& Wrapper::add(std::string name) {
if (sections.find(name) != sections.end()) { public:
throw std::runtime_error("section duplication"); Reader(
} SettingsHandler& handler,
Section* section = new Section(name); const std::string& file,
sections[name] = section; const std::string& source)
keyOrder.push_back(name); : BasicParser(file, source), handler(handler) {
return *section; }
}
void read() {
Section* Wrapper::section(std::string name) { skipWhitespace();
auto found = sections.find(name); if (!hasNext()) {
if (found == sections.end()) { return;
return nullptr; }
} readSection("");
return found->second; }
} };
std::string Wrapper::write() const { void toml::parse(
std::stringstream ss; SettingsHandler& handler,
for (const std::string& key : keyOrder) { const std::string& file,
const Section* section = sections.at(key); const std::string& source
ss << "[" << key << "]\n"; ) {
for (const std::string& key : section->keys()) { Reader reader(handler, file, source);
ss << key << " = "; reader.read();
const Field* field = section->field(key); }
assert(field != nullptr);
switch (field->type) { std::string toml::stringify(SettingsHandler& handler) {
case fieldtype::ftbool: auto& sections = handler.getSections();
ss << (*((bool*)field->ptr) ? "true" : "false");
break; std::stringstream ss;
case fieldtype::ftint: ss << *((int*)field->ptr); break; for (auto& section : sections) {
case fieldtype::ftuint: ss << *((uint*)field->ptr); break; ss << "[" << section.name << "]\n";
case fieldtype::ftint64: ss << *((int64_t*)field->ptr); break; for (const std::string& key : section.keys) {
case fieldtype::ftfloat: ss << *((float*)field->ptr); break; ss << key << " = ";
case fieldtype::ftdouble: ss << *((double*)field->ptr); break; auto setting = handler.getSetting(section.name+"."+key);
case fieldtype::ftstring: assert(setting != nullptr);
ss << util::escape(*((const std::string*)field->ptr)); if (auto integer = dynamic_cast<IntegerSetting*>(setting)) {
break; ss << integer->get();
} } else if (auto number = dynamic_cast<NumberSetting*>(setting)) {
ss << "\n"; ss << number->get();
} } else if (auto flag = dynamic_cast<FlagSetting*>(setting)) {
ss << "\n"; ss << (flag->get() ? "true" : "false");
} } else if (auto string = dynamic_cast<StringSetting*>(setting)) {
return ss.str(); ss << util::escape(string->get());
} }
ss << "\n";
Reader::Reader(Wrapper* wrapper, std::string file, std::string source) }
: BasicParser(file, source), wrapper(wrapper) { ss << "\n";
} }
return ss.str();
void Reader::skipWhitespace() { }
BasicParser::skipWhitespace();
if (hasNext() && source[pos] == '#') {
skipLine();
if (hasNext() && is_whitespace(peek())) {
skipWhitespace();
}
}
}
void Reader::read() {
skipWhitespace();
if (!hasNext()) {
return;
}
readSection(nullptr);
}
void Section::set(const std::string& name, double value) {
const Field* field = this->field(name);
if (field == nullptr) {
std::cerr << "warning: unknown key '" << name << "'" << std::endl;
} else {
switch (field->type) {
case fieldtype::ftbool: *(bool*)(field->ptr) = fabs(value) > 0.0; break;
case fieldtype::ftint: *(int*)(field->ptr) = value; break;
case fieldtype::ftuint: *(uint*)(field->ptr) = value; break;
case fieldtype::ftint64: *(int64_t*)(field->ptr) = value; break;
case fieldtype::ftfloat: *(float*)(field->ptr) = value; break;
case fieldtype::ftdouble: *(double*)(field->ptr) = value; break;
case fieldtype::ftstring: *(std::string*)(field->ptr) = std::to_string(value); break;
default:
std::cerr << "error: type error for key '" << name << "'" << std::endl;
}
}
}
void Section::set(const std::string& name, bool value) {
const Field* field = this->field(name);
if (field == nullptr) {
std::cerr << "warning: unknown key '" << name << "'" << std::endl;
} else {
switch (field->type) {
case fieldtype::ftbool: *(bool*)(field->ptr) = value; break;
case fieldtype::ftint: *(int*)(field->ptr) = (int)value; break;
case fieldtype::ftuint: *(uint*)(field->ptr) = (uint)value; break;
case fieldtype::ftint64: *(int64_t*)(field->ptr) = (int64_t)value; break;
case fieldtype::ftfloat: *(float*)(field->ptr) = (float)value; break;
case fieldtype::ftdouble: *(double*)(field->ptr) = (double)value; break;
case fieldtype::ftstring: *(std::string*)(field->ptr) = value ? "true" : "false"; break;
default:
std::cerr << "error: type error for key '" << name << "'" << std::endl;
}
}
}
void Section::set(const std::string& name, std::string value) {
const Field* field = this->field(name);
if (field == nullptr) {
std::cerr << "warning: unknown key '" << name << "'" << std::endl;
} else {
switch (field->type) {
case fieldtype::ftstring: *(std::string*)(field->ptr) = value; break;
default:
std::cerr << "error: type error for key '" << name << "'" << std::endl;
}
}
}
void Reader::readSection(Section* section /*nullable*/) {
while (hasNext()) {
skipWhitespace();
if (!hasNext()) {
break;
}
char c = nextChar();
if (c == '[') {
std::string name = parseName();
Section* section = wrapper->section(name);
pos++;
readSection(section);
return;
}
pos--;
std::string name = parseName();
expect('=');
c = peek();
if (is_digit(c)) {
number_u num;
if (parseNumber(1, num)) {
if (section)
section->set(name, (double)std::get<integer_t>(num));
} else {
if (section)
section->set(name, std::get<number_t>(num));
}
} else if (c == '-' || c == '+') {
int sign = c == '-' ? -1 : 1;
pos++;
number_u num;
if (parseNumber(sign, num)) {
if (section)
section->set(name, (double)std::get<integer_t>(num));
} else {
if (section)
section->set(name, std::get<number_t>(num));
}
} else if (is_identifier_start(c)) {
std::string identifier = parseName();
if (identifier == "true" || identifier == "false") {
bool flag = identifier == "true";
if (section) {
section->set(name, flag);
}
} else if (identifier == "inf") {
if (section) {
section->set(name, INFINITY);
}
} else if (identifier == "nan") {
if (section) {
section->set(name, NAN);
}
}
} else if (c == '"' || c == '\'') {
pos++;
std::string str = parseString(c);
if (section) {
section->set(name, str);
}
} else {
throw error("feature is not supported");
}
expectNewLine();
}
}

View File

@ -1,72 +1,22 @@
#ifndef CODERS_TOML_H_ #ifndef CODERS_TOML_H_
#define CODERS_TOML_H_ #define CODERS_TOML_H_
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "commons.h" #include "commons.h"
namespace toml { class SettingsHandler;
enum class fieldtype {
ftbool, namespace toml {
ftint, std::string stringify(SettingsHandler& handler);
ftuint,
ftint64, void parse(
ftfloat, SettingsHandler& handler,
ftdouble, const std::string& file,
ftstring, const std::string& source
}; );
}
struct Field {
fieldtype type; #endif // CODERS_TOML_H_
void* ptr;
};
class Section {
std::unordered_map<std::string, Field> fields;
std::vector<std::string> keyOrder;
std::string name;
void add(std::string name, Field field);
public:
Section(std::string name);
void add(std::string name, bool* ptr);
void add(std::string name, int* ptr);
void add(std::string name, int64_t* ptr);
void add(std::string name, uint* ptr);
void add(std::string name, float* ptr);
void add(std::string name, double* ptr);
void add(std::string name, std::string* ptr);
const Field* field(const std::string& name) const;
void set(const std::string& name, double value);
void set(const std::string& name, bool value);
void set(const std::string& name, std::string value);
const std::string& getName() const;
const std::vector<std::string>& keys() const;
};
class Wrapper {
std::unordered_map<std::string, Section*> sections;
std::vector<std::string> keyOrder;
public:
~Wrapper();
Section& add(std::string section);
Section* section(std::string name);
std::string write() const;
};
class Reader : public BasicParser {
Wrapper* wrapper;
void skipWhitespace() override;
void readSection(Section* section);
public:
Reader(Wrapper* wrapper, std::string file, std::string source);
void read();
};
}
#endif // CODERS_TOML_H_

View File

@ -176,7 +176,7 @@ const std::string& Document::getEncoding() const {
return encoding; return encoding;
} }
Parser::Parser(std::string filename, std::string source) Parser::Parser(const std::string& filename, const std::string& source)
: BasicParser(filename, source) { : BasicParser(filename, source) {
} }

View File

@ -118,7 +118,7 @@ namespace xml {
std::string parseText(); std::string parseText();
std::string parseXMLName(); std::string parseXMLName();
public: public:
Parser(std::string filename, std::string source); Parser(const std::string& filename, const std::string& source);
xmldocument parse(); xmldocument parse();
}; };

View File

@ -11,6 +11,7 @@
#include "content/ContentLoader.h" #include "content/ContentLoader.h"
#include "core_defs.h" #include "core_defs.h"
#include "files/files.h" #include "files/files.h"
#include "files/settings_io.hpp"
#include "frontend/locale/langs.h" #include "frontend/locale/langs.h"
#include "frontend/menu.hpp" #include "frontend/menu.hpp"
#include "frontend/screens/Screen.hpp" #include "frontend/screens/Screen.hpp"
@ -56,14 +57,14 @@ inline void create_channel(Engine* engine, std::string name, NumberSetting& sett
})); }));
} }
Engine::Engine(EngineSettings& settings, EnginePaths* paths) Engine::Engine(EngineSettings& settings, SettingsHandler& settingsHandler, EnginePaths* paths)
: settings(settings), settingsHandler(settings), paths(paths) : settings(settings), settingsHandler(settingsHandler), paths(paths)
{ {
controller = std::make_unique<EngineController>(this); controller = std::make_unique<EngineController>(this);
if (Window::initialize(&this->settings.display)){ if (Window::initialize(&this->settings.display)){
throw initialize_error("could not initialize window"); throw initialize_error("could not initialize window");
} }
audio::initialize(settings.audio.enabled); audio::initialize(settings.audio.enabled.get());
create_channel(this, "master", settings.audio.volumeMaster); create_channel(this, "master", settings.audio.volumeMaster);
create_channel(this, "regular", settings.audio.volumeRegular); create_channel(this, "regular", settings.audio.volumeRegular);
create_channel(this, "music", settings.audio.volumeMusic); create_channel(this, "music", settings.audio.volumeMusic);

View File

@ -10,7 +10,6 @@
#include "content/ContentPack.h" #include "content/ContentPack.h"
#include "content/PacksManager.hpp" #include "content/PacksManager.hpp"
#include "files/engine_paths.h" #include "files/engine_paths.h"
#include "files/settings_io.h"
#include "util/ObjectsKeeper.hpp" #include "util/ObjectsKeeper.hpp"
#include <filesystem> #include <filesystem>
@ -27,6 +26,7 @@ class EnginePaths;
class ResPaths; class ResPaths;
class Batch2D; class Batch2D;
class EngineController; class EngineController;
class SettingsHandler;
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -41,7 +41,7 @@ public:
class Engine : public util::ObjectsKeeper { class Engine : public util::ObjectsKeeper {
EngineSettings& settings; EngineSettings& settings;
SettingsHandler settingsHandler; SettingsHandler& settingsHandler;
EnginePaths* paths; EnginePaths* paths;
std::unique_ptr<Assets> assets = nullptr; std::unique_ptr<Assets> assets = nullptr;
@ -65,7 +65,7 @@ class Engine : public util::ObjectsKeeper {
void processPostRunnables(); void processPostRunnables();
void loadAssets(); void loadAssets();
public: public:
Engine(EngineSettings& settings, EnginePaths* paths); Engine(EngineSettings& settings, SettingsHandler& settingsHandler, EnginePaths* paths);
~Engine(); ~Engine();
/// @brief Start main engine input/update/render loop. /// @brief Start main engine input/update/render loop.

View File

@ -35,8 +35,8 @@ WorldFiles::WorldFiles(fs::path directory) : directory(directory), regions(direc
WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings) WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings)
: WorldFiles(directory) : WorldFiles(directory)
{ {
generatorTestMode = settings.generatorTestMode; generatorTestMode = settings.generatorTestMode.get();
doWriteLights = settings.doWriteLights; doWriteLights = settings.doWriteLights.get();
regions.generatorTestMode = generatorTestMode; regions.generatorTestMode = generatorTestMode;
regions.doWriteLights = doWriteLights; regions.doWriteLights = doWriteLights;
} }

View File

@ -1,4 +1,4 @@
#include "settings_io.h" #include "settings_io.hpp"
#include "../window/Events.h" #include "../window/Events.h"
#include "../window/input.h" #include "../window/input.h"
@ -10,30 +10,70 @@
static debug::Logger logger("settings_io"); static debug::Logger logger("settings_io");
struct SectionsBuilder {
std::unordered_map<std::string, Setting*>& map;
std::vector<Section>& sections;
SectionsBuilder(
std::unordered_map<std::string, Setting*>& map,
std::vector<Section>& sections
) : map(map), sections(sections) {
}
void section(std::string name) {
sections.push_back(Section {name, {}});
}
void add(std::string name, Setting* setting, bool writeable=true) {
Section& section = sections.at(sections.size()-1);
map[section.name+"."+name] = setting;
section.keys.push_back(name);
}
};
SettingsHandler::SettingsHandler(EngineSettings& settings) { SettingsHandler::SettingsHandler(EngineSettings& settings) {
// public settings SectionsBuilder builder(map, sections);
map.emplace("audio.volume-master", &settings.audio.volumeMaster);
map.emplace("audio.volume-regular", &settings.audio.volumeRegular);
map.emplace("audio.volume-ui", &settings.audio.volumeUI);
map.emplace("audio.volume-ambient", &settings.audio.volumeAmbient);
map.emplace("audio.volume-music", &settings.audio.volumeMusic);
map.emplace("display.vsync", &settings.display.vsync); builder.section("audio");
map.emplace("display.fullscreen", &settings.display.fullscreen); builder.add("enabled", &settings.audio.enabled, false);
builder.add("volume-master", &settings.audio.volumeMaster);
builder.add("volume-regular", &settings.audio.volumeRegular);
builder.add("volume-ui", &settings.audio.volumeUI);
builder.add("volume-ambient", &settings.audio.volumeAmbient);
builder.add("volume-music", &settings.audio.volumeMusic);
map.emplace("camera.sensitivity", &settings.camera.sensitivity); builder.section("display");
map.emplace("camera.fov", &settings.camera.fov); builder.add("width", &settings.display.width);
map.emplace("camera.fov-effects", &settings.camera.fovEffects); builder.add("height", &settings.display.height);
map.emplace("camera.shaking", &settings.camera.shaking); builder.add("samples", &settings.display.samples);
builder.add("vsync", &settings.display.vsync);
builder.add("fullscreen", &settings.display.fullscreen);
map.emplace("chunks.load-distance", &settings.chunks.loadDistance); builder.section("camera");
map.emplace("chunks.load-speed", &settings.chunks.loadSpeed); builder.add("sensitivity", &settings.camera.sensitivity);
builder.add("fov", &settings.camera.fov);
builder.add("fov-effects", &settings.camera.fovEffects);
builder.add("shaking", &settings.camera.shaking);
map.emplace("graphics.fog-curve", &settings.graphics.fogCurve); builder.section("chunks");
map.emplace("graphics.backlight", &settings.graphics.backlight); builder.add("load-distance", &settings.chunks.loadDistance);
map.emplace("graphics.gamma", &settings.graphics.gamma); builder.add("load-speed", &settings.chunks.loadSpeed);
builder.add("padding", &settings.chunks.padding);
map.emplace("ui.language", &settings.ui.language); builder.section("graphics");
builder.add("fog-curve", &settings.graphics.fogCurve);
builder.add("backlight", &settings.graphics.backlight);
builder.add("gamma", &settings.graphics.gamma);
builder.add("frustum-culling", &settings.graphics.frustumCulling);
builder.add("skybox-resolution", &settings.graphics.skyboxResolution);
builder.section("ui");
builder.add("language", &settings.ui.language);
builder.add("world-preview-size", &settings.ui.worldPreviewSize);
builder.section("debug");
builder.add("generator-test-mode", &settings.debug.generatorTestMode);
builder.add("do-write-lights", &settings.debug.doWriteLights);
} }
std::unique_ptr<dynamic::Value> SettingsHandler::getValue(const std::string& name) const { std::unique_ptr<dynamic::Value> SettingsHandler::getValue(const std::string& name) const {
@ -123,51 +163,8 @@ void SettingsHandler::setValue(const std::string& name, const dynamic::Value& va
} }
} }
toml::Wrapper* create_wrapper(EngineSettings& settings) { std::vector<Section>& SettingsHandler::getSections() {
auto wrapper = std::make_unique<toml::Wrapper>(); return sections;
toml::Section& audio = wrapper->add("audio");
audio.add("enabled", &settings.audio.enabled);
audio.add("volume-master", &*settings.audio.volumeMaster);
audio.add("volume-regular", &*settings.audio.volumeRegular);
audio.add("volume-ui", &*settings.audio.volumeUI);
audio.add("volume-ambient", &*settings.audio.volumeAmbient);
audio.add("volume-music", &*settings.audio.volumeMusic);
toml::Section& display = wrapper->add("display");
display.add("fullscreen", &*settings.display.fullscreen);
display.add("width", &settings.display.width);
display.add("height", &settings.display.height);
display.add("samples", &settings.display.samples);
display.add("vsync", &*settings.display.vsync);
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.fovEffects);
camera.add("fov", &*settings.camera.fov);
camera.add("shaking", &*settings.camera.shaking);
camera.add("sensitivity", &*settings.camera.sensitivity);
toml::Section& graphics = wrapper->add("graphics");
graphics.add("gamma", &*settings.graphics.gamma);
graphics.add("fog-curve", &*settings.graphics.fogCurve);
graphics.add("backlight", &*settings.graphics.backlight);
graphics.add("frustum-culling", &*settings.graphics.frustumCulling);
graphics.add("skybox-resolution", &*settings.graphics.skyboxResolution);
toml::Section& debug = wrapper->add("debug");
debug.add("generator-test-mode", &settings.debug.generatorTestMode);
debug.add("show-chunk-borders", &settings.debug.showChunkBorders);
debug.add("do-write-lights", &settings.debug.doWriteLights);
toml::Section& ui = wrapper->add("ui");
ui.add("language", &*settings.ui.language);
ui.add("world-preview-size", &*settings.ui.worldPreviewSize);
return wrapper.release();
} }
std::string write_controls() { std::string write_controls() {

View File

@ -1,18 +1,21 @@
#ifndef FILES_SETTINGS_IO_H_ #ifndef FILES_SETTINGS_IO_HPP_
#define FILES_SETTINGS_IO_H_ #define FILES_SETTINGS_IO_HPP_
#include <string> #include <string>
#include <memory> #include <memory>
#include <vector>
#include <unordered_map> #include <unordered_map>
#include "../settings.h" #include "../settings.h"
#include "../data/dynamic.h" #include "../data/dynamic.h"
namespace toml { struct Section {
class Wrapper; std::string name;
} std::vector<std::string> keys;
};
class SettingsHandler { class SettingsHandler {
std::unordered_map<std::string, Setting*> map; std::unordered_map<std::string, Setting*> map;
std::vector<Section> sections;
public: public:
SettingsHandler(EngineSettings& settings); SettingsHandler(EngineSettings& settings);
@ -20,10 +23,12 @@ public:
void setValue(const std::string& name, const dynamic::Value& value); void setValue(const std::string& name, const dynamic::Value& value);
std::string toString(const std::string& name) const; std::string toString(const std::string& name) const;
Setting* getSetting(const std::string& name) const; Setting* getSetting(const std::string& name) const;
std::vector<Section>& getSections();
}; };
extern std::string write_controls(); std::string write_controls();
extern toml::Wrapper* create_wrapper(EngineSettings& settings);
extern void load_controls(std::string filename, std::string source);
#endif // FILES_SETTINGS_IO_H_ void load_controls(std::string filename, std::string source);
#endif // FILES_SETTINGS_IO_HPP_

View File

@ -146,10 +146,10 @@ std::shared_ptr<UINode> create_debug_panel(
L"Show Chunk Borders", glm::vec2(400, 24) L"Show Chunk Borders", glm::vec2(400, 24)
); );
checkbox->setSupplier([=]() { checkbox->setSupplier([=]() {
return engine->getSettings().debug.showChunkBorders; return WorldRenderer::showChunkBorders;
}); });
checkbox->setConsumer([=](bool checked) { checkbox->setConsumer([=](bool checked) {
engine->getSettings().debug.showChunkBorders = checked; WorldRenderer::showChunkBorders = checked;
}); });
panel->add(checkbox); panel->add(checkbox);
} }

View File

@ -45,7 +45,7 @@ class Reader : public BasicParser {
} }
} }
public: public:
Reader(std::string file, std::string source) : BasicParser(file, source) { Reader(const std::string& file, const std::string& source) : BasicParser(file, source) {
} }
void read(langs::Lang& lang, std::string prefix) { void read(langs::Lang& lang, std::string prefix) {

View File

@ -35,6 +35,8 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
bool WorldRenderer::showChunkBorders = false;
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player) WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player)
: engine(engine), : engine(engine),
level(frontend->getLevel()), level(frontend->getLevel()),
@ -225,7 +227,7 @@ void WorldRenderer::renderDebugLines(
linesShader->use(); linesShader->use();
if (settings.debug.showChunkBorders){ if (showChunkBorders){
linesShader->uniformMatrix("u_projview", camera->getProjView()); linesShader->uniformMatrix("u_projview", camera->getProjView());
glm::vec3 coord = player->camera->position; glm::vec3 coord = player->camera->position;
if (coord.x < 0) coord.x--; if (coord.x < 0) coord.x--;

View File

@ -56,6 +56,8 @@ class WorldRenderer {
const EngineSettings& settings const EngineSettings& settings
); );
public: public:
static bool showChunkBorders;
WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player); WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player);
~WorldRenderer(); ~WorldRenderer();

View File

@ -1,189 +1,190 @@
#include "lua_commons.h" #include "lua_commons.h"
#include "api_lua.h" #include "api_lua.h"
#include "../../../engine.h" #include "../../../engine.h"
#include "../../../files/engine_paths.h" #include "../../../files/settings_io.hpp"
#include "../../../frontend/menu.hpp" #include "../../../files/engine_paths.h"
#include "../../../frontend/screens/MenuScreen.hpp" #include "../../../frontend/menu.hpp"
#include "../../../logic/LevelController.h" #include "../../../frontend/screens/MenuScreen.hpp"
#include "../../../logic/EngineController.hpp" #include "../../../logic/LevelController.h"
#include "../../../window/Events.h" #include "../../../logic/EngineController.hpp"
#include "../../../window/Window.h" #include "../../../window/Events.h"
#include "../../../world/WorldGenerators.h" #include "../../../window/Window.h"
#include "../scripting.h" #include "../../../world/WorldGenerators.h"
#include "../scripting.h"
#include <vector>
#include <memory> #include <vector>
#include <memory>
namespace scripting {
extern lua::LuaState* state; namespace scripting {
} extern lua::LuaState* state;
}
static int l_new_world(lua_State* L) {
auto name = lua_tostring(L, 1); static int l_new_world(lua_State* L) {
auto seed = lua_tostring(L, 2); auto name = lua_tostring(L, 1);
auto generator = lua_tostring(L, 3); auto seed = lua_tostring(L, 2);
auto controller = scripting::engine->getController(); auto generator = lua_tostring(L, 3);
controller->createWorld(name, seed, generator); auto controller = scripting::engine->getController();
return 0; controller->createWorld(name, seed, generator);
} return 0;
}
static int l_open_world(lua_State* L) {
auto name = lua_tostring(L, 1); static int l_open_world(lua_State* L) {
auto name = lua_tostring(L, 1);
auto controller = scripting::engine->getController();
controller->openWorld(name, false); auto controller = scripting::engine->getController();
return 0; controller->openWorld(name, false);
} return 0;
}
static int l_close_world(lua_State* L) {
if (scripting::controller == nullptr) { static int l_close_world(lua_State* L) {
luaL_error(L, "no world open"); if (scripting::controller == nullptr) {
} luaL_error(L, "no world open");
bool save_world = lua_toboolean(L, 1); }
if (save_world) { bool save_world = lua_toboolean(L, 1);
scripting::controller->saveWorld(); if (save_world) {
} scripting::controller->saveWorld();
// destroy LevelScreen and run quit callbacks }
scripting::engine->setScreen(nullptr); // destroy LevelScreen and run quit callbacks
// create and go to menu screen scripting::engine->setScreen(nullptr);
scripting::engine->setScreen(std::make_shared<MenuScreen>(scripting::engine)); // create and go to menu screen
return 0; scripting::engine->setScreen(std::make_shared<MenuScreen>(scripting::engine));
} return 0;
}
static int l_delete_world(lua_State* L) {
auto name = lua_tostring(L, 1); static int l_delete_world(lua_State* L) {
auto controller = scripting::engine->getController(); auto name = lua_tostring(L, 1);
controller->deleteWorld(name); auto controller = scripting::engine->getController();
return 0; controller->deleteWorld(name);
} return 0;
}
static int l_remove_packs(lua_State* L) {
if (!lua_istable(L, 1)) { static int l_remove_packs(lua_State* L) {
luaL_error(L, "strings array expected as an argument"); if (!lua_istable(L, 1)) {
} luaL_error(L, "strings array expected as an argument");
std::vector<std::string> packs; }
int len = lua_objlen(L, 1); std::vector<std::string> packs;
for (int i = 0; i < len; i++) { int len = lua_objlen(L, 1);
lua_rawgeti(L, -1, i+1); for (int i = 0; i < len; i++) {
packs.push_back(lua_tostring(L, -1)); lua_rawgeti(L, -1, i+1);
lua_pop(L, 1); packs.push_back(lua_tostring(L, -1));
} lua_pop(L, 1);
auto controller = scripting::engine->getController(); }
controller->removePacks(scripting::controller, packs); auto controller = scripting::engine->getController();
return 0; controller->removePacks(scripting::controller, packs);
} return 0;
}
static int l_add_packs(lua_State* L) {
if (!lua_istable(L, 1)) { static int l_add_packs(lua_State* L) {
luaL_error(L, "strings array expected as an argument"); if (!lua_istable(L, 1)) {
} luaL_error(L, "strings array expected as an argument");
std::vector<std::string> packs; }
int len = lua_objlen(L, 1); std::vector<std::string> packs;
for (int i = 0; i < len; i++) { int len = lua_objlen(L, 1);
lua_rawgeti(L, -1, i+1); for (int i = 0; i < len; i++) {
packs.push_back(lua_tostring(L, -1)); lua_rawgeti(L, -1, i+1);
lua_pop(L, 1); packs.push_back(lua_tostring(L, -1));
} lua_pop(L, 1);
auto controller = scripting::engine->getController(); }
controller->addPacks(scripting::controller, packs); auto controller = scripting::engine->getController();
return 0; controller->addPacks(scripting::controller, packs);
} return 0;
}
static int l_get_bindings(lua_State* L) {
auto& bindings = Events::bindings; static int l_get_bindings(lua_State* L) {
lua_createtable(L, bindings.size(), 0); auto& bindings = Events::bindings;
lua_createtable(L, bindings.size(), 0);
int i = 0;
for (auto& entry : bindings) { int i = 0;
lua_pushstring(L, entry.first.c_str()); for (auto& entry : bindings) {
lua_rawseti(L, -2, i + 1); lua_pushstring(L, entry.first.c_str());
i++; lua_rawseti(L, -2, i + 1);
} i++;
return 1; }
} return 1;
}
static int l_get_setting(lua_State* L) {
auto name = lua_tostring(L, 1); static int l_get_setting(lua_State* L) {
const auto value = scripting::engine->getSettingsHandler().getValue(name); auto name = lua_tostring(L, 1);
scripting::state->pushvalue(*value); const auto value = scripting::engine->getSettingsHandler().getValue(name);
return 1; scripting::state->pushvalue(*value);
} return 1;
}
static int l_set_setting(lua_State* L) {
auto name = lua_tostring(L, 1); static int l_set_setting(lua_State* L) {
const auto value = scripting::state->tovalue(2); auto name = lua_tostring(L, 1);
scripting::engine->getSettingsHandler().setValue(name, *value); const auto value = scripting::state->tovalue(2);
return 0; scripting::engine->getSettingsHandler().setValue(name, *value);
} return 0;
}
static int l_str_setting(lua_State* L) {
auto name = lua_tostring(L, 1); static int l_str_setting(lua_State* L) {
const auto string = scripting::engine->getSettingsHandler().toString(name); auto name = lua_tostring(L, 1);
scripting::state->pushstring(string); const auto string = scripting::engine->getSettingsHandler().toString(name);
return 1; scripting::state->pushstring(string);
} return 1;
}
static int l_get_setting_info(lua_State* L) {
auto name = lua_tostring(L, 1); static int l_get_setting_info(lua_State* L) {
auto setting = scripting::engine->getSettingsHandler().getSetting(name); auto name = lua_tostring(L, 1);
lua_createtable(L, 0, 1); auto setting = scripting::engine->getSettingsHandler().getSetting(name);
if (auto number = dynamic_cast<NumberSetting*>(setting)) { lua_createtable(L, 0, 1);
lua_pushnumber(L, number->getMin()); if (auto number = dynamic_cast<NumberSetting*>(setting)) {
lua_setfield(L, -2, "min"); lua_pushnumber(L, number->getMin());
lua_pushnumber(L, number->getMax()); lua_setfield(L, -2, "min");
lua_setfield(L, -2, "max"); lua_pushnumber(L, number->getMax());
return 1; lua_setfield(L, -2, "max");
} return 1;
if (auto integer = dynamic_cast<IntegerSetting*>(setting)) { }
lua_pushinteger(L, integer->getMin()); if (auto integer = dynamic_cast<IntegerSetting*>(setting)) {
lua_setfield(L, -2, "min"); lua_pushinteger(L, integer->getMin());
lua_pushinteger(L, integer->getMax()); lua_setfield(L, -2, "min");
lua_setfield(L, -2, "max"); lua_pushinteger(L, integer->getMax());
return 1; lua_setfield(L, -2, "max");
} return 1;
lua_pop(L, 1); }
luaL_error(L, "unsupported setting type"); lua_pop(L, 1);
return 0; luaL_error(L, "unsupported setting type");
} return 0;
}
static int l_quit(lua_State* L) {
Window::setShouldClose(true); static int l_quit(lua_State* L) {
return 0; Window::setShouldClose(true);
} return 0;
}
static int l_get_default_generator(lua_State* L) {
lua_pushstring(L, WorldGenerators::getDefaultGeneratorID().c_str()); static int l_get_default_generator(lua_State* L) {
return 1; lua_pushstring(L, WorldGenerators::getDefaultGeneratorID().c_str());
} return 1;
}
static int l_get_generators(lua_State* L) {
const auto& generators = WorldGenerators::getGeneratorsIDs(); static int l_get_generators(lua_State* L) {
lua_createtable(L, generators.size(), 0); const auto& generators = WorldGenerators::getGeneratorsIDs();
lua_createtable(L, generators.size(), 0);
int i = 0;
for (auto& id : generators) { int i = 0;
lua_pushstring(L, id.c_str()); for (auto& id : generators) {
lua_rawseti(L, -2, i + 1); lua_pushstring(L, id.c_str());
i++; lua_rawseti(L, -2, i + 1);
} i++;
return 1; }
} return 1;
}
const luaL_Reg corelib [] = {
{"new_world", lua_wrap_errors<l_new_world>}, const luaL_Reg corelib [] = {
{"open_world", lua_wrap_errors<l_open_world>}, {"new_world", lua_wrap_errors<l_new_world>},
{"close_world", lua_wrap_errors<l_close_world>}, {"open_world", lua_wrap_errors<l_open_world>},
{"delete_world", lua_wrap_errors<l_delete_world>}, {"close_world", lua_wrap_errors<l_close_world>},
{"add_packs", lua_wrap_errors<l_add_packs>}, {"delete_world", lua_wrap_errors<l_delete_world>},
{"remove_packs", lua_wrap_errors<l_remove_packs>}, {"add_packs", lua_wrap_errors<l_add_packs>},
{"get_bindings", lua_wrap_errors<l_get_bindings>}, {"remove_packs", lua_wrap_errors<l_remove_packs>},
{"get_setting", lua_wrap_errors<l_get_setting>}, {"get_bindings", lua_wrap_errors<l_get_bindings>},
{"set_setting", lua_wrap_errors<l_set_setting>}, {"get_setting", lua_wrap_errors<l_get_setting>},
{"str_setting", lua_wrap_errors<l_str_setting>}, {"set_setting", lua_wrap_errors<l_set_setting>},
{"get_setting_info", lua_wrap_errors<l_get_setting_info>}, {"str_setting", lua_wrap_errors<l_str_setting>},
{"quit", lua_wrap_errors<l_quit>}, {"get_setting_info", lua_wrap_errors<l_get_setting_info>},
{"get_default_generator", lua_wrap_errors<l_get_default_generator>}, {"quit", lua_wrap_errors<l_quit>},
{"get_generators", lua_wrap_errors<l_get_generators>}, {"get_default_generator", lua_wrap_errors<l_get_default_generator>},
{NULL, NULL} {"get_generators", lua_wrap_errors<l_get_generators>},
}; {NULL, NULL}
};

View File

@ -10,7 +10,7 @@
struct AudioSettings { struct AudioSettings {
/// @brief try to initialize AL backend /// @brief try to initialize AL backend
bool enabled = true; FlagSetting enabled {true};
NumberSetting volumeMaster {1.0f, 0.0f, 1.0f, setting_format::percent}; NumberSetting volumeMaster {1.0f, 0.0f, 1.0f, setting_format::percent};
NumberSetting volumeRegular {1.0f, 0.0f, 1.0f, setting_format::percent}; NumberSetting volumeRegular {1.0f, 0.0f, 1.0f, setting_format::percent};
@ -23,17 +23,13 @@ struct DisplaySettings {
/// @brief Is window in full screen mode /// @brief Is window in full screen mode
FlagSetting fullscreen {false}; FlagSetting fullscreen {false};
/// @brief Window width (pixels) /// @brief Window width (pixels)
int width = 1280; IntegerSetting width {1280};
/// @brief Window height (pixels) /// @brief Window height (pixels)
int height = 720; IntegerSetting height {720};
/// @brief Anti-aliasing samples /// @brief Anti-aliasing samples
int samples = 0; IntegerSetting samples {0};
/// @brief VSync on /// @brief VSync on
FlagSetting vsync {true}; FlagSetting vsync {true};
/// @brief Window title
std::string title = "VoxelEngine-Cpp v" +
std::to_string(ENGINE_VERSION_MAJOR) + "." +
std::to_string(ENGINE_VERSION_MINOR);
}; };
struct ChunksSettings { struct ChunksSettings {
@ -60,6 +56,7 @@ struct GraphicsSettings {
/// @brief Fog opacity is calculated as `pow(depth*k, fogCurve)` where k depends on chunksLoadDistance. /// @brief Fog opacity is calculated as `pow(depth*k, fogCurve)` where k depends on chunksLoadDistance.
/// 1.0 is linear, 2.0 is quadratic /// 1.0 is linear, 2.0 is quadratic
NumberSetting fogCurve {1.6f, 1.0f, 6.0f}; NumberSetting fogCurve {1.6f, 1.0f, 6.0f};
/// @brief Lighting gamma
NumberSetting gamma {1.0f, 0.5f, 2.0f}; NumberSetting gamma {1.0f, 0.5f, 2.0f};
/// @brief Enable blocks backlight to prevent complete darkness /// @brief Enable blocks backlight to prevent complete darkness
FlagSetting backlight {true}; FlagSetting backlight {true};
@ -70,9 +67,8 @@ struct GraphicsSettings {
struct DebugSettings { struct DebugSettings {
/// @brief Turns off chunks saving/loading /// @brief Turns off chunks saving/loading
bool generatorTestMode = false; FlagSetting generatorTestMode {false};
bool showChunkBorders = false; FlagSetting doWriteLights {true};
bool doWriteLights = true;
}; };
struct UiSettings { struct UiSettings {

View File

@ -9,7 +9,7 @@
#include "engine.h" #include "engine.h"
#include "coders/toml.h" #include "coders/toml.h"
#include "files/files.h" #include "files/files.h"
#include "files/settings_io.h" #include "files/settings_io.hpp"
#include "files/engine_paths.h" #include "files/engine_paths.h"
#include "util/platform.h" #include "util/platform.h"
#include "util/command_line.h" #include "util/command_line.h"
@ -33,18 +33,17 @@ int main(int argc, char** argv) {
fs::path userfiles = paths.getUserfiles(); fs::path userfiles = paths.getUserfiles();
try { try {
EngineSettings settings; EngineSettings settings;
std::unique_ptr<toml::Wrapper> wrapper (create_wrapper(settings)); SettingsHandler handler(settings);
fs::path settings_file = userfiles/fs::path(SETTINGS_FILE); fs::path settings_file = userfiles/fs::path(SETTINGS_FILE);
fs::path controls_file = userfiles/fs::path(CONTROLS_FILE); fs::path controls_file = userfiles/fs::path(CONTROLS_FILE);
if (fs::is_regular_file(settings_file)) { if (fs::is_regular_file(settings_file)) {
logger.info() << "loading settings"; logger.info() << "loading settings";
std::string text = files::read_string(settings_file); std::string text = files::read_string(settings_file);
toml::Reader reader(wrapper.get(), settings_file.string(), text); toml::parse(handler, settings_file.string(), text);
reader.read();
} }
corecontent::setup_bindings(); corecontent::setup_bindings();
Engine engine(settings, &paths); Engine engine(settings, handler, &paths);
if (fs::is_regular_file(controls_file)) { if (fs::is_regular_file(controls_file)) {
logger.info() << "loading controls"; logger.info() << "loading controls";
std::string text = files::read_string(controls_file); std::string text = files::read_string(controls_file);
@ -53,7 +52,7 @@ int main(int argc, char** argv) {
engine.mainloop(); engine.mainloop();
logger.info() << "saving settings"; logger.info() << "saving settings";
files::write_string(settings_file, wrapper->write()); files::write_string(settings_file, toml::stringify(handler));
files::write_string(controls_file, write_controls()); files::write_string(controls_file, write_controls());
} }
catch (const initialize_error& err) { catch (const initialize_error& err) {

View File

@ -72,8 +72,8 @@ void window_size_callback(GLFWwindow*, int width, int height) {
} }
if (!Window::isFullscreen() && !Window::isMaximized()) { if (!Window::isFullscreen() && !Window::isMaximized()) {
Window::getSettings()->width = width; Window::getSettings()->width.set(width);
Window::getSettings()->height = height; Window::getSettings()->height.set(height);
} }
} }
Window::resetScissor(); Window::resetScissor();
@ -110,8 +110,12 @@ void error_callback(int error, const char* description) {
int Window::initialize(DisplaySettings* settings){ int Window::initialize(DisplaySettings* settings){
Window::settings = settings; Window::settings = settings;
Window::width = settings->width; Window::width = settings->width.get();
Window::height = settings->height; Window::height = settings->height.get();
std::string title = "VoxelEngine-Cpp v" +
std::to_string(ENGINE_VERSION_MAJOR) + "." +
std::to_string(ENGINE_VERSION_MINOR);
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
if (glfwInit() == GLFW_FALSE) { if (glfwInit() == GLFW_FALSE) {
@ -129,9 +133,9 @@ int Window::initialize(DisplaySettings* settings){
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
#endif #endif
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, settings->samples); glfwWindowHint(GLFW_SAMPLES, settings->samples.get());
window = glfwCreateWindow(width, height, settings->title.c_str(), nullptr, nullptr); window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
if (window == nullptr){ if (window == nullptr){
logger.error() << "failed to create GLFW window"; logger.error() << "failed to create GLFW window";
glfwTerminate(); glfwTerminate();
@ -286,7 +290,11 @@ void Window::toggleFullscreen(){
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE); glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE);
} }
else { else {
glfwSetWindowMonitor(window, nullptr, posX, posY, settings->width, settings->height, GLFW_DONT_CARE); glfwSetWindowMonitor(window, nullptr,
posX, posY,
settings->width.get(), settings->height.get(),
GLFW_DONT_CARE
);
glfwSetWindowAttrib(window, GLFW_MAXIMIZED, GLFW_FALSE); glfwSetWindowAttrib(window, GLFW_MAXIMIZED, GLFW_FALSE);
} }

View File

@ -57,7 +57,7 @@ void World::write(Level* level) {
if (chunk == nullptr || !chunk->isLighted()) if (chunk == nullptr || !chunk->isLighted())
continue; continue;
bool lightsUnsaved = !chunk->isLoadedLights() && bool lightsUnsaved = !chunk->isLoadedLights() &&
settings.debug.doWriteLights; settings.debug.doWriteLights.get();
if (!chunk->isUnsaved() && !lightsUnsaved) if (!chunk->isUnsaved() && !lightsUnsaved)
continue; continue;
regions.put(chunk.get()); regions.put(chunk.get());