quantum-space-buddies/QSB/QSBSceneManager.cs

90 lines
2.7 KiB
C#
Raw Normal View History

2022-05-04 04:20:59 +00:00
using HarmonyLib;
using OWML.Common;
using QSB.Patches;
2020-11-04 09:34:01 +00:00
using QSB.Utility;
2021-03-23 13:18:29 +00:00
using QSB.WorldSync;
2020-11-04 09:34:01 +00:00
using System;
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB;
public static class QSBSceneManager
2020-08-16 20:39:21 +00:00
{
2022-03-03 03:46:33 +00:00
public static OWScene CurrentScene => LoadManager.GetCurrentScene();
public static bool IsInUniverse => InUniverse(CurrentScene);
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
public static event Action<OWScene, OWScene, bool> OnSceneLoaded;
public static event Action<OWScene, OWScene> OnUniverseSceneLoaded;
2020-08-16 20:39:21 +00:00
2022-05-04 04:20:59 +00:00
/// <summary>
/// runs before objects are destroyed
/// </summary>
public static event LoadManager.SceneLoadEvent OnPreSceneLoad;
/// <summary>
/// runs after objects are awakened and started
/// </summary>
public static event LoadManager.SceneLoadEvent OnPostSceneLoad;
2022-03-03 03:46:33 +00:00
static QSBSceneManager()
{
LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad;
DebugLog.DebugWrite("Scene Manager ready.", MessageType.Success);
2022-05-04 04:20:59 +00:00
LoadManager.OnStartSceneLoad += (originalScene, loadScene) =>
{
DebugLog.DebugWrite($"PRE SCENE LOAD ({originalScene} -> {loadScene})", MessageType.Info);
OnPreSceneLoad?.SafeInvoke(originalScene, loadScene);
};
LoadManager.OnCompleteSceneLoad += (originalScene, loadScene) =>
Delay.RunNextFrame(() =>
{
DebugLog.DebugWrite($"POST SCENE LOAD ({originalScene} -> {loadScene})", MessageType.Info);
OnPostSceneLoad?.SafeInvoke(originalScene, loadScene);
});
2022-03-03 03:46:33 +00:00
}
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
private static void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene)
{
DebugLog.DebugWrite($"COMPLETE SCENE LOAD ({oldScene} -> {newScene})", MessageType.Info);
QSBWorldSync.RemoveWorldObjects();
2022-03-03 03:46:33 +00:00
var universe = InUniverse(newScene);
if (QSBCore.IsInMultiplayer && universe)
2020-12-14 21:20:53 +00:00
{
2022-03-03 03:46:33 +00:00
// So objects have time to be deleted, made, whatever
Delay.RunNextFrame(() => QSBWorldSync.BuildWorldObjects(newScene).Forget());
2020-12-14 21:20:53 +00:00
}
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
OnSceneLoaded?.SafeInvoke(oldScene, newScene, universe);
if (universe)
2020-12-14 21:20:53 +00:00
{
2022-03-03 03:46:33 +00:00
OnUniverseSceneLoaded?.SafeInvoke(oldScene, newScene);
2020-12-14 21:20:53 +00:00
}
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
if (newScene == OWScene.TitleScreen && QSBCore.IsInMultiplayer)
{
QSBNetworkManager.singleton.StopHost();
}
2020-12-14 21:20:53 +00:00
}
2022-03-03 03:46:33 +00:00
private static bool InUniverse(OWScene scene) =>
scene is OWScene.SolarSystem or OWScene.EyeOfTheUniverse;
2022-05-04 04:20:59 +00:00
}
[HarmonyPatch(typeof(GhostBrain))]
internal class Patch : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnModStart;
[HarmonyPrefix]
[HarmonyPatch(nameof(GhostBrain.Awake))]
private static void Awake() => DebugLog.DebugWrite("GhostBrain.Awake");
[HarmonyPrefix]
[HarmonyPatch(nameof(GhostBrain.Start))]
private static void Start() => DebugLog.DebugWrite("GhostBrain.Start");
[HarmonyPrefix]
[HarmonyPatch(nameof(GhostBrain.OnDestroy))]
private static void OnDestroy() => DebugLog.DebugWrite("GhostBrain.OnDestroy");
}