The Update(TM), Part 1
This commit is contained in:
@@ -1,23 +1,17 @@
|
||||
#include "settings.h"
|
||||
|
||||
static void _settings_setting_load(Settings* self, const std::string& line);
|
||||
static void _settings_setting_write(Settings* self, std::ostream& out, SettingsEntry entry);
|
||||
|
||||
static void
|
||||
_settings_setting_load(Settings* self, const std::string& line)
|
||||
static void _settings_setting_load(Settings* self, const std::string& line)
|
||||
{
|
||||
for (s32 i = 0; i < SETTINGS_COUNT; i++)
|
||||
{
|
||||
const std::string& key = SETTINGS_ENTRIES[i].key;
|
||||
size_t keyLength = key.length();
|
||||
|
||||
// Compare keys
|
||||
if (line.compare(0, keyLength, key) == 0)
|
||||
{
|
||||
const char* value = line.c_str() + keyLength;
|
||||
void* target = (u8*)self + SETTINGS_ENTRIES[i].offset;
|
||||
|
||||
// Based on type, assign value to offset of settings
|
||||
switch (SETTINGS_ENTRIES[i].type)
|
||||
{
|
||||
case SETTINGS_TYPE_INT:
|
||||
@@ -40,9 +34,7 @@ _settings_setting_load(Settings* self, const std::string& line)
|
||||
}
|
||||
}
|
||||
|
||||
// Writes a given setting to the stream
|
||||
static void
|
||||
_settings_setting_write(Settings* self, std::ostream& out, SettingsEntry entry)
|
||||
static void _settings_setting_write(Settings* self, std::ostream& out, SettingsEntry entry)
|
||||
{
|
||||
u8* selfPointer = (u8*)self;
|
||||
std::string value;
|
||||
@@ -68,60 +60,51 @@ _settings_setting_write(Settings* self, std::ostream& out, SettingsEntry entry)
|
||||
out << entry.key << value << "\n";
|
||||
}
|
||||
|
||||
// Saves the current settings
|
||||
// Note: this is just for this program's settings, additional imgui settings handled elsewhere
|
||||
void
|
||||
settings_save(Settings* self)
|
||||
void settings_save(Settings* self)
|
||||
{
|
||||
std::ifstream input(PATH_SETTINGS);
|
||||
std::ifstream input(SETTINGS_PATH);
|
||||
std::string oldContents;
|
||||
|
||||
if (!input)
|
||||
{
|
||||
std::cout << STRING_ERROR_SETTINGS_INIT << PATH_SETTINGS << std::endl;
|
||||
log_error(std::format(SETTINGS_INIT_ERROR, SETTINGS_PATH));
|
||||
return;
|
||||
}
|
||||
|
||||
// We're writing after the imgui stuff
|
||||
oldContents.assign((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
|
||||
input.close();
|
||||
|
||||
std::ofstream output(PATH_SETTINGS);
|
||||
std::ofstream output(SETTINGS_PATH);
|
||||
if (!output)
|
||||
{
|
||||
std::cout << STRING_ERROR_SETTINGS_INIT << PATH_SETTINGS << std::endl;
|
||||
log_error(std::format(SETTINGS_INIT_ERROR, SETTINGS_PATH));
|
||||
return;
|
||||
}
|
||||
|
||||
// [Settings]
|
||||
output << SETTINGS_SECTION << "\n";
|
||||
|
||||
// Write each setting
|
||||
for (s32 i = 0; i < SETTINGS_COUNT; i++)
|
||||
_settings_setting_write(self, output, SETTINGS_ENTRIES[i]);
|
||||
|
||||
// Write the the imgui section
|
||||
output << "\n" << SETTINGS_SECTION_IMGUI << "\n";
|
||||
output << oldContents;
|
||||
|
||||
output.close();
|
||||
}
|
||||
|
||||
// Load settings
|
||||
void
|
||||
settings_load(Settings* self)
|
||||
void settings_init(Settings* self)
|
||||
{
|
||||
std::ifstream file(PATH_SETTINGS);
|
||||
std::ifstream file(SETTINGS_PATH);
|
||||
|
||||
if (!file)
|
||||
{
|
||||
std::cerr << STRING_ERROR_SETTINGS_INIT << PATH_SETTINGS << std::endl;
|
||||
log_error(std::format(SETTINGS_INIT_ERROR, SETTINGS_PATH));
|
||||
return;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
bool inSettingsSection = false;
|
||||
|
||||
// Iterare through settings lines until the imgui section is reached, then end
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (line == SETTINGS_SECTION)
|
||||
@@ -130,10 +113,7 @@ settings_load(Settings* self)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == SETTINGS_SECTION_IMGUI)
|
||||
break;
|
||||
|
||||
if (inSettingsSection)
|
||||
_settings_setting_load(self, line);
|
||||
if (line == SETTINGS_SECTION_IMGUI) break;
|
||||
if (inSettingsSection) _settings_setting_load(self, line);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user