fix check for "enabled"

This commit is contained in:
Mister_Nebula 2021-02-18 10:34:35 +00:00
parent 8a5afcfb5b
commit aee4a88391
7 changed files with 54 additions and 35 deletions

View File

@ -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<IQSBQuantumObject>();
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<IQSBQuantumObject>();
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<IQSBQuantumObject>();
var obj = objects.ToList()[message.ObjectId];
var obj = QuantumManager.Instance.GetObject(message.ObjectId);
obj.ControllingPlayer = message.AuthorityOwner;
if (obj.ControllingPlayer == 0 && obj.IsEnabled)
{

View File

@ -45,30 +45,27 @@ namespace QSB.QuantumSync.Patches
}
public static bool Socketed_ChangeQuantumState(SocketedQuantumObject __instance)
=> QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject>(QuantumManager.Instance.GetId(__instance)).ControllingPlayer == QSBPlayerManager.LocalPlayerId;
=> QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject, SocketedQuantumObject>(__instance).ControllingPlayer == QSBPlayerManager.LocalPlayerId;
public static void Socketed_MoveToSocket(SocketedQuantumObject __instance, QuantumSocket socket)
{
var id = QuantumManager.Instance.GetId(__instance);
var worldObject = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject>(id);
if (worldObject == null)
var objectWorldObject = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject, SocketedQuantumObject>(__instance);
var socketWorldObject = QSBWorldSync.GetWorldObject<QSBQuantumSocket, QuantumSocket>(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<QSBQuantumShuffleObject>(QuantumManager.Instance.GetId(__instance)).ControllingPlayer != QSBPlayerManager.LocalPlayerId)
var shuffleWorldObject = QSBWorldSync.GetWorldObject<QSBQuantumShuffleObject, QuantumShuffleObject>(__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<QSBMultiStateQuantumObject>(QuantumManager.Instance.GetId(__instance));
var qsbObj = QSBWorldSync.GetWorldObject<QSBMultiStateQuantumObject, MultiStateQuantumObject>(__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<QSBSocketedQuantumObject>(QuantumManager.Instance.GetId(__instance)).ControllingPlayer == QSBPlayerManager.LocalPlayerId;
var shrineWorldObject = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject, SocketedQuantumObject>(__instance);
var isInControl = shrineWorldObject.ControllingPlayer == QSBPlayerManager.LocalPlayerId;
return isInControl;
}

View File

@ -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<QuantumMoon>("ChangeQuantumState", typeof(ServerQuantumPatches), nameof(Moon_ChangeQuantumState));
public override void DoUnpatches()
public override void DoUnpatches()
=> QSBCore.Helper.HarmonyHelper.Unpatch<QuantumMoon>("ChangeQuantumState");
public static bool Moon_ChangeQuantumState(

View File

@ -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<IQSBQuantumObject>()
.ToList()
.IndexOf(obj);
public IQSBQuantumObject GetObject(int id)
=> QSBWorldSync
.GetWorldObjects<IQSBQuantumObject>()
.ToList()[id];
}
}

View File

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

View File

@ -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<VisibilityTracker[]>("_visibilityTrackers");
return visibilityTrackers.All(x => x.GetValue<Shape[]>("_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;

View File

@ -27,6 +27,15 @@ namespace QSB.WorldSync
where TWorldObject : IWorldObject
=> GetWorldObjects<TWorldObject>().FirstOrDefault(x => x.ObjectId == id);
public static TWorldObject GetWorldObject<TWorldObject, TUnityObject>(TUnityObject unityObject)
where TWorldObject : WorldObject<TUnityObject>
where TUnityObject : MonoBehaviour
{
var allWorldObjects = GetWorldObjects<TWorldObject>();
var correctWorldObject = allWorldObjects.First(x => x.AttachedObject == unityObject);
return correctWorldObject;
}
public static void RemoveWorldObjects<TWorldObject>()
{
var itemsToRemove = WorldObjects.Where(x => x is TWorldObject);