mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 10:17:22 +01:00
Merge pull request #366 from daemitus/StyleCop-mini-5
This commit is contained in:
commit
e412772697
6 changed files with 147 additions and 75 deletions
|
|
@ -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
|
||||
/// <summary>
|
||||
/// This class manages the execution of <see cref="ScratchpadDocument"/> classes.
|
||||
/// </summary>
|
||||
internal class ScratchExecutionManager
|
||||
{
|
||||
private readonly Dalamud dalamud;
|
||||
private Dictionary<Guid, IDalamudPlugin> loadedScratches = new Dictionary<Guid, IDalamudPlugin>();
|
||||
|
||||
public ScratchMacroProcessor MacroProcessor { get; private set; } = new ScratchMacroProcessor();
|
||||
private Dictionary<Guid, IDalamudPlugin> loadedScratches = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScratchExecutionManager"/> class.
|
||||
/// </summary>
|
||||
/// <param name="dalamud">The Dalamud instance.</param>
|
||||
public ScratchExecutionManager(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()
|
||||
{
|
||||
foreach (var dalamudPlugin in this.loadedScratches)
|
||||
|
|
@ -37,6 +46,11 @@ namespace Dalamud.Interface.Scratchpad
|
|||
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)
|
||||
{
|
||||
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")
|
||||
|
|
|
|||
|
|
@ -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
|
||||
/// <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>();
|
||||
|
||||
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)
|
||||
{
|
||||
TrackedScratches.Add(new ScratchpadDocument
|
||||
this.TrackedScratches.Add(new ScratchpadDocument
|
||||
{
|
||||
Title = Path.GetFileName(path),
|
||||
Content = File.ReadAllText(path),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
/// <summary>
|
||||
/// The load status of a <see cref="ScratchpadDocument"/> class.
|
||||
/// </summary>
|
||||
internal enum ScratchLoadStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Unknown.
|
||||
/// </summary>
|
||||
Unknown,
|
||||
|
||||
/// <summary>
|
||||
/// Failure to compile.
|
||||
/// </summary>
|
||||
FailureCompile,
|
||||
|
||||
/// <summary>
|
||||
/// Failure to initialize.
|
||||
/// </summary>
|
||||
FailureInit,
|
||||
|
||||
/// <summary>
|
||||
/// Success.
|
||||
/// </summary>
|
||||
Success,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
/// <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 {
|
||||
|
||||
|
|
@ -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; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process the given macro input and return a script.
|
||||
/// </summary>
|
||||
/// <param name="input">Input to process.</param>
|
||||
/// <returns>A runnable script.</returns>
|
||||
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}Delegate> hook{i}Inst;\n";
|
||||
|
||||
hookInit += $"var addrH{i} = pi.TargetModuleScanner.ScanText(\"{hook.Sig}\");\n";
|
||||
hookInit +=
|
||||
$"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 = new Hook<Hook{i}Delegate>(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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
/// <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 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";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the document has unsaved content.
|
||||
/// </summary>
|
||||
public bool HasUnsaved { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the document is open.
|
||||
/// </summary>
|
||||
public bool IsOpen { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the load status of the document.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
/// <summary>
|
||||
/// This class facilitates interacting with the ScratchPad window.
|
||||
/// </summary>
|
||||
internal class ScratchpadWindow : Window, IDisposable
|
||||
{
|
||||
private readonly Dalamud dalamud;
|
||||
|
||||
public ScratchExecutionManager Execution { get; private set; }
|
||||
|
||||
private List<ScratchpadDocument> documents = new List<ScratchpadDocument>();
|
||||
|
||||
private ScratchFileWatcher watcher = new ScratchFileWatcher();
|
||||
|
||||
private List<ScratchpadDocument> documents = new();
|
||||
private ScratchFileWatcher watcher = new();
|
||||
private string pathInput = string.Empty;
|
||||
|
||||
public ScratchpadWindow(Dalamud dalamud) :
|
||||
base("Plugin Scratchpad", ImGuiWindowFlags.MenuBar)
|
||||
/// <summary>
|
||||
/// 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.documents.Add(new ScratchpadDocument());
|
||||
|
|
@ -36,6 +35,12 @@ namespace Dalamud.Interface.Scratchpad
|
|||
this.Execution = new ScratchExecutionManager(dalamud);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ScratchPad execution manager.
|
||||
/// </summary>
|
||||
public ScratchExecutionManager Execution { get; private set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
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
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose of managed and unmanaged resources.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
this.Execution.DisposeAllScratches();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue