feat: add GameLifecycle service

This commit is contained in:
goat 2023-03-27 19:43:29 +02:00
parent 03a409306f
commit 9ff7eb801f
No known key found for this signature in database
GPG key ID: 49E2AA8C6A76498B
3 changed files with 79 additions and 3 deletions

View file

@ -23,6 +23,7 @@ namespace Dalamud.Game.ClientState;
[ServiceManager.BlockingEarlyLoadedService]
public sealed class ClientState : IDisposable, IServiceType
{
private readonly GameLifecycle lifecycle;
private readonly ClientStateAddressResolver address;
private readonly Hook<SetupTerritoryTypeDelegate> setupTerritoryTypeHook;
@ -36,8 +37,9 @@ public sealed class ClientState : IDisposable, IServiceType
private bool lastFramePvP = false;
[ServiceManager.ServiceConstructor]
private ClientState(SigScanner sigScanner, DalamudStartInfo startInfo)
private ClientState(SigScanner sigScanner, DalamudStartInfo startInfo, GameLifecycle lifecycle)
{
this.lifecycle = lifecycle;
this.address = new ClientStateAddressResolver();
this.address.Setup(sigScanner);
@ -174,6 +176,8 @@ public sealed class ClientState : IDisposable, IServiceType
this.IsLoggedIn = true;
this.Login?.InvokeSafely(this, null);
gameGui.ResetUiHideState();
this.lifecycle.ResetLogout();
}
if (!condition.Any() && this.lastConditionNone == false)
@ -183,6 +187,8 @@ public sealed class ClientState : IDisposable, IServiceType
this.IsLoggedIn = false;
this.Logout?.InvokeSafely(this, null);
gameGui.ResetUiHideState();
this.lifecycle.SetLogout();
}
this.IsPvP = GameMain.IsInPvPArea();

View file

@ -25,6 +25,8 @@ namespace Dalamud.Game;
[ServiceManager.BlockingEarlyLoadedService]
public sealed class Framework : IDisposable, IServiceType
{
private readonly GameLifecycle lifecycle;
private static Stopwatch statsStopwatch = new();
private readonly Stopwatch updateStopwatch = new();
@ -41,10 +43,11 @@ public sealed class Framework : IDisposable, IServiceType
[ServiceManager.ServiceDependency]
private readonly DalamudConfiguration configuration = Service<DalamudConfiguration>.Get();
[ServiceManager.ServiceConstructor]
private Framework(SigScanner sigScanner)
private Framework(SigScanner sigScanner, GameLifecycle lifecycle)
{
this.lifecycle = lifecycle;
this.hitchDetector = new HitchDetector("FrameworkUpdate", this.configuration.FrameworkUpdateHitch);
this.Address = new FrameworkAddressResolver();
@ -489,6 +492,10 @@ public sealed class Framework : IDisposable, IServiceType
this.IsFrameworkUnloading = true;
this.DispatchUpdateEvents = false;
// All the same, for now...
this.lifecycle.SetShuttingDown();
this.lifecycle.SetUnloading();
Log.Information("Framework::Destroy!");
Service<Dalamud>.Get().Unload();
this.RunPendingTickTasks();

View file

@ -0,0 +1,63 @@
using System.Threading;
using Dalamud.IoC;
using Dalamud.IoC.Internal;
namespace Dalamud.Game;
/// <summary>
/// Class offering cancellation tokens for common gameplay events.
/// </summary>
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
public class GameLifecycle : IServiceType
{
private readonly CancellationTokenSource dalamudUnloadCts = new();
private readonly CancellationTokenSource gameShutdownCts = new();
private CancellationTokenSource logoutCts = new();
/// <summary>
/// Initializes a new instance of the <see cref="GameLifecycle"/> class.
/// </summary>
[ServiceManager.ServiceConstructor]
internal GameLifecycle()
{
}
/// <summary>
/// Gets a token that is cancelled when Dalamud is unloading.
/// </summary>
public CancellationToken DalamudUnloadingToken => this.dalamudUnloadCts.Token;
/// <summary>
/// Gets a token that is cancelled when the game is shutting down.
/// </summary>
public CancellationToken GameShuttingDownToken => this.gameShutdownCts.Token;
/// <summary>
/// Gets a token that is cancelled when a character is logging out.
/// </summary>
public CancellationToken LogoutToken => this.logoutCts.Token;
/// <summary>
/// Mark an unload.
/// </summary>
internal void SetUnloading() => this.dalamudUnloadCts.Cancel();
/// <summary>
/// Mark a shutdown.
/// </summary>
internal void SetShuttingDown() => this.gameShutdownCts.Cancel();
/// <summary>
/// Mark a logout.
/// </summary>
internal void SetLogout() => this.logoutCts.Cancel();
/// <summary>
/// Unmark a logout.
/// </summary>
internal void ResetLogout() => this.logoutCts = new();
}