Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
Add new ipc method
Browse files Browse the repository at this point in the history
  • Loading branch information
Limiana committed Dec 30, 2023
1 parent f10b55e commit 802b9ad
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions PalettePlus/Interop/IpcProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<string>? IApiVersion;

private static ICallGateProvider<Character, string>? IGetCharaPalette;
private static ICallGateProvider<Character, string, object>? ISetCharaPalette;
private static ICallGateProvider<Character, object>? IRemoveCharaPalette;
private static ICallGateProvider<Character, object>? IRedrawChara;
private static ICallGateProvider<Character, string, object>? IPaletteChanged;

// Constructs a new Palette object from character memory instead of fetching from cache.
private static ICallGateProvider<Character, string>? IBuildCharaPalette;
private static ICallGateProvider<Character, string>? IBuildCharaPaletteOrEmpty;

private static ICallGateProvider<string[]>? IGetSavedPalettes;

internal static void Init() {
try {
Expand All @@ -44,13 +52,19 @@ internal static void Init() {
IRemoveCharaPalette = PluginServices.Interface.GetIpcProvider<Character, object>(RemoveCharaPaletteStr);
IRemoveCharaPalette.RegisterAction(RemoveCharaPalette);

IRedrawChara = PluginServices.Interface.GetIpcProvider<Character, object>(RedrawCharaStr);
IRedrawChara.RegisterAction(RedrawChara);

IBuildCharaPalette = PluginServices.Interface.GetIpcProvider<Character, string>(BuildCharaPaletteStr);
IBuildCharaPalette.RegisterFunc(BuildCharaPalette);

IBuildCharaPaletteOrEmpty = PluginServices.Interface.GetIpcProvider<Character, string>(BuildCharaPaletteOrEmptyStr);
IBuildCharaPaletteOrEmpty.RegisterFunc(BuildCharaPaletteOrEmpty);

IPaletteChanged = PluginServices.Interface.GetIpcProvider<Character, string, object>(PaletteChangedStr);

IGetSavedPalettes = PluginServices.Interface.GetIpcProvider<string[]>(GetSavedPalettesStr);
IGetSavedPalettes.RegisterFunc(GetSavedPalettes);
} catch (Exception e) {
PluginServices.Log.Error("Failed to initialise Palette+ IPC", e);
}
Expand All @@ -61,8 +75,10 @@ internal static void Dispose() {
IGetCharaPalette?.UnregisterFunc();
ISetCharaPalette?.UnregisterAction();
IRemoveCharaPalette?.UnregisterAction();
IRedrawChara?.UnregisterAction();
IBuildCharaPalette?.UnregisterFunc();
IBuildCharaPaletteOrEmpty?.UnregisterFunc();
IGetSavedPalettes?.UnregisterFunc();
}

// IPC Methods
Expand All @@ -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();
}
Expand All @@ -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());
}

Expand Down

0 comments on commit 802b9ad

Please sign in to comment.