quantum-space-buddies/QSB/Syncs/Sectored/BaseSectoredSync.cs

234 lines
5.0 KiB
C#
Raw Normal View History

2021-12-12 10:02:19 +00:00
using OWML.Common;
using QSB.Player;
2021-08-14 15:03:01 +01:00
using QSB.SectorSync;
2021-08-22 16:53:46 +01:00
using QSB.SectorSync.WorldObjects;
2021-08-14 15:03:01 +01:00
using QSB.Utility;
using QSB.WorldSync;
using QuantumUNET.Transport;
namespace QSB.Syncs.Sectored
{
public abstract class BaseSectoredSync : SyncBase
{
public override bool IgnoreDisabledAttachedObject => false;
public override bool IgnoreNullReferenceTransform => true;
public QSBSector ReferenceSector { get; set; }
public SectorSync.SectorSync SectorSync { get; private set; }
private int _sectorIdWaitingSlot = int.MinValue;
public override void Start()
{
SectorSync = gameObject.AddComponent<SectorSync.SectorSync>();
QSBSectorManager.Instance.SectoredSyncs.Add(this);
base.Start();
}
protected override void OnDestroy()
{
base.OnDestroy();
QSBSectorManager.Instance.SectoredSyncs.Remove(this);
if (SectorSync != null)
{
Destroy(SectorSync);
}
}
2021-10-25 10:42:25 +01:00
protected override void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse)
{
base.OnSceneLoaded(oldScene, newScene, isInUniverse);
SetReferenceSector(null);
}
2021-08-14 15:03:01 +01:00
protected override void Init()
{
base.Init();
if (!QSBSectorManager.Instance.IsReady)
{
return;
}
if (!HasAuthority)
{
return;
}
QSBCore.UnityEvents.RunWhen(() => SectorSync.IsReady, InitSector);
}
private void InitSector()
{
var closestSector = SectorSync.GetClosestSector(AttachedObject.transform);
if (closestSector != null)
{
SetReferenceSector(closestSector);
}
else
{
2021-12-12 10:03:33 +00:00
DebugLog.ToConsole($"Warning - {LogName}'s initial sector was null.", MessageType.Warning);
2021-08-14 15:03:01 +01:00
}
}
public override void Update()
{
if (_sectorIdWaitingSlot == int.MinValue)
{
2021-10-23 21:17:49 +01:00
if (ReferenceSector != null && ReferenceSector.Transform != ReferenceTransform)
{
2021-12-12 10:03:33 +00:00
DebugLog.ToConsole($"Warning - {LogName} : ReferenceSector.Transform was different to ReferenceTransform. Correcting...", MessageType.Warning);
2021-10-23 21:17:49 +01:00
SetReferenceTransform(ReferenceSector.Transform);
}
2021-08-14 15:03:01 +01:00
base.Update();
return;
}
if (!WorldObjectManager.AllObjectsReady)
2021-08-14 15:03:01 +01:00
{
base.Update();
return;
}
var sector = _sectorIdWaitingSlot == -1
? null
: QSBWorldSync.GetWorldFromId<QSBSector>(_sectorIdWaitingSlot);
if (sector != ReferenceSector)
{
if (sector == null)
{
2021-12-12 10:03:33 +00:00
DebugLog.ToConsole($"Error - {LogName} got sector of ID -1. (From waiting slot.)", MessageType.Error);
2021-08-14 15:03:01 +01:00
base.Update();
return;
}
SetReferenceSector(sector);
}
_sectorIdWaitingSlot = int.MinValue;
base.Update();
}
public override void SerializeTransform(QNetworkWriter writer, bool initialState)
{
2021-12-12 10:02:19 +00:00
if (IsPlayerObject)
2021-08-14 15:03:01 +01:00
{
2021-12-12 10:02:19 +00:00
if (!QSBPlayerManager.PlayerExists(PlayerId))
{
writer.Write(-1);
return;
}
else if (!Player.IsReady)
{
writer.Write(-1);
return;
}
2021-08-14 15:03:01 +01:00
}
2021-12-12 10:02:19 +00:00
if (ReferenceSector != null)
2021-08-14 15:03:01 +01:00
{
writer.Write(ReferenceSector.ObjectId);
}
else
{
if (_isInitialized)
{
2021-12-12 10:02:19 +00:00
DebugLog.ToConsole($"Warning - ReferenceSector of {LogName} is null.", MessageType.Warning);
2021-08-14 15:03:01 +01:00
}
writer.Write(-1);
}
}
public override void DeserializeTransform(QNetworkReader reader, bool initialState)
{
int sectorId;
if (!WorldObjectManager.AllObjectsReady)
2021-08-14 15:03:01 +01:00
{
sectorId = reader.ReadInt32();
if (initialState && sectorId != -1)
{
_sectorIdWaitingSlot = sectorId;
}
2021-08-22 17:17:46 +01:00
2021-08-14 15:03:01 +01:00
return;
}
sectorId = reader.ReadInt32();
var sector = sectorId == -1
? null
: QSBWorldSync.GetWorldFromId<QSBSector>(sectorId);
if (sector != ReferenceSector)
{
if (sector == null)
{
2021-12-12 10:03:33 +00:00
DebugLog.ToConsole($"Error - {LogName} got sector of ID -1. (From deserializing transform.)", MessageType.Error);
2021-08-14 15:03:01 +01:00
return;
}
SetReferenceSector(sector);
}
}
protected bool UpdateSectors()
2021-08-14 15:03:01 +01:00
{
2021-11-19 00:28:36 -08:00
var referenceNull = ReferenceTransform == null || ReferenceSector == null;
2021-08-14 15:03:01 +01:00
var sectorManagerReady = QSBSectorManager.Instance.IsReady;
if (!sectorManagerReady)
{
if (referenceNull && HasAuthority)
{
DebugLog.ToConsole($"Warning - Reference was null, but sector manager wasn't ready. " +
2021-11-19 00:28:36 -08:00
$"Transform:{ReferenceTransform == null}, Sector:{ReferenceSector == null}",
2021-12-12 10:03:33 +00:00
MessageType.Warning);
2021-08-14 15:03:01 +01:00
}
2021-11-10 21:51:14 -08:00
DebugLog.DebugWrite($"{LogName} : Sector Manager not ready.");
2021-08-14 15:03:01 +01:00
return false;
}
if (!HasAuthority)
{
2021-08-19 16:32:18 +01:00
return true;
2021-08-14 15:03:01 +01:00
}
if (referenceNull)
2021-08-14 15:03:01 +01:00
{
if (SectorSync.IsReady)
2021-08-14 15:03:01 +01:00
{
var closestSector = SectorSync.GetClosestSector(AttachedObject.transform);
if (closestSector != null)
{
SetReferenceTransform(closestSector.Transform);
return true;
}
else
2021-08-14 15:03:01 +01:00
{
2021-12-12 10:03:33 +00:00
DebugLog.ToConsole($"Error - No closest sector found to {LogName}!", MessageType.Error);
return false;
2021-08-14 15:03:01 +01:00
}
}
else
{
return false;
2021-08-14 15:03:01 +01:00
}
}
return true;
}
protected override bool UpdateTransform()
=> UpdateSectors();
2021-08-14 15:03:01 +01:00
public void SetReferenceSector(QSBSector sector)
{
ReferenceSector = sector;
SetReferenceTransform(sector?.Transform);
}
}
}