Little bit of cleanup and style application

This commit is contained in:
Ottermandias 2022-06-14 15:11:44 +02:00
parent 45f9b2cf8d
commit b78a6266b1

View file

@ -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<string>? ProviderGetCharacterCustomization;
internal ICallGateProvider<string, string, object>? ProviderApplyCharacterCustomization;
internal ICallGateProvider<int>? 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<string>? ProviderGetCharacterCustomization;
internal ICallGateProvider<string, string, object>? ProviderApplyCharacterCustomization;
internal ICallGateProvider<int>? 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<int>(LabelProviderApiVersion);
ProviderGetApiVersion.RegisterFunc(GetApiVersion);
}
catch (Exception ex)
{
PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApiVersion}.");
}
public void Dispose()
try
{
DisposeProviders();
ProviderGetCharacterCustomization = _pluginInterface.GetIpcProvider<string>(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<string, string, object>(LabelProviderApplyCharacterCustomization);
ProviderApplyCharacterCustomization.RegisterAction(ApplyCharacterCustomization);
}
private void InitializeProviders()
catch (Exception ex)
{
try
{
ProviderGetApiVersion = pluginInterface.GetIpcProvider<int>(LabelProviderApiVersion);
ProviderGetApiVersion.RegisterFunc(GetApiVersion);
}
catch (Exception ex)
{
PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApiVersion}.");
}
try
{
ProviderGetCharacterCustomization = pluginInterface.GetIpcProvider<string>(LabelProviderGetCharacterCustomization);
ProviderGetCharacterCustomization.RegisterFunc(GetCharacterCustomization);
}
catch (Exception ex)
{
PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}.");
}
try
{
ProviderApplyCharacterCustomization = pluginInterface.GetIpcProvider<string, string, object>(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();
}
}