DalamudPython/DalamudPython/Commands.cs

102 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
namespace DalamudPython {
public class Commands {
public static readonly Dictionary<string, string> All = new() {
["/python"] = "Run a line of Python",
["/py"] = "Alias for /python",
["/pyprint"] = "Run a line of Python and print the result to the chat window",
["/pyadd"] = "Add a line of Python to a temporary script",
["/pyexecute"] = "Run the temporary script",
["/pyreset"] = "Clear the temporary script",
["/pyload"] = "Read the contents of a file relative to the config folder and run them as a Python script",
};
private Plugin Plugin { get; }
private string? Script { get; set; }
public Commands(Plugin plugin) {
this.Plugin = plugin;
}
public void OnCommand(string name, string args) {
switch (name) {
case "/python":
case "/py":
case "/pyprint":
this.OneLiner(name, args);
break;
case "/pyadd":
this.Add(args);
break;
case "/pyexecute":
this.RunScript(args);
break;
case "/pyreset":
this.Script = null;
break;
case "/pyload":
this.LoadFile(args);
break;
}
}
private void Add(string args) {
this.Script ??= "";
this.Script += args + "\n";
}
private void RunScript(string args) {
var script = this.Script;
if (script == null) {
return;
}
var print = args == "print";
try {
this.Execute(script, print);
} finally {
this.Script = null;
}
}
private void Execute(string script, bool print) {
var scope = this.Plugin.Engine.CreateScope();
scope.SetVariable("interface", this.Plugin.Interface);
var fullScript = $@"import clr
from Dalamud import *
from Dalamud.Plugin import *
from Lumina import *
from Lumina.Excel.GeneratedSheets import *
{script}";
var result = this.Plugin.Engine.Execute(fullScript, scope);
if (!print) {
return;
}
this.Plugin.Interface.Framework.Gui.Chat.Print(result.ToString());
}
private void OneLiner(string name, string args) {
var print = name == "/pyprint";
this.Execute(args, print);
}
private void LoadFile(string args) {
if (string.IsNullOrWhiteSpace(args)) {
return;
}
var scriptPath = Path.Combine(this.Plugin.ConfigDirectory, args);
var script = File.ReadAllText(scriptPath);
this.Execute(script, false);
}
}
}