update world objects

This commit is contained in:
Mister_Nebula 2021-02-24 10:45:25 +00:00
parent 786277278e
commit 75fdc85fff
40 changed files with 250 additions and 152 deletions

View File

@ -1,6 +1,5 @@
using QSB.ElevatorSync.WorldObjects; using QSB.ElevatorSync.WorldObjects;
using QSB.WorldSync; using QSB.WorldSync;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace QSB.ElevatorSync namespace QSB.ElevatorSync
@ -9,8 +8,6 @@ namespace QSB.ElevatorSync
{ {
public static ElevatorManager Instance { get; private set; } public static ElevatorManager Instance { get; private set; }
private List<Elevator> _elevators;
public void Awake() public void Awake()
{ {
Instance = this; Instance = this;
@ -19,8 +16,7 @@ namespace QSB.ElevatorSync
public void OnDestroy() => QSBSceneManager.OnSceneLoaded -= OnSceneLoaded; public void OnDestroy() => QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
private void OnSceneLoaded(OWScene scene, bool isInUniverse) => _elevators = QSBWorldSync.Init<QSBElevator, Elevator>(); private void OnSceneLoaded(OWScene scene, bool isInUniverse)
=> QSBWorldSync.Init<QSBElevator, Elevator>();
public int GetId(Elevator elevator) => _elevators.IndexOf(elevator);
} }
} }

View File

@ -22,7 +22,7 @@ namespace QSB.ElevatorSync.Events
public override void OnReceiveRemote(bool server, BoolWorldObjectMessage message) public override void OnReceiveRemote(bool server, BoolWorldObjectMessage message)
{ {
var elevator = QSBWorldSync.GetWorldObject<QSBElevator>(message.ObjectId); var elevator = QSBWorldSync.GetWorldFromId<QSBElevator>(message.ObjectId);
elevator?.RemoteCall(message.State); elevator?.RemoteCall(message.State);
} }
} }

View File

@ -1,6 +1,8 @@
using OWML.Utils; using OWML.Utils;
using QSB.ElevatorSync.WorldObjects;
using QSB.Events; using QSB.Events;
using QSB.Patches; using QSB.Patches;
using QSB.WorldSync;
namespace QSB.ElevatorSync.Patches namespace QSB.ElevatorSync.Patches
{ {
@ -11,7 +13,7 @@ namespace QSB.ElevatorSync.Patches
public static void StartLift(Elevator __instance) public static void StartLift(Elevator __instance)
{ {
var isGoingUp = __instance.GetValue<bool>("_goingToTheEnd"); var isGoingUp = __instance.GetValue<bool>("_goingToTheEnd");
var id = ElevatorManager.Instance.GetId(__instance); var id = QSBWorldSync.GetIdFromUnity<QSBElevator, Elevator>(__instance);
QSBEventManager.FireEvent(EventNames.QSBStartLift, id, isGoingUp); QSBEventManager.FireEvent(EventNames.QSBStartLift, id, isGoingUp);
} }

View File

@ -1,4 +1,5 @@
using OWML.Utils; using OWML.Utils;
using QSB.Utility;
using QSB.WorldSync; using QSB.WorldSync;
using UnityEngine; using UnityEngine;
@ -15,6 +16,7 @@ namespace QSB.ElevatorSync.WorldObjects
public override void Init(Elevator elevator, int id) public override void Init(Elevator elevator, int id)
{ {
DebugLog.DebugWrite($"init with id {id}");
AttachedObject = elevator; AttachedObject = elevator;
ObjectId = id; ObjectId = id;
QSBCore.Helper.Events.Unity.RunWhen(() => AttachedObject.GetValue<SingleInteractionVolume>("_interactVolume") != null, InitValues); QSBCore.Helper.Events.Unity.RunWhen(() => AttachedObject.GetValue<SingleInteractionVolume>("_interactVolume") != null, InitValues);

View File

@ -50,5 +50,6 @@
public static string QSBEnterShrine = "QSBEnterShrine"; public static string QSBEnterShrine = "QSBEnterShrine";
public static string QSBExitShrine = "QSBExitShrine"; public static string QSBExitShrine = "QSBExitShrine";
public static string QSBPlayerEntangle = "QSBPlayerEntangle"; public static string QSBPlayerEntangle = "QSBPlayerEntangle";
public static string QSBDropItem = "QSBDropItem";
} }
} }

View File

@ -34,6 +34,7 @@
IdentifySignal, IdentifySignal,
TextTranslated, TextTranslated,
EnterLeave, EnterLeave,
PlayerEntangle PlayerEntangle,
DropItem
} }
} }

View File

@ -23,7 +23,7 @@ namespace QSB.GeyserSync.Events
public override void OnReceiveRemote(bool server, BoolWorldObjectMessage message) public override void OnReceiveRemote(bool server, BoolWorldObjectMessage message)
{ {
var geyser = QSBWorldSync.GetWorldObject<QSBGeyser>(message.ObjectId); var geyser = QSBWorldSync.GetWorldFromId<QSBGeyser>(message.ObjectId);
geyser?.SetState(message.State); geyser?.SetState(message.State);
} }
} }

View File

