Add basic Glamourer IPC

This commit is contained in:
Stanley Dimant 2022-06-14 14:37:48 +02:00
parent e4e9ed81b8
commit 7d6e1323f4
2 changed files with 91 additions and 1 deletions

View file

@ -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<string>? ProviderGetCharacterCustomization;
internal ICallGateProvider<string, string, object>? 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<string>(LabelProviderGetCharacterCustomization);
ProviderGetCharacterCustomization.RegisterFunc(GetCharacterCustomization);
}
catch (Exception ex)
{
PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}.");
}
try
{
ProviderApplyCharacterCustomization = pi.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 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();
}
}
}

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(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