Decompiled source of Thermal Window v1.0.0

olefar.thermal_window.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using FistVR;
using Microsoft.CodeAnalysis;
using UnityEngine;
using Valve.VR;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("olefar")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("System for supporting thermal rendering of the main camera view")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("olefar.thermal_window")]
[assembly: AssemblyTitle("Thermal Window")]
[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string id = null, string name = null, string version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string id = null, string name = null, string version = null)
		{
		}
	}
}
namespace Olefar
{
	[AddComponentMenu("")]
	public class MainCameraThermalComponent : MonoBehaviour
	{
		private static MainCameraThermalComponent _instance = null;

		private static bool initializedShaderProperties = false;

		private static int _ThermalViewEnabled = -1;

		private static bool renderNextFrame = false;

		public static MainCameraThermalComponent instance
		{
			get
			{
				if ((Object)(object)_instance == (Object)null && (Object)(object)PIPScope_MainCamera.instance != (Object)null)
				{
					_instance = ((Component)PIPScope_MainCamera.instance).gameObject.AddComponent<MainCameraThermalComponent>();
				}
				return _instance;
			}
		}

		private void Start()
		{
			if ((Object)(object)_instance != (Object)null && (Object)(object)_instance != (Object)(object)this)
			{
				Object.Destroy((Object)(object)this);
			}
			else if ((Object)(object)PIPScope_MainCamera.instance == (Object)null)
			{
				Object.Destroy((Object)(object)this);
			}
			else if ((Object)(object)((Component)this).gameObject != (Object)(object)((Component)PIPScope_MainCamera.instance).gameObject)
			{
				Object.Destroy((Object)(object)this);
			}
		}

		private void OnPreCull()
		{
			if (renderNextFrame)
			{
				MainCameraThermalView.Render();
				if (!initializedShaderProperties)
				{
					_ThermalViewEnabled = Shader.PropertyToID("_ThermalViewEnabled");
				}
				Shader.SetGlobalFloat(_ThermalViewEnabled, 1f);
			}
		}

		private void OnPostRender()
		{
			if (renderNextFrame)
			{
				Shader.SetGlobalFloat(_ThermalViewEnabled, 0f);
				renderNextFrame = false;
			}
		}

		public static void RenderThisFrame()
		{
			if ((Object)(object)instance != (Object)null)
			{
				renderNextFrame = true;
			}
			else
			{
				renderNextFrame = false;
			}
		}
	}
	[AddComponentMenu("")]
	public class MainCameraThermalView : MonoBehaviour
	{
		private static MainCameraThermalView _instance;

		private Camera cameraLeft;

		private Camera cameraRight;

		private RenderTexture rtLeft;

		private RenderTexture rtRight;

		public static MainCameraThermalView instance
		{
			get
			{
				if ((Object)(object)_instance == (Object)null)
				{
					CreateInstance();
				}
				return _instance;
			}
		}

