From aee4a88391cba8a4c1bb7567f2f5bfb600cd24e9 Mon Sep 17 00:00:00 2001 From: Mister_Nebula <41904486+misternebula@users.noreply.github.com> Date: Thu, 18 Feb 2021 10:34:35 +0000 Subject: [PATCH] fix check for "enabled" --- .../Events/QuantumAuthorityEvent.cs | 11 ++----- QSB/QuantumSync/Patches/QuantumPatches.cs | 31 +++++++++---------- .../Patches/ServerQuantumPatches.cs | 4 +-- QSB/QuantumSync/QuantumManager.cs | 14 ++++++--- .../WorldObjects/QSBQuantumObject.cs | 8 +++-- QSB/Utility/OnEnableDisableTracker.cs | 12 +++++-- QSB/WorldSync/QSBWorldSync.cs | 9 ++++++ 7 files changed, 54 insertions(+), 35 deletions(-) diff --git a/QSB/QuantumSync/Events/QuantumAuthorityEvent.cs b/QSB/QuantumSync/Events/QuantumAuthorityEvent.cs index d7a3041c..de3aafd0 100644 --- a/QSB/QuantumSync/Events/QuantumAuthorityEvent.cs +++ b/QSB/QuantumSync/Events/QuantumAuthorityEvent.cs @@ -1,7 +1,5 @@ using QSB.Events; using QSB.Player; -using QSB.WorldSync; -using System.Linq; namespace QSB.QuantumSync.Events { @@ -28,8 +26,7 @@ namespace QSB.QuantumSync.Events return false; } - var objects = QSBWorldSync.GetWorldObjects(); - var obj = objects.ToList()[message.ObjectId]; + var obj = QuantumManager.Instance.GetObject(message.ObjectId); // Deciding if to change the object's owner // Message @@ -45,15 +42,13 @@ namespace QSB.QuantumSync.Events public override void OnReceiveLocal(bool server, QuantumAuthorityMessage message) { - var objects = QSBWorldSync.GetWorldObjects(); - var obj = objects.ToList()[message.ObjectId]; + var obj = QuantumManager.Instance.GetObject(message.ObjectId); obj.ControllingPlayer = message.AuthorityOwner; } public override void OnReceiveRemote(bool server, QuantumAuthorityMessage message) { - var objects = QSBWorldSync.GetWorldObjects(); - var obj = objects.ToList()[message.ObjectId]; + var obj = QuantumManager.Instance.GetObject(message.ObjectId); obj.ControllingPlayer = message.AuthorityOwner; if (obj.ControllingPlayer == 0 && obj.IsEnabled) { diff --git a/QSB/QuantumSync/Patches/QuantumPatches.cs b/QSB/QuantumSync/Patches/QuantumPatches.cs index 10ce095d..678971e8 100644 --- a/QSB/QuantumSync/Patches/QuantumPatches.cs +++ b/QSB/QuantumSync/Patches/QuantumPatches.cs @@ -45,30 +45,27 @@ namespace QSB.QuantumSync.Patches } public static bool Socketed_ChangeQuantumState(SocketedQuantumObject __instance) - => QSBWorldSync.GetWorldObject(QuantumManager.Instance.GetId(__instance)).ControllingPlayer == QSBPlayerManager.LocalPlayerId; + => QSBWorldSync.GetWorldObject(__instance).ControllingPlayer == QSBPlayerManager.LocalPlayerId; public static void Socketed_MoveToSocket(SocketedQuantumObject __instance, QuantumSocket socket) { - var id = QuantumManager.Instance.GetId(__instance); - var worldObject = QSBWorldSync.GetWorldObject(id); - if (worldObject == null) + var objectWorldObject = QSBWorldSync.GetWorldObject(__instance); + var socketWorldObject = QSBWorldSync.GetWorldObject(socket); + if (objectWorldObject == null) { - DebugLog.ToConsole($"Worldobject is null for id {id}!"); + DebugLog.ToConsole($"Worldobject is null for {__instance.name}!"); return; } - if (worldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId) + if (objectWorldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId) { return; } - var objId = QuantumManager.Instance.GetId(__instance); - var socketId = QuantumManager.Instance.GetId(socket); - //DebugLog.DebugWrite($"{__instance.name} to socket {socketId}"); QSBEventManager.FireEvent( EventNames.QSBSocketStateChange, - objId, - socketId, + objectWorldObject.ObjectId, + socketWorldObject.ObjectId, __instance.transform.localRotation); } @@ -79,7 +76,8 @@ namespace QSB.QuantumSync.Patches ref Transform[] ____shuffledObjects, ref bool __result) { - if (QSBWorldSync.GetWorldObject(QuantumManager.Instance.GetId(__instance)).ControllingPlayer != QSBPlayerManager.LocalPlayerId) + var shuffleWorldObject = QSBWorldSync.GetWorldObject(__instance); + if (shuffleWorldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId) { return false; } @@ -100,7 +98,7 @@ namespace QSB.QuantumSync.Patches //DebugLog.DebugWrite($"{__instance.name} shuffled."); QSBEventManager.FireEvent( EventNames.QSBQuantumShuffle, - QuantumManager.Instance.GetId(__instance), + shuffleWorldObject.ObjectId, ____indexList.ToArray()); __result = true; return false; @@ -108,7 +106,7 @@ namespace QSB.QuantumSync.Patches public static bool MultiState_ChangeQuantumState(MultiStateQuantumObject __instance) { - var qsbObj = QSBWorldSync.GetWorldObject(QuantumManager.Instance.GetId(__instance)); + var qsbObj = QSBWorldSync.GetWorldObject(__instance); var isInControl = qsbObj.ControllingPlayer == QSBPlayerManager.LocalPlayerId; return isInControl; } @@ -129,7 +127,7 @@ namespace QSB.QuantumSync.Patches //DebugLog.DebugWrite($"{owner.AttachedObject.name} to quantum state {Array.IndexOf(owner.QuantumStates, __instance)}"); QSBEventManager.FireEvent( EventNames.QSBMultiStateChange, - QuantumManager.Instance.GetId(owner.AttachedObject), + owner.ObjectId, Array.IndexOf(owner.QuantumStates, __instance)); } @@ -162,7 +160,8 @@ namespace QSB.QuantumSync.Patches public static bool Shrine_ChangeQuantumState(QuantumShrine __instance) { - var isInControl = QSBWorldSync.GetWorldObject(QuantumManager.Instance.GetId(__instance)).ControllingPlayer == QSBPlayerManager.LocalPlayerId; + var shrineWorldObject = QSBWorldSync.GetWorldObject(__instance); + var isInControl = shrineWorldObject.ControllingPlayer == QSBPlayerManager.LocalPlayerId; return isInControl; } diff --git a/QSB/QuantumSync/Patches/ServerQuantumPatches.cs b/QSB/QuantumSync/Patches/ServerQuantumPatches.cs index ad8a37cc..6574375a 100644 --- a/QSB/QuantumSync/Patches/ServerQuantumPatches.cs +++ b/QSB/QuantumSync/Patches/ServerQuantumPatches.cs @@ -12,10 +12,10 @@ namespace QSB.QuantumSync.Patches { public override QSBPatchTypes Type => QSBPatchTypes.OnServerClientConnect; - public override void DoPatches() + public override void DoPatches() => QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ServerQuantumPatches), nameof(Moon_ChangeQuantumState)); - public override void DoUnpatches() + public override void DoUnpatches() => QSBCore.Helper.HarmonyHelper.Unpatch("ChangeQuantumState"); public static bool Moon_ChangeQuantumState( diff --git a/QSB/QuantumSync/QuantumManager.cs b/QSB/QuantumSync/QuantumManager.cs index 37ade1e0..ee2b9a15 100644 --- a/QSB/QuantumSync/QuantumManager.cs +++ b/QSB/QuantumSync/QuantumManager.cs @@ -160,9 +160,15 @@ namespace QSB.QuantumSync .Any(x => VisibilityOccluder.CanYouSee(tracker, x.mainCamera.transform.position)); } - public int GetId(SocketedQuantumObject obj) => _socketedQuantumObjects.IndexOf(obj); - public int GetId(MultiStateQuantumObject obj) => _multiStateQuantumObjects.IndexOf(obj); - public int GetId(QuantumSocket obj) => _quantumSockets.IndexOf(obj); - public int GetId(QuantumShuffleObject obj) => _quantumShuffleObjects.IndexOf(obj); + public int GetId(IQSBQuantumObject obj) + => QSBWorldSync + .GetWorldObjects() + .ToList() + .IndexOf(obj); + + public IQSBQuantumObject GetObject(int id) + => QSBWorldSync + .GetWorldObjects() + .ToList()[id]; } } \ No newline at end of file diff --git a/QSB/QuantumSync/WorldObjects/QSBQuantumObject.cs b/QSB/QuantumSync/WorldObjects/QSBQuantumObject.cs index 8ff4bc13..690af6e4 100644 --- a/QSB/QuantumSync/WorldObjects/QSBQuantumObject.cs +++ b/QSB/QuantumSync/WorldObjects/QSBQuantumObject.cs @@ -1,4 +1,5 @@ -using QSB.Events; +using OWML.Utils; +using QSB.Events; using QSB.Player; using QSB.Utility; using QSB.WorldSync; @@ -31,6 +32,7 @@ namespace QSB.QuantumSync.WorldObjects ControllingPlayer = 0u; } + private void OnEnable() { IsEnabled = true; @@ -43,7 +45,7 @@ namespace QSB.QuantumSync.WorldObjects // controlled by another player, dont care that we activate it return; } - var id = QSBWorldSync.GetWorldObjects().ToList().IndexOf(this); + var id = QuantumManager.Instance.GetId(this); // no one is controlling this object right now, request authority QSBEventManager.FireEvent(EventNames.QSBQuantumAuthority, id, QSBPlayerManager.LocalPlayerId); } @@ -60,7 +62,7 @@ namespace QSB.QuantumSync.WorldObjects // not being controlled by us, don't care if we leave area return; } - var id = QSBWorldSync.GetWorldObjects().ToList().IndexOf(this); + var id = QuantumManager.Instance.GetId(this); // send event to other players that we're releasing authority QSBEventManager.FireEvent(EventNames.QSBQuantumAuthority, id, 0u); } diff --git a/QSB/Utility/OnEnableDisableTracker.cs b/QSB/Utility/OnEnableDisableTracker.cs index 988a6d7c..9c19247c 100644 --- a/QSB/Utility/OnEnableDisableTracker.cs +++ b/QSB/Utility/OnEnableDisableTracker.cs @@ -1,4 +1,6 @@ -using System; +using OWML.Utils; +using System; +using System.Linq; using UnityEngine; namespace QSB.Utility @@ -18,6 +20,12 @@ namespace QSB.Utility private void OnDestroy() => QSBSceneManager.OnSceneLoaded -= (OWScene scene, bool inUniverse) => Destroy(this); + private bool GetAnyVisibilityTrackersActive() + { + var visibilityTrackers = AttachedComponent.GetValue("_visibilityTrackers"); + return visibilityTrackers.All(x => x.GetValue("_shapes").All(y => y.enabled)); + } + private void Update() { if (AttachedComponent == null) @@ -25,7 +33,7 @@ namespace QSB.Utility DebugLog.ToConsole($"Attached component is null!", OWML.Common.MessageType.Error); return; } - var state = AttachedComponent.isActiveAndEnabled ? ComponentState.Enabled : ComponentState.Disabled; + var state = AttachedComponent.isActiveAndEnabled && GetAnyVisibilityTrackersActive() ? ComponentState.Enabled : ComponentState.Disabled; if (_wasEnabled != state) { _wasEnabled = state; diff --git a/QSB/WorldSync/QSBWorldSync.cs b/QSB/WorldSync/QSBWorldSync.cs index d53d5ed1..52af4abd 100644 --- a/QSB/WorldSync/QSBWorldSync.cs +++ b/QSB/WorldSync/QSBWorldSync.cs @@ -27,6 +27,15 @@ namespace QSB.WorldSync where TWorldObject : IWorldObject => GetWorldObjects().FirstOrDefault(x => x.ObjectId == id); + public static TWorldObject GetWorldObject(TUnityObject unityObject) + where TWorldObject : WorldObject + where TUnityObject : MonoBehaviour + { + var allWorldObjects = GetWorldObjects(); + var correctWorldObject = allWorldObjects.First(x => x.AttachedObject == unityObject); + return correctWorldObject; + } + public static void RemoveWorldObjects() { var itemsToRemove = WorldObjects.Where(x => x is TWorldObject);