diff --git a/Glamourer/Api/GlamourerIpc.cs b/Glamourer/Api/GlamourerIpc.cs new file mode 100644 index 0000000..fb57924 --- /dev/null +++ b/Glamourer/Api/GlamourerIpc.cs @@ -0,0 +1,101 @@ +using Dalamud.Game.ClientState; +using Dalamud.Game.ClientState.Objects; +using Dalamud.Game.ClientState.Objects.Types; +using Dalamud.Logging; +using Dalamud.Plugin; +using Dalamud.Plugin.Ipc; +using System; + +namespace Glamourer.Api +{ + public class GlamourerIpc : IDisposable + { + public const string LabelProviderApiVersion = "Glamourer.ApiVersion"; + public const string LabelProviderGetCharacterCustomization = "Glamourer.GetCharacterCustomization"; + public const string LabelProviderApplyCharacterCustomization = "Glamourer.ApplyCharacterCustomization"; + private readonly ClientState clientState; + private readonly ObjectTable objectTable; + private readonly DalamudPluginInterface pluginInterface; + + internal ICallGateProvider? ProviderGetCharacterCustomization; + internal ICallGateProvider? ProviderApplyCharacterCustomization; + internal ICallGateProvider? ProviderGetApiVersion; + + public GlamourerIpc(ClientState clientState, ObjectTable objectTable, DalamudPluginInterface pluginInterface) + { + this.clientState = clientState; + this.objectTable = objectTable; + this.pluginInterface = pluginInterface; + + InitializeProviders(); + } + + public void Dispose() + { + DisposeProviders(); + } + + private void DisposeProviders() + { + ProviderApplyCharacterCustomization?.UnregisterFunc(); + ProviderGetCharacterCustomization?.UnregisterAction(); + ProviderGetApiVersion?.UnregisterFunc(); + } + + private void InitializeProviders() + { + try + { + ProviderGetApiVersion = pluginInterface.GetIpcProvider(LabelProviderApiVersion); + ProviderGetApiVersion.RegisterFunc(GetApiVersion); + } + catch (Exception ex) + { + PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApiVersion}."); + } + + try + { + ProviderGetCharacterCustomization = pluginInterface.GetIpcProvider(LabelProviderGetCharacterCustomization); + ProviderGetCharacterCustomization.RegisterFunc(GetCharacterCustomization); + } + catch (Exception ex) + { + PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}."); + } + + try + { + ProviderApplyCharacterCustomization = pluginInterface.GetIpcProvider(LabelProviderApplyCharacterCustomization); + ProviderApplyCharacterCustomization.RegisterAction((customization, characterName) => ApplyCharacterCustomization(customization, characterName)); + } + catch (Exception ex) + { + PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}."); + } + } + + private int GetApiVersion() => 0; + + private void ApplyCharacterCustomization(string customization, string characterName) + { + var save = CharacterSave.FromString(customization); + foreach (var gameObject in objectTable) + { + if (gameObject.Name.ToString() == characterName) + { + var player = (Character)gameObject; + save.Apply(player); + Glamourer.Penumbra.UpdateCharacters(player, null); + } + } + } + + private string GetCharacterCustomization() + { + CharacterSave save = new CharacterSave(); + save.LoadCharacter(clientState.LocalPlayer!); + return save.ToBase64(); + } + } +} diff --git a/Glamourer/Glamourer.cs b/Glamourer/Glamourer.cs index 2e5faea..c20ca21 100644 --- a/Glamourer/Glamourer.cs +++ b/Glamourer/Glamourer.cs @@ -4,6 +4,7 @@ using System.Reflection; using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Game.Command; using Dalamud.Plugin; +using Glamourer.Api; using Glamourer.Customization; using Glamourer.Designs; using Glamourer.FileSystem; @@ -27,6 +28,7 @@ namespace Glamourer public readonly DesignManager Designs; public readonly FixedDesigns FixedDesigns; public static RevertableDesigns RevertableDesigns = new(); + public readonly GlamourerIpc GlamourerIpc; public static string Version = string.Empty; @@ -41,6 +43,7 @@ namespace Glamourer Designs = new DesignManager(); Penumbra = new PenumbraAttach(Config.AttachToPenumbra); PlayerWatcher = PlayerWatchFactory.Create(Dalamud.Framework, Dalamud.ClientState, Dalamud.Objects); + GlamourerIpc = new GlamourerIpc(Dalamud.ClientState, Dalamud.Objects, Dalamud.PluginInterface); if (!Config.ApplyFixedDesigns) PlayerWatcher.Disable(); @@ -200,6 +203,7 @@ namespace Glamourer Penumbra.Dispose(); PlayerWatcher.Dispose(); _interface.Dispose(); + GlamourerIpc.Dispose(); Dalamud.Commands.RemoveHandler("/glamour"); Dalamud.Commands.RemoveHandler("/glamourer"); }