From a892e800680898b09d73428d73a4b6e552bfb9cb Mon Sep 17 00:00:00 2001 From: Aaro Varis Date: Sun, 9 Mar 2025 01:09:04 +0200 Subject: [PATCH] more player sync stuff --- BoneSync/BoneSync.csproj | 2 +- BoneSync/Data/ByteEncoder.cs | 31 ++++++--- BoneSync/Data/PlayerScripts.cs | 20 ++++-- BoneSync/MelonLoaderMod.cs | 5 +- .../LobbyManager/SteamLobbyManager.cs | 5 +- .../Networking/Messages/GunSyncMessage.cs | 1 + .../Networking/Messages/PlayerSyncMessage.cs | 15 ++++- BoneSync/Patching/SkeletonHandPatches.cs | 64 +++++++++++++++++++ BoneSync/Player/PlayerRig.cs | 46 +++++++++++-- BoneSync/Sync/Components/SyncableBase.cs | 2 +- .../Sync/Components/SyncableNetworking.cs | 2 +- .../Sync/Components/SyncableProperties.cs | 8 ++- BoneSync/Sync/PlayerSync.cs | 38 ----------- 13 files changed, 172 insertions(+), 67 deletions(-) create mode 100644 BoneSync/Patching/SkeletonHandPatches.cs delete mode 100644 BoneSync/Sync/PlayerSync.cs diff --git a/BoneSync/BoneSync.csproj b/BoneSync/BoneSync.csproj index 9f92ef2..818b769 100644 --- a/BoneSync/BoneSync.csproj +++ b/BoneSync/BoneSync.csproj @@ -110,6 +110,7 @@ + @@ -120,7 +121,6 @@ - diff --git a/BoneSync/Data/ByteEncoder.cs b/BoneSync/Data/ByteEncoder.cs index e23f804..1b1f682 100644 --- a/BoneSync/Data/ByteEncoder.cs +++ b/BoneSync/Data/ByteEncoder.cs @@ -304,24 +304,35 @@ namespace BoneSync.Data return mag; } + public void WriteCompressedFloat(float value) + { + // write a float in the range of 0-1 with 2 decimal places + WriteByte((byte)(value * 100)); + } + + public float ReadCompressedFloat() + { + return ReadByte() / 100f; + } + public void WriteFingerCurl(SimpleFingerCurl fingerCurl) { - WriteFloat(fingerCurl.thumb); - WriteFloat(fingerCurl.index); - WriteFloat(fingerCurl.middle); - WriteFloat(fingerCurl.ring); - WriteFloat(fingerCurl.pinky); + WriteCompressedFloat(fingerCurl.thumb); + WriteCompressedFloat(fingerCurl.index); + WriteCompressedFloat(fingerCurl.middle); + WriteCompressedFloat(fingerCurl.ring); + WriteCompressedFloat(fingerCurl.pinky); } public SimpleFingerCurl ReadFingerCurl() { SimpleFingerCurl fingerCurl = new SimpleFingerCurl() { - thumb = ReadFloat(), - index = ReadFloat(), - middle = ReadFloat(), - ring = ReadFloat(), - pinky = ReadFloat() + thumb = ReadCompressedFloat(), + index = ReadCompressedFloat(), + middle = ReadCompressedFloat(), + ring = ReadCompressedFloat(), + pinky = ReadCompressedFloat() }; return fingerCurl; } diff --git a/BoneSync/Data/PlayerScripts.cs b/BoneSync/Data/PlayerScripts.cs index 7da651c..a114bf7 100644 --- a/BoneSync/Data/PlayerScripts.cs +++ b/BoneSync/Data/PlayerScripts.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using MelonLoader; using StressLevelZero.Interaction; using StressLevelZero.Player; using StressLevelZero.Rig; @@ -35,12 +36,19 @@ namespace BoneSync.Data playerHealth.reloadLevelOnDeath = !BoneSync.lobby.IsConnected(); - PhysicsRig physicsRig = playerRig.physicsRig; - playerPhysBody = physicsRig.physBody; - playerGrounder = playerPhysBody.physG; - playerLeftHand = physicsRig.leftHand; - playerRightHand = physicsRig.rightHand; - playerAnimatorController = playerRig.gameWorldSkeletonRig.characterAnimationManager.animator.runtimeAnimatorController; + try + { + PhysicsRig physicsRig = playerRig.physicsRig; + playerPhysBody = physicsRig.physBody; + playerGrounder = playerPhysBody.physG; + playerLeftHand = physicsRig.leftHand; + playerRightHand = physicsRig.rightHand; + playerAnimatorController = playerRig.gameWorldSkeletonRig.characterAnimationManager.animator.runtimeAnimatorController; + } catch + { + MelonLogger.Warning("Failed to get physicsRig player scripts!"); + } + GetHandPoses(); } diff --git a/BoneSync/MelonLoaderMod.cs b/BoneSync/MelonLoaderMod.cs index 27b088b..a543204 100644 --- a/BoneSync/MelonLoaderMod.cs +++ b/BoneSync/MelonLoaderMod.cs @@ -71,7 +71,8 @@ namespace BoneSync public override void OnUpdate() { - transport.Tick(); + bool processPackets = transport.Tick(); + //PlayerRig.Tick(); if (Input.GetKeyDown(KeyCode.P)) @@ -92,7 +93,7 @@ namespace BoneSync if (Input.GetKeyDown(KeyCode.N)) { - MelonLogger.Msg("Creting debug player rig"); + MelonLogger.Msg("Creating debug player rig"); PlayerRig debugRig = PlayerRig.GetPlayerRig(0); PlayerSyncInfo? playerSyncInfo = PlayerRig.GetLocalSyncInfo(); if (!playerSyncInfo.HasValue) diff --git a/BoneSync/Networking/LobbyManager/SteamLobbyManager.cs b/BoneSync/Networking/LobbyManager/SteamLobbyManager.cs index 0bfeb12..2160fd4 100644 --- a/BoneSync/Networking/LobbyManager/SteamLobbyManager.cs +++ b/BoneSync/Networking/LobbyManager/SteamLobbyManager.cs @@ -20,6 +20,7 @@ namespace BoneSync.Networking.LobbyManager _lobbyInstance = lobby; MelonLogger.Msg("Created lobby " + lobby.Id); UpdateLobbyData(); + _lobbyInstance.SetPublic(); }; SteamMatchmaking.OnLobbyEntered += (Lobby lobby) => { @@ -57,6 +58,9 @@ namespace BoneSync.Networking.LobbyManager ulong lobbyId = ulong.Parse(connectString.Split(':')[1]); JoinLobby(lobbyId); }; + + + //SteamMatchmaking.LobbyList. } private Lobby _lobbyInstance; @@ -102,7 +106,6 @@ namespace BoneSync.Networking.LobbyManager SteamFriends.SetRichPresence("connect", null); SteamFriends.SetRichPresence("status", "Not in a multiplayer lobby"); } - } public override void CreateLobby() diff --git a/BoneSync/Networking/Messages/GunSyncMessage.cs b/BoneSync/Networking/Messages/GunSyncMessage.cs index 08989c1..a5c8f55 100644 --- a/BoneSync/Networking/Messages/GunSyncMessage.cs +++ b/BoneSync/Networking/Messages/GunSyncMessage.cs @@ -15,6 +15,7 @@ namespace BoneSync.Networking.Messages { StateUpdate = 0, Fire = 1, + EjectCartridge = 2, } public struct GunSyncInfo diff --git a/BoneSync/Networking/Messages/PlayerSyncMessage.cs b/BoneSync/Networking/Messages/PlayerSyncMessage.cs index c3a03b4..66ccd2a 100644 --- a/BoneSync/Networking/Messages/PlayerSyncMessage.cs +++ b/BoneSync/Networking/Messages/PlayerSyncMessage.cs @@ -1,6 +1,7 @@  using BoneSync.Data; +using BoneSync.Player; using BoneSync.Sync; using StressLevelZero.Player; using System; @@ -36,6 +37,10 @@ namespace BoneSync.Networking.Messages public SimpleSyncTransform rightHandPos; public SimpleFingerCurl leftHandFingerCurl; public SimpleFingerCurl rightHandFingerCurl; + public byte poseIndexRight; + public byte poseIndexLeft; + public float poseRadiusRight; + public float poseRadiusLeft; } [PacketType(PacketType.PlayerSync), PacketReliability(PacketReliability.Unreliable)] @@ -53,6 +58,10 @@ namespace BoneSync.Networking.Messages byteEncoder.WriteSimpleTransform(_playerSyncInfo.rightHandPos); byteEncoder.WriteFingerCurl(_playerSyncInfo.leftHandFingerCurl); byteEncoder.WriteFingerCurl(_playerSyncInfo.rightHandFingerCurl); + byteEncoder.WriteByte(_playerSyncInfo.poseIndexRight); + byteEncoder.WriteByte(_playerSyncInfo.poseIndexLeft); + byteEncoder.WriteFloat(_playerSyncInfo.poseRadiusRight); + byteEncoder.WriteFloat(_playerSyncInfo.poseRadiusLeft); } public PlayerSyncMessage(Packet packet) @@ -64,11 +73,15 @@ namespace BoneSync.Networking.Messages _playerSyncInfo.rightHandPos = byteEncoder.ReadSimpleTransform(); _playerSyncInfo.leftHandFingerCurl = byteEncoder.ReadFingerCurl(); _playerSyncInfo.rightHandFingerCurl = byteEncoder.ReadFingerCurl(); + _playerSyncInfo.poseIndexRight = byteEncoder.ReadByte(); + _playerSyncInfo.poseIndexLeft = byteEncoder.ReadByte(); + _playerSyncInfo.poseRadiusRight = byteEncoder.ReadFloat(); + _playerSyncInfo.poseRadiusLeft = byteEncoder.ReadFloat(); } public override void Execute() { - PlayerSync.OnPlayerSync(this); + PlayerRig.OnPlayerSync(this); } } } diff --git a/BoneSync/Patching/SkeletonHandPatches.cs b/BoneSync/Patching/SkeletonHandPatches.cs new file mode 100644 index 0000000..7231eec --- /dev/null +++ b/BoneSync/Patching/SkeletonHandPatches.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using HarmonyLib; +using MelonLoader; +using StressLevelZero.Rig; +using BoneSync.Data; +using StressLevelZero; + +namespace BoneSync.Patching +{ + [HarmonyPatch(typeof(SkeletonHand))] + internal static class SkeletonHandPatches + { + public static byte poseIndexRight = 0; + public static float radiusRight = 0.0f; + + public static byte poseIndexLeft = 0; + public static float radiusLeft = 0.0f; + + [HarmonyPatch(nameof(SkeletonHand.SetHandPose)), HarmonyPostfix] + private static void SetHandPosePostfix(SkeletonHand __instance, string handPoseName) + { + if (!__instance.GetCharacterAnimationManager()) return; + MelonLogger.Msg($"SetHandPosePostfix: {handPoseName}"); + + int poseIndex = PlayerScripts.playerHandPoses.IndexOf(handPoseName); + switch (__instance.handedness) + { + case Handedness.LEFT: + poseIndexLeft = (byte)poseIndex; + break; + case Handedness.RIGHT: + poseIndexRight = (byte)poseIndex; + break; + } + + } + + [HarmonyPatch(nameof(SkeletonHand.SetCylinderRadius)), HarmonyPostfix] + private static void SetCylinderRadiusPrefix(SkeletonHand __instance, float radius) + { + if (!__instance.GetCharacterAnimationManager()) return; + MelonLogger.Msg($"SetCylinderRadiusPrefix: {radius}"); + + switch (__instance.handedness) + { + case Handedness.LEFT: + radiusLeft = radius; + break; + case Handedness.RIGHT: + radiusRight = radius; + break; + } + + + + } + + } +} diff --git a/BoneSync/Player/PlayerRig.cs b/BoneSync/Player/PlayerRig.cs index 75604e7..79ab243 100644 --- a/BoneSync/Player/PlayerRig.cs +++ b/BoneSync/Player/PlayerRig.cs @@ -16,6 +16,7 @@ using System.IO; using BoneSync.Data; using StressLevelZero.Interaction; using UnhollowerBaseLib; +using BoneSync.Patching; namespace BoneSync.Player @@ -72,6 +73,7 @@ namespace BoneSync.Player public void UpdatePlayerSync(PlayerSyncInfo playerSyncInfo) { + //MelonLogger.Msg("Updating player sync for " + _ownerId); playerRig.transform.position = playerSyncInfo.rootPos; headTransform.ApplySimpleTransform(playerSyncInfo.headPos); @@ -82,6 +84,19 @@ namespace BoneSync.Player SetFingerCurl(Handedness.RIGHT, playerSyncInfo.rightHandFingerCurl); } + public void UpdatePose(Handedness hand, int index) + { + Il2CppStringArray handPoses = PlayerScripts.playerHandPoses; + if (handPoses.Count < index + 1) + return; + UpdatePose(hand, handPoses[index]); + } + + public void UpdatePose(Handedness hand, string pose) => characterAnimationManager?.SetHandPose(hand, pose); + + public void UpdatePoseRadius(Handedness hand, float radius) => characterAnimationManager?.SetCylinderRadius(hand, radius); + + public static PlayerSyncInfo? GetLocalSyncInfo() { GameObject localPlayerRig = GameObject.Find("[RigManager (Default Brett)]/[SkeletonRig (GameWorld Brett)]"); @@ -110,15 +125,25 @@ namespace BoneSync.Player rightHandPos = new SimpleSyncTransform(localRightHandTransform), //leftHandFingerCurl = new SimpleFingerCurl(PlayerScripts.playerLeftHand.fingerCurl), //rightHandFingerCurl = new SimpleFingerCurl(PlayerScripts.playerRightHand.fingerCurl) + poseIndexLeft = SkeletonHandPatches.poseIndexLeft, + poseIndexRight = SkeletonHandPatches.poseIndexRight, + poseRadiusLeft = SkeletonHandPatches.radiusLeft, + poseRadiusRight = SkeletonHandPatches.radiusRight }; + if (PlayerScripts.playerLeftHand) + playerSyncInfo.leftHandFingerCurl = new SimpleFingerCurl(PlayerScripts.playerLeftHand.fingerCurl); + if (PlayerScripts.playerRightHand) + playerSyncInfo.rightHandFingerCurl = new SimpleFingerCurl(PlayerScripts.playerRightHand.fingerCurl); + return playerSyncInfo; } private static void SendLocalPlayerSync() { if (!BoneSync.lobby.IsConnected()) return; + //MelonLogger.Msg("Sending local player sync"); PlayerSyncInfo? playerSyncInfo = GetLocalSyncInfo(); if (!playerSyncInfo.HasValue) return; PlayerSyncMessage playerSyncMessage = new PlayerSyncMessage(playerSyncInfo.Value); @@ -129,7 +154,7 @@ namespace BoneSync.Player { if (_playerRigs.ContainsKey(ownerId)) { - MelonLogger.Msg("PlayerRig already exists for " + ownerId); + //MelonLogger.Msg("PlayerRig already exists for " + ownerId); return _playerRigs[ownerId]; } @@ -138,10 +163,9 @@ namespace BoneSync.Player MelonLogger.Msg("playerRepBundle is null! Did you forget to load the bundle?"); return null; } - PlayerScripts.GetPlayerScripts(); + //PlayerScripts.GetPlayerScripts(); PlayerRig rig = new PlayerRig(ownerId); - rig.EnsurePlayerRig(); return rig; } @@ -157,7 +181,7 @@ namespace BoneSync.Player if (playerRig != null) return; playerRig = GameObject.Instantiate(rigBundle.LoadAsset("PlayerRep")); - playerRig.name = "PlayerRep (" + _ownerId + ")"; + playerRig.name = "PlayerRep"; body = playerRig.GetComponentInChildren(); characterAnimationManager = playerRig.GetComponentInChildren(); @@ -191,7 +215,11 @@ namespace BoneSync.Player body.FullBodyUpdate(repInputVel, Vector3.zero); body.ArtToBlender.UpdateBlender(); } - catch { } + catch (Exception e) + { + MelonLogger.Warning("Failed to update player rig " + _ownerId); + MelonLogger.Warning(e.ToString()); + } } @@ -201,6 +229,14 @@ namespace BoneSync.Player _ownerId = id; } + public static void OnPlayerSync(PlayerSyncMessage playerSyncMessage) + { + //MelonLogger.Msg("Player Sync Received " + playerSyncMessage.senderId); + PlayerRig playerRig = PlayerRig.GetPlayerRig(playerSyncMessage.senderId); + if (playerRig == null) return; + playerRig.UpdatePlayerSync(playerSyncMessage.playerSyncInfo); + + } public void Destroy() { diff --git a/BoneSync/Sync/Components/SyncableBase.cs b/BoneSync/Sync/Components/SyncableBase.cs index a6e4fee..43bc62c 100644 --- a/BoneSync/Sync/Components/SyncableBase.cs +++ b/BoneSync/Sync/Components/SyncableBase.cs @@ -195,7 +195,7 @@ namespace BoneSync.Sync.Components sockets = GetComponentsInChildren(); aiBrain = GetComponent(); buttonToggles = GetComponentsInChildren(); - puppetMaster = aiBrain.puppetMaster; + puppetMaster = aiBrain?.puppetMaster; if (sockets.Length == 0) { plugs = GetComponentsInChildren(); diff --git a/BoneSync/Sync/Components/SyncableNetworking.cs b/BoneSync/Sync/Components/SyncableNetworking.cs index 45b1c40..e83693c 100644 --- a/BoneSync/Sync/Components/SyncableNetworking.cs +++ b/BoneSync/Sync/Components/SyncableNetworking.cs @@ -78,7 +78,7 @@ namespace BoneSync.Sync.Components public void OnOwnershipTransferRequest(ulong newOwnerId) { //MelonLogger.Msg("Ownership transfer request for " + _syncId + " to " + newOwnerId); - if (isOwner && !IsHolding()) + if (isOwner && !IsHolding() && !_isInHolster) { MelonLogger.Msg("Sending ownership transfer for " + _syncId + " to " + newOwnerId); OwnershipTransferMessageData data = new OwnershipTransferMessageData() diff --git a/BoneSync/Sync/Components/SyncableProperties.cs b/BoneSync/Sync/Components/SyncableProperties.cs index 953cf4a..f0a6385 100644 --- a/BoneSync/Sync/Components/SyncableProperties.cs +++ b/BoneSync/Sync/Components/SyncableProperties.cs @@ -69,9 +69,15 @@ namespace BoneSync.Sync.Components BulletObject bulletObject = gunSyncInfo.bulletObject; StripDamage(ref bulletObject); gun.EjectCartridge(); - //PoolSpawner.SpawnProjectile(position, rotation, gunSyncInfo.bulletObject, "1911", null); + PoolSpawner.SpawnProjectile(position, rotation, gunSyncInfo.bulletObject, "1911", null); PoolSpawner.SpawnMuzzleFlare(position, rotation, PoolSpawner.MuzzleFlareType.Default); + return; + } + if (gunSyncInfo.messageType == GunSyncMessageType.EjectCartridge) + { + gun.EjectCartridge(); + return; } } } diff --git a/BoneSync/Sync/PlayerSync.cs b/BoneSync/Sync/PlayerSync.cs deleted file mode 100644 index 049d652..0000000 --- a/BoneSync/Sync/PlayerSync.cs +++ /dev/null @@ -1,38 +0,0 @@ -using BoneSync.Data; -using BoneSync.Networking; -using BoneSync.Networking.Messages; -using BoneSync.Player; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using UnityEngine; - -namespace BoneSync.Sync -{ - internal static class PlayerSync - { - - private static GameObject _localPlayerRig; - - public static void SyncPlayer() - { - PlayerSyncInfo playerSyncInfo = new PlayerSyncInfo(); - playerSyncInfo.headPos = new SimpleSyncTransform(); - playerSyncInfo.leftHandPos = new SimpleSyncTransform(); - playerSyncInfo.rightHandPos = new SimpleSyncTransform(); - - PlayerSyncMessage playerSyncMessage = new PlayerSyncMessage(playerSyncInfo); - playerSyncMessage.Broadcast(); - } - - public static void OnPlayerSync(PlayerSyncMessage playerSyncMessage) - { - PlayerRig playerRig = PlayerRig.GetPlayerRig(playerSyncMessage.senderId); - if (playerRig == null) return; - playerRig.UpdatePlayerSync(playerSyncMessage.playerSyncInfo); - - } - } -}