mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-10 04:42:38 +01:00
c302f10b19
For now, there's only single OS-dependent bit of code, it it should be handled on FreeBSD just like on Linux. While here, change macro testing from #if XXX to #if defined(XXX), this is clener and not prone to "undefined macro" errors
97 lines
1.9 KiB
C++
97 lines
1.9 KiB
C++
#include "GameConfig.hpp"
|
|
#include <rw/defines.hpp>
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
|
|
#include <ini.h>
|
|
|
|
const std::string kConfigDirectoryName("OpenRW");
|
|
|
|
GameConfig::GameConfig(const std::string& configName, const std::string& configPath)
|
|
: m_configName(configName)
|
|
, m_configPath(configPath)
|
|
, m_valid(false)
|
|
, m_inputInvertY(false)
|
|
{
|
|
if (m_configPath.empty())
|
|
{
|
|
m_configPath = getDefaultConfigPath();
|
|
}
|
|
|
|
// Look up the path to use
|
|
auto configFile = getConfigFile();
|
|
|
|
if (ini_parse(configFile.c_str(), handler, this) < 0)
|
|
{
|
|
m_valid = false;
|
|
}
|
|
else
|
|
{
|
|
m_valid = true;
|
|
}
|
|
}
|
|
|
|
std::string GameConfig::getConfigFile()
|
|
{
|
|
return m_configPath + "/" + m_configName;
|
|
}
|
|
|
|
bool GameConfig::isValid()
|
|
{
|
|
return m_valid;
|
|
}
|
|
|
|
std::string GameConfig::getDefaultConfigPath()
|
|
{
|
|
#if defined(RW_LINUX) || defined(RW_FREEBSD)
|
|
char* config_home = getenv("XDG_CONFIG_HOME");
|
|
if (config_home != nullptr) {
|
|
return std::string(config_home) + "/" + kConfigDirectoryName;
|
|
}
|
|
char* home = getenv("HOME");
|
|
if (home != nullptr) {
|
|
return std::string(home) + "/.config/" + kConfigDirectoryName;
|
|
}
|
|
|
|
#elif defined(RW_OSX)
|
|
char* home = getenv("HOME");
|
|
if (home)
|
|
return std::string(home) + "/Library/Preferences/" + kConfigDirectoryName;
|
|
|
|
#else
|
|
#error Dont know how to find default config path
|
|
#endif
|
|
|
|
// Well now we're stuck.
|
|
RW_ERROR("No default config path found.");
|
|
return ".";
|
|
}
|
|
|
|
int GameConfig::handler(void* user,
|
|
const char* section,
|
|
const char* name,
|
|
const char* value)
|
|
{
|
|
auto self = static_cast<GameConfig*>(user);
|
|
#define MATCH(_s, _n) (strcmp(_s, section) == 0 && strcmp(_n, name) == 0)
|
|
|
|
if (MATCH("game", "path"))
|
|
{
|
|
self->m_gamePath = value;
|
|
}
|
|
else if (MATCH("input", "invert_y"))
|
|
{
|
|
self->m_inputInvertY = atoi(value) > 0;
|
|
}
|
|
else
|
|
{
|
|
RW_MESSAGE("Unhandled config entry [" << section << "] " << name << " = " << value);
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
|
|
#undef MATCH
|
|
}
|
|
|