Allowed keys of SyncTables to be non-strings
This commit is contained in:
parent
836e2f6e7d
commit
5e8db3de00
|
@ -42,6 +42,28 @@ static bool smlua_value_to_lnt(int index, struct LSTNetworkType* lnt) {
|
|||
return false;
|
||||
}
|
||||
|
||||
static void smlua_push_lnt(struct LSTNetworkType* lnt) {
|
||||
lua_State* L = gLuaState;
|
||||
switch (lnt->type) {
|
||||
case LST_NETWORK_TYPE_INTEGER:
|
||||
lua_pushinteger(L, lnt->value.integer);
|
||||
break;
|
||||
case LST_NETWORK_TYPE_NUMBER:
|
||||
lua_pushnumber(L, lnt->value.number);
|
||||
break;
|
||||
case LST_NETWORK_TYPE_BOOLEAN:
|
||||
lua_pushboolean(L, lnt->value.boolean);
|
||||
break;
|
||||
case LST_NETWORK_TYPE_STRING:
|
||||
lua_pushstring(L, lnt->value.string);
|
||||
break;
|
||||
case LST_NETWORK_TYPE_NIL:
|
||||
lua_pushnil(L);
|
||||
break;
|
||||
default: SOFT_ASSERT(false);
|
||||
}
|
||||
}
|
||||
|
||||
static void smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool alterSeq) {
|
||||
LUA_STACK_CHECK_BEGIN();
|
||||
lua_State* L = gLuaState;
|
||||
|
@ -65,11 +87,8 @@ static void smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al
|
|||
}
|
||||
|
||||
// get key
|
||||
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) ? "<null>" : key);
|
||||
return;
|
||||
}
|
||||
struct LSTNetworkType lntKey = { 0 };
|
||||
if (!smlua_value_to_lnt(keyIndex, &lntKey)) { return; }
|
||||
|
||||
|
||||
///////////
|
||||
|
@ -78,10 +97,7 @@ static void smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al
|
|||
|
||||
// get value
|
||||
struct LSTNetworkType lntValue = { 0 };
|
||||
bool validValue = smlua_value_to_lnt(valueIndex, &lntValue);
|
||||
if (!validValue) {
|
||||
return;
|
||||
}
|
||||
if (!smlua_value_to_lnt(valueIndex, &lntValue)) { return; }
|
||||
|
||||
// set value
|
||||
lua_getfield(L, syncTableIndex, "_table");
|
||||
|
@ -97,19 +113,19 @@ static void smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al
|
|||
|
||||
// get seq table
|
||||
lua_getfield(L, syncTableIndex, "_seq");
|
||||
int seqT = lua_gettop(L);
|
||||
|
||||
// get seq number
|
||||
lua_getfield(L, -1, key);
|
||||
lua_pushvalue(L, keyIndex);
|
||||
lua_gettable(L, -2);
|
||||
u64 seq = lua_tointeger(L, -1);
|
||||
lua_pop(L, 1);
|
||||
lua_pop(L, 1); // pop seq value
|
||||
|
||||
// set seq number
|
||||
if (!gLuaInitializingScript && alterSeq) {
|
||||
seq += MAX_PLAYERS + (MAX_PLAYERS - gNetworkPlayers[0].globalIndex);
|
||||
smlua_push_number_field(seqT, (char*)key, seq);
|
||||
lua_pushvalue(L, keyIndex);
|
||||
lua_pushinteger(L, seq);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
lua_pop(L, 1); // pop seq table
|
||||
|
||||
|
||||
///////////
|
||||
|
@ -127,7 +143,7 @@ static void smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al
|
|||
|
||||
// send over the network
|
||||
if (!gLuaInitializingScript && seq > 0) {
|
||||
network_send_lua_sync_table(toLocalIndex, seq, modRemoteIndex, lst, index, key, &lntValue);
|
||||
network_send_lua_sync_table(toLocalIndex, seq, modRemoteIndex, lst, index, &lntKey, &lntValue);
|
||||
}
|
||||
|
||||
LUA_STACK_CHECK_END();
|
||||
|
@ -139,7 +155,7 @@ static int smlua__set_sync_table_field(UNUSED lua_State* L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 lst, u16 index, const char* key, struct LSTNetworkType* lntValue) {
|
||||
void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 lst, u16 index, struct LSTNetworkType* lntKey, struct LSTNetworkType* lntValue) {
|
||||
LUA_STACK_CHECK_BEGIN();
|
||||
lua_State* L = gLuaState;
|
||||
|
||||
|
@ -200,12 +216,12 @@ void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 ls
|
|||
|
||||
// get seq table
|
||||
lua_getfield(L, -1, "_seq");
|
||||
int seqT = lua_gettop(L);
|
||||
|
||||
// get seq number
|
||||
lua_getfield(L, -1, key);
|
||||
smlua_push_lnt(lntKey);
|
||||
lua_gettable(L, -2);
|
||||
u64 readSeq = lua_tointeger(L, -1);
|
||||
lua_pop(L, 1);
|
||||
lua_pop(L, 1); // pop seq value
|
||||
|
||||
// validate seq
|
||||
if (seq <= readSeq) {
|
||||
|
@ -218,7 +234,9 @@ void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 ls
|
|||
}
|
||||
|
||||
// set seq number
|
||||
smlua_push_number_field(seqT, (char*)key, seq);
|
||||
smlua_push_lnt(lntKey);
|
||||
lua_pushinteger(L, seq);
|
||||
lua_settable(L, -3);
|
||||
lua_pop(L, 1); // pop seq table
|
||||
|
||||
// get internal table
|
||||
|
@ -227,34 +245,9 @@ void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 ls
|
|||
int t = lua_gettop(L);
|
||||
|
||||
// set key/value
|
||||
switch (lntValue->type) {
|
||||
case LST_NETWORK_TYPE_INTEGER:
|
||||
lua_pushstring(L, key);
|
||||
lua_pushinteger(L, lntValue->value.integer);
|
||||
lua_rawset(L, t);
|
||||
break;
|
||||
case LST_NETWORK_TYPE_NUMBER:
|
||||
lua_pushstring(L, key);
|
||||
lua_pushnumber(L, lntValue->value.number);
|
||||
lua_rawset(L, t);
|
||||
break;
|
||||
case LST_NETWORK_TYPE_BOOLEAN:
|
||||
lua_pushstring(L, key);
|
||||
lua_pushboolean(L, lntValue->value.boolean);
|
||||
lua_rawset(L, t);
|
||||
break;
|
||||
case LST_NETWORK_TYPE_STRING:
|
||||
lua_pushstring(L, key);
|
||||
lua_pushstring(L, lntValue->value.string);
|
||||
lua_rawset(L, t);
|
||||
break;
|
||||
case LST_NETWORK_TYPE_NIL:
|
||||
lua_pushstring(L, key);
|
||||
lua_pushnil(L);
|
||||
lua_rawset(L, t);
|
||||
break;
|
||||
default: SOFT_ASSERT(false);
|
||||
}
|
||||
smlua_push_lnt(lntKey);
|
||||
smlua_push_lnt(lntValue);
|
||||
lua_rawset(L, t);
|
||||
|
||||
lua_pop(L, 1); // pop internal table
|
||||
lua_pop(L, syncTableSize); // pop sync table
|
||||
|
|
|
@ -9,7 +9,7 @@ enum LuaSyncTableType {
|
|||
|
||||
struct LSTNetworkType;
|
||||
|
||||
void smlua_set_sync_table_field_from_network(u64 seq, u16 remoteIndex, u16 lst, u16 index, const char* key, struct LSTNetworkType* lntValue);
|
||||
void smlua_set_sync_table_field_from_network(u64 seq, u16 remoteIndex, u16 lst, u16 index, struct LSTNetworkType* lntKey, 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);
|
||||
|
|
|
@ -329,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, struct LSTNetworkType* lntValue);
|
||||
void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 remoteIndex, u16 lst, u16 index, struct LSTNetworkType* lntKey, struct LSTNetworkType* lntValue);
|
||||
void network_receive_lua_sync_table(struct Packet* p);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -111,8 +111,7 @@ static bool packet_read_lnt(struct Packet* p, struct LSTNetworkType* lnt) {
|
|||
|
||||
/////////////////////////////////////////////////////////////
|
||||
|
||||
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);
|
||||
void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 modRemoteIndex, u16 lst, u16 index, struct LSTNetworkType* lntKey, struct LSTNetworkType* lntValue) {
|
||||
|
||||
struct Packet p = { 0 };
|
||||
packet_init(&p, PACKET_LUA_SYNC_TABLE, true, PLMT_NONE);
|
||||
|
@ -121,12 +120,8 @@ void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 modRemoteIndex, u
|
|||
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));
|
||||
|
||||
if (!packet_write_lnt(&p, lntValue)) {
|
||||
return;
|
||||
}
|
||||
if (!packet_write_lnt(&p, lntKey)) { return; }
|
||||
if (!packet_write_lnt(&p, lntValue)) { return; }
|
||||
|
||||
if (toLocalIndex == 0 || toLocalIndex >= MAX_PLAYERS) {
|
||||
network_send(&p);
|
||||
|
@ -140,8 +135,7 @@ void network_receive_lua_sync_table(struct Packet* p) {
|
|||
u16 modRemoteIndex = 0;
|
||||
u16 lst = 0;
|
||||
u16 index = 0;
|
||||
u16 keyLength = 0;
|
||||
char key[65] = { 0 };
|
||||
struct LSTNetworkType lntKey = { 0 };
|
||||
struct LSTNetworkType lntValue = { 0 };
|
||||
|
||||
packet_read(p, &seq, sizeof(u64));
|
||||
|
@ -149,16 +143,8 @@ void network_receive_lua_sync_table(struct Packet* p) {
|
|||
packet_read(p, &lst, sizeof(u16));
|
||||
packet_read(p, &index, sizeof(u16));
|
||||
|
||||
packet_read(p, &keyLength, sizeof(u16));
|
||||
if (keyLength > 64) {
|
||||
LOG_ERROR("received lua sync table with invalid key length: %d", keyLength);
|
||||
return;
|
||||
}
|
||||
packet_read(p, &key, keyLength * sizeof(u8));
|
||||
if (!packet_read_lnt(p, &lntKey)) { return; }
|
||||
if (!packet_read_lnt(p, &lntValue)) { return; }
|
||||
|
||||
if (!packet_read_lnt(p, &lntValue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
smlua_set_sync_table_field_from_network(seq, modRemoteIndex, lst, index, key, &lntValue);
|
||||
smlua_set_sync_table_field_from_network(seq, modRemoteIndex, lst, index, &lntKey, &lntValue);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue