Skip to content

Commit

Permalink
Merge pull request #1218 from withmorten/ini
Browse files Browse the repository at this point in the history
switch out current ini_parser.hpp with pulzed/mINI
  • Loading branch information
erorcun authored Jul 11, 2021
2 parents a8fd3f8 + c0cbf80 commit 3515363
Show file tree
Hide file tree
Showing 3 changed files with 821 additions and 382 deletions.
109 changes: 60 additions & 49 deletions src/core/re3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,117 +182,127 @@ CustomFrontendOptionsPopulate(void)
#endif

#ifdef LOAD_INI_SETTINGS
#include "ini_parser.hpp"
#define MINI_CASE_SENSITIVE
#include "ini.h"

mINI::INIFile ini("re3.ini");
mINI::INIStructure cfg;

linb::ini cfg;
bool ReadIniIfExists(const char *cat, const char *key, uint32 *out)
{
std::string strval = cfg.get(cat, key, "\xBA");
const char *value = strval.c_str();
char *endPtr;
if (value && value[0] != '\xBA') {
*out = strtoul(value, &endPtr, 0);
mINI::INIMap<std::string> section = cfg.get(cat);
if (section.has(key)) {
char *endPtr;
*out = strtoul(section.get(key).c_str(), &endPtr, 0);
return true;
}
return false;
}

bool ReadIniIfExists(const char *cat, const char *key, uint8 *out)
{
mINI::INIMap<std::string> section = cfg.get(cat);
if (section.has(key)) {
char *endPtr;
*out = strtoul(section.get(key).c_str(), &endPtr, 0);
return true;
}
return false;
}

bool ReadIniIfExists(const char *cat, const char *key, bool *out)
{
std::string strval = cfg.get(cat, key, "\xBA");
const char *value = strval.c_str();
char *endPtr;
if (value && value[0] != '\xBA') {
*out = strtoul(value, &endPtr, 0);
mINI::INIMap<std::string> section = cfg.get(cat);
if (section.has(key)) {
char *endPtr;
*out = strtoul(section.get(key).c_str(), &endPtr, 0);
return true;
}
return false;
}

bool ReadIniIfExists(const char *cat, const char *key, int32 *out)
{
std::string strval = cfg.get(cat, key, "\xBA");
const char *value = strval.c_str();
char *endPtr;
if (value && value[0] != '\xBA') {
*out = strtol(value, &endPtr, 0);
mINI::INIMap<std::string> section = cfg.get(cat);
if (section.has(key)) {
char *endPtr;
*out = strtol(section.get(key).c_str(), &endPtr, 0);
return true;
}
return false;
}

bool ReadIniIfExists(const char *cat, const char *key, int8 *out)
{
std::string strval = cfg.get(cat, key, "\xBA");
const char *value = strval.c_str();
char *endPtr;
if (value && value[0] != '\xBA') {
*out = strtol(value, &endPtr, 0);
mINI::INIMap<std::string> section = cfg.get(cat);
if (section.has(key)) {
char *endPtr;
*out = strtol(section.get(key).c_str(), &endPtr, 0);
return true;
}
return false;
}

bool ReadIniIfExists(const char *cat, const char *key, float *out)
{
std::string strval = cfg.get(cat, key, "\xBA");
const char *value = strval.c_str();
if (value && value[0] != '\xBA') {
*out = atof(value);
mINI::INIMap<std::string> section = cfg.get(cat);
if (section.has(key)) {
char *endPtr;
*out = strtof(section.get(key).c_str(), &endPtr);
return true;
}
return false;
}

bool ReadIniIfExists(const char *cat, const char *key, char *out, int size)
{
std::string strval = cfg.get(cat, key, "\xBA");
const char *value = strval.c_str();
if (value && value[0] != '\xBA') {
strncpy(out, value, size);
mINI::INIMap<std::string> section = cfg.get(cat);
if (section.has(key)) {
strncpy(out, section.get(key).c_str(), size - 1);
out[size - 1] = '\0';
return true;
}
return false;
}

void StoreIni(const char *cat, const char *key, uint32 val)
{
char temp[10];
char temp[11];
sprintf(temp, "%u", val);
cfg.set(cat, key, temp);
cfg[cat][key] = temp;
}

void StoreIni(const char *cat, const char *key, uint8 val)
{
char temp[10];
sprintf(temp, "%u", (uint32)val);
cfg.set(cat, key, temp);
char temp[11];
sprintf(temp, "%u", val);
cfg[cat][key] = temp;
}

void StoreIni(const char *cat, const char *key, int32 val)
{
char temp[10];
char temp[11];
sprintf(temp, "%d", val);
cfg.set(cat, key, temp);
cfg[cat][key] = temp;
}

void StoreIni(const char *cat, const char *key, int8 val)
{
char temp[10];
sprintf(temp, "%d", (int32)val);
cfg.set(cat, key, temp);
char temp[11];
sprintf(temp, "%d", val);
cfg[cat][key] = temp;
}

void StoreIni(const char *cat, const char *key, float val)
{
char temp[10];
char temp[50];
sprintf(temp, "%f", val);
cfg.set(cat, key, temp);
cfg[cat][key] = temp;
}

void StoreIni(const char *cat, const char *key, char *val, int size)
{
cfg.set(cat, key, val);
cfg[cat][key] = val;
}

const char *iniControllerActions[] = { "PED_FIREWEAPON", "PED_CYCLE_WEAPON_RIGHT", "PED_CYCLE_WEAPON_LEFT", "GO_FORWARD", "GO_BACK", "GO_LEFT", "GO_RIGHT", "PED_SNIPER_ZOOM_IN",
Expand Down Expand Up @@ -354,7 +364,7 @@ void LoadINIControllerSettings()
#endif
// force to default GTA behaviour (never overwrite bindings on joy change/initialization) if user init'ed/set bindings before we introduced that
if (!ReadIniIfExists("Controller", "PadButtonsInited", &ControlsManager.ms_padButtonsInited)) {
ControlsManager.ms_padButtonsInited = cfg.category_size("Bindings") != 0 ? 16 : 0;
ControlsManager.ms_padButtonsInited = cfg.get("Bindings").size() != 0 ? 16 : 0;
}

for (int32 i = 0; i < MAX_CONTROLLERACTIONS; i++) {
Expand Down Expand Up @@ -456,12 +466,13 @@ void SaveINIControllerSettings()
#endif
#endif
StoreIni("Controller", "PadButtonsInited", ControlsManager.ms_padButtonsInited);
cfg.write_file("re3.ini");

ini.write(cfg);
}

bool LoadINISettings()
{
if (!cfg.load_file("re3.ini"))
if (!ini.read(cfg))
return false;

#ifdef IMPROVED_VIDEOMODE
Expand Down Expand Up @@ -527,7 +538,7 @@ bool LoadINISettings()
#endif

#ifdef CUSTOM_FRONTEND_OPTIONS
bool migrate = cfg.category_size("FrontendOptions") != 0;
bool migrate = cfg.get("FrontendOptions").size() != 0;
for (int i = 0; i < MENUPAGES; i++) {
for (int j = 0; j < NUM_MENUROWS; j++) {
CMenuScreenCustom::CMenuEntry &option = aScreens[i].m_aEntries[j];
Expand All @@ -539,7 +550,7 @@ bool LoadINISettings()
// Migrate from old .ini to new .ini
// Old values can only be int8, new ones can contain float if it is slider
if (migrate && ReadIniIfExists("FrontendOptions", option.m_CFO->save, (int8*)option.m_CFO->value))
cfg.remove("FrontendOptions", option.m_CFO->save);
cfg["FrontendOptions"].remove(option.m_CFO->save);
else if (option.m_Action == MENUACTION_CFO_SLIDER)
ReadIniIfExists(option.m_CFO->saveCat, option.m_CFO->save, (float*)option.m_CFO->value);
else
Expand Down Expand Up @@ -642,7 +653,7 @@ void SaveINISettings()
}
#endif

cfg.write_file("re3.ini");
ini.write(cfg);
}

#endif
Expand Down
Loading

0 comments on commit 3515363

Please sign in to comment.