diff --git a/DalamudPython/Commands.cs b/DalamudPython/Commands.cs index 408ed75..e6cc81c 100644 --- a/DalamudPython/Commands.cs +++ b/DalamudPython/Commands.cs @@ -1,5 +1,9 @@ using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Reflection; +using Dalamud.IoC; +using Dalamud.Plugin; namespace DalamudPython { public class Commands { @@ -65,14 +69,22 @@ namespace DalamudPython { } private void Execute(string script, bool print) { + var services = typeof(IDalamudPlugin).Assembly.GetTypes() + .Where(t => t.GetCustomAttribute(typeof(PluginInterfaceAttribute)) != null) + .Where(t => t.Namespace != null) + .Select(t => $"from {t.Namespace!} import {t.Name}"); + var scope = this.Plugin.Engine.CreateScope(); scope.SetVariable("interface", this.Plugin.Interface); scope.SetVariable("store", this.Store); var fullScript = $@"import clr +from DalamudPython.Util import * from Dalamud import * from Dalamud.Plugin import * +{string.Join('\n', services)} from Lumina import * from Lumina.Excel.GeneratedSheets import * +### begin custom {script}"; var result = this.Plugin.Engine.Execute(fullScript, scope); @@ -80,7 +92,7 @@ from Lumina.Excel.GeneratedSheets import * return; } - this.Plugin.Interface.Framework.Gui.Chat.Print(result.ToString()); + this.Plugin.ChatGui.Print(result.ToString()); } private void OneLiner(string name, string args) { diff --git a/DalamudPython/DalamudPython.csproj b/DalamudPython/DalamudPython.csproj index c69ecb2..89e3d56 100755 --- a/DalamudPython/DalamudPython.csproj +++ b/DalamudPython/DalamudPython.csproj @@ -1,10 +1,12 @@ - net48 + net5-windows latest enable - 1.1.2 + 2.0.0 + false + true @@ -24,13 +26,11 @@ $(AppData)\XIVLauncher\addon\Hooks\dev\Lumina.Excel.dll False - - - - + + diff --git a/DalamudPython/FodyWeavers.xml b/DalamudPython/FodyWeavers.xml deleted file mode 100644 index f1dea8f..0000000 --- a/DalamudPython/FodyWeavers.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/DalamudPython/ILRepack.targets b/DalamudPython/ILRepack.targets deleted file mode 100644 index 272ed9a..0000000 --- a/DalamudPython/ILRepack.targets +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - diff --git a/DalamudPython/Plugin.cs b/DalamudPython/Plugin.cs index b6f056b..fddbbb6 100644 --- a/DalamudPython/Plugin.cs +++ b/DalamudPython/Plugin.cs @@ -2,6 +2,8 @@ using System.IO; using System.Reflection; using Dalamud.Game.Command; +using Dalamud.Game.Gui; +using Dalamud.IoC; using Dalamud.Plugin; using IronPython.Hosting; using Microsoft.Scripting.Hosting; @@ -10,11 +12,18 @@ namespace DalamudPython { public class Plugin : IDalamudPlugin { public string Name => "DalamudPython"; - public DalamudPluginInterface Interface { get; private set; } = null!; + [PluginService] + public DalamudPluginInterface Interface { get; private init; } = null!; + + [PluginService] + internal ChatGui ChatGui { get; private init; } = null!; + + [PluginService] + internal CommandManager CommandManager { get; private init; } = null!; public ScriptEngine Engine { get; } = Python.CreateEngine(); - private Commands Commands { get; set; } = null!; + private Commands Commands { get; } public string ConfigDirectory => Path.Combine(new[] { Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @@ -23,12 +32,11 @@ namespace DalamudPython { this.Name, }); - public void Initialize(DalamudPluginInterface pluginInterface) { - this.Interface = pluginInterface ?? throw new ArgumentNullException(nameof(pluginInterface)); - + public Plugin() { // set a global variable for the plugin interface this.Engine.Runtime.Globals.SetVariable("interface", this.Interface); // load Dalamud, Lumina, and ImGuiNET + this.Engine.Runtime.LoadAssembly(this.GetType().Assembly); this.Engine.Runtime.LoadAssembly(Assembly.GetAssembly(typeof(DalamudPluginInterface))); this.Engine.Runtime.LoadAssembly(Assembly.GetAssembly(typeof(Lumina.GameData))); this.Engine.Runtime.LoadAssembly(Assembly.GetAssembly(typeof(Lumina.Excel.ExcelRow))); @@ -37,9 +45,9 @@ namespace DalamudPython { this.Commands = new Commands(this); - foreach (var command in Commands.All) { - this.Interface.CommandManager.AddHandler(command.Key, new CommandInfo(this.Commands.OnCommand) { - HelpMessage = command.Value, + foreach (var (name, desc) in Commands.All) { + this.CommandManager.AddHandler(name, new CommandInfo(this.Commands.OnCommand) { + HelpMessage = desc, }); } @@ -48,7 +56,7 @@ namespace DalamudPython { public void Dispose() { foreach (var name in Commands.All.Keys) { - this.Interface.CommandManager.RemoveHandler(name); + this.CommandManager.RemoveHandler(name); } this.Engine.Runtime.Shutdown(); diff --git a/DalamudPython/Util.cs b/DalamudPython/Util.cs new file mode 100755 index 0000000..5f1465e --- /dev/null +++ b/DalamudPython/Util.cs @@ -0,0 +1,14 @@ +using System.Reflection; +using Dalamud.Logging; +using Dalamud.Plugin; + +namespace DalamudPython { + public static class Util { + public static T GetService() { + PluginLog.Log($"Requesting {typeof(T)}"); + var service = typeof(IDalamudPlugin).Assembly.GetType("Dalamud.Service`1")!.MakeGenericType(typeof(T)); + var get = service.GetMethod("Get", BindingFlags.Public | BindingFlags.Static)!; + return (T) get.Invoke(null, null)!; + } + } +}