Simplify options saving/loading code

This commit is contained in:
UnknownShadow200 2020-07-22 19:19:46 +10:00
parent a0f4171209
commit f55a55b3c6
5 changed files with 40 additions and 54 deletions

View File

@ -624,10 +624,7 @@ void Game_Free(void* obj) {
Logger_WarnFunc = Logger_DialogWarn;
Gfx_Free();
if (!Options_ChangedCount()) return;
Options_Load();
Options_Save();
Options_SaveIfChanged();
}
#define Game_DoFrameBody() \

View File

@ -225,7 +225,6 @@ static struct ChooseModeScreen {
CC_NOINLINE static void ChooseMode_Click(cc_bool classic, cc_bool classicHacks) {
Launcher_ClassicBackground = classic;
Options_Load();
Options_SetBool(OPT_CLASSIC_MODE, classic);
if (classic) Options_SetBool(OPT_CLASSIC_HACKS, classicHacks);
@ -235,8 +234,8 @@ CC_NOINLINE static void ChooseMode_Click(cc_bool classic, cc_bool classicHacks)
Options_SetBool(OPT_SERVER_TEXTURES, !classic);
Options_SetBool(OPT_CLASSIC_TABLIST, classic);
Options_SetBool(OPT_CLASSIC_OPTIONS, classic);
Options_Save();
Options_SaveIfChanged();
Launcher_SetScreen(MainScreen_MakeInstance());
}
@ -520,7 +519,7 @@ static void DirectConnectScreen_Load(struct DirectConnectScreen* s) {
String addr; char addrBuffer[STRING_SIZE];
String mppass; char mppassBuffer[STRING_SIZE];
String user, ip, port;
Options_Load();
Options_Reload();
Options_UNSAFE_Get("launcher-dc-username", &user);
Options_UNSAFE_Get("launcher-dc-ip", &ip);
@ -539,14 +538,6 @@ static void DirectConnectScreen_Load(struct DirectConnectScreen* s) {
LInput_SetText(&s->iptMppass, &mppass);
}
static void DirectConnectScreen_Save(const String* user, const String* mppass, const String* ip, const String* port) {
Options_Load();
Options_Set("launcher-dc-username", user);
Options_Set("launcher-dc-ip", ip);
Options_Set("launcher-dc-port", port);
Options_SetSecure("launcher-dc-mppass", mppass, user);
}
static void DirectConnectScreen_StartClient(void* w, int x, int y) {
static const String loopbackIp = String_FromConst("127.0.0.1");
static const String defMppass = String_FromConst("(none)");
@ -578,7 +569,11 @@ static void DirectConnectScreen_StartClient(void* w, int x, int y) {
}
if (!mppass->length) mppass = &defMppass;
DirectConnectScreen_Save(user, mppass, &ip, &port);
Options_Set("launcher-dc-username", user);
Options_Set("launcher-dc-ip", &ip);
Options_Set("launcher-dc-port", &port);
Options_SetSecure("launcher-dc-mppass", mppass, user);
DirectConnectScreen_SetStatus("");
Launcher_StartGame(user, mppass, &ip, &port, &String_Empty);
}

View File

@ -94,12 +94,6 @@ static CC_NOINLINE void InitFramebuffer(void) {
Window_AllocFramebuffer(&Launcher_Framebuffer);
}
static CC_NOINLINE void SaveOptionsIfChanged(void) {
if (!Options_ChangedCount()) return;
Options_Load();
Options_Save();
}
/*########################################################################################################################*
*---------------------------------------------------------Event handler---------------------------------------------------*
@ -290,7 +284,7 @@ void Launcher_Run(void) {
Thread_Sleep(10);
}
SaveOptionsIfChanged();
Options_SaveIfChanged();
Launcher_Free();
if (Launcher_ShouldUpdate) Launcher_ApplyUpdate();
@ -560,9 +554,6 @@ cc_bool Launcher_StartGame(const String* user, const String* mppass, const Strin
if (lastJoin + 1000 > now) return false;
lastJoin = now;
/* Make sure if the client has changed some settings in the meantime, we keep the changes */
Options_Load();
/* Save resume info */
if (server->length) {
Options_Set("launcher-server", server);
@ -570,11 +561,10 @@ cc_bool Launcher_StartGame(const String* user, const String* mppass, const Strin
Options_Set("launcher-ip", ip);
Options_Set("launcher-port", port);
Options_SetSecure("launcher-mppass", mppass, user);
Options_Save();
}
/* Save options BEFORE starting new game process */
/* Otherwise can have 'file already in use' errors on startup */
SaveOptionsIfChanged();
/* Otherwise can get 'file already in use' errors on startup */
Options_SaveIfChanged();
String_InitArray(args, argsBuffer);
String_AppendString(&args, user);

View File

@ -10,14 +10,12 @@
struct StringsBuffer Options;
static struct StringsBuffer changedOpts;
int Options_ChangedCount(void) { return changedOpts.count; }
void Options_Free(void) {
StringsBuffer_Clear(&Options);
StringsBuffer_Clear(&changedOpts);
}
cc_bool Options_HasChanged(const String* key) {
static cc_bool HasChanged(const String* key) {
String entry;
int i;
@ -121,38 +119,35 @@ void Options_SetString(const String* key, const String* value) {
Options_Save();
#endif
if (Options_HasChanged(key)) return;
if (HasChanged(key)) return;
StringsBuffer_Add(&changedOpts, key);
}
static cc_bool Options_LoadFilter(const String* entry) {
String key, value;
String_UNSAFE_Separate(entry, '=', &key, &value);
return !Options_HasChanged(&key);
return !HasChanged(&key);
}
void Options_Load(void) {
static cc_bool inited;
EntryList_Load(&Options, "options-default.txt", '=', NULL);
EntryList_Load(&Options, "options.txt", '=', NULL);
}
void Options_Reload(void) {
String entry, key, value;
int i;
if (!inited) {
EntryList_Load(&Options, "options-default.txt", '=', NULL);
EntryList_Load(&Options, "options.txt", '=', NULL);
inited = true;
} else {
/* Reset all the unchanged options */
for (i = Options.count - 1; i >= 0; i--) {
entry = StringsBuffer_UNSAFE_Get(&Options, i);
String_UNSAFE_Separate(&entry, '=', &key, &value);
if (Options_HasChanged(&key)) continue;
if (HasChanged(&key)) continue;
StringsBuffer_Remove(&Options, i);
}
/* Load only options which have not changed */
EntryList_Load(&Options, "options.txt", '=', Options_LoadFilter);
}
}
void Options_Save(void) {
@ -160,6 +155,12 @@ void Options_Save(void) {
StringsBuffer_Clear(&changedOpts);
}
void Options_SaveIfChanged(void) {
if (!changedOpts.count) return;
Options_Reload();
Options_Save();
}
void Options_SetSecure(const char* opt, const String* src, const String* key) {
char data[2000];
cc_uint8* enc;

View File

@ -69,8 +69,6 @@
#define OPT_CAMERA_MASS "cameramass"
extern struct StringsBuffer Options;
/* Returns the number of options changed via Options_SetXYZ since last save. */
int Options_ChangedCount(void);
/* Frees any memory allocated in storing options. */
void Options_Free(void);
@ -97,10 +95,15 @@ CC_API void Options_SetInt(const char* keyRaw, int value);
CC_API void Options_Set(const char* keyRaw, const String* value);
/* Sets value of given option to given string. */
CC_API void Options_SetString(const String* key, const String* value);
/* Loads options from disc. Leaves options changed in this session alone. */
CC_API void Options_Load(void);
/* Loads options from disc. */
void Options_Load(void);
/* Reloads options from disc, leaving options changed in this session alone. */
CC_API void Options_Reload(void);
/* Saves all options to disc. */
CC_API void Options_Save(void);
/* Saves options to disc, if any were changed via Options_SetXYZ since last save. */
void Options_SaveIfChanged(void);
/* Attempts to securely encode an option. */
/* NOTE: Not all platforms support secure saving. DO NOT RELY ON THIS BEING SECURE! */