StyleCop: Scratchpad

This commit is contained in:
Raymond Lynch 2021-05-29 21:10:45 -04:00
parent 97d8fa855a
commit bf9855b293
7 changed files with 148 additions and 76 deletions

View file

@ -2,7 +2,7 @@
<PropertyGroup Label="Target"> <PropertyGroup Label="Target">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<TargetFramework>net472</TargetFramework> <TargetFramework>net472</TargetFramework>
<LangVersion>8.0</LangVersion> <LangVersion>9.0</LangVersion>
<Platforms>AnyCPU;x64</Platforms> <Platforms>AnyCPU;x64</Platforms>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="Build"> <PropertyGroup Label="Build">

View file

@ -1,32 +1,41 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dalamud.Configuration;
using Dalamud.Plugin; using Dalamud.Plugin;
using ImGuiNET; using ImGuiNET;
using Lumina.Excel.GeneratedSheets; using Lumina.Excel.GeneratedSheets;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting; using Microsoft.CodeAnalysis.Scripting;
using Serilog; using Serilog;
namespace Dalamud.Interface.Scratchpad namespace Dalamud.Interface.Scratchpad
{ {
class ScratchExecutionManager /// <summary>
/// This class manages the execution of <see cref="ScratchpadDocument"/> classes.
/// </summary>
internal class ScratchExecutionManager
{ {
private readonly Dalamud dalamud; private readonly Dalamud dalamud;
private Dictionary<Guid, IDalamudPlugin> loadedScratches = new Dictionary<Guid, IDalamudPlugin>(); private Dictionary<Guid, IDalamudPlugin> loadedScratches = new();
public ScratchMacroProcessor MacroProcessor { get; private set; } = new ScratchMacroProcessor();
/// <summary>
/// Initializes a new instance of the <see cref="ScratchExecutionManager"/> class.
/// </summary>
/// <param name="dalamud">The Dalamud instance.</param>
public ScratchExecutionManager(Dalamud dalamud) public ScratchExecutionManager(Dalamud dalamud)
{ {
this.dalamud = dalamud; this.dalamud = dalamud;
} }
/// <summary>
/// Gets the ScratchPad macro processor.
/// </summary>
public ScratchMacroProcessor MacroProcessor { get; private set; } = new();
/// <summary>
/// Dispose of all currently loaded ScratchPads.
/// </summary>
public void DisposeAllScratches() public void DisposeAllScratches()
{ {
foreach (var dalamudPlugin in this.loadedScratches) foreach (var dalamudPlugin in this.loadedScratches)
@ -37,6 +46,11 @@ namespace Dalamud.Interface.Scratchpad
this.loadedScratches.Clear(); this.loadedScratches.Clear();
} }
/// <summary>
/// Renew a given ScratchPadDocument.
/// </summary>
/// <param name="doc">The document to renew.</param>
/// <returns>The new load status.</returns>
public ScratchLoadStatus RenewScratch(ScratchpadDocument doc) public ScratchLoadStatus RenewScratch(ScratchpadDocument doc)
{ {
var existingScratch = this.loadedScratches.FirstOrDefault(x => x.Key == doc.Id); var existingScratch = this.loadedScratches.FirstOrDefault(x => x.Key == doc.Id);
@ -51,8 +65,7 @@ namespace Dalamud.Interface.Scratchpad
var options = ScriptOptions.Default var options = ScriptOptions.Default
.AddReferences(typeof(ImGui).Assembly) .AddReferences(typeof(ImGui).Assembly)
.AddReferences(typeof(Dalamud).Assembly) .AddReferences(typeof(Dalamud).Assembly)
.AddReferences(typeof(FFXIVClientStructs.Attributes.Addon) .AddReferences(typeof(FFXIVClientStructs.Attributes.Addon).Assembly) // FFXIVClientStructs
.Assembly) // FFXIVClientStructs
.AddReferences(typeof(Lumina.GameData).Assembly) // Lumina .AddReferences(typeof(Lumina.GameData).Assembly) // Lumina
.AddReferences(typeof(TerritoryType).Assembly) // Lumina.Excel .AddReferences(typeof(TerritoryType).Assembly) // Lumina.Excel
// .WithReferences(MetadataReference.CreateFromFile(typeof(ScratchExecutionManager).Assembly.Location)) // .WithReferences(MetadataReference.CreateFromFile(typeof(ScratchExecutionManager).Assembly.Location))

View file

@ -1,21 +1,27 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dalamud.Interface.Scratchpad namespace Dalamud.Interface.Scratchpad
{ {
class ScratchFileWatcher /// <summary>
/// A file watcher for <see cref="ScratchpadDocument"/> classes.
/// </summary>
internal class ScratchFileWatcher
{ {
private FileSystemWatcher watcher = new();
/// <summary>
/// Gets or sets the list of tracked ScratchPad documents.
/// </summary>
public List<ScratchpadDocument> TrackedScratches { get; set; } = new List<ScratchpadDocument>(); public List<ScratchpadDocument> TrackedScratches { get; set; } = new List<ScratchpadDocument>();
private FileSystemWatcher watcher = new FileSystemWatcher(); /// <summary>
/// Load a new ScratchPadDocument from disk.
/// </summary>
/// <param name="path">The filepath to load.</param>
public void Load(string path) public void Load(string path)
{ {
TrackedScratches.Add(new ScratchpadDocument this.TrackedScratches.Add(new ScratchpadDocument
{ {
Title = Path.GetFileName(path), Title = Path.GetFileName(path),
Content = File.ReadAllText(path), Content = File.ReadAllText(path),

View file

@ -1,16 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dalamud.Interface.Scratchpad namespace Dalamud.Interface.Scratchpad
{ {
enum ScratchLoadStatus /// <summary>
/// The load status of a <see cref="ScratchpadDocument"/> class.
/// </summary>
internal enum ScratchLoadStatus
{ {
/// <summary>
/// Unknown.
/// </summary>
Unknown, Unknown,
/// <summary>
/// Failure to compile.
/// </summary>
FailureCompile, FailureCompile,
/// <summary>
/// Failure to initialize.
/// </summary>
FailureInit, FailureInit,
/// <summary>
/// Success.
/// </summary>
Success, Success,
} }
} }

View file

@ -1,17 +1,18 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Dalamud.Plugin; using Dalamud.Plugin;
namespace Dalamud.Interface.Scratchpad namespace Dalamud.Interface.Scratchpad
{ {
class ScratchMacroProcessor /// <summary>
/// This class converts ScratchPad macros into runnable scripts.
/// </summary>
internal class ScratchMacroProcessor
{ {
private const string template = @" private const string Template = @"
public class ScratchPlugin : IDalamudPlugin { public class ScratchPlugin : IDalamudPlugin {
@ -53,19 +54,11 @@ public class ScratchPlugin : IDalamudPlugin {
Dispose, Dispose,
} }
private class HookInfo /// <summary>
{ /// Process the given macro input and return a script.
public string Body { get; set; } /// </summary>
/// <param name="input">Input to process.</param>
public string Arguments { get; set; } /// <returns>A runnable script.</returns>
public string Invocation { get; set; }
public string RetType { get; set; }
public string Sig { get; set; }
}
public string Process(string input) public string Process(string input)
{ {
var lines = input.Split(new[] { '\r', '\n' }); var lines = input.Split(new[] { '\r', '\n' });
@ -181,10 +174,8 @@ public class ScratchPlugin : IDalamudPlugin {
$"private Hook<Hook{i}Delegate> hook{i}Inst;\n"; $"private Hook<Hook{i}Delegate> hook{i}Inst;\n";
hookInit += $"var addrH{i} = pi.TargetModuleScanner.ScanText(\"{hook.Sig}\");\n"; hookInit += $"var addrH{i} = pi.TargetModuleScanner.ScanText(\"{hook.Sig}\");\n";
hookInit += hookInit += $"this.hook{i}Inst = new Hook<Hook{i}Delegate>(addrH{i}, new Hook{i}Delegate(Hook{i}Detour), this);\n";
$"this.hook{i}Inst = new Hook<Hook{i}Delegate>(addrH{i}, new Hook{i}Delegate(Hook{i}Detour), this);\n"; hookInit += $"this.hook{i}Inst.Enable();\n";
hookInit +=
$"this.hook{i}Inst.Enable();\n";
var originalCall = $"this.hook{i}Inst.Original({hook.Invocation});\n"; var originalCall = $"this.hook{i}Inst.Original({hook.Invocation});\n";
if (hook.RetType != "void") if (hook.RetType != "void")
@ -225,7 +216,7 @@ public class ScratchPlugin : IDalamudPlugin {
noneBody += "\n" + hookDetour; noneBody += "\n" + hookDetour;
disposeBody += "\n" + hookDispose; disposeBody += "\n" + hookDispose;
var output = template; var output = Template;
output = output.Replace("{SETUPBODY}", setupBody); output = output.Replace("{SETUPBODY}", setupBody);
output = output.Replace("{INITBODY}", initBody); output = output.Replace("{INITBODY}", initBody);
output = output.Replace("{DRAWBODY}", drawBody); output = output.Replace("{DRAWBODY}", drawBody);
@ -234,5 +225,18 @@ public class ScratchPlugin : IDalamudPlugin {
return output; 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; }
}
} }
} }

View file

@ -1,25 +1,45 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dalamud.Interface.Scratchpad namespace Dalamud.Interface.Scratchpad
{ {
class ScratchpadDocument /// <summary>
/// This class represents a single document in the ScratchPad.
/// </summary>
internal class ScratchpadDocument
{ {
/// <summary>
/// Gets or sets the guid ID of the document.
/// </summary>
public Guid Id { get; set; } = Guid.NewGuid(); public Guid Id { get; set; } = Guid.NewGuid();
public string Content = "INITIALIZE:\n\tPluginLog.Information(\"Loaded!\");\nEND;\n\nDISPOSE:\n\tPluginLog.Information(\"Disposed!\");\nEND;\n"; /// <summary>
/// Gets or sets the document content.
/// </summary>
public string Content { get; set; } = "INITIALIZE:\n\tPluginLog.Information(\"Loaded!\");\nEND;\n\nDISPOSE:\n\tPluginLog.Information(\"Disposed!\");\nEND;\n";
/// <summary>
/// Gets or sets the document title.
/// </summary>
public string Title { get; set; } = "New Document"; public string Title { get; set; } = "New Document";
/// <summary>
/// Gets or sets a value indicating whether the document has unsaved content.
/// </summary>
public bool HasUnsaved { get; set; } public bool HasUnsaved { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the document is open.
/// </summary>
public bool IsOpen { get; set; } public bool IsOpen { get; set; }
/// <summary>
/// Gets or sets the load status of the document.
/// </summary>
public ScratchLoadStatus Status { get; set; } public ScratchLoadStatus Status { get; set; }
public bool IsMacro = true; /// <summary>
/// Gets or sets a value indicating whether this document is a macro.
/// </summary>
public bool IsMacro { get; set; } = true;
} }
} }

View file

@ -2,31 +2,30 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Dalamud.Interface.Colors; using Dalamud.Interface.Colors;
using Dalamud.Interface.Windowing; using Dalamud.Interface.Windowing;
using Dalamud.Plugin;
using ImGuiNET; using ImGuiNET;
using Serilog; using Serilog;
namespace Dalamud.Interface.Scratchpad namespace Dalamud.Interface.Scratchpad
{ {
class ScratchpadWindow : Window, IDisposable /// <summary>
/// This class facilitates interacting with the ScratchPad window.
/// </summary>
internal class ScratchpadWindow : Window, IDisposable
{ {
private readonly Dalamud dalamud; private readonly Dalamud dalamud;
private List<ScratchpadDocument> documents = new();
public ScratchExecutionManager Execution { get; private set; } private ScratchFileWatcher watcher = new();
private List<ScratchpadDocument> documents = new List<ScratchpadDocument>();
private ScratchFileWatcher watcher = new ScratchFileWatcher();
private string pathInput = string.Empty; private string pathInput = string.Empty;
public ScratchpadWindow(Dalamud dalamud) : /// <summary>
base("Plugin Scratchpad", ImGuiWindowFlags.MenuBar) /// Initializes a new instance of the <see cref="ScratchpadWindow"/> class.
/// </summary>
/// <param name="dalamud">The Dalamud instance.</param>
public ScratchpadWindow(Dalamud dalamud)
: base("Plugin Scratchpad", ImGuiWindowFlags.MenuBar)
{ {
this.dalamud = dalamud; this.dalamud = dalamud;
this.documents.Add(new ScratchpadDocument()); this.documents.Add(new ScratchpadDocument());
@ -36,6 +35,12 @@ namespace Dalamud.Interface.Scratchpad
this.Execution = new ScratchExecutionManager(dalamud); this.Execution = new ScratchExecutionManager(dalamud);
} }
/// <summary>
/// Gets the ScratchPad execution manager.
/// </summary>
public ScratchExecutionManager Execution { get; private set; }
/// <inheritdoc/>
public override void Draw() public override void Draw()
{ {
if (ImGui.BeginPopupModal("Choose Path")) if (ImGui.BeginPopupModal("Choose Path"))
@ -53,7 +58,11 @@ namespace Dalamud.Interface.Scratchpad
ImGui.SetItemDefaultFocus(); ImGui.SetItemDefaultFocus();
ImGui.SameLine(); ImGui.SameLine();
if (ImGui.Button("Cancel", new Vector2(120, 0))) { ImGui.CloseCurrentPopup(); } if (ImGui.Button("Cancel", new Vector2(120, 0)))
{
ImGui.CloseCurrentPopup();
}
ImGui.EndPopup(); 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.BeginTabItem(docs[i].Title + (docs[i].HasUnsaved ? "*" : string.Empty) + "###ScratchItem" + i, ref isOpen))
{ {
if (ImGui.InputTextMultiline("###ScratchInput" + i, ref docs[i].Content, 20000, var content = docs[i].Content;
new Vector2(-1, -34), ImGuiInputTextFlags.AllowTabInput)) if (ImGui.InputTextMultiline("###ScratchInput" + i, ref content, 20000, new Vector2(-1, -34), ImGuiInputTextFlags.AllowTabInput))
{ {
docs[i].Content = content;
docs[i].HasUnsaved = true; docs[i].HasUnsaved = true;
} }
@ -133,7 +143,11 @@ namespace Dalamud.Interface.Scratchpad
ImGui.SameLine(); 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(); ImGui.SameLine();
@ -167,6 +181,9 @@ namespace Dalamud.Interface.Scratchpad
} }
} }
/// <summary>
/// Dispose of managed and unmanaged resources.
/// </summary>
public void Dispose() public void Dispose()
{ {
this.Execution.DisposeAllScratches(); this.Execution.DisposeAllScratches();