Skip to content

Commit

Permalink
New subcommands to check scripts and clear action queues
Browse files Browse the repository at this point in the history
In the event that a script is in an infinite queue loop (queueing a function that queues itself), you can now forcibly clear the action queue for a single script (by informal slug) or for ALL scripts.
  • Loading branch information
PrincessRTFM committed Feb 1, 2024
1 parent d8f136b commit 7e2d0f1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 11 deletions.
16 changes: 16 additions & 0 deletions WoLua/Lua/ScriptContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ public ScriptContainer(string file, string name, string slug) {
public ScriptContainer(string file) : this(file, new DirectoryInfo(Path.GetDirectoryName(file)!).Name) { }
public ScriptContainer(string file, string name) : this(file, name, NameToSlug(name)) { }

internal bool ReportError() {
if (!this.LoadSuccess) {
Service.Plugin.Error($"\"{this.PrettyName}\" ({this.InternalName}) failed to load due to an error.");
return true;
}
if (!this.Ready) {
Service.Plugin.Error($"\"{this.PrettyName}\" ({this.InternalName}) did not register a callback function.");
return true;
}
if (this.ErrorOnCall) {
Service.Plugin.Error($"\"{this.PrettyName}\" ({this.InternalName}) encountered an error during a previous call and has been disabled.");
return true;
}
return false;
}

public bool SetCallback(DynValue func) {
if (this.Disposed)
return false;
Expand Down
71 changes: 60 additions & 11 deletions WoLua/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;

using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
Expand Down Expand Up @@ -133,7 +134,7 @@ public void OnCommand(string command, string argline) {
case "invoke":
case "run":
if (args.Length < 2) {
this.Error("Invalid usage. You must pass a command name, optionally followed by any parameters.");
this.Error("Invalid usage. You must pass a script name, optionally followed by any parameters.");
}
else {
string name = args[1];
Expand All @@ -147,6 +148,46 @@ public void OnCommand(string command, string argline) {
this.Invoke(name, parameters);
}
break;
case "info":
if (args.Length < 2) {
this.Error("Invalid usage. You must pass a script name.");
}
else if (this.FindScriptByInformalSlug(args[1], out ScriptContainer? script) && !script.ReportError()) {
this.Print($"\"{script.PrettyName}\" ({script.InternalName}) has {script.ActionQueue.Count} queued action{(script.ActionQueue.Count == 1 ? "" : "s")}");
}
else {
this.Error($"No script could be found with the informal identifier {args[1]}.");
}
break;
case "halt":
case "stop":
case "clear":
if (args.Length < 2) {
this.Error("Invalid usage. You must pass a script name.");
}
else if (this.FindScriptByInformalSlug(args[1], out ScriptContainer? script) && !script.ReportError()) {
int had = script.ActionQueue.Count;
script.ActionQueue.Clear();
this.Print($"Cleared {had} action{(had == 1 ? "" : "s")} from {script.PrettyName}'s queue.");
}
else {
this.Error($"No script could be found with the informal identifier {args[1]}.");
}
break;
case "halt-all":
case "stop-all":
case "clear-all":
case "haltall":
case "stopall":
case "clearall":
foreach (ScriptContainer script in Service.Scripts.Values) {
if (!script.ReportError()) {
int had = script.ActionQueue.Count;
script.ActionQueue.Clear();
this.Print($"Cleared {had} action{(had == 1 ? "" : "s")} from {script.PrettyName}'s queue.");
}
}
break;
case "commands":
case "list":
case "ls": // bit of an easter egg for programmers, I guess
Expand Down Expand Up @@ -230,23 +271,31 @@ public void OnCommand(string command, string argline) {
}
}

public void Invoke(string name, string parameters) {
internal bool FindScriptByInformalSlug(string identifier, [NotNullWhen(true)] out ScriptContainer? script) {
script = null;
if (this.disposed)
return;
return false;

string[] tries = new string[] {
name,
name.ToLower(),
name.ToUpper(),
name.ToLowerInvariant(),
name.ToUpperInvariant(),
identifier,
identifier.ToLower(),
identifier.ToUpper(),
identifier.ToLowerInvariant(),
identifier.ToUpperInvariant(),
};
ScriptContainer? cmd = null;
foreach (string attempt in tries) {
if (Service.Scripts.TryGetValue(attempt, out cmd))
if (Service.Scripts.TryGetValue(attempt, out script))
break;
}
if (cmd is not null) {

return script is not null;
}

public void Invoke(string name, string parameters) {
if (this.disposed)
return;

if (this.FindScriptByInformalSlug(name, out ScriptContainer? cmd)) {
cmd.Invoke(parameters);
}
else {
Expand Down

0 comments on commit 7e2d0f1

Please sign in to comment.