AAAAA (world object variable syncers)

This commit is contained in:
Mister_Nebula 2022-03-13 16:14:22 +00:00
parent 9d8a937738
commit ff001cc8c8
10 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1,50 @@
using Cysharp.Threading.Tasks;
using Mirror;
using QSB.EchoesOfTheEye.EclipseDoors.VariableSync;
using QSB.EchoesOfTheEye.EclipseDoors.WorldObjects;
using QSB.Utility;
using QSB.WorldSync;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace QSB.EchoesOfTheEye.EclipseDoors;
internal class DoorManager : WorldObjectManager
{
public override WorldObjectScene WorldObjectScene => WorldObjectScene.SolarSystem;
public override bool DlcOnly => true;
public static readonly List<EclipseDoorController> Doors = new();
public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct)
{
Doors.Clear();
Doors.AddRange(QSBWorldSync.GetUnityObjects<EclipseDoorController>().SortDeterministic());
QSBWorldSync.Init<QSBEclipseDoorController, EclipseDoorController>();
var allDoors = QSBWorldSync.GetWorldObjects<QSBEclipseDoorController>().ToArray();
if (QSBCore.IsHost)
{
foreach (var item in allDoors)
{
var networkObject = Instantiate(QSBNetworkManager.singleton.DoorPrefab);
networkObject.SpawnWithServerAuthority();
}
}
await UniTask.WaitUntil(() => EclipseDoorVariableSyncer.GetSpecificSyncers<EclipseDoorVariableSyncer>().Count == Doors.Count, cancellationToken: ct);
foreach (var item in allDoors)
{
var index = Doors.IndexOf(item.AttachedObject);
var syncer = EclipseDoorVariableSyncer.GetSpecificSyncers<EclipseDoorVariableSyncer>()[index];
item.SetSyncer(syncer);
}
}
}

View File

@ -0,0 +1,6 @@
using QSB.Utility.VariableSync;
using UnityEngine;
namespace QSB.EchoesOfTheEye.EclipseDoors.VariableSync;
public class EclipseDoorVariableSyncer : WorldObjectVariableSyncer<Vector3> { }

View File

@ -0,0 +1,15 @@
using QSB.EchoesOfTheEye.EclipseDoors.VariableSync;
using QSB.WorldSync;
namespace QSB.EchoesOfTheEye.EclipseDoors.WorldObjects;
internal class QSBEclipseDoorController : VariableSyncedWorldObject<EclipseDoorController, EclipseDoorVariableSyncer>
{
public override void SendInitialState(uint to)
{
}
public override string ReturnLabel()
=> $"{base.ReturnLabel()}\r\n- SyncerValue:{Syncer.Value}\r\n- HasAuth:{Syncer.netIdentity.hasAuthority}";
}

View File

@ -0,0 +1,52 @@
using Cysharp.Threading.Tasks;
using Mirror;
using QSB.EchoesOfTheEye.EclipseDoors.VariableSync;
using QSB.EchoesOfTheEye.EclipseDoors.WorldObjects;
using QSB.EchoesOfTheEye.EclipseElevators.VariableSync;
using QSB.EchoesOfTheEye.EclipseElevators.WorldObjects;
using QSB.Utility;
using QSB.WorldSync;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace QSB.EchoesOfTheEye.EclipseDoors;
internal class EclipseElevatorManager : WorldObjectManager
{
public override WorldObjectScene WorldObjectScene => WorldObjectScene.SolarSystem;
public override bool DlcOnly => true;
public static readonly List<EclipseElevatorController> Elevators = new();
public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct)
{
Elevators.Clear();
Elevators.AddRange(QSBWorldSync.GetUnityObjects<EclipseElevatorController>().SortDeterministic());
QSBWorldSync.Init<QSBEclipseElevatorController, EclipseElevatorController>();
var allElevators = QSBWorldSync.GetWorldObjects<QSBEclipseElevatorController>().ToArray();
if (QSBCore.IsHost)
{
foreach (var item in allElevators)
{
var networkObject = Instantiate(QSBNetworkManager.singleton.ElevatorPrefab);
networkObject.SpawnWithServerAuthority();
}
}
await UniTask.WaitUntil(() => EclipseDoorVariableSyncer.GetSpecificSyncers<EclipseElevatorVariableSyncer>().Count == Elevators.Count, cancellationToken: ct);
foreach (var item in allElevators)
{
var index = Elevators.IndexOf(item.AttachedObject);
var syncer = EclipseDoorVariableSyncer.GetSpecificSyncers<EclipseElevatorVariableSyncer>()[index];
item.SetSyncer(syncer);
}
}
}

