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 Dalamud.Plugin.Ipc;
using System; 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"; _clientState = clientState;
public const string LabelProviderGetCharacterCustomization = "Glamourer.GetCharacterCustomization"; _objectTable = objectTable;
public const string LabelProviderApplyCharacterCustomization = "Glamourer.ApplyCharacterCustomization"; _pluginInterface = pluginInterface;
private readonly ClientState clientState;
private readonly ObjectTable objectTable;
private readonly DalamudPluginInterface pluginInterface;
internal ICallGateProvider<string>? ProviderGetCharacterCustomization; InitializeProviders();
internal ICallGateProvider<string, string, object>? ProviderApplyCharacterCustomization; }
internal ICallGateProvider<int>? ProviderGetApiVersion;
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; ProviderGetApiVersion = _pluginInterface.GetIpcProvider<int>(LabelProviderApiVersion);
this.objectTable = objectTable; ProviderGetApiVersion.RegisterFunc(GetApiVersion);
this.pluginInterface = pluginInterface; }
catch (Exception ex)
InitializeProviders(); {
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(); ProviderApplyCharacterCustomization =
ProviderGetCharacterCustomization?.UnregisterAction(); _pluginInterface.GetIpcProvider<string, string, object>(LabelProviderApplyCharacterCustomization);
ProviderGetApiVersion?.UnregisterFunc(); ProviderApplyCharacterCustomization.RegisterAction(ApplyCharacterCustomization);
} }
catch (Exception ex)
private void InitializeProviders()
{ {
try PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}.");
{
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();
} }
} }
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();
}
} }