From 0bd1640796aae01a7179643f94d56cad82c87a1c Mon Sep 17 00:00:00 2001 From: Jackson <9527380+Jaksuhn@users.noreply.github.com> Date: Tue, 8 Oct 2024 23:59:38 +0200 Subject: [PATCH] make a new config cause I cbf --- YesAlready/Configuration.cs | 11 ++-- YesAlready/Features/CustomAddonCallbacks.cs | 65 ++++++++++++++++++++- YesAlready/UI/Tabs/Custom.cs | 57 +++--------------- 3 files changed, 76 insertions(+), 57 deletions(-) diff --git a/YesAlready/Configuration.cs b/YesAlready/Configuration.cs index 038e398..b8f593c 100644 --- a/YesAlready/Configuration.cs +++ b/YesAlready/Configuration.cs @@ -1,11 +1,10 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; - using Dalamud.Configuration; using Dalamud.Game.ClientState.Keys; using Dalamud.Game.Text; using Newtonsoft.Json; +using System.Collections.Generic; +using System.IO; +using System.Linq; namespace YesAlready; @@ -69,13 +68,13 @@ public partial class Configuration() : IPluginConfiguration public bool MiragePrismRemoveDispel { get; set; } = false; public bool MiragePrismExecuteCast { get; set; } = false; - public List CustomBothers { get; set; } = []; + public List CustomCallbacks { get; set; } = []; public class CustomBother { public string Addon { get; set; } public bool UpdateState { get; set; } = true; - public object[] CallbackParams { get; set; } + public string CallbackParams { get; set; } } public enum TradeMultipleMode diff --git a/YesAlready/Features/CustomAddonCallbacks.cs b/YesAlready/Features/CustomAddonCallbacks.cs index 4463c5b..19677ef 100644 --- a/YesAlready/Features/CustomAddonCallbacks.cs +++ b/YesAlready/Features/CustomAddonCallbacks.cs @@ -1,7 +1,9 @@ using Dalamud.Game.Addon.Lifecycle; using Dalamud.Game.Addon.Lifecycle.AddonArgTypes; using ECommons.Automation; +using System.Collections.Generic; using System.Linq; +using System.Text; using YesAlready.BaseFeatures; namespace YesAlready.Features; @@ -10,7 +12,7 @@ public class CustomAddonCallbacks : BaseFeature public override void Enable() { base.Enable(); - foreach (var addon in P.Config.CustomBothers) + foreach (var addon in P.Config.CustomCallbacks) Svc.AddonLifecycle.RegisterListener(AddonEvent.PostSetup, addon.Addon, AddonSetup); } @@ -25,7 +27,66 @@ protected static unsafe void AddonSetup(AddonEvent eventType, AddonArgs addonInf if (!P.Active) return; var addon = addonInfo.Base(); - var callbacks = P.Config.CustomBothers.First(x => x.Addon == addonInfo.AddonName).CallbackParams; + var callbacks = P.Config.CustomCallbacks.First(x => x.Addon == addonInfo.AddonName).CallbackParams; Callback.Fire(addon, true, callbacks); } + + public static string CallbackToString(object[] args) + { + var sb = new StringBuilder(); + foreach (var obj in args) + { + if (obj is uint) + sb.Append('u'); + else if (obj is string str && str.Contains(' ')) + sb.Append($"\"{str}\""); + else + sb.Append(obj.ToString()); + sb.Append(' '); + } + if (sb.Length >= 2) + sb.Length -= 2; + return sb.ToString(); + } + + public static object[] CallbackToArray(string args) + { + var rawValues = args.Split(' '); + var valueArgs = new List(); + + var current = ""; + var inQuotes = false; + + for (var i = 0; i < rawValues.Length; i++) + { + if (!inQuotes) + { + if (rawValues[i].StartsWith('\"')) + { + inQuotes = true; + current = rawValues[i].TrimStart('"'); + } + else + { + if (int.TryParse(rawValues[i], out var iValue)) valueArgs.Add(iValue); + else if (uint.TryParse(rawValues[i].TrimEnd('U', 'u'), out var uValue)) valueArgs.Add(uValue); + else if (bool.TryParse(rawValues[i], out var bValue)) valueArgs.Add(bValue); + else valueArgs.Add(rawValues[i]); + } + } + else + { + if (rawValues[i].EndsWith('\"')) + { + inQuotes = false; + current += " " + rawValues[i].TrimEnd('"'); + valueArgs.Add(current); + current = ""; + } + else + current += " " + rawValues[i]; + } + } + return [.. valueArgs]; + } } diff --git a/YesAlready/UI/Tabs/Custom.cs b/YesAlready/UI/Tabs/Custom.cs index e8134c5..5b0d54b 100644 --- a/YesAlready/UI/Tabs/Custom.cs +++ b/YesAlready/UI/Tabs/Custom.cs @@ -23,9 +23,9 @@ public static void Draw() DrawButtons(); - foreach (var bother in P.Config.CustomBothers.ToList()) + foreach (var bother in P.Config.CustomCallbacks.ToList()) { - using var id = ImRaii.PushId(P.Config.CustomBothers.IndexOf(bother)); + using var id = ImRaii.PushId(P.Config.CustomCallbacks.IndexOf(bother)); var name = bother.Addon; if (ImGui.InputText("Addon Name", ref name, 50, ImGuiInputTextFlags.EnterReturnsTrue)) { @@ -34,18 +34,18 @@ public static void Draw() ToggleCustomBothers(); } - var args = string.Join(" ", bother.CallbackParams); + var args = bother.CallbackParams; if (ImGui.InputText("Parameters", ref args, 150, ImGuiInputTextFlags.EnterReturnsTrue)) { - bother.CallbackParams = ParseArgs(args); + bother.CallbackParams = args; P.Config.Save(); ToggleCustomBothers(); } ImGui.SameLine(); - if (ImGuiEx.IconButton(FontAwesomeIcon.Trash, "Remove Entry", id: $"Delete##{P.Config.CustomBothers.IndexOf(bother)}")) + if (ImGuiEx.IconButton(FontAwesomeIcon.Trash, "Remove Entry", id: $"Delete##{P.Config.CustomCallbacks.IndexOf(bother)}")) { - P.Config.CustomBothers.Remove(bother); + P.Config.CustomCallbacks.Remove(bother); P.Config.Save(); ToggleCustomBothers(); } @@ -60,10 +60,10 @@ public static void DrawButtons() if (ImGuiEx.IconButton(FontAwesomeIcon.Plus, "Add new entry")) { - P.Config.CustomBothers.Add(new CustomBother + P.Config.CustomCallbacks.Add(new CustomBother { Addon = "AddonName", - CallbackParams = [-1] + CallbackParams = "-1" }); P.Config.Save(); } @@ -101,45 +101,4 @@ private static void ToggleCustomBothers() } } } - - private static object[] ParseArgs(string args) - { - var rawValues = args.Split(' '); - var valueArgs = new List(); - - var current = ""; - var inQuotes = false; - - for (var i = 0; i < rawValues.Length; i++) - { - if (!inQuotes) - { - if (rawValues[i].StartsWith('\"')) - { - inQuotes = true; - current = rawValues[i].TrimStart('"'); - } - else - { - if (int.TryParse(rawValues[i], out var iValue)) valueArgs.Add(iValue); - else if (uint.TryParse(rawValues[i].TrimEnd('U', 'u'), out var uValue)) valueArgs.Add(uValue); - else if (bool.TryParse(rawValues[i], out var bValue)) valueArgs.Add(bValue); - else valueArgs.Add(rawValues[i]); - } - } - else - { - if (rawValues[i].EndsWith('\"')) - { - inQuotes = false; - current += " " + rawValues[i].TrimEnd('"'); - valueArgs.Add(current); - current = ""; - } - else - current += " " + rawValues[i]; - } - } - return [.. valueArgs]; - } }