diff --git a/Glamourer/Api/GlamourerIpc.cs b/Glamourer/Api/GlamourerIpc.cs index fb57924..3a642cd 100644 --- a/Glamourer/Api/GlamourerIpc.cs +++ b/Glamourer/Api/GlamourerIpc.cs @@ -6,96 +6,97 @@ using Dalamud.Plugin; using Dalamud.Plugin.Ipc; using System; -namespace Glamourer.Api +namespace Glamourer.Api; + +public class GlamourerIpc : IDisposable { - public class GlamourerIpc : IDisposable + public const int CurrentApiVersion = 0; + 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) { - 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; + _clientState = clientState; + _objectTable = objectTable; + _pluginInterface = pluginInterface; - internal ICallGateProvider? ProviderGetCharacterCustomization; - internal ICallGateProvider? ProviderApplyCharacterCustomization; - internal ICallGateProvider? ProviderGetApiVersion; + InitializeProviders(); + } - public GlamourerIpc(ClientState clientState, ObjectTable objectTable, DalamudPluginInterface pluginInterface) + public void Dispose() + => DisposeProviders(); + + private void DisposeProviders() + { + ProviderApplyCharacterCustomization?.UnregisterFunc(); + ProviderGetCharacterCustomization?.UnregisterAction(); + ProviderGetApiVersion?.UnregisterFunc(); + } + + private void InitializeProviders() + { + try { - this.clientState = clientState; - this.objectTable = objectTable; - this.pluginInterface = pluginInterface; - - InitializeProviders(); + ProviderGetApiVersion = _pluginInterface.GetIpcProvider(LabelProviderApiVersion); + ProviderGetApiVersion.RegisterFunc(GetApiVersion); + } + catch (Exception ex) + { + PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApiVersion}."); } - public void Dispose() + try { - DisposeProviders(); + ProviderGetCharacterCustomization = _pluginInterface.GetIpcProvider(LabelProviderGetCharacterCustomization); + ProviderGetCharacterCustomization.RegisterFunc(GetCharacterCustomization); + } + catch (Exception ex) + { + PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}."); } - private void DisposeProviders() + try { - ProviderApplyCharacterCustomization?.UnregisterFunc(); - ProviderGetCharacterCustomization?.UnregisterAction(); - ProviderGetApiVersion?.UnregisterFunc(); + ProviderApplyCharacterCustomization = + _pluginInterface.GetIpcProvider(LabelProviderApplyCharacterCustomization); + ProviderApplyCharacterCustomization.RegisterAction(ApplyCharacterCustomization); } - - private void InitializeProviders() + catch (Exception ex) { - 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(); + PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}."); } } + + private static int GetApiVersion() + => CurrentApiVersion; + + private void ApplyCharacterCustomization(string customization, string characterName) + { + var save = CharacterSave.FromString(customization); + foreach (var gameObject in _objectTable) + { + if (gameObject.Name.ToString() != characterName) + continue; + + var player = (Character)gameObject; + save.Apply(player); + Glamourer.Penumbra.UpdateCharacters(player, null); + } + } + + private string GetCharacterCustomization() + { + var save = new CharacterSave(); + save.LoadCharacter(_clientState.LocalPlayer!); + return save.ToBase64(); + } }