Merge pull request #39 from rootdarkarchon/main

Adjust API for various calls, adjust revert handling
This commit is contained in:
Ottermandias 2022-06-26 16:30:48 +02:00 committed by GitHub
commit 636a0051f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 30 deletions

View file

@ -10,19 +10,23 @@ namespace Glamourer.Api;
public class GlamourerIpc : IDisposable public class GlamourerIpc : IDisposable
{ {
public const int CurrentApiVersion = 0; public const int CurrentApiVersion = 0;
public const string LabelProviderApiVersion = "Glamourer.ApiVersion"; public const string LabelProviderApiVersion = "Glamourer.ApiVersion";
public const string LabelProviderGetCharacterCustomization = "Glamourer.GetCharacterCustomization"; public const string LabelProviderGetAllCustomization = "Glamourer.GetAllCustomization";
public const string LabelProviderApplyCharacterCustomization = "Glamourer.ApplyCharacterCustomization"; public const string LabelProviderApplyAll = "Glamourer.ApplyAll";
public const string LabelProviderRevertCharacterCustomization = "Glamourer.RevertCharacterCustomization"; public const string LabelProviderApplyOnlyEquipment = "Glamourer.ApplyOnlyEquipment";
public const string LabelProviderApplyOnlyCustomization = "Glamourer.ApplyOnlyCustomization";
public const string LabelProviderRevert = "Glamourer.Revert";
private readonly ClientState _clientState; private readonly ClientState _clientState;
private readonly ObjectTable _objectTable; private readonly ObjectTable _objectTable;
private readonly DalamudPluginInterface _pluginInterface; private readonly DalamudPluginInterface _pluginInterface;
internal ICallGateProvider<string>? ProviderGetCharacterCustomization; internal ICallGateProvider<string, string?>? ProviderGetAllCustomization;
internal ICallGateProvider<string, string, object>? ProviderApplyCharacterCustomization; internal ICallGateProvider<string, string, object>? ProviderApplyAll;
internal ICallGateProvider<string, object>? ProviderRevertCharacterCustomization; internal ICallGateProvider<string, string, object>? ProviderApplyOnlyCustomization;
internal ICallGateProvider<string, string, object>? ProviderApplyOnlyEquipment;
internal ICallGateProvider<string, object>? ProviderRevert;
internal ICallGateProvider<int>? ProviderGetApiVersion; internal ICallGateProvider<int>? ProviderGetApiVersion;
public GlamourerIpc(ClientState clientState, ObjectTable objectTable, DalamudPluginInterface pluginInterface) public GlamourerIpc(ClientState clientState, ObjectTable objectTable, DalamudPluginInterface pluginInterface)
@ -39,10 +43,13 @@ public class GlamourerIpc : IDisposable
private void DisposeProviders() private void DisposeProviders()
{ {
ProviderApplyCharacterCustomization?.UnregisterFunc(); ProviderGetAllCustomization?.UnregisterFunc();
ProviderGetCharacterCustomization?.UnregisterAction(); ProviderApplyAll?.UnregisterAction();
ProviderRevertCharacterCustomization?.UnregisterAction(); ProviderApplyOnlyCustomization?.UnregisterAction();
ProviderApplyOnlyEquipment?.UnregisterAction();
ProviderRevert?.UnregisterAction();
ProviderGetApiVersion?.UnregisterFunc(); ProviderGetApiVersion?.UnregisterFunc();
} }
private void InitializeProviders() private void InitializeProviders()
@ -59,41 +66,63 @@ public class GlamourerIpc : IDisposable
try try
{ {
ProviderGetCharacterCustomization = _pluginInterface.GetIpcProvider<string>(LabelProviderGetCharacterCustomization); ProviderGetAllCustomization = _pluginInterface.GetIpcProvider<string, string?>(LabelProviderGetAllCustomization);
ProviderGetCharacterCustomization.RegisterFunc(GetCharacterCustomization); ProviderGetAllCustomization.RegisterFunc(GetAllCustomization);
} }
catch (Exception ex) catch (Exception ex)
{ {
PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}."); PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyOnlyEquipment}.");
} }
try try
{ {
ProviderApplyCharacterCustomization = ProviderApplyAll =
_pluginInterface.GetIpcProvider<string, string, object>(LabelProviderApplyCharacterCustomization); _pluginInterface.GetIpcProvider<string, string, object>(LabelProviderApplyAll);
ProviderApplyCharacterCustomization.RegisterAction(ApplyCharacterCustomization); ProviderApplyAll.RegisterAction(ApplyAll);
} }
catch (Exception ex) catch (Exception ex)
{ {
PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyCharacterCustomization}."); PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyAll}.");
} }
try try
{ {
ProviderRevertCharacterCustomization = ProviderApplyOnlyCustomization =
_pluginInterface.GetIpcProvider<string, object>(LabelProviderRevertCharacterCustomization); _pluginInterface.GetIpcProvider<string, string, object>(LabelProviderApplyOnlyCustomization);
ProviderRevertCharacterCustomization.RegisterAction(RevertCharacterCustomization); ProviderApplyOnlyCustomization.RegisterAction(ApplyOnlyCustomization);
} }
catch (Exception ex) catch (Exception ex)
{ {
PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderRevertCharacterCustomization}."); PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyOnlyCustomization}.");
}
try
{
ProviderApplyOnlyEquipment =
_pluginInterface.GetIpcProvider<string, string, object>(LabelProviderApplyOnlyEquipment);
ProviderApplyOnlyEquipment.RegisterAction(ApplyOnlyEquipment);
}
catch (Exception ex)
{
PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderApplyOnlyEquipment}.");
}
try
{
ProviderRevert =
_pluginInterface.GetIpcProvider<string, object>(LabelProviderRevert);
ProviderRevert.RegisterAction(Revert);
}
catch (Exception ex)
{
PluginLog.Error(ex, $"Error registering IPC provider for {LabelProviderRevert}.");
} }
} }
private static int GetApiVersion() private static int GetApiVersion()
=> CurrentApiVersion; => CurrentApiVersion;
private void ApplyCharacterCustomization(string customization, string characterName) private void ApplyAll(string customization, string characterName)
{ {
var save = CharacterSave.FromString(customization); var save = CharacterSave.FromString(customization);
foreach (var gameObject in _objectTable) foreach (var gameObject in _objectTable)
@ -103,14 +132,45 @@ public class GlamourerIpc : IDisposable
var player = (Character)gameObject; var player = (Character)gameObject;
Glamourer.RevertableDesigns.Revert(player); Glamourer.RevertableDesigns.Revert(player);
Glamourer.RevertableDesigns.Add(player);
save.Apply(player); save.Apply(player);
Glamourer.Penumbra.UpdateCharacters(player, null); Glamourer.Penumbra.UpdateCharacters(player, null);
break; break;
} }
} }
private void RevertCharacterCustomization(string characterName) private void ApplyOnlyCustomization(string customization, string characterName)
{
var save = CharacterSave.FromString(customization);
foreach (var gameObject in _objectTable)
{
if (gameObject.Name.ToString() != characterName)
continue;
var player = (Character)gameObject;
Glamourer.RevertableDesigns.Revert(player);
save.ApplyOnlyCustomizations(player);
Glamourer.Penumbra.UpdateCharacters(player, null);
break;
}
}
private void ApplyOnlyEquipment(string customization, string characterName)
{
var save = CharacterSave.FromString(customization);
foreach (var gameObject in _objectTable)
{
if (gameObject.Name.ToString() != characterName)
continue;
var player = (Character)gameObject;
Glamourer.RevertableDesigns.Revert(player);
save.ApplyOnlyEquipment(player);
Glamourer.Penumbra.UpdateCharacters(player, null);
break;
}
}
private void Revert(string characterName)
{ {
foreach (var gameObject in _objectTable) foreach (var gameObject in _objectTable)
{ {
@ -120,14 +180,26 @@ public class GlamourerIpc : IDisposable
var player = (Character)gameObject; var player = (Character)gameObject;
Glamourer.RevertableDesigns.Revert(player); Glamourer.RevertableDesigns.Revert(player);
Glamourer.Penumbra.UpdateCharacters(player, null); Glamourer.Penumbra.UpdateCharacters(player, null);
break; return;
} }
Glamourer.RevertableDesigns.RevertByNameWithoutApplication(characterName);
} }
private string GetCharacterCustomization() private string? GetAllCustomization(string characterName)
{ {
var save = new CharacterSave(); CharacterSave save = null!;
save.LoadCharacter(_clientState.LocalPlayer!); foreach (var gameObject in _objectTable)
return save.ToBase64(); {
if (gameObject.Name.ToString() != characterName)
continue;
var player = (Character)gameObject;
save = new CharacterSave();
save.LoadCharacter(player);
break;
}
return save?.ToBase64() ?? null;
} }
} }

View file

@ -19,12 +19,22 @@ namespace Glamourer.Designs
return true; return true;
} }
public bool RevertByNameWithoutApplication(string actorName)
{
if (!Saves.ContainsKey(actorName))
return false;
Saves.Remove(actorName);
return true;
}
public bool Revert(Character actor) public bool Revert(Character actor)
{ {
if (!Saves.TryGetValue(actor.Name.ToString(), out var save)) if (!Saves.TryGetValue(actor.Name.ToString(), out var save))
return false; return false;
save.Apply(actor); save.Apply(actor);
Saves.Remove(actor.Name.ToString());
return true; return true;
} }
} }