pool changes

This commit is contained in:
2025-03-03 16:23:40 +02:00
parent c11256f9a1
commit 6402afc72b
14 changed files with 129 additions and 108 deletions

View File

@@ -6,7 +6,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace BoneSync.Networking namespace BoneSync.Data
{ {
public static class BitPacking public static class BitPacking
{ {
@@ -194,19 +194,19 @@ namespace BoneSync.Networking
return UnityEngine.Quaternion.Euler(eulerAngles); return UnityEngine.Quaternion.Euler(eulerAngles);
} }
public void WriteSimpleTransform(SimpleTransform value) public void WriteSimpleTransform(SimpleSyncTransform value)
{ {
WriteVector3(value.position); WriteVector3(value.position);
WriteQuaternion(value.rotation); WriteQuaternion(value.rotation);
WriteVector3(value.scale); WriteVector3(value.scale);
} }
public SimpleTransform ReadSimpleTransform() public SimpleSyncTransform ReadSimpleTransform()
{ {
UnityEngine.Vector3 position = ReadVector3(); UnityEngine.Vector3 position = ReadVector3();
UnityEngine.Quaternion rotation = ReadQuaternion(); UnityEngine.Quaternion rotation = ReadQuaternion();
UnityEngine.Vector3 scale = ReadVector3(); UnityEngine.Vector3 scale = ReadVector3();
return new SimpleTransform() return new SimpleSyncTransform()
{ {
position = position, position = position,
rotation = rotation, rotation = rotation,

View File

@@ -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<string, SpawnableObject> _spawnableCache = new Dictionary<string, SpawnableObject>();
private static void _UpdateSpawnableCache()
{
Il2CppReferenceArray<UnityObject> foundSpawnables = UnityObject.FindObjectsOfTypeIncludingAssets(Il2CppType.Of<SpawnableObject>());
foreach (UnityObject obj in foundSpawnables)
{
SpawnableObject spawnable = obj.Cast<SpawnableObject>();
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();
}
}
}

View File

@@ -5,24 +5,24 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
namespace BoneSync.Networking namespace BoneSync.Data
{ {
public static class SimpleTransformExtensions 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.position = simpleTransform.position;
transform.rotation = simpleTransform.rotation; transform.rotation = simpleTransform.rotation;
transform.localScale = simpleTransform.scale; transform.localScale = simpleTransform.scale;
} }
} }
public struct SimpleTransform public struct SimpleSyncTransform
{ {
public Vector3 position; public Vector3 position;
public Quaternion rotation; public Quaternion rotation;
public Vector3 scale; public Vector3 scale;
public SimpleTransform(UnityEngine.Transform transform) public SimpleSyncTransform(UnityEngine.Transform transform)
{ {
position = transform.position; position = transform.position;
rotation = transform.rotation; rotation = transform.rotation;

View File

@@ -8,6 +8,7 @@ using BoneSync.Player;
using BoneSync.Sync; using BoneSync.Sync;
using Facepunch.Steamworks; using Facepunch.Steamworks;
using System.Reflection.Emit; using System.Reflection.Emit;
using BoneSync.Data;
namespace BoneSync namespace BoneSync
{ {
@@ -36,6 +37,8 @@ namespace BoneSync
SceneSync.Initialize(); SceneSync.Initialize();
NetworkMessage.RegisterPacketTypes(); NetworkMessage.RegisterPacketTypes();
SpawnableManager.Initialize();
PatchAll(); PatchAll();
} }

View File

@@ -1,4 +1,5 @@
using BoneSync.Sync; using BoneSync.Data;
using BoneSync.Sync;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -11,7 +12,7 @@ namespace BoneSync.Networking.Messages
public struct ObjectSyncTransform public struct ObjectSyncTransform
{ {
public SimpleTransform transform; public SimpleSyncTransform transform;
public Vector3 velocity; public Vector3 velocity;
public Vector3 angularVelocity; public Vector3 angularVelocity;
} }

View File

@@ -1,5 +1,6 @@
 
using BoneSync.Data;
using BoneSync.Sync; using BoneSync.Sync;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -12,9 +13,9 @@ namespace BoneSync.Networking.Messages
public struct PlayerSyncInfo public struct PlayerSyncInfo
{ {
public SimpleTransform headPos; public SimpleSyncTransform headPos;
public SimpleTransform leftHandPos; public SimpleSyncTransform leftHandPos;
public SimpleTransform rightHandPos; public SimpleSyncTransform rightHandPos;
} }
[PacketType(PacketType.PlayerSync)] [PacketType(PacketType.PlayerSync)]

View File

@@ -1,4 +1,5 @@
using BoneSync.Patching; using BoneSync.Data;
using BoneSync.Patching;
using BoneSync.Sync; using BoneSync.Sync;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -16,8 +17,8 @@ namespace BoneSync.Networking.Messages
public struct SpawnPoolableInfo public struct SpawnPoolableInfo
{ {
public string poolName; public string spawnableTitle;
public SimpleTransform spawnLocation; public SimpleSyncTransform spawnLocation;
} }
public struct RegisterSyncableInfo public struct RegisterSyncableInfo
{ {
@@ -44,7 +45,7 @@ namespace BoneSync.Networking.Messages
switch (_info.type) switch (_info.type)
{ {
case RegisterSyncType.RegisterAndSpawn: case RegisterSyncType.RegisterAndSpawn:
byteEncoder.WriteString(_info.spawnInfo.Value.poolName); byteEncoder.WriteString(_info.spawnInfo.Value.spawnableTitle);
byteEncoder.WriteSimpleTransform(_info.spawnInfo.Value.spawnLocation); byteEncoder.WriteSimpleTransform(_info.spawnInfo.Value.spawnLocation);
break; break;
case RegisterSyncType.RegisterFromPath: case RegisterSyncType.RegisterFromPath:
@@ -66,7 +67,7 @@ namespace BoneSync.Networking.Messages
case RegisterSyncType.RegisterAndSpawn: case RegisterSyncType.RegisterAndSpawn:
_info.spawnInfo = new SpawnPoolableInfo() _info.spawnInfo = new SpawnPoolableInfo()
{ {
poolName = byteEncoder.ReadString(), spawnableTitle = byteEncoder.ReadString(),
spawnLocation = byteEncoder.ReadSimpleTransform(), spawnLocation = byteEncoder.ReadSimpleTransform(),
}; };
break; break;

View File

@@ -1,4 +1,5 @@
using BoneSync.Networking.Transport; using BoneSync.Data;
using BoneSync.Networking.Transport;
using MelonLoader; using MelonLoader;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

View File

@@ -1,4 +1,5 @@
using BoneSync.Networking.Messages; using BoneSync.Data;
using BoneSync.Networking.Messages;
using Facepunch.Steamworks; using Facepunch.Steamworks;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

View File

@@ -9,21 +9,26 @@ using HarmonyLib;
using StressLevelZero.Interaction; using StressLevelZero.Interaction;
namespace BoneSync.Patching 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 Prefix(ForcePullGrip __instance, ref bool __state, Hand hand)
public static void OnEnablePatch(ForcePullGrip __instance)
{ {
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<InteractableHost>(); InteractableHost interactableHost = __instance.GetComponent<InteractableHost>();
if (interactableHost == null) return; if (interactableHost == null) return;
Syncable syncable = ObjectSync.MakeOrGetSyncable(interactableHost); Syncable syncable = ObjectSync.MakeOrGetSyncable(interactableHost);
if (syncable)
{
syncable.RegisterSyncable();
}
}
syncable?.RegisterSyncable();
}
} }
} }

View File

@@ -1,4 +1,5 @@
using BoneSync.Networking; using BoneSync.Data;
using BoneSync.Networking;
using BoneSync.Networking.Messages; using BoneSync.Networking.Messages;
using BoneSync.Patching; using BoneSync.Patching;
using MelonLoader; using MelonLoader;
@@ -46,7 +47,7 @@ namespace BoneSync.Sync.Components
{ {
objectSyncTransforms[i] = new ObjectSyncTransform() objectSyncTransforms[i] = new ObjectSyncTransform()
{ {
transform = new SimpleTransform(_transforms[i]), transform = new SimpleSyncTransform(_transforms[i]),
velocity = Vector3.zero velocity = Vector3.zero
}; };
} }

View File

@@ -1,8 +1,10 @@
using BoneSync.Networking; using BoneSync.Data;
using BoneSync.Networking;
using BoneSync.Networking.Messages; using BoneSync.Networking.Messages;
using BoneSync.Patching; using BoneSync.Patching;
using BoneSync.Sync.Components; using BoneSync.Sync.Components;
using MelonLoader; using MelonLoader;
using StressLevelZero.Data;
using StressLevelZero.Interaction; using StressLevelZero.Interaction;
using StressLevelZero.Pool; using StressLevelZero.Pool;
using System; using System;
@@ -47,8 +49,8 @@ namespace BoneSync.Sync
type = RegisterSyncType.RegisterAndSpawn, type = RegisterSyncType.RegisterAndSpawn,
spawnInfo = new SpawnPoolableInfo() spawnInfo = new SpawnPoolableInfo()
{ {
poolName = syncable.poolee.pool.name, spawnableTitle = syncable.poolee.spawnObject.title,
spawnLocation = new SimpleTransform(syncable.poolee.transform), spawnLocation = new SimpleSyncTransform(syncable.poolee.transform),
}, },
}; };
} }
@@ -76,7 +78,7 @@ namespace BoneSync.Sync
} else } else
{ {
info.callbackId = (ushort)Random.Range(1, ushort.MaxValue); info.callbackId = (ushort)Random.Range(1, ushort.MaxValue);
ObjectSyncCache._callbackIdToSyncable[info.callbackId] = syncable; ObjectSyncCache.CallbackIdToSyncable[info.callbackId] = syncable;
new RegisterSyncableMessage(info).SendToHost(); new RegisterSyncableMessage(info).SendToHost();
} }
@@ -168,19 +170,19 @@ namespace BoneSync.Sync
public static Syncable SpawnPooleeAndMakeSyncable(SpawnPoolableInfo spawnInfo) public static Syncable SpawnPooleeAndMakeSyncable(SpawnPoolableInfo spawnInfo)
{ {
SimpleTransform spawnLocation = spawnInfo.spawnLocation; SimpleSyncTransform spawnLocation = spawnInfo.spawnLocation;
// !! This is a bit of a hack, but it works for now SpawnableObject spawnableObject = SpawnableManager.GetSpawnable(spawnInfo.spawnableTitle);
// if pool starts with "pool - " remove it if (spawnableObject == null) {
if (spawnInfo.poolName.StartsWith("pool - ")) MelonLogger.Warning("[SpawnPooleeAndMakeSyncable] Failed to find spawnable: " + spawnInfo.spawnableTitle);
{ return null;
spawnInfo.poolName = spawnInfo.poolName.Substring(7);
} }
Pool pool = PoolManager.GetPool(spawnInfo.poolName); Pool pool = SpawnableManager.GetPool(spawnableObject);
if (!pool) if (!pool)
{ {
MelonLogger.Warning("[SpawnPooleeAndMakeSyncable] Failed to find pool: " + spawnInfo.poolName); MelonLogger.Warning("[SpawnPooleeAndMakeSyncable] Failed to find pool: " + spawnInfo.spawnableTitle);
return null; return null;
} }
Poolee poolee = CallPatchedMethods.InstantiatePoolee(pool, spawnLocation.position, spawnLocation.rotation, pool.Prefab.transform.localScale); 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); 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); MelonLogger.Msg("Found syncable for callback id: " + info.callbackId);
syncable = ObjectSyncCache._callbackIdToSyncable[info.callbackId]; syncable = ObjectSyncCache.CallbackIdToSyncable[info.callbackId];
ObjectSyncCache._callbackIdToSyncable.Remove(info.callbackId); ObjectSyncCache.CallbackIdToSyncable.Remove(info.callbackId);
} else } else
{ {
switch (info.type) switch (info.type)
@@ -233,7 +235,7 @@ namespace BoneSync.Sync
syncable = ObjectSyncCache.GetSyncable(info.transformPath); syncable = ObjectSyncCache.GetSyncable(info.transformPath);
break; break;
case RegisterSyncType.RegisterAndSpawn: 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); syncable = SpawnPooleeAndMakeSyncable(info.spawnInfo.Value);
break; break;
} }

