From 97fa7b15ea4765e1aeecf63f04bdbb5d13f85595 Mon Sep 17 00:00:00 2001 From: MysterD Date: Mon, 31 Jan 2022 21:32:57 -0800 Subject: [PATCH] Refactoring lua sync table --- autogen/autogen.sh | 1 - autogen/lua_constants/constants.lua | 4 +- src/pc/lua/smlua.h | 2 +- src/pc/lua/smlua_constants_autogen.c | 4 +- src/pc/lua/smlua_sync_table.c | 200 +++++++++++------- src/pc/lua/smlua_sync_table.h | 4 +- src/pc/lua/smlua_utils.c | 24 +++ src/pc/lua/smlua_utils.h | 5 + src/pc/network/packets/packet.h | 30 +-- .../network/packets/packet_lua_sync_table.c | 178 +++++++++------- 10 files changed, 272 insertions(+), 180 deletions(-) diff --git a/autogen/autogen.sh b/autogen/autogen.sh index ff809e82..0cae629f 100644 --- a/autogen/autogen.sh +++ b/autogen/autogen.sh @@ -1,5 +1,4 @@ #!/usr/bin/bash - python3 ./autogen/convert_structs.py python3 ./autogen/convert_functions.py python3 ./autogen/convert_constants.py diff --git a/autogen/lua_constants/constants.lua b/autogen/lua_constants/constants.lua index 6a1b867a..0d54477e 100644 --- a/autogen/lua_constants/constants.lua +++ b/autogen/lua_constants/constants.lua @@ -33,9 +33,7 @@ _SyncTable = { __newindex = function (t,k,v) local _table = rawget(t, '_table') if _table[k] == v then return end - if _set_sync_table_field(t, k, v) ~= 0 then - _table[k] = v - end + _set_sync_table_field(t, k, v) end } diff --git a/src/pc/lua/smlua.h b/src/pc/lua/smlua.h index cad6b1c6..6802c666 100644 --- a/src/pc/lua/smlua.h +++ b/src/pc/lua/smlua.h @@ -23,7 +23,7 @@ #ifdef DEVELOPMENT #define LUA_STACK_CHECK_BEGIN() int __LUA_STACK_TOP = lua_gettop(gLuaState) -#define LUA_STACK_CHECK_END() if (__LUA_STACK_TOP != lua_gettop(gLuaState)) { smlua_dump_stack(); fflush(stdout); } assert(__LUA_STACK_TOP == lua_gettop(gLuaState)) +#define LUA_STACK_CHECK_END() if ((__LUA_STACK_TOP) != lua_gettop(gLuaState)) { smlua_dump_stack(); fflush(stdout); } assert((__LUA_STACK_TOP) == lua_gettop(gLuaState)) #else #define LUA_STACK_CHECK_BEGIN() #define LUA_STACK_CHECK_END() diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index 1de08e1a..b3868b04 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -26,9 +26,7 @@ char gSmluaConstants[] = "HOOK_UPDATE = 0\n" " __newindex = function (t,k,v)\n" " local _table = rawget(t, '_table')\n" " if _table[k] == v then return end\n" -" if _set_sync_table_field(t, k, v) ~= 0 then\n" -" _table[k] = v\n" -" end\n" +" _set_sync_table_field(t, k, v)\n" " end\n" "}\n" "function vec3f_copy(dest, src)\n" diff --git a/src/pc/lua/smlua_sync_table.c b/src/pc/lua/smlua_sync_table.c index 0fb36182..510378fc 100644 --- a/src/pc/lua/smlua_sync_table.c +++ b/src/pc/lua/smlua_sync_table.c @@ -3,70 +3,100 @@ #include "pc/network/network.h" #include "pc/network/network_player.h" -static u8 smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool alterSeq, bool push) { +static bool smlua_value_to_lnt(int index, struct LSTNetworkType* lnt) { + lua_State* L = gLuaState; + int valueType = lua_type(L, index); + + if (valueType == LUA_TNUMBER) { + lnt->type = LST_NETWORK_TYPE_INTEGER; + lnt->value.integer = lua_tointeger(L, index); + + if (lnt->value.integer == 0) { + lnt->type = LST_NETWORK_TYPE_NUMBER; + lnt->value.number = lua_tonumber(L, index); + } + return true; + } + + if (valueType == LUA_TBOOLEAN) { + lnt->type = LST_NETWORK_TYPE_BOOLEAN; + lnt->value.boolean = lua_toboolean(L, index); + return true; + } + + if (valueType == LUA_TSTRING) { + lnt->type = LST_NETWORK_TYPE_STRING; + lnt->value.string = lua_tostring(L, index); + if (lnt->value.string == NULL || strlen(lnt->value.string) > 256) { + LOG_LUA("smlua_value_to_lnt on invalid string value: '%s'", (lnt->value.string == NULL) ? "" : lnt->value.string); + return false; + } + return true; + } + + if (valueType == LUA_TNIL) { + lnt->type = LST_NETWORK_TYPE_NIL; + return true; + } + + return false; +} + +static void smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool alterSeq) { LUA_STACK_CHECK_BEGIN(); lua_State* L = gLuaState; - // get remoteIndex - u16 remoteIndex = smlua_get_integer_field(stackIndex + 1, "_remoteIndex"); + int syncTableIndex = stackIndex + 1; + int keyIndex = stackIndex + 2; + int valueIndex = stackIndex + 3; + + // get modRemoteIndex + u16 modRemoteIndex = smlua_get_integer_field(syncTableIndex, "_remoteIndex"); if (!gSmLuaConvertSuccess) { - LOG_LUA("smlua_sync_table_send_field on invalid remoteIndex: %u", remoteIndex); - if (push) { lua_pushinteger(L, 0); } - return 0; + LOG_LUA("smlua_sync_table_send_field on invalid modRemoteIndex: %u", modRemoteIndex); + return; } // get lst - enum LuaSyncTableType lst = smlua_get_integer_field(stackIndex + 1, "_type"); - if (!gSmLuaConvertSuccess) { return 0; } - if (lst >= LST_MAX) { + enum LuaSyncTableType lst = smlua_get_integer_field(syncTableIndex, "_type"); + if (!gSmLuaConvertSuccess || lst >= LST_MAX) { LOG_LUA("smlua_sync_table_send_field on invalid LST: %u", lst); - if (push) { lua_pushinteger(L, 0); } - return 0; + return; } // get key - const char* key = smlua_to_string(L, stackIndex + 2); - if (!gSmLuaConvertSuccess) { return 0; } - if (key == NULL || strlen(key) == 0 || strlen(key) > 64) { + const char* key = smlua_to_string(L, keyIndex); + if (!gSmLuaConvertSuccess || key == NULL || strlen(key) == 0 || strlen(key) > 64) { LOG_LUA("smlua_sync_table_send_field on invalid key: '%s'", (key == NULL) ? "" : key); - if (push) { lua_pushinteger(L, 0); } - return 0; + return; } + + /////////// + // value // + /////////// + // get value - union LSTNetworkUnion lUnion = { 0 }; - enum LSTNetworkType lUnionType = LST_NETWORK_TYPE_MAX; - int valueType = lua_type(L, stackIndex + 3); - if (valueType == LUA_TNUMBER) { - lUnion.integer = lua_tointeger(L, stackIndex + 3); - lUnionType = LST_NETWORK_TYPE_INTEGER; - - if (lUnion.integer == 0) { - lUnion.number = lua_tonumber(L, stackIndex + 3); - lUnionType = LST_NETWORK_TYPE_NUMBER; - } - - } else if (valueType == LUA_TBOOLEAN) { - lUnion.boolean = lua_toboolean(L, stackIndex + 3); - lUnionType = LST_NETWORK_TYPE_BOOLEAN; - } else if (valueType == LUA_TSTRING) { - lUnion.string = lua_tostring(L, stackIndex + 3); - lUnionType = LST_NETWORK_TYPE_STRING; - if (lUnion.string == NULL || strlen(lUnion.string) > 256) { - LOG_LUA("smlua_sync_table_send_field on invalid string value: '%s'", (lUnion.string == NULL) ? "" : lUnion.string); - if (push) { lua_pushinteger(L, 0); } - return 0; - } - } else if (valueType == LUA_TNIL) { - lUnionType = LST_NETWORK_TYPE_NIL; - } else { - LOG_LUA("smlua_sync_table_send_field on invalid type: '%d'", valueType); - if (push) { lua_pushinteger(L, 0); } - return 0; + struct LSTNetworkType lntValue = { 0 }; + bool validValue = smlua_value_to_lnt(valueIndex, &lntValue); + if (!validValue) { + return; } + // set value + lua_getfield(L, syncTableIndex, "_table"); + lua_pushvalue(L, -3); + lua_pushvalue(L, -3); + lua_settable(L, -3); + lua_pop(L, 1); // pop _table + + + ///////// + // seq // + ///////// + // get seq table - lua_getfield(L, stackIndex + 1, "_seq"); + lua_getfield(L, syncTableIndex, "_seq"); int seqT = lua_gettop(L); // get seq number @@ -81,25 +111,35 @@ static u8 smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool alte } lua_pop(L, 1); + + /////////// + // index // + /////////// + // get index - u16 index = smlua_get_integer_field(stackIndex + 1, "_index"); + u16 index = smlua_get_integer_field(syncTableIndex, "_index"); index = network_player_global_index_from_local(index); + + ///////////// + // network // + ///////////// + + // send over the network if (!gLuaInitializingScript && seq > 0) { - network_send_lua_sync_table(toLocalIndex, seq, remoteIndex, lst, index, key, lUnionType, lUnion); + network_send_lua_sync_table(toLocalIndex, seq, modRemoteIndex, lst, index, key, &lntValue); } LUA_STACK_CHECK_END(); - if (push) { lua_pushinteger(L, 1); } - return 1; } static int smlua__set_sync_table_field(UNUSED lua_State* L) { if (!smlua_functions_valid_param_count(L, 3)) { return 0; } - return smlua_sync_table_send_field(0, 0, true, true); + smlua_sync_table_send_field(0, 0, true); + return 1; } -void smlua_set_sync_table_field_from_network(u64 seq, u16 remoteIndex, u16 lst, u16 index, const char* key, u16 lUnionType, union LSTNetworkUnion lUnion) { +void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 lst, u16 index, const char* key, struct LSTNetworkType* lntValue) { LUA_STACK_CHECK_BEGIN(); lua_State* L = gLuaState; @@ -117,13 +157,13 @@ void smlua_set_sync_table_field_from_network(u64 seq, u16 remoteIndex, u16 lst, // figure out entry struct ModListEntry* entry = NULL; for (int i = 0; i < table->entryCount; i++) { - if (table->entries[i].remoteIndex == remoteIndex) { + if (table->entries[i].remoteIndex == modRemoteIndex) { entry = &table->entries[i]; break; } } if (entry == NULL) { - LOG_ERROR("Could not find mod list entry for remoteIndex: %u", remoteIndex); + LOG_ERROR("Could not find mod list entry for modRemoteIndex: %u", modRemoteIndex); return; } @@ -133,15 +173,15 @@ void smlua_set_sync_table_field_from_network(u64 seq, u16 remoteIndex, u16 lst, return; } - // sanity check lUnionType - if (lUnionType >= LST_NETWORK_TYPE_MAX) { - LOG_ERROR("Received sync table field packet with an invalid lUnionType: %u", lUnionType); + // sanity check lntValue + if (lntValue->type >= LST_NETWORK_TYPE_MAX) { + LOG_ERROR("Received sync table field packet with an invalid lnt type: %u", lntValue->type); return; } lua_getglobal(L, "_G"); // get global table lua_getfield(L, LUA_REGISTRYINDEX, entry->path); // get the file's "global" table - + // get sync table u16 syncTableSize = 0; switch (lst) { @@ -187,25 +227,25 @@ void smlua_set_sync_table_field_from_network(u64 seq, u16 remoteIndex, u16 lst, int t = lua_gettop(L); // set key/value - switch (lUnionType) { + switch (lntValue->type) { case LST_NETWORK_TYPE_INTEGER: lua_pushstring(L, key); - lua_pushinteger(L, lUnion.integer); + lua_pushinteger(L, lntValue->value.integer); lua_rawset(L, t); break; case LST_NETWORK_TYPE_NUMBER: lua_pushstring(L, key); - lua_pushnumber(L, lUnion.number); + lua_pushnumber(L, lntValue->value.number); lua_rawset(L, t); break; case LST_NETWORK_TYPE_BOOLEAN: lua_pushstring(L, key); - lua_pushboolean(L, lUnion.boolean); + lua_pushboolean(L, lntValue->value.boolean); lua_rawset(L, t); break; case LST_NETWORK_TYPE_STRING: lua_pushstring(L, key); - lua_pushstring(L, lUnion.string); + lua_pushstring(L, lntValue->value.string); lua_rawset(L, t); break; case LST_NETWORK_TYPE_NIL: @@ -233,7 +273,7 @@ static void smlua_exec_str(char* str) { LUA_STACK_CHECK_END(); } -void smlua_sync_table_init_globals(char* path, u16 remoteIndex) { +void smlua_sync_table_init_globals(char* path, u16 modRemoteIndex) { LUA_STACK_CHECK_BEGIN(); lua_State* L = gLuaState; @@ -243,20 +283,22 @@ void smlua_sync_table_init_globals(char* path, u16 remoteIndex) { lua_newtable(L); int t = lua_gettop(L); - smlua_push_integer_field(t, "_remoteIndex", remoteIndex); + // push fields + smlua_push_integer_field(t, "_remoteIndex", modRemoteIndex); smlua_push_integer_field(t, "_type", LST_GLOBAL); smlua_push_integer_field(t, "_index", 0); + smlua_push_table_field(t, "_seq"); + smlua_push_table_field(t, "_table"); + smlua_push_string_field(t, "_name", "gGlobalSyncTable"); + smlua_push_nil_field(t, "_parent"); - lua_newtable(L); - lua_setfield(L, t, "_seq"); - - lua_newtable(L); - lua_setfield(L, t, "_table"); - + // attach metatable lua_pushglobaltable(L); lua_getfield(L, -1, "_SyncTable"); lua_setmetatable(L, -3); lua_pop(L, 1); // pop global table + + // attach sync table to file's "globals" lua_setfield(L, base, "gGlobalSyncTable"); } { @@ -268,21 +310,21 @@ void smlua_sync_table_init_globals(char* path, u16 remoteIndex) { lua_newtable(L); int t = lua_gettop(L); - smlua_push_integer_field(t, "_remoteIndex", remoteIndex); + smlua_push_integer_field(t, "_remoteIndex", modRemoteIndex); smlua_push_integer_field(t, "_type", LST_PLAYER); smlua_push_integer_field(t, "_index", i); + smlua_push_table_field(t, "_seq"); + smlua_push_table_field(t, "_table"); + smlua_push_string_field(t, "_name", "gPlayerSyncTable"); // <--- incorrect + smlua_push_nil_field(t, "_parent"); // <--- incorrect - lua_newtable(L); - lua_setfield(L, t, "_seq"); - - lua_newtable(L); - lua_setfield(L, t, "_table"); - + // attach metatable lua_pushglobaltable(L); lua_getfield(L, -1, "_SyncTable"); lua_setmetatable(L, -3); lua_pop(L, 1); // pop global table + // attach table to gPlayerSyncTable lua_settable(L, playerTop); } lua_setfield(L, base, "gPlayerSyncTable"); @@ -318,7 +360,7 @@ static void smlua_sync_table_send_table(u8 toLocalIndex) { lua_pushvalue(L, tableIndex); // insert sync table lua_insert(L, -3); // re-order sync table - smlua_sync_table_send_field(toLocalIndex, internalIndex, false, false); + smlua_sync_table_send_field(toLocalIndex, internalIndex, false); lua_remove(L, -3); // remove sync table diff --git a/src/pc/lua/smlua_sync_table.h b/src/pc/lua/smlua_sync_table.h index 16a5dccc..2a90b823 100644 --- a/src/pc/lua/smlua_sync_table.h +++ b/src/pc/lua/smlua_sync_table.h @@ -7,9 +7,9 @@ enum LuaSyncTableType { LST_MAX, }; -union LSTNetworkUnion; +struct LSTNetworkType; -void smlua_set_sync_table_field_from_network(u64 seq, u16 remoteIndex, u16 lst, u16 index, const char* key, u16 lUnionType, union LSTNetworkUnion lUnion); +void smlua_set_sync_table_field_from_network(u64 seq, u16 remoteIndex, u16 lst, u16 index, const char* key, struct LSTNetworkType* lntValue); void smlua_sync_table_init_globals(char* path, u16 remoteIndex); void smlua_bind_sync_table(void); void smlua_sync_table_send_all(u8 toLocalIndex); diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index bfebe2ea..0b8e97fa 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -151,6 +151,21 @@ void smlua_push_number_field(int index, char* name, lua_Number val) { lua_setfield(gLuaState, index, name); } +void smlua_push_string_field(int index, char* name, const char* val) { + lua_pushstring(gLuaState, val); + lua_setfield(gLuaState, index, name); +} + +void smlua_push_nil_field(int index, char* name) { + lua_pushnil(gLuaState); + lua_setfield(gLuaState, index, name); +} + +void smlua_push_table_field(int index, char* name) { + lua_newtable(gLuaState); + lua_setfield(gLuaState, index, name); +} + lua_Integer smlua_get_integer_field(int index, char* name) { if (lua_type(gLuaState, index) != LUA_TTABLE) { LOG_LUA("smlua_get_integer_field received improper type '%d'", lua_type(gLuaState, index)); @@ -175,6 +190,15 @@ lua_Number smlua_get_number_field(int index, char* name) { return val; } +bool smlua_is_table_empty(int index) { + lua_pushnil(gLuaState); // key + if (lua_next(gLuaState, index)) { + lua_pop(gLuaState, 2); + return false; + } + return true; +} + void smlua_dump_stack(void) { lua_State* L = gLuaState; int top = lua_gettop(L); diff --git a/src/pc/lua/smlua_utils.h b/src/pc/lua/smlua_utils.h index b7162c68..79493f54 100644 --- a/src/pc/lua/smlua_utils.h +++ b/src/pc/lua/smlua_utils.h @@ -17,10 +17,15 @@ void* smlua_to_cobject(lua_State* L, int index, u16 lot); void smlua_push_object(lua_State* L, u16 lot, void* p); void smlua_push_integer_field(int index, char* name, lua_Integer val); void smlua_push_number_field(int index, char* name, lua_Number val); +void smlua_push_string_field(int index, char* name, const char* val); +void smlua_push_nil_field(int index, char* name); +void smlua_push_table_field(int index, char* name); lua_Integer smlua_get_integer_field(int index, char* name); lua_Number smlua_get_number_field(int index, char* name); +bool smlua_is_table_empty(int index); + void smlua_dump_stack(void); void smlua_dump_globals(void); void smlua_dump_table(int index); diff --git a/src/pc/network/packets/packet.h b/src/pc/network/packets/packet.h index 6cc0fa3b..7d9fcff3 100644 --- a/src/pc/network/packets/packet.h +++ b/src/pc/network/packets/packet.h @@ -104,20 +104,22 @@ enum KickReasonType { EKT_FULL_PARTY, }; -union LSTNetworkUnion { - long long integer; - double number; - u8 boolean; - const char* string; -}; +struct LSTNetworkType { + enum { + LST_NETWORK_TYPE_INTEGER, + LST_NETWORK_TYPE_NUMBER, + LST_NETWORK_TYPE_BOOLEAN, + LST_NETWORK_TYPE_STRING, + LST_NETWORK_TYPE_NIL, + LST_NETWORK_TYPE_MAX + } type; -enum LSTNetworkType { - LST_NETWORK_TYPE_INTEGER, - LST_NETWORK_TYPE_NUMBER, - LST_NETWORK_TYPE_BOOLEAN, - LST_NETWORK_TYPE_STRING, - LST_NETWORK_TYPE_NIL, - LST_NETWORK_TYPE_MAX + union { + long long integer; + double number; + u8 boolean; + const char* string; + } value; }; // packet.c @@ -327,7 +329,7 @@ void network_receive_download(struct Packet* p); // packet_lua_sync_table.c void network_send_lua_sync_table_request(void); void network_receive_lua_sync_table_request(struct Packet* p); -void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 remoteIndex, u16 lst, u16 index, const char* key, enum LSTNetworkType lUnionType, union LSTNetworkUnion lUnion); +void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 remoteIndex, u16 lst, u16 index, const char* key, struct LSTNetworkType* lntValue); void network_receive_lua_sync_table(struct Packet* p); #endif diff --git a/src/pc/network/packets/packet_lua_sync_table.c b/src/pc/network/packets/packet_lua_sync_table.c index 21223fb4..bedda45d 100644 --- a/src/pc/network/packets/packet_lua_sync_table.c +++ b/src/pc/network/packets/packet_lua_sync_table.c @@ -5,6 +5,8 @@ static char sLuaStrValue[257] = { 0 }; +///////////////////////////////////////////////////////////// + void network_send_lua_sync_table_request(void) { SOFT_ASSERT(gNetworkType == NT_CLIENT); struct Packet p = { 0 }; @@ -20,54 +22,110 @@ void network_receive_lua_sync_table_request(struct Packet* p) { LOG_INFO("received lua sync table request"); } -void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 remoteIndex, u16 lst, u16 index, const char* key, enum LSTNetworkType lUnionType, union LSTNetworkUnion lUnion) { +///////////////////////////////////////////////////////////// + +static bool packet_write_lnt(struct Packet* p, struct LSTNetworkType* lnt) { + u16 lntType = lnt->type; + packet_write(p, &lntType, sizeof(u16)); + + switch (lnt->type) { + case LST_NETWORK_TYPE_NUMBER: { + f64 number = lnt->value.number; + packet_write(p, &number, sizeof(f64)); + return true; + } + + case LST_NETWORK_TYPE_INTEGER: { + s64 integer = lnt->value.integer; + packet_write(p, &integer, sizeof(s64)); + return true; + } + + case LST_NETWORK_TYPE_BOOLEAN: { + packet_write(p, &lnt->value.boolean, sizeof(u8)); + return true; + } + + case LST_NETWORK_TYPE_STRING: { + snprintf(sLuaStrValue, 256, "%s", lnt->value.string); + u16 valueLength = strlen(sLuaStrValue); + packet_write(p, &valueLength, sizeof(u16)); + packet_write(p, &sLuaStrValue, valueLength * sizeof(u8)); + return true; + } + + case LST_NETWORK_TYPE_NIL: { + // no-op + return true; + } + + default: + break; + } + + LOG_ERROR("attempted to send lua sync table with invalid lnt type: %d", lnt->type); + return false; +} + +static bool packet_read_lnt(struct Packet* p, struct LSTNetworkType* lnt) { + packet_read(p, &lnt->type, sizeof(u16)); + + switch (lnt->type) { + case LST_NETWORK_TYPE_NUMBER: + packet_read(p, &lnt->value.number, sizeof(f64)); + return true; + + case LST_NETWORK_TYPE_INTEGER: + packet_read(p, &lnt->value.integer, sizeof(s64)); + return true; + + case LST_NETWORK_TYPE_BOOLEAN: + packet_read(p, &lnt->value.boolean, sizeof(u8)); + return true; + + case LST_NETWORK_TYPE_STRING: { + u16 valueLength = 0; + packet_read(p, &valueLength, sizeof(u16)); + if (valueLength > 256) { + LOG_ERROR("received lua sync table with invalid value length: %d", valueLength); + return false; + } + + packet_read(p, &sLuaStrValue, valueLength * sizeof(u8)); + sLuaStrValue[valueLength] = 0; + lnt->value.string = sLuaStrValue; + return true; + } + + case LST_NETWORK_TYPE_NIL: + // no-op + return true; + + default: + break; + } + + LOG_ERROR("received lua sync table with invalid type: %d", lnt->type); + return false; +} + +///////////////////////////////////////////////////////////// + +void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 modRemoteIndex, u16 lst, u16 index, const char* key, struct LSTNetworkType* lntValue) { u16 keyLength = strlen(key); struct Packet p = { 0 }; packet_init(&p, PACKET_LUA_SYNC_TABLE, true, PLMT_NONE); packet_write(&p, &seq, sizeof(u64)); - packet_write(&p, &remoteIndex, sizeof(u16)); + packet_write(&p, &modRemoteIndex, sizeof(u16)); packet_write(&p, &lst, sizeof(u16)); packet_write(&p, &index, sizeof(u16)); packet_write(&p, &keyLength, sizeof(u16)); packet_write(&p, (char*)key, keyLength * sizeof(u8)); - packet_write(&p, &lUnionType, sizeof(u16)); - switch (lUnionType) { - case LST_NETWORK_TYPE_NUMBER: { - f64 number = lUnion.number; - packet_write(&p, &number, sizeof(f64)); - //LOG_INFO("tx lua_sync_table: %llu, %u, %u, %s, %u, %f", seq, remoteIndex, lst, key, lUnionType, number); - break; - } - case LST_NETWORK_TYPE_INTEGER: { - s64 integer = lUnion.integer; - packet_write(&p, &integer, sizeof(s64)); - //LOG_INFO("tx lua_sync_table: %llu, %u, %u, %s, %u, %llu", seq, remoteIndex, lst, key, lUnionType, integer); - break; - } - case LST_NETWORK_TYPE_BOOLEAN: { - packet_write(&p, &lUnion.boolean, sizeof(u8)); - //LOG_INFO("tx lua_sync_table: %llu, %u, %u, %s, %u, %u", seq, remoteIndex, lst, key, lUnionType, lUnion.boolean); - break; - } - case LST_NETWORK_TYPE_STRING: { - snprintf(sLuaStrValue, 256, "%s", lUnion.string); - u16 valueLength = strlen(sLuaStrValue); - packet_write(&p, &valueLength, sizeof(u16)); - packet_write(&p, &sLuaStrValue, valueLength * sizeof(u8)); - //LOG_INFO("tx lua_sync_table: %llu, %u, %u, %s, %u, %s", seq, remoteIndex, lst, key, lUnionType, sLuaStrValue); - break; - } - case LST_NETWORK_TYPE_NIL: { - // no-op - //LOG_INFO("tx lua_sync_table: %llu, %u, %u, %s, %u, ", seq, remoteIndex, lst, key, lUnionType); - break; - } - default: - LOG_ERROR("attempted to send lua sync table with invalid type: %d", lUnionType); - return; + if (!packet_write_lnt(&p, lntValue)) { + return; } if (toLocalIndex == 0 || toLocalIndex >= MAX_PLAYERS) { @@ -79,16 +137,15 @@ void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 remoteIndex, u16 void network_receive_lua_sync_table(struct Packet* p) { u64 seq = 0; - u16 remoteIndex = 0; + u16 modRemoteIndex = 0; u16 lst = 0; u16 index = 0; u16 keyLength = 0; char key[65] = { 0 }; - u16 lUnionType = 0; - union LSTNetworkUnion lUnion; + struct LSTNetworkType lntValue = { 0 }; packet_read(p, &seq, sizeof(u64)); - packet_read(p, &remoteIndex, sizeof(u16)); + packet_read(p, &modRemoteIndex, sizeof(u16)); packet_read(p, &lst, sizeof(u16)); packet_read(p, &index, sizeof(u16)); @@ -99,42 +156,9 @@ void network_receive_lua_sync_table(struct Packet* p) { } packet_read(p, &key, keyLength * sizeof(u8)); - packet_read(p, &lUnionType, sizeof(u16)); - switch (lUnionType) { - case LST_NETWORK_TYPE_NUMBER: - packet_read(p, &lUnion.number, sizeof(f64)); - //LOG_INFO("rx lua_sync_table: %llu, %u, %u, %s, %u, %f", seq, remoteIndex, lst, key, lUnionType, lUnion.number); - break; - case LST_NETWORK_TYPE_INTEGER: - packet_read(p, &lUnion.integer, sizeof(s64)); - //LOG_INFO("rx lua_sync_table: %llu, %u, %u, %s, %u, %llu", seq, remoteIndex, lst, key, lUnionType, lUnion.integer); - break; - case LST_NETWORK_TYPE_BOOLEAN: - packet_read(p, &lUnion.boolean, sizeof(u8)); - //LOG_INFO("rx lua_sync_table: %llu, %u, %u, %s, %u, %u", seq, remoteIndex, lst, key, lUnionType, lUnion.boolean); - break; - case LST_NETWORK_TYPE_STRING: { - u16 valueLength = 0; - packet_read(p, &valueLength, sizeof(u16)); - if (keyLength > 256) { - LOG_ERROR("received lua sync table with invalid value length: %d", keyLength); - return; - } - - packet_read(p, &sLuaStrValue, valueLength * sizeof(u8)); - sLuaStrValue[valueLength] = 0; - lUnion.string = sLuaStrValue; - //LOG_INFO("rx lua_sync_table: %llu, %u, %u, %s, %u, %s", seq, remoteIndex, lst, key, lUnionType, lUnion.string); - break; - } - case LST_NETWORK_TYPE_NIL: - // no-op - //LOG_INFO("rx lua_sync_table: %llu, %u, %u, %s, %u, ", seq, remoteIndex, lst, key, lUnionType); - break; - default: - LOG_ERROR("received lua sync table with invalid type: %d", lUnionType); - return; + if (!packet_read_lnt(p, &lntValue)) { + return; } - smlua_set_sync_table_field_from_network(seq, remoteIndex, lst, index, key, lUnionType, lUnion); + smlua_set_sync_table_field_from_network(seq, modRemoteIndex, lst, index, key, &lntValue); }