Skip to content

Commit

Permalink
make a new config cause I cbf
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaksuhn committed Oct 8, 2024
1 parent a999056 commit 0bd1640
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 57 deletions.
11 changes: 5 additions & 6 deletions YesAlready/Configuration.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -69,13 +68,13 @@ public partial class Configuration() : IPluginConfiguration
public bool MiragePrismRemoveDispel { get; set; } = false;
public bool MiragePrismExecuteCast { get; set; } = false;

public List<CustomBother> CustomBothers { get; set; } = [];
public List<CustomBother> 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
Expand Down
65 changes: 63 additions & 2 deletions YesAlready/Features/CustomAddonCallbacks.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

Expand All @@ -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<object>();

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];
}
}
57 changes: 8 additions & 49 deletions YesAlready/UI/Tabs/Custom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -101,45 +101,4 @@ private static void ToggleCustomBothers()
}
}
}

private static object[] ParseArgs(string args)
{
var rawValues = args.Split(' ');
var valueArgs = new List<object>();

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];
}
}

0 comments on commit 0bd1640

Please sign in to comment.