From 0d8ef94647612d4950faf8f0289d69a2b70d7cd7 Mon Sep 17 00:00:00 2001
From: goat <16760685+goaaats@users.noreply.github.com>
Date: Thu, 1 Apr 2021 20:39:03 +0200
Subject: [PATCH] refactor: new code style in DalamudPluginInterface.cs
---
Dalamud/Plugin/DalamudPluginInterface.cs | 265 ++++++++++++-----------
1 file changed, 141 insertions(+), 124 deletions(-)
diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs
index de4832cc3..c0d9f12e3 100644
--- a/Dalamud/Plugin/DalamudPluginInterface.cs
+++ b/Dalamud/Plugin/DalamudPluginInterface.cs
@@ -1,12 +1,9 @@
using System;
-using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Reflection;
-using System.Runtime.InteropServices;
-using System.Text;
-using System.Threading.Tasks;
+
using Dalamud.Configuration;
using Dalamud.Data;
using Dalamud.Game;
@@ -15,7 +12,6 @@ using Dalamud.Game.Chat.SeStringHandling.Payloads;
using Dalamud.Game.ClientState;
using Dalamud.Game.Command;
using Dalamud.Game.Internal;
-using Dalamud.Game.Internal.Gui;
using Dalamud.Interface;
namespace Dalamud.Plugin
@@ -23,97 +19,23 @@ namespace Dalamud.Plugin
///
/// This class acts as an interface to various objects needed to interact with Dalamud and the game.
///
- public class DalamudPluginInterface : IDisposable {
- ///
- /// The reason this plugin was loaded.
- ///
- public PluginLoadReason Reason { get; }
-
- ///
- /// Get the directory Dalamud assets are stored in.
- ///
- public DirectoryInfo DalamudAssetDirectory => this.dalamud.AssetDirectory;
-
- ///
- /// Get the directory your plugin configurations are stored in.
- ///
- public DirectoryInfo ConfigDirectory => new DirectoryInfo(GetPluginConfigDirectory());
-
- ///
- /// Get the config file of your plugin.
- ///
- public FileInfo ConfigFile => this.configs.GetConfigFile(this.pluginName);
-
- ///
- /// The CommandManager object that allows you to add and remove custom chat commands.
- ///
- public readonly CommandManager CommandManager;
-
- ///
- /// The ClientState object that allows you to access current client memory information like actors, territories, etc.
- ///
- public readonly ClientState ClientState;
-
- ///
- /// The Framework object that allows you to interact with the client.
- ///
- public readonly Framework Framework;
-
- ///
- /// A UiBuilder instance which allows you to draw UI into the game via ImGui draw calls.
- ///
- public readonly UiBuilder UiBuilder;
-
- ///
- /// A SigScanner instance targeting the main module of the FFXIV process.
- ///
- public readonly SigScanner TargetModuleScanner;
-
- ///
- /// A DataManager instance which allows you to access game data needed by the main dalamud features.
- ///
- public readonly DataManager Data;
-
- ///
- /// A SeStringManager instance which allows creating and parsing SeString payloads.
- ///
- public readonly SeStringManager SeStringManager;
-
- ///
- /// Returns true if Dalamud is running in Debug mode or the /xldev menu is open. This can occur on release builds.
- ///
-#if DEBUG
- public bool IsDebugging => true;
-#else
- public bool IsDebugging => this.dalamud.DalamudUi.IsDevMenu;
-#endif
-
- ///
- /// Event that gets fired when loc is changed
- ///
- public event LanguageChangedDelegate OnLanguageChanged;
-
- ///
- /// Delegate for localization change with two-letter iso lang code
- ///
- ///
- public delegate void LanguageChangedDelegate(string langCode);
-
- ///
- /// Current ui language in two-letter iso format
- ///
- public string UiLanguage { get; private set; }
-
+ public class DalamudPluginInterface : IDisposable
+ {
private readonly Dalamud dalamud;
private readonly string pluginName;
private readonly PluginConfigurations configs;
///
+ /// Initializes a new instance of the class.
/// Set up the interface and populate all fields needed.
///
- ///
- internal DalamudPluginInterface(Dalamud dalamud, string pluginName, PluginConfigurations configs, PluginLoadReason reason) {
- Reason = reason;
+ /// The dalamud instance to expose.
+ /// The internal name of the plugin.
+ /// The plugin configurations handler.
+ /// The reason this plugin was loaded.
+ internal DalamudPluginInterface(Dalamud dalamud, string pluginName, PluginConfigurations configs, PluginLoadReason reason)
+ {
+ this.Reason = reason;
this.CommandManager = dalamud.CommandManager;
this.Framework = dalamud.Framework;
this.ClientState = dalamud.ClientState;
@@ -127,28 +49,100 @@ namespace Dalamud.Plugin
this.configs = configs;
this.UiLanguage = this.dalamud.Configuration.LanguageOverride;
- dalamud.LocalizationManager.OnLocalizationChanged += OnLocalizationChanged;
- }
-
- private void OnLocalizationChanged(string langCode) {
- this.UiLanguage = langCode;
- OnLanguageChanged?.Invoke(langCode);
+ dalamud.LocalizationManager.OnLocalizationChanged += this.OnLocalizationChanged;
}
///
- /// Unregister your plugin and dispose all references. You have to call this when your IDalamudPlugin is disposed.
+ /// Delegate for localization change with two-letter iso lang code.
///
- public void Dispose() {
- this.UiBuilder.Dispose();
- this.Framework.Gui.Chat.RemoveChatLinkHandler(this.pluginName);
- this.dalamud.LocalizationManager.OnLocalizationChanged -= OnLocalizationChanged;
- }
+ /// The new language code.
+ public delegate void LanguageChangedDelegate(string langCode);
+
+ ///
+ /// Event that gets fired when loc is changed
+ ///
+ public event LanguageChangedDelegate OnLanguageChanged;
+
+ ///
+ /// Gets the reason this plugin was loaded.
+ ///
+ public PluginLoadReason Reason { get; }
+
+ ///
+ /// Gets the directory Dalamud assets are stored in.
+ ///
+ public DirectoryInfo DalamudAssetDirectory => this.dalamud.AssetDirectory;
+
+ ///
+ /// Gets the directory your plugin configurations are stored in.
+ ///
+ public DirectoryInfo ConfigDirectory => new DirectoryInfo(this.GetPluginConfigDirectory());
+
+ ///
+ /// Gets the config file of your plugin.
+ ///
+ public FileInfo ConfigFile => this.configs.GetConfigFile(this.pluginName);
+
+ ///
+ /// Gets the CommandManager object that allows you to add and remove custom chat commands.
+ ///
+ public CommandManager CommandManager { get; private set; }
+
+ ///
+ /// Gets the ClientState object that allows you to access current client memory information like actors, territories, etc.
+ ///
+ public ClientState ClientState { get; private set; }
+
+ ///
+ /// Gets the Framework object that allows you to interact with the client.
+ ///
+ public Framework Framework { get; private set; }
+
+ ///
+ /// Gets the UiBuilder instance which allows you to draw UI into the game via ImGui draw calls.
+ ///
+ public UiBuilder UiBuilder { get; private set; }
+
+ ///
+ /// Gets the SigScanner instance targeting the main module of the FFXIV process.
+ ///
+ public SigScanner TargetModuleScanner { get; private set; }
+
+ ///
+ /// Gets the DataManager instance which allows you to access game data needed by the main dalamud features.
+ ///
+ public DataManager Data { get; private set; }
+
+ ///
+ /// Gets the SeStringManager instance which allows creating and parsing SeString payloads.
+ ///
+ public SeStringManager SeStringManager { get; private set; }
+
+ ///
+ /// Gets a value indicating whether Dalamud is running in Debug mode or the /xldev menu is open. This can occur on release builds.
+ ///
+#if DEBUG
+ public bool IsDebugging => true;
+#else
+ public bool IsDebugging => this.dalamud.DalamudUi.IsDevMenu;
+#endif
+
+ ///
+ /// Gets the current UI language in two-letter iso format.
+ ///
+ public string UiLanguage { get; private set; }
+
+ ///
+ /// Gets the action that should be executed when any plugin sends a message.
+ ///
+ internal Action AnyPluginIpcAction { get; private set; }
///
/// Save a plugin configuration(inheriting IPluginConfiguration).
///
/// The current configuration.
- public void SavePluginConfig(IPluginConfiguration currentConfig) {
+ public void SavePluginConfig(IPluginConfiguration currentConfig)
+ {
if (currentConfig == null)
return;
@@ -159,7 +153,8 @@ namespace Dalamud.Plugin
/// Get a previously saved plugin configuration or null if none was saved before.
///
/// A previously saved config or null if none was saved before.
- public IPluginConfiguration GetPluginConfig() {
+ public IPluginConfiguration GetPluginConfig()
+ {
// This is done to support json deserialization of plugin configurations
// even after running an in-game update of plugins, where the assembly version
// changes.
@@ -182,9 +177,9 @@ namespace Dalamud.Plugin
}
///
- /// Get the config directory
+ /// Get the config directory.
///
- /// directory with path of AppData/XIVLauncher/pluginConfig/PluginInternalName
+ /// directory with path of AppData/XIVLauncher/pluginConfig/PluginInternalName.
public string GetPluginConfigDirectory() => this.configs.GetDirectory(this.pluginName);
#region Chat Links
@@ -192,43 +187,44 @@ namespace Dalamud.Plugin
///
/// Register a chat link handler.
///
- ///
- ///
+ /// The ID of the command.
+ /// The action to be executed.
/// Returns an SeString payload for the link.
- public DalamudLinkPayload AddChatLinkHandler(uint commandId, Action commandAction) {
+ public DalamudLinkPayload AddChatLinkHandler(uint commandId, Action commandAction)
+ {
return this.Framework.Gui.Chat.AddChatLinkHandler(this.pluginName, commandId, commandAction);
}
///
/// Remove a chat link handler.
///
- ///
- public void RemoveChatLinkHandler(uint commandId) {
+ /// The ID of the command.
+ public void RemoveChatLinkHandler(uint commandId)
+ {
this.Framework.Gui.Chat.RemoveChatLinkHandler(this.pluginName, commandId);
}
///
/// Removes all chat link handlers registered by the plugin.
///
- public void RemoveChatLinkHandler() {
+ public void RemoveChatLinkHandler()
+ {
this.Framework.Gui.Chat.RemoveChatLinkHandler(this.pluginName);
}
#endregion
#region IPC
- internal Action anyPluginIpcAction;
-
///
/// Subscribe to an IPC message by any plugin.
///
/// The action to take when a message was received.
public void SubscribeAny(Action action)
{
- if (this.anyPluginIpcAction != null)
+ if (this.AnyPluginIpcAction != null)
throw new InvalidOperationException("Can't subscribe multiple times.");
- this.anyPluginIpcAction = action;
+ this.AnyPluginIpcAction = action;
}
///
@@ -236,7 +232,8 @@ namespace Dalamud.Plugin
///
/// The InternalName of the plugin to subscribe to.
/// The action to take when a message was received.
- public void Subscribe(string pluginName, Action action) {
+ public void Subscribe(string pluginName, Action action)
+ {
if (this.dalamud.PluginManager.IpcSubscriptions.Any(x => x.SourcePluginName == this.pluginName && x.SubPluginName == pluginName))
throw new InvalidOperationException("Can't add multiple subscriptions for the same plugin.");
@@ -248,17 +245,18 @@ namespace Dalamud.Plugin
///
public void UnsubscribeAny()
{
- if (this.anyPluginIpcAction == null)
+ if (this.AnyPluginIpcAction == null)
throw new InvalidOperationException("Wasn't subscribed to this plugin.");
- this.anyPluginIpcAction = null;
+ this.AnyPluginIpcAction = null;
}
///
/// Unsubscribe from messages from a plugin.
///
/// The InternalName of the plugin to unsubscribe from.
- public void Unsubscribe(string pluginName) {
+ public void Unsubscribe(string pluginName)
+ {
var sub = this.dalamud.PluginManager.IpcSubscriptions.FirstOrDefault(x => x.SourcePluginName == this.pluginName && x.SubPluginName == pluginName);
if (sub.SubAction == null)
throw new InvalidOperationException("Wasn't subscribed to this plugin.");
@@ -270,9 +268,11 @@ namespace Dalamud.Plugin
/// Send a message to all subscribed plugins.
///
/// The message to send.
- public void SendMessage(ExpandoObject message) {
+ public void SendMessage(ExpandoObject message)
+ {
var subs = this.dalamud.PluginManager.IpcSubscriptions.Where(x => x.SubPluginName == this.pluginName);
- foreach (var sub in subs.Select(x => x.SubAction)) {
+ foreach (var sub in subs.Select(x => x.SubAction))
+ {
sub.Invoke(message);
}
}
@@ -287,10 +287,10 @@ namespace Dalamud.Plugin
{
var (_, _, pluginInterface, _) = this.dalamud.PluginManager.Plugins.FirstOrDefault(x => x.Definition.InternalName == pluginName);
- if (pluginInterface?.anyPluginIpcAction == null)
+ if (pluginInterface?.AnyPluginIpcAction == null)
return false;
- pluginInterface.anyPluginIpcAction.Invoke(this.pluginName, message);
+ pluginInterface.AnyPluginIpcAction.Invoke(this.pluginName, message);
return true;
}
@@ -304,7 +304,8 @@ namespace Dalamud.Plugin
/// The message template.
/// Values to log.
[Obsolete]
- public void Log(string messageTemplate, params object[] values) {
+ public void Log(string messageTemplate, params object[] values)
+ {
Serilog.Log.Information(messageTemplate, values);
}
@@ -332,5 +333,21 @@ namespace Dalamud.Plugin
}
#endregion
+
+ ///
+ /// Unregister your plugin and dispose all references. You have to call this when your IDalamudPlugin is disposed.
+ ///
+ public void Dispose()
+ {
+ this.UiBuilder.Dispose();
+ this.Framework.Gui.Chat.RemoveChatLinkHandler(this.pluginName);
+ this.dalamud.LocalizationManager.OnLocalizationChanged -= this.OnLocalizationChanged;
+ }
+
+ private void OnLocalizationChanged(string langCode)
+ {
+ this.UiLanguage = langCode;
+ this.OnLanguageChanged?.Invoke(langCode);
+ }
}
}