From 6518f958b7d76bb1628dbae5b28886d979a857a2 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 23 Jul 2023 16:14:23 +0200 Subject: [PATCH] Add IPC event on state change and increment IPC minor version. --- Glamourer/Api/GlamourerIpc.Events.cs | 21 +++++++++++++++++++++ Glamourer/Api/GlamourerIpc.cs | 18 ++++++++++++------ Glamourer/Events/StateChanged.cs | 1 + Glamourer/State/StateManager.cs | 2 +- 4 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 Glamourer/Api/GlamourerIpc.Events.cs diff --git a/Glamourer/Api/GlamourerIpc.Events.cs b/Glamourer/Api/GlamourerIpc.Events.cs new file mode 100644 index 0000000..dabaaf7 --- /dev/null +++ b/Glamourer/Api/GlamourerIpc.Events.cs @@ -0,0 +1,21 @@ +using System; +using Glamourer.Events; +using Glamourer.Interop.Structs; +using Glamourer.State; +using Penumbra.Api.Helpers; + +namespace Glamourer.Api; + +public partial class GlamourerIpc +{ + public const string LabelStateChanged = "Glamourer.StateChanged"; + + private readonly StateChanged _stateChangedEvent; + private readonly EventProvider> _stateChangedProvider; + + private void OnStateChanged(StateChanged.Type type, StateChanged.Source source, ActorState state, ActorData actors, object? data = null) + { + foreach (var actor in actors.Objects) + _stateChangedProvider.Invoke(type, actor.Address, new Lazy(() => _designConverter.ShareBase64(state))); + } +} diff --git a/Glamourer/Api/GlamourerIpc.cs b/Glamourer/Api/GlamourerIpc.cs index d3a2c83..f6f8620 100644 --- a/Glamourer/Api/GlamourerIpc.cs +++ b/Glamourer/Api/GlamourerIpc.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; using Glamourer.Designs; +using Glamourer.Events; using Glamourer.Interop; using Glamourer.Services; using Glamourer.State; @@ -16,23 +17,21 @@ namespace Glamourer.Api; public partial class GlamourerIpc : IDisposable { public const int CurrentApiVersionMajor = 0; - public const int CurrentApiVersionMinor = 1; + public const int CurrentApiVersionMinor = 2; private readonly StateManager _stateManager; private readonly ObjectManager _objects; private readonly ActorService _actors; - private readonly ItemManager _items; private readonly DesignConverter _designConverter; - - public GlamourerIpc(DalamudPluginInterface pi, StateManager stateManager, ObjectManager objects, ActorService actors, ItemManager items, - DesignConverter designConverter) + public GlamourerIpc(DalamudPluginInterface pi, StateManager stateManager, ObjectManager objects, ActorService actors, + DesignConverter designConverter, StateChanged stateChangedEvent) { _stateManager = stateManager; _objects = objects; _actors = actors; - _items = items; _designConverter = designConverter; + _stateChangedEvent = stateChangedEvent; _apiVersionProvider = new FuncProvider(pi, LabelApiVersion, ApiVersion); _apiVersionsProvider = new FuncProvider<(int Major, int Minor)>(pi, LabelApiVersions, ApiVersions); @@ -51,6 +50,10 @@ public partial class GlamourerIpc : IDisposable _revertProvider = new ActionProvider(pi, LabelRevert, Revert); _revertCharacterProvider = new ActionProvider(pi, LabelRevertCharacter, RevertCharacter); + + _stateChangedProvider = new EventProvider>(pi, LabelStateChanged); + + _stateChangedEvent.Subscribe(OnStateChanged, StateChanged.Priority.GlamourerIpc); } public void Dispose() @@ -69,6 +72,9 @@ public partial class GlamourerIpc : IDisposable _applyOnlyCustomizationToCharacterProvider.Dispose(); _revertProvider.Dispose(); _revertCharacterProvider.Dispose(); + + _stateChangedEvent.Unsubscribe(OnStateChanged); + _stateChangedProvider.Dispose(); } private IEnumerable FindActors(string actorName) diff --git a/Glamourer/Events/StateChanged.cs b/Glamourer/Events/StateChanged.cs index 4c6f5be..2c5c5c8 100644 --- a/Glamourer/Events/StateChanged.cs +++ b/Glamourer/Events/StateChanged.cs @@ -57,6 +57,7 @@ public sealed class StateChanged : EventWrapper actors = ApplyAll(state, redraw, true); Glamourer.Log.Verbose( $"Reset entire state of {state.Identifier.Incognito(null)} to game base. [Affecting {actors.ToLazyString("nothing")}.]"); - _event.Invoke(StateChanged.Type.Design, StateChanged.Source.Manual, state, actors, null); + _event.Invoke(StateChanged.Type.Reset, StateChanged.Source.Manual, state, actors, null); } public void ReapplyState(Actor actor)