From d96175ed16920f36e5851df736016657f11a9cda Mon Sep 17 00:00:00 2001
From: MidoriKami <9083275+MidoriKami@users.noreply.github.com>
Date: Mon, 11 Sep 2023 22:10:36 -0700
Subject: [PATCH] Add DutyStatePluginScoped (#1382)
---
Dalamud/Game/DutyState/DutyState.cs | 95 ++++++++++++++-----
.../DutyState/DutyStateAddressResolver.cs | 2 -
Dalamud/Plugin/Services/IDutyState.cs | 4 +-
3 files changed, 74 insertions(+), 27 deletions(-)
diff --git a/Dalamud/Game/DutyState/DutyState.cs b/Dalamud/Game/DutyState/DutyState.cs
index 49fc874e3..2f117a492 100644
--- a/Dalamud/Game/DutyState/DutyState.cs
+++ b/Dalamud/Game/DutyState/DutyState.cs
@@ -1,25 +1,19 @@
-using System;
-using System.Runtime.InteropServices;
+using System.Runtime.InteropServices;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Hooking;
using Dalamud.IoC;
using Dalamud.IoC.Internal;
using Dalamud.Plugin.Services;
-using Dalamud.Utility;
namespace Dalamud.Game.DutyState;
///
/// This class represents the state of the currently occupied duty.
///
-[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.EarlyLoadedService]
-#pragma warning disable SA1015
-[ResolveVia]
-#pragma warning restore SA1015
-public unsafe class DutyState : IDisposable, IServiceType, IDutyState
+internal unsafe class DutyState : IDisposable, IServiceType, IDutyState
{
private readonly DutyStateAddressResolver address;
private readonly Hook contentDirectorNetworkMessageHook;
@@ -49,16 +43,16 @@ public unsafe class DutyState : IDisposable, IServiceType, IDutyState
private delegate byte SetupContentDirectNetworkMessageDelegate(IntPtr a1, IntPtr a2, ushort* a3);
///
- public event EventHandler DutyStarted;
+ public event EventHandler? DutyStarted;
///
- public event EventHandler DutyWiped;
+ public event EventHandler? DutyWiped;
///
- public event EventHandler DutyRecommenced;
+ public event EventHandler? DutyRecommenced;
///
- public event EventHandler DutyCompleted;
+ public event EventHandler? DutyCompleted;
///
public bool IsDutyStarted { get; private set; }
@@ -66,7 +60,7 @@ public unsafe class DutyState : IDisposable, IServiceType, IDutyState
private bool CompletedThisTerritory { get; set; }
///
- void IDisposable.Dispose()
+ public void Dispose()
{
this.contentDirectorNetworkMessageHook.Dispose();
this.framework.Update -= this.FrameworkOnUpdateEvent;
@@ -92,33 +86,33 @@ public unsafe class DutyState : IDisposable, IServiceType, IDutyState
// Duty Commenced
case 0x4000_0001:
this.IsDutyStarted = true;
- this.DutyStarted.InvokeSafely(this, this.clientState.TerritoryType);
+ this.DutyStarted?.Invoke(this, this.clientState.TerritoryType);
break;
// Party Wipe
case 0x4000_0005:
this.IsDutyStarted = false;
- this.DutyWiped.InvokeSafely(this, this.clientState.TerritoryType);
+ this.DutyWiped?.Invoke(this, this.clientState.TerritoryType);
break;
// Duty Recommence
case 0x4000_0006:
this.IsDutyStarted = true;
- this.DutyRecommenced.InvokeSafely(this, this.clientState.TerritoryType);
+ this.DutyRecommenced?.Invoke(this, this.clientState.TerritoryType);
break;
// Duty Completed Flytext Shown
case 0x4000_0002 when !this.CompletedThisTerritory:
this.IsDutyStarted = false;
this.CompletedThisTerritory = true;
- this.DutyCompleted.InvokeSafely(this, this.clientState.TerritoryType);
+ this.DutyCompleted?.Invoke(this, this.clientState.TerritoryType);
break;
// Duty Completed
case 0x4000_0003 when !this.CompletedThisTerritory:
this.IsDutyStarted = false;
this.CompletedThisTerritory = true;
- this.DutyCompleted.InvokeSafely(this, this.clientState.TerritoryType);
+ this.DutyCompleted?.Invoke(this, this.clientState.TerritoryType);
break;
}
}
@@ -161,11 +155,68 @@ public unsafe class DutyState : IDisposable, IServiceType, IDutyState
}
private bool IsBoundByDuty()
+ => this.condition.Any(ConditionFlag.BoundByDuty,
+ ConditionFlag.BoundByDuty56,
+ ConditionFlag.BoundByDuty95);
+
+ private bool IsInCombat()
+ => this.condition.Any(ConditionFlag.InCombat);
+}
+
+///
+/// Plugin scoped version of DutyState.
+///
+[PluginInterface]
+[InterfaceVersion("1.0")]
+[ServiceManager.ScopedService]
+#pragma warning disable SA1015
+[ResolveVia]
+#pragma warning restore SA1015
+internal class DutyStatePluginScoped : IDisposable, IServiceType, IDutyState
+{
+ [ServiceManager.ServiceDependency]
+ private readonly DutyState dutyStateService = Service.Get();
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ internal DutyStatePluginScoped()
{
- return this.condition[ConditionFlag.BoundByDuty] ||
- this.condition[ConditionFlag.BoundByDuty56] ||
- this.condition[ConditionFlag.BoundByDuty95];
+ this.dutyStateService.DutyStarted += this.DutyStartedForward;
+ this.dutyStateService.DutyWiped += this.DutyWipedForward;
+ this.dutyStateService.DutyRecommenced += this.DutyRecommencedForward;
+ this.dutyStateService.DutyCompleted += this.DutyCompletedForward;
}
- private bool IsInCombat() => this.condition[ConditionFlag.InCombat];
+ ///
+ public event EventHandler? DutyStarted;
+
+ ///
+ public event EventHandler? DutyWiped;
+
+ ///
+ public event EventHandler? DutyRecommenced;
+
+ ///
+ public event EventHandler? DutyCompleted;
+
+ ///
+ public bool IsDutyStarted => this.dutyStateService.IsDutyStarted;
+
+ ///
+ public void Dispose()
+ {
+ this.dutyStateService.DutyStarted -= this.DutyStartedForward;
+ this.dutyStateService.DutyWiped -= this.DutyWipedForward;
+ this.dutyStateService.DutyRecommenced -= this.DutyRecommencedForward;
+ this.dutyStateService.DutyCompleted -= this.DutyCompletedForward;
+ }
+
+ private void DutyStartedForward(object sender, ushort territoryId) => this.DutyStarted?.Invoke(sender, territoryId);
+
+ private void DutyWipedForward(object sender, ushort territoryId) => this.DutyWiped?.Invoke(sender, territoryId);
+
+ private void DutyRecommencedForward(object sender, ushort territoryId) => this.DutyRecommenced?.Invoke(sender, territoryId);
+
+ private void DutyCompletedForward(object sender, ushort territoryId) => this.DutyCompleted?.Invoke(sender, territoryId);
}
diff --git a/Dalamud/Game/DutyState/DutyStateAddressResolver.cs b/Dalamud/Game/DutyState/DutyStateAddressResolver.cs
index 801e5ef55..436883dc2 100644
--- a/Dalamud/Game/DutyState/DutyStateAddressResolver.cs
+++ b/Dalamud/Game/DutyState/DutyStateAddressResolver.cs
@@ -1,5 +1,3 @@
-using System;
-
namespace Dalamud.Game.DutyState;
///
diff --git a/Dalamud/Plugin/Services/IDutyState.cs b/Dalamud/Plugin/Services/IDutyState.cs
index a2331364c..3d49f68cb 100644
--- a/Dalamud/Plugin/Services/IDutyState.cs
+++ b/Dalamud/Plugin/Services/IDutyState.cs
@@ -1,6 +1,4 @@
-using System;
-
-namespace Dalamud.Plugin.Services;
+namespace Dalamud.Plugin.Services;
///
/// This class represents the state of the currently occupied duty.