2018-03-17 00:04:26 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "settings.h"
|
|
|
|
#include "sqlite3.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "defines.h"
|
2018-04-16 21:19:26 +02:00
|
|
|
#include "db.h"
|
2018-03-17 00:04:26 +01:00
|
|
|
|
|
|
|
static sqlite3 *db = NULL;
|
|
|
|
static Settings settings;
|
|
|
|
|
|
|
|
static const char *KEY_MUSIC_ENABLED = "music_enabled";
|
|
|
|
static const char *KEY_SOUND_ENABLED = "sound_enabled";
|
|
|
|
|
2018-08-12 16:14:07 +02:00
|
|
|
static
|
|
|
|
DbQuery MIGRATE_COMMANDS[] = {
|
|
|
|
{
|
|
|
|
"CREATE TABLE IF NOT EXISTS settings_int("
|
|
|
|
"key TEXT PRIMARY KEY, "
|
|
|
|
"value INTEGER)",
|
|
|
|
NULL, NULL
|
|
|
|
},
|
|
|
|
{ NULL } // Sentinel
|
2018-05-05 20:59:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int load_settings_cb(void *unused, int count, char **values, char **colNames);
|
2018-08-12 16:14:07 +02:00
|
|
|
DbQuery LOAD_SETTINGS = { "SELECT * FROM settings_int", load_settings_cb, NULL };
|
2018-05-05 20:59:53 +02:00
|
|
|
|
2018-03-17 00:04:26 +01:00
|
|
|
static void
|
|
|
|
set_default_settings(void)
|
|
|
|
{
|
|
|
|
settings.music_enabled = true;
|
|
|
|
settings.sound_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_tables(void)
|
|
|
|
{
|
2018-05-05 20:59:53 +02:00
|
|
|
for (unsigned int i = 0;; ++i) {
|
2018-08-12 16:14:07 +02:00
|
|
|
DbQuery *query = &MIGRATE_COMMANDS[i];
|
2018-05-05 20:59:53 +02:00
|
|
|
if (query->stmt == NULL)
|
|
|
|
break;
|
|
|
|
|
2018-08-12 16:14:07 +02:00
|
|
|
db_execute(db, query);
|
2018-05-05 20:59:53 +02:00
|
|
|
}
|
2018-03-17 00:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
load_settings_cb(void *unused, int count, char **values, char **colNames)
|
|
|
|
{
|
|
|
|
UNUSED(unused);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
while (i < count) {
|
|
|
|
if (strcmp("key", colNames[i]) != 0)
|
|
|
|
continue;
|
|
|
|
|
2018-04-16 21:19:26 +02:00
|
|
|
debug("Loading setting: %s = %s", values[i], values[i + 1]);
|
2018-03-17 00:04:26 +01:00
|
|
|
if (!strcmp(KEY_SOUND_ENABLED, values[i])) {
|
2018-04-16 21:19:26 +02:00
|
|
|
settings.sound_enabled = (bool)atoi(values[i + 1]);
|
2018-03-17 00:04:26 +01:00
|
|
|
i += 2;
|
2018-04-16 21:19:26 +02:00
|
|
|
}
|
|
|
|
else if (!strcmp(KEY_MUSIC_ENABLED, values[i])) {
|
|
|
|
settings.music_enabled = (bool)atoi(values[i + 1]);
|
2018-03-17 00:04:26 +01:00
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_settings(void)
|
|
|
|
{
|
2018-08-12 16:14:07 +02:00
|
|
|
db_execute(db, &LOAD_SETTINGS);
|
2018-03-17 00:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
settings_init(void)
|
|
|
|
{
|
2018-08-12 16:14:07 +02:00
|
|
|
if (!db_open(DB_FILE, &db)) {
|
2018-04-16 21:19:26 +02:00
|
|
|
db_close(&db);
|
2018-03-17 00:04:26 +01:00
|
|
|
fatal("Exiting");
|
|
|
|
}
|
2018-04-16 21:19:26 +02:00
|
|
|
set_default_settings();
|
2018-03-17 00:04:26 +01:00
|
|
|
create_tables();
|
|
|
|
load_settings();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
save_setting_int(const char *key, int value)
|
|
|
|
{
|
|
|
|
const char *stmtStr = "INSERT OR REPLACE INTO settings_int(key, value) values (?, ?)";
|
2018-08-12 16:14:07 +02:00
|
|
|
sqlite3_stmt *stmt = db_prepare(db, stmtStr);
|
2018-03-17 00:04:26 +01:00
|
|
|
|
|
|
|
debug("Saving setting: %s = %d", key, value);
|
2018-08-12 16:14:07 +02:00
|
|
|
sqlite3_bind_text(stmt,
|
|
|
|
1,
|
|
|
|
key,
|
|
|
|
(int) strlen(key),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 2, value);
|
|
|
|
sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
2018-03-17 00:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
settings_save(void)
|
|
|
|
{
|
|
|
|
save_setting_int(KEY_SOUND_ENABLED, settings.sound_enabled);
|
|
|
|
save_setting_int(KEY_MUSIC_ENABLED, settings.music_enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings *
|
|
|
|
settings_get(void)
|
|
|
|
{
|
|
|
|
return &settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
settings_close(void)
|
|
|
|
{
|
|
|
|
settings_save();
|
2018-04-16 21:19:26 +02:00
|
|
|
db_close(&db);
|
2018-03-17 00:04:26 +01:00
|
|
|
}
|