Merge pull request #37 from rootdarkarchon/main

Add basic Glamourer IPC
This commit is contained in:
Ottermandias 2022-06-14 15:03:01 +02:00 committed by GitHub
commit 45f9b2cf8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 0 deletions

View file

@ -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<string>? ProviderGetCharacterCustomization;
internal ICallGateProvider<string, string, object>? ProviderApplyCharacterCustomization;
internal ICallGateProvider<int>? 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<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();
}
}
}

View file

@ -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");
}