Add IPC event on state change and increment IPC minor version.

This commit is contained in:
Ottermandias 2023-07-23 16:14:23 +02:00
parent fa4f22f6f5
commit 6518f958b7
4 changed files with 35 additions and 7 deletions

View file

@ -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<StateChanged.Type, nint, Lazy<string>> _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<string>(() => _designConverter.ShareBase64(state)));
}
}

View file

@ -4,6 +4,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Glamourer.Designs; using Glamourer.Designs;
using Glamourer.Events;
using Glamourer.Interop; using Glamourer.Interop;
using Glamourer.Services; using Glamourer.Services;
using Glamourer.State; using Glamourer.State;
@ -16,23 +17,21 @@ namespace Glamourer.Api;
public partial class GlamourerIpc : IDisposable public partial class GlamourerIpc : IDisposable
{ {
public const int CurrentApiVersionMajor = 0; public const int CurrentApiVersionMajor = 0;
public const int CurrentApiVersionMinor = 1; public const int CurrentApiVersionMinor = 2;
private readonly StateManager _stateManager; private readonly StateManager _stateManager;
private readonly ObjectManager _objects; private readonly ObjectManager _objects;
private readonly ActorService _actors; private readonly ActorService _actors;
private readonly ItemManager _items;
private readonly DesignConverter _designConverter; private readonly DesignConverter _designConverter;
public GlamourerIpc(DalamudPluginInterface pi, StateManager stateManager, ObjectManager objects, ActorService actors,
public GlamourerIpc(DalamudPluginInterface pi, StateManager stateManager, ObjectManager objects, ActorService actors, ItemManager items, DesignConverter designConverter, StateChanged stateChangedEvent)
DesignConverter designConverter)
{ {
_stateManager = stateManager; _stateManager = stateManager;
_objects = objects; _objects = objects;
_actors = actors; _actors = actors;
_items = items;
_designConverter = designConverter; _designConverter = designConverter;
_stateChangedEvent = stateChangedEvent;
_apiVersionProvider = new FuncProvider<int>(pi, LabelApiVersion, ApiVersion); _apiVersionProvider = new FuncProvider<int>(pi, LabelApiVersion, ApiVersion);
_apiVersionsProvider = new FuncProvider<(int Major, int Minor)>(pi, LabelApiVersions, ApiVersions); _apiVersionsProvider = new FuncProvider<(int Major, int Minor)>(pi, LabelApiVersions, ApiVersions);
@ -51,6 +50,10 @@ public partial class GlamourerIpc : IDisposable
_revertProvider = new ActionProvider<string>(pi, LabelRevert, Revert); _revertProvider = new ActionProvider<string>(pi, LabelRevert, Revert);
_revertCharacterProvider = new ActionProvider<Character?>(pi, LabelRevertCharacter, RevertCharacter); _revertCharacterProvider = new ActionProvider<Character?>(pi, LabelRevertCharacter, RevertCharacter);
_stateChangedProvider = new EventProvider<StateChanged.Type, nint, Lazy<string>>(pi, LabelStateChanged);
_stateChangedEvent.Subscribe(OnStateChanged, StateChanged.Priority.GlamourerIpc);
} }
public void Dispose() public void Dispose()
@ -69,6 +72,9 @@ public partial class GlamourerIpc : IDisposable
_applyOnlyCustomizationToCharacterProvider.Dispose(); _applyOnlyCustomizationToCharacterProvider.Dispose();
_revertProvider.Dispose(); _revertProvider.Dispose();
_revertCharacterProvider.Dispose(); _revertCharacterProvider.Dispose();
_stateChangedEvent.Unsubscribe(OnStateChanged);
_stateChangedProvider.Dispose();
} }
private IEnumerable<ActorIdentifier> FindActors(string actorName) private IEnumerable<ActorIdentifier> FindActors(string actorName)

View file

@ -57,6 +57,7 @@ public sealed class StateChanged : EventWrapper<Action<StateChanged.Type, StateC
public enum Priority public enum Priority
{ {
GlamourerIpc = int.MinValue,
} }
public StateChanged() public StateChanged()

View file

@ -439,7 +439,7 @@ public class StateManager : IReadOnlyDictionary<ActorIdentifier, ActorState>
actors = ApplyAll(state, redraw, true); actors = ApplyAll(state, redraw, true);
Glamourer.Log.Verbose( Glamourer.Log.Verbose(
$"Reset entire state of {state.Identifier.Incognito(null)} to game base. [Affecting {actors.ToLazyString("nothing")}.]"); $"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) public void ReapplyState(Actor actor)