Decompiled source of ModelReplacementAPIMirrorDecorFix v1.2.0

ModelReplacementAPIMirrorDecorFix.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ModelReplacementAPIMirrorDecorFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+9f903b65ecb0cac9ff5f393aa6372525fd9b2460")]
[assembly: AssemblyProduct("ModelReplacementAPIMirrorDecorFix")]
[assembly: AssemblyTitle("ModelReplacementAPIMirrorDecorFix")]
[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 ModelReplacementAPIMirrorDecorFix
{
	[BepInPlugin("com.bigboe97.ModelReplacementAPIMirrorDecorFix", "ModelReplacementAPI-MirrorDecor-Fix", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const int armsLayer = 4;

		private Coroutine patchCoroutine;

		private void Awake()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			new Harmony("com.bigboe97.ModelReplacementAPIMirrorDecorFix").PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"ModelReplacementAPIMirrorDecorFix loaded");
			SceneManager.sceneLoaded += OnSceneLoaded;
			Application.logMessageReceived += new LogCallback(OnLogMessageReceived);
			patchCoroutine = ((MonoBehaviour)this).StartCoroutine(TryPatchMirrorScriptRoutine());
		}

		private void OnDestroy()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			SceneManager.sceneLoaded -= OnSceneLoaded;
			Application.logMessageReceived -= new LogCallback(OnLogMessageReceived);
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"New scene loaded, starting to check for mirror camera");
			if (patchCoroutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(patchCoroutine);
			}
			patchCoroutine = ((MonoBehaviour)this).StartCoroutine(TryPatchMirrorScriptRoutine());
		}

		private void OnLogMessageReceived(string condition, string stackTrace, LogType type)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			if ((int)type == 3 && condition.Contains("Instantiating prefab for item Mirror"))
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Mirror instantiation detected, starting to check for mirror camera");
				if (patchCoroutine != null)
				{
					((MonoBehaviour)this).StopCoroutine(patchCoroutine);
				}
				patchCoroutine = ((MonoBehaviour)this).StartCoroutine(TryPatchMirrorScriptRoutine());
			}
		}

		private IEnumerator TryPatchMirrorScriptRoutine()
		{
			Type mirrorScriptType = AccessTools.TypeByName("MirrorDecor.MirrorScript");
			if (mirrorScriptType == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"MirrorScript type not found!");
				yield break;
			}
			bool patched = false;
			bool firstTry = true;
			while (!patched)
			{
				Object[] array = Object.FindObjectsOfType(mirrorScriptType, true);
				foreach (Object obj in array)
				{
					MonoBehaviour val = (MonoBehaviour)(object)((obj is MonoBehaviour) ? obj : null);
					if ((Object)(object)val == (Object)null)
					{
						continue;
					}
					Camera[] componentsInChildren = ((Component)val).GetComponentsInChildren<Camera>(true);
					if (componentsInChildren.Length != 0)
					{
						Camera[] array2 = componentsInChildren;
						foreach (Camera val2 in array2)
						{
							((BaseUnityPlugin)this).Logger.LogInfo((object)("Mirror camera '" + ((Object)val2).name + "' found - attempt patching"));
							int cullingMask = val2.cullingMask & -17;
							val2.cullingMask = cullingMask;
							((BaseUnityPlugin)this).Logger.LogInfo((object)("Mirror camera '" + ((Object)val2).name + "' patched - new cullingMask = " + Convert.ToString(val2.cullingMask, 2).PadLeft(32, '0')));
						}
						patched = true;
					}
				}
				if (!patched)
				{
					if (firstTry)
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)"Mirror camera not found yet, will keep retrying until found");
						firstTry = false;
					}
					yield return (object)new WaitForSeconds(1f);
				}
			}
		}
	}
}