From 79bcc96d9b095d2c905b57dfaba3fc6ec0e20b59 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Fri, 13 Mar 2020 03:21:26 +0100 Subject: [PATCH] config: Use localStorage on Emscripten --- src/config.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/config.c b/src/config.c index c7ea736..942c16c 100644 --- a/src/config.c +++ b/src/config.c @@ -47,10 +47,31 @@ static void StoreConfig(struct Game* game) { SYMBOL_EXPORT void SetConfigOption(struct Game* game, char* section, char* name, char* value) { al_set_config_value(game->_priv.config, section, name, value); +#ifdef __EMSCRIPTEN__ + EM_ASM({ + var key = '[' + UTF8ToString($0) + '] ' + UTF8ToString($1); + window.localStorage.setItem(key, UTF8ToString($2)); + }, + section, name, value); +#endif StoreConfig(game); } SYMBOL_EXPORT const char* GetConfigOption(struct Game* game, char* section, char* name) { +#ifdef __EMSCRIPTEN__ + char* buf = (char*)EM_ASM_INT({ + var key = '[' + UTF8ToString($0) + '] ' + UTF8ToString($1); + var value = window.localStorage.getItem(key); + if (value) { + var buf = _malloc(255); + stringToUTF8(value, buf, 255); + return buf; + } + return 0; + }, + section, name); + return buf ? AddGarbage(game, buf) : NULL; +#endif return al_get_config_value(game->_priv.config, section, name); } @@ -64,6 +85,13 @@ SYMBOL_EXPORT const char* GetConfigOptionDefault(struct Game* game, char* sectio SYMBOL_EXPORT void DeleteConfigOption(struct Game* game, char* section, char* name) { al_remove_config_key(game->_priv.config, section, name); +#ifdef __EMSCRIPTEN__ + EM_ASM({ + var key = '[' + UTF8ToString($0) + '] ' + UTF8ToString($1); + window.localStorage.removeItem(key); + }, + section, name); +#endif StoreConfig(game); }