Decompiled source of BepInExResoniteShim v0.9.0

plugins/BepInExResoniteShim/BepInExResoniteShim.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.NET.Common;
using Elements.Core;
using FrooxEngine;
using HarmonyLib;
using Renderite.Shared;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: TargetFramework(".NETCoreApp,Version=v9.0", FrameworkDisplayName = ".NET 9.0")]
[assembly: AssemblyCompany("ResoniteModding")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.9.0.0")]
[assembly: AssemblyInformationalVersion("0.9.0+565b6e607df9bb1bd01d8f5b92782b912d7012e4")]
[assembly: AssemblyProduct("BepInEx Resonite Shim")]
[assembly: AssemblyTitle("BepInExResoniteShim")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/ResoniteModding/BepInExResoniteShim")]
[assembly: AssemblyVersion("0.9.0.0")]
[module: RefSafetyRules(11)]
namespace BepInExResoniteShim;

public class ResonitePlugin : BepInPlugin
{
	[field: CompilerGenerated]
	public string Author
	{
		[CompilerGenerated]
		get;
		[CompilerGenerated]
		protected set;
	}

	[field: CompilerGenerated]
	public string Link
	{
		[CompilerGenerated]
		get;
		[CompilerGenerated]
		protected set;
	}

	public ResonitePlugin(string GUID, string Name, string Version, string Author, string Link)
		: base(GUID, Name, Version)
	{
		this.Author = Author;
		this.Link = Link;
	}
}
[ResonitePlugin("ResoniteModding.BepInExResoniteShim", "BepInEx Resonite Shim", "0.9.0", "ResoniteModding", "https://github.com/ResoniteModding/BepInExResoniteShim")]
internal class BepInExResoniteShim : BasePlugin
{
	[HarmonyPatch]
	private class LocationFixer
	{
		[CompilerGenerated]
		private sealed class <Transpiler>d__1 : global::System.Collections.Generic.IEnumerable<CodeInstruction>, global::System.Collections.IEnumerable, global::System.Collections.Generic.IEnumerator<CodeInstruction>, global::System.Collections.IEnumerator, global::System.IDisposable
		{
			private int <>1__state;

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private global::System.Collections.Generic.IEnumerable<CodeInstruction> codes;

			public global::System.Collections.Generic.IEnumerable<CodeInstruction> <>3__codes;

			private int <killCount>5__2;

			private MethodInfo <cachePath>5__3;

			private MethodInfo <asmLoc>5__4;

			private global::System.Collections.Generic.IEnumerator<CodeInstruction> <>7__wrap4;

			private CodeInstruction <code>5__6;

			CodeInstruction global::System.Collections.Generic.IEnumerator<CodeInstruction>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object global::System.Collections.IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <Transpiler>d__1(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

			[DebuggerHidden]
			void global::System.IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || (uint)(num - 1) <= 3u)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<cachePath>5__3 = null;
				<asmLoc>5__4 = null;
				<>7__wrap4 = null;
				<code>5__6 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0187: Unknown result type (might be due to invalid IL or missing references)
				//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
				//IL_01ac: Expected O, but got Unknown
				//IL_0163: Unknown result type (might be due to invalid IL or missing references)
				//IL_0169: Unknown result type (might be due to invalid IL or missing references)
				//IL_0173: Expected O, but got Unknown
				//IL_00da: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ff: Expected O, but got Unknown
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<killCount>5__2 = 0;
						<cachePath>5__3 = AccessTools.PropertyGetter(typeof(GlobalTypeRegistry), "MetadataCachePath");
						<asmLoc>5__4 = AccessTools.PropertyGetter(typeof(Assembly), "Location");
						<>7__wrap4 = codes.GetEnumerator();
						<>1__state = -3;
						break;
					case 1:
						<>1__state = -3;
						<killCount>5__2 = 2;
						goto IL_0140;
					case 2:
						<>1__state = -3;
						goto IL_0140;
					case 3:
						<>1__state = -3;
						<>2__current = new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(LocationFixer), "ProcessCachePath", (global::System.Type[])null, (global::System.Type[])null));
						<>1__state = 4;
						return true;
					case 4:
						{
							<>1__state = -3;
							goto IL_01bf;
						}
						IL_0140:
						if (CodeInstructionExtensions.Calls(<code>5__6, <cachePath>5__3))
						{
							Log.LogDebug((object)"Patched CachePath");
							<>2__current = new CodeInstruction(OpCodes.Ldarg_1, (object)null);
							<>1__state = 3;
							return true;
						}
						goto IL_01bf;
						IL_01bf:
						<code>5__6 = null;
						break;
					}
					while (((global::System.Collections.IEnumerator)<>7__wrap4).MoveNext())
					{
						<code>5__6 = <>7__wrap4.Current;
						if (<killCount>5__2 > 0)
						{
							<killCount>5__2--;
							continue;
						}
						if (CodeInstructionExtensions.Calls(<code>5__6, <asmLoc>5__4))
						{
							Log.LogDebug((object)"Patched AsmLoc");
							<>2__current = new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(LocationFixer), "ProcessCacheTime", (global::System.Type[])null, (global::System.Type[])null));
							<>1__state = 1;
							return true;
						}
						<>2__current = <code>5__6;
						<>1__state = 2;
						return true;
					}
					<>m__Finally1();
					<>7__wrap4 = null;
					return false;
				}
				catch
				{
					//try-fault
					((global::System.IDisposable)this).Dispose();
					throw;
				}
			}

			bool global::System.Collections.IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			private void <>m__Finally1()
			{
				<>1__state = -1;
				if (<>7__wrap4 != null)
				{
					((global::System.IDisposable)<>7__wrap4).Dispose();
				}
			}

			[DebuggerHidden]
			void global::System.Collections.IEnumerator.Reset()
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				throw new NotSupportedException();
			}

			[DebuggerHidden]
			global::System.Collections.Generic.IEnumerator<CodeInstruction> global::System.Collections.Generic.IEnumerable<CodeInstruction>.GetEnumerator()
			{
				<Transpiler>d__1 <Transpiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<Transpiler>d__ = this;
				}
				else
				{
					<Transpiler>d__ = new <Transpiler>d__1(0);
				}
				<Transpiler>d__.codes = <>3__codes;
				return <Transpiler>d__;
			}

			[DebuggerHidden]
			global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator()
			{
				return (global::System.Collections.IEnumerator)((global::System.Collections.Generic.IEnumerable<CodeInstruction>)this).GetEnumerator();
			}
		}

		public static MethodBase TargetMethod()
		{
			return (MethodBase)(object)AccessTools.FirstConstructor(typeof(AssemblyTypeRegistry), (Func<ConstructorInfo, bool>)((ConstructorInfo x) => ((MethodBase)x).GetParameters().Length > 3));
		}

		[IteratorStateMachine(typeof(<Transpiler>d__1))]
		public static global::System.Collections.Generic.IEnumerable<CodeInstruction> Transpiler(global::System.Collections.Generic.IEnumerable<CodeInstruction> codes)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <Transpiler>d__1(-2)
			{
				<>3__codes = codes
			};
		}

		public static string? ProcessCachePath(string cachePath, Assembly asm)
		{
			if (string.IsNullOrWhiteSpace(asm.Location))
			{
				return null;
			}
			return cachePath;
		}

		public static global::System.DateTime ProcessCacheTime(Assembly asm)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			if (string.IsNullOrWhiteSpace(asm.Location))
			{
				return global::System.DateTime.UtcNow;
			}
			return ((FileSystemInfo)new FileInfo(asm.Location)).LastWriteTimeUtc;
		}
	}

	[HarmonyPatch(/*Could not decode attribute arguments.*/)]
	private class AssemblyLoadFixer
	{
		[CompilerGenerated]
		private sealed class <Transpiler>d__0 : global::System.Collections.Generic.IEnumerable<CodeInstruction>, global::System.Collections.IEnumerable, global::System.Collections.Generic.IEnumerator<CodeInstruction>, global::System.Collections.IEnumerator, global::System.IDisposable
		{
			private int <>1__state;

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private global::System.Collections.Generic.IEnumerable<CodeInstruction> codes;

			public global::System.Collections.Generic.IEnumerable<CodeInstruction> <>3__codes;

			private MethodInfo <loadFrom>5__2;

			private global::System.Collections.Generic.IEnumerator<CodeInstruction> <>7__wrap2;

			CodeInstruction global::System.Collections.Generic.IEnumerator<CodeInstruction>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object global::System.Collections.IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <Transpiler>d__0(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

			[DebuggerHidden]
			void global::System.IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || (uint)(num - 1) <= 1u)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<loadFrom>5__2 = null;
				<>7__wrap2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_008b: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b0: Expected O, but got Unknown
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<loadFrom>5__2 = AccessTools.Method(typeof(Assembly), "LoadFrom", new global::System.Type[1] { typeof(string) }, (global::System.Type[])null);
						<>7__wrap2 = codes.GetEnumerator();
						<>1__state = -3;
						break;
					case 1:
						<>1__state = -3;
						break;
					case 2:
						<>1__state = -3;
						break;
					}
					if (((global::System.Collections.IEnumerator)<>7__wrap2).MoveNext())
					{
						CodeInstruction current = <>7__wrap2.Current;
						if (CodeInstructionExtensions.Calls(current, <loadFrom>5__2))
						{
							<>2__current = new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(AssemblyLoadFixer), "LoadFrom", (global::System.Type[])null, (global::System.Type[])null));
							<>1__state = 1;
							return true;
						}
						<>2__current = current;
						<>1__state = 2;
						return true;
					}
					<>m__Finally1();
					<>7__wrap2 = null;
					return false;
				}
				catch
				{
					//try-fault
					((global::System.IDisposable)this).Dispose();
					throw;
				}
			}

			bool global::System.Collections.IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			private void <>m__Finally1()
			{
				<>1__state = -1;
				if (<>7__wrap2 != null)
				{
					((global::System.IDisposable)<>7__wrap2).Dispose();
				}
			}

			[DebuggerHidden]
			void global::System.Collections.IEnumerator.Reset()
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				throw new NotSupportedException();
			}

			[DebuggerHidden]
			global::System.Collections.Generic.IEnumerator<CodeInstruction> global::System.Collections.Generic.IEnumerable<CodeInstruction>.GetEnumerator()
			{
				<Transpiler>d__0 <Transpiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<Transpiler>d__ = this;
				}
				else
				{
					<Transpiler>d__ = new <Transpiler>d__0(0);
				}
				<Transpiler>d__.codes = <>3__codes;
				return <Transpiler>d__;
			}

			[DebuggerHidden]
			global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator()
			{
				return (global::System.Collections.IEnumerator)((global::System.Collections.Generic.IEnumerable<CodeInstruction>)this).GetEnumerator();
			}
		}

		[IteratorStateMachine(typeof(<Transpiler>d__0))]
		public static global::System.Collections.Generic.IEnumerable<CodeInstruction> Transpiler(global::System.Collections.Generic.IEnumerable<CodeInstruction> codes)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <Transpiler>d__0(-2)
			{
				<>3__codes = codes
			};
		}

		public static Assembly? LoadFrom(string path)
		{
			Log.LogDebug((object)("Bypassing LoadFrom: " + path));
			return null;
		}
	}

	[HarmonyPatchCategory("WindowTitlePatcher")]
	[HarmonyPatch(typeof(RendererInitData), "Pack")]
	private class WindowTitlePatcher
	{
		private static string? GetBepisLoaderVersion()
		{
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Expected O, but got Unknown
			try
			{
				Assembly val = Enumerable.FirstOrDefault<Assembly>((global::System.Collections.Generic.IEnumerable<Assembly>)AppDomain.CurrentDomain.GetAssemblies(), (Func<Assembly, bool>)((Assembly a) => a.GetName().Name == "BepisLoader"));
				if (val != (Assembly)null)
				{
					Version version = val.GetName().Version;
					if (version != (Version)null)
					{
						return $" v{version.Major}.{version.Minor}.{version.Build}";
					}
				}
			}
			catch (global::System.Exception ex)
			{
				ManualLogSource log = Log;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(35, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Failed to get BepisLoader version: ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
				}
				log.LogWarning(val2);
			}
			return null;
		}

		public static void Prefix(RendererInitData __instance)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			if (ShowWatermark.Value && __instance.windowTitle == "Resonite")
			{
				string bepisLoaderVersion = GetBepisLoaderVersion();
				string text = (__instance.windowTitle = "Resonite - BepisLoader" + bepisLoaderVersion);
				ManualLogSource log = Log;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(38, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Successfully patched window title to: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text);
				}
				log.LogInfo(val);
			}
		}
	}

	internal static ManualLogSource Log;

	private static ConfigEntry<bool> ShowWatermark;

	public override void Load()
	{
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_010f: Expected O, but got Unknown
		//IL_009b: Expected O, but got Unknown
		Log = ((BasePlugin)this).Log;
		ShowWatermark = ((BasePlugin)this).Config.Bind<bool>("General", "ShowWatermark", true, "Shows 'BepisLoader' watermark in the window title");
		global::System.Type type2 = null;
		try
		{
			global::System.Type[] types = GenericTypesAttribute.GetTypes((Group)1);
			foreach (global::System.Type type3 in types)
			{
				type2 = type3;
				if (!TomlTypeConverter.CanConvert(type3))
				{
					TomlTypeConverter.AddConverter(type3, new TypeConverter
					{
						ConvertToString = (object obj, global::System.Type type) => (string)((MethodBase)typeof(Coder<>).MakeGenericType(new global::System.Type[1] { type }).GetMethod("EncodeToString")).Invoke((object)null, new object[1] { obj }),
						ConvertToObject = (string str, global::System.Type type) => ((MethodBase)typeof(Coder<>).MakeGenericType(new global::System.Type[1] { type }).GetMethod("DecodeFromString")).Invoke((object)null, new object[1] { str })
					});
				}
			}
			type2 = typeof(dummy);
			TomlTypeConverter.AddConverter(typeof(dummy), new TypeConverter
			{
				ConvertToString = (object _, global::System.Type _) => "dummy",
				ConvertToObject = (string _, global::System.Type _) => (object)default(dummy)
			});
		}
		catch (global::System.Exception ex)
		{
			Log.LogError((object)("Failed to register generic type converters (Last attempted = " + (((object)type2)?.ToString() ?? "NULL") + "): " + (object)ex));
		}
		RunPatches();
	}

	private void RunPatches()
	{
		((BasePlugin)this).HarmonyInstance.PatchAllUncategorized();
		((BasePlugin)this).HarmonyInstance.SafePatchCategory("GraphicalClientPatch");
		((BasePlugin)this).HarmonyInstance.SafePatchCategory("WindowTitlePatcher");
		((BasePlugin)this).HarmonyInstance.SafePatchCategory("LogAlerter");
	}
}
internal static class HarmonyExtensions
{
	[field: CompilerGenerated]
	public static bool AnyPatchFailed
	{
		[CompilerGenerated]
		get;
		[CompilerGenerated]
		private set;
	}

