Decompiled source of HorizonOverlay v1.0.1

plugins/HorizonOverlay.dll

Decompiled 11 hours ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using ComputerysModdingUtilities;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: StraftatMod(true)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("HorizonOverlay")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("HorizonOverlay")]
[assembly: AssemblyTitle("HorizonOverlay")]
[assembly: AssemblyVersion("1.0.0.0")]
[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;
		}
	}
}
[BepInPlugin("com.27n.horizonoverlay", "Horizon Overlay", "1.0.0")]
public class HorizonOverlay : BaseUnityPlugin
{
	private Camera playerCamera;

	private ConfigEntry<float> lineThickness;

	private ConfigEntry<float> lineWidth;

	private ConfigEntry<float> crosshairGap;

	private ConfigEntry<float> blendFactor;

	private ConfigEntry<float> generalAlpha;

	private ConfigEntry<Color> lineColor;

	private ConfigEntry<int> overlayMode;

	private ConfigEntry<bool> outlineEnabled;

	private ConfigEntry<float> outlineThickness;

	private ConfigEntry<Color> outlineColor;

	private ConfigEntry<float> overlayAlpha;

	private ConfigEntry<float> outlineAlpha;

	private Texture2D lineTex;

	private Texture2D overlayTexture;

	private bool overlayLoaded;

	private float checkTimer;