		private void Start()
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_instance != (Object)null && (Object)(object)_instance != (Object)(object)this)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
			}
			else if ((Object)(object)_instance == (Object)null)
			{
				_instance = this;
				((Component)this).transform.SetParent((Transform)null);
				((Component)this).transform.position = Vector3.zero;
				((Component)this).transform.rotation = Quaternion.identity;
				((Component)this).transform.localScale = Vector3.one;
				InitializeInstance();
			}
		}

		private static void CreateInstance()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			_instance = new GameObject("Main Camera Thermal View").AddComponent<MainCameraThermalView>();
			_instance.InitializeInstance();
		}

		public void InitializeInstance()
		{
			if ((Object)(object)cameraLeft == (Object)null)
			{
				cameraLeft = CreateCamera("Camera Left");
			}
			if ((Object)(object)cameraRight == (Object)null)
			{
				cameraRight = CreateCamera("Camera Right");
			}
			Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
		}

		private static Camera CreateCamera(string name)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: 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_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: 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)
			GameObject val = new GameObject(name);
			val.transform.SetParent(((Component)_instance).transform, false);
			val.transform.localPosition = Vector3.zero;
			val.transform.localRotation = Quaternion.identity;
			val.transform.localScale = Vector3.zero;
			Camera obj = val.AddComponent<Camera>();
			((Behaviour)obj).enabled = false;
			obj.clearFlags = (CameraClearFlags)2;
			obj.backgroundColor = new Color(1f, 0f, 0f, 0f);
			return obj;
		}

		public static void Render()
		{
			instance.PositionCameras();
			_instance.ValidateRenderTextures();
			PIPScope.PrepareThermalRendering();
			ObjectTemperature.PreCull(true);
			RenderCamera(_instance.cameraLeft);
			RenderCamera(_instance.cameraRight);
			ObjectTemperature.PostRender();
			Shader.SetGlobalTexture("_ThermalMainCameraViewL", (Texture)(object)_instance.rtLeft);
			Shader.SetGlobalTexture("_ThermalMainCameraViewR", (Texture)(object)_instance.rtRight);
		}

		private static void RenderCamera(Camera cam)
		{
			cam.RenderWithShader(ManagerSingleton<FXM>.Instance.ThermalShader, "RenderType");
		}

		private void PositionCameras()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			((Component)this).transform.position = ((Component)PIPScope_MainCamera.instance).transform.position;
			((Component)this).transform.rotation = ((Component)PIPScope_MainCamera.instance).transform.rotation;
			SetupEyeCamera(cameraLeft, (StereoscopicEye)0);
			SetupEyeCamera(cameraRight, (StereoscopicEye)1);
		}

		private void SetupEyeCamera(Camera cam, StereoscopicEye eye)
		{
			//IL_0000: 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_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: 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_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: 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_0059: 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)
			EVREye val = (EVREye)(((int)eye != 0) ? 1 : 0);
			HmdMatrix34_t eyeToHeadTransform = SteamVR.instance.hmd.GetEyeToHeadTransform(val);
			((Component)cam).transform.localPosition = new Vector3(eyeToHeadTransform.m3, eyeToHeadTransform.m7, 0f - eyeToHeadTransform.m11);
			((Component)cam).transform.localRotation = GetEyeRotation(eyeToHeadTransform);
			cam.projectionMatrix = ((PIPScope_Camera)PIPScope_MainCamera.instance).cam.GetStereoProjectionMatrix(eye);
		}

		private Quaternion GetEyeRotation(HmdMatrix34_t eyeMatrix)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: 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_006c: 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_0095: 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_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: 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_00fc: 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_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: 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_001a: 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_015c: Unknown result type (might be due to invalid IL or missing references)
			if ((eyeMatrix.m2 != 0f || eyeMatrix.m6 != 0f || eyeMatrix.m10 != 0f) && (eyeMatrix.m1 != 0f || eyeMatrix.m5 != 0f || eyeMatrix.m9 != 0f))
			{
				float num = Mathf.Sqrt(Mathf.Max(0f, 1f + eyeMatrix.m0 + eyeMatrix.m5 + eyeMatrix.m10)) / 2f;
				float sizeval = Mathf.Sqrt(Mathf.Max(0f, 1f + eyeMatrix.m0 - eyeMatrix.m5 - eyeMatrix.m10)) / 2f;
				float sizeval2 = Mathf.Sqrt(Mathf.Max(0f, 1f - eyeMatrix.m0 + eyeMatrix.m5 - eyeMatrix.m10)) / 2f;
				float sizeval3 = Mathf.Sqrt(Mathf.Max(0f, 1f - eyeMatrix.m0 - eyeMatrix.m5 + eyeMatrix.m10)) / 2f;
				_copysign(ref sizeval, 0f - eyeMatrix.m9 + eyeMatrix.m6);
				_copysign(ref sizeval2, 0f - eyeMatrix.m2 + eyeMatrix.m8);
				_copysign(ref sizeval3, eyeMatrix.m4 - eyeMatrix.m1);
				return new Quaternion(sizeval, sizeval2, sizeval3, num);
			}
			return Quaternion.identity;
		}

		private static void _copysign(ref float sizeval, float signval)
		{
			if (signval > 0f != sizeval > 0f)
			{
				sizeval = 0f - sizeval;
			}
		}

		private void ValidateRenderTextures()
		{
			ValidateRenderTexture(ref rtLeft, ref cameraLeft);
			ValidateRenderTexture(ref rtRight, ref cameraRight);
		}

		private static void ValidateRenderTexture(ref RenderTexture rt, ref Camera targetCamera)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			if ((Object)(object)rt == (Object)null)
			{
				rt = new RenderTexture((int)SteamVR.instance.sceneWidth, (int)SteamVR.instance.sceneHeight, 24, (RenderTextureFormat)2, (RenderTextureReadWrite)0);
				rt.antiAliasing = ((QualitySettings.antiAliasing == 0) ? 1 : QualitySettings.antiAliasing);
				rt.Create();
				targetCamera.targetTexture = rt;
			}
		}

		private void OnDestroy()
		{
			if ((Object)(object)rtLeft != (Object)null)
			{
				rtLeft.Release();
				Object.Destroy((Object)(object)rtLeft);
			}
			if ((Object)(object)rtRight != (Object)null)
			{
				rtLeft.Release();
				Object.Destroy((Object)(object)rtLeft);
			}
		}
	}
	[BepInProcess("h3vr.exe")]
	[AddComponentMenu("")]
	[BepInPlugin("olefar.thermal_window", "Thermal Window", "1.0.0")]
	public class ThermalWindowPlugin : BaseUnityPlugin
	{
		public const string Id = "olefar.thermal_window";

		internal static ManualLogSource Logger { get; private set; }

		public static string Name => "Thermal Window";

		public static string Version => "1.0.0";

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
		}
	}
	public class ThermalWindow : MonoBehaviour
	{
		private void Update()
		{
			MainCameraThermalComponent.RenderThisFrame();
		}
	}
}