mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-12 09:06:55 -04:00
Simplify options saving/loading code
This commit is contained in:
parent
a0f4171209
commit
f55a55b3c6
@ -624,10 +624,7 @@ void Game_Free(void* obj) {
|
|||||||
|
|
||||||
Logger_WarnFunc = Logger_DialogWarn;
|
Logger_WarnFunc = Logger_DialogWarn;
|
||||||
Gfx_Free();
|
Gfx_Free();
|
||||||
|
Options_SaveIfChanged();
|
||||||
if (!Options_ChangedCount()) return;
|
|
||||||
Options_Load();
|
|
||||||
Options_Save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define Game_DoFrameBody() \
|
#define Game_DoFrameBody() \
|
||||||
|
@ -225,7 +225,6 @@ static struct ChooseModeScreen {
|
|||||||
|
|
||||||
CC_NOINLINE static void ChooseMode_Click(cc_bool classic, cc_bool classicHacks) {
|
CC_NOINLINE static void ChooseMode_Click(cc_bool classic, cc_bool classicHacks) {
|
||||||
Launcher_ClassicBackground = classic;
|
Launcher_ClassicBackground = classic;
|
||||||
Options_Load();
|
|
||||||
Options_SetBool(OPT_CLASSIC_MODE, classic);
|
Options_SetBool(OPT_CLASSIC_MODE, classic);
|
||||||
if (classic) Options_SetBool(OPT_CLASSIC_HACKS, classicHacks);
|
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_SERVER_TEXTURES, !classic);
|
||||||
Options_SetBool(OPT_CLASSIC_TABLIST, classic);
|
Options_SetBool(OPT_CLASSIC_TABLIST, classic);
|
||||||
Options_SetBool(OPT_CLASSIC_OPTIONS, classic);
|
Options_SetBool(OPT_CLASSIC_OPTIONS, classic);
|
||||||
Options_Save();
|
|
||||||
|
|
||||||
|
Options_SaveIfChanged();
|
||||||
Launcher_SetScreen(MainScreen_MakeInstance());
|
Launcher_SetScreen(MainScreen_MakeInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -520,7 +519,7 @@ static void DirectConnectScreen_Load(struct DirectConnectScreen* s) {
|
|||||||
String addr; char addrBuffer[STRING_SIZE];
|
String addr; char addrBuffer[STRING_SIZE];
|
||||||
String mppass; char mppassBuffer[STRING_SIZE];
|
String mppass; char mppassBuffer[STRING_SIZE];
|
||||||
String user, ip, port;
|
String user, ip, port;
|
||||||
Options_Load();
|
Options_Reload();
|
||||||
|
|
||||||
Options_UNSAFE_Get("launcher-dc-username", &user);
|
Options_UNSAFE_Get("launcher-dc-username", &user);
|
||||||
Options_UNSAFE_Get("launcher-dc-ip", &ip);
|
Options_UNSAFE_Get("launcher-dc-ip", &ip);
|
||||||
@ -539,14 +538,6 @@ static void DirectConnectScreen_Load(struct DirectConnectScreen* s) {
|
|||||||
LInput_SetText(&s->iptMppass, &mppass);
|
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 void DirectConnectScreen_StartClient(void* w, int x, int y) {
|
||||||
static const String loopbackIp = String_FromConst("127.0.0.1");
|
static const String loopbackIp = String_FromConst("127.0.0.1");
|
||||||
static const String defMppass = String_FromConst("(none)");
|
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;
|
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("");
|
DirectConnectScreen_SetStatus("");
|
||||||
Launcher_StartGame(user, mppass, &ip, &port, &String_Empty);
|
Launcher_StartGame(user, mppass, &ip, &port, &String_Empty);
|
||||||
}
|
}
|
||||||
|
@ -94,12 +94,6 @@ static CC_NOINLINE void InitFramebuffer(void) {
|
|||||||
Window_AllocFramebuffer(&Launcher_Framebuffer);
|
Window_AllocFramebuffer(&Launcher_Framebuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static CC_NOINLINE void SaveOptionsIfChanged(void) {
|
|
||||||
if (!Options_ChangedCount()) return;
|
|
||||||
Options_Load();
|
|
||||||
Options_Save();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
*---------------------------------------------------------Event handler---------------------------------------------------*
|
*---------------------------------------------------------Event handler---------------------------------------------------*
|
||||||
@ -290,7 +284,7 @@ void Launcher_Run(void) {
|
|||||||
Thread_Sleep(10);
|
Thread_Sleep(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
SaveOptionsIfChanged();
|
Options_SaveIfChanged();
|
||||||
Launcher_Free();
|
Launcher_Free();
|
||||||
if (Launcher_ShouldUpdate) Launcher_ApplyUpdate();
|
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;
|
if (lastJoin + 1000 > now) return false;
|
||||||
lastJoin = now;
|
lastJoin = now;
|
||||||
|
|
||||||
/* Make sure if the client has changed some settings in the meantime, we keep the changes */
|
|
||||||
Options_Load();
|
|
||||||
|
|
||||||
/* Save resume info */
|
/* Save resume info */
|
||||||
if (server->length) {
|
if (server->length) {
|
||||||
Options_Set("launcher-server", server);
|
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-ip", ip);
|
||||||
Options_Set("launcher-port", port);
|
Options_Set("launcher-port", port);
|
||||||
Options_SetSecure("launcher-mppass", mppass, user);
|
Options_SetSecure("launcher-mppass", mppass, user);
|
||||||
Options_Save();
|
|
||||||
}
|
}
|
||||||
/* Save options BEFORE starting new game process */
|
/* Save options BEFORE starting new game process */
|
||||||
/* Otherwise can have 'file already in use' errors on startup */
|
/* Otherwise can get 'file already in use' errors on startup */
|
||||||
SaveOptionsIfChanged();
|
Options_SaveIfChanged();
|
||||||
|
|
||||||
String_InitArray(args, argsBuffer);
|
String_InitArray(args, argsBuffer);
|
||||||
String_AppendString(&args, user);
|
String_AppendString(&args, user);
|
||||||
|
@ -10,14 +10,12 @@
|
|||||||
struct StringsBuffer Options;
|
struct StringsBuffer Options;
|
||||||
static struct StringsBuffer changedOpts;
|
static struct StringsBuffer changedOpts;
|
||||||
|
|
||||||
int Options_ChangedCount(void) { return changedOpts.count; }
|
|
||||||
|
|
||||||
void Options_Free(void) {
|
void Options_Free(void) {
|
||||||
StringsBuffer_Clear(&Options);
|
StringsBuffer_Clear(&Options);
|
||||||
StringsBuffer_Clear(&changedOpts);
|
StringsBuffer_Clear(&changedOpts);
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_bool Options_HasChanged(const String* key) {
|
static cc_bool HasChanged(const String* key) {
|
||||||
String entry;
|
String entry;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -121,45 +119,48 @@ void Options_SetString(const String* key, const String* value) {
|
|||||||
Options_Save();
|
Options_Save();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (Options_HasChanged(key)) return;
|
if (HasChanged(key)) return;
|
||||||
StringsBuffer_Add(&changedOpts, key);
|
StringsBuffer_Add(&changedOpts, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
static cc_bool Options_LoadFilter(const String* entry) {
|
static cc_bool Options_LoadFilter(const String* entry) {
|
||||||
String key, value;
|
String key, value;
|
||||||
String_UNSAFE_Separate(entry, '=', &key, &value);
|
String_UNSAFE_Separate(entry, '=', &key, &value);
|
||||||
return !Options_HasChanged(&key);
|
return !HasChanged(&key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Options_Load(void) {
|
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;
|
String entry, key, value;
|
||||||
int i;
|
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 */
|
/* Reset all the unchanged options */
|
||||||
for (i = Options.count - 1; i >= 0; i--) {
|
for (i = Options.count - 1; i >= 0; i--) {
|
||||||
entry = StringsBuffer_UNSAFE_Get(&Options, i);
|
entry = StringsBuffer_UNSAFE_Get(&Options, i);
|
||||||
String_UNSAFE_Separate(&entry, '=', &key, &value);
|
String_UNSAFE_Separate(&entry, '=', &key, &value);
|
||||||
|
|
||||||
if (Options_HasChanged(&key)) continue;
|
if (HasChanged(&key)) continue;
|
||||||
StringsBuffer_Remove(&Options, i);
|
StringsBuffer_Remove(&Options, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Load only options which have not changed */
|
/* Load only options which have not changed */
|
||||||
EntryList_Load(&Options, "options.txt", '=', Options_LoadFilter);
|
EntryList_Load(&Options, "options.txt", '=', Options_LoadFilter);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Options_Save(void) {
|
void Options_Save(void) {
|
||||||
EntryList_Save(&Options, "options.txt");
|
EntryList_Save(&Options, "options.txt");
|
||||||
StringsBuffer_Clear(&changedOpts);
|
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) {
|
void Options_SetSecure(const char* opt, const String* src, const String* key) {
|
||||||
char data[2000];
|
char data[2000];
|
||||||
cc_uint8* enc;
|
cc_uint8* enc;
|
||||||
|
@ -69,8 +69,6 @@
|
|||||||
#define OPT_CAMERA_MASS "cameramass"
|
#define OPT_CAMERA_MASS "cameramass"
|
||||||
|
|
||||||
extern struct StringsBuffer Options;
|
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. */
|
/* Frees any memory allocated in storing options. */
|
||||||
void Options_Free(void);
|
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);
|
CC_API void Options_Set(const char* keyRaw, const String* value);
|
||||||
/* Sets value of given option to given string. */
|
/* Sets value of given option to given string. */
|
||||||
CC_API void Options_SetString(const String* key, const String* value);
|
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. */
|
/* Saves all options to disc. */
|
||||||
CC_API void Options_Save(void);
|
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. */
|
/* Attempts to securely encode an option. */
|
||||||
/* NOTE: Not all platforms support secure saving. DO NOT RELY ON THIS BEING SECURE! */
|
/* NOTE: Not all platforms support secure saving. DO NOT RELY ON THIS BEING SECURE! */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user