Added network utils functions to Lua

This commit is contained in:
MysterD 2022-02-02 20:24:35 -08:00
parent 12f66994e6
commit cecb145bbb
16 changed files with 195 additions and 50 deletions

View file

@ -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:

View file

@ -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)

View file

@ -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);

View file

@ -209,6 +209,13 @@
<br />
- 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)
<br />
- surface_collision.h
- [f32_find_wall_collision](#f32_find_wall_collision)
- [find_ceil](#find_ceil)
@ -3538,6 +3545,70 @@
<br />
---
# functions from network_utils.h
<br />
## [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:](#)
<br />
## [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:](#)
<br />
## [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:](#)
<br />
---
# functions from surface_collision.h

View file

@ -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;

View file

@ -2,6 +2,7 @@
#define SMLUA_COBJECT_H
enum LuaValueType {
LVT_BOOL,
LVT_U8,
LVT_U16,
LVT_U32,

View file

@ -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

View file

@ -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

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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);

View file

@ -6,6 +6,7 @@
#include <types.h>
#include <assert.h>
#include "network_player.h"
#include "network_utils.h"
#include "packets/packet.h"
#include "pc/utils/string_linked_list.h"
#include "../cliopts.h"

View file

@ -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; }

View file

@ -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);

View file

@ -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;
}

View file

@ -0,0 +1,12 @@
#ifndef NETWORK_UTILS_H
#define NETWORK_UTILS_H
#include <stdbool.h>
#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