Decompiled source of MimicVoiceFix v1.0.2

MimicVoiceFix.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using HarmonyLib;
using MelonLoader;
using Microsoft.CodeAnalysis;
using MimicVoiceFix;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(MimicVoiceFixMod), "MimicVoiceFix", "1.0.2", "User", null)]
[assembly: AssemblyCompany("MimicVoiceFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MimicVoiceFix")]
[assembly: AssemblyTitle("MimicVoiceFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace MimicVoiceFix
{
	public class MimicVoiceFixMod : MelonMod
	{
		public override void OnInitializeMelon()
		{
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Expected O, but got Unknown
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Expected O, but got Unknown
			MelonLogger.Msg("MimicVoiceFix v1.0.2 loaded! Analyzing game assemblies...");
			try
			{
				Type voiceEffectType = AccessTools.TypeByName("Mimic.Audio.VoiceEffectPreset");
				if ((object)voiceEffectType == null)
				{
					MelonLogger.Error("Could not find Mimic.Audio.VoiceEffectPreset type!");
					return;
				}
				MelonLogger.Msg("Found VoiceEffectPreset type. Available methods:");
				MethodInfo[] methods = voiceEffectType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				MethodInfo[] array = methods;
				foreach (MethodInfo methodInfo in array)
				{
					string text = string.Join(", ", (from p in methodInfo.GetParameters()
						select p.ParameterType.Name + " " + p.Name).ToArray());
					MelonLogger.Msg("  - " + methodInfo.ReturnType.Name + " " + methodInfo.Name + "(" + text + ")");
				}
				MethodInfo[] array2 = (from m in methods.Where((MethodInfo m) => (object)m.DeclaringType == voiceEffectType).ToArray()
					where m.Name.IndexOf("LowPass", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("HighPass", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Distortion", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Chorus", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Reverb", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Echo", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Amplifier", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Apply", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Filter", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("SetDirty", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("MarkDirty", StringComparison.OrdinalIgnoreCase) >= 0
					select m).ToArray();
				if (array2.Length == 0)
				{
					array2 = methods.Where((MethodInfo m) => m.Name.IndexOf("LowPass", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("HighPass", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Distortion", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Chorus", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Reverb", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Echo", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Amplifier", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Apply", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("Filter", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("SetDirty", StringComparison.OrdinalIgnoreCase) >= 0 || m.Name.IndexOf("MarkDirty", StringComparison.OrdinalIgnoreCase) >= 0).ToArray();
				}
				if (array2.Length == 0 && methods.Length != 0)
				{
					array2 = new MethodInfo[1] { methods[0] };
				}
				if (array2.Length != 0)
				{
					Harmony val = new Harmony("com.mimicvoicefix.patch");
					MethodInfo method = typeof(VoiceEffectPresetPatch).GetMethod("GenericPostfix", BindingFlags.Static | BindingFlags.Public);
					array = array2;
					foreach (MethodInfo methodInfo2 in array)
					{
						MelonLogger.Msg("Patching method: " + methodInfo2.Name);
						val.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, new HarmonyMethod(method), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					}
					MelonLogger.Msg($"Patched {array2.Length} method(s).");
				}
				else
				{
					MelonLogger.Warning("Could not find any method to patch. Voice effects may not be disabled.");
				}
			}
			catch (Exception arg)
			{
				MelonLogger.Error($"Error during initialization: {arg}");
			}
		}
	}
	public class VoiceEffectPresetPatch
	{
		public static void GenericPostfix(object __instance, params object[] __args)
		{
			try
			{
				DisableFiltersOnObject(__instance);
				if (__args != null)
				{
					for (int i = 0; i < __args.Length; i++)
					{
						DisableFiltersOnObject(__args[i]);
					}
				}
			}
			catch (Exception arg)
			{
				MelonLogger.Error($"Error in voice effect patch: {arg}");
			}
		}

		private static void DisableFiltersOnObject(object target)
		{
			if (target != null)
			{
				Type type = target.GetType();
				DisableFilter(target, type, "LowPass");
				DisableFilter(target, type, "HighPass");
				DisableFilter(target, type, "Distortion");
				DisableFilter(target, type, "Chorus");
				DisableFilter(target, type, "Reverb");
				DisableFilter(target, type, "Echo");
				DisableFilter(target, type, "Amplifier");
			}
		}

		private static void DisableFilter(object filterRef, Type filterType, string propertyName)
		{
			try
			{
				PropertyInfo property = filterType.GetProperty(propertyName);
				if ((object)property != null)
				{
					object value = property.GetValue(filterRef, null);
					value?.GetType().GetProperty("enabled")?.SetValue(value, false, null);
				}
			}
			catch
			{
			}
		}
	}
}