diff --git a/MuteSFX/AudioModuleManager.cs b/MuteSFX/AudioModuleManager.cs index 07ae88a..6800bff 100644 --- a/MuteSFX/AudioModuleManager.cs +++ b/MuteSFX/AudioModuleManager.cs @@ -28,23 +28,24 @@ public static class AudioModuleManager foreach (string clipName in clipNames) { string clipPath = Path.Combine(path, clipName); - if (!File.Exists(clipPath)) + + if (File.Exists(clipPath)) + continue; + + byte[] clipData; + string resourceName = "MuteSFX.SFX." + clipName; + + using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { - byte[] clipData = null; - string resourceName = "MuteSFX.SFX." + clipName; - using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) - { - clipData = new byte[stream.Length]; - stream.Read(clipData, 0, clipData.Length); - } - - using (FileStream fileStream = new FileStream(clipPath, FileMode.CreateNew)) - { - fileStream.Write(clipData, 0, clipData.Length); - } - - MuteSFX.Logger.Msg("Placed missing sfx in audio folder: " + clipName); + if (stream == null) continue; + clipData = new byte[stream.Length]; + stream.Read(clipData, 0, clipData.Length); } + + using (FileStream fileStream = new FileStream(clipPath, FileMode.CreateNew)) + fileStream.Write(clipData, 0, clipData.Length); + + MuteSFX.Logger.Msg("Placed missing sfx in audio folder: " + clipName); } } diff --git a/MuteSFX/Main.cs b/MuteSFX/Main.cs index 13a3f49..0abc4fa 100644 --- a/MuteSFX/Main.cs +++ b/MuteSFX/Main.cs @@ -1,7 +1,5 @@ -using ABI_RC.Core.Base; -using MelonLoader; -using System.Reflection; -using HarmonyLib; +using MelonLoader; +using ABI_RC.Systems.GameEventSystem; namespace NAK.MuteSFX; @@ -10,12 +8,12 @@ public class MuteSFX : MelonMod #region Mod Settings internal static MelonLogger.Instance Logger; - internal const string SettingsCategory = nameof(MuteSFX); + private const string SettingsCategory = nameof(MuteSFX); - public static readonly MelonPreferences_Category Category = + private static readonly MelonPreferences_Category Category = MelonPreferences.CreateCategory(SettingsCategory); - public static readonly MelonPreferences_Entry EntryEnabled = + private static readonly MelonPreferences_Entry EntryEnabled = Category.CreateEntry("Enabled", true, description: "Toggle MuteSFX entirely."); #endregion @@ -26,10 +24,8 @@ public class MuteSFX : MelonMod { Logger = LoggerInstance; - HarmonyInstance.Patch( - typeof(AudioManagement).GetMethod(nameof(AudioManagement.SetMicrophoneActive)), - postfix: new HarmonyMethod(typeof(MuteSFX).GetMethod(nameof(OnSetMicrophoneActive_Postfix), BindingFlags.NonPublic | BindingFlags.Static)) - ); + CVRGameEventSystem.Microphone.OnMute.AddListener(() => { OnMicrophoneStatusChanged(false); }); + CVRGameEventSystem.Microphone.OnUnmute.AddListener(() => { OnMicrophoneStatusChanged(true); }); AudioModuleManager.SetupDefaultAudioClips(); } @@ -38,18 +34,10 @@ public class MuteSFX : MelonMod #region Patched Methods - private static void OnSetMicrophoneActive_Postfix(bool active) + private static void OnMicrophoneStatusChanged(bool active) { - try - { - if (EntryEnabled.Value) - AudioModuleManager.PlayAudioModule(active ? AudioModuleManager.sfx_unmute : AudioModuleManager.sfx_mute); - } - catch (Exception e) - { - Logger.Error($"Error during the patched method {nameof(OnSetMicrophoneActive_Postfix)}"); - Logger.Error(e); - } + if (EntryEnabled.Value) + AudioModuleManager.PlayAudioModule(active ? AudioModuleManager.sfx_unmute : AudioModuleManager.sfx_mute); } #endregion diff --git a/MuteSFX/Properties/AssemblyInfo.cs b/MuteSFX/Properties/AssemblyInfo.cs index e106d62..175c9ba 100644 --- a/MuteSFX/Properties/AssemblyInfo.cs +++ b/MuteSFX/Properties/AssemblyInfo.cs @@ -27,6 +27,6 @@ using System.Reflection; namespace NAK.MuteSFX.Properties; internal static class AssemblyInfoParams { - public const string Version = "1.0.1"; + public const string Version = "1.0.2"; public const string Author = "NotAKidoS"; } \ No newline at end of file diff --git a/MuteSFX/format.json b/MuteSFX/format.json index 79e7d0f..2a9fd18 100644 --- a/MuteSFX/format.json +++ b/MuteSFX/format.json @@ -1,7 +1,7 @@ { "_id": 172, "name": "MuteSFX", - "modversion": "1.0.1", + "modversion": "1.0.2", "gameversion": "2023r171", "loaderversion": "0.6.1", "modtype": "Mod", @@ -18,6 +18,6 @@ ], "downloadlink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/releases/download/r14/MuteSFX.dll", "sourcelink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/tree/main/MuteSFX/", - "changelog": "- Fixes for 2023r171.", + "changelog": "- Fixed Push to Talk spamming SFX. [https://github.com/NotAKidOnSteam/NAK_CVR_Mods/issues/29](#29)", "embedcolor": "#92e492" } \ No newline at end of file