	private void Awake()
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Expected O, but got Unknown
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Expected O, but got Unknown
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Expected O, but got Unknown
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Expected O, but got Unknown
		//IL_0137: Unknown result type (might be due to invalid IL or missing references)
		//IL_0141: Expected O, but got Unknown
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Expected O, but got Unknown
		//IL_01db: Unknown result type (might be due to invalid IL or missing references)
		//IL_021e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0228: Expected O, but got Unknown
		//IL_027d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0287: Expected O, but got Unknown
		//IL_02be: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c8: Expected O, but got Unknown
		//IL_02ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d8: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)"[27N] HorizonOverlay Awake()");
		lineThickness = ((BaseUnityPlugin)this).Config.Bind<float>("Line", "Thickness", 3f, new ConfigDescription("Thickness of the horizon line in pixels", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1000f), Array.Empty<object>()));
		lineWidth = ((BaseUnityPlugin)this).Config.Bind<float>("Line", "Width", 0.6f, new ConfigDescription("Width of the horizon line relative to screen width (0-1)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		crosshairGap = ((BaseUnityPlugin)this).Config.Bind<float>("Line", "CrosshairGap", 20f, new ConfigDescription("Gap in pixels for crosshair in the middle of the line", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1000f), Array.Empty<object>()));
		blendFactor = ((BaseUnityPlugin)this).Config.Bind<float>("Line", "PerspectiveBlend", 0.5f, new ConfigDescription("Blend factor between linear and perspective line movement", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		generalAlpha = ((BaseUnityPlugin)this).Config.Bind<float>("Line", "Alpha", 1f, new ConfigDescription("Opacity of main line", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		lineColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Line", "Color", Color.white, "Color of the horizon line");
		outlineEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Outline", "Enabled", true, "Whether the horizon line has an outline");
		outlineThickness = ((BaseUnityPlugin)this).Config.Bind<float>("Outline", "Thickness", 2f, new ConfigDescription("Outline thickness in pixels", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1000f), Array.Empty<object>()));
		outlineColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Outline", "Color", Color.black, "Color of the outline");
		outlineAlpha = ((BaseUnityPlugin)this).Config.Bind<float>("Outline", "Alpha", 1f, new ConfigDescription("Overall transparency of the outline (0 = invisible, 1 = fully visible)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		overlayMode = ((BaseUnityPlugin)this).Config.Bind<int>("Overlay", "OverlayMode", 2, "0 = Line only, 1 = Image only, 2 = Both");
		overlayAlpha = ((BaseUnityPlugin)this).Config.Bind<float>("Overlay", "Alpha", 1f, new ConfigDescription("Overall transparency of the overlay image (0 = invisible, 1 = fully visible)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		((BaseUnityPlugin)this).Config.Bind<string>("Overlay", "NOTE_ImageLocation", "Put overlay.png next to HorizonOverlay.dll", new ConfigDescription("If you wish to overlay an image as well, it must be named overlay.png and placed next to the .dll file \nExample path:\nBepInEx/plugins/Unknown-HorizonOverlay.dll/overlay.png", (AcceptableValueBase)(object)new AcceptableValueList<string>(new string[1] { "README" }), Array.Empty<object>()));
		lineTex = new Texture2D(1, 1, (TextureFormat)4, false);
		((Texture)lineTex).wrapMode = (TextureWrapMode)1;
		((Texture)lineTex).filterMode = (FilterMode)1;
		LoadOverlayImage();
	}

	private void Update()
	{
		checkTimer -= Time.deltaTime;
		if (checkTimer <= 0f)
		{
			playerCamera = FindPlayerCamera();
			checkTimer = 0.5f;
		}
	}

	private Camera FindPlayerCamera()
	{
		Camera[] array = Object.FindObjectsOfType<Camera>();
		foreach (Camera val in array)
		{
			if (((Behaviour)val).enabled && (val.cullingMask & (1 << LayerMask.NameToLayer("Default"))) != 0 && ((Object)((Component)val).transform.root).name.ToLower().Contains("player"))
			{
				return val;
			}
		}
		return Camera.main;
	}

	private void LoadOverlayImage()
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Expected O, but got Unknown
		try
		{
			string text = Path.Combine(Path.GetDirectoryName(((object)this).GetType().Assembly.Location), "overlay.png");
			if (!File.Exists(text))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[27N] No overlay image found, skipping image overlay");
				overlayLoaded = false;
				return;
			}
			byte[] array = File.ReadAllBytes(text);
			overlayTexture = new Texture2D(2, 2, (TextureFormat)4, false);
			ImageConversion.LoadImage(overlayTexture, array);
			((Texture)overlayTexture).wrapMode = (TextureWrapMode)1;
			((Texture)overlayTexture).filterMode = (FilterMode)1;
			overlayLoaded = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[27N] Overlay image loaded successfully from " + text));
		}
		catch (Exception ex)
		{
			overlayLoaded = false;
			((BaseUnityPlugin)this).Logger.LogError((object)("[27N] Failed to load overlay image: " + ex));
		}
	}

	private void OnGUI()
	{
		try
		{
			if (!((Object)(object)playerCamera == (Object)null) && ((Object)((Component)playerCamera).transform.root).name.ToLower().Contains("player"))
			{
				if (overlayMode.Value == 0 || overlayMode.Value == 2)
				{
					DrawHorizonLine();
				}
				if ((overlayMode.Value == 1 || overlayMode.Value == 2) && overlayLoaded)
				{
					DrawOverlayImage();
				}
			}
		}
		catch (Exception arg)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)$"[27N] Exception in OnGUI: {arg}");
		}
	}

	private void DrawHorizonLine()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_0212: Unknown result type (might be due to invalid IL or missing references)
		//IL_0239: Unknown result type (might be due to invalid IL or missing references)
		//IL_0260: Unknown result type (might be due to invalid IL or missing references)
		//IL_0284: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_030e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0330: Unknown result type (might be due to invalid IL or missing references)
		float num = playerCamera.fieldOfView * (MathF.PI / 180f);
		float num2 = Mathf.Asin(Mathf.Clamp(((Component)playerCamera).transform.forward.y, -1f, 1f));
		float num3 = Mathf.Clamp(num2 / (num * 0.5f), -1f, 1f);
		float num4 = Mathf.Tan(num2) / Mathf.Tan(num * 0.5f);
		float num5 = Mathf.Lerp(num3, num4, blendFactor.Value);
		float num6 = 1f - Mathf.Clamp01(Mathf.Abs(num5));
		float num7 = (float)Screen.height * 0.5f;
		float num8 = num7 + num5 * num7;
		float num9 = lineThickness.Value * 0.5f;
		float num10 = num8 - num9;
		float num11 = Mathf.Clamp01(lineWidth.Value) * (float)Screen.width;
		float num12 = (float)Screen.width * 0.5f;
		float num13 = crosshairGap.Value * 0.5f;
		float num14 = Mathf.Max(0f, num11 * 0.5f - num13);
		Color value = lineColor.Value;
		value.a *= num6 * generalAlpha.Value;
		lineTex.SetPixel(0, 0, value);
		lineTex.Apply();
		float value2 = outlineThickness.Value;
		float num15 = num12 - num13 - num14;
		GUI.DrawTexture(new Rect(num15, num10, num14 - value2, lineThickness.Value), (Texture)(object)lineTex);
		GUI.DrawTexture(new Rect(num12 + num13 + value2, num10, num14 - value2, lineThickness.Value), (Texture)(object)lineTex);
		if (outlineEnabled.Value && outlineThickness.Value > 0f)
		{
			float num16 = num12 - num14 - num13;
			float num17 = num12 + num13;
			Color value3 = outlineColor.Value;
			value3.a *= num6 * outlineAlpha.Value;
			lineTex.SetPixel(0, 0, value3);
			lineTex.Apply();
			float num18 = num14 + value2;
			GUI.DrawTexture(new Rect(num16 - value2, num10 - value2, num18, value2), (Texture)(object)lineTex);
			GUI.DrawTexture(new Rect(num16 - value2, num10 + lineThickness.Value, num18, value2), (Texture)(object)lineTex);
			GUI.DrawTexture(new Rect(num16 - value2, num10, value2, lineThickness.Value), (Texture)(object)lineTex);
			GUI.DrawTexture(new Rect(num16 + num14 - value2, num10, value2, lineThickness.Value), (Texture)(object)lineTex);
			GUI.DrawTexture(new Rect(num17, num10 - value2, num14 + value2, value2), (Texture)(object)lineTex);
			GUI.DrawTexture(new Rect(num17, num10 + lineThickness.Value, num14 + value2, value2), (Texture)(object)lineTex);
			GUI.DrawTexture(new Rect(num17, num10, value2, lineThickness.Value), (Texture)(object)lineTex);
			GUI.DrawTexture(new Rect(num17 + num14, num10, value2, lineThickness.Value), (Texture)(object)lineTex);
		}
	}

	private void DrawOverlayImage()
	{
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		if (overlayLoaded && !((Object)(object)overlayTexture == (Object)null) && !((Object)(object)playerCamera == (Object)null))
		{
			float num = playerCamera.fieldOfView * (MathF.PI / 180f);
			float num2 = Mathf.Asin(Mathf.Clamp(((Component)playerCamera).transform.forward.y, -1f, 1f));
			float num3 = Mathf.Clamp(num2 / (num * 0.5f), -1f, 1f);
			float num4 = Mathf.Tan(num2) / Mathf.Tan(num * 0.5f);
			float num5 = Mathf.Lerp(num3, num4, blendFactor.Value);
			float num6 = 1f - Mathf.Clamp01(Mathf.Abs(num5));
			float num7 = (float)Screen.height * 0.5f;
			float num8 = num7 + num5 * num7 - (float)((Texture)overlayTexture).height * 0.5f;
			float num9 = (float)(Screen.width - ((Texture)overlayTexture).width) * 0.5f;
			Color color = GUI.color;
			GUI.color = new Color(1f, 1f, 1f, overlayAlpha.Value * num6);
			GUI.DrawTexture(new Rect(num9, num8, (float)((Texture)overlayTexture).width, (float)((Texture)overlayTexture).height), (Texture)(object)overlayTexture, (ScaleMode)2, true);
			GUI.color = color;
		}
	}
}