View File

@@ -14,11 +14,10 @@ namespace BoneSync.Sync
{ {
private static Dictionary<string, Syncable> _pathToSyncable = new Dictionary<string, Syncable>(); private static Dictionary<string, Syncable> _pathToSyncable = new Dictionary<string, Syncable>();
private static Dictionary<ushort, Syncable> _idToSyncable = new Dictionary<ushort, Syncable>(); private static Dictionary<ushort, Syncable> _idToSyncable = new Dictionary<ushort, Syncable>();
private static Dictionary<Poolee, Syncable> _pooleeToSyncable = new Dictionary<Poolee, Syncable>();
private static Dictionary<InteractableHost, Syncable> _interactableHostToSyncable = new Dictionary<InteractableHost, Syncable>();
private static Dictionary<InteractableHostManager, Syncable> _interactableHostManagerToSyncable = new Dictionary<InteractableHostManager, Syncable>();
public static Dictionary<ushort, Syncable> _callbackIdToSyncable = new Dictionary<ushort, Syncable>(); //private static Dictionary<Component, Syncable> _componentToSyncable = new Dictionary<Component, Syncable>();
public static Dictionary<ushort, Syncable> CallbackIdToSyncable = new Dictionary<ushort, Syncable>();
public static void AddSyncable(Syncable syncable) public static void AddSyncable(Syncable syncable)
{ {
@@ -31,18 +30,6 @@ namespace BoneSync.Sync
{ {
_idToSyncable[syncable.GetSyncId()] = syncable; _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) public static void RemoveSyncable(Syncable syncable)
@@ -56,18 +43,6 @@ namespace BoneSync.Sync
{ {
_idToSyncable.Remove(syncable.GetSyncId()); _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) public static void UpdateSyncId(Syncable syncable)
@@ -104,33 +79,6 @@ namespace BoneSync.Sync
return null; 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() public static void DISCARD_ALL_SYNCABLES()
{ {
foreach (Syncable syncable in Syncable.syncablesCache.Values) foreach (Syncable syncable in Syncable.syncablesCache.Values)

View File

@@ -1,4 +1,5 @@
using BoneSync.Networking; using BoneSync.Data;
using BoneSync.Networking;
using BoneSync.Networking.Messages; using BoneSync.Networking.Messages;
using BoneSync.Player; using BoneSync.Player;
using System; using System;
@@ -39,9 +40,9 @@ namespace BoneSync.Sync
public static void SyncPlayer() public static void SyncPlayer()
{ {
PlayerSyncInfo playerSyncInfo = new PlayerSyncInfo(); PlayerSyncInfo playerSyncInfo = new PlayerSyncInfo();
playerSyncInfo.headPos = new SimpleTransform(); playerSyncInfo.headPos = new SimpleSyncTransform();
playerSyncInfo.leftHandPos = new SimpleTransform(); playerSyncInfo.leftHandPos = new SimpleSyncTransform();
playerSyncInfo.rightHandPos = new SimpleTransform(); playerSyncInfo.rightHandPos = new SimpleSyncTransform();
PlayerSyncMessage playerSyncMessage = new PlayerSyncMessage(playerSyncInfo); PlayerSyncMessage playerSyncMessage = new PlayerSyncMessage(playerSyncInfo);
playerSyncMessage.Broadcast(); playerSyncMessage.Broadcast();