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.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,

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
using BoneSync.Networking.Transport;
using BoneSync.Data;
using BoneSync.Networking.Transport;
using MelonLoader;
using System;
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 System;
using System.Collections.Generic;

View File

@@ -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<InteractableHost>();
if (interactableHost == null) return;
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.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
};
}

View File

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

View File

@@ -14,11 +14,10 @@ namespace BoneSync.Sync
{
private static Dictionary<string, Syncable> _pathToSyncable = new Dictionary<string, 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)
{
@@ -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)

View File

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