mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-04-14 23:58:26 +02:00
Removed my settings classes.
This commit is contained in:
parent
afef61dabf
commit
83160ae6cf
@ -1,90 +0,0 @@
|
|||||||
#include "db_settings.h"
|
|
||||||
|
|
||||||
#include "database/database.h"
|
|
||||||
#include "database/database_manager.h"
|
|
||||||
#include "database/query_builder.h"
|
|
||||||
#include "database/query_result.h"
|
|
||||||
#include "database/table_builder.h"
|
|
||||||
|
|
||||||
void DBSettings::set_value(const String &key, const Variant &value) {
|
|
||||||
const Variant &v = _data[key];
|
|
||||||
|
|
||||||
int id = 0;
|
|
||||||
|
|
||||||
if (!v.is_null()) {
|
|
||||||
id = _key_map[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
_data[key] = value;
|
|
||||||
|
|
||||||
Ref<QueryBuilder> qb = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
|
||||||
|
|
||||||
if (id == 0) {
|
|
||||||
qb->insert(_table, "key,value")->values()->val(key)->val(value.to_string())->cvalues();
|
|
||||||
qb->select_last_insert_id();
|
|
||||||
Ref<QueryResult> res = qb->run();
|
|
||||||
|
|
||||||
id = res->get_last_insert_rowid();
|
|
||||||
|
|
||||||
_key_map[key] = id;
|
|
||||||
} else {
|
|
||||||
qb->update(_table)->set()->setp("key", key)->setp("value", value.to_string())->cset()->where()->wp("id", id);
|
|
||||||
qb->run_query();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DBSettings::load() {
|
|
||||||
_data.clear();
|
|
||||||
_key_map.clear();
|
|
||||||
|
|
||||||
Ref<QueryBuilder> qb = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
|
||||||
qb->select("id,key,value")->from(_table);
|
|
||||||
|
|
||||||
Ref<QueryResult> res = qb->run();
|
|
||||||
|
|
||||||
while (res->next_row()) {
|
|
||||||
int id = res->get_cell_int(0);
|
|
||||||
String key = res->get_cell_str(1);
|
|
||||||
String value = res->get_cell_str(2);
|
|
||||||
|
|
||||||
_data[key] = Variant::parse_string(value);
|
|
||||||
_key_map[key] = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DBSettings::migrate() {
|
|
||||||
Ref<TableBuilder> tb = DatabaseManager::get_singleton()->ddb->get_table_builder();
|
|
||||||
tb->drop_table_if_exists(_table);
|
|
||||||
|
|
||||||
tb->create_table(_table);
|
|
||||||
tb->integer("id")->auto_increment()->next_row();
|
|
||||||
tb->varchar("key", 300)->not_null()->next_row();
|
|
||||||
tb->text("value")->not_null()->next_row();
|
|
||||||
tb->primary_key("id");
|
|
||||||
tb->ccreate_table();
|
|
||||||
|
|
||||||
tb->run_query();
|
|
||||||
}
|
|
||||||
|
|
||||||
void DBSettings::set_table(const String &table) {
|
|
||||||
_table = table;
|
|
||||||
}
|
|
||||||
|
|
||||||
DBSettings *DBSettings::get_singleton() {
|
|
||||||
return _db_settings_singleton;
|
|
||||||
}
|
|
||||||
|
|
||||||
DBSettings::DBSettings(const bool singleton) :
|
|
||||||
Settings(singleton) {
|
|
||||||
|
|
||||||
_table = "settings";
|
|
||||||
|
|
||||||
if (singleton) {
|
|
||||||
_db_settings_singleton = this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DBSettings::~DBSettings() {
|
|
||||||
}
|
|
||||||
|
|
||||||
DBSettings *DBSettings::_db_settings_singleton = nullptr;
|
|
@ -1,32 +0,0 @@
|
|||||||
#ifndef DB_SETTINGS_H
|
|
||||||
#define DB_SETTINGS_H
|
|
||||||
|
|
||||||
#include "core/settings/settings.h"
|
|
||||||
|
|
||||||
class DBSettings : public Settings {
|
|
||||||
RCPP_OBJECT(DBSettings, Settings);
|
|
||||||
|
|
||||||
public:
|
|
||||||
virtual void set_value(const String &key, const Variant &value);
|
|
||||||
|
|
||||||
void load();
|
|
||||||
|
|
||||||
void migrate();
|
|
||||||
|
|
||||||
void set_table(const String &table);
|
|
||||||
|
|
||||||
static DBSettings *get_singleton();
|
|
||||||
|
|
||||||
DBSettings(const bool singleton = false);
|
|
||||||
virtual ~DBSettings();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
String _table;
|
|
||||||
|
|
||||||
std::map<String, int> _key_map;
|
|
||||||
|
|
||||||
private:
|
|
||||||
static DBSettings *_db_settings_singleton;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,132 +0,0 @@
|
|||||||
#include "settings.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
Variant Settings::get_value(const String &key, const Variant &def) {
|
|
||||||
std::map<String, Variant>::iterator e = _data.find(key);
|
|
||||||
|
|
||||||
if (e != _data.end()) {
|
|
||||||
return e->second;
|
|
||||||
} else {
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String Settings::get_value_string(const String &key, const String &def) {
|
|
||||||
std::map<String, Variant>::iterator e = _data.find(key);
|
|
||||||
|
|
||||||
if (e != _data.end()) {
|
|
||||||
return e->second;
|
|
||||||
} else {
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int Settings::get_value_int(const String &key, const int def) {
|
|
||||||
std::map<String, Variant>::iterator e = _data.find(key);
|
|
||||||
|
|
||||||
if (e != _data.end()) {
|
|
||||||
return e->second.to_int();
|
|
||||||
} else {
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
float Settings::get_value_float(const String &key, const float def) {
|
|
||||||
std::map<String, Variant>::iterator e = _data.find(key);
|
|
||||||
|
|
||||||
if (e != _data.end()) {
|
|
||||||
return e->second.to_float();
|
|
||||||
} else {
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
double Settings::get_value_double(const String &key, const double def) {
|
|
||||||
std::map<String, Variant>::iterator e = _data.find(key);
|
|
||||||
|
|
||||||
if (e != _data.end()) {
|
|
||||||
return e->second.to_float();
|
|
||||||
} else {
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bool Settings::get_value_bool(const String &key, const bool def) {
|
|
||||||
std::map<String, Variant>::iterator e = _data.find(key);
|
|
||||||
|
|
||||||
if (e != _data.end()) {
|
|
||||||
return e->second.to_bool();
|
|
||||||
} else {
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Settings::set_value(const String &key, const Variant &value) {
|
|
||||||
_data[key] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Settings::parse_ini_file(const String &path) {
|
|
||||||
FILE *f = fopen(path.c_str(), "r");
|
|
||||||
|
|
||||||
if (!f) {
|
|
||||||
printf("Settings::parse_file: Error opening file!\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fseek(f, 0, SEEK_END);
|
|
||||||
long fsize = ftell(f);
|
|
||||||
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
|
|
||||||
|
|
||||||
String config_str;
|
|
||||||
config_str.resize(fsize);
|
|
||||||
|
|
||||||
fread(config_str.dataw(), 1, fsize, f);
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
config_str.replace('\r', ' ');
|
|
||||||
Vector<String> ns = config_str.split('\n');
|
|
||||||
|
|
||||||
for (int i = 0; i < ns.size(); ++i) {
|
|
||||||
String s = ns[i];
|
|
||||||
|
|
||||||
s.trim();
|
|
||||||
|
|
||||||
if (s.size() == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s[0] == '#') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int eindex = s.find('=');
|
|
||||||
|
|
||||||
if (eindex == -1 || eindex == s.size() - 1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String k = s.substr_index(0, eindex - 1);
|
|
||||||
String v = s.substr_index(eindex + 1, s.size() - 1);
|
|
||||||
|
|
||||||
_data[k] = Variant::parse_string(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Settings *Settings::get_singleton() {
|
|
||||||
return _singleton;
|
|
||||||
}
|
|
||||||
|
|
||||||
Settings::Settings(const bool singleton) {
|
|
||||||
if (singleton) {
|
|
||||||
if (_singleton) {
|
|
||||||
printf("Settings singleton overridden!\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
_singleton = this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Settings::~Settings() {
|
|
||||||
if (_singleton == this) {
|
|
||||||
_singleton = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Settings *Settings::_singleton = nullptr;
|
|
@ -1,38 +0,0 @@
|
|||||||
#ifndef SETTINGS_H
|
|
||||||
#define SETTINGS_H
|
|
||||||
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
#include "core/object.h"
|
|
||||||
|
|
||||||
#include "core/string.h"
|
|
||||||
#include "core/variant.h"
|
|
||||||
|
|
||||||
class Settings : public Object {
|
|
||||||
RCPP_OBJECT(Settings, Object);
|
|
||||||
|
|
||||||
public:
|
|
||||||
Variant get_value(const String &key, const Variant &def = Variant());
|
|
||||||
String get_value_string(const String &key, const String &def = "");
|
|
||||||
int get_value_int(const String &key, const int def = 0);
|
|
||||||
float get_value_float(const String &key, const float def = 0);
|
|
||||||
double get_value_double(const String &key, const double def = 0);
|
|
||||||
bool get_value_bool(const String &key, const bool def = false);
|
|
||||||
|
|
||||||
virtual void set_value(const String &key, const Variant &value);
|
|
||||||
|
|
||||||
void parse_ini_file(const String &path);
|
|
||||||
|
|
||||||
static Settings *get_singleton();
|
|
||||||
|
|
||||||
Settings(const bool singleton = false);
|
|
||||||
virtual ~Settings();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::map<String, Variant> _data;
|
|
||||||
|
|
||||||
private:
|
|
||||||
static Settings *_singleton;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
Loading…
Reference in New Issue
Block a user