Decompiled source of AudioReplacer v1.7.0

Mods/AudioReplacer.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using AudioImportLib;
using AudioReplacer;
using HarmonyLib;
using MelonLoader;
using MelonLoader.Preferences;
using MelonLoader.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("AudioReplacer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany(null)]
[assembly: AssemblyProduct("AudioReplacer")]
[assembly: AssemblyCopyright("Created by trev")]
[assembly: AssemblyTrademark(null)]
[assembly: ComVisible(false)]
[assembly: AssemblyFileVersion("1.3.0")]
[assembly: NeutralResourcesLanguage("en")]
[assembly: MelonInfo(typeof(Core), "AudioReplacer", "1.3.0", "trev", null)]
[assembly: MelonGame(null, null)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[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 AudioReplacer
{
	public static class BuildInfo
	{
		public const string Name = "AudioReplacer";

		public const string Author = "trev";

		public const string Company = null;

		public const string Version = "1.3.0";

		public const string DownloadLink = null;
	}
	public class Core : MelonMod
	{
		public static Dictionary<string, AudioClip> AudioClips = new Dictionary<string, AudioClip>();

		public static bool LogSounds;

		private static bool overrideEnabled;

		private readonly string customAudioPath = Path.Combine(MelonEnvironment.UserDataDirectory, "CustomAudio");

		private readonly string[] allowedExts = new string[2] { ".wav", ".mp3" };

		public override void OnInitializeMelon()
		{
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Expected O, but got Unknown
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Expected O, but got Unknown
			if (!Directory.Exists(customAudioPath))
			{
				Directory.CreateDirectory(customAudioPath);
			}
			MelonPreferences_Category val = MelonPreferences.CreateCategory("AudioReplacer", "");
			val.CreateEntry<bool>("LogSounds", false, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
			LogSounds = val.GetEntry<bool>("LogSounds").Value;
			MelonPreferences.Save();
			string[] files = Directory.GetFiles(customAudioPath, "*", SearchOption.AllDirectories);
			string text = files.SingleOrDefault((string f) => Path.GetFileNameWithoutExtension(f) == "REPLACE_ALL");
			if (text == null)
			{
				string[] array = files;
				foreach (string text2 in array)
				{
					if (allowedExts.Contains<string>(Path.GetExtension(text2)))
					{
						AudioClip val2 = API.LoadAudioClip(text2, true);
						AudioClips.Add(((Object)val2).name, val2);
						((MelonBase)this).LoggerInstance.Msg("Added: " + ((Object)val2).name);
					}
				}
			}
			else
			{
				AudioClip val3 = API.LoadAudioClip(text, true);
				AudioClips.Add(((Object)val3).name, val3);
				overrideEnabled = true;
				((MelonBase)this).LoggerInstance.Msg("Added override: " + ((Object)val3).name);
			}
			((MelonBase)this).HarmonyInstance.Patch((MethodBase)AccessTools.Method(typeof(AudioSource), "Play", Array.Empty<Type>(), (Type[])null), new HarmonyMethod(typeof(Core).GetMethod("AudioPlayPatch")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((MelonBase)this).HarmonyInstance.Patch((MethodBase)AccessTools.Method(typeof(AudioSource), "Play", new Type[1] { typeof(ulong) }, (Type[])null), new HarmonyMethod(typeof(Core).GetMethod("AudioPlayPatch")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		public static void AudioPlayPatch(AudioSource __instance)
		{
			if (!((Object)(object)__instance.clip == (Object)null))
			{
				if (LogSounds)
				{
					MelonLogger.Msg($"Playing \"{((Object)__instance.clip).name}\" from object \"{((Object)((Component)__instance).gameObject).name}\"");
				}
				string key = (overrideEnabled ? "REPLACE_ALL" : ((Object)__instance.clip).name);
				if (AudioClips.TryGetValue(key, out var value))
				{
					__instance.pitch = 1f;
					__instance.clip = value;
				}
			}
		}
	}
}