From 7d6e1323f454c79201aa04ed62de433380d15249 Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Tue, 14 Jun 2022 14:37:48 +0200 Subject: [PATCH] Add basic Glamourer IPC --- Glamourer/Api/GlamourerIpc.cs | 87 +++++++++++++++++++++++++++++++++++ Glamourer/Glamourer.cs | 5 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 Glamourer/Api/GlamourerIpc.cs diff --git a/Glamourer/Api/GlamourerIpc.cs b/Glamourer/Api/GlamourerIpc.cs new file mode 100644 index 0000000..1d5c9b1 --- /dev/null +++ b/Glamourer/Api/GlamourerIpc.cs @@ -0,0 +1,87 @@ +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; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Glamourer.Api +{ + public class GlamourerIpc : IDisposable + { + public const string LabelProviderGetCharacterCustomization = "Glamourer.GetCharacterCustomization"; + public const string LabelProviderApplyCharacterCustomization = "Glamourer.ApplyCharacterCustomization"; + + private readonly ObjectTable objectTable; + private readonly DalamudPluginInterface pi; + + internal ICallGateProvider? ProviderGetCharacterCustomization; + internal ICallGateProvider? ProviderApplyCharacterCustomization; + + public GlamourerIpc(ObjectTable objectTable, DalamudPluginInterface pi) + { + this.objectTable = objectTable; + this.pi = pi; + + InitializeProviders(); + } + + public void Dispose() + { + DisposeProviders(); + } + + private void DisposeProviders() + { + ProviderApplyCharacterCustomization?.UnregisterFunc(); + ProviderGetCharacterCustomization?.UnregisterAction(); + } + + private void InitializeProviders() + { + try + { + ProviderGetCharacterCustomization = pi.GetIpcProvider(LabelProviderGetCharacterCustomization); + ProviderGetCharacterCustomization.RegisterFunc(GetCharacterCustomization); + } + catch (Exception ex) + { + PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}."); + } + + try + { + ProviderApplyCharacterCustomization = pi.GetIpcProvider(LabelProviderApplyCharacterCustomization); + ProviderApplyCharacterCustomization.RegisterAction((customization, characterName) => ApplyCharacterCustomization(customization, characterName)); + } + catch (Exception ex) + { + PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}."); + } + } + + private void ApplyCharacterCustomization(string customization, string characterName) + { + var save = CharacterSave.FromString(customization); + foreach (var gameObject in objectTable) + { + if (gameObject.Name.ToString() == characterName) + { + save.Apply((Character)gameObject); + } + } + } + + private string GetCharacterCustomization() + { + CharacterSave save = new CharacterSave(); + save.LoadCharacter((Character)Glamourer.GetPlayer("self")!); + return save.ToBase64(); + } + } +} diff --git a/Glamourer/Glamourer.cs b/Glamourer/Glamourer.cs index 2e5faea..4ce9552 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(Dalamud.Objects, pluginInterface); if (!Config.ApplyFixedDesigns) PlayerWatcher.Disable(); @@ -61,7 +64,7 @@ namespace Glamourer public void OnGlamourer(string command, string arguments) => _interface.ToggleVisibility(); - private static GameObject? GetPlayer(string name) + public static GameObject? GetPlayer(string name) { var lowerName = name.ToLowerInvariant(); return lowerName switch