Decompiled source of BetterMirrors v1.0.1

Mods/BetterMirrors.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BetterMirrors;
using BoneLib;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppPuppetMasta;
using Il2CppSLZ.Bonelab;
using Il2CppSLZ.Interaction;
using Il2CppSLZ.Marrow;
using Il2CppSLZ.Marrow.Combat;
using Il2CppSLZ.Marrow.Interaction;
using Il2CppSLZ.Marrow.Pool;
using Il2CppSLZ.Marrow.Warehouse;
using Il2CppSLZ.Marrow.Zones;
using LabFusion.MonoBehaviours;
using MelonLoader;
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: ComVisible(false)]
[assembly: Guid("61d95d89-1cce-472c-a72c-cf5b83651091")]
[assembly: MelonInfo(typeof(global::BetterMirrors.BetterMirrors), "BetterMirrors", "1.0.0", "EvroDev", null)]
[assembly: MelonGame(null, null)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("BetterMirrors")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BetterMirrors")]
[assembly: AssemblyTitle("BetterMirrors")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace BetterMirrors
{
	public static class BuildInfo
	{
		public const string Name = "BetterMirrors";

		public const string Author = "EvroDev";

		public const string Company = null;

		public const string Version = "1.0.0";

		public const string DownloadLink = null;
	}
	public class BetterMirrors : MelonMod
	{
		public static List<Type> SyncTypes = new List<Type>();

		public static Assembly ass;

		public override void OnInitializeMelon()
		{
			Hooking.OnLevelLoaded += Hooking_OnLevelLoaded;
			ass = ((MelonBase)this).MelonAssembly.Assembly;
			Type[] types = ass.GetTypes();
			foreach (Type type in types)
			{
				if ((!type.Name.Contains("Mono") || !type.Name.Contains("Security")) && type.InheritsFromGenericType(typeof(MirrorSyncable<>)) && !type.IsInterface && !type.IsAbstract && !type.Equals(typeof(MirrorSyncable<>)))
				{
					try
					{
						SyncTypes.Add(type);
					}
					catch (Exception)
					{
					}
				}
			}
		}

		private void Hooking_OnLevelLoaded(LevelInfo obj)
		{
			foreach (Mirror item in Object.FindObjectsOfType<Mirror>())
			{
				MirrorExtension.ExtendMirror(item);
			}
		}
	}
	public static class TypeExtensions
	{
		public static bool InheritsFromGenericType(this Type type, Type genericType)
		{
			if (type == null || genericType == null)
			{
				return false;
			}
			if (!genericType.IsGenericTypeDefinition)
			{
				throw new ArgumentException("The specified genericType must be a generic type definition.");
			}
			Type[] interfaces = type.GetInterfaces();
			foreach (Type type2 in interfaces)
			{
				if (type2.IsGenericType && type2.GetGenericTypeDefinition() == genericType)
				{
					return true;
				}
			}
			Type type3 = type;
			while (type3 != null && type3 != typeof(object))
			{
				if (type3.IsGenericType && type3.GetGenericTypeDefinition() == genericType)
				{
					return true;
				}
				type3 = type3.BaseType;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(Mirror), "OnTriggerEnter")]
	public static class MirrorEnterRegister
	{
		public static void Postfix(Mirror __instance)
		{
			if ((Object)(object)((Component)__instance).GetComponentInChildren<MirrorExtension>() == (Object)null && (Object)(object)((Component)__instance).GetComponentInParent<DestroyOnDisconnect>() == (Object)null)
			{
				MirrorExtension.ExtendMirror(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(ZoneEvents), "OnEnter")]
	public static class ZoneEnterStupidHookingShit
	{
		public static void Postfix(ZoneEvents __instance, MarrowEntity activatorEntity)
		{
			MirrorExtension mirrorExtension = default(MirrorExtension);
			if (((Component)__instance).TryGetComponent<MirrorExtension>(ref mirrorExtension))
			{
				mirrorExtension.OnEnter(activatorEntity);
			}
		}
	}
	[HarmonyPatch(typeof(ZoneEvents), "OnExit")]
	public static class ZoneEnterStupidHookingShit2
	{
		public static void Postfix(ZoneEvents __instance, MarrowEntity activatorEntity)
		{
			MirrorExtension mirrorExtension = default(MirrorExtension);
			if (((Component)__instance).TryGetComponent<MirrorExtension>(ref mirrorExtension))
			{
				mirrorExtension.OnExit(activatorEntity);
			}
		}
	}
	[RegisterTypeInIl2Cpp]
	public class MirrorExtension : MonoBehaviour
	{
		public Mirror mirror;

		public Dictionary<MarrowEntity, GameObject> EntityMirroredPairs = new Dictionary<MarrowEntity, GameObject>();

		public Dictionary<GameObject, GameObject> BodyMirroredPairs = new Dictionary<GameObject, GameObject>();

		public Dictionary<MarrowEntity, List<IMirrorSyncable>> EntityMirrorSyncs = new Dictionary<MarrowEntity, List<IMirrorSyncable>>();

		public MirrorExtension(IntPtr ptr)
			: base(ptr)
		{
		}

		private void OnEnable()
		{
			ClearReflections();
			if ((Object)(object)((Component)this).GetComponentInParent<DestroyOnDisconnect>() != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
			}
		}

		public void ClearReflections()
		{
			foreach (GameObject value in EntityMirroredPairs.Values)
			{
				Object.Destroy((Object)(object)value);
			}
			EntityMirroredPairs = new Dictionary<MarrowEntity, GameObject>();
			BodyMirroredPairs = new Dictionary<GameObject, GameObject>();
			EntityMirrorSyncs = new Dictionary<MarrowEntity, List<IMirrorSyncable>>();
		}

		public static void ExtendMirror(Mirror m)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)((Component)m).GetComponentInChildren<MirrorExtension>() != (Object)null) && !((Object)(object)((Component)m).GetComponentInParent<DestroyOnDisconnect>() != (Object)null))
			{
				GameObject val = new GameObject("Mirror Extension");
				val.transform.parent = ((Component)m).transform;
				val.transform.localPosition = Vector3.zero;
				val.transform.localRotation = Quaternion.identity;
				val.transform.localScale = Vector3.one;
				val.layer = 29;
				BoxCollider val2 = val.AddComponent<BoxCollider>();
				((Collider)val2).isTrigger = true;
				val2.center = ((Component)m).GetComponent<BoxCollider>().center;
				val2.extents = ((Component)m).GetComponent<BoxCollider>().extents;
				Zone zone = val.AddComponent<Zone>();
				ZoneEvents val3 = val.AddComponent<ZoneEvents>();
				((ZoneItem)val3)._zone = zone;
				((ZoneItem)val3).OnEnable();
				MirrorExtension mirrorExtension = val.AddComponent<MirrorExtension>();
				mirrorExtension.mirror = m;
			}
		}

		private void Update()
		{
			foreach (MarrowEntity key in EntityMirroredPairs.Keys)
			{
				UpdateEntityReflection(key);
			}
		}

		public void UpdateEntityReflection(MarrowEntity entity)
		{
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)entity == (Object)null || (Object)(object)EntityMirroredPairs[entity] == (Object)null || !EntityMirroredPairs[entity].active)
			{
				return;
			}
			Transform transform = EntityMirroredPairs[entity].transform;
			ReflectOverTransformXYPlane(((Component)entity).transform, transform, mirror._reflectTran);
			foreach (MarrowBody item in (Il2CppArrayBase<MarrowBody>)(object)entity.Bodies)
			{
				if (!((Object)(object)((Component)item).gameObject == (Object)(object)((Component)entity).gameObject) && BodyMirroredPairs.ContainsKey(((Component)item).gameObject))
				{
					Transform transform2 = BodyMirroredPairs[((Component)item).gameObject].transform;
					transform2.localRotation = ((Component)item).transform.localRotation;
					transform2.localPosition = ((Component)item).transform.localPosition;
				}
			}
			if (!EntityMirrorSyncs.ContainsKey(entity))
			{
				return;
			}
			foreach (IMirrorSyncable item2 in EntityMirrorSyncs[entity])
			{
				item2?.Sync();
			}
		}

		public void OnEnter(MarrowEntity entity)
		{
			//IL_03ae: Unknown result type (might be due to invalid IL or missing references)
			if (!ShouldInstantiate(entity))
			{
				return;
			}
			if (EntityMirroredPairs.ContainsKey(entity))
			{
				EntityMirroredPairs[entity].SetActive(true);
				return;
			}
			GameObject val = Object.Instantiate<GameObject>(((Component)entity).gameObject);
			EntityMirroredPairs.Add(entity, val);
			for (int i = 0; i < ((Il2CppArrayBase<MarrowBody>)(object)entity.Bodies).Count; i++)
			{
				MarrowBody val2 = ((Il2CppArrayBase<MarrowBody>)(object)entity.Bodies)[i];
				MarrowBody val3 = ((Il2CppArrayBase<MarrowBody>)(object)val.GetComponent<MarrowEntity>().Bodies)[i];
				BodyMirroredPairs.Add(((Component)val2).gameObject, ((Component)val3).gameObject);
			}
			foreach (Tracker componentsInChild in val.GetComponentsInChildren<Tracker>())
			{
				Object.Destroy((Object)(object)((Component)componentsInChild).gameObject);
			}
			foreach (MarrowBody componentsInChild2 in val.GetComponentsInChildren<MarrowBody>())
			{
				Object.Destroy((Object)(object)componentsInChild2);
			}
			foreach (MarrowEntity componentsInChild3 in val.GetComponentsInChildren<MarrowEntity>())
			{
				Object.Destroy((Object)(object)componentsInChild3);
			}
			foreach (Poolee componentsInChild4 in val.GetComponentsInChildren<Poolee>())
			{
				Object.Destroy((Object)(object)componentsInChild4);
			}
			foreach (Grip componentsInChild5 in val.GetComponentsInChildren<Grip>())
			{
				Object.Destroy((Object)(object)componentsInChild5);
			}
			foreach (ForcePullGrip componentsInChild6 in val.GetComponentsInChildren<ForcePullGrip>())
			{
				Object.Destroy((Object)(object)componentsInChild6);
			}
			foreach (Collider componentsInChild7 in val.GetComponentsInChildren<Collider>())
			{
				Object.Destroy((Object)(object)componentsInChild7);
			}
			foreach (InteractableIcon componentsInChild8 in val.GetComponentsInChildren<InteractableIcon>())
			{
				Object.Destroy((Object)(object)componentsInChild8);
			}
			List<IMirrorSyncable> list = new List<IMirrorSyncable>();
			foreach (Type syncType in BetterMirrors.SyncTypes)
			{
				try
				{
					IMirrorSyncable mirrorSyncable = Activator.CreateInstance(syncType) as IMirrorSyncable;
					Component[] componentsInChildrenOfMyType = mirrorSyncable.GetComponentsInChildrenOfMyType(((Component)entity).gameObject);
					Component[] componentsInChildrenOfMyType2 = mirrorSyncable.GetComponentsInChildrenOfMyType(val);
					for (int j = 0; j < componentsInChildrenOfMyType.Length; j++)
					{
						Component source = componentsInChildrenOfMyType[j];
						Component target = componentsInChildrenOfMyType2[j];
						IMirrorSyncable mirrorSyncable2 = Activator.CreateInstance(syncType) as IMirrorSyncable;
						mirrorSyncable2.Setup(source, target);
						list.Add(mirrorSyncable2);
					}
				}
				catch (Exception)
				{
				}
			}
			if (list.Count > 0)
			{
				EntityMirrorSyncs.Add(entity, list);
			}
			foreach (Rigidbody componentsInChild9 in val.GetComponentsInChildren<Rigidbody>())
			{
				componentsInChild9.isKinematic = true;
			}
			val.transform.localScale = new Vector3(-1f, 1f, 1f);
		}

		public void OnExit(MarrowEntity entity)
		{
			if (EntityMirroredPairs.ContainsKey(entity))
			{
				EntityMirroredPairs[entity].SetActive(false);
			}
		}

		private void OnEntityDespawned(GameObject entity)
		{
		}

		public bool ShouldInstantiate(MarrowEntity entity)
		{
			if (entity.Tags.Tags.Count == 0)
			{
				return true;
			}
			if (entity.Tags.Tags.Count == 1 && ((ScannableReference)entity.Tags.Tags[0]).Barcode.ID == "SLZ.Marrow.BoneTag.Being")
			{
				return true;
			}
			return false;
		}

		public void ReflectOverTransformXYPlane(Transform original, Transform target, Transform planeTransform)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: 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_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: 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_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: 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_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			Vector3 position = original.position;
			Quaternion rotation = original.rotation;
			Vector3 forward = planeTransform.forward;
			Vector3 position2 = planeTransform.position;
			Vector3 val = position - position2;
			float num = Vector3.Dot(val, forward);
			Vector3 position3 = position - 2f * num * forward;
			Vector3 val2 = rotation * Vector3.forward;
			Vector3 val3 = val2 - 2f * Vector3.Dot(val2, forward) * forward;
			Vector3 val4 = rotation * Vector3.up;
			Vector3 val5 = val4 - 2f * Vector3.Dot(val4, forward) * forward;
			Quaternion rotation2 = Quaternion.LookRotation(val3, val5);
			target.position = position3;
			target.rotation = rotation2;
			target.localScale = Vector3.Scale(original.lossyScale, new Vector3(-1f, 1f, 1f));
		}
	}
	public interface IMirrorSyncable
	{
		void Sync();

		void Setup(Component source, Component target);

		Component[] GetComponentsInChildrenOfMyType(GameObject obj);
	}
	public class MirrorSyncable<T> : IMirrorSyncable where T : Component
	{
		public T Source;

		public T Target;

		public void Setup(Component source, Component target)
		{
			Source = (T)(object)((source is T) ? source : null);
			Target = (T)(object)((target is T) ? target : null);
		}

		public Component[] GetComponentsInChildrenOfMyType(GameObject obj)
		{
			return (Component[])(object)Il2CppArrayBase<T>.op_Implicit(obj.GetComponentsInChildren<T>(true));
		}

		public virtual void Sync()
		{
		}
	}
}
namespace BetterMirrors.Syncing
{
	public class BlendShapeSync : MirrorSyncable<SkinnedMeshRenderer>
	{
		public override void Sync()
		{
		}
	}
	public class GunSync : MirrorSyncable<Gun>
	{
		public override void Sync()
		{
		}
	}
	internal class LaserSync : MirrorSyncable<SLZ_LaserPointer>
	{
		public override void Sync()
		{
			((Component)Target).gameObject.SetActive(((Component)Source).gameObject.active);
		}
	}
	internal class PowerLegsSync : MirrorSyncable<BehaviourPowerLegs>
	{
		public override void Sync()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			((Behaviour)Target).enabled = false;
			for (int i = 0; i < ((Il2CppArrayBase<Transform>)(object)Target.handPoser.leftHandRefs).Count; i++)
			{
				Transform val = ((Il2CppArrayBase<Transform>)(object)Target.handPoser.leftHandRefs)[i];
				Transform val2 = ((Il2CppArrayBase<Transform>)(object)Source.handPoser.leftHandRefs)[i];
				val.localRotation = val2.localRotation;
			}
			for (int j = 0; j < ((Il2CppArrayBase<Transform>)(object)Target.handPoser.rightHandRefs).Count; j++)
			{
				Transform val3 = ((Il2CppArrayBase<Transform>)(object)Target.handPoser.rightHandRefs)[j];
				Transform val4 = ((Il2CppArrayBase<Transform>)(object)Source.handPoser.rightHandRefs)[j];
				val3.localRotation = val4.localRotation;
			}
			Target.ik.neck1.localRotation = Source.ik.neck1.localRotation;
			Target.ik.neck2.localRotation = Source.ik.neck2.localRotation;
		}
	}
	public class VisualDamageSync : MirrorSyncable<VisualDamageController>
	{
		public override void Sync()
		{
			if (Target.impactManager.hitDataMatrixArray == Source.impactManager.hitDataMatrixArray)
			{
				return;
			}
			Target.impactManager.hitDataMatrixArray = Source.impactManager.hitDataMatrixArray;
			foreach (Renderer item in (Il2CppArrayBase<Renderer>)(object)Target.Renderers)
			{
				foreach (Material item2 in (Il2CppArrayBase<Material>)(object)item.materials)
				{
					Target.impactManager.SetImpactsOnMaterial(item2);
				}
			}
		}
	}
}