diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index d0ecc4f32..48757eaee 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -10247,6 +10247,20 @@ function get_area_update_counter() -- ... end +--- @param initialValue integer +--- @return Pointer_integer +--- Returns a temporary signed 32-bit integer pointer with its value set to `initialValue` +function get_temp_s32_pointer(initialValue) + -- ... +end + +--- @param pointer Pointer_integer +--- @return integer +--- Gets the signed 32-bit integer value from `pointer` +function deref_s32_pointer(pointer) + -- ... +end + --- @param message string --- @param lines integer --- Creates a DJUI popup that is broadcasted to every client diff --git a/docs/lua/functions-6.md b/docs/lua/functions-6.md index 7c1a8f8c6..02daaf59b 100644 --- a/docs/lua/functions-6.md +++ b/docs/lua/functions-6.md @@ -2936,6 +2936,52 @@ Gets the area update counter incremented when objects are updated
+## [get_temp_s32_pointer](#get_temp_s32_pointer) + +### Description +Returns a temporary signed 32-bit integer pointer with its value set to `initialValue` + +### Lua Example +`local PointerValue = get_temp_s32_pointer(initialValue)` + +### Parameters +| Field | Type | +| ----- | ---- | +| initialValue | `integer` | + +### Returns +- `Pointer` <`integer`> + +### C Prototype +`s32* get_temp_s32_pointer(s32 initialValue);` + +[:arrow_up_small:](#) + +
+ +## [deref_s32_pointer](#deref_s32_pointer) + +### Description +Gets the signed 32-bit integer value from `pointer` + +### Lua Example +`local integerValue = deref_s32_pointer(pointer)` + +### Parameters +| Field | Type | +| ----- | ---- | +| pointer | `Pointer` <`integer`> | + +### Returns +- `integer` + +### C Prototype +`s32 deref_s32_pointer(s32* pointer);` + +[:arrow_up_small:](#) + +
+ ## [djui_popup_create_global](#djui_popup_create_global) ### Description diff --git a/docs/lua/functions.md b/docs/lua/functions.md index d655f9dc0..4c70a4c95 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1861,6 +1861,8 @@ - smlua_misc_utils.h - [get_network_area_timer](functions-6.md#get_network_area_timer) - [get_area_update_counter](functions-6.md#get_area_update_counter) + - [get_temp_s32_pointer](functions-6.md#get_temp_s32_pointer) + - [deref_s32_pointer](functions-6.md#deref_s32_pointer) - [djui_popup_create_global](functions-6.md#djui_popup_create_global) - [djui_is_popup_disabled](functions-6.md#djui_is_popup_disabled) - [djui_set_popup_disabled_override](functions-6.md#djui_set_popup_disabled_override) diff --git a/src/pc/lua/smlua_functions.c b/src/pc/lua/smlua_functions.c index e48c5e6f2..b3cce5ec3 100644 --- a/src/pc/lua/smlua_functions.c +++ b/src/pc/lua/smlua_functions.c @@ -1081,7 +1081,7 @@ int smlua_func_gfx_set_command(lua_State* L) { } Gfx* gfx = smlua_to_cobject(L, 1, LOT_GFX); - if (!gSmLuaConvertSuccess) { + if (!gSmLuaConvertSuccess || !gfx) { LOG_LUA("gfx_set_command: Failed to convert parameter %u", 1); return 0; } diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index 66b8fb205..ad367e98b 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -30875,6 +30875,40 @@ int smlua_func_get_area_update_counter(UNUSED lua_State* L) { return 1; } +int smlua_func_get_temp_s32_pointer(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", "get_temp_s32_pointer", 1, top); + return 0; + } + + s32 initialValue = smlua_to_integer(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "get_temp_s32_pointer"); return 0; } + + smlua_push_pointer(L, LVT_S32_P, (void*)get_temp_s32_pointer(initialValue), NULL); + + return 1; +} + +int smlua_func_deref_s32_pointer(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", "deref_s32_pointer", 1, top); + return 0; + } + + s32* pointer = (s32*)smlua_to_cpointer(L, 1, LVT_S32_P); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "deref_s32_pointer"); return 0; } + + lua_pushinteger(L, deref_s32_pointer(pointer)); + + return 1; +} + int smlua_func_djui_popup_create_global(lua_State* L) { if (L == NULL) { return 0; } @@ -35793,6 +35827,8 @@ void smlua_bind_functions_autogen(void) { // smlua_misc_utils.h smlua_bind_function(L, "get_network_area_timer", smlua_func_get_network_area_timer); smlua_bind_function(L, "get_area_update_counter", smlua_func_get_area_update_counter); + smlua_bind_function(L, "get_temp_s32_pointer", smlua_func_get_temp_s32_pointer); + smlua_bind_function(L, "deref_s32_pointer", smlua_func_deref_s32_pointer); smlua_bind_function(L, "djui_popup_create_global", smlua_func_djui_popup_create_global); smlua_bind_function(L, "djui_is_popup_disabled", smlua_func_djui_is_popup_disabled); smlua_bind_function(L, "djui_set_popup_disabled_override", smlua_func_djui_set_popup_disabled_override); diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index 7abd88fe3..97ee0f7c9 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -150,12 +150,10 @@ bool smlua_is_cobject(lua_State* L, int index, UNUSED u16 lot) { void* smlua_to_cobject(lua_State* L, int index, u16 lot) { s32 indexType = lua_type(L, index); + if (indexType == LUA_TNIL) { return NULL; } if (indexType != LUA_TUSERDATA) { - // suppress script errors - if (indexType != LUA_TNIL) { - LOG_LUA_LINE("smlua_to_cobject received improper type '%s'", lua_typename(L, indexType)); - gSmLuaConvertSuccess = false; - } + LOG_LUA_LINE("smlua_to_cobject received improper type '%s'", lua_typename(L, indexType)); + gSmLuaConvertSuccess = false; return NULL; } @@ -179,6 +177,7 @@ void* smlua_to_cobject(lua_State* L, int index, u16 lot) { void* smlua_to_cpointer(lua_State* L, int index, u16 lvt) { s32 indexType = lua_type(L, index); + if (indexType == LUA_TNIL) { return NULL; } if (indexType != LUA_TUSERDATA) { LOG_LUA_LINE("smlua_to_cpointer received improper type '%s'", lua_typename(L, indexType)); gSmLuaConvertSuccess = false; diff --git a/src/pc/lua/utils/smlua_misc_utils.c b/src/pc/lua/utils/smlua_misc_utils.c index 5479bf531..36891e727 100644 --- a/src/pc/lua/utils/smlua_misc_utils.c +++ b/src/pc/lua/utils/smlua_misc_utils.c @@ -51,6 +51,22 @@ u16 get_area_update_counter(void) { /// +s32* get_temp_s32_pointer(s32 initialValue) { + static s32 value = 0; + value = initialValue; + return &value; +} + +s32 deref_s32_pointer(s32* pointer) { + if (pointer == NULL) { + LOG_LUA_LINE("Tried to dereference null pointer!"); + return 0; + } + return *pointer; +} + +/// + void djui_popup_create_global(const char* message, int lines) { djui_popup_create(message, lines); network_send_global_popup(message, lines); diff --git a/src/pc/lua/utils/smlua_misc_utils.h b/src/pc/lua/utils/smlua_misc_utils.h index ad6433f89..ac6721e9b 100644 --- a/src/pc/lua/utils/smlua_misc_utils.h +++ b/src/pc/lua/utils/smlua_misc_utils.h @@ -44,6 +44,11 @@ u32 get_network_area_timer(void); /* |description|Gets the area update counter incremented when objects are updated|descriptionEnd| */ u16 get_area_update_counter(void); +/* |description|Returns a temporary signed 32-bit integer pointer with its value set to `initialValue`|descriptionEnd| */ +s32* get_temp_s32_pointer(s32 initialValue); +/* |description|Gets the signed 32-bit integer value from `pointer`|descriptionEnd| */ +s32 deref_s32_pointer(s32* pointer); + /* |description|Creates a DJUI popup that is broadcasted to every client|descriptionEnd| */ void djui_popup_create_global(const char* message, int lines); /* |description|Returns if popups are disabled|descriptionEnd| */