From 6c2a7c2b6816ac1360a479a9318720700bc388df Mon Sep 17 00:00:00 2001 From: Mister_Nebula <41904486+misternebula@users.noreply.github.com> Date: Tue, 26 Jan 2021 23:41:53 +0000 Subject: [PATCH] shit --- .../Events/MultiStateChangeEvent.cs | 2 + .../Events/QuantumAuthorityEvent.cs | 8 +- .../Patches/ClientQuantumPatches.cs | 9 +- QSB/QuantumSync/Patches/QuantumPatches.cs | 142 ++++++++++++++++++ .../Patches/ServerQuantumPatches.cs | 91 ----------- .../WorldObjects/QSBQuantumObject.cs | 15 +- 6 files changed, 159 insertions(+), 108 deletions(-) diff --git a/QSB/QuantumSync/Events/MultiStateChangeEvent.cs b/QSB/QuantumSync/Events/MultiStateChangeEvent.cs index e36ec17a..10b96824 100644 --- a/QSB/QuantumSync/Events/MultiStateChangeEvent.cs +++ b/QSB/QuantumSync/Events/MultiStateChangeEvent.cs @@ -1,5 +1,6 @@ using QSB.Events; using QSB.QuantumSync.WorldObjects; +using QSB.Utility; using QSB.WorldSync; namespace QSB.QuantumSync.Events @@ -37,6 +38,7 @@ namespace QSB.QuantumSync.Events return; } var qsbObj = QSBWorldSync.GetWorldObject(message.ObjectId); + DebugLog.DebugWrite($"{qsbObj.AttachedObject.name} to state {message.StateIndex}"); qsbObj.ChangeState(message.StateIndex); } } diff --git a/QSB/QuantumSync/Events/QuantumAuthorityEvent.cs b/QSB/QuantumSync/Events/QuantumAuthorityEvent.cs index 48f955fd..04f9a6d1 100644 --- a/QSB/QuantumSync/Events/QuantumAuthorityEvent.cs +++ b/QSB/QuantumSync/Events/QuantumAuthorityEvent.cs @@ -23,18 +23,18 @@ namespace QSB.QuantumSync.Events public override void OnReceiveLocal(bool server, QuantumAuthorityMessage message) { - DebugLog.DebugWrite($"Local set {message.ObjectId} to owner {message.AuthorityOwner}"); var objects = QSBWorldSync.GetWorldObjects(); - var obj = objects.First(x => (x as IWorldObject).ObjectId == message.ObjectId); + var obj = objects.ToList()[message.ObjectId]; obj.ControllingPlayer = message.AuthorityOwner; + DebugLog.DebugWrite($"Local set (message:{message.ObjectId}) (obj:{(obj as IWorldObject).ObjectId}) to (message:{message.AuthorityOwner}) (obj:{obj.ControllingPlayer})"); } public override void OnReceiveRemote(bool server, QuantumAuthorityMessage message) { var objects = QSBWorldSync.GetWorldObjects(); - var obj = objects.First(x => (x as IWorldObject).ObjectId == message.ObjectId); + var obj = objects.ToList()[message.ObjectId]; obj.ControllingPlayer = message.AuthorityOwner; - DebugLog.DebugWrite($"Set {message.ObjectId} to owner {message.AuthorityOwner}"); + DebugLog.DebugWrite($"Set (message:{message.ObjectId}) (obj:{(obj as IWorldObject).ObjectId}) to (message:{message.AuthorityOwner}) (obj:{obj.ControllingPlayer})"); } } } \ No newline at end of file diff --git a/QSB/QuantumSync/Patches/ClientQuantumPatches.cs b/QSB/QuantumSync/Patches/ClientQuantumPatches.cs index 905d817e..1bafdb7b 100644 --- a/QSB/QuantumSync/Patches/ClientQuantumPatches.cs +++ b/QSB/QuantumSync/Patches/ClientQuantumPatches.cs @@ -6,14 +6,7 @@ namespace QSB.QuantumSync.Patches { public override QSBPatchTypes Type => QSBPatchTypes.OnNonServerClientConnect; - public override void DoPatches() - { - QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ClientQuantumPatches), nameof(ReturnFalsePatch)); - QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ClientQuantumPatches), nameof(ReturnFalsePatch)); - QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ClientQuantumPatches), nameof(ReturnFalsePatch)); - QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ClientQuantumPatches), nameof(ReturnFalsePatch)); - QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ClientQuantumPatches), nameof(ReturnFalsePatch)); - } + public override void DoPatches() => QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ClientQuantumPatches), nameof(ReturnFalsePatch)); public static bool ReturnFalsePatch() => false; } diff --git a/QSB/QuantumSync/Patches/QuantumPatches.cs b/QSB/QuantumSync/Patches/QuantumPatches.cs index c230abaf..9d42ef62 100644 --- a/QSB/QuantumSync/Patches/QuantumPatches.cs +++ b/QSB/QuantumSync/Patches/QuantumPatches.cs @@ -1,5 +1,12 @@ using QSB.Events; using QSB.Patches; +using QSB.Player; +using QSB.QuantumSync.WorldObjects; +using QSB.Utility; +using QSB.WorldSync; +using System; +using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace QSB.QuantumSync.Patches @@ -10,10 +17,145 @@ namespace QSB.QuantumSync.Patches public override void DoPatches() { + QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(QuantumPatches), nameof(Socketed_ChangeQuantumState)); + QSBCore.Helper.HarmonyHelper.AddPostfix("MoveToSocket", typeof(QuantumPatches), nameof(Socketed_MoveToSocket)); + + QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(QuantumPatches), nameof(Shuffle_ChangeQuantumState)); + + QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(QuantumPatches), nameof(MultiState_ChangeQuantumState)); + QSBCore.Helper.HarmonyHelper.AddPostfix("SetVisible", typeof(QuantumPatches), nameof(QuantumState_SetVisible)); + + QSBCore.Helper.HarmonyHelper.AddPrefix("IsPlayerInDarkness", typeof(QuantumPatches), nameof(Shrine_IsPlayerInDarkness)); + QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(QuantumPatches), nameof(Shrine_ChangeQuantumState)); QSBCore.Helper.HarmonyHelper.AddPrefix("OnEntry", typeof(QuantumPatches), nameof(Shrine_OnEntry)); QSBCore.Helper.HarmonyHelper.AddPrefix("OnExit", typeof(QuantumPatches), nameof(Shrine_OnExit)); } + public static bool Socketed_ChangeQuantumState(SocketedQuantumObject __instance) + => QSBWorldSync.GetWorldObject(QuantumManager.Instance.GetId(__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) + { + DebugLog.DebugWrite($"Worldobject is null for id {id}!"); + return; + } + if (worldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId) + { + return; + } + + var objId = QuantumManager.Instance.GetId(__instance); + var socketId = QuantumManager.Instance.GetId(socket); + DebugLog.DebugWrite($"{__instance.name} to socket {socketId}"); + GlobalMessenger + .FireEvent( + EventNames.QSBSocketStateChange, + objId, + socketId, + __instance.transform.localRotation); + } + + public static bool Shuffle_ChangeQuantumState( + QuantumShuffleObject __instance, + ref List ____indexList, + ref Vector3[] ____localPositions, + ref Transform[] ____shuffledObjects, + ref bool __result) + { + if (QSBWorldSync.GetWorldObject(QuantumManager.Instance.GetId(__instance)).ControllingPlayer != QSBPlayerManager.LocalPlayerId) + { + return false; + } + + ____indexList.Clear(); + ____indexList = Enumerable.Range(0, ____localPositions.Length).ToList(); + for (var i = 0; i < ____indexList.Count; ++i) + { + var random = UnityEngine.Random.Range(i, ____indexList.Count); + var temp = ____indexList[i]; + ____indexList[i] = ____indexList[random]; + ____indexList[random] = temp; + } + for (var j = 0; j < ____shuffledObjects.Length; j++) + { + ____shuffledObjects[j].localPosition = ____localPositions[____indexList[j]]; + } + DebugLog.DebugWrite($"{__instance.name} shuffled."); + GlobalMessenger + .FireEvent( + EventNames.QSBQuantumShuffle, + QuantumManager.Instance.GetId(__instance), + ____indexList.ToArray()); + __result = true; + return false; + } + + public static bool MultiState_ChangeQuantumState(MultiStateQuantumObject __instance) + { + var qsbObj = QSBWorldSync.GetWorldObject(QuantumManager.Instance.GetId(__instance)); + var isInControl = qsbObj.ControllingPlayer == QSBPlayerManager.LocalPlayerId; + DebugLog.DebugWrite($"Multistate (instance:{__instance.name}) (obj:{qsbObj.AttachedObject.name}) change state - in control:{isInControl} ({qsbObj.ControllingPlayer} vs {QSBPlayerManager.LocalPlayerId})"); + return isInControl; + } + + public static void QuantumState_SetVisible(QuantumState __instance, bool visible) + { + if (!visible) + { + return; + } + var allMultiStates = QSBWorldSync.GetWorldObjects(); + var owner = allMultiStates.First(x => x.QuantumStates.Contains(__instance)); + DebugLog.DebugWrite($"{owner.AttachedObject.name} controller is {owner.ControllingPlayer}"); + if (owner.ControllingPlayer != QSBPlayerManager.LocalPlayerId) + { + return; + } + DebugLog.DebugWrite($"{owner.AttachedObject.name} to quantum state {Array.IndexOf(owner.QuantumStates, __instance)}"); + GlobalMessenger + .FireEvent( + EventNames.QSBMultiStateChange, + QuantumManager.Instance.GetId(owner.AttachedObject), + Array.IndexOf(owner.QuantumStates, __instance)); + } + + public static bool Shrine_IsPlayerInDarkness(ref bool __result, Light[] ____lamps, float ____fadeFraction, bool ____isProbeInside, NomaiGateway ____gate) + { + foreach (var lamp in ____lamps) + { + if (lamp.intensity > 0f) + { + __result = false; + return false; + } + } + + var playersInMoon = QSBPlayerManager.PlayerList.Where(x => x.IsInMoon); + if (playersInMoon.Any(x => !x.IsInShrine) + || playersInMoon.Any(x => x.FlashLight != null && x.FlashLight.FlashlightOn) + || (QSBPlayerManager.LocalPlayer.IsInShrine && PlayerState.IsFlashlightOn())) + { + __result = false; + return false; + } + // TODO : make this *really* check for all players - check other probes and other jetpacks! + __result = ____gate.GetOpenFraction() == 0f + && !____isProbeInside + && Locator.GetThrusterLightTracker().GetLightRange() <= 0f; + return false; + } + + public static bool Shrine_ChangeQuantumState(QuantumShrine __instance) + { + var isInControl = QSBWorldSync.GetWorldObject(QuantumManager.Instance.GetId(__instance)).ControllingPlayer == QSBPlayerManager.LocalPlayerId; + DebugLog.DebugWrite($"Shrine change state - in control? {isInControl}"); + return isInControl; + } + public static bool Shrine_OnEntry( GameObject hitObj, ref bool ____isPlayerInside, diff --git a/QSB/QuantumSync/Patches/ServerQuantumPatches.cs b/QSB/QuantumSync/Patches/ServerQuantumPatches.cs index 51bfbc02..eed029f1 100644 --- a/QSB/QuantumSync/Patches/ServerQuantumPatches.cs +++ b/QSB/QuantumSync/Patches/ServerQuantumPatches.cs @@ -2,11 +2,7 @@ using QSB.Events; using QSB.Patches; using QSB.Player; -using QSB.QuantumSync.WorldObjects; using QSB.Utility; -using QSB.WorldSync; -using System; -using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; @@ -20,69 +16,8 @@ namespace QSB.QuantumSync.Patches public override void DoPatches() { - QSBCore.Helper.HarmonyHelper.AddPostfix("MoveToSocket", typeof(ServerQuantumPatches), nameof(Socketed_MoveToSocket)); - QSBCore.Helper.HarmonyHelper.AddPostfix("SetVisible", typeof(ServerQuantumPatches), nameof(QuantumState_SetVisible)); - QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ServerQuantumPatches), nameof(Shuffle_ChangeQuantumState)); QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ServerQuantumPatches), nameof(Moon_ChangeQuantumState)); QSBCore.Helper.HarmonyHelper.AddPrefix("CheckPlayerFogProximity", typeof(ServerQuantumPatches), nameof(Moon_CheckPlayerFogProximity)); - QSBCore.Helper.HarmonyHelper.AddPrefix("IsPlayerInDarkness", typeof(ServerQuantumPatches), nameof(Shrine_IsPlayerInDarkness)); - } - - public static void Socketed_MoveToSocket(SocketedQuantumObject __instance, QuantumSocket socket) - { - var objId = QuantumManager.Instance.GetId(__instance); - var socketId = QuantumManager.Instance.GetId(socket); - GlobalMessenger - .FireEvent( - EventNames.QSBSocketStateChange, - objId, - socketId, - __instance.transform.localRotation); - } - - public static void QuantumState_SetVisible(QuantumState __instance, bool visible) - { - if (!visible) - { - return; - } - var allMultiStates = QSBWorldSync.GetWorldObjects(); - var owner = allMultiStates.First(x => x.QuantumStates.Contains(__instance)); - DebugLog.DebugWrite($"{owner.AttachedObject.name} to state {Array.IndexOf(owner.QuantumStates, __instance)}"); - GlobalMessenger - .FireEvent( - EventNames.QSBMultiStateChange, - QuantumManager.Instance.GetId(owner.AttachedObject), - Array.IndexOf(owner.QuantumStates, __instance)); - } - - public static bool Shuffle_ChangeQuantumState( - QuantumShuffleObject __instance, - ref List ____indexList, - ref Vector3[] ____localPositions, - ref Transform[] ____shuffledObjects, - ref bool __result) - { - ____indexList.Clear(); - ____indexList = Enumerable.Range(0, ____localPositions.Length).ToList(); - for (var i = 0; i < ____indexList.Count; ++i) - { - var random = UnityEngine.Random.Range(i, ____indexList.Count); - var temp = ____indexList[i]; - ____indexList[i] = ____indexList[random]; - ____indexList[random] = temp; - } - for (var j = 0; j < ____shuffledObjects.Length; j++) - { - ____shuffledObjects[j].localPosition = ____localPositions[____indexList[j]]; - } - GlobalMessenger - .FireEvent( - EventNames.QSBQuantumShuffle, - QuantumManager.Instance.GetId(__instance), - ____indexList.ToArray()); - __result = true; - return false; } public static bool Moon_ChangeQuantumState( @@ -313,31 +248,5 @@ namespace QSB.QuantumSync.Patches ____shipLandingCamFogBubble.SetFogAlpha(fogAlpha); return false; } - - public static bool Shrine_IsPlayerInDarkness(ref bool __result, Light[] ____lamps, float ____fadeFraction, bool ____isProbeInside, NomaiGateway ____gate) - { - foreach (var lamp in ____lamps) - { - if (lamp.intensity > 0f) - { - __result = false; - return false; - } - } - - var playersInMoon = QSBPlayerManager.PlayerList.Where(x => x.IsInMoon); - if (playersInMoon.Any(x => !x.IsInShrine) - || playersInMoon.Any(x => x.FlashLight != null && x.FlashLight.FlashlightOn) - || (QSBPlayerManager.LocalPlayer.IsInShrine && PlayerState.IsFlashlightOn())) - { - __result = false; - return false; - } - // TODO : make this *really* check for all players - check other probes and other jetpacks! - __result = ____gate.GetOpenFraction() == 0f - && !____isProbeInside - && Locator.GetThrusterLightTracker().GetLightRange() <= 0f; - return false; - } } } \ No newline at end of file diff --git a/QSB/QuantumSync/WorldObjects/QSBQuantumObject.cs b/QSB/QuantumSync/WorldObjects/QSBQuantumObject.cs index a3cc84ac..12af2c48 100644 --- a/QSB/QuantumSync/WorldObjects/QSBQuantumObject.cs +++ b/QSB/QuantumSync/WorldObjects/QSBQuantumObject.cs @@ -2,6 +2,7 @@ using QSB.Player; using QSB.Utility; using QSB.WorldSync; +using System.Linq; using UnityEngine; namespace QSB.QuantumSync.WorldObjects @@ -20,13 +21,15 @@ namespace QSB.QuantumSync.WorldObjects private void OnEnable() { - if (ControllingPlayer != 0 && !QSBCore.IsServer) + if (ControllingPlayer != 0) { - // controlled by another player, dont care that we activate it (unless we're the server!) + // controlled by another player, dont care that we activate it return; } - // no one is controlling this object right now (or we're the server, and we want to take ownership), request authority - GlobalMessenger.FireEvent(EventNames.QSBQuantumAuthority, ObjectId, QSBPlayerManager.LocalPlayerId); + var id = QSBWorldSync.GetWorldObjects().ToList().IndexOf(this); + DebugLog.DebugWrite($"ON ENABLE {(this as WorldObject).AttachedObject.name} ({id})"); + // no one is controlling this object right now, request authority + GlobalMessenger.FireEvent(EventNames.QSBQuantumAuthority, id, QSBPlayerManager.LocalPlayerId); } private void OnDisable() @@ -36,8 +39,10 @@ namespace QSB.QuantumSync.WorldObjects // not being controlled by us, don't care if we leave area return; } + var id = QSBWorldSync.GetWorldObjects().ToList().IndexOf(this); + DebugLog.DebugWrite($"ON DISABLE {(this as WorldObject).AttachedObject.name} ({id})"); // send event to other players that we're releasing authority - GlobalMessenger.FireEvent(EventNames.QSBQuantumAuthority, ObjectId, 0); + GlobalMessenger.FireEvent(EventNames.QSBQuantumAuthority, id, 0); } } }