Decompiled source of MirageFix v1.0.0

plugins/ElfDevelop.MirageFix.dll

Decompiled 11 hours ago
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Threading;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirage.Core.Audio;
using NAudio.Wave;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[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 MirageFix
{
	[ContentWarningPlugin("ElfDevelop.MirageFix", "1.0.0", true)]
	[BepInPlugin("ElfDevelop.MirageFix", "MirageFix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class MirageFixPlugin : BaseUnityPlugin
	{
		private const int WaitMilliseconds = 10000;

		private static readonly ConcurrentQueue<Action> mainThreadWork = new ConcurrentQueue<Action>();

		private static int mainThreadId;

		private static int moved;

		internal static ManualLogSource Logger { get; private set; } = null;

		private void Awake()
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			mainThreadId = Thread.CurrentThread.ManagedThreadId;
			new Harmony("ElfDevelop.MirageFix").Patch((MethodBase)AccessTools.Method(typeof(Format), "convertToAudioClip", (Type[])null, (Type[])null), new HarmonyMethod(AccessTools.Method(typeof(MirageFixPlugin), "ConvertOnMainThread", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			Logger.LogInfo((object)"MirageFix 1.0.0 has loaded!");
		}

		private void Update()
		{
			Action result;
			while (mainThreadWork.TryDequeue(out result))
			{
				result();
			}
		}

		private static bool ConvertOnMainThread(WaveFileReader audioReader, ref AudioClip __result)
		{
			if (Thread.CurrentThread.ManagedThreadId == mainThreadId)
			{
				return true;
			}
			ISampleProvider obj = WaveExtensionMethods.ToSampleProvider((IWaveProvider)(object)audioReader);
			float[] samples = new float[(int)audioReader.SampleCount];
			obj.Read(samples, 0, samples.Length);
			int channels = ((WaveStream)audioReader).WaveFormat.Channels;
			int sampleRate = ((WaveStream)audioReader).WaveFormat.SampleRate;
			AudioClip clip = null;
			Exception error = null;
			ManualResetEventSlim done = new ManualResetEventSlim();
			mainThreadWork.Enqueue(delegate
			{
				try
				{
					clip = AudioClip.Create("qwbarch.Mirage", samples.Length, channels, sampleRate, false);
					clip.SetData(samples, 0);
				}
				catch (Exception ex)
				{
					error = ex;
				}
				finally
				{
					done.Set();
				}
			});
			if (!done.Wait(10000))
			{
				throw new TimeoutException("MirageFix: the main thread did not make the mimic's audio clip in time");
			}
			done.Dispose();
			if (error != null)
			{
				throw error;
			}
			if (moved++ == 0)
			{
				Logger.LogInfo((object)"Made a mimic's audio clip on the main thread instead of a background thread");
			}
			__result = clip;
			return false;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}