Decompiled source of LaserFix v1.0.1

LaserFix.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("LaserFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+456e2804074b7ec55068f7239b35b6419a21beaf")]
[assembly: AssemblyProduct("LaserFix")]
[assembly: AssemblyTitle("LaserFix")]
[assembly: AssemblyVersion("1.0.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 LaserFix
{
	[BepInPlugin("sodmods.laserfix", "Laser Fix", "1.0.1")]
	public class Plugin : BasePlugin
	{
		internal static ManualLogSource Logger;

		private static int glassMask;

		private const float Step = 0.25f;

		public override void Load()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			Logger = ((BasePlugin)this).Log;
			int num = LayerMask.NameToLayer("Transparent");
			glassMask = 1 << ((num >= 0) ? num : 25);
			new Harmony("sodmods.laserfix").PatchAll(typeof(Plugin).Assembly);
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(41, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Laser Fix loaded (window glass on layer ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>((num >= 0) ? num : 25);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(")");
			}
			log.LogInfo(val);
		}

		internal static void StopAtGlass(Transform beam, Transform origin, Light glow)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: 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_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: 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_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: 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_009d: 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)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)beam == (Object)null || (Object)(object)origin == (Object)null)
			{
				return;
			}
			float z = beam.lossyScale.z;
			if (z <= 0.05f)
			{
				return;
			}
			Vector3 position = origin.position;
			Vector3 forward = origin.forward;
			float num = z;
			RaycastHit val = default(RaycastHit);
			if (Physics.Raycast(new Ray(position, forward), ref val, z, glassMask, (QueryTriggerInteraction)2) && IsGlass(((RaycastHit)(ref val)).transform))
			{
				num = ((RaycastHit)(ref val)).distance;
			}
			if (IsOutside(position) == false)
			{
				for (float num2 = 0.25f; num2 < num; num2 += 0.25f)
				{
					if (IsOutside(position + forward * num2) != true)
					{
						continue;
					}
					float num3 = num2 - 0.25f;
					float num4 = num2;
					for (int i = 0; i < 6; i++)
					{
						float num5 = (num3 + num4) * 0.5f;
						if (IsOutside(position + forward * num5) == true)
						{
							num4 = num5;
						}
						else
						{
							num3 = num5;
						}
					}
					num = Mathf.Min(num, num3);
					break;
				}
			}
			if (!(num >= z - 0.01f))
			{
				Transform parent = beam.parent;
				beam.parent = null;
				beam.localScale = new Vector3(beam.localScale.x, beam.localScale.y, num);
				beam.parent = parent;
				if ((Object)(object)glow != (Object)null)
				{
					glow.range = num;
				}
			}
		}

		internal static bool? IsOutside(Vector3 position)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			PathFinder instance = PathFinder.Instance;
			Dictionary<Vector3, NewNode> val = ((instance != null) ? instance.nodeMap : null);
			if (val == null || (Object)(object)CityData.Instance == (Object)null)
			{
				return null;
			}
			NewNode val2 = default(NewNode);
			if (!val.TryGetValue(Vector3Int.op_Implicit(CityData.Instance.RealPosToNodeInt(position)), ref val2) || (Object)(object)((val2 != null) ? val2.gameLocation : null) == (Object)null)
			{
				return null;
			}
			return val2.gameLocation.isOutside;
		}

		private static bool IsGlass(Transform t)
		{
			int num = 0;
			while (num < 3 && (Object)(object)t != (Object)null)
			{
				string name = ((Object)t).name;
				if (name.IndexOf("Window", StringComparison.OrdinalIgnoreCase) >= 0 || name.IndexOf("Glass", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return true;
				}
				num++;
				t = t.parent;
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(Toolbox), "HandleLaserBehaviour")]
	internal static class CameraLaserPatch
	{
		private static void Postfix(GameObject laser, Light laserLight)
		{
			try
			{
				if ((Object)(object)laser != (Object)null)
				{
					Plugin.StopAtGlass(laser.transform, laser.transform, laserLight);
				}
			}
			catch (Exception ex)
			{
				Plugin.Logger.LogError((object)ex);
			}
		}
	}
	[HarmonyPatch(typeof(LaserGridController), "Update")]
	internal static class LaserGridPatch
	{
		private static void Postfix(LaserGridController __instance)
		{
			try
			{
				if ((Object)(object)__instance != (Object)null && (Object)(object)__instance.laser != (Object)null && (Object)(object)__instance.laserParent != (Object)null)
				{
					Plugin.StopAtGlass(__instance.laser, __instance.laserParent, null);
				}
			}
			catch (Exception ex)
			{
				Plugin.Logger.LogError((object)ex);
			}
		}
	}
}