From 802b9ad91011644f91cba3fe531002c710110cb0 Mon Sep 17 00:00:00 2001 From: Limiana <5073202+Limiana@users.noreply.github.com> Date: Sat, 30 Dec 2023 03:55:54 +0300 Subject: [PATCH] Add new ipc method --- PalettePlus/Interop/IpcProvider.cs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/PalettePlus/Interop/IpcProvider.cs b/PalettePlus/Interop/IpcProvider.cs index c63926d..f8663d8 100644 --- a/PalettePlus/Interop/IpcProvider.cs +++ b/PalettePlus/Interop/IpcProvider.cs @@ -6,6 +6,9 @@ using PalettePlus.Services; using PalettePlus.Palettes; +using System.Linq; +using Newtonsoft.Json; +using PalettePlus.Extensions; namespace PalettePlus.Interop { public static class IpcProvider { @@ -15,20 +18,25 @@ public static class IpcProvider { public const string GetCharaPaletteStr = "PalettePlus.GetCharaPalette"; public const string SetCharaPaletteStr = "PalettePlus.SetCharaPalette"; public const string RemoveCharaPaletteStr = "PalettePlus.RemoveCharaPalette"; + public const string RedrawCharaStr = "PalettePlus.RedrawChara"; public const string BuildCharaPaletteStr = "PalettePlus.BuildCharaPalette"; public const string BuildCharaPaletteOrEmptyStr = "PalettePlus.BuildCharaPaletteOrEmpty"; public const string PaletteChangedStr = "PalettePlus.PaletteChanged"; + public const string GetSavedPalettesStr = "PalettePlus.GetSavedPalettes"; private static ICallGateProvider? IApiVersion; private static ICallGateProvider? IGetCharaPalette; private static ICallGateProvider? ISetCharaPalette; private static ICallGateProvider? IRemoveCharaPalette; + private static ICallGateProvider? IRedrawChara; private static ICallGateProvider? IPaletteChanged; // Constructs a new Palette object from character memory instead of fetching from cache. private static ICallGateProvider? IBuildCharaPalette; private static ICallGateProvider? IBuildCharaPaletteOrEmpty; + + private static ICallGateProvider? IGetSavedPalettes; internal static void Init() { try { @@ -44,6 +52,9 @@ internal static void Init() { IRemoveCharaPalette = PluginServices.Interface.GetIpcProvider(RemoveCharaPaletteStr); IRemoveCharaPalette.RegisterAction(RemoveCharaPalette); + IRedrawChara = PluginServices.Interface.GetIpcProvider(RedrawCharaStr); + IRedrawChara.RegisterAction(RedrawChara); + IBuildCharaPalette = PluginServices.Interface.GetIpcProvider(BuildCharaPaletteStr); IBuildCharaPalette.RegisterFunc(BuildCharaPalette); @@ -51,6 +62,9 @@ internal static void Init() { IBuildCharaPaletteOrEmpty.RegisterFunc(BuildCharaPaletteOrEmpty); IPaletteChanged = PluginServices.Interface.GetIpcProvider(PaletteChangedStr); + + IGetSavedPalettes = PluginServices.Interface.GetIpcProvider(GetSavedPalettesStr); + IGetSavedPalettes.RegisterFunc(GetSavedPalettes); } catch (Exception e) { PluginServices.Log.Error("Failed to initialise Palette+ IPC", e); } @@ -61,8 +75,10 @@ internal static void Dispose() { IGetCharaPalette?.UnregisterFunc(); ISetCharaPalette?.UnregisterAction(); IRemoveCharaPalette?.UnregisterAction(); + IRedrawChara?.UnregisterAction(); IBuildCharaPalette?.UnregisterFunc(); IBuildCharaPaletteOrEmpty?.UnregisterFunc(); + IGetSavedPalettes?.UnregisterFunc(); } // IPC Methods @@ -78,7 +94,12 @@ private static void SetCharaPalette(Character chara, string json) private static void RemoveCharaPalette(Character chara) => PaletteService.RemoveCharaPalette(chara); - private static string BuildCharaPalette(Character chara) { + private unsafe static void RedrawChara(Character actor) { + actor.Redraw(); + } + + + private static string BuildCharaPalette(Character chara) { PaletteService.BuildCharaPalette(chara, out var palette, out _, false); return palette.ToString(); } @@ -89,7 +110,12 @@ private static string BuildCharaPaletteOrEmpty(Character chara) { return palette.ToString(); } - internal static void PaletteChanged(Character character, Palette? palette) { + private static string[] GetSavedPalettes() { + return PalettePlus.Config.SavedPalettes.Select(JsonConvert.SerializeObject).ToArray(); + } + + + internal static void PaletteChanged(Character character, Palette? palette) { IPaletteChanged?.SendMessage(character, palette == null ? string.Empty : palette.ToString()); }