diff --git a/SoundFilter/Commands.cs b/SoundFilter/Commands.cs index 3e5002e..de3f55e 100755 --- a/SoundFilter/Commands.cs +++ b/SoundFilter/Commands.cs @@ -12,13 +12,13 @@ namespace SoundFilter { public Commands(SoundFilterPlugin plugin) { this.Plugin = plugin; - this.Plugin.Interface.CommandManager.AddHandler(Name, new CommandInfo(this.OnCommand) { - HelpMessage = $"Toggle the {SoundFilterPlugin.Name} config", + this.Plugin.CommandManager.AddHandler(Name, new CommandInfo(this.OnCommand) { + HelpMessage = $"Toggle the {this.Plugin.Name} config", }); } public void Dispose() { - this.Plugin.Interface.CommandManager.RemoveHandler(Name); + this.Plugin.CommandManager.RemoveHandler(Name); } private void OnCommand(string command, string args) { @@ -27,13 +27,13 @@ namespace SoundFilter { return; } - var chat = this.Plugin.Interface.Framework.Gui.Chat; + var chat = this.Plugin.ChatGui; var split = args.Split(' '); if (split.Length < 1) { - chat.PrintError($"[{SoundFilterPlugin.Name}] {Language.CommandNotEnoughArguments}"); - chat.PrintError($"[{SoundFilterPlugin.Name}] /soundfilter log"); - chat.PrintError($"[{SoundFilterPlugin.Name}] /soundfilter [filter name]"); + chat.PrintError($"[{this.Plugin.Name}] {Language.CommandNotEnoughArguments}"); + chat.PrintError($"[{this.Plugin.Name}] /soundfilter log"); + chat.PrintError($"[{this.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($"[{SoundFilterPlugin.Name}] {Language.CommandNoSuchFilter}"); + chat.PrintError($"[{this.Plugin.Name}] {Language.CommandNoSuchFilter}"); return; } @@ -58,13 +58,22 @@ namespace SoundFilter { _ => null, }; if (enabled == null) { - chat.PrintError($"[{SoundFilterPlugin.Name}] {Language.CommandInvalidSubcommand}"); + chat.PrintError($"[{this.Plugin.Name}] {Language.CommandInvalidSubcommand}"); return; } if (filter != null) { filter.Enabled = enabled.Value; } else { + switch (this.Plugin.Config.Enabled) { + case true when !enabled.Value: + this.Plugin.Filter.Disable(); + break; + case false when enabled.Value: + this.Plugin.Filter.Enable(); + break; + } + this.Plugin.Config.Enabled = enabled.Value; } diff --git a/SoundFilter/Config/Migrator.cs b/SoundFilter/Config/Migrator.cs index 9814b72..2884a6f 100755 --- a/SoundFilter/Config/Migrator.cs +++ b/SoundFilter/Config/Migrator.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.IO; -using Dalamud.Plugin; +using Dalamud.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -22,9 +22,9 @@ namespace SoundFilter.Config { private static void MigrateV1(JObject old) { var filters = new List(); - WithEachObject(old["Filtered"], (glob, filter) => { - var name = filter["Name"].Value(); - var enabled = filter["Enabled"].Value(); + WithEachObject(old["Filtered"]!, (glob, filter) => { + var name = filter["Name"]!.Value()!; + var enabled = filter["Enabled"]!.Value(); filters.Add(new CustomFilter { Name = name, Enabled = enabled, @@ -48,7 +48,7 @@ namespace SoundFilter.Config { goto DefaultConfiguration; } - var config = JsonConvert.DeserializeObject(text); + var config = JsonConvert.DeserializeObject(text)!; int GetVersion() { if (config.TryGetValue("Version", out var token)) { @@ -78,7 +78,7 @@ namespace SoundFilter.Config { } if (version == Configuration.LatestVersion) { - return config.ToObject(); + return config.ToObject()!; } DefaultConfiguration: diff --git a/SoundFilter/Filter.cs b/SoundFilter/Filter.cs index 4c4a914..63de32a 100755 --- a/SoundFilter/Filter.cs +++ b/SoundFilter/Filter.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Runtime.ExceptionServices; using System.Runtime.InteropServices; using Dalamud.Hooking; -using Dalamud.Plugin; +using Dalamud.Logging; namespace SoundFilter { internal unsafe class Filter : IDisposable { @@ -56,13 +56,13 @@ namespace SoundFilter { private IntPtr MusicManager { get { - if (!this.Plugin.Interface.TargetModuleScanner.TryScanText(Signatures.MusicManagerOffset, out var instructionPtr)) { + if (!this.Plugin.SigScanner.TryScanText(Signatures.MusicManagerOffset, out var instructionPtr)) { PluginLog.LogWarning("Could not find music manager"); return IntPtr.Zero; } var offset = *(int*) (instructionPtr + 3); - return *(IntPtr*) (this.Plugin.Interface.Framework.Address.BaseAddress + offset); + return *(IntPtr*) (this.Plugin.Framework.Address.BaseAddress + offset); } } @@ -126,16 +126,16 @@ namespace SoundFilter { } internal void Enable() { - if (this.PlaySpecificSoundHook == null && this.Plugin.Interface.TargetModuleScanner.TryScanText(Signatures.PlaySpecificSound, out var playPtr)) { - this.PlaySpecificSoundHook = new Hook(playPtr, new PlaySpecificSoundDelegate(this.PlaySpecificSoundDetour)); + if (this.PlaySpecificSoundHook == null && this.Plugin.SigScanner.TryScanText(Signatures.PlaySpecificSound, out var playPtr)) { + this.PlaySpecificSoundHook = new Hook(playPtr, this.PlaySpecificSoundDetour); } - if (this.GetResourceSyncHook == null && this.Plugin.Interface.TargetModuleScanner.TryScanText(Signatures.GetResourceSync, out var syncPtr)) { - this.GetResourceSyncHook = new Hook(syncPtr, new GetResourceSyncPrototype(this.GetResourceSyncDetour)); + if (this.GetResourceSyncHook == null && this.Plugin.SigScanner.TryScanText(Signatures.GetResourceSync, out var syncPtr)) { + this.GetResourceSyncHook = new Hook(syncPtr, this.GetResourceSyncDetour); } - if (this.GetResourceAsyncHook == null && this.Plugin.Interface.TargetModuleScanner.TryScanText(Signatures.GetResourceAsync, out var asyncPtr)) { - this.GetResourceAsyncHook = new Hook(asyncPtr, new GetResourceAsyncPrototype(this.GetResourceAsyncDetour)); + if (this.GetResourceAsyncHook == null && this.Plugin.SigScanner.TryScanText(Signatures.GetResourceAsync, out var asyncPtr)) { + this.GetResourceAsyncHook = new Hook(asyncPtr, this.GetResourceAsyncDetour); } this.PlaySpecificSoundHook?.Enable(); @@ -149,6 +149,20 @@ namespace SoundFilter { this.GetResourceAsyncHook?.Disable(); } + internal void Toggle(bool save = true) { + if (this.Plugin.Config.Enabled) { + this.Disable(); + } else { + this.Enable(); + } + + this.Plugin.Config.Enabled ^= true; + + if (save) { + this.Plugin.Config.Save(); + } + } + public void Dispose() { this.PlaySpecificSoundHook?.Dispose(); this.GetResourceSyncHook?.Dispose(); diff --git a/SoundFilter/FodyWeavers.xml b/SoundFilter/FodyWeavers.xml index 0609f45..37de851 100755 --- a/SoundFilter/FodyWeavers.xml +++ b/SoundFilter/FodyWeavers.xml @@ -1,5 +1,3 @@  - - diff --git a/SoundFilter/PluginShim.cs b/SoundFilter/PluginShim.cs deleted file mode 100755 index cbc8bfa..0000000 --- a/SoundFilter/PluginShim.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Dalamud.Plugin; - -namespace SoundFilter { - // ReSharper disable once UnusedType.Global - public class PluginShim : IDalamudPlugin { - public string Name => SoundFilterPlugin.Name; - - private SoundFilterPlugin? Plugin { get; set; } - - public void Initialize(DalamudPluginInterface pluginInterface) { - this.Plugin = new SoundFilterPlugin(pluginInterface); - } - - public void Dispose() { - this.Plugin?.Dispose(); - } - } -} diff --git a/SoundFilter/SoundFilter.csproj b/SoundFilter/SoundFilter.csproj index 54e56f1..784e878 100755 --- a/SoundFilter/SoundFilter.csproj +++ b/SoundFilter/SoundFilter.csproj @@ -1,10 +1,12 @@ - net48 + net5-windows 1.4.2 latest enable true + false + true @@ -39,11 +41,12 @@ - + - - + - + + + diff --git a/SoundFilter/SoundFilter.yaml b/SoundFilter/SoundFilter.yaml index 5eee63f..480b9d3 100755 --- a/SoundFilter/SoundFilter.yaml +++ b/SoundFilter/SoundFilter.yaml @@ -1,9 +1,13 @@ name: Sound Filter author: ascclemens +punchline: Filter any game sound. description: |- Filters any sound or set of sounds from the game. - Remove a battle sound effect you don't like - Remove specific emote sounds - Remove specific background music + + Icons: filter by Kirby Wu from the Noun Project + and Sound by Gregor Cresnar from the Noun Project repo_url: https://git.sr.ht/jkcclemens/SoundFilter diff --git a/SoundFilter/SoundFilterPlugin.cs b/SoundFilter/SoundFilterPlugin.cs index 4108f27..f071575 100755 --- a/SoundFilter/SoundFilterPlugin.cs +++ b/SoundFilter/SoundFilterPlugin.cs @@ -1,25 +1,41 @@ -using System; +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 SoundFilter.Config; using SoundFilter.Resources; using SoundFilter.Ui; namespace SoundFilter { - internal class SoundFilterPlugin : IDisposable { - internal const string Name = "Sound Filter"; + // ReSharper disable once ClassNeverInstantiated.Global + internal class SoundFilterPlugin : IDalamudPlugin { + public string Name => "Sound Filter"; + + [PluginService] + internal DalamudPluginInterface Interface { get; init; } = null!; + + [PluginService] + internal ChatGui ChatGui { get; init; } = null!; + + [PluginService] + internal CommandManager CommandManager { get; init; } = null!; + + [PluginService] + internal Framework Framework { get; init; } = null!; + + [PluginService] + internal SigScanner SigScanner { get; init; } = null!; - internal DalamudPluginInterface Interface { get; } internal Configuration Config { get; } internal Filter Filter { get; } internal PluginUi Ui { get; } private Commands Commands { get; } - internal SoundFilterPlugin(DalamudPluginInterface @interface) { - this.Interface = @interface; - + public SoundFilterPlugin() { this.Config = Migrator.LoadConfiguration(this); this.Config.Initialise(this.Interface); @@ -35,14 +51,14 @@ namespace SoundFilter { return; } - var message = string.Format(Language.LoadWarning, Name); - this.Interface.Framework.Gui.Chat.PrintChat(new XivChatEntry { - Name = Name, - MessageBytes = new SeString(new Payload[] { - new UIForegroundPayload(this.Interface.Data, 502), - new TextPayload($"[{Name}] {message}"), - new UIForegroundPayload(this.Interface.Data, 0), - }).Encode(), + var message = string.Format(Language.LoadWarning, this.Name); + this.ChatGui.PrintChat(new XivChatEntry { + Name = this.Name, + Message = new SeString( + new UIForegroundPayload(502), + new TextPayload($"[{this.Name}] {message}"), + new UIForegroundPayload(0) + ), }); } diff --git a/SoundFilter/Ui/PluginUi.cs b/SoundFilter/Ui/PluginUi.cs index 8886b4b..ff46927 100755 --- a/SoundFilter/Ui/PluginUi.cs +++ b/SoundFilter/Ui/PluginUi.cs @@ -1,6 +1,6 @@ using System; using System.Globalization; -using Dalamud.Plugin; +using Dalamud.Logging; using SoundFilter.Resources; namespace SoundFilter.Ui { @@ -17,18 +17,19 @@ namespace SoundFilter.Ui { this.Settings = new Settings(this.Plugin); this.SoundLog = new SoundLog(this.Plugin); - this.Plugin.Interface.UiBuilder.OnBuildUi += this.Draw; - this.Plugin.Interface.OnLanguageChanged += this.ConfigureLanguage; + this.Plugin.Interface.UiBuilder.Draw += this.Draw; + this.Plugin.Interface.LanguageChanged += this.ConfigureLanguage; } public void Dispose() { - this.Plugin.Interface.OnLanguageChanged -= this.ConfigureLanguage; - this.Plugin.Interface.UiBuilder.OnBuildUi -= this.Draw; + this.Plugin.Interface.LanguageChanged -= this.ConfigureLanguage; + this.Plugin.Interface.UiBuilder.Draw -= this.Draw; this.Settings.Dispose(); } private void ConfigureLanguage(string? langCode = null) { + // ReSharper disable once ConstantNullCoalescingCondition langCode ??= this.Plugin.Interface.UiLanguage ?? "en"; try { Language.Culture = new CultureInfo(langCode); diff --git a/SoundFilter/Ui/Settings.cs b/SoundFilter/Ui/Settings.cs index 9049069..9c45212 100755 --- a/SoundFilter/Ui/Settings.cs +++ b/SoundFilter/Ui/Settings.cs @@ -17,14 +17,14 @@ namespace SoundFilter.Ui { this.Plugin = plugin; this.AddFilter = new AddFilter(plugin); - this.Plugin.Interface.UiBuilder.OnOpenConfigUi += this.Toggle; + this.Plugin.Interface.UiBuilder.OpenConfigUi += this.Toggle; } public void Dispose() { - this.Plugin.Interface.UiBuilder.OnOpenConfigUi -= this.Toggle; + this.Plugin.Interface.UiBuilder.OpenConfigUi -= this.Toggle; } - internal void Toggle(object? sender = null, object? args = null) { + internal void Toggle() { this._showWindow = !this._showWindow; } @@ -48,7 +48,7 @@ namespace SoundFilter.Ui { ImGui.SetNextWindowSize(new Vector2(500, 450), ImGuiCond.FirstUseEver); - var windowTitle = string.Format(Language.SettingsWindowTitle, SoundFilterPlugin.Name); + var windowTitle = string.Format(Language.SettingsWindowTitle, this.Plugin.Name); if (!ImGui.Begin($"{windowTitle}###soundfilter-settings", ref this._showWindow)) { ImGui.End(); return; diff --git a/SoundFilter/Util.cs b/SoundFilter/Util.cs index 0a62b95..ae4d797 100755 --- a/SoundFilter/Util.cs +++ b/SoundFilter/Util.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.Linq; using System.Text; using Dalamud.Game; using Dalamud.Interface; diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..38c4057 Binary files /dev/null and b/icon.png differ diff --git a/icon.svg b/icon.svg new file mode 100755 index 0000000..f1400aa --- /dev/null +++ b/icon.svg @@ -0,0 +1,98 @@ + + + + + + + + + + + Artboard 227 + + + + Artboard 227 + + + + + + + + + +