refactor: handle events in their own classes

Also deprecate /prmt and replace with /nosol.
This commit is contained in:
Anna 2021-02-24 20:01:12 -05:00
parent 819ac1b457
commit 7a0614b4bb
3 changed files with 74 additions and 46 deletions

38
NoSoliciting/Commands.cs Executable file
View File

@ -0,0 +1,38 @@
using System;
using Dalamud.Game.Command;
namespace NoSoliciting {
public class Commands : IDisposable {
private Plugin Plugin { get; }
internal Commands(Plugin plugin) {
this.Plugin = plugin;
this.Plugin.Interface.CommandManager.AddHandler("/prmt", new CommandInfo(this.OnCommand) {
HelpMessage = "Opens the NoSoliciting configuration (deprecated)",
ShowInHelp = false,
});
this.Plugin.Interface.CommandManager.AddHandler("/nosol", new CommandInfo(this.OnCommand) {
HelpMessage = "Opens the NoSoliciting configuration",
});
}
public void Dispose() {
this.Plugin.Interface.CommandManager.RemoveHandler("/nosol");
this.Plugin.Interface.CommandManager.RemoveHandler("/prmt");
}
private void OnCommand(string command, string args) {
if (command == "/prmt") {
this.Plugin.Interface.Framework.Gui.Chat.PrintError($"[{this.Plugin.Name}] The /prmt command is deprecated and will be removed. Please use /nosol instead.");
}
if (args == "report") {
this.Plugin.Ui.ToggleReporting();
return;
}
this.Plugin.Ui.ToggleSettings();
}
}
}

View File

@ -1,11 +1,9 @@
using Dalamud.Game.Command;
using Dalamud.Plugin;
using Dalamud.Plugin;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using NoSoliciting.Interface;
using NoSoliciting.Ml;
namespace NoSoliciting {
@ -14,11 +12,12 @@ namespace NoSoliciting {
public string Name => "NoSoliciting";
private PluginUi Ui { get; set; } = null!;
private Filter Filter { get; set; } = null!;
public DalamudPluginInterface Interface { get; private set; } = null!;
public PluginConfiguration Config { get; private set; } = null!;
public PluginUi Ui { get; private set; } = null!;
public Commands Commands { get; private set; } = null!;
public Definitions? Definitions { get; private set; }
public MlFilter? MlFilter { get; set; }
public bool MlReady => this.Config.UseMachineLearning && this.MlFilter != null;
@ -34,12 +33,6 @@ namespace NoSoliciting {
// ReSharper disable once AutoPropertyCanBeMadeGetOnly.Local
public string AssemblyLocation { get; private set; } = Assembly.GetExecutingAssembly().Location;
private const string LibraryName = "NoSoliciting.CursedWorkaround";
private AppDomain InnerDomain { get; set; } = null!;
public IClassifier Classifier { get; private set; } = null!;
public void Initialize(DalamudPluginInterface pluginInterface) {
string path = Environment.GetEnvironmentVariable("PATH")!;
string newPath = Path.GetDirectoryName(this.AssemblyLocation)!;
@ -47,6 +40,7 @@ namespace NoSoliciting {
this.Interface = pluginInterface ?? throw new ArgumentNullException(nameof(pluginInterface), "DalamudPluginInterface cannot be null");
this.Ui = new PluginUi(this);
this.Commands = new Commands(this);
this.Config = this.Interface.GetPluginConfig() as PluginConfiguration ?? new PluginConfiguration();
this.Config.Initialise(this.Interface);
@ -61,12 +55,21 @@ namespace NoSoliciting {
// pre-compute the max ilvl to prevent stutter
FilterUtil.MaxItemLevelAttainable(this.Interface.Data);
}
this.Interface.UiBuilder.OnBuildUi += this.Ui.Draw;
this.Interface.UiBuilder.OnOpenConfigUi += this.Ui.OpenSettings;
this.Interface.CommandManager.AddHandler("/prmt", new CommandInfo(this.OnCommand) {
HelpMessage = "Opens the NoSoliciting configuration",
});
protected virtual void Dispose(bool disposing) {
if (this._disposedValue) {
return;
}
if (disposing) {
this.Filter.Dispose();
this.MlFilter?.Dispose();
this.Commands.Dispose();
this.Ui.Dispose();
}
this._disposedValue = true;
}
internal void InitialiseMachineLearning() {
@ -94,15 +97,6 @@ namespace NoSoliciting {
});
}
private void OnCommand(string command, string args) {
if (args == "report") {
this.Ui.OpenReporting();
return;
}
this.Ui.OpenSettings(null, null);
}
public void AddMessageHistory(Message message) {
this._messageHistory.Insert(0, message);
@ -119,22 +113,6 @@ namespace NoSoliciting {
this._partyFinderHistory.Add(message);
}
protected virtual void Dispose(bool disposing) {
if (this._disposedValue) {
return;
}
if (disposing) {
this.Filter.Dispose();
this.MlFilter?.Dispose();
this.Interface.UiBuilder.OnBuildUi -= this.Ui.Draw;
this.Interface.UiBuilder.OnOpenConfigUi -= this.Ui.OpenSettings;
this.Interface.CommandManager.RemoveHandler("/prmt");
}
this._disposedValue = true;
}
public void Dispose() {
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
this.Dispose(true);

View File

@ -14,7 +14,7 @@ using System.Threading.Tasks;
using NoSoliciting.Ml;
namespace NoSoliciting {
public class PluginUi {
public class PluginUi : IDisposable {
private Plugin Plugin { get; }
private ReportStatus LastReportStatus { get; set; } = ReportStatus.None;
@ -33,18 +33,30 @@ namespace NoSoliciting {
}
public PluginUi(Plugin plugin) {
this.Plugin = plugin ?? throw new ArgumentNullException(nameof(plugin), "Plugin cannot be null");
this.Plugin = plugin;
this.Plugin.Interface.UiBuilder.OnBuildUi += this.Draw;
this.Plugin.Interface.UiBuilder.OnOpenConfigUi += this.OpenSettings;
}
public void OpenSettings(object? sender, EventArgs? e) {
public void Dispose() {
this.Plugin.Interface.UiBuilder.OnOpenConfigUi -= this.OpenSettings;
this.Plugin.Interface.UiBuilder.OnBuildUi -= this.Draw;
}
private void OpenSettings(object? sender, EventArgs? e) {
this.ShowSettings = true;
}
public void OpenReporting() {
this.ShowReporting = true;
public void ToggleSettings() {
this.ShowSettings = !this.ShowSettings;
}
public void Draw() {
public void ToggleReporting() {
this.ShowReporting = !this.ShowReporting;
}
private void Draw() {
if (this.ShowSettings) {
this.DrawSettings();
}