From de048928ce028cc41860e36e6921a1e88c8e0fbf Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 26 Mar 2022 01:08:15 -0700 Subject: [PATCH] Added ability for Lua mods to adjust gServerSettings --- autogen/convert_constants.py | 1 + autogen/convert_structs.py | 32 ++++++++++++++++++++------- autogen/lua_definitions/constants.lua | 31 ++++++++++++++++++++++++++ autogen/lua_definitions/structs.lua | 10 +++++++++ docs/lua/constants.md | 26 ++++++++++++++++++++++ docs/lua/globals.md | 9 ++++++++ docs/lua/structs.md | 18 +++++++++++++++ src/pc/lua/smlua_cobject.c | 5 +++++ src/pc/lua/smlua_cobject_autogen.c | 14 ++++++++++++ src/pc/lua/smlua_cobject_autogen.h | 1 + src/pc/lua/smlua_constants_autogen.c | 9 ++++++++ 11 files changed, 148 insertions(+), 8 deletions(-) diff --git a/autogen/convert_constants.py b/autogen/convert_constants.py index f3b779123..ef63410c2 100644 --- a/autogen/convert_constants.py +++ b/autogen/convert_constants.py @@ -16,6 +16,7 @@ in_files = [ "include/mario_animation_ids.h", "include/sounds.h", "src/game/characters.h", + "src/pc/network/network.h", "src/pc/network/network_player.h", "include/PR/os_cont.h", "src/game/interaction.c", diff --git a/autogen/convert_structs.py b/autogen/convert_structs.py index 5c552d8e4..659e08330 100644 --- a/autogen/convert_structs.py +++ b/autogen/convert_structs.py @@ -19,6 +19,7 @@ in_files = [ 'src/pc/lua/utils/smlua_misc_utils.h', 'src/pc/lua/utils/smlua_collision_utils.h', 'src/game/spawn_sound.h', + 'src/pc/network/network.h', ] out_filename_c = 'src/pc/lua/smlua_cobject_autogen.c' @@ -80,10 +81,17 @@ override_field_immutable = { "Area": [ "localAreaTimer" ], } -sLuaManuallyDefinedStructs = [ - 'struct Vec3f { float x; float y; float z; }', - 'struct Vec3s { s16 x; s16 y; s16 z; }' -] +override_allowed_structs = { + "src/pc/network/network.h": [ 'ServerSettings' ] +} + +sLuaManuallyDefinedStructs = [{ + 'path': 'n/a', + 'structs': [ + 'struct Vec3f { float x; float y; float z; }', + 'struct Vec3s { s16 x; s16 y; s16 z; }' + ] +}] total_structs = 0 total_fields = 0 @@ -188,10 +196,15 @@ def parse_struct(struct_str): return struct -def parse_structs(struct_strs): +def parse_structs(extracted): structs = [] - for struct_str in struct_strs: - structs.append(parse_struct(struct_str)) + for e in extracted: + for struct in e['structs']: + parsed = parse_struct(struct) + if e['path'] in override_allowed_structs: + if parsed['identifier'] not in override_allowed_structs[e['path']]: + continue + structs.append(parsed) return structs ############################################################################ @@ -445,7 +458,10 @@ def build_files(): extracted = [] for in_file in in_files: path = get_path(in_file) - extracted.extend(extract_structs(path)) + extracted.append({ + 'path': in_file, + 'structs': extract_structs(path) + }) parsed = parse_structs(extracted) parsed = sorted(parsed, key=lambda d: d['identifier']) diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index 74f5bf487..6880a84c9 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -3727,6 +3727,37 @@ MARIO_HAND_HOLDING_WING_CAP = 4 --- @type MarioHandGSCId MARIO_HAND_RIGHT_OPEN = 5 +--- @type integer +MAX_SYNC_OBJECTS = 256 + +--- @type integer +MAX_SYNC_OBJECT_FIELDS = 64 + +--- @type integer +PACKET_LENGTH = 2048 + +--- @type integer +SYNC_DISTANCE_INFINITE = 0 + +--- @class NetworkSystemType + +--- @type NetworkSystemType +NS_SOCKET = 0 + +--- @type NetworkSystemType +NS_DISCORD = 1 + +--- @class PlayerInteractions + +--- @type PlayerInteractions +PLAYER_INTERACTIONS_NONE = 0 + +--- @type PlayerInteractions +PLAYER_INTERACTIONS_SOLID = 1 + +--- @type PlayerInteractions +PLAYER_INTERACTIONS_PVP = 2 + --- @type integer MAX_RX_SEQ_IDS = 16 diff --git a/autogen/lua_definitions/structs.lua b/autogen/lua_definitions/structs.lua index 44fdfd607..17982f406 100644 --- a/autogen/lua_definitions/structs.lua +++ b/autogen/lua_definitions/structs.lua @@ -1386,6 +1386,16 @@ --- @field public hitPos Vec3f --- @field public surface Surface +--- @class ServerSettings +--- @field public bubbleDeath integer +--- @field public enableCheats integer +--- @field public headlessServer integer +--- @field public playerInteractions PlayerInteractions +--- @field public playerKnockbackStrength integer +--- @field public shareLives integer +--- @field public skipIntro integer +--- @field public stayInLevelAfterStar integer + --- @class SoundState --- @field public animFrame1 integer --- @field public animFrame2 integer diff --git a/docs/lua/constants.md b/docs/lua/constants.md index 9fdba5f90..e38d2a841 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -24,6 +24,9 @@ - [enum MarioEyesGSCId](#enum-MarioEyesGSCId) - [enum MarioGrabPosGSCId](#enum-MarioGrabPosGSCId) - [enum MarioHandGSCId](#enum-MarioHandGSCId) +- [network.h](#networkh) + - [enum NetworkSystemType](#enum-NetworkSystemType) + - [enum PlayerInteractions](#enum-PlayerInteractions) - [network_player.h](#network_playerh) - [enum NetworkPlayerType](#enum-NetworkPlayerType) - [obj_behaviors.c](#obj_behaviorsc) @@ -1300,6 +1303,29 @@
+## [network.h](#network.h) +- MAX_SYNC_OBJECTS +- MAX_SYNC_OBJECT_FIELDS +- PACKET_LENGTH +- SYNC_DISTANCE_INFINITE + +### [enum NetworkSystemType](#NetworkSystemType) +| Identifier | Value | +| :--------- | :---- | +| NS_SOCKET | 0 | +| NS_DISCORD | 1 | + +### [enum PlayerInteractions](#PlayerInteractions) +| Identifier | Value | +| :--------- | :---- | +| PLAYER_INTERACTIONS_NONE | 0 | +| PLAYER_INTERACTIONS_SOLID | 1 | +| PLAYER_INTERACTIONS_PVP | 2 | + +[:arrow_up_small:](#) + +
+ ## [network_player.h](#network_player.h) - MAX_RX_SEQ_IDS - NETWORK_PLAYER_TIMEOUT diff --git a/docs/lua/globals.md b/docs/lua/globals.md index a09e1f3b2..524a6dcfb 100644 --- a/docs/lua/globals.md +++ b/docs/lua/globals.md @@ -58,6 +58,15 @@ The `gGlobalObjectCollisionData` table contains references to object collision d
+## [gServerSettings](#gServerSettings) +`gServerSettings`'s fields are listed in [ServerSettings](structs.md#ServerSettings). + +__**NOTE**__: You should only change the fields in this struct on init, and it shouldn't be done inside of if statements or functions. Failing to follow this advice can result in desyncs. + +[:arrow_up_small:](#) + +
+ ## [gGlobalSyncTable](#gGlobalSyncTable) The `gGlobalSyncTable` is a table used for networking. Any field set inside of this table is automatically synchronized with all other clients. Do not use this table for player-specific variables, keep those in [gPlayerSyncTable](#gPlayerSyncTable). Player-specific variable will desynchronize within this table since it doesn't automatically translate `playerIndex`. diff --git a/docs/lua/structs.md b/docs/lua/structs.md index aa99d2316..ed1336953 100644 --- a/docs/lua/structs.md +++ b/docs/lua/structs.md @@ -42,6 +42,7 @@ - [PlayerGeometry](#PlayerGeometry) - [RayIntersectionInfo](#RayIntersectionInfo) - [SPTask](#SPTask) +- [ServerSettings](#ServerSettings) - [SoundState](#SoundState) - [SpawnInfo](#SpawnInfo) - [SpawnParticlesInfo](#SpawnParticlesInfo) @@ -1731,6 +1732,23 @@
+## [ServerSettings](#ServerSettings) + +| Field | Type | Access | +| ----- | ---- | ------ | +| bubbleDeath | `integer` | | +| enableCheats | `integer` | | +| headlessServer | `integer` | | +| playerInteractions | [enum PlayerInteractions](constants.md#enum-PlayerInteractions) | | +| playerKnockbackStrength | `integer` | | +| shareLives | `integer` | | +| skipIntro | `integer` | | +| stayInLevelAfterStar | `integer` | | + +[:arrow_up_small:](#) + +
+ ## [SoundState](#SoundState) | Field | Type | Access | diff --git a/src/pc/lua/smlua_cobject.c b/src/pc/lua/smlua_cobject.c index 1b559a7ad..52c9a2f6c 100644 --- a/src/pc/lua/smlua_cobject.c +++ b/src/pc/lua/smlua_cobject.c @@ -490,6 +490,11 @@ void smlua_cobject_init_globals(void) { lua_setglobal(L, "gLakituState"); } + { + smlua_push_object(L, LOT_SERVERSETTINGS, &gServerSettings); + lua_setglobal(L, "gServerSettings"); + } + } void smlua_cobject_init_per_file_globals(char* path) { diff --git a/src/pc/lua/smlua_cobject_autogen.c b/src/pc/lua/smlua_cobject_autogen.c index 66a20f52f..a443628dd 100644 --- a/src/pc/lua/smlua_cobject_autogen.c +++ b/src/pc/lua/smlua_cobject_autogen.c @@ -14,6 +14,7 @@ #include "src/pc/lua/utils/smlua_misc_utils.h" #include "src/pc/lua/utils/smlua_collision_utils.h" #include "src/game/spawn_sound.h" +#include "src/pc/network/network.h" #include "include/object_fields.h" @@ -1522,6 +1523,18 @@ static struct LuaObjectField sRayIntersectionInfoFields[LUA_RAY_INTERSECTION_INF { "surface", LVT_COBJECT_P, offsetof(struct RayIntersectionInfo, surface), false, LOT_SURFACE }, }; +#define LUA_SERVER_SETTINGS_FIELD_COUNT 8 +static struct LuaObjectField sServerSettingsFields[LUA_SERVER_SETTINGS_FIELD_COUNT] = { + { "bubbleDeath", LVT_U8, offsetof(struct ServerSettings, bubbleDeath), false, LOT_NONE }, + { "enableCheats", LVT_U8, offsetof(struct ServerSettings, enableCheats), false, LOT_NONE }, + { "headlessServer", LVT_U8, offsetof(struct ServerSettings, headlessServer), false, LOT_NONE }, + { "playerInteractions", LVT_S32, offsetof(struct ServerSettings, playerInteractions), false, LOT_NONE }, + { "playerKnockbackStrength", LVT_U8, offsetof(struct ServerSettings, playerKnockbackStrength), false, LOT_NONE }, + { "shareLives", LVT_U8, offsetof(struct ServerSettings, shareLives), false, LOT_NONE }, + { "skipIntro", LVT_U8, offsetof(struct ServerSettings, skipIntro), false, LOT_NONE }, + { "stayInLevelAfterStar", LVT_U8, offsetof(struct ServerSettings, stayInLevelAfterStar), false, LOT_NONE }, +}; + #define LUA_SOUND_STATE_FIELD_COUNT 4 static struct LuaObjectField sSoundStateFields[LUA_SOUND_STATE_FIELD_COUNT] = { { "animFrame1", LVT_S8, offsetof(struct SoundState, animFrame1), false, LOT_NONE }, @@ -1719,6 +1732,7 @@ struct LuaObjectTable sLuaObjectAutogenTable[LOT_AUTOGEN_MAX - LOT_AUTOGEN_MIN] { LOT_PLAYERCAMERASTATE, sPlayerCameraStateFields, LUA_PLAYER_CAMERA_STATE_FIELD_COUNT }, { LOT_PLAYERGEOMETRY, sPlayerGeometryFields, LUA_PLAYER_GEOMETRY_FIELD_COUNT }, { LOT_RAYINTERSECTIONINFO, sRayIntersectionInfoFields, LUA_RAY_INTERSECTION_INFO_FIELD_COUNT }, + { LOT_SERVERSETTINGS, sServerSettingsFields, LUA_SERVER_SETTINGS_FIELD_COUNT }, { LOT_SOUNDSTATE, sSoundStateFields, LUA_SOUND_STATE_FIELD_COUNT }, { LOT_SPAWNINFO, sSpawnInfoFields, LUA_SPAWN_INFO_FIELD_COUNT }, { LOT_SPAWNPARTICLESINFO, sSpawnParticlesInfoFields, LUA_SPAWN_PARTICLES_INFO_FIELD_COUNT }, diff --git a/src/pc/lua/smlua_cobject_autogen.h b/src/pc/lua/smlua_cobject_autogen.h index 4d776c2ee..32fcaa856 100644 --- a/src/pc/lua/smlua_cobject_autogen.h +++ b/src/pc/lua/smlua_cobject_autogen.h @@ -44,6 +44,7 @@ enum LuaObjectAutogenType { LOT_PLAYERCAMERASTATE, LOT_PLAYERGEOMETRY, LOT_RAYINTERSECTIONINFO, + LOT_SERVERSETTINGS, LOT_SOUNDSTATE, LOT_SPAWNINFO, LOT_SPAWNPARTICLESINFO, diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index a76e9b5c6..7e78fea86 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -1387,6 +1387,15 @@ char gSmluaConstants[] = "" "GRAB_POS_LIGHT_OBJ = 1\n" "GRAB_POS_HEAVY_OBJ = 2\n" "GRAB_POS_BOWSER = 3\n" +"SYNC_DISTANCE_INFINITE = 0\n" +"MAX_SYNC_OBJECTS = 256\n" +"MAX_SYNC_OBJECT_FIELDS = 64\n" +"PACKET_LENGTH = 2048\n" +"NS_SOCKET = 0\n" +"NS_DISCORD = 1\n" +"PLAYER_INTERACTIONS_NONE = 0\n" +"PLAYER_INTERACTIONS_SOLID = 1\n" +"PLAYER_INTERACTIONS_PVP = 2\n" "UNKNOWN_LOCAL_INDEX = (-1)\n" "UNKNOWN_GLOBAL_INDEX = (-1)\n" "UNKNOWN_NETWORK_INDEX = (-1)\n"