fix a dumb orb transform sync bug

This commit is contained in:
JohnCorby 2022-01-25 19:42:22 -08:00
parent 88aa0b6978
commit 11cefff4f2
5 changed files with 42 additions and 58 deletions

View File

@ -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.
/// </summary>
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<NomaiOrbTransformSync> _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>();
_qsbOrb = OrbManager.Orbs[_instances.IndexOf(this)].GetWorldObject<QSBOrb>();
_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();
}

View File

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

View File

@ -103,18 +103,6 @@ namespace QSB.Utility
public static bool IsInRange<T>(this IList<T> list, int index) => index >= 0 && index < list.Count;
public static bool TryGet<T>(this IList<T> list, int index, out T element)
{
if (!list.IsInRange(index))
{
element = default;
return false;
}
element = list[index];
return true;
}
public static void RaiseEvent<T>(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
}
}

View File

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

View File

@ -25,5 +25,7 @@ namespace QSB.WorldSync
/// indicates that this is now ready
protected void FinishDelayedReady() => QSBWorldSync._numManagersReadying--;
public override string ToString() => GetType().Name;
}
}