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

239 lines
4.9 KiB
C#
Raw Normal View History

2022-01-14 20:59:42 -08:00
using Mirror;
using OWML.Common;
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;
namespace QSB.Syncs.Sectored
{
2022-01-14 22:39:41 -08:00
public abstract class BaseSectoredSync : SyncBase
2022-01-14 20:59:42 -08:00
{
2022-01-16 04:52:48 -08:00
public override bool AllowDisabledAttachedObject => false;
public override bool AllowNullReferenceTransform => true;
2022-01-14 20:59:42 -08:00
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>();
2022-01-14 22:39:41 -08:00
QSBSectorManager.Instance.SectoredSyncs.Add(this);
2022-01-14 20:59:42 -08:00
base.Start();
}
protected override void OnDestroy()
{
base.OnDestroy();
2022-01-14 22:39:41 -08:00
QSBSectorManager.Instance.SectoredSyncs.Remove(this);
2022-01-14 20:59:42 -08:00
if (SectorSync != null)
{
Destroy(SectorSync);
}
}
protected override void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse)
{
base.OnSceneLoaded(oldScene, newScene, isInUniverse);
SetReferenceSector(null);
}
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();
if (closestSector != null)
{
SetReferenceSector(closestSector);
}
else
{
DebugLog.ToConsole($"Warning - {LogName}'s initial sector was null.", MessageType.Warning);
}
}
protected override void Update()
{
if (_sectorIdWaitingSlot == int.MinValue)
{
if (ReferenceSector != null && ReferenceSector.Transform != ReferenceTransform)
{
DebugLog.ToConsole($"Warning - {LogName} : ReferenceSector.Transform was different to ReferenceTransform. Correcting...", MessageType.Warning);
SetReferenceTransform(ReferenceSector.Transform);
}
base.Update();
return;
}
if (!WorldObjectManager.AllObjectsReady)
{
base.Update();
return;
}
var sector = _sectorIdWaitingSlot == -1
? null
: _sectorIdWaitingSlot.GetWorldObject<QSBSector>();
if (sector != ReferenceSector)
{
if (sector == null)
{
DebugLog.ToConsole($"Error - {LogName} got sector of ID -1. (From waiting slot.)", MessageType.Error);
base.Update();
return;
}
SetReferenceSector(sector);
}
_sectorIdWaitingSlot = int.MinValue;
base.Update();
}
protected override void Serialize(NetworkWriter writer)
2022-01-14 20:59:42 -08:00
{
2022-01-16 04:51:37 -08:00
base.Serialize(writer);
2022-01-14 20:59:42 -08:00
if (IsPlayerObject)
{
if (Player == null)
{
// happens once right when you start hosting
writer.Write(-1);
return;
}
if (!Player.IsReady)
{
writer.Write(-1);
return;
}
}
if (ReferenceSector != null)
{
writer.Write(ReferenceSector.ObjectId);
}
else
{
2022-01-16 04:34:52 -08:00
if (IsInitialized)
2022-01-14 20:59:42 -08:00
{
DebugLog.ToConsole($"Warning - ReferenceSector of {LogName} is null.", MessageType.Warning);
}
writer.Write(-1);
}
}
protected override void Deserialize(NetworkReader reader)
2022-01-14 20:59:42 -08:00
{
2022-01-16 04:51:37 -08:00
base.Deserialize(reader);
2022-01-14 20:59:42 -08:00
int sectorId;
if (!WorldObjectManager.AllObjectsReady)
{
sectorId = reader.ReadInt();
if (sectorId != -1)
2022-01-14 20:59:42 -08:00
{
_sectorIdWaitingSlot = sectorId;
}
return;
}
sectorId = reader.ReadInt();
var sector = sectorId == -1
? null
: sectorId.GetWorldObject<QSBSector>();
if (sector != ReferenceSector)
{
if (sector == null)
{
DebugLog.ToConsole($"Error - {LogName} got sector of ID -1. (From deserializing transform.)", MessageType.Error);
return;
}
SetReferenceSector(sector);
}
}
protected bool UpdateSectors()
{
var referenceNull = ReferenceTransform == null || ReferenceSector == null;
var sectorManagerReady = QSBSectorManager.Instance.IsReady;
if (!sectorManagerReady)
{
if (referenceNull && hasAuthority)
{
DebugLog.ToConsole($"Warning - Reference was null, but sector manager wasn't ready. " +
$"Transform:{ReferenceTransform == null}, Sector:{ReferenceSector == null}",
MessageType.Warning);
}
DebugLog.DebugWrite($"{LogName} : Sector Manager not ready.");
return false;
}
if (!hasAuthority)
{
return true;
}
if (referenceNull)
{
if (SectorSync.IsReady)
{
var closestSector = SectorSync.GetClosestSector();
if (closestSector != null)
{
SetReferenceSector(closestSector);
return true;
}
else
{
DebugLog.ToConsole($"Error - No closest sector found to {LogName}!", MessageType.Error);
return false;
}
}
else
{
return false;
}
}
return true;
}
protected override bool UpdateTransform()
=> UpdateSectors();
public void SetReferenceSector(QSBSector sector)
{
ReferenceSector = sector;
SetReferenceTransform(sector?.Transform);
}
}
2021-08-14 15:03:01 +01:00
}