2013-12-19 17:10:14 +00:00
|
|
|
#include "cfg.h"
|
2015-08-16 21:35:13 +00:00
|
|
|
#include "ini.h"
|
2020-03-29 15:32:53 +00:00
|
|
|
#include "stdclass.h"
|
|
|
|
|
|
|
|
#include <cerrno>
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2020-03-29 17:29:14 +00:00
|
|
|
static std::string cfgPath;
|
2019-02-16 13:25:54 +00:00
|
|
|
static bool save_config = true;
|
2019-10-29 13:34:29 +00:00
|
|
|
static bool autoSave = true;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-03-25 15:47:47 +00:00
|
|
|
static emucfg::ConfigFile cfgdb;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
static void saveConfigFile()
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2021-01-19 10:11:01 +00:00
|
|
|
FILE* cfgfile = nowide::fopen(cfgPath.c_str(), "wt");
|
2013-12-19 17:10:14 +00:00
|
|
|
if (!cfgfile)
|
2015-08-24 12:46:45 +00:00
|
|
|
{
|
2019-07-01 16:23:10 +00:00
|
|
|
WARN_LOG(COMMON, "Error: Unable to open file '%s' for saving", cfgPath.c_str());
|
2015-08-24 12:46:45 +00:00
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
else
|
|
|
|
{
|
2015-08-18 17:36:19 +00:00
|
|
|
cfgdb.save(cfgfile);
|
2021-01-19 10:11:01 +00:00
|
|
|
std::fclose(cfgfile);
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-14 19:17:37 +00:00
|
|
|
void cfgSaveStr(const std::string& section, const std::string& key, const std::string& value)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2021-03-14 19:17:37 +00:00
|
|
|
cfgdb.set(section, key, value);
|
2019-10-29 13:34:29 +00:00
|
|
|
|
|
|
|
if (save_config && autoSave)
|
2021-03-01 09:13:40 +00:00
|
|
|
saveConfigFile();
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cfgOpen()
|
|
|
|
{
|
2019-02-25 16:52:53 +00:00
|
|
|
if (get_writable_config_path("").empty())
|
|
|
|
// Config dir not set (android onboarding)
|
|
|
|
return false;
|
|
|
|
|
2020-11-26 15:45:57 +00:00
|
|
|
const char* filename = "emu.cfg";
|
2020-03-29 17:29:14 +00:00
|
|
|
std::string config_path_read = get_readonly_config_path(filename);
|
2015-08-28 23:28:51 +00:00
|
|
|
cfgPath = get_writable_config_path(filename);
|
2015-08-24 11:30:01 +00:00
|
|
|
|
2021-01-19 10:11:01 +00:00
|
|
|
FILE* cfgfile = nowide::fopen(config_path_read.c_str(), "r");
|
2015-08-24 12:45:52 +00:00
|
|
|
if(cfgfile != NULL) {
|
|
|
|
cfgdb.parse(cfgfile);
|
2021-01-19 10:11:01 +00:00
|
|
|
std::fclose(cfgfile);
|
2015-08-24 12:05:51 +00:00
|
|
|
}
|
2015-08-24 12:45:52 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Config file can't be opened
|
|
|
|
int error_code = errno;
|
2019-07-01 16:23:10 +00:00
|
|
|
WARN_LOG(COMMON, "Warning: Unable to open the config file '%s' for reading (%s)", config_path_read.c_str(), strerror(error_code));
|
2015-08-28 23:28:51 +00:00
|
|
|
if (error_code == ENOENT || cfgPath != config_path_read)
|
2015-08-24 12:45:52 +00:00
|
|
|
{
|
|
|
|
// Config file didn't exist
|
2019-07-01 16:23:10 +00:00
|
|
|
INFO_LOG(COMMON, "Creating new empty config file at '%s'", cfgPath.c_str());
|
2021-03-01 09:13:40 +00:00
|
|
|
saveConfigFile();
|
2015-08-24 12:45:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// There was some other error (may be a permissions problem or something like that)
|
|
|
|
save_config = false;
|
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
std::string cfgLoadStr(const std::string& section, const std::string& key, const std::string& def)
|
2015-03-22 00:16:28 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
return cfgdb.get(section, key, def);
|
2015-03-22 00:16:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
void cfgSaveInt(const std::string& section, const std::string& key, s32 value)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
cfgSaveStr(section, key, std::to_string(value));
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
s32 cfgLoadInt(const std::string& section, const std::string& key, s32 def)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
return cfgdb.get_int(section, key, def);
|
2016-03-02 05:48:34 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 16:01:05 +00:00
|
|
|
int64_t cfgLoadInt64(const std::string& section, const std::string& key, int64_t def) {
|
|
|
|
return cfgdb.get_int64(section, key, def);
|
|
|
|
}
|
|
|
|
void cfgSaveInt64(const std::string& section, const std::string& key, int64_t value) {
|
|
|
|
cfgdb.set_int64(section, key, value);
|
|
|
|
}
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
void cfgSaveBool(const std::string& section, const std::string& key, bool value)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
cfgSaveStr(section, key, value ? "yes" : "no");
|
2015-08-16 21:35:13 +00:00
|
|
|
}
|
2019-02-16 13:25:54 +00:00
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
bool cfgLoadBool(const std::string& section, const std::string& key, bool def)
|
2019-02-16 13:25:54 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
return cfgdb.get_bool(section, key, def);
|
2019-02-16 13:25:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
void cfgSetVirtual(const std::string& section, const std::string& key, const std::string& value)
|
2019-02-16 13:25:54 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
cfgdb.set(section, key, value, true);
|
2019-02-16 13:25:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
bool cfgIsVirtual(const std::string& section, const std::string& key)
|
2019-02-16 13:25:54 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
return cfgdb.is_virtual(section, key);
|
2019-02-16 13:25:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
bool cfgHasSection(const std::string& section)
|
2019-02-16 13:25:54 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
return cfgdb.has_section(section);
|
2019-02-16 13:25:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 09:13:40 +00:00
|
|
|
void cfgDeleteSection(const std::string& section)
|
2019-02-16 13:25:54 +00:00
|
|
|
{
|
2021-03-01 09:13:40 +00:00
|
|
|
cfgdb.delete_section(section);
|
2019-02-16 13:25:54 +00:00
|
|
|
}
|
2019-10-29 13:34:29 +00:00
|
|
|
|
|
|
|
void cfgSetAutoSave(bool autoSave)
|
|
|
|
{
|
|
|
|
::autoSave = autoSave;
|
|
|
|
if (autoSave)
|
2021-03-01 09:13:40 +00:00
|
|
|
saveConfigFile();
|
2019-10-29 13:34:29 +00:00
|
|
|
}
|