The Omega Refactor(TM) + Input Options
This commit is contained in:
167
src/settings.cpp
167
src/settings.cpp
@@ -1,31 +1,36 @@
|
||||
#include "settings.h"
|
||||
|
||||
static void _settings_setting_load(Settings* self, char* line);
|
||||
static void _settings_setting_write(Settings* self, SDL_IOStream* io, SettingsItem type);
|
||||
static void _settings_setting_load(Settings* self, const std::string& line);
|
||||
static void _settings_setting_write(Settings* self, std::ostream& out, SettingsEntry entry);
|
||||
|
||||
/* Load a particular settings from a line */
|
||||
static void
|
||||
_settings_setting_load(Settings* self, char* line)
|
||||
static void
|
||||
_settings_setting_load(Settings* self, const std::string& line)
|
||||
{
|
||||
for (int i = 0; i < SETTINGS_COUNT; i++)
|
||||
for (s32 i = 0; i < SETTINGS_COUNT; i++)
|
||||
{
|
||||
if (strncmp(line, SETTINGS_ENTRIES[i].value, strlen(SETTINGS_ENTRIES[i].value)) == 0)
|
||||
const std::string& key = SETTINGS_ENTRIES[i].key;
|
||||
size_t keyLength = key.length();
|
||||
|
||||
// Compare keys
|
||||
if (line.compare(0, keyLength, key) == 0)
|
||||
{
|
||||
char* value = line + strlen(SETTINGS_ENTRIES[i].value);
|
||||
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:
|
||||
*(s32*)target = std::atoi(value);
|
||||
break;
|
||||
case SETTINGS_TYPE_BOOL:
|
||||
*(s32*)target = atoi(value);
|
||||
*(s32*)target = string_to_bool(std::string(value));
|
||||
break;
|
||||
case SETTINGS_TYPE_FLOAT:
|
||||
*(f32*)target = atof(value);
|
||||
*(f32*)target = std::atof(value);
|
||||
break;
|
||||
case SETTINGS_TYPE_STRING:
|
||||
strncpy((char*)target, value, SETTINGS_BUFFER_ITEM - 1);
|
||||
((char*)target)[SETTINGS_BUFFER_ITEM - 1] = '\0';
|
||||
*(std::string*)target = std::string(value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -35,126 +40,100 @@ _settings_setting_load(Settings* self, char* line)
|
||||
}
|
||||
}
|
||||
|
||||
/* Writes a particular setting to the current IO */
|
||||
static void
|
||||
_settings_setting_write(Settings* self, SDL_IOStream* io, SettingsItem type)
|
||||
// Writes a given setting to the stream
|
||||
static void
|
||||
_settings_setting_write(Settings* self, std::ostream& out, SettingsEntry entry)
|
||||
{
|
||||
char valueBuffer[SETTINGS_BUFFER_ITEM];
|
||||
SettingsEntry entry = SETTINGS_ENTRIES[type];
|
||||
|
||||
u8* selfPointer = (u8*)self;
|
||||
|
||||
memset(valueBuffer, '\0', sizeof(valueBuffer));
|
||||
std::string value;
|
||||
|
||||
switch (entry.type)
|
||||
{
|
||||
case SETTINGS_TYPE_INT:
|
||||
snprintf(valueBuffer, SETTINGS_BUFFER_ITEM, entry.format, *(s32*)(selfPointer + entry.offset));
|
||||
value = std::format("{}", *(s32*)(selfPointer + entry.offset));
|
||||
break;
|
||||
case SETTINGS_TYPE_BOOL:
|
||||
snprintf(valueBuffer, SETTINGS_BUFFER_ITEM, entry.format, *(bool*)(selfPointer + entry.offset));
|
||||
value = std::format("{}", *(bool*)(selfPointer + entry.offset));
|
||||
break;
|
||||
case SETTINGS_TYPE_FLOAT:
|
||||
snprintf(valueBuffer, SETTINGS_BUFFER_ITEM, entry.format, *(f32*)(selfPointer + entry.offset));
|
||||
case SETTINGS_TYPE_FLOAT:
|
||||
value = std::format("{:.3f}", *(f32*)(selfPointer + entry.offset));
|
||||
break;
|
||||
case SETTINGS_TYPE_STRING:
|
||||
snprintf(valueBuffer, SETTINGS_BUFFER_ITEM, entry.value, entry.format, *(char*)(selfPointer + entry.offset));
|
||||
value = *(std::string*)(selfPointer + entry.offset);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_WriteIO(io, valueBuffer, strlen(valueBuffer));
|
||||
SDL_WriteIO(io, "\n", strlen("\n"));
|
||||
out << entry.key << value << "\n";
|
||||
}
|
||||
|
||||
/* Saves the program's settings to the PATH_SETTINGS file */
|
||||
void
|
||||
// Saves the current settings
|
||||
// Note: this is just for this program's settings, additional imgui settings handled elsewhere
|
||||
void
|
||||
settings_save(Settings* self)
|
||||
{
|
||||
char buffer[SETTINGS_BUFFER];
|
||||
std::ifstream input(PATH_SETTINGS);
|
||||
std::string oldContents;
|
||||
|
||||
/* Get the original settings.ini buffer (as previously saved by imgui before this is called) */
|
||||
memset(buffer, '\0', SETTINGS_BUFFER);
|
||||
|
||||
SDL_IOStream* io = SDL_IOFromFile(PATH_SETTINGS, "r");
|
||||
|
||||
if (!io)
|
||||
{
|
||||
printf(STRING_ERROR_SETTINGS_INIT, PATH_SETTINGS);
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_ReadIO(io, buffer, SETTINGS_BUFFER);
|
||||
SDL_CloseIO(io);
|
||||
|
||||
io = SDL_IOFromFile(PATH_SETTINGS, "w");
|
||||
|
||||
if (!io)
|
||||
if (!input)
|
||||
{
|
||||
printf(STRING_ERROR_SETTINGS_INIT, PATH_SETTINGS);
|
||||
std::cout << STRING_ERROR_SETTINGS_INIT << PATH_SETTINGS << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
/* [Settings] */
|
||||
SDL_WriteIO(io, SETTINGS_SECTION, strlen(SETTINGS_SECTION));
|
||||
SDL_WriteIO(io, "\n", strlen("\n"));
|
||||
// We're writing after the imgui stuff
|
||||
oldContents.assign((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
|
||||
input.close();
|
||||
|
||||
/* Write down all elements */
|
||||
std::ofstream output(PATH_SETTINGS);
|
||||
if (!output)
|
||||
{
|
||||
std::cout << STRING_ERROR_SETTINGS_INIT << PATH_SETTINGS << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// [Settings]
|
||||
output << SETTINGS_SECTION << "\n";
|
||||
|
||||
// Write each setting
|
||||
for (s32 i = 0; i < SETTINGS_COUNT; i++)
|
||||
_settings_setting_write(self, io, (SettingsItem)i);
|
||||
_settings_setting_write(self, output, SETTINGS_ENTRIES[i]);
|
||||
|
||||
SDL_WriteIO(io, "\n", strlen("\n"));
|
||||
// Write the the imgui section
|
||||
output << "\n" << SETTINGS_SECTION_IMGUI << "\n";
|
||||
output << oldContents;
|
||||
|
||||
/* specify that the other settings are imgui */
|
||||
SDL_WriteIO(io, SETTINGS_SECTION_IMGUI, strlen(SETTINGS_SECTION_IMGUI));
|
||||
SDL_WriteIO(io, "\n", strlen("\n"));
|
||||
|
||||
/* Then write original contents */
|
||||
SDL_WriteIO(io, buffer, strlen(buffer));
|
||||
SDL_CloseIO(io);
|
||||
output.close();
|
||||
}
|
||||
|
||||
/* Loads the settings from the PATH_SETTINGS file */
|
||||
void
|
||||
// Load settings
|
||||
void
|
||||
settings_load(Settings* self)
|
||||
{
|
||||
char buffer[SETTINGS_BUFFER];
|
||||
char* line = NULL;
|
||||
|
||||
memset(buffer, '\0', SETTINGS_BUFFER);
|
||||
|
||||
SDL_IOStream* io = SDL_IOFromFile(PATH_SETTINGS, "r");
|
||||
|
||||
if (!io)
|
||||
{
|
||||
printf(STRING_ERROR_SETTINGS_INIT, PATH_SETTINGS);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t bytesRead = SDL_ReadIO(io, buffer, SETTINGS_BUFFER);
|
||||
SDL_CloseIO(io);
|
||||
|
||||
buffer[bytesRead] = '\0';
|
||||
|
||||
line = strtok(buffer, "\n");
|
||||
|
||||
/* The settings will be the first section in the file */
|
||||
/* Go through its elements, load them to settings, and go on with your day */
|
||||
while (line != NULL)
|
||||
std::ifstream file(PATH_SETTINGS);
|
||||
if (!file)
|
||||
{
|
||||
if (strcmp(line, SETTINGS_SECTION) == 0)
|
||||
std::cerr << STRING_ERROR_SETTINGS_INIT << PATH_SETTINGS << std::endl;
|
||||
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)
|
||||
{
|
||||
line = strtok(NULL, "\n");
|
||||
inSettingsSection = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
_settings_setting_load(self, line);
|
||||
|
||||
/* get out here */
|
||||
if (strcmp(line, SETTINGS_SECTION_IMGUI) == 0)
|
||||
break;
|
||||
if (line == SETTINGS_SECTION_IMGUI)
|
||||
break;
|
||||
|
||||
line = strtok(NULL, "\n");
|
||||
if (inSettingsSection)
|
||||
_settings_setting_load(self, line);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user