View File

@ -0,0 +1,6 @@
using QSB.Utility.VariableSync;
using UnityEngine;
namespace QSB.EchoesOfTheEye.EclipseElevators.VariableSync;
internal class EclipseElevatorVariableSyncer : WorldObjectVariableSyncer<Vector3> { }

View File

@ -0,0 +1,15 @@
using QSB.EchoesOfTheEye.EclipseElevators.VariableSync;
using QSB.WorldSync;
namespace QSB.EchoesOfTheEye.EclipseElevators.WorldObjects;
internal class QSBEclipseElevatorController : VariableSyncedWorldObject<EclipseElevatorController, EclipseElevatorVariableSyncer>
{
public override void SendInitialState(uint to)
{
}
public override string ReturnLabel()
=> $"{base.ReturnLabel()}\r\n- SyncerValue:{Syncer.Value}\r\n- HasAuth:{Syncer.netIdentity.hasAuthority}";
}

View File

@ -7,6 +7,8 @@ using QSB.Anglerfish.TransformSync;
using QSB.AuthoritySync;
using QSB.ClientServerStateSync;
using QSB.DeathSync;
using QSB.EchoesOfTheEye.EclipseDoors.VariableSync;
using QSB.EchoesOfTheEye.EclipseElevators.VariableSync;
using QSB.EchoesOfTheEye.RaftSync.TransformSync;
using QSB.JellyfishSync.TransformSync;
using QSB.Messaging;
@ -22,6 +24,7 @@ using QSB.Syncs.Occasional;
using QSB.TimeSync;
using QSB.Tools.ProbeTool.TransformSync;
using QSB.Utility;
using QSB.Utility.VariableSync;
using QSB.WorldSync;
using System;
using System.Linq;
@ -43,6 +46,8 @@ public class QSBNetworkManager : NetworkManager, IAddComponentOnStart
public GameObject JellyfishPrefab { get; private set; }
public GameObject OccasionalPrefab { get; private set; }
public GameObject RaftPrefab { get; private set; }
public GameObject DoorPrefab { get; private set; }
public GameObject ElevatorPrefab { get; private set; }
private string PlayerName { get; set; }
private const int MaxConnections = 128;
@ -117,6 +122,12 @@ public class QSBNetworkManager : NetworkManager, IAddComponentOnStart
RaftPrefab = MakeNewNetworkObject(8, "NetworkRaft", typeof(RaftTransformSync));
spawnPrefabs.Add(RaftPrefab);
DoorPrefab = MakeNewNetworkObject(9, "NetworkEclipseDoor", typeof(EclipseDoorVariableSyncer));
spawnPrefabs.Add(DoorPrefab);
ElevatorPrefab = MakeNewNetworkObject(10, "NetworkEclipseElevator", typeof(EclipseElevatorVariableSyncer));
spawnPrefabs.Add(ElevatorPrefab);
ConfigureNetworkManager();
}

View File

@ -0,0 +1,8 @@
using QSB.WorldSync;
namespace QSB.Utility.VariableSync;
public interface IWorldObjectVariableSyncer
{
public void Init(IWorldObject worldObject);
}

View File

@ -0,0 +1,37 @@
using QSB.WorldSync;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.SceneManagement;
namespace QSB.Utility.VariableSync;
public class WorldObjectVariableSyncer<T> : BaseVariableSyncer<T>, IWorldObjectVariableSyncer
{
private static List<BaseVariableSyncer<T>> _instances = new();
public IWorldObject AttachedWorldObject { get; private set; }
public static List<U> GetSpecificSyncers<U>()
=> _instances.OfType<U>().ToList();
public void Init(IWorldObject worldObject)
{
AttachedWorldObject = worldObject;
}
public override void OnStartClient()
{
_instances.Add(this);
DontDestroyOnLoad(this);
base.OnStartClient();
}
public override void OnStopClient()
{
_instances.Remove(this);
// DontDestroyOnLoad simply moves a GO to a scene called "DontDestroyOnLoad"
// so to undo it we can just move it back
SceneManager.MoveGameObjectToScene(gameObject, SceneManager.GetActiveScene());
base.OnStopClient();
}
}

View File

@ -0,0 +1,17 @@
using QSB.Utility.VariableSync;
using UnityEngine;
namespace QSB.WorldSync;
internal abstract class VariableSyncedWorldObject<T, U> : WorldObject<T>
where T : MonoBehaviour
where U : IWorldObjectVariableSyncer
{
protected U Syncer;
public void SetSyncer(U syncer)
{
Syncer = syncer;
Syncer.Init(this);
}
}