2021-07-17 08:52:46 +00:00
using QSB.Player ;
using QSB.Utility ;
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-03-25 22:01:10 +00:00
private static readonly List < WorldObjectManager > _managers = new List < WorldObjectManager > ( ) ;
2021-03-23 13:18:29 +00:00
2021-05-02 07:54:00 +00:00
public static bool AllReady { get ; private set ; }
2021-03-25 22:01:10 +00:00
public virtual void Awake ( )
2021-05-02 07:54:00 +00:00
{
QSBSceneManager . OnSceneLoaded + = OnSceneLoaded ;
_managers . Add ( this ) ;
}
2021-03-23 13:18:29 +00:00
public virtual void OnDestroy ( )
2021-05-02 07:54:00 +00:00
{
QSBSceneManager . OnSceneLoaded - = OnSceneLoaded ;
_managers . Remove ( this ) ;
}
2021-08-09 10:49:58 +00:00
public static void SetNotReady ( ) = > AllReady = false ;
2021-07-17 08:52:46 +00:00
2021-08-09 10:49:58 +00:00
private void OnSceneLoaded ( OWScene oldScene , OWScene newScene , bool inUniverse ) = > AllReady = 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 )
{
DebugLog . ToConsole ( $"Warning - Tried to rebuild WorldObjects when Network Manager not ready!" , OWML . Common . MessageType . Warning ) ;
QSBCore . UnityEvents . RunWhen ( ( ) = > QSBNetworkManager . Instance . IsReady , ( ) = > Rebuild ( scene ) ) ;
return ;
}
if ( QSBPlayerManager . LocalPlayerId = = uint . MaxValue )
{
DebugLog . ToConsole ( $"Warning - Tried to rebuild WorldObjects when LocalPlayer is not ready!" , OWML . Common . MessageType . Warning ) ;
QSBCore . UnityEvents . RunWhen ( ( ) = > QSBPlayerManager . LocalPlayerId ! = uint . MaxValue , ( ) = > Rebuild ( scene ) ) ;
return ;
}
if ( QSBPlayerManager . LocalPlayer . PlayerStates . IsReady )
{
DoRebuild ( scene ) ;
return ;
}
QSBCore . UnityEvents . RunWhen ( ( ) = > QSBPlayerManager . LocalPlayer . PlayerStates . IsReady , ( ) = > DoRebuild ( scene ) ) ;
}
private static void DoRebuild ( OWScene scene )
2021-03-23 13:18:29 +00:00
{
foreach ( var manager in _managers )
{
2021-07-17 08:52:46 +00:00
try
{
manager . RebuildWorldObjects ( scene ) ;
}
catch ( Exception ex )
{
DebugLog . ToConsole ( $"Exception - Exception when trying to rebuild WorldObjects of manager {manager.GetType().Name} : {ex.Message} Stacktrace :\r\n{ex.StackTrace}" , OWML . Common . MessageType . Error ) ;
}
2021-03-23 13:18:29 +00:00
}
2021-06-18 21:38:32 +00:00
2021-07-17 08:52:46 +00:00
QSBCore . UnityEvents . FireInNUpdates ( ( ) = > AllReady = true , 1 ) ;
2021-03-23 13:18:29 +00:00
}
protected abstract void RebuildWorldObjects ( OWScene scene ) ;
}
}