From cecb145bbbec99bbd2992872aec54a0f80547b24 Mon Sep 17 00:00:00 2001 From: MysterD Date: Wed, 2 Feb 2022 20:24:35 -0800 Subject: [PATCH] Added network utils functions to Lua --- autogen/common.py | 2 +- autogen/convert_functions.py | 7 ++- autogen/lua_functions/network_utils.h | 3 ++ docs/lua/functions.md | 71 +++++++++++++++++++++++++++ src/pc/lua/smlua_cobject.c | 2 + src/pc/lua/smlua_cobject.h | 1 + src/pc/lua/smlua_cobject_autogen.c | 40 +++++++-------- src/pc/lua/smlua_functions_autogen.c | 41 ++++++++++++++++ src/pc/lua/smlua_sync_table.c | 5 +- src/pc/lua/smlua_utils.c | 10 ++++ src/pc/lua/smlua_utils.h | 2 +- src/pc/network/network.h | 1 + src/pc/network/network_player.c | 20 -------- src/pc/network/network_player.h | 3 -- src/pc/network/network_utils.c | 25 ++++++++++ src/pc/network/network_utils.h | 12 +++++ 16 files changed, 195 insertions(+), 50 deletions(-) create mode 100644 autogen/lua_functions/network_utils.h create mode 100644 src/pc/network/network_utils.c create mode 100644 src/pc/network/network_utils.h diff --git a/autogen/common.py b/autogen/common.py index 470743437..db7068639 100644 --- a/autogen/common.py +++ b/autogen/common.py @@ -20,7 +20,7 @@ def translate_type_to_lvt(ptype): if 'enum ' in ptype: return 'LVT_S32' if ptype == 'bool': - return 'LVT_U8' + return 'LVT_BOOL' if ptype in usf_types: return 'LVT_' + ptype.upper() if ptype in vec3_types: diff --git a/autogen/convert_functions.py b/autogen/convert_functions.py index 35dfec434..2e2e88288 100644 --- a/autogen/convert_functions.py +++ b/autogen/convert_functions.py @@ -25,6 +25,7 @@ template = """/* THIS FILE IS AUTOGENERATED */ #include "object_fields.h" #include "engine/math_util.h" #include "engine/surface_collision.h" +#include "pc/network/network_utils.h" $[FUNCTIONS] @@ -100,8 +101,6 @@ def normalize_type(t): def alter_type(t): if t.startswith('enum '): return 'int' - if t == 'bool': - return 'u8' return t @@ -113,6 +112,8 @@ def build_param(param, i): if ptype in param_override_build: return param_override_build[ptype]['before'].replace('$[IDENTIFIER]', str(pid)).replace('$[INDEX]', str(i)) + elif ptype == 'bool': + return ' %s %s = smlua_to_boolean(L, %d);\n' % (ptype, pid, i) elif ptype in integer_types: return ' %s %s = smlua_to_integer(L, %d);\n' % (ptype, pid, i) elif ptype in number_types: @@ -151,6 +152,8 @@ def build_call(function): lfunc = 'lua_pushinteger' elif ftype in number_types: lfunc = 'lua_pushnumber' + elif ftype == 'bool': + lfunc = 'lua_pushboolean' return ' %s(L, %s);\n' % (lfunc, ccall) diff --git a/autogen/lua_functions/network_utils.h b/autogen/lua_functions/network_utils.h new file mode 100644 index 000000000..da17db1e4 --- /dev/null +++ b/autogen/lua_functions/network_utils.h @@ -0,0 +1,3 @@ +bool network_is_server(void); +u8 network_global_index_from_local(u8 localIndex); +u8 network_local_index_from_global(u8 globalIndex); diff --git a/docs/lua/functions.md b/docs/lua/functions.md index e063e38ea..8701e127d 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -209,6 +209,13 @@
+- network_utils.h + - [network_global_index_from_local](#network_global_index_from_local) + - [network_is_server](#network_is_server) + - [network_local_index_from_global](#network_local_index_from_global) + +
+ - surface_collision.h - [f32_find_wall_collision](#f32_find_wall_collision) - [find_ceil](#find_ceil) @@ -3538,6 +3545,70 @@
+--- +# functions from network_utils.h + +
+ + +## [network_global_index_from_local](#network_global_index_from_local) + +### Lua Example +`local integerValue = network_global_index_from_local(localIndex)` + +### Parameters +| Field | Type | +| ----- | ---- | +| localIndex | integer | + +### Returns +- integer + +### C Prototype +`u8 network_global_index_from_local(u8 localIndex);` + +[:arrow_up_small:](#) + +
+ +## [network_is_server](#network_is_server) + +### Lua Example +`local boolValue = network_is_server()` + +### Parameters +- None + +### Returns +- bool + +### C Prototype +`bool network_is_server(void);` + +[:arrow_up_small:](#) + +
+ +## [network_local_index_from_global](#network_local_index_from_global) + +### Lua Example +`local integerValue = network_local_index_from_global(globalIndex)` + +### Parameters +| Field | Type | +| ----- | ---- | +| globalIndex | integer | + +### Returns +- integer + +### C Prototype +`u8 network_local_index_from_global(u8 globalIndex);` + +[:arrow_up_small:](#) + +
+ --- # functions from surface_collision.h diff --git a/src/pc/lua/smlua_cobject.c b/src/pc/lua/smlua_cobject.c index e5ac3dcff..59ced761a 100644 --- a/src/pc/lua/smlua_cobject.c +++ b/src/pc/lua/smlua_cobject.c @@ -82,6 +82,7 @@ static int smlua__get_field(lua_State* L) { u8* p = ((u8*)pointer) + data->valueOffset; switch (data->valueType) { + case LVT_BOOL: lua_pushboolean(L, *(u8* )p); break; case LVT_U8: lua_pushinteger(L, *(u8* )p); break; case LVT_U16: lua_pushinteger(L, *(u16*)p); break; case LVT_U32: lua_pushinteger(L, *(u32*)p); break; @@ -139,6 +140,7 @@ static int smlua__set_field(lua_State* L) { u8* p = ((u8*)pointer) + data->valueOffset; switch (data->valueType) { + case LVT_BOOL:*(u8*) p = smlua_to_boolean(L, -1); break; case LVT_U8: *(u8*) p = smlua_to_integer(L, -1); break; case LVT_U16: *(u16*)p = smlua_to_integer(L, -1); break; case LVT_U32: *(u32*)p = smlua_to_integer(L, -1); break; diff --git a/src/pc/lua/smlua_cobject.h b/src/pc/lua/smlua_cobject.h index 05a8e5730..8a9ce98cf 100644 --- a/src/pc/lua/smlua_cobject.h +++ b/src/pc/lua/smlua_cobject.h @@ -2,6 +2,7 @@ #define SMLUA_COBJECT_H enum LuaValueType { + LVT_BOOL, LVT_U8, LVT_U16, LVT_U32, diff --git a/src/pc/lua/smlua_cobject_autogen.c b/src/pc/lua/smlua_cobject_autogen.c index 9bf0d8699..c022666cf 100644 --- a/src/pc/lua/smlua_cobject_autogen.c +++ b/src/pc/lua/smlua_cobject_autogen.c @@ -436,26 +436,26 @@ static struct LuaObjectField sModeTransitionInfoFields[LUA_MODE_TRANSITION_INFO_ #define LUA_NETWORK_PLAYER_FIELD_COUNT 17 static struct LuaObjectField sNetworkPlayerFields[LUA_NETWORK_PLAYER_FIELD_COUNT] = { - { "connected", LVT_U8, offsetof(struct NetworkPlayer, connected), true, LOT_NONE }, - { "currActNum", LVT_S16, offsetof(struct NetworkPlayer, currActNum), true, LOT_NONE }, - { "currAreaIndex", LVT_S16, offsetof(struct NetworkPlayer, currAreaIndex), true, LOT_NONE }, - { "currAreaSyncValid", LVT_U8, offsetof(struct NetworkPlayer, currAreaSyncValid), true, LOT_NONE }, - { "currCourseNum", LVT_S16, offsetof(struct NetworkPlayer, currCourseNum), true, LOT_NONE }, - { "currLevelAreaSeqId", LVT_U16, offsetof(struct NetworkPlayer, currLevelAreaSeqId), true, LOT_NONE }, - { "currLevelNum", LVT_S16, offsetof(struct NetworkPlayer, currLevelNum), true, LOT_NONE }, - { "currLevelSyncValid", LVT_U8, offsetof(struct NetworkPlayer, currLevelSyncValid), true, LOT_NONE }, - { "fadeOpacity", LVT_U8, offsetof(struct NetworkPlayer, fadeOpacity), true, LOT_NONE }, - { "globalIndex", LVT_U8, offsetof(struct NetworkPlayer, globalIndex), true, LOT_NONE }, - { "lastReceived", LVT_F32, offsetof(struct NetworkPlayer, lastReceived), true, LOT_NONE }, - { "lastSent", LVT_F32, offsetof(struct NetworkPlayer, lastSent), true, LOT_NONE }, - { "localIndex", LVT_U8, offsetof(struct NetworkPlayer, localIndex), true, LOT_NONE }, - { "modelIndex", LVT_U8, offsetof(struct NetworkPlayer, modelIndex), true, LOT_NONE }, -// { "name", LOT_???, offsetof(struct NetworkPlayer, name), true, LOT_??? }, <--- UNIMPLEMENTED - { "onRxSeqId", LVT_U8, offsetof(struct NetworkPlayer, onRxSeqId), true, LOT_NONE }, - { "paletteIndex", LVT_U8, offsetof(struct NetworkPlayer, paletteIndex), true, LOT_NONE }, -// { "rxPacketHash", LOT_???, offsetof(struct NetworkPlayer, rxPacketHash), true, LOT_??? }, <--- UNIMPLEMENTED -// { "rxSeqIds", LOT_???, offsetof(struct NetworkPlayer, rxSeqIds), true, LOT_??? }, <--- UNIMPLEMENTED - { "type", LVT_U8, offsetof(struct NetworkPlayer, type), true, LOT_NONE }, + { "connected", LVT_BOOL, offsetof(struct NetworkPlayer, connected), true, LOT_NONE }, + { "currActNum", LVT_S16, offsetof(struct NetworkPlayer, currActNum), true, LOT_NONE }, + { "currAreaIndex", LVT_S16, offsetof(struct NetworkPlayer, currAreaIndex), true, LOT_NONE }, + { "currAreaSyncValid", LVT_BOOL, offsetof(struct NetworkPlayer, currAreaSyncValid), true, LOT_NONE }, + { "currCourseNum", LVT_S16, offsetof(struct NetworkPlayer, currCourseNum), true, LOT_NONE }, + { "currLevelAreaSeqId", LVT_U16, offsetof(struct NetworkPlayer, currLevelAreaSeqId), true, LOT_NONE }, + { "currLevelNum", LVT_S16, offsetof(struct NetworkPlayer, currLevelNum), true, LOT_NONE }, + { "currLevelSyncValid", LVT_BOOL, offsetof(struct NetworkPlayer, currLevelSyncValid), true, LOT_NONE }, + { "fadeOpacity", LVT_U8, offsetof(struct NetworkPlayer, fadeOpacity), true, LOT_NONE }, + { "globalIndex", LVT_U8, offsetof(struct NetworkPlayer, globalIndex), true, LOT_NONE }, + { "lastReceived", LVT_F32, offsetof(struct NetworkPlayer, lastReceived), true, LOT_NONE }, + { "lastSent", LVT_F32, offsetof(struct NetworkPlayer, lastSent), true, LOT_NONE }, + { "localIndex", LVT_U8, offsetof(struct NetworkPlayer, localIndex), true, LOT_NONE }, + { "modelIndex", LVT_U8, offsetof(struct NetworkPlayer, modelIndex), true, LOT_NONE }, +// { "name", LOT_???, offsetof(struct NetworkPlayer, name), true, LOT_??? }, <--- UNIMPLEMENTED + { "onRxSeqId", LVT_U8, offsetof(struct NetworkPlayer, onRxSeqId), true, LOT_NONE }, + { "paletteIndex", LVT_U8, offsetof(struct NetworkPlayer, paletteIndex), true, LOT_NONE }, +// { "rxPacketHash", LOT_???, offsetof(struct NetworkPlayer, rxPacketHash), true, LOT_??? }, <--- UNIMPLEMENTED +// { "rxSeqIds", LOT_???, offsetof(struct NetworkPlayer, rxSeqIds), true, LOT_??? }, <--- UNIMPLEMENTED + { "type", LVT_U8, offsetof(struct NetworkPlayer, type), true, LOT_NONE }, }; #define LUA_OBJECT_FIELD_COUNT 22 diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index 9cdadceae..9a9b2d9e6 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -12,6 +12,7 @@ #include "object_fields.h" #include "engine/math_util.h" #include "engine/surface_collision.h" +#include "pc/network/network_utils.h" ////////////// // camera.h // @@ -2258,6 +2259,41 @@ int smlua_func_stop_and_set_height_to_floor(lua_State* L) { return 1; } + ///////////////////// + // network_utils.h // +///////////////////// + +int smlua_func_network_global_index_from_local(lua_State* L) { + if(!smlua_functions_valid_param_count(L, 1)) { return 0; } + + u8 localIndex = smlua_to_integer(L, 1); + if (!gSmLuaConvertSuccess) { return 0; } + + lua_pushinteger(L, network_global_index_from_local(localIndex)); + + return 1; +} + +int smlua_func_network_is_server(UNUSED lua_State* L) { + if(!smlua_functions_valid_param_count(L, 0)) { return 0; } + + + lua_pushboolean(L, network_is_server()); + + return 1; +} + +int smlua_func_network_local_index_from_global(lua_State* L) { + if(!smlua_functions_valid_param_count(L, 1)) { return 0; } + + u8 globalIndex = smlua_to_integer(L, 1); + if (!gSmLuaConvertSuccess) { return 0; } + + lua_pushinteger(L, network_local_index_from_global(globalIndex)); + + return 1; +} + ///////////////////////// // surface_collision.h // ///////////////////////// @@ -2680,6 +2716,11 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "stationary_ground_step", smlua_func_stationary_ground_step); smlua_bind_function(L, "stop_and_set_height_to_floor", smlua_func_stop_and_set_height_to_floor); + // network_utils.h + smlua_bind_function(L, "network_global_index_from_local", smlua_func_network_global_index_from_local); + smlua_bind_function(L, "network_is_server", smlua_func_network_is_server); + smlua_bind_function(L, "network_local_index_from_global", smlua_func_network_local_index_from_global); + // surface_collision.h //smlua_bind_function(L, "f32_find_wall_collision", smlua_func_f32_find_wall_collision); <--- UNIMPLEMENTED //smlua_bind_function(L, "find_ceil", smlua_func_find_ceil); <--- UNIMPLEMENTED diff --git a/src/pc/lua/smlua_sync_table.c b/src/pc/lua/smlua_sync_table.c index 41f4f2150..3c0623b15 100644 --- a/src/pc/lua/smlua_sync_table.c +++ b/src/pc/lua/smlua_sync_table.c @@ -1,7 +1,6 @@ #include "smlua.h" #include "pc/mod_list.h" #include "pc/network/network.h" -#include "pc/network/network_player.h" #define MAX_UNWOUND_SIZE 256 static struct LSTNetworkType sUnwoundLnts[MAX_UNWOUND_LNT] = { 0 }; @@ -70,7 +69,7 @@ static bool smlua_sync_table_unwind(int syncTableIndex, int keyIndex) { if (lst == LST_PLAYER) { struct LSTNetworkType* n = &sUnwoundLnts[sUnwoundLntsCount - 1]; assert(n->type == LST_NETWORK_TYPE_INTEGER); - n->value.integer = network_player_global_index_from_local(n->value.integer); + n->value.integer = network_global_index_from_local(n->value.integer); } lua_pop(L, 1); // pop _name value @@ -320,7 +319,7 @@ void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 ln if (smlua_get_integer_field(-1, "_type") == LST_PLAYER) { lua_pop(L, 1); // pop wrong table assert(lntKeys[i].type == LST_NETWORK_TYPE_INTEGER); - lua_pushinteger(L, network_player_local_index_from_global(lntKeys[i].value.integer)); + lua_pushinteger(L, network_local_index_from_global(lntKeys[i].value.integer)); lua_gettable(L, -2); } } diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index 05d7a14ba..a0ac3fe53 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -39,6 +39,16 @@ bool smlua_is_table_empty(int index) { /////////////////////////////////////////////////////////////////////////////////////////// +bool smlua_to_boolean(lua_State* L, int index) { + if (lua_type(L, index) != LUA_TBOOLEAN) { + LOG_LUA("smlua_to_boolean received improper type '%d'", lua_type(L, index)); + smlua_logline(); + gSmLuaConvertSuccess = false; + return 0; + } + return lua_toboolean(L, index) ? true : false; +} + lua_Integer smlua_to_integer(lua_State* L, int index) { if (lua_type(L, index) == LUA_TBOOLEAN) { return lua_toboolean(L, index) ? 1 : 0; diff --git a/src/pc/lua/smlua_utils.h b/src/pc/lua/smlua_utils.h index fa1098c9f..23f062c96 100644 --- a/src/pc/lua/smlua_utils.h +++ b/src/pc/lua/smlua_utils.h @@ -9,7 +9,7 @@ s16* smlua_get_vec3s_from_buffer(void); void smlua_bind_function(lua_State* L, const char* name, void* func); bool smlua_is_table_empty(int index); - +bool smlua_to_boolean(lua_State* L, int index); lua_Integer smlua_to_integer(lua_State* L, int index); lua_Number smlua_to_number(lua_State* L, int index); const char* smlua_to_string(lua_State* L, int index); diff --git a/src/pc/network/network.h b/src/pc/network/network.h index c97a2afee..fbfd719e8 100644 --- a/src/pc/network/network.h +++ b/src/pc/network/network.h @@ -6,6 +6,7 @@ #include #include #include "network_player.h" +#include "network_utils.h" #include "packets/packet.h" #include "pc/utils/string_linked_list.h" #include "../cliopts.h" diff --git a/src/pc/network/network_player.c b/src/pc/network/network_player.c index b2ebd84eb..71064bc84 100644 --- a/src/pc/network/network_player.c +++ b/src/pc/network/network_player.c @@ -42,26 +42,6 @@ u8 network_player_connected_count(void) { return count; } -u8 network_player_global_index_from_local(u8 localIndex) { - if (gNetworkType == NT_SERVER) { return localIndex; } - - if (gNetworkPlayerLocal == NULL) { return UNKNOWN_GLOBAL_INDEX; } - if (localIndex == 0) { return gNetworkPlayerLocal->globalIndex; } // me - if (localIndex == 1) { return 0; } // server - - return localIndex - ((localIndex <= gNetworkPlayerLocal->globalIndex) ? 1 : 0); -} - -u8 network_player_local_index_from_global(u8 globalIndex) { - if (gNetworkType == NT_SERVER) { return globalIndex; } - - if (gNetworkPlayerLocal == NULL) { return UNKNOWN_LOCAL_INDEX; } - if (gNetworkPlayerLocal->globalIndex == globalIndex) { return 0; } // me - if (globalIndex == 0) { return 1; } // server - - return globalIndex + ((globalIndex < gNetworkPlayerLocal->globalIndex) ? 1 : 0); -} - struct NetworkPlayer* network_player_from_global_index(u8 globalIndex) { for (int i = 0; i < MAX_PLAYERS; i++) { if (!gNetworkPlayers[i].connected) { continue; } diff --git a/src/pc/network/network_player.h b/src/pc/network/network_player.h index 5f000607f..b5bf10ba2 100644 --- a/src/pc/network/network_player.h +++ b/src/pc/network/network_player.h @@ -50,9 +50,6 @@ void network_player_update_model(u8 localIndex); bool network_player_any_connected(void); u8 network_player_connected_count(void); -u8 network_player_global_index_from_local(u8 localIndex); -u8 network_player_local_index_from_global(u8 globalIndex); - struct NetworkPlayer* network_player_from_global_index(u8 globalIndex); struct NetworkPlayer* get_network_player_from_level(s16 courseNum, s16 actNum, s16 levelNum); struct NetworkPlayer* get_network_player_from_area(s16 courseNum, s16 actNum, s16 levelNum, s16 areaIndex); diff --git a/src/pc/network/network_utils.c b/src/pc/network/network_utils.c new file mode 100644 index 000000000..fd51c2897 --- /dev/null +++ b/src/pc/network/network_utils.c @@ -0,0 +1,25 @@ +#include "network_utils.h" + +u8 network_global_index_from_local(u8 localIndex) { + if (gNetworkType == NT_SERVER) { return localIndex; } + + if (gNetworkPlayerLocal == NULL) { return UNKNOWN_GLOBAL_INDEX; } + if (localIndex == 0) { return gNetworkPlayerLocal->globalIndex; } // me + if (localIndex == 1) { return 0; } // server + + return localIndex - ((localIndex <= gNetworkPlayerLocal->globalIndex) ? 1 : 0); +} + +u8 network_local_index_from_global(u8 globalIndex) { + if (gNetworkType == NT_SERVER) { return globalIndex; } + + if (gNetworkPlayerLocal == NULL) { return UNKNOWN_LOCAL_INDEX; } + if (gNetworkPlayerLocal->globalIndex == globalIndex) { return 0; } // me + if (globalIndex == 0) { return 1; } // server + + return globalIndex + ((globalIndex < gNetworkPlayerLocal->globalIndex) ? 1 : 0); +} + +bool network_is_server(void) { + return gNetworkType == NT_SERVER; +} diff --git a/src/pc/network/network_utils.h b/src/pc/network/network_utils.h new file mode 100644 index 000000000..3be574622 --- /dev/null +++ b/src/pc/network/network_utils.h @@ -0,0 +1,12 @@ +#ifndef NETWORK_UTILS_H +#define NETWORK_UTILS_H + +#include +#include "network.h" + +u8 network_global_index_from_local(u8 localIndex); +u8 network_local_index_from_global(u8 globalIndex); + +bool network_is_server(void); + +#endif \ No newline at end of file