diff --git a/Dalamud/Interface/Scratchpad/ScratchExecutionManager.cs b/Dalamud/Interface/Scratchpad/ScratchExecutionManager.cs index 3110fed5a..07010bc84 100644 --- a/Dalamud/Interface/Scratchpad/ScratchExecutionManager.cs +++ b/Dalamud/Interface/Scratchpad/ScratchExecutionManager.cs @@ -1,32 +1,41 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Dalamud.Configuration; + using Dalamud.Plugin; using ImGuiNET; using Lumina.Excel.GeneratedSheets; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.Scripting; using Serilog; namespace Dalamud.Interface.Scratchpad { - class ScratchExecutionManager + /// + /// This class manages the execution of classes. + /// + internal class ScratchExecutionManager { private readonly Dalamud dalamud; - private Dictionary loadedScratches = new Dictionary(); - - public ScratchMacroProcessor MacroProcessor { get; private set; } = new ScratchMacroProcessor(); + private Dictionary loadedScratches = new(); + /// + /// Initializes a new instance of the class. + /// + /// The Dalamud instance. public ScratchExecutionManager(Dalamud dalamud) { this.dalamud = dalamud; } + /// + /// Gets the ScratchPad macro processor. + /// + public ScratchMacroProcessor MacroProcessor { get; private set; } = new(); + + /// + /// Dispose of all currently loaded ScratchPads. + /// public void DisposeAllScratches() { foreach (var dalamudPlugin in this.loadedScratches) @@ -37,6 +46,11 @@ namespace Dalamud.Interface.Scratchpad this.loadedScratches.Clear(); } + /// + /// Renew a given ScratchPadDocument. + /// + /// The document to renew. + /// The new load status. public ScratchLoadStatus RenewScratch(ScratchpadDocument doc) { var existingScratch = this.loadedScratches.FirstOrDefault(x => x.Key == doc.Id); @@ -51,11 +65,10 @@ namespace Dalamud.Interface.Scratchpad var options = ScriptOptions.Default .AddReferences(typeof(ImGui).Assembly) .AddReferences(typeof(Dalamud).Assembly) - .AddReferences(typeof(FFXIVClientStructs.Attributes.Addon) - .Assembly) // FFXIVClientStructs + .AddReferences(typeof(FFXIVClientStructs.Attributes.Addon).Assembly) // FFXIVClientStructs .AddReferences(typeof(Lumina.GameData).Assembly) // Lumina .AddReferences(typeof(TerritoryType).Assembly) // Lumina.Excel - //.WithReferences(MetadataReference.CreateFromFile(typeof(ScratchExecutionManager).Assembly.Location)) + // .WithReferences(MetadataReference.CreateFromFile(typeof(ScratchExecutionManager).Assembly.Location)) .AddImports("System") .AddImports("System.IO") .AddImports("System.Reflection") diff --git a/Dalamud/Interface/Scratchpad/ScratchFileWatcher.cs b/Dalamud/Interface/Scratchpad/ScratchFileWatcher.cs index 65ecf49e0..805ce11fe 100644 --- a/Dalamud/Interface/Scratchpad/ScratchFileWatcher.cs +++ b/Dalamud/Interface/Scratchpad/ScratchFileWatcher.cs @@ -1,21 +1,27 @@ -using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Dalamud.Interface.Scratchpad { - class ScratchFileWatcher + /// + /// A file watcher for classes. + /// + internal class ScratchFileWatcher { + private FileSystemWatcher watcher = new(); + + /// + /// Gets or sets the list of tracked ScratchPad documents. + /// public List TrackedScratches { get; set; } = new List(); - private FileSystemWatcher watcher = new FileSystemWatcher(); - + /// + /// Load a new ScratchPadDocument from disk. + /// + /// The filepath to load. public void Load(string path) { - TrackedScratches.Add(new ScratchpadDocument + this.TrackedScratches.Add(new ScratchpadDocument { Title = Path.GetFileName(path), Content = File.ReadAllText(path), diff --git a/Dalamud/Interface/Scratchpad/ScratchLoadStatus.cs b/Dalamud/Interface/Scratchpad/ScratchLoadStatus.cs index 1baf6d280..b9398c0f5 100644 --- a/Dalamud/Interface/Scratchpad/ScratchLoadStatus.cs +++ b/Dalamud/Interface/Scratchpad/ScratchLoadStatus.cs @@ -1,16 +1,28 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace Dalamud.Interface.Scratchpad { - enum ScratchLoadStatus + /// + /// The load status of a class. + /// + internal enum ScratchLoadStatus { + /// + /// Unknown. + /// Unknown, + + /// + /// Failure to compile. + /// FailureCompile, + + /// + /// Failure to initialize. + /// FailureInit, + + /// + /// Success. + /// Success, } } diff --git a/Dalamud/Interface/Scratchpad/ScratchMacroProcessor.cs b/Dalamud/Interface/Scratchpad/ScratchMacroProcessor.cs index 6265f20b7..f47c2cab2 100644 --- a/Dalamud/Interface/Scratchpad/ScratchMacroProcessor.cs +++ b/Dalamud/Interface/Scratchpad/ScratchMacroProcessor.cs @@ -1,17 +1,18 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Runtime.InteropServices; -using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; + using Dalamud.Plugin; namespace Dalamud.Interface.Scratchpad { - class ScratchMacroProcessor + /// + /// This class converts ScratchPad macros into runnable scripts. + /// + internal class ScratchMacroProcessor { - private const string template = @" + private const string Template = @" public class ScratchPlugin : IDalamudPlugin { @@ -53,22 +54,14 @@ public class ScratchPlugin : IDalamudPlugin { Dispose, } - private class HookInfo - { - public string Body { get; set; } - - public string Arguments { get; set; } - - public string Invocation { get; set; } - - public string RetType { get; set; } - - public string Sig { get; set; } - } - + /// + /// Process the given macro input and return a script. + /// + /// Input to process. + /// A runnable script. public string Process(string input) { - var lines = input.Split(new[] {'\r', '\n'}); + var lines = input.Split(new[] { '\r', '\n' }); var ctx = ParseContext.None; @@ -181,10 +174,8 @@ public class ScratchPlugin : IDalamudPlugin { $"private Hook hook{i}Inst;\n"; hookInit += $"var addrH{i} = pi.TargetModuleScanner.ScanText(\"{hook.Sig}\");\n"; - hookInit += - $"this.hook{i}Inst = new Hook(addrH{i}, new Hook{i}Delegate(Hook{i}Detour), this);\n"; - hookInit += - $"this.hook{i}Inst.Enable();\n"; + hookInit += $"this.hook{i}Inst = new Hook(addrH{i}, new Hook{i}Delegate(Hook{i}Detour), this);\n"; + hookInit += $"this.hook{i}Inst.Enable();\n"; var originalCall = $"this.hook{i}Inst.Original({hook.Invocation});\n"; if (hook.RetType != "void") @@ -225,7 +216,7 @@ public class ScratchPlugin : IDalamudPlugin { noneBody += "\n" + hookDetour; disposeBody += "\n" + hookDispose; - var output = template; + var output = Template; output = output.Replace("{SETUPBODY}", setupBody); output = output.Replace("{INITBODY}", initBody); output = output.Replace("{DRAWBODY}", drawBody); @@ -234,5 +225,18 @@ public class ScratchPlugin : IDalamudPlugin { return output; } + + private class HookInfo + { + public string Body { get; set; } + + public string Arguments { get; set; } + + public string Invocation { get; set; } + + public string RetType { get; set; } + + public string Sig { get; set; } + } } } diff --git a/Dalamud/Interface/Scratchpad/ScratchpadDocument.cs b/Dalamud/Interface/Scratchpad/ScratchpadDocument.cs index 07f66b63b..22c1e6b51 100644 --- a/Dalamud/Interface/Scratchpad/ScratchpadDocument.cs +++ b/Dalamud/Interface/Scratchpad/ScratchpadDocument.cs @@ -1,25 +1,45 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Dalamud.Interface.Scratchpad { - class ScratchpadDocument + /// + /// This class represents a single document in the ScratchPad. + /// + internal class ScratchpadDocument { + /// + /// Gets or sets the guid ID of the document. + /// public Guid Id { get; set; } = Guid.NewGuid(); - public string Content = "INITIALIZE:\n\tPluginLog.Information(\"Loaded!\");\nEND;\n\nDISPOSE:\n\tPluginLog.Information(\"Disposed!\");\nEND;\n"; + /// + /// Gets or sets the document content. + /// + public string Content { get; set; } = "INITIALIZE:\n\tPluginLog.Information(\"Loaded!\");\nEND;\n\nDISPOSE:\n\tPluginLog.Information(\"Disposed!\");\nEND;\n"; + /// + /// Gets or sets the document title. + /// public string Title { get; set; } = "New Document"; + /// + /// Gets or sets a value indicating whether the document has unsaved content. + /// public bool HasUnsaved { get; set; } + /// + /// Gets or sets a value indicating whether the document is open. + /// public bool IsOpen { get; set; } + /// + /// Gets or sets the load status of the document. + /// public ScratchLoadStatus Status { get; set; } - public bool IsMacro = true; + /// + /// Gets or sets a value indicating whether this document is a macro. + /// + public bool IsMacro { get; set; } = true; } } diff --git a/Dalamud/Interface/Scratchpad/ScratchpadWindow.cs b/Dalamud/Interface/Scratchpad/ScratchpadWindow.cs index 753eb9f87..91324117c 100644 --- a/Dalamud/Interface/Scratchpad/ScratchpadWindow.cs +++ b/Dalamud/Interface/Scratchpad/ScratchpadWindow.cs @@ -2,31 +2,30 @@ using System; using System.Collections.Generic; using System.Linq; using System.Numerics; -using System.Text; -using System.Threading.Tasks; using Dalamud.Interface.Colors; using Dalamud.Interface.Windowing; -using Dalamud.Plugin; using ImGuiNET; using Serilog; namespace Dalamud.Interface.Scratchpad { - class ScratchpadWindow : Window, IDisposable + /// + /// This class facilitates interacting with the ScratchPad window. + /// + internal class ScratchpadWindow : Window, IDisposable { private readonly Dalamud dalamud; - - public ScratchExecutionManager Execution { get; private set; } - - private List documents = new List(); - - private ScratchFileWatcher watcher = new ScratchFileWatcher(); - + private List documents = new(); + private ScratchFileWatcher watcher = new(); private string pathInput = string.Empty; - public ScratchpadWindow(Dalamud dalamud) : - base("Plugin Scratchpad", ImGuiWindowFlags.MenuBar) + /// + /// Initializes a new instance of the class. + /// + /// The Dalamud instance. + public ScratchpadWindow(Dalamud dalamud) + : base("Plugin Scratchpad", ImGuiWindowFlags.MenuBar) { this.dalamud = dalamud; this.documents.Add(new ScratchpadDocument()); @@ -36,6 +35,12 @@ namespace Dalamud.Interface.Scratchpad this.Execution = new ScratchExecutionManager(dalamud); } + /// + /// Gets the ScratchPad execution manager. + /// + public ScratchExecutionManager Execution { get; private set; } + + /// public override void Draw() { if (ImGui.BeginPopupModal("Choose Path")) @@ -53,7 +58,11 @@ namespace Dalamud.Interface.Scratchpad ImGui.SetItemDefaultFocus(); ImGui.SameLine(); - if (ImGui.Button("Cancel", new Vector2(120, 0))) { ImGui.CloseCurrentPopup(); } + if (ImGui.Button("Cancel", new Vector2(120, 0))) + { + ImGui.CloseCurrentPopup(); + } + ImGui.EndPopup(); } @@ -88,9 +97,10 @@ namespace Dalamud.Interface.Scratchpad if (ImGui.BeginTabItem(docs[i].Title + (docs[i].HasUnsaved ? "*" : string.Empty) + "###ScratchItem" + i, ref isOpen)) { - if (ImGui.InputTextMultiline("###ScratchInput" + i, ref docs[i].Content, 20000, - new Vector2(-1, -34), ImGuiInputTextFlags.AllowTabInput)) + var content = docs[i].Content; + if (ImGui.InputTextMultiline("###ScratchInput" + i, ref content, 20000, new Vector2(-1, -34), ImGuiInputTextFlags.AllowTabInput)) { + docs[i].Content = content; docs[i].HasUnsaved = true; } @@ -133,7 +143,11 @@ namespace Dalamud.Interface.Scratchpad ImGui.SameLine(); - ImGui.Checkbox("Use Macros", ref docs[i].IsMacro); + var isMacro = docs[i].IsMacro; + if (ImGui.Checkbox("Use Macros", ref isMacro)) + { + docs[i].IsMacro = isMacro; + } ImGui.SameLine(); @@ -167,6 +181,9 @@ namespace Dalamud.Interface.Scratchpad } } + /// + /// Dispose of managed and unmanaged resources. + /// public void Dispose() { this.Execution.DisposeAllScratches();