	public static void SafePatchCategory(this Harmony instance, string categoryName)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Expected O, but got Unknown
		try
		{
			instance.PatchCategory(categoryName);
		}
		catch (global::System.Exception ex)
		{
			ManualLogSource log = BepInExResoniteShim.Log;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(18, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Failed to patch ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(categoryName);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<global::System.Exception>(ex);
			}
			log.LogError(val);
			AnyPatchFailed = true;
		}
	}
}
[HarmonyPatchCategory("GraphicalClientPatch")]
[HarmonyPatch(/*Could not decode attribute arguments.*/)]
internal class GraphicalClientPatch
{
	public static void Postfix(ref string ___AssemblyDirectory)
	{
		___AssemblyDirectory = Paths.GameRootPath;
	}
}
[HarmonyPatchCategory("LogAlerter")]
[HarmonyPatch(typeof(UniLog), "add_OnLog")]
internal class LogAlerter
{
	private static void Postfix(Action<string> value)
	{
		if (HarmonyExtensions.AnyPatchFailed)
		{
			value.Invoke("[BepisLoader] BepInExResoniteShim partially loaded.");
		}
		else
		{
			value.Invoke("[BepisLoader] BepInExResoniteShim loaded successfully.");
		}
	}
}
public static class PluginMetadata
{
	public const string GUID = "ResoniteModding.BepInExResoniteShim";

	public const string NAME = "BepInEx Resonite Shim";

	public const string VERSION = "0.9.0";

	public const string AUTHORS = "ResoniteModding";

	public const string REPOSITORY_URL = "https://github.com/ResoniteModding/BepInExResoniteShim";
}