quantum-space-buddies/QSB/WorldSync/WorldObjectManager.cs

111 lines
3.1 KiB
C#
Raw Normal View History

2021-12-07 15:56:08 +00:00
using OWML.Common;
using QSB.Player;
using QSB.Utility;
2021-12-07 15:56:08 +00:00
using System;
using System.Collections.Generic;
2021-03-23 13:18:29 +00:00
using UnityEngine;
namespace QSB.WorldSync
{
public abstract class WorldObjectManager : MonoBehaviour
{
2021-11-20 19:49:50 +00:00
private static readonly List<WorldObjectManager> _managers = new();
2021-03-23 13:18:29 +00:00
/// <summary>
/// Set when all WorldObjectManagers have called Init() on all their objects (AKA all the objects are created)
/// </summary>
public static bool AllObjectsAdded { get; private set; }
/// <summary>
/// Set when all WorldObjects have finished running Init()
/// </summary>
public static bool AllObjectsReady { get; private set; }
2021-03-25 22:01:10 +00:00
public virtual void Awake()
{
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
_managers.Add(this);
}
2021-03-23 13:18:29 +00:00
public virtual void OnDestroy()
{
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
_managers.Remove(this);
}
public static void SetNotReady()
{
AllObjectsAdded = false;
AllObjectsReady = false;
}
2021-07-17 08:52:46 +00:00
private void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool inUniverse)
{
AllObjectsAdded = false;
AllObjectsReady = false;
}
2021-03-23 13:18:29 +00:00
public static void Rebuild(OWScene scene)
2021-07-17 08:52:46 +00:00
{
if (!QSBNetworkManager.Instance.IsReady)
{
2021-12-11 10:50:17 +00:00
DebugLog.ToConsole($"Warning - Tried to rebuild WorldObjects when Network Manager not ready! Building when ready...", MessageType.Warning);
2021-07-17 08:52:46 +00:00
QSBCore.UnityEvents.RunWhen(() => QSBNetworkManager.Instance.IsReady, () => Rebuild(scene));
return;
}
if (QSBPlayerManager.LocalPlayerId == uint.MaxValue)
{
2021-12-11 10:50:17 +00:00
DebugLog.ToConsole($"Warning - Tried to rebuild WorldObjects when LocalPlayer is not ready! Building when ready...", MessageType.Warning);
2021-07-17 08:52:46 +00:00
QSBCore.UnityEvents.RunWhen(() => QSBPlayerManager.LocalPlayerId != uint.MaxValue, () => Rebuild(scene));
return;
}
2021-12-11 10:50:17 +00:00
DoRebuild(scene);
2021-07-17 08:52:46 +00:00
}
private static void DoRebuild(OWScene scene)
2021-03-23 13:18:29 +00:00
{
2021-12-12 06:00:21 +00:00
QSBWorldSync.RemoveWorldObjects();
_numManagersReadying = 0;
_numObjectsReadying = 0;
AllObjectsAdded = false;
AllObjectsReady = false;
2021-03-23 13:18:29 +00:00
foreach (var manager in _managers)
{
2021-07-17 08:52:46 +00:00
try
{
DebugLog.DebugWrite($"Rebuilding {manager.GetType().Name}", MessageType.Info);
2021-07-17 08:52:46 +00:00
manager.RebuildWorldObjects(scene);
}
catch (Exception ex)
{
2021-12-12 05:45:28 +00:00
DebugLog.ToConsole($"Exception - Exception when trying to rebuild WorldObjects of manager {manager.GetType().Name} : {ex.Message} Stacktrace :\r\n{ex.StackTrace}", MessageType.Error);
2021-07-17 08:52:46 +00:00
}
2021-03-23 13:18:29 +00:00
}
2021-06-18 21:38:32 +00:00
QSBCore.UnityEvents.RunWhen(() => _numManagersReadying == 0, () =>
{
AllObjectsAdded = true;
DebugLog.DebugWrite("World Objects added.", MessageType.Success);
QSBCore.UnityEvents.RunWhen(() => _numObjectsReadying == 0, () =>
{
AllObjectsReady = true;
DebugLog.DebugWrite("World Objects ready.", MessageType.Success);
});
});
}
2021-10-29 22:00:13 +00:00
2021-03-23 13:18:29 +00:00
protected abstract void RebuildWorldObjects(OWScene scene);
private static uint _numManagersReadying;
internal static uint _numObjectsReadying;
/// indicates that this won't become ready immediately
protected void StartDelayedReady() => _numManagersReadying++;
/// indicates that this is now ready
protected void FinishDelayedReady() => _numManagersReadying--;
2021-03-23 13:18:29 +00:00
}
}