diff --git a/BoneSync/Networking/ByteEncoder.cs b/BoneSync/Data/ByteEncoder.cs similarity index 97% rename from BoneSync/Networking/ByteEncoder.cs rename to BoneSync/Data/ByteEncoder.cs index 668a19c..4baada6 100644 --- a/BoneSync/Networking/ByteEncoder.cs +++ b/BoneSync/Data/ByteEncoder.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace BoneSync.Networking +namespace BoneSync.Data { public static class BitPacking { @@ -194,19 +194,19 @@ namespace BoneSync.Networking return UnityEngine.Quaternion.Euler(eulerAngles); } - public void WriteSimpleTransform(SimpleTransform value) + public void WriteSimpleTransform(SimpleSyncTransform value) { WriteVector3(value.position); WriteQuaternion(value.rotation); WriteVector3(value.scale); } - public SimpleTransform ReadSimpleTransform() + public SimpleSyncTransform ReadSimpleTransform() { UnityEngine.Vector3 position = ReadVector3(); UnityEngine.Quaternion rotation = ReadQuaternion(); UnityEngine.Vector3 scale = ReadVector3(); - return new SimpleTransform() + return new SimpleSyncTransform() { position = position, rotation = rotation, diff --git a/BoneSync/Data/SpawnableManager.cs b/BoneSync/Data/SpawnableManager.cs new file mode 100644 index 0000000..1fb4bd6 --- /dev/null +++ b/BoneSync/Data/SpawnableManager.cs @@ -0,0 +1,56 @@ +using StressLevelZero.Data; +using StressLevelZero.Pool; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnhollowerBaseLib; +using UnhollowerRuntimeLib; + +using UnityObject = UnityEngine.Object; + +namespace BoneSync.Data +{ + public static class SpawnableManager + { + private static Dictionary _spawnableCache = new Dictionary(); + + private static void _UpdateSpawnableCache() + { + Il2CppReferenceArray foundSpawnables = UnityObject.FindObjectsOfTypeIncludingAssets(Il2CppType.Of()); + foreach (UnityObject obj in foundSpawnables) + { + SpawnableObject spawnable = obj.Cast(); + RegisterSpawnable(spawnable); + } + } + + private static void _ClearCache() + { + _spawnableCache.Clear(); + } + public static void RegisterSpawnable(SpawnableObject spawnable) + { + if (!_spawnableCache.ContainsKey(spawnable.title)) + _spawnableCache.Add(spawnable.title, spawnable); + } + public static SpawnableObject GetSpawnable(string title) + { + if (_spawnableCache.ContainsKey(title)) + return _spawnableCache[title]; + return null; + } + + public static Pool GetPool(SpawnableObject spawnable) { + PoolManager.RegisterPool(spawnable); + return PoolManager.GetPool(spawnable.title); + } + + public static void Initialize() + { + _ClearCache(); + _UpdateSpawnableCache(); + } + } +} diff --git a/BoneSync/Networking/Structs.cs b/BoneSync/Data/Structs.cs similarity index 79% rename from BoneSync/Networking/Structs.cs rename to BoneSync/Data/Structs.cs index 4b85b73..239c45d 100644 --- a/BoneSync/Networking/Structs.cs +++ b/BoneSync/Data/Structs.cs @@ -5,24 +5,24 @@ using System.Text; using System.Threading.Tasks; using UnityEngine; -namespace BoneSync.Networking +namespace BoneSync.Data { public static class SimpleTransformExtensions { - public static void ApplySimpleTransform(this UnityEngine.Transform transform, SimpleTransform simpleTransform) + public static void ApplySimpleTransform(this UnityEngine.Transform transform, SimpleSyncTransform simpleTransform) { transform.position = simpleTransform.position; transform.rotation = simpleTransform.rotation; transform.localScale = simpleTransform.scale; } } - public struct SimpleTransform + public struct SimpleSyncTransform { public Vector3 position; public Quaternion rotation; public Vector3 scale; - public SimpleTransform(UnityEngine.Transform transform) + public SimpleSyncTransform(UnityEngine.Transform transform) { position = transform.position; rotation = transform.rotation; diff --git a/BoneSync/MelonLoaderMod.cs b/BoneSync/MelonLoaderMod.cs index c7dfb6d..be252c0 100644 --- a/BoneSync/MelonLoaderMod.cs +++ b/BoneSync/MelonLoaderMod.cs @@ -8,6 +8,7 @@ using BoneSync.Player; using BoneSync.Sync; using Facepunch.Steamworks; using System.Reflection.Emit; +using BoneSync.Data; namespace BoneSync { @@ -36,6 +37,8 @@ namespace BoneSync SceneSync.Initialize(); NetworkMessage.RegisterPacketTypes(); + SpawnableManager.Initialize(); + PatchAll(); } diff --git a/BoneSync/Networking/Messages/ObjectSyncMessage.cs b/BoneSync/Networking/Messages/ObjectSyncMessage.cs index 6bbfa2e..760d201 100644 --- a/BoneSync/Networking/Messages/ObjectSyncMessage.cs +++ b/BoneSync/Networking/Messages/ObjectSyncMessage.cs @@ -1,4 +1,5 @@ -using BoneSync.Sync; +using BoneSync.Data; +using BoneSync.Sync; using System; using System.Collections.Generic; using System.Linq; @@ -11,7 +12,7 @@ namespace BoneSync.Networking.Messages public struct ObjectSyncTransform { - public SimpleTransform transform; + public SimpleSyncTransform transform; public Vector3 velocity; public Vector3 angularVelocity; } diff --git a/BoneSync/Networking/Messages/PlayerSyncMessage.cs b/BoneSync/Networking/Messages/PlayerSyncMessage.cs index 0cf2ae3..975d484 100644 --- a/BoneSync/Networking/Messages/PlayerSyncMessage.cs +++ b/BoneSync/Networking/Messages/PlayerSyncMessage.cs @@ -1,5 +1,6 @@  +using BoneSync.Data; using BoneSync.Sync; using System; using System.Collections.Generic; @@ -12,9 +13,9 @@ namespace BoneSync.Networking.Messages public struct PlayerSyncInfo { - public SimpleTransform headPos; - public SimpleTransform leftHandPos; - public SimpleTransform rightHandPos; + public SimpleSyncTransform headPos; + public SimpleSyncTransform leftHandPos; + public SimpleSyncTransform rightHandPos; } [PacketType(PacketType.PlayerSync)] diff --git a/BoneSync/Networking/Messages/RegisterSyncableMessage.cs b/BoneSync/Networking/Messages/RegisterSyncableMessage.cs index 0726fff..b8d24be 100644 --- a/BoneSync/Networking/Messages/RegisterSyncableMessage.cs +++ b/BoneSync/Networking/Messages/RegisterSyncableMessage.cs @@ -1,4 +1,5 @@ -using BoneSync.Patching; +using BoneSync.Data; +using BoneSync.Patching; using BoneSync.Sync; using System; using System.Collections.Generic; @@ -16,8 +17,8 @@ namespace BoneSync.Networking.Messages public struct SpawnPoolableInfo { - public string poolName; - public SimpleTransform spawnLocation; + public string spawnableTitle; + public SimpleSyncTransform spawnLocation; } public struct RegisterSyncableInfo { @@ -44,7 +45,7 @@ namespace BoneSync.Networking.Messages switch (_info.type) { case RegisterSyncType.RegisterAndSpawn: - byteEncoder.WriteString(_info.spawnInfo.Value.poolName); + byteEncoder.WriteString(_info.spawnInfo.Value.spawnableTitle); byteEncoder.WriteSimpleTransform(_info.spawnInfo.Value.spawnLocation); break; case RegisterSyncType.RegisterFromPath: @@ -66,7 +67,7 @@ namespace BoneSync.Networking.Messages case RegisterSyncType.RegisterAndSpawn: _info.spawnInfo = new SpawnPoolableInfo() { - poolName = byteEncoder.ReadString(), + spawnableTitle = byteEncoder.ReadString(), spawnLocation = byteEncoder.ReadSimpleTransform(), }; break; diff --git a/BoneSync/Networking/NetworkMessage.cs b/BoneSync/Networking/NetworkMessage.cs index ddd0d5e..19a49c0 100644 --- a/BoneSync/Networking/NetworkMessage.cs +++ b/BoneSync/Networking/NetworkMessage.cs @@ -1,4 +1,5 @@ -using BoneSync.Networking.Transport; +using BoneSync.Data; +using BoneSync.Networking.Transport; using MelonLoader; using System; using System.Collections.Generic; diff --git a/BoneSync/Networking/Packet.cs b/BoneSync/Networking/Packet.cs index aa7620b..b9772bd 100644 --- a/BoneSync/Networking/Packet.cs +++ b/BoneSync/Networking/Packet.cs @@ -1,4 +1,5 @@ -using BoneSync.Networking.Messages; +using BoneSync.Data; +using BoneSync.Networking.Messages; using Facepunch.Steamworks; using System; using System.Collections.Generic; diff --git a/BoneSync/Patching/GripPatches.cs b/BoneSync/Patching/GripPatches.cs index 14c05ae..4406096 100644 --- a/BoneSync/Patching/GripPatches.cs +++ b/BoneSync/Patching/GripPatches.cs @@ -9,21 +9,26 @@ using HarmonyLib; using StressLevelZero.Interaction; namespace BoneSync.Patching { - [HarmonyPatch(typeof(ForcePullGrip))] - internal class ForcePullGripPatches + + // Credit: Entanglement, this patch is based on the one from Entanglement + [HarmonyPatch(typeof(ForcePullGrip), nameof(ForcePullGrip.OnFarHandHoverUpdate))] + public class ForcePullPatch { - [HarmonyPatch(nameof(ForcePullGrip.Pull)), HarmonyPostfix] - public static void OnEnablePatch(ForcePullGrip __instance) + public static void Prefix(ForcePullGrip __instance, ref bool __state, Hand hand) { - MelonLoader.MelonLogger.Msg("ForcePullGrip.Pull: " + __instance.name); + __state = __instance.pullCoroutine != null; + } + + public static void Postfix(ForcePullGrip __instance, ref bool __state, Hand hand) + { + if (!(__instance.pullCoroutine != null && !__state)) + return; + InteractableHost interactableHost = __instance.GetComponent(); if (interactableHost == null) return; Syncable syncable = ObjectSync.MakeOrGetSyncable(interactableHost); - if (syncable) - { - syncable.RegisterSyncable(); - } + + syncable?.RegisterSyncable(); } - } } diff --git a/BoneSync/Sync/Components/SyncablePhysics.cs b/BoneSync/Sync/Components/SyncablePhysics.cs index dff9d71..aae553f 100644 --- a/BoneSync/Sync/Components/SyncablePhysics.cs +++ b/BoneSync/Sync/Components/SyncablePhysics.cs @@ -1,4 +1,5 @@ -using BoneSync.Networking; +using BoneSync.Data; +using BoneSync.Networking; using BoneSync.Networking.Messages; using BoneSync.Patching; using MelonLoader; @@ -46,7 +47,7 @@ namespace BoneSync.Sync.Components { objectSyncTransforms[i] = new ObjectSyncTransform() { - transform = new SimpleTransform(_transforms[i]), + transform = new SimpleSyncTransform(_transforms[i]), velocity = Vector3.zero }; } diff --git a/BoneSync/Sync/ObjectSync.cs b/BoneSync/Sync/ObjectSync.cs index cd8dde8..1cfa4f1 100644 --- a/BoneSync/Sync/ObjectSync.cs +++ b/BoneSync/Sync/ObjectSync.cs @@ -1,8 +1,10 @@ -using BoneSync.Networking; +using BoneSync.Data; +using BoneSync.Networking; using BoneSync.Networking.Messages; using BoneSync.Patching; using BoneSync.Sync.Components; using MelonLoader; +using StressLevelZero.Data; using StressLevelZero.Interaction; using StressLevelZero.Pool; using System; @@ -47,8 +49,8 @@ namespace BoneSync.Sync type = RegisterSyncType.RegisterAndSpawn, spawnInfo = new SpawnPoolableInfo() { - poolName = syncable.poolee.pool.name, - spawnLocation = new SimpleTransform(syncable.poolee.transform), + spawnableTitle = syncable.poolee.spawnObject.title, + spawnLocation = new SimpleSyncTransform(syncable.poolee.transform), }, }; } @@ -76,7 +78,7 @@ namespace BoneSync.Sync } else { info.callbackId = (ushort)Random.Range(1, ushort.MaxValue); - ObjectSyncCache._callbackIdToSyncable[info.callbackId] = syncable; + ObjectSyncCache.CallbackIdToSyncable[info.callbackId] = syncable; new RegisterSyncableMessage(info).SendToHost(); } @@ -167,20 +169,20 @@ namespace BoneSync.Sync public static Syncable SpawnPooleeAndMakeSyncable(SpawnPoolableInfo spawnInfo) { - - SimpleTransform spawnLocation = spawnInfo.spawnLocation; - // !! This is a bit of a hack, but it works for now - // if pool starts with "pool - " remove it - if (spawnInfo.poolName.StartsWith("pool - ")) - { - spawnInfo.poolName = spawnInfo.poolName.Substring(7); + SimpleSyncTransform spawnLocation = spawnInfo.spawnLocation; + + SpawnableObject spawnableObject = SpawnableManager.GetSpawnable(spawnInfo.spawnableTitle); + if (spawnableObject == null) { + MelonLogger.Warning("[SpawnPooleeAndMakeSyncable] Failed to find spawnable: " + spawnInfo.spawnableTitle); + return null; } - Pool pool = PoolManager.GetPool(spawnInfo.poolName); + Pool pool = SpawnableManager.GetPool(spawnableObject); + if (!pool) { - MelonLogger.Warning("[SpawnPooleeAndMakeSyncable] Failed to find pool: " + spawnInfo.poolName); + MelonLogger.Warning("[SpawnPooleeAndMakeSyncable] Failed to find pool: " + spawnInfo.spawnableTitle); return null; } Poolee poolee = CallPatchedMethods.InstantiatePoolee(pool, spawnLocation.position, spawnLocation.rotation, pool.Prefab.transform.localScale); @@ -219,11 +221,11 @@ namespace BoneSync.Sync MelonLogger.Msg("Received register sync message with callback id: " + info.callbackId); } - if (hasCallback && ObjectSyncCache._callbackIdToSyncable.ContainsKey(info.callbackId)) + if (hasCallback && ObjectSyncCache.CallbackIdToSyncable.ContainsKey(info.callbackId)) { MelonLogger.Msg("Found syncable for callback id: " + info.callbackId); - syncable = ObjectSyncCache._callbackIdToSyncable[info.callbackId]; - ObjectSyncCache._callbackIdToSyncable.Remove(info.callbackId); + syncable = ObjectSyncCache.CallbackIdToSyncable[info.callbackId]; + ObjectSyncCache.CallbackIdToSyncable.Remove(info.callbackId); } else { switch (info.type) @@ -233,7 +235,7 @@ namespace BoneSync.Sync syncable = ObjectSyncCache.GetSyncable(info.transformPath); break; case RegisterSyncType.RegisterAndSpawn: - MelonLogger.Msg("Registering and spawning syncable from pool: " + info.spawnInfo.Value.poolName + " with id: " + info.id); + MelonLogger.Msg("Registering and spawning syncable from pool: " + info.spawnInfo.Value.spawnableTitle + " with id: " + info.id); syncable = SpawnPooleeAndMakeSyncable(info.spawnInfo.Value); break; } diff --git a/BoneSync/Sync/ObjectSyncCache.cs b/BoneSync/Sync/ObjectSyncCache.cs index 07aa461..54ca40e 100644 --- a/BoneSync/Sync/ObjectSyncCache.cs +++ b/BoneSync/Sync/ObjectSyncCache.cs @@ -14,11 +14,10 @@ namespace BoneSync.Sync { private static Dictionary _pathToSyncable = new Dictionary(); private static Dictionary _idToSyncable = new Dictionary(); - private static Dictionary _pooleeToSyncable = new Dictionary(); - private static Dictionary _interactableHostToSyncable = new Dictionary(); - private static Dictionary _interactableHostManagerToSyncable = new Dictionary(); - public static Dictionary _callbackIdToSyncable = new Dictionary(); + //private static Dictionary _componentToSyncable = new Dictionary(); + + public static Dictionary CallbackIdToSyncable = new Dictionary(); public static void AddSyncable(Syncable syncable) { @@ -31,18 +30,6 @@ namespace BoneSync.Sync { _idToSyncable[syncable.GetSyncId()] = syncable; } - if (syncable.interactableHost) - { - _interactableHostToSyncable[syncable.interactableHost] = syncable; - } - if (syncable.interactableManager) - { - _interactableHostManagerToSyncable[syncable.interactableManager] = syncable; - } - if (syncable.poolee) - { - _pooleeToSyncable[syncable.poolee] = syncable; - } } public static void RemoveSyncable(Syncable syncable) @@ -56,18 +43,6 @@ namespace BoneSync.Sync { _idToSyncable.Remove(syncable.GetSyncId()); } - if (syncable.interactableHost) - { - _interactableHostToSyncable.Remove(syncable.interactableHost); - } - if (syncable.interactableManager) - { - _interactableHostManagerToSyncable.Remove(syncable.interactableManager); - } - if (syncable.poolee) - { - _pooleeToSyncable.Remove(syncable.poolee); - } } public static void UpdateSyncId(Syncable syncable) @@ -104,33 +79,6 @@ namespace BoneSync.Sync return null; } - public static Syncable GetSyncable(Poolee poolee) - { - if (_pooleeToSyncable.ContainsKey(poolee)) - { - return _pooleeToSyncable[poolee]; - } - return null; - } - - public static Syncable GetSyncable(InteractableHost interactableHost) - { - if (_interactableHostToSyncable.ContainsKey(interactableHost)) - { - return _interactableHostToSyncable[interactableHost]; - } - return null; - } - - public static Syncable GetSyncable(InteractableHostManager interactableHostManager) - { - if (_interactableHostManagerToSyncable.ContainsKey(interactableHostManager)) - { - return _interactableHostManagerToSyncable[interactableHostManager]; - } - return null; - } - public static void DISCARD_ALL_SYNCABLES() { foreach (Syncable syncable in Syncable.syncablesCache.Values) diff --git a/BoneSync/Sync/PlayerSync.cs b/BoneSync/Sync/PlayerSync.cs index 12cea52..a8850e7 100644 --- a/BoneSync/Sync/PlayerSync.cs +++ b/BoneSync/Sync/PlayerSync.cs @@ -1,4 +1,5 @@ -using BoneSync.Networking; +using BoneSync.Data; +using BoneSync.Networking; using BoneSync.Networking.Messages; using BoneSync.Player; using System; @@ -39,9 +40,9 @@ namespace BoneSync.Sync public static void SyncPlayer() { PlayerSyncInfo playerSyncInfo = new PlayerSyncInfo(); - playerSyncInfo.headPos = new SimpleTransform(); - playerSyncInfo.leftHandPos = new SimpleTransform(); - playerSyncInfo.rightHandPos = new SimpleTransform(); + playerSyncInfo.headPos = new SimpleSyncTransform(); + playerSyncInfo.leftHandPos = new SimpleSyncTransform(); + playerSyncInfo.rightHandPos = new SimpleSyncTransform(); PlayerSyncMessage playerSyncMessage = new PlayerSyncMessage(playerSyncInfo); playerSyncMessage.Broadcast();