Client spawning fix

This commit is contained in:
Aaro Varis
2025-03-02 01:30:39 +02:00
parent 76062e026e
commit b485ab3259
4 changed files with 56 additions and 21 deletions

View File

@@ -14,7 +14,7 @@ namespace BoneSync.Networking.Messages
public ulong NewOwnerId; public ulong NewOwnerId;
} }
[PacketType(PacketType.ObjectOwnership)] [PacketType(PacketType.ObjectOwnership), PacketReliability(PacketReliability.ReliableFast)]
internal class OnwershipTransferMessage : NetworkMessage internal class OnwershipTransferMessage : NetworkMessage
{ {
private OnwershipTransferMessageData Data; private OnwershipTransferMessageData Data;

View File

@@ -74,6 +74,7 @@ namespace BoneSync.Patching
[HarmonyPostfix] [HarmonyPostfix]
private static void InstantiatePooleePatchPost(Pool __instance, Poolee __result, Vector3 position, Quaternion rotation) private static void InstantiatePooleePatchPost(Pool __instance, Poolee __result, Vector3 position, Quaternion rotation)
{ {
if (CallPatchedMethods.allowPatchedMethodCall) return;
if (__instance == null) return; if (__instance == null) return;
if (PoolBlacklist.isBlacklistedPool(__instance)) return; if (PoolBlacklist.isBlacklistedPool(__instance)) return;
MelonLogger.Msg("Patched Instantiating object in pool: " + __instance.name); MelonLogger.Msg("Patched Instantiating object in pool: " + __instance.name);
@@ -128,7 +129,7 @@ namespace BoneSync.Patching
MelonLogger.Msg("Poolee.OnSpawn: " + __instance.gameObject.transform.GetPath()); MelonLogger.Msg("Poolee.OnSpawn: " + __instance.gameObject.transform.GetPath());
ObjectSync.MakeOrGetSyncable(__instance); Syncable syncable = ObjectSync.MakeOrGetSyncable(__instance);
bool spawnNormally = BoneSync.lobby.IsHost || PoolBlacklist.IsClientSpawnPool(__instance.pool); bool spawnNormally = BoneSync.lobby.IsHost || PoolBlacklist.IsClientSpawnPool(__instance.pool);

View File

@@ -92,6 +92,17 @@ namespace BoneSync.Sync.Components
private SpawnFragment spawnFragment; private SpawnFragment spawnFragment;
private IEnumerator _CheckAutoSyncCo()
{
yield return null;
bool shouldAutoSync = CheckIfShouldAutoSync();
if (shouldAutoSync && (BoneSync.lobby.IsHost || ClientSpawningAllowed()))
{
MelonLogger.Msg("AutoSyncing: " + transform.GetPath());
RegisterSyncable();
}
yield break;
}
public void OnEnable() public void OnEnable()
{ {
@@ -99,12 +110,7 @@ namespace BoneSync.Sync.Components
FindComponents(); FindComponents();
bool shouldAutoSync = CheckIfShouldAutoSync(); MelonCoroutines.Start(_CheckAutoSyncCo());
if (shouldAutoSync && (BoneSync.lobby.IsHost || ClientSpawningAllowed()))
{
MelonLogger.Msg("AutoSyncing: " + transform.GetPath());
RegisterSyncable();
}
} }
public bool CheckIfShouldAutoSync() public bool CheckIfShouldAutoSync()
@@ -156,26 +162,49 @@ namespace BoneSync.Sync.Components
return false; return false;
} }
public void OnDestroy() private void _DiscardSyncable(bool force)
{
DiscardSyncable();
//MelonLogger.Msg("Syncable destroyed: " + transform.GetPath());
}
public void DiscardSyncable()
{ {
if (Registered) if (Registered)
{ {
MelonLogger.Warning("Discarding registered syncable: " + transform.GetPath()); MelonLogger.Warning("Discarding registered syncable: " + transform.GetPath() + " force: " + force);
if (!force) return;
} }
syncablesCache.Remove(gameObject); syncablesCache.Remove(gameObject);
ObjectSyncCache.RemoveSyncable(this); ObjectSyncCache.RemoveSyncable(this);
Destroy(this); Destroy(this); // delete the component
}
public void OnDestroy()
{
if (Registered)
{
MelonLogger.Warning("Destroying registered syncable: " + transform.GetPath());
}
_DiscardSyncable(true);
//MelonLogger.Msg("Syncable destroyed: " + transform.GetPath());
}
private IEnumerator _FlagForDiscardCo(bool force)
{
yield return null;
_DiscardSyncable(force);
}
public void DiscardSyncable(bool force = false)
{
MelonCoroutines.Start(_FlagForDiscardCo(force));
} }
public void OnDisable() public void OnDisable()
{ {
DiscardSyncable(); if (Registered && !isOwner)
{
MelonLogger.Warning("tried to disable non-owner syncable: " + transform.GetPath());
gameObject.SetActive(true);
} else
{
DiscardSyncable();
}
} }
public void RegisterSyncable() public void RegisterSyncable()

View File

@@ -111,7 +111,7 @@ namespace BoneSync.Sync
// delete all sub syncables // delete all sub syncables
if (deleteSubSyncabled) if (deleteSubSyncabled)
{ {
Syncable[] subSyncables = gameObject.GetComponentsInChildren<Syncable>(true); Syncable[] subSyncables = gameObject.GetComponentsInChildren<Syncable>();
for (int i = 0; i < subSyncables.Length; i++) for (int i = 0; i < subSyncables.Length; i++)
{ {
if (subSyncables[i] != syncable) if (subSyncables[i] != syncable)
@@ -244,10 +244,15 @@ namespace BoneSync.Sync
public static void OnObjectSyncMessage(ObjectSyncMessage objectSyncMessage) public static void OnObjectSyncMessage(ObjectSyncMessage objectSyncMessage)
{ {
ObjectSyncMessageData data = objectSyncMessage.objectSyncMessageData; ObjectSyncMessageData data = objectSyncMessage.objectSyncMessageData;
Syncable syncable = ObjectSyncCache.GetSyncable(data.objectId); ushort objectId = data.objectId;
if (objectId >= _nextSyncableId && !BoneSync.lobby.IsHost)
{
_nextSyncableId = (ushort)(objectId + 1);
}
Syncable syncable = ObjectSyncCache.GetSyncable(objectId);
if (syncable == null) if (syncable == null)
{ {
MelonLogger.Msg("SyncEvent: Syncable not found for id: " + data.objectId); MelonLogger.Msg("SyncEvent: Syncable not found for id: " + objectId);
return; return;
} }
syncable.ApplyObjectSyncTransforms(data.objectSyncTransforms); syncable.ApplyObjectSyncTransforms(data.objectSyncTransforms);