From 11cefff4f217ec9f63634b80f261b13ec54e8565 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Tue, 25 Jan 2022 19:42:22 -0800 Subject: [PATCH] fix a dumb orb transform sync bug --- .../TransformSync/NomaiOrbTransformSync.cs | 38 +++++++++---------- QSB/Syncs/SyncBase.cs | 8 ++-- QSB/Utility/Extensions.cs | 28 +++++++------- QSB/WorldSync/QSBWorldSync.cs | 24 +++--------- QSB/WorldSync/WorldObjectManager.cs | 2 + 5 files changed, 42 insertions(+), 58 deletions(-) diff --git a/QSB/OrbSync/TransformSync/NomaiOrbTransformSync.cs b/QSB/OrbSync/TransformSync/NomaiOrbTransformSync.cs index 4c60c11b..ca2c7596 100644 --- a/QSB/OrbSync/TransformSync/NomaiOrbTransformSync.cs +++ b/QSB/OrbSync/TransformSync/NomaiOrbTransformSync.cs @@ -1,8 +1,6 @@ -using OWML.Common; -using QSB.AuthoritySync; +using QSB.AuthoritySync; using QSB.OrbSync.WorldObjects; using QSB.Syncs.Unsectored.Transforms; -using QSB.Utility; using QSB.WorldSync; using System.Collections.Generic; using UnityEngine; @@ -15,14 +13,14 @@ namespace QSB.OrbSync.TransformSync /// normally prints error when attached object is null. /// this overrides it so that doesn't happen, since the orb can be destroyed. /// - protected override bool CheckValid() => _attachedBody && base.CheckValid(); + protected override bool CheckValid() => AttachedTransform && base.CheckValid(); + protected override bool UseInterpolation => true; protected override float DistanceLeeway => 1f; protected override Transform InitLocalTransform() => _qsbOrb.AttachedObject.transform; protected override Transform InitRemoteTransform() => _qsbOrb.AttachedObject.transform; - private OWRigidbody _attachedBody; private QSBOrb _qsbOrb; private static readonly List _instances = new(); @@ -40,28 +38,21 @@ namespace QSB.OrbSync.TransformSync protected override void Init() { - var index = _instances.IndexOf(this); - if (!OrbManager.Orbs.TryGet(index, out var orb)) - { - DebugLog.ToConsole($"Error - No orb at index {index}.", MessageType.Error); - return; - } - - _qsbOrb = orb.GetWorldObject(); + _qsbOrb = OrbManager.Orbs[_instances.IndexOf(this)].GetWorldObject(); _qsbOrb.TransformSync = this; base.Init(); - _attachedBody = AttachedTransform.GetAttachedOWRigidbody(); - SetReferenceTransform(_attachedBody.GetOrigParent()); + var body = AttachedTransform.GetAttachedOWRigidbody(); + SetReferenceTransform(body.GetOrigParent()); if (QSBCore.IsHost) { netIdentity.RegisterAuthQueue(); } - _attachedBody.OnUnsuspendOWRigidbody += OnUnsuspend; - _attachedBody.OnSuspendOWRigidbody += OnSuspend; - netIdentity.SendAuthQueueMessage(_attachedBody.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add); + body.OnUnsuspendOWRigidbody += OnUnsuspend; + body.OnSuspendOWRigidbody += OnSuspend; + netIdentity.SendAuthQueueMessage(body.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add); } protected override void Uninit() @@ -71,9 +62,14 @@ namespace QSB.OrbSync.TransformSync netIdentity.UnregisterAuthQueue(); } - _attachedBody.OnUnsuspendOWRigidbody -= OnUnsuspend; - _attachedBody.OnSuspendOWRigidbody -= OnSuspend; - _attachedBody = null; + // this is null sometimes on here, but not on other similar transforms syncs (like anglers) + // idk why, but whatever + if (AttachedTransform) + { + var body = AttachedTransform.GetAttachedOWRigidbody(); + body.OnUnsuspendOWRigidbody -= OnUnsuspend; + body.OnSuspendOWRigidbody -= OnSuspend; + } base.Uninit(); } diff --git a/QSB/Syncs/SyncBase.cs b/QSB/Syncs/SyncBase.cs index 55b4d179..742fe586 100644 --- a/QSB/Syncs/SyncBase.cs +++ b/QSB/Syncs/SyncBase.cs @@ -150,7 +150,7 @@ namespace QSB.Syncs QSBSceneManager.OnSceneLoaded -= OnSceneLoaded; if (IsInitialized) { - Uninit(); + this.Try("uninitializing (from object destroy)", Uninit); } } @@ -158,7 +158,7 @@ namespace QSB.Syncs { if (IsInitialized) { - Uninit(); + this.Try("uninitializing (from scene change)", Uninit); } } @@ -196,11 +196,11 @@ namespace QSB.Syncs { if (!IsInitialized && CheckReady()) { - Init(); + this.Try("initializing", Init); } else if (IsInitialized && !CheckReady()) { - Uninit(); + this.Try("uninitializing", Uninit); base.Update(); return; } diff --git a/QSB/Utility/Extensions.cs b/QSB/Utility/Extensions.cs index 8418ec86..bf668142 100644 --- a/QSB/Utility/Extensions.cs +++ b/QSB/Utility/Extensions.cs @@ -103,18 +103,6 @@ namespace QSB.Utility public static bool IsInRange(this IList list, int index) => index >= 0 && index < list.Count; - public static bool TryGet(this IList list, int index, out T element) - { - if (!list.IsInRange(index)) - { - element = default; - return false; - } - - element = list[index]; - return true; - } - public static void RaiseEvent(this T instance, string eventName, params object[] args) { const BindingFlags flags = BindingFlags.Instance @@ -123,8 +111,8 @@ namespace QSB.Utility | BindingFlags.NonPublic | BindingFlags.DeclaredOnly; if (typeof(T) - .GetField(eventName, flags)? - .GetValue(instance) is not MulticastDelegate multiDelegate) + .GetField(eventName, flags)? + .GetValue(instance) is not MulticastDelegate multiDelegate) { return; } @@ -142,6 +130,18 @@ namespace QSB.Utility return new Guid(bytes); } + public static void Try(this object self, string description, Action action) + { + try + { + action(); + } + catch (Exception e) + { + DebugLog.ToConsole($"{self} - error {description} : {e}", MessageType.Error); + } + } + #endregion } } diff --git a/QSB/WorldSync/QSBWorldSync.cs b/QSB/WorldSync/QSBWorldSync.cs index 32f05e44..1b3917e5 100644 --- a/QSB/WorldSync/QSBWorldSync.cs +++ b/QSB/WorldSync/QSBWorldSync.cs @@ -46,19 +46,12 @@ namespace QSB.WorldSync { case WorldObjectType.SolarSystem when QSBSceneManager.CurrentScene != OWScene.SolarSystem: case WorldObjectType.Eye when QSBSceneManager.CurrentScene != OWScene.EyeOfTheUniverse: - DebugLog.DebugWrite($"skipping {manager.GetType().Name} as it is type {manager.WorldObjectType} and scene is {QSBSceneManager.CurrentScene}"); + DebugLog.DebugWrite($"skipping {manager} as it is type {manager.WorldObjectType} and scene is {QSBSceneManager.CurrentScene}"); continue; } - try - { - DebugLog.DebugWrite($"Building {manager.GetType().Name}", MessageType.Info); - manager.BuildWorldObjects(scene); - } - catch (Exception ex) - { - DebugLog.ToConsole($"Exception - Exception when trying to build WorldObjects of manager {manager.GetType().Name} : {ex}", MessageType.Error); - } + DebugLog.DebugWrite($"Building {manager}", MessageType.Info); + manager.Try("building world objects", () => manager.BuildWorldObjects(scene)); } QSBCore.UnityEvents.RunWhen(() => _numManagersReadying == 0, () => @@ -87,14 +80,7 @@ namespace QSB.WorldSync foreach (var item in WorldObjects) { - try - { - item.OnRemoval(); - } - catch (Exception e) - { - DebugLog.ToConsole($"Error - Exception in OnRemoval() for {item.GetType()}. {e}", MessageType.Error); - } + item.Try("removing", item.OnRemoval); } WorldObjects.Clear(); @@ -102,7 +88,7 @@ namespace QSB.WorldSync foreach (var manager in Managers) { - manager.UnbuildWorldObjects(); + manager.Try("unbuilding world objects", manager.UnbuildWorldObjects); } } diff --git a/QSB/WorldSync/WorldObjectManager.cs b/QSB/WorldSync/WorldObjectManager.cs index fd62ea8c..ca66290c 100644 --- a/QSB/WorldSync/WorldObjectManager.cs +++ b/QSB/WorldSync/WorldObjectManager.cs @@ -25,5 +25,7 @@ namespace QSB.WorldSync /// indicates that this is now ready protected void FinishDelayedReady() => QSBWorldSync._numManagersReadying--; + + public override string ToString() => GetType().Name; } }