From 994d5134a562596dab83eb2bd4f0a65feea72bcc Mon Sep 17 00:00:00 2001 From: Anna Date: Thu, 28 Sep 2023 02:29:56 -0400 Subject: [PATCH] refactor: update for api 9 --- SoundFilter/Commands.cs | 16 +++++----- SoundFilter/Config/Migrator.cs | 5 ++- SoundFilter/Filter.cs | 19 ++++++------ .../{SoundFilterPlugin.cs => Plugin.cs} | 31 +++++++++++-------- SoundFilter/Ui/AddFilter.cs | 6 ++-- SoundFilter/Ui/PluginUi.cs | 7 ++--- SoundFilter/Ui/Settings.cs | 6 ++-- SoundFilter/Ui/SoundLog.cs | 4 +-- 8 files changed, 48 insertions(+), 46 deletions(-) rename SoundFilter/{SoundFilterPlugin.cs => Plugin.cs} (64%) diff --git a/SoundFilter/Commands.cs b/SoundFilter/Commands.cs index de3f55e..2e1e0ba 100755 --- a/SoundFilter/Commands.cs +++ b/SoundFilter/Commands.cs @@ -7,13 +7,13 @@ namespace SoundFilter { internal class Commands : IDisposable { private const string Name = "/soundfilter"; - private SoundFilterPlugin Plugin { get; } + private Plugin Plugin { get; } - public Commands(SoundFilterPlugin plugin) { + public Commands(Plugin plugin) { this.Plugin = plugin; this.Plugin.CommandManager.AddHandler(Name, new CommandInfo(this.OnCommand) { - HelpMessage = $"Toggle the {this.Plugin.Name} config", + HelpMessage = $"Toggle the {Plugin.Name} config", }); } @@ -31,9 +31,9 @@ namespace SoundFilter { var split = args.Split(' '); if (split.Length < 1) { - chat.PrintError($"[{this.Plugin.Name}] {Language.CommandNotEnoughArguments}"); - chat.PrintError($"[{this.Plugin.Name}] /soundfilter log"); - chat.PrintError($"[{this.Plugin.Name}] /soundfilter [filter name]"); + chat.PrintError($"[{Plugin.Name}] {Language.CommandNotEnoughArguments}"); + chat.PrintError($"[{Plugin.Name}] /soundfilter log"); + chat.PrintError($"[{Plugin.Name}] /soundfilter [filter name]"); return; } @@ -46,7 +46,7 @@ namespace SoundFilter { var filterName = split.Length > 1 ? string.Join(" ", split.Skip(1)) : null; var filter = filterName == null ? null : this.Plugin.Config.Filters.FirstOrDefault(filter => filter.Name == filterName); if (filterName != null && filter == null) { - chat.PrintError($"[{this.Plugin.Name}] {Language.CommandNoSuchFilter}"); + chat.PrintError($"[{Plugin.Name}] {Language.CommandNoSuchFilter}"); return; } @@ -58,7 +58,7 @@ namespace SoundFilter { _ => null, }; if (enabled == null) { - chat.PrintError($"[{this.Plugin.Name}] {Language.CommandInvalidSubcommand}"); + chat.PrintError($"[{Plugin.Name}] {Language.CommandInvalidSubcommand}"); return; } diff --git a/SoundFilter/Config/Migrator.cs b/SoundFilter/Config/Migrator.cs index 2884a6f..735787c 100755 --- a/SoundFilter/Config/Migrator.cs +++ b/SoundFilter/Config/Migrator.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using Dalamud.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -38,7 +37,7 @@ namespace SoundFilter.Config { old["Version"] = 2; } - public static Configuration LoadConfiguration(SoundFilterPlugin plugin) { + public static Configuration LoadConfiguration(Plugin plugin) { var fileInfo = plugin.Interface.ConfigFile; var text = fileInfo.Exists ? File.ReadAllText(fileInfo.FullName) @@ -70,7 +69,7 @@ namespace SoundFilter.Config { MigrateV1(config); break; default: - PluginLog.Warning($"Tried to migrate from an unknown version: {version}"); + Plugin.Log.Warning($"Tried to migrate from an unknown version: {version}"); goto DefaultConfiguration; } diff --git a/SoundFilter/Filter.cs b/SoundFilter/Filter.cs index ac57f7f..a40c6e1 100755 --- a/SoundFilter/Filter.cs +++ b/SoundFilter/Filter.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; using Dalamud.Hooking; -using Dalamud.Logging; using FFXIVClientStructs.FFXIV.Client.System.Framework; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; @@ -48,7 +47,7 @@ namespace SoundFilter { #endregion - private SoundFilterPlugin Plugin { get; } + private Plugin Plugin { get; } private bool WasStreamingEnabled { get; } private ConcurrentDictionary Scds { get; } = new(); @@ -61,7 +60,7 @@ namespace SoundFilter { private IntPtr MusicManager { get { if (!this.Plugin.SigScanner.TryScanText(Signatures.MusicManagerOffset, out var instructionPtr)) { - PluginLog.LogWarning("Could not find music manager"); + Plugin.Log.Warning("Could not find music manager"); return IntPtr.Zero; } @@ -89,7 +88,7 @@ namespace SoundFilter { } } - internal Filter(SoundFilterPlugin plugin) { + internal Filter(Plugin plugin) { this.Plugin = plugin; this.WasStreamingEnabled = this.Streaming; @@ -131,19 +130,19 @@ namespace SoundFilter { internal void Enable() { if (this.PlaySpecificSoundHook == null && this.Plugin.SigScanner.TryScanText(Signatures.PlaySpecificSound, out var playPtr)) { - this.PlaySpecificSoundHook = Hook.FromAddress(playPtr, this.PlaySpecificSoundDetour); + this.PlaySpecificSoundHook = this.Plugin.GameInteropProvider.HookFromAddress(playPtr, this.PlaySpecificSoundDetour); } if (this.GetResourceSyncHook == null && this.Plugin.SigScanner.TryScanText(Signatures.GetResourceSync, out var syncPtr)) { - this.GetResourceSyncHook = Hook.FromAddress(syncPtr, this.GetResourceSyncDetour); + this.GetResourceSyncHook = this.Plugin.GameInteropProvider.HookFromAddress(syncPtr, this.GetResourceSyncDetour); } if (this.GetResourceAsyncHook == null && this.Plugin.SigScanner.TryScanText(Signatures.GetResourceAsync, out var asyncPtr)) { - this.GetResourceAsyncHook = Hook.FromAddress(asyncPtr, this.GetResourceAsyncDetour); + this.GetResourceAsyncHook = this.Plugin.GameInteropProvider.HookFromAddress(asyncPtr, this.GetResourceAsyncDetour); } if (this.LoadSoundFileHook == null && this.Plugin.SigScanner.TryScanText(Signatures.LoadSoundFile, out var soundPtr)) { - this.LoadSoundFileHook = Hook.FromAddress(soundPtr, this.LoadSoundFileDetour); + this.LoadSoundFileHook = this.Plugin.GameInteropProvider.HookFromAddress(soundPtr, this.LoadSoundFileDetour); } this.PlaySpecificSoundHook?.Enable(); @@ -179,7 +178,7 @@ namespace SoundFilter { idx = 0; } } catch (Exception ex) { - PluginLog.LogError(ex, "Error in PlaySpecificSoundDetour"); + Plugin.Log.Error(ex, "Error in PlaySpecificSoundDetour"); } return this.PlaySpecificSoundHook!.Original(a1, idx); @@ -257,7 +256,7 @@ namespace SoundFilter { this.Scds[dataPtr] = name; } } catch (Exception ex) { - PluginLog.LogError(ex, "Error in LoadSoundFileDetour"); + Plugin.Log.Error(ex, "Error in LoadSoundFileDetour"); } return ret; diff --git a/SoundFilter/SoundFilterPlugin.cs b/SoundFilter/Plugin.cs similarity index 64% rename from SoundFilter/SoundFilterPlugin.cs rename to SoundFilter/Plugin.cs index f071575..4003bca 100755 --- a/SoundFilter/SoundFilterPlugin.cs +++ b/SoundFilter/Plugin.cs @@ -1,41 +1,46 @@ using Dalamud.Game; -using Dalamud.Game.Command; -using Dalamud.Game.Gui; using Dalamud.Game.Text; using Dalamud.Game.Text.SeStringHandling; using Dalamud.Game.Text.SeStringHandling.Payloads; using Dalamud.IoC; using Dalamud.Plugin; +using Dalamud.Plugin.Services; using SoundFilter.Config; using SoundFilter.Resources; using SoundFilter.Ui; namespace SoundFilter { // ReSharper disable once ClassNeverInstantiated.Global - internal class SoundFilterPlugin : IDalamudPlugin { - public string Name => "Sound Filter"; + internal class Plugin : IDalamudPlugin { + public static string Name => "Sound Filter"; + + [PluginService] + internal static IPluginLog Log { get; private set; } = null!; [PluginService] internal DalamudPluginInterface Interface { get; init; } = null!; [PluginService] - internal ChatGui ChatGui { get; init; } = null!; + internal IChatGui ChatGui { get; init; } = null!; [PluginService] - internal CommandManager CommandManager { get; init; } = null!; + internal ICommandManager CommandManager { get; init; } = null!; [PluginService] - internal Framework Framework { get; init; } = null!; + internal IFramework Framework { get; init; } = null!; [PluginService] - internal SigScanner SigScanner { get; init; } = null!; + internal ISigScanner SigScanner { get; init; } = null!; + + [PluginService] + internal IGameInteropProvider GameInteropProvider { get; init; } = null!; internal Configuration Config { get; } internal Filter Filter { get; } internal PluginUi Ui { get; } private Commands Commands { get; } - public SoundFilterPlugin() { + public Plugin() { this.Config = Migrator.LoadConfiguration(this); this.Config.Initialise(this.Interface); @@ -51,12 +56,12 @@ namespace SoundFilter { return; } - var message = string.Format(Language.LoadWarning, this.Name); - this.ChatGui.PrintChat(new XivChatEntry { - Name = this.Name, + var message = string.Format(Language.LoadWarning, Name); + this.ChatGui.Print(new XivChatEntry { + Name = Name, Message = new SeString( new UIForegroundPayload(502), - new TextPayload($"[{this.Name}] {message}"), + new TextPayload($"[{Name}] {message}"), new UIForegroundPayload(0) ), }); diff --git a/SoundFilter/Ui/AddFilter.cs b/SoundFilter/Ui/AddFilter.cs index f21975b..9d7a284 100755 --- a/SoundFilter/Ui/AddFilter.cs +++ b/SoundFilter/Ui/AddFilter.cs @@ -9,19 +9,19 @@ using SoundFilter.Resources; namespace SoundFilter.Ui { internal class AddFilter { private Guid Id { get; } = Guid.NewGuid(); - private SoundFilterPlugin Plugin { get; } + private Plugin Plugin { get; } private CustomFilter? Filter { get; } private string _filterName = string.Empty; private string _newSoundPath = string.Empty; private readonly List _soundPaths = new(); - internal AddFilter(SoundFilterPlugin plugin) { + internal AddFilter(Plugin plugin) { this.Plugin = plugin; this.Filter = null; } - internal AddFilter(SoundFilterPlugin plugin, CustomFilter filter) { + internal AddFilter(Plugin plugin, CustomFilter filter) { this.Plugin = plugin; this.Filter = filter; diff --git a/SoundFilter/Ui/PluginUi.cs b/SoundFilter/Ui/PluginUi.cs index ff46927..9b6fbf7 100755 --- a/SoundFilter/Ui/PluginUi.cs +++ b/SoundFilter/Ui/PluginUi.cs @@ -1,15 +1,14 @@ using System; using System.Globalization; -using Dalamud.Logging; using SoundFilter.Resources; namespace SoundFilter.Ui { public class PluginUi : IDisposable { - private SoundFilterPlugin Plugin { get; } + private Plugin Plugin { get; } internal Settings Settings { get; } private SoundLog SoundLog { get; } - internal PluginUi(SoundFilterPlugin plugin) { + internal PluginUi(Plugin plugin) { this.Plugin = plugin; this.ConfigureLanguage(); @@ -34,7 +33,7 @@ namespace SoundFilter.Ui { try { Language.Culture = new CultureInfo(langCode); } catch (Exception ex) { - PluginLog.LogError(ex, $"Could not set culture to {langCode} - falling back to default"); + Plugin.Log.Error(ex, $"Could not set culture to {langCode} - falling back to default"); Language.Culture = CultureInfo.DefaultThreadCurrentUICulture; } } diff --git a/SoundFilter/Ui/Settings.cs b/SoundFilter/Ui/Settings.cs index 8be6156..b07072f 100755 --- a/SoundFilter/Ui/Settings.cs +++ b/SoundFilter/Ui/Settings.cs @@ -6,14 +6,14 @@ using SoundFilter.Resources; namespace SoundFilter.Ui { public class Settings : IDisposable { - private SoundFilterPlugin Plugin { get; } + private Plugin Plugin { get; } private AddFilter AddFilter { get; } private AddFilter? EditFilter { get; set; } private bool _showWindow; private int _dragging = -1; - internal Settings(SoundFilterPlugin plugin) { + internal Settings(Plugin plugin) { this.Plugin = plugin; this.AddFilter = new AddFilter(plugin); @@ -48,7 +48,7 @@ namespace SoundFilter.Ui { ImGui.SetNextWindowSize(new Vector2(500, 450), ImGuiCond.FirstUseEver); - var windowTitle = string.Format(Language.SettingsWindowTitle, this.Plugin.Name); + var windowTitle = string.Format(Language.SettingsWindowTitle, Plugin.Name); if (!ImGui.Begin($"{windowTitle}###soundfilter-settings", ref this._showWindow)) { ImGui.End(); return; diff --git a/SoundFilter/Ui/SoundLog.cs b/SoundFilter/Ui/SoundLog.cs index 77ea8d9..41ac1e2 100755 --- a/SoundFilter/Ui/SoundLog.cs +++ b/SoundFilter/Ui/SoundLog.cs @@ -7,11 +7,11 @@ using SoundFilter.Resources; namespace SoundFilter.Ui { public class SoundLog { - private SoundFilterPlugin Plugin { get; } + private Plugin Plugin { get; } private string _search = string.Empty; - internal SoundLog(SoundFilterPlugin plugin) { + internal SoundLog(Plugin plugin) { this.Plugin = plugin; }