Move try/catch from settings to file, overwrite invalid settings file
This commit is contained in:
@@ -19,28 +19,48 @@ namespace Inferno {
|
|||||||
static int32_t length(const std::string& path, std::ifstream& file);
|
static int32_t length(const std::string& path, std::ifstream& file);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static void ioRead(T* t, const std::string& path)
|
static bool ioRead(T* t, const std::string& path)
|
||||||
{
|
{
|
||||||
std::ifstream file(path);
|
std::ifstream file(path);
|
||||||
ASSERT(file.is_open(), "File could not open '{}'", path);
|
ASSERT(file.is_open(), "File could not open '{}'", path);
|
||||||
|
|
||||||
if (file.is_open()) {
|
if (!file.is_open()) {
|
||||||
file >> *t;
|
return false;
|
||||||
file.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
file >> *t;
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
file.close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static void ioWrite(T* t, const std::string& path)
|
static bool ioWrite(T* t, const std::string& path)
|
||||||
{
|
{
|
||||||
std::ofstream file (path);
|
std::ofstream file (path);
|
||||||
ASSERT(file.is_open(), "File could not open! {}", path);
|
ASSERT(file.is_open(), "File could not open! {}", path);
|
||||||
|
|
||||||
if (file.is_open()) {
|
if (!file.is_open()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
// Write file with single tabs, nicely formatted
|
// Write file with single tabs, nicely formatted
|
||||||
file << std::setfill ('\t') << std::setw(1) << *t << std::endl;
|
file << std::setfill ('\t') << std::setw(1) << *t << std::endl;
|
||||||
file.close();
|
|
||||||
}
|
}
|
||||||
|
catch (...) {
|
||||||
|
file.close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ namespace Inferno {
|
|||||||
void Settings::initialize()
|
void Settings::initialize()
|
||||||
{
|
{
|
||||||
Settings::load();
|
Settings::load();
|
||||||
|
Settings::save();
|
||||||
|
|
||||||
info() << "Settings initialized";
|
info() << "Settings initialized";
|
||||||
}
|
}
|
||||||
@@ -23,29 +24,32 @@ namespace Inferno {
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::load()
|
bool Settings::load()
|
||||||
{
|
{
|
||||||
json object;
|
json object;
|
||||||
|
|
||||||
try {
|
if (!File::ioRead(&object, m_path)) {
|
||||||
File::ioRead(&object, m_path);
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
warn() << "Settings invalid formatting, using default values";
|
warn() << "Settings invalid formatting, using default values";
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto settings = Json::getPropertyValue<SettingsProperties>(object, json::value_t::object);
|
auto settings = Json::getPropertyValue<SettingsProperties>(object, json::value_t::object);
|
||||||
if (settings) {
|
if (settings) {
|
||||||
m_properties = settings.value();
|
m_properties = settings.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Settings::save()
|
bool Settings::save()
|
||||||
{
|
{
|
||||||
json object = m_properties;
|
json object = m_properties;
|
||||||
File::ioWrite(&object, m_path);
|
if (!File::ioWrite(&object, m_path)) {
|
||||||
info() << "Settings saved";
|
warn() << "Settings could not be saved";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
info() << "Settings saved";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace Inferno {
|
|||||||
static void initialize();
|
static void initialize();
|
||||||
static void destroy();
|
static void destroy();
|
||||||
|
|
||||||
static void load();
|
static bool load();
|
||||||
static bool save();
|
static bool save();
|
||||||
|
|
||||||
static inline SettingsProperties& get() { return m_properties; }
|
static inline SettingsProperties& get() { return m_properties; }
|
||||||
|
|||||||
Reference in New Issue
Block a user