diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index b88dfa6b7..5295fc836 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -5389,6 +5389,12 @@ function mod_storage_clear() -- ... end +--- @param key string +--- @return boolean +function mod_storage_exists(key) + -- ... +end + --- @param key string --- @return string function mod_storage_load(key) diff --git a/docs/lua/functions-4.md b/docs/lua/functions-4.md index 639e95e22..1139d831c 100644 --- a/docs/lua/functions-4.md +++ b/docs/lua/functions-4.md @@ -1124,6 +1124,26 @@
+## [mod_storage_exists](#mod_storage_exists) + +### Lua Example +`local booleanValue = mod_storage_exists(key)` + +### Parameters +| Field | Type | +| ----- | ---- | +| key | `string` | + +### Returns +- `boolean` + +### C Prototype +`bool mod_storage_exists(const char* key);` + +[:arrow_up_small:](#) + +
+ ## [mod_storage_load](#mod_storage_load) ### Lua Example diff --git a/docs/lua/functions.md b/docs/lua/functions.md index 7c464aa7b..ea6821af1 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1199,6 +1199,7 @@ - mod_storage.h - [mod_storage_clear](functions-4.md#mod_storage_clear) + - [mod_storage_exists](functions-4.md#mod_storage_exists) - [mod_storage_load](functions-4.md#mod_storage_load) - [mod_storage_load_bool](functions-4.md#mod_storage_load_bool) - [mod_storage_load_number](functions-4.md#mod_storage_load_number) diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index 736b5e639..c53a0b66d 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -20756,6 +20756,23 @@ int smlua_func_mod_storage_clear(UNUSED lua_State* L) { return 1; } +int smlua_func_mod_storage_exists(lua_State* L) { + if (L == NULL) { return 0; } + + int top = lua_gettop(L); + if (top != 1) { + LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "mod_storage_exists", 1, top); + return 0; + } + + const char* key = smlua_to_string(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "mod_storage_exists"); return 0; } + + lua_pushboolean(L, mod_storage_exists(key)); + + return 1; +} + int smlua_func_mod_storage_load(lua_State* L) { if (L == NULL) { return 0; } @@ -34556,6 +34573,7 @@ void smlua_bind_functions_autogen(void) { // mod_storage.h smlua_bind_function(L, "mod_storage_clear", smlua_func_mod_storage_clear); + smlua_bind_function(L, "mod_storage_exists", smlua_func_mod_storage_exists); smlua_bind_function(L, "mod_storage_load", smlua_func_mod_storage_load); smlua_bind_function(L, "mod_storage_load_bool", smlua_func_mod_storage_load_bool); smlua_bind_function(L, "mod_storage_load_number", smlua_func_mod_storage_load_number); diff --git a/src/pc/mods/mod_storage.cpp b/src/pc/mods/mod_storage.cpp index 2a41d6f0a..cadf3257e 100644 --- a/src/pc/mods/mod_storage.cpp +++ b/src/pc/mods/mod_storage.cpp @@ -145,6 +145,22 @@ C_FIELD bool mod_storage_load_bool(const char* key) { return !strcmp(value, "true"); } +C_FIELD bool mod_storage_exists(const char* key) { + if (gLuaActiveMod == NULL) { return false; } + if (strlen(key) > MAX_KEY_VALUE_LENGTH) { return false; } + if (!char_valid((char *)key)) { return false; } + + char filename[SYS_MAX_PATH] = { 0 }; + mod_storage_get_filename(filename); + if (!fs_sys_path_exists(filename)) { return false; } + + mINI::INIFile file(filename); + mINI::INIStructure ini; + file.read(ini); + + return ini["storage"].has(key); +} + C_FIELD bool mod_storage_remove(const char* key) { if (gLuaActiveMod == NULL) { return false; } if (strlen(key) > MAX_KEY_VALUE_LENGTH) { return false; } diff --git a/src/pc/mods/mod_storage.h b/src/pc/mods/mod_storage.h index d11de7755..96e42d96b 100644 --- a/src/pc/mods/mod_storage.h +++ b/src/pc/mods/mod_storage.h @@ -20,6 +20,7 @@ const char *mod_storage_load(const char* key); f32 mod_storage_load_number(const char* key); bool mod_storage_load_bool(const char* key); +bool mod_storage_exists(const char* key); bool mod_storage_remove(const char* key); bool mod_storage_clear(void);