@ -0,0 +1,37 @@
using QSB.Events;
using QSB.ItemSync.WorldObjects;
using QSB.Utility;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.ItemSync.Events
{
internal class DropItemEvent : QSBEvent<DropItemMessage>
{
public override QSB.Events.EventType Type => QSB.Events.EventType.DropItem;
public override void SetupListener()
=> GlobalMessenger<int, Vector3, Vector3, Transform, Sector, DetachableFragment>.AddListener(EventNames.QSBDropItem, Handler);
public override void CloseListener()
=> GlobalMessenger<int, Vector3, Vector3, Transform, Sector, DetachableFragment>.RemoveListener(EventNames.QSBDropItem, Handler);
private void Handler(int objectId, Vector3 position, Vector3 normal, Transform parent, Sector sector, DetachableFragment fragment)
=> SendEvent(CreateMessage(objectId, position, normal, parent, sector, fragment));
private DropItemMessage CreateMessage(int objectId, Vector3 position, Vector3 normal, Transform parent, Sector sector, DetachableFragment fragment) => new DropItemMessage
{
ObjectId = objectId,
Position = position,
Normal = normal,
Parent = parent,
Sector = sector,
DetachableFragment = fragment
};
public override void OnReceiveRemote(bool server, DropItemMessage message)
{
var worldObject = QSBWorldSync.GetWorldFromId<IQSBOWItem>(message.ObjectId);
}
}
}

View File

@ -0,0 +1,38 @@
using QSB.Messaging;
using QSB.SectorSync.WorldObjects;
using QSB.WorldSync;
using QuantumUNET.Transport;
using UnityEngine;
namespace QSB.ItemSync.Events
{
public class DropItemMessage : PlayerMessage
{
public int ObjectId { get; set; }
public Vector3 Position { get; set; }
public Vector3 Normal { get; set; }
public Transform Parent { get; set; }
public Sector Sector { get; set; }
public DetachableFragment DetachableFragment { get; set; }
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
ObjectId = reader.ReadInt32();
Position = reader.ReadVector3();
Normal = reader.ReadVector3();
var sectorId = reader.ReadInt32();
Sector = QSBWorldSync.GetWorldFromId<QSBSector>(sectorId).AttachedObject;
}
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(ObjectId);
writer.Write(Position);
writer.Write(Normal);
var qsbSector = QSBWorldSync.GetWorldFromUnity<QSBSector, Sector>(Sector);
writer.Write(qsbSector.ObjectId);
}
}
}

View File

@ -1,11 +1,6 @@
using System; namespace QSB.ItemSync.Events
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QSB.ItemSync.Events
{ {
class SocketItemEvent internal class SocketItemEvent
{ {
} }
} }

View File

@ -2,15 +2,11 @@
using QSB.ItemSync.WorldObjects; using QSB.ItemSync.WorldObjects;
using QSB.Utility; using QSB.Utility;
using QSB.WorldSync; using QSB.WorldSync;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine; using UnityEngine;
namespace QSB.ItemSync namespace QSB.ItemSync
{ {
class ItemManager : MonoBehaviour internal class ItemManager : MonoBehaviour
{ {
public static ItemManager Instance { get; private set; } public static ItemManager Instance { get; private set; }

View File

@ -1,15 +1,11 @@
using QSB.Patches; using QSB.Patches;
using QSB.Utility; using QSB.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text;
using UnityEngine; using UnityEngine;
namespace QSB.ItemSync.Patches namespace QSB.ItemSync.Patches
{ {
class ItemPatches : QSBPatch internal class ItemPatches : QSBPatch
{ {
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect; public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;

View File

@ -2,8 +2,7 @@
{ {
public enum SocketEventType public enum SocketEventType
{ {
StartUnsocket, Unsocket,
CompleteUnsocket,
Socket Socket
} }
} }

View File

@ -1,6 +1,8 @@
namespace QSB.ItemSync.WorldObjects using QSB.WorldSync;
namespace QSB.ItemSync.WorldObjects
{ {
public interface IQSBOWItem public interface IQSBOWItem : IWorldObjectTypeSubset
{ {
uint HoldingPlayer { get; set; } uint HoldingPlayer { get; set; }
} }

View File

@ -1,14 +1,9 @@
using QSB.WorldSync; using QSB.WorldSync;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace QSB.ItemSync.WorldObjects namespace QSB.ItemSync.WorldObjects
{ {
class QSBOWItem<T> : WorldObject<T>, IQSBOWItem internal class QSBOWItem<T> : WorldObject<T>, IQSBOWItem
where T : MonoBehaviour where T : OWItem
{ {
public uint HoldingPlayer { get; set; } public uint HoldingPlayer { get; set; }

View File

@ -3,7 +3,7 @@ using UnityEngine;
namespace QSB.ItemSync.WorldObjects namespace QSB.ItemSync.WorldObjects
{ {
class QSBOWItemSocket<T> : WorldObject<T>, IQSBOWItemSocket internal class QSBOWItemSocket<T> : WorldObject<T>, IQSBOWItemSocket
where T : MonoBehaviour where T : MonoBehaviour
{ {
public override void Init(T attachedObject, int id) { } public override void Init(T attachedObject, int id) { }

View File

@ -1,11 +1,6 @@
using System; namespace QSB.ItemSync.WorldObjects
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QSB.ItemSync.WorldObjects
{ {
class QSBScrollItem : QSBOWItem<ScrollItem> internal class QSBScrollItem : QSBOWItem<ScrollItem>
{ {
public override void Init(ScrollItem attachedObject, int id) public override void Init(ScrollItem attachedObject, int id)
{ {

View File

@ -1,11 +1,6 @@
using System; namespace QSB.ItemSync.WorldObjects
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QSB.ItemSync.WorldObjects
{ {
class QSBScrollSocket : QSBOWItemSocket<ScrollSocket> internal class QSBScrollSocket : QSBOWItemSocket<ScrollSocket>
{ {
public override void Init(ScrollSocket attachedObject, int id) public override void Init(ScrollSocket attachedObject, int id)
{ {

View File

@ -23,7 +23,7 @@ namespace QSB.OrbSync.Events
public override void OnReceiveRemote(bool server, OrbSlotMessage message) public override void OnReceiveRemote(bool server, OrbSlotMessage message)
{ {
var orbSlot = QSBWorldSync.GetWorldObject<QSBOrbSlot>(message.SlotId); var orbSlot = QSBWorldSync.GetWorldFromId<QSBOrbSlot>(message.SlotId);
orbSlot?.SetState(message.SlotState, message.OrbId); orbSlot?.SetState(message.SlotState, message.OrbId);
} }
} }

View File

@ -1,5 +1,6 @@
using QSB.Events; using QSB.Events;
using QSB.QuantumSync; using QSB.QuantumSync;
using QSB.WorldSync;
using QSB.WorldSync.Events; using QSB.WorldSync.Events;
namespace QSB.Player.Events namespace QSB.Player.Events
@ -27,7 +28,7 @@ namespace QSB.Player.Events
player.EntangledObject = null; player.EntangledObject = null;
return; return;
} }
var quantumObject = QuantumManager.GetObject(message.ObjectId); var quantumObject = QSBWorldSync.GetWorldFromId<IQSBQuantumObject>(message.ObjectId);
player.EntangledObject = quantumObject; player.EntangledObject = quantumObject;
} }
@ -39,7 +40,7 @@ namespace QSB.Player.Events
player.EntangledObject = null; player.EntangledObject = null;
return; return;
} }
var quantumObject = QuantumManager.GetObject(message.ObjectId); var quantumObject = QSBWorldSync.GetWorldFromId<IQSBQuantumObject>(message.ObjectId);
player.EntangledObject = quantumObject; player.EntangledObject = quantumObject;
} }
} }

View File

@ -28,7 +28,7 @@ namespace QSB.Player.Events
{ {
return; return;
} }
var sector = QSBWorldSync.GetWorldObject<QSBSector>(message.ObjectId); var sector = QSBWorldSync.GetWorldFromId<QSBSector>(message.ObjectId);
if (sector == null) if (sector == null)
{ {

View File

@ -1,6 +1,7 @@
using OWML.Utils; using OWML.Utils;
using QSB.Events; using QSB.Events;
using QSB.QuantumSync; using QSB.QuantumSync;
using QSB.WorldSync;
using UnityEngine; using UnityEngine;
namespace QSB.Player namespace QSB.Player
@ -20,7 +21,7 @@ namespace QSB.Player
if (_previousCollidingQuantumObject != collidingQuantumObject) if (_previousCollidingQuantumObject != collidingQuantumObject)
{ {
var objectIndex = (collidingQuantumObject != null) var objectIndex = (collidingQuantumObject != null)
? QuantumManager.GetId(QuantumManager.GetObject(collidingQuantumObject)) ? QSBWorldSync.GetIdFromTypeSubset(QuantumManager.GetObject(collidingQuantumObject))
: -1; : -1;
QSBEventManager.FireEvent( QSBEventManager.FireEvent(

View File

@ -132,6 +132,8 @@
<Compile Include="Instruments\QSBCamera\CameraManager.cs" /> <Compile Include="Instruments\QSBCamera\CameraManager.cs" />
<Compile Include="Instruments\QSBCamera\CameraMode.cs" /> <Compile Include="Instruments\QSBCamera\CameraMode.cs" />
<Compile Include="Instruments\InstrumentsManager.cs" /> <Compile Include="Instruments\InstrumentsManager.cs" />
<Compile Include="ItemSync\Events\DropItemEvent.cs" />
<Compile Include="ItemSync\Events\DropItemMessage.cs" />
<Compile Include="ItemSync\Events\SocketItemEvent.cs" /> <Compile Include="ItemSync\Events\SocketItemEvent.cs" />
<Compile Include="ItemSync\ItemManager.cs" /> <Compile Include="ItemSync\ItemManager.cs" />
<Compile Include="ItemSync\Patches\ItemPatches.cs" /> <Compile Include="ItemSync\Patches\ItemPatches.cs" />
@ -219,9 +221,11 @@
<Compile Include="SectorSync\WorldObjects\QSBSector.cs" /> <Compile Include="SectorSync\WorldObjects\QSBSector.cs" />
<Compile Include="SectorSync\QSBSectorManager.cs" /> <Compile Include="SectorSync\QSBSectorManager.cs" />
<Compile Include="TransformSync\TransformSync.cs" /> <Compile Include="TransformSync\TransformSync.cs" />
<Compile Include="Utility\Callback6Args.cs" />
<Compile Include="Utility\DebugBoxManager.cs" /> <Compile Include="Utility\DebugBoxManager.cs" />
<Compile Include="Utility\DebugZOverride.cs" /> <Compile Include="Utility\DebugZOverride.cs" />
<Compile Include="Utility\Extensions.cs" /> <Compile Include="Utility\Extensions.cs" />
<Compile Include="Utility\GlobalMessenger6Args.cs" />
<Compile Include="Utility\IRepeating.cs" /> <Compile Include="Utility\IRepeating.cs" />
<Compile Include="Utility\OnEnableDisableTracker.cs" /> <Compile Include="Utility\OnEnableDisableTracker.cs" />
<Compile Include="Utility\Popcron.Gizmos\Constants.cs" /> <Compile Include="Utility\Popcron.Gizmos\Constants.cs" />
@ -272,6 +276,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WorldSync\FactReveal.cs" /> <Compile Include="WorldSync\FactReveal.cs" />
<Compile Include="WorldSync\IWorldObject.cs" /> <Compile Include="WorldSync\IWorldObject.cs" />
<Compile Include="WorldSync\IWorldObjectTypeSubset.cs" />
<Compile Include="WorldSync\QSBWorldSync.cs" /> <Compile Include="WorldSync\QSBWorldSync.cs" />
<Compile Include="WorldSync\WorldObject.cs" /> <Compile Include="WorldSync\WorldObject.cs" />
</ItemGroup> </ItemGroup>

View File

@ -157,7 +157,7 @@ namespace QSB
// Used for diagnosing specific socketed objects. // Used for diagnosing specific socketed objects.
// 110 = Cave Twin entanglement shard // 110 = Cave Twin entanglement shard
// 342 = Timber Hearth museum shard // 342 = Timber Hearth museum shard
var socketedObject = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject>(SocketedObjToDebug); var socketedObject = QSBWorldSync.GetWorldFromId<QSBSocketedQuantumObject>(SocketedObjToDebug);
GUI.Label(new Rect(220, offset, 200f, 20f), $"{SocketedObjToDebug} Controller : {socketedObject.ControllingPlayer}"); GUI.Label(new Rect(220, offset, 200f, 20f), $"{SocketedObjToDebug} Controller : {socketedObject.ControllingPlayer}");
offset += _debugLineSpacing; offset += _debugLineSpacing;
GUI.Label(new Rect(220, offset, 200f, 20f), $"{SocketedObjToDebug} Illuminated : {socketedObject.AttachedObject.IsIlluminated()}"); GUI.Label(new Rect(220, offset, 200f, 20f), $"{SocketedObjToDebug} Illuminated : {socketedObject.AttachedObject.IsIlluminated()}");

View File

@ -26,7 +26,7 @@ namespace QSB.QuantumSync.Events
{ {
return; return;
} }
var qsbObj = QSBWorldSync.GetWorldObject<QSBMultiStateQuantumObject>(message.ObjectId); var qsbObj = QSBWorldSync.GetWorldFromId<QSBMultiStateQuantumObject>(message.ObjectId);
qsbObj.DebugBoxText.text = message.StateIndex.ToString(); qsbObj.DebugBoxText.text = message.StateIndex.ToString();
} }
@ -36,7 +36,7 @@ namespace QSB.QuantumSync.Events
{ {
return; return;
} }
var qsbObj = QSBWorldSync.GetWorldObject<QSBMultiStateQuantumObject>(message.ObjectId); var qsbObj = QSBWorldSync.GetWorldFromId<QSBMultiStateQuantumObject>(message.ObjectId);
qsbObj.ChangeState(message.StateIndex); qsbObj.ChangeState(message.StateIndex);
} }
} }

View File

@ -1,5 +1,6 @@
using QSB.Events; using QSB.Events;
using QSB.Player; using QSB.Player;
using QSB.WorldSync;
namespace QSB.QuantumSync.Events namespace QSB.QuantumSync.Events
{ {
@ -26,7 +27,7 @@ namespace QSB.QuantumSync.Events
return false; return false;
} }
var obj = QuantumManager.GetObject(message.ObjectId); var obj = QSBWorldSync.GetWorldFromId<IQSBQuantumObject>(message.ObjectId);
// Deciding if to change the object's owner // Deciding if to change the object's owner
// Message // Message
@ -42,13 +43,13 @@ namespace QSB.QuantumSync.Events
public override void OnReceiveLocal(bool server, QuantumAuthorityMessage message) public override void OnReceiveLocal(bool server, QuantumAuthorityMessage message)
{ {
var obj = QuantumManager.GetObject(message.ObjectId); var obj = QSBWorldSync.GetWorldFromId<IQSBQuantumObject>(message.ObjectId);
obj.ControllingPlayer = message.AuthorityOwner; obj.ControllingPlayer = message.AuthorityOwner;
} }
public override void OnReceiveRemote(bool server, QuantumAuthorityMessage message) public override void OnReceiveRemote(bool server, QuantumAuthorityMessage message)
{ {
var obj = QuantumManager.GetObject(message.ObjectId); var obj = QSBWorldSync.GetWorldFromId<IQSBQuantumObject>(message.ObjectId);
obj.ControllingPlayer = message.AuthorityOwner; obj.ControllingPlayer = message.AuthorityOwner;
if (obj.ControllingPlayer == 0 && obj.IsEnabled) if (obj.ControllingPlayer == 0 && obj.IsEnabled)
{ {

View File

@ -26,7 +26,7 @@ namespace QSB.QuantumSync.Events
{ {
return; return;
} }
var obj = QSBWorldSync.GetWorldObject<QSBQuantumShuffleObject>(message.ObjectId); var obj = QSBWorldSync.GetWorldFromId<QSBQuantumShuffleObject>(message.ObjectId);
obj.ShuffleObjects(message.IndexArray); obj.ShuffleObjects(message.IndexArray);
} }
} }

View File

@ -28,7 +28,7 @@ namespace QSB.QuantumSync.Events
{ {
return; return;
} }
var obj = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject>(message.ObjectId); var obj = QSBWorldSync.GetWorldFromId<QSBSocketedQuantumObject>(message.ObjectId);
obj.MoveToSocket(message); obj.MoveToSocket(message);
} }
} }

View File

@ -54,7 +54,7 @@ namespace QSB.QuantumSync.Patches
ref QuantumSocket ____recentlyObscuredSocket, ref QuantumSocket ____recentlyObscuredSocket,
QuantumSocket ____occupiedSocket) QuantumSocket ____occupiedSocket)
{ {
var socketedWorldObject = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject, SocketedQuantumObject>(__instance); var socketedWorldObject = QSBWorldSync.GetWorldFromUnity<QSBSocketedQuantumObject, SocketedQuantumObject>(__instance);
if (socketedWorldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId) if (socketedWorldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId)
{ {
return false; return false;
@ -151,8 +151,8 @@ namespace QSB.QuantumSync.Patches
public static void Socketed_MoveToSocket(SocketedQuantumObject __instance, QuantumSocket socket) public static void Socketed_MoveToSocket(SocketedQuantumObject __instance, QuantumSocket socket)
{ {
var objectWorldObject = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject, SocketedQuantumObject>(__instance); var objectWorldObject = QSBWorldSync.GetWorldFromUnity<QSBSocketedQuantumObject, SocketedQuantumObject>(__instance);
var socketWorldObject = QSBWorldSync.GetWorldObject<QSBQuantumSocket, QuantumSocket>(socket); var socketWorldObject = QSBWorldSync.GetWorldFromUnity<QSBQuantumSocket, QuantumSocket>(socket);
if (objectWorldObject == null) if (objectWorldObject == null)
{ {
DebugLog.ToConsole($"Worldobject is null for {__instance.name}!"); DebugLog.ToConsole($"Worldobject is null for {__instance.name}!");
@ -178,7 +178,7 @@ namespace QSB.QuantumSync.Patches
ref Transform[] ____shuffledObjects, ref Transform[] ____shuffledObjects,
ref bool __result) ref bool __result)
{ {
var shuffleWorldObject = QSBWorldSync.GetWorldObject<QSBQuantumShuffleObject, QuantumShuffleObject>(__instance); var shuffleWorldObject = QSBWorldSync.GetWorldFromUnity<QSBQuantumShuffleObject, QuantumShuffleObject>(__instance);
if (shuffleWorldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId) if (shuffleWorldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId)
{ {
return false; return false;
@ -197,7 +197,6 @@ namespace QSB.QuantumSync.Patches
{ {
____shuffledObjects[j].localPosition = ____localPositions[____indexList[j]]; ____shuffledObjects[j].localPosition = ____localPositions[____indexList[j]];
} }
//DebugLog.DebugWrite($"{__instance.name} shuffled.");
QSBEventManager.FireEvent( QSBEventManager.FireEvent(
EventNames.QSBQuantumShuffle, EventNames.QSBQuantumShuffle,
shuffleWorldObject.ObjectId, shuffleWorldObject.ObjectId,
@ -208,7 +207,7 @@ namespace QSB.QuantumSync.Patches
public static bool MultiState_ChangeQuantumState(MultiStateQuantumObject __instance) public static bool MultiState_ChangeQuantumState(MultiStateQuantumObject __instance)
{ {
var qsbObj = QSBWorldSync.GetWorldObject<QSBMultiStateQuantumObject, MultiStateQuantumObject>(__instance); var qsbObj = QSBWorldSync.GetWorldFromUnity<QSBMultiStateQuantumObject, MultiStateQuantumObject>(__instance);
var isInControl = qsbObj.ControllingPlayer == QSBPlayerManager.LocalPlayerId; var isInControl = qsbObj.ControllingPlayer == QSBPlayerManager.LocalPlayerId;
return isInControl; return isInControl;
} }
@ -262,7 +261,7 @@ namespace QSB.QuantumSync.Patches
public static bool Shrine_ChangeQuantumState(QuantumShrine __instance) public static bool Shrine_ChangeQuantumState(QuantumShrine __instance)
{ {
var shrineWorldObject = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject, SocketedQuantumObject>(__instance); var shrineWorldObject = QSBWorldSync.GetWorldFromUnity<QSBSocketedQuantumObject, SocketedQuantumObject>(__instance);
var isInControl = shrineWorldObject.ControllingPlayer == QSBPlayerManager.LocalPlayerId; var isInControl = shrineWorldObject.ControllingPlayer == QSBPlayerManager.LocalPlayerId;
return isInControl; return isInControl;
} }

View File

@ -95,15 +95,15 @@ namespace QSB.QuantumSync
IQSBQuantumObject worldObj = null; IQSBQuantumObject worldObj = null;
if (unityObject.GetType() == typeof(SocketedQuantumObject) || unityObject.GetType() == typeof(QuantumShrine)) if (unityObject.GetType() == typeof(SocketedQuantumObject) || unityObject.GetType() == typeof(QuantumShrine))
{ {
worldObj = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject, SocketedQuantumObject>((SocketedQuantumObject)unityObject); worldObj = QSBWorldSync.GetWorldFromUnity<QSBSocketedQuantumObject, SocketedQuantumObject>((SocketedQuantumObject)unityObject);
} }
else if (unityObject.GetType() == typeof(MultiStateQuantumObject)) else if (unityObject.GetType() == typeof(MultiStateQuantumObject))
{ {
worldObj = QSBWorldSync.GetWorldObject<QSBMultiStateQuantumObject, MultiStateQuantumObject>((MultiStateQuantumObject)unityObject); worldObj = QSBWorldSync.GetWorldFromUnity<QSBMultiStateQuantumObject, MultiStateQuantumObject>((MultiStateQuantumObject)unityObject);
} }
else if (unityObject.GetType() == typeof(QuantumShuffleObject)) else if (unityObject.GetType() == typeof(QuantumShuffleObject))
{ {
worldObj = QSBWorldSync.GetWorldObject<QSBQuantumShuffleObject, QuantumShuffleObject>((QuantumShuffleObject)unityObject); worldObj = QSBWorldSync.GetWorldFromUnity<QSBQuantumShuffleObject, QuantumShuffleObject>((QuantumShuffleObject)unityObject);
} }
else else
{ {
@ -111,51 +111,5 @@ namespace QSB.QuantumSync
} }
return worldObj; return worldObj;
} }
public static IQSBQuantumObject GetObject(int id)
{
var objects = QSBWorldSync
.GetWorldObjects<IQSBQuantumObject>()
.ToList();
if (objects.Count == 0)
{
DebugLog.ToConsole($"Error - tried to get IQSBQuantumObject, but there are none!", MessageType.Error);
return null;
}
if (objects.Count <= id)
{
DebugLog.ToConsole($"Error - Index {id} does not exist in list of IQSBObjects! (Count:{objects.Count})", MessageType.Error);
return null;
}
if (id < 0)
{
DebugLog.ToConsole($"Error - tried to get IQSBQuantumObject with index less than zero...", MessageType.Error);
return null;
}
return objects[id];
}
public static int GetId(IQSBQuantumObject obj)
{
var objects = QSBWorldSync
.GetWorldObjects<IQSBQuantumObject>()
.ToList();
if (obj == null)
{
DebugLog.ToConsole($"Error - tried to get id of null IQSBQuantumObject!", MessageType.Error);
return -1;
}
if (objects.Count == 0)
{
DebugLog.ToConsole($"Error - tried to get id of IQSBQuantumObject, but there are none!", MessageType.Error);
return -1;
}
if (!objects.Contains(obj))
{
DebugLog.ToConsole($"Error - tried to get id of IQSBQuantumObject that doesn't exist in WorldObject list?!", MessageType.Error);
return -1;
}
return objects.IndexOf(obj);
}
} }
} }

View File

@ -1,6 +1,8 @@
namespace QSB.QuantumSync using QSB.WorldSync;
namespace QSB.QuantumSync
{ {
public interface IQSBQuantumObject public interface IQSBQuantumObject : IWorldObjectTypeSubset
{ {
uint ControllingPlayer { get; set; } uint ControllingPlayer { get; set; }
bool IsEnabled { get; set; } bool IsEnabled { get; set; }

View File

@ -42,7 +42,7 @@ namespace QSB.QuantumSync.WorldObjects
// controlled by another player, dont care that we activate it // controlled by another player, dont care that we activate it
return; return;
} }
var id = QuantumManager.GetId(this); var id = QSBWorldSync.GetIdFromTypeSubset(this);
// no one is controlling this object right now, request authority // no one is controlling this object right now, request authority
QSBEventManager.FireEvent(EventNames.QSBQuantumAuthority, id, QSBPlayerManager.LocalPlayerId); QSBEventManager.FireEvent(EventNames.QSBQuantumAuthority, id, QSBPlayerManager.LocalPlayerId);
} }
@ -59,7 +59,7 @@ namespace QSB.QuantumSync.WorldObjects
// not being controlled by us, don't care if we leave area // not being controlled by us, don't care if we leave area
return; return;
} }
var id = QuantumManager.GetId(this); var id = QSBWorldSync.GetIdFromTypeSubset(this);
// send event to other players that we're releasing authority // send event to other players that we're releasing authority
QSBEventManager.FireEvent(EventNames.QSBQuantumAuthority, id, 0u); QSBEventManager.FireEvent(EventNames.QSBQuantumAuthority, id, 0u);
} }

View File

@ -35,7 +35,7 @@ namespace QSB.QuantumSync.WorldObjects
public void MoveToSocket(SocketStateChangeMessage message) public void MoveToSocket(SocketStateChangeMessage message)
{ {
var qsbSocket = QSBWorldSync.GetWorldObject<QSBQuantumSocket>(message.SocketId); var qsbSocket = QSBWorldSync.GetWorldFromId<QSBQuantumSocket>(message.SocketId);
if (qsbSocket == null) if (qsbSocket == null)
{ {
DebugLog.ToConsole($"Couldn't find socket id {message.SocketId}", MessageType.Error); DebugLog.ToConsole($"Couldn't find socket id {message.SocketId}", MessageType.Error);

View File

@ -29,17 +29,17 @@ namespace QSB.TranslationSync.Events
} }
if (message.TextType == NomaiTextType.WallText) if (message.TextType == NomaiTextType.WallText)
{ {
var obj = QSBWorldSync.GetWorldObject<QSBWallText>(message.ObjectId); var obj = QSBWorldSync.GetWorldFromId<QSBWallText>(message.ObjectId);
obj.HandleSetAsTranslated(message.TextId); obj.HandleSetAsTranslated(message.TextId);
} }
else if (message.TextType == NomaiTextType.Computer) else if (message.TextType == NomaiTextType.Computer)
{ {
var obj = QSBWorldSync.GetWorldObject<QSBComputer>(message.ObjectId); var obj = QSBWorldSync.GetWorldFromId<QSBComputer>(message.ObjectId);
obj.HandleSetAsTranslated(message.TextId); obj.HandleSetAsTranslated(message.TextId);
} }
else if (message.TextType == NomaiTextType.VesselComputer) else if (message.TextType == NomaiTextType.VesselComputer)
{ {
var obj = QSBWorldSync.GetWorldObject<QSBVesselComputer>(message.ObjectId); var obj = QSBWorldSync.GetWorldFromId<QSBVesselComputer>(message.ObjectId);
obj.HandleSetAsTranslated(message.TextId); obj.HandleSetAsTranslated(message.TextId);
} }
else else

View File

@ -1,5 +1,7 @@
using QSB.Events; using QSB.Events;
using QSB.Patches; using QSB.Patches;
using QSB.TranslationSync.WorldObjects;
using QSB.WorldSync;
namespace QSB.TranslationSync.Patches namespace QSB.TranslationSync.Patches
{ {
@ -30,7 +32,7 @@ namespace QSB.TranslationSync.Patches
QSBEventManager.FireEvent( QSBEventManager.FireEvent(
EventNames.QSBTextTranslated, EventNames.QSBTextTranslated,
NomaiTextType.WallText, NomaiTextType.WallText,
SpiralManager.Instance.GetId(__instance), QSBWorldSync.GetIdFromUnity<QSBWallText, NomaiWallText>(__instance),
id); id);
return true; return true;
} }
@ -44,7 +46,7 @@ namespace QSB.TranslationSync.Patches
QSBEventManager.FireEvent( QSBEventManager.FireEvent(
EventNames.QSBTextTranslated, EventNames.QSBTextTranslated,
NomaiTextType.Computer, NomaiTextType.Computer,
SpiralManager.Instance.GetId(__instance), QSBWorldSync.GetIdFromUnity<QSBComputer, NomaiComputer>(__instance),
id); id);
return true; return true;
} }
@ -58,7 +60,7 @@ namespace QSB.TranslationSync.Patches
QSBEventManager.FireEvent( QSBEventManager.FireEvent(
EventNames.QSBTextTranslated, EventNames.QSBTextTranslated,
NomaiTextType.VesselComputer, NomaiTextType.VesselComputer,
SpiralManager.Instance.GetId(__instance), QSBWorldSync.GetIdFromUnity<QSBVesselComputer, NomaiVesselComputer>(__instance),
id); id);
return true; return true;
} }

View File

@ -1,6 +1,5 @@
using QSB.TranslationSync.WorldObjects; using QSB.TranslationSync.WorldObjects;
using QSB.WorldSync; using QSB.WorldSync;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace QSB.TranslationSync namespace QSB.TranslationSync
@ -9,10 +8,6 @@ namespace QSB.TranslationSync
{ {
public static SpiralManager Instance { get; private set; } public static SpiralManager Instance { get; private set; }
private List<NomaiWallText> _nomaiWallTexts;
private List<NomaiComputer> _nomaiComputers;
private List<NomaiVesselComputer> _nomaiVesselComputers;
public void Awake() public void Awake()
{ {
Instance = this; Instance = this;
@ -23,13 +18,9 @@ namespace QSB.TranslationSync
private void OnSceneLoaded(OWScene scene) private void OnSceneLoaded(OWScene scene)
{ {
_nomaiWallTexts = QSBWorldSync.Init<QSBWallText, NomaiWallText>(); QSBWorldSync.Init<QSBWallText, NomaiWallText>();
_nomaiComputers = QSBWorldSync.Init<QSBComputer, NomaiComputer>(); QSBWorldSync.Init<QSBComputer, NomaiComputer>();
_nomaiVesselComputers = QSBWorldSync.Init<QSBVesselComputer, NomaiVesselComputer>(); QSBWorldSync.Init<QSBVesselComputer, NomaiVesselComputer>();
} }
public int GetId(NomaiWallText obj) => _nomaiWallTexts.IndexOf(obj);
public int GetId(NomaiComputer obj) => _nomaiComputers.IndexOf(obj);
public int GetId(NomaiVesselComputer obj) => _nomaiVesselComputers.IndexOf(obj);
} }
} }

View File

@ -0,0 +1 @@
public delegate void Callback<T, U, V, W, X, Y>(T arg1, U arg2, V arg3, W arg4, X arg5, Y arg6);

View File

@ -0,0 +1,79 @@
using OWML.Common;
using System;
using System.Collections.Generic;
namespace QSB.Utility
{
public static class GlobalMessenger<T, U, V, W, X, Y>
{
public static void AddListener(string eventType, Callback<T, U, V, W, X, Y> handler)
{
object obj = _eventTable;
lock (obj)
{
if (!_eventTable.TryGetValue(eventType, out var eventData))
{
eventData = new EventData();
_eventTable.Add(eventType, eventData);
}
eventData.Callbacks.Add(handler);
}
}
public static void RemoveListener(string eventType, Callback<T, U, V, W, X, Y> handler)
{
object obj = _eventTable;
lock (obj)
{
if (_eventTable.TryGetValue(eventType, out var eventData))
{
var num = eventData.Callbacks.IndexOf(handler);
if (num >= 0)
{
eventData.Callbacks[num] = eventData.Callbacks[eventData.Callbacks.Count - 1];
eventData.Callbacks.RemoveAt(eventData.Callbacks.Count - 1);
}
}
}
}
public static void FireEvent(string eventType, T arg1, U arg2, V arg3, W arg4, X arg5, Y arg6)
{
object obj = _eventTable;
lock (obj)
{
if (_eventTable.TryGetValue(eventType, out var eventData))
{
if (eventData.IsInvoking)
{
throw new InvalidOperationException("GlobalMessenger does not support recursive FireEvent calls to the same eventType.");
}
eventData.IsInvoking = true;
eventData.Temp.AddRange(eventData.Callbacks);
for (var i = 0; i < eventData.Temp.Count; i++)
{
try
{
eventData.Temp[i](arg1, arg2, arg3, arg4, arg5, arg6);
}
catch (Exception exception)
{
DebugLog.ToConsole($"Error - {exception.Message}", MessageType.Error);
}
}
eventData.Temp.Clear();
eventData.IsInvoking = false;
}
}
}
private static IDictionary<string, EventData> _eventTable = new Dictionary<string, EventData>(ComparerLibrary.stringEqComparer);
private class EventData
{
public List<Callback<T, U, V, W, X, Y>> Callbacks = new List<Callback<T, U, V, W, X, Y>>();
public List<Callback<T, U, V, W, X, Y>> Temp = new List<Callback<T, U, V, W, X, Y>>();
public bool IsInvoking;
}
}
}

View File

@ -0,0 +1,4 @@
namespace QSB.WorldSync
{
public interface IWorldObjectTypeSubset { }
}

View File

@ -23,11 +23,10 @@ namespace QSB.WorldSync
public static IEnumerable<TWorldObject> GetWorldObjects<TWorldObject>() public static IEnumerable<TWorldObject> GetWorldObjects<TWorldObject>()
=> WorldObjects.OfType<TWorldObject>(); => WorldObjects.OfType<TWorldObject>();
public static TWorldObject GetWorldObject<TWorldObject>(int id) public static TWorldObject GetWorldFromId<TWorldObject>(int id)
where TWorldObject : IWorldObject => GetWorldObjects<TWorldObject>().ToList()[id];
=> GetWorldObjects<TWorldObject>().FirstOrDefault(x => x.ObjectId == id);
public static TWorldObject GetWorldObject<TWorldObject, TUnityObject>(TUnityObject unityObject) public static TWorldObject GetWorldFromUnity<TWorldObject, TUnityObject>(TUnityObject unityObject)
where TWorldObject : WorldObject<TUnityObject> where TWorldObject : WorldObject<TUnityObject>
where TUnityObject : MonoBehaviour where TUnityObject : MonoBehaviour
{ {
@ -41,6 +40,15 @@ namespace QSB.WorldSync
return correctWorldObject; return correctWorldObject;
} }
public static int GetIdFromUnity<TWorldObject, TUnityObject>(TUnityObject unityObject)
where TWorldObject : WorldObject<TUnityObject>
where TUnityObject : MonoBehaviour
=> GetWorldFromUnity<TWorldObject, TUnityObject>(unityObject).ObjectId;
public static int GetIdFromTypeSubset<TTypeSubset>(TTypeSubset typeSubset)
where TTypeSubset : IWorldObjectTypeSubset
=> GetWorldObjects<TTypeSubset>().ToList().IndexOf(typeSubset);
public static void RemoveWorldObjects<TWorldObject>() public static void RemoveWorldObjects<TWorldObject>()
{ {
var itemsToRemove = WorldObjects.Where(x => x is TWorldObject); var itemsToRemove = WorldObjects.Where(x => x is TWorldObject);
@ -68,7 +76,7 @@ namespace QSB.WorldSync
DebugLog.DebugWrite($"{typeof(TWorldObject).Name} init : {list.Count} instances.", MessageType.Info); DebugLog.DebugWrite($"{typeof(TWorldObject).Name} init : {list.Count} instances.", MessageType.Info);
for (var id = 0; id < list.Count; id++) for (var id = 0; id < list.Count; id++)
{ {
var obj = GetWorldObject<TWorldObject>(id) ?? CreateWorldObject<TWorldObject>(); var obj = CreateWorldObject<TWorldObject>();
obj.Init(list[id], id); obj.Init(list[id], id);
} }
return list; return list;