Decompiled source of IronSightBlurr v0.0.1

IronSightBlurr.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using FistVR;
using Sodalite;
using Sodalite.Api;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Jerry.IronSightBlur;

[BepInPlugin("JerryDLL.IronSightBlurr", "IronSightBlurr", "1.0.6")]
[BepInDependency("nrgill28.Sodalite", "1.4.1")]
[BepInProcess("h3vr.exe")]
public sealed class IronSightBlurr : BaseUnityPlugin
{
	private ConfigEntry<bool> master;

	private ConfigEntry<bool> forceAim;

	private ConfigEntry<bool> visualizeNear;

	private ConfigEntry<int> debugView;

	private ConfigEntry<float> headX;

	private ConfigEntry<float> headY;

	private ConfigEntry<float> minZ;

	private ConfigEntry<float> maxZ;

	private ConfigEntry<float> angle;

	private ConfigEntry<float> blurEnd;

	private ConfigEntry<float> transition;

	private ConfigEntry<float> strength;

	private ConfigEntry<float> scopeDistance;

	private readonly List<IronSightBlurrEffect> effects = new List<IronSightBlurrEffect>();

	private readonly List<Type> scopeTypes = new List<Type>();

	private readonly List<Component> scopes = new List<Component>();

	private readonly List<FVRViveHand> playerHands = new List<FVRViveHand>();

	private WristMenuButton button;

	private WristMenuButton diagnosticsButton;

	private string state = "Waiting for first update";

	private string gunDetails = "No held firearm";

	private string lastGunDetails = "No firearm observed yet";

	private float lastGunTime;

	private Shader shader;

	private AssetBundle bundle;

	private float shaderRevision;

	private float nextScan;

	private bool sceneEventsRegistered;

	private bool refreshPending;

	private bool configReloadPending;

	private int refreshAfterFrame;

	private bool hasPlayerSnapshot;

	private FVRPlayerBody trackedBody;

	private Camera trackedEye;

	private Transform trackedHead;

	private ConfigEntry<float> Number(string section, string key, float value, float low, float high, string description)
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Expected O, but got Unknown
		return ((BaseUnityPlugin)this).Config.Bind<float>(section, key, value, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<float>(low, high), new object[0]));
	}

	private void Awake()
	{
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Expected O, but got Unknown
		//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f2: Expected O, but got Unknown
		master = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable near-only iron sight depth of field. Wrist button saves this value.");
		forceAim = ((BaseUnityPlugin)this).Config.Bind<bool>("Diagnostics", "ForceAim", false, "TEST ONLY: bypass held-gun/aim checks. Master switch and optical-scope protection still apply.");
		visualizeNear = ((BaseUnityPlugin)this).Config.Bind<bool>("Diagnostics", "VisualizeNear", false, "TEST ONLY: tint near-depth pixels red when active. Turn off after checking depth coverage.");
		debugView = ((BaseUnityPlugin)this).Config.Bind<int>("Diagnostics", "DebugView", 0, new ConfigDescription("0=normal/red near visualization; 1=depth-independent green tint; 2=depth grayscale (white at 1 metre). Requires active effect. Reset to 0 after testing.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 2), new object[0]));
		headX = Number("Aim", "HeadRangeX", 0.05f, 0.005f, 0.5f, "Maximum head offset on muzzle local X, metres.");
		headY = Number("Aim", "HeadRangeY", 0.25f, 0.005f, 0.5f, "Maximum head offset on muzzle local Y, metres; allow sight height above bore.");
		minZ = Number("Aim", "MinMuzzleDistance", 0.25f, 0f, 3f, "Minimum distance behind muzzle along its forward axis, metres.");
		maxZ = Number("Aim", "MaxMuzzleDistance", 1.5f, 0.05f, 5f, "Maximum distance behind muzzle, metres. Must exceed minimum.");
		angle = Number("Aim", "MaxAimAngle", 45f, 1f, 90f, "Maximum angle between head forward and muzzle forward, degrees.");
		blurEnd = Number("DepthOfField", "BlurDistance", 0.25f, 0.005f, 2f, "Camera-space distance where near blur becomes zero, metres. Farther pixels remain unchanged.");
		transition = Number("DepthOfField", "TransitionWidth", 0.025f, 0.001f, 0.5f, "Soft depth transition ending at BlurDistance, metres. Larger values soften the distance threshold.");
		strength = Number("DepthOfField", "BlurRadius", 10f, 0f, 32f, "Foreground blur radius in render-target pixels, including silhouette spread. Try 8-12. Zero disables blur. Existing config values are preserved.");
		scopeDistance = Number("Optics", "PIPScopeEyepieceRange", 0.3f, 0.02f, 1f, "Suppress blur whenever an active PIPScope ocular lens is within this head distance, metres.");
		string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "ironsightblurr-shaders");
		if (File.Exists(text))
		{
			bundle = AssetBundle.LoadFromFile(text);
			if ((Object)(object)bundle != (Object)null)
			{
				shader = bundle.LoadAsset<Shader>("Assets/IronSightBlurr.shader");
			}
		}
		if ((Object)(object)shader == (Object)null)
		{
			shader = Shader.Find("Hidden/IronSightBlurr/NearOnly");
		}
		if ((Object)(object)shader == (Object)null || !shader.isSupported)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Near-only shader missing/unsupported. Build Tools/IronSightBlurr/Build Shader Bundle and place ironsightblurr-shaders beside the plugin DLL. Effect stays off.");
		}
		if ((Object)(object)shader != (Object)null)
		{
			Material val = new Material(shader);
			shaderRevision = (val.HasProperty("_IronSightRevision") ? val.GetFloat("_IronSightRevision") : 0f);
			Object.Destroy((Object)(object)val);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Shader bundle path=" + text + "; shader=" + ((Object)shader).name + "; revision=" + shaderRevision));
			if (shaderRevision < 104f)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"OLD SHADER BUNDLE: revision 104 required. Rebuild and replace ironsightblurr-shaders together with the DLL. Effect disabled until updated.");
			}
		}
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly in assemblies)
		{
			Type type = assembly.GetType("PIPScope", throwOnError: false);
			if ((object)type != null && typeof(Component).IsAssignableFrom(type))
			{
				scopeTypes.Add(type);
			}
		}
		if (scopeTypes.Count == 0)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"PIPScope type unavailable; blur stays off to protect optical sights.");
		}
	}

	private void Start()
	{
		EnsureButtons();
	}

	private void EnsureButtons()
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Expected O, but got Unknown
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Expected O, but got Unknown
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Expected O, but got Unknown
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Expected O, but got Unknown
		if (button == null)
		{
			button = new WristMenuButton("IronSightBlurr: Toggle", new ButtonClickEvent(Toggle));
		}
		if (diagnosticsButton == null)
		{
			diagnosticsButton = new WristMenuButton("IronSightBlurr: Diagnose", new ButtonClickEvent(Diagnose));
		}
		if (!WristMenuAPI.Buttons.Contains(button))
		{
			WristMenuAPI.Buttons.Add(button);
		}
		if (!WristMenuAPI.Buttons.Contains(diagnosticsButton))
		{
			WristMenuAPI.Buttons.Add(diagnosticsButton);
		}
	}

	private void OnEnable()
	{
		if (!sceneEventsRegistered)
		{
			SceneManager.sceneLoaded += SceneLoaded;
			SceneManager.sceneUnloaded += SceneUnloaded;
			SceneManager.activeSceneChanged += ActiveSceneChanged;
			sceneEventsRegistered = true;
		}
		RequestRefresh(reloadConfig: true);
	}

	private void UnregisterSceneEvents()
	{
		if (sceneEventsRegistered)
		{
			SceneManager.sceneLoaded -= SceneLoaded;
			SceneManager.sceneUnloaded -= SceneUnloaded;
			SceneManager.activeSceneChanged -= ActiveSceneChanged;
			sceneEventsRegistered = false;
		}
	}

	private void SceneLoaded(Scene scene, LoadSceneMode mode)
	{
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		RequestRefresh(reloadConfig: true);
		((BaseUnityPlugin)this).Logger.LogInfo((object)string.Concat("Scene loaded: ", ((Scene)(ref scene)).name, "; refreshing player bindings (", mode, ")."));
	}

	private void SceneUnloaded(Scene scene)
	{
		RequestRefresh();
	}

	private void ActiveSceneChanged(Scene previous, Scene current)
	{
		RequestRefresh(reloadConfig: true);
	}

	private void ClearRuntimeBindings()
	{
		foreach (IronSightBlurrEffect effect in effects)
		{
			if ((Object)(object)effect != (Object)null)
			{
				effect.Active = false;
				((Behaviour)effect).enabled = false;
				Object.Destroy((Object)(object)effect);
			}
		}
		effects.Clear();
		playerHands.Clear();
		scopes.Clear();
		trackedBody = null;
		trackedEye = null;
		trackedHead = null;
		hasPlayerSnapshot = false;
		gunDetails = "Waiting for player after refresh";
		lastGunDetails = "No firearm observed since refresh";
		lastGunTime = Time.unscaledTime;
	}

	private void RequestRefresh(bool reloadConfig = false)
	{
		ClearRuntimeBindings();
		refreshPending = true;
		configReloadPending = configReloadPending || reloadConfig;
		refreshAfterFrame = Time.frameCount + 1;
		nextScan = 0f;
		state = "Waiting for scene/player refresh";
	}

	private void Diagnose(object sender, ButtonClickEventArgs args)
	{
		//IL_033e: Unknown result type (might be due to invalid IL or missing references)
		((BaseUnityPlugin)this).Logger.LogInfo((object)("IronSightBlurr 1.0.6: " + state + "; " + gunDetails + "; BlurDistance=" + blurEnd.Value + "; Radius=" + strength.Value + "; scopeTypes=" + scopeTypes.Count + "; scopes=" + scopes.Count + "; shaderRevision=" + shaderRevision + "; ForceAim=" + forceAim.Value + "; VisualizeNear=" + visualizeNear.Value + "; DebugView=" + debugView.Value));
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Player hands=" + playerHands.Count + "; recent firearm=" + lastGunDetails + "; secondsAgo=" + (Time.unscaledTime - lastGunTime).ToString("F1")));
		foreach (FVRViveHand playerHand in playerHands)
		{
			if ((Object)(object)playerHand != (Object)null)
			{
				FVRInteractiveObject currentInteractable = playerHand.CurrentInteractable;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Hand=" + ((Object)playerHand).name + "; interactable=" + (((Object)(object)currentInteractable == (Object)null) ? "null" : (((Object)currentInteractable).name + " (" + ((object)currentInteractable).GetType().FullName + ")"))));
			}
		}
		foreach (IronSightBlurrEffect effect in effects)
		{
			if (!((Object)(object)effect == (Object)null))
			{
				Camera component = ((Component)effect).GetComponent<Camera>();
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Effect camera=" + ((Object)component).name + "; active=" + effect.Active + "; renderCalls=" + effect.RenderCalls + "; processedCalls=" + effect.ProcessedCalls + "; depthTexture=" + effect.DepthTextureInfo + "; nearClip=" + component.nearClipPlane + "; farClip=" + component.farClipPlane + "; depth=" + component.depthTextureMode));
				if (component.nearClipPlane >= blurEnd.Value)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"BlurDistance is at/before the near clipping plane: no visible geometry can receive blur. Increase BlurDistance.");
				}
			}
		}
		Camera[] allCameras = Camera.allCameras;
		foreach (Camera component in allCameras)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Available camera=" + ((Object)component).name + "; isEyeCam=" + ((Object)(object)GM.CurrentPlayerBody != (Object)null && (Object)(object)GM.CurrentPlayerBody.EyeCam == (Object)(object)component) + "; enabled=" + ((Behaviour)component).enabled + "; targetTexture=" + ((Object)(object)component.targetTexture != (Object)null)));
		}
	}

	private void Toggle(object sender, ButtonClickEventArgs args)
	{
		master.Value = !master.Value;
		((BaseUnityPlugin)this).Config.Save();
		if (!master.Value)
		{
			StopEffects();
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("IronSightBlurr " + (master.Value ? "ON" : "OFF")));
	}

	private void LateUpdate()
	{
		//IL_04e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0397: Unknown result type (might be due to invalid IL or missing references)
		//IL_039c: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_03af: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0497: Unknown result type (might be due to invalid IL or missing references)
		//IL_049e: Unknown result type (might be due to invalid IL or missing references)
		if (refreshPending)
		{
			if (Time.frameCount < refreshAfterFrame)
			{
				return;
			}
			refreshPending = false;
			if (configReloadPending)
			{
				configReloadPending = false;
				try
				{
					((BaseUnityPlugin)this).Config.Reload();
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Config reloaded after scene/player enable: Enabled=" + master.Value + "; BlurDistance=" + blurEnd.Value + "; BlurRadius=" + strength.Value + "; TransitionWidth=" + transition.Value));
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("Config reload failed: " + ex.Message));
				}
			}
			EnsureButtons();
		}
		FVRMovementManager currentMovementManager = GM.CurrentMovementManager;
		FVRPlayerBody currentPlayerBody = GM.CurrentPlayerBody;
		Camera val = (((Object)(object)currentPlayerBody != (Object)null) ? currentPlayerBody.EyeCam : null);
		Transform val2 = (((Object)(object)currentPlayerBody != (Object)null) ? currentPlayerBody.Head : null);
		if ((Object)(object)val2 == (Object)null && (Object)(object)currentMovementManager != (Object)null)
		{
			val2 = currentMovementManager.Head;
		}
		if (hasPlayerSnapshot && ((Object)(object)trackedBody != (Object)(object)currentPlayerBody || (Object)(object)trackedEye != (Object)(object)val || (Object)(object)trackedHead != (Object)(object)val2))
		{
			RequestRefresh();
			return;
		}
		trackedBody = currentPlayerBody;
		trackedEye = val;
		trackedHead = val2;
		hasPlayerSnapshot = true;
		if ((Object)(object)val2 == (Object)null || !master.Value || (Object)(object)shader == (Object)null || !shader.isSupported || shaderRevision < 104f || scopeTypes.Count == 0)
		{
			state = ((!master.Value) ? "Disabled" : (((Object)(object)val2 == (Object)null) ? "No player head" : (((Object)(object)shader == (Object)null) ? "Shader missing" : ((!shader.isSupported) ? "Shader unsupported" : ((shaderRevision < 104f) ? "Old shader bundle; rebuild/replace it" : "PIPScope type missing")))));
			StopEffects();
			return;
		}
		if (Time.unscaledTime >= nextScan)
		{
			nextScan = Time.unscaledTime + 0.25f;
			Scan(val2);
		}
		bool flag = false;
		gunDetails = "No held firearm";
		foreach (FVRViveHand playerHand in playerHands)
		{
			if ((Object)(object)playerHand == (Object)null || !((Component)playerHand).gameObject.activeInHierarchy)
			{
				continue;
			}
			FVRFireArm val3 = HeldGun(playerHand);
			if ((Object)(object)val3 == (Object)null)
			{
				continue;
			}
			if ((Object)(object)val3.MuzzlePos == (Object)null)
			{
				gunDetails = ((Object)val3).name + "; missing MuzzlePos";
				continue;
			}
			Transform muzzlePos = val3.MuzzlePos;
			Vector3 val4 = Quaternion.Inverse(muzzlePos.rotation) * (val2.position - muzzlePos.position);
			gunDetails = ((Object)val3).name + "; head in muzzle axes=" + ((Vector3)(ref val4)).ToString("F3") + "; angle=" + Vector3.Angle(val2.forward, muzzlePos.forward).ToString("F1");
			lastGunDetails = gunDetails;
			lastGunTime = Time.unscaledTime;
			if (Mathf.Abs(val4.x) <= headX.Value && Mathf.Abs(val4.y) <= headY.Value && 0f - val4.z >= minZ.Value && 0f - val4.z <= maxZ.Value && Vector3.Angle(val2.forward, muzzlePos.forward) <= angle.Value)
			{
				flag = true;
			}
		}
		bool flag2 = NearScope(val2.position);
		bool flag3 = (flag || forceAim.Value) && !flag2;
		int num = 0;
		for (int num2 = effects.Count - 1; num2 >= 0; num2--)
		{
			IronSightBlurrEffect ironSightBlurrEffect = effects[num2];
			if ((Object)(object)ironSightBlurrEffect == (Object)null)
			{
				effects.RemoveAt(num2);
			}
			else
			{
				ironSightBlurrEffect.Active = flag3 && IsPlayerCamera(((Component)ironSightBlurrEffect).GetComponent<Camera>(), val2);
				if (ironSightBlurrEffect.Active)
				{
					num++;
				}
				ironSightBlurrEffect.VisualizeNear = visualizeNear.Value;
				ironSightBlurrEffect.DebugView = debugView.Value;
				ironSightBlurrEffect.Distance = blurEnd.Value;
				ironSightBlurrEffect.Transition = Mathf.Min(transition.Value, blurEnd.Value);
				ironSightBlurrEffect.Radius = strength.Value;
			}
		}
		state = (flag2 ? "Blocked by nearby PIPScope eyepiece" : ((!flag3) ? "Aim conditions not met" : ((num == 0) ? "No matching player camera" : "Active")));
	}

	private static bool IsPlayerCamera(Camera camera, Transform head)
	{
		//IL_0086: 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_00b5: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)camera == (Object)null || !((Behaviour)camera).enabled || !((Component)camera).gameObject.activeInHierarchy)
		{
			return false;
		}
		if ((Object)(object)GM.CurrentPlayerBody != (Object)null && (Object)(object)GM.CurrentPlayerBody.EyeCam == (Object)(object)camera)
		{
			return true;
		}
		if ((Object)(object)((Component)camera).GetComponent("PIPScope_MainCamera") != (Object)null)
		{
			return true;
		}
		return (Object)(object)camera.targetTexture == (Object)null && (int)camera.stereoTargetEye != 0 && ((Object)(object)((Component)camera).transform == (Object)(object)head || ((Component)camera).transform.IsChildOf(head) || Vector3.Distance(((Component)camera).transform.position, head.position) < 0.15f) && (Object)(object)((Component)camera).GetComponent("PIPScope_Camera") == (Object)null;
	}

	private void Scan(Transform head)
	{
		RefreshHands(head);
		scopes.Clear();
		foreach (Type scopeType in scopeTypes)
		{
			Object[] array = Object.FindObjectsOfType(scopeType);
			foreach (Object val in array)
			{
				Component val2 = (Component)(object)((val is Component) ? val : null);
				if ((Object)(object)val2 != (Object)null)
				{
					scopes.Add(val2);
				}
			}
		}
		Camera[] allCameras = Camera.allCameras;
		foreach (Camera val3 in allCameras)
		{
			if (!IsPlayerCamera(val3, head))
			{
				continue;
			}
			bool flag = false;
			foreach (IronSightBlurrEffect effect in effects)
			{
				if ((Object)(object)effect != (Object)null && (Object)(object)((Component)effect).gameObject == (Object)(object)((Component)val3).gameObject)
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				IronSightBlurrEffect ironSightBlurrEffect = ((Component)val3).gameObject.AddComponent<IronSightBlurrEffect>();
				ironSightBlurrEffect.Initialize(shader);
				effects.Add(ironSightBlurrEffect);
			}
		}
	}

	private void AddHand(FVRViveHand hand)
	{
		if ((Object)(object)hand != (Object)null && ((Component)hand).gameObject.activeInHierarchy && !playerHands.Contains(hand))
		{
			playerHands.Add(hand);
		}
	}

	private void AddHandAt(Transform anchor)
	{
		if (!((Object)(object)anchor == (Object)null))
		{
			AddHand(((Component)anchor).GetComponent<FVRViveHand>());
			AddHand(((Component)anchor).GetComponentInParent<FVRViveHand>());
			FVRViveHand[] componentsInChildren = ((Component)anchor).GetComponentsInChildren<FVRViveHand>();
			foreach (FVRViveHand hand in componentsInChildren)
			{
				AddHand(hand);
			}
		}
	}

	private void RefreshHands(Transform head)
	{
		playerHands.Clear();
		FVRMovementManager currentMovementManager = GM.CurrentMovementManager;
		FVRViveHand[] hands;
		if ((Object)(object)currentMovementManager != (Object)null)
		{
			if (currentMovementManager.Hands != null)
			{
				hands = currentMovementManager.Hands;
				foreach (FVRViveHand hand in hands)
				{
					AddHand(hand);
				}
			}
			AddHandAt(currentMovementManager.LeftHand);
			AddHandAt(currentMovementManager.RightHand);
		}
		if ((Object)(object)GM.CurrentPlayerBody != (Object)null)
		{
			AddHandAt(GM.CurrentPlayerBody.LeftHand);
			AddHandAt(GM.CurrentPlayerBody.RightHand);
		}
		hands = Object.FindObjectsOfType<FVRViveHand>();
		foreach (FVRViveHand hand in hands)
		{
			if ((Object)(object)hand.Head == (Object)(object)head || ((Object)(object)currentMovementManager != (Object)null && (Object)(object)hand.MovementManager == (Object)(object)currentMovementManager))
			{
				AddHand(hand);
			}
		}
	}

	private static FVRFireArm HeldGun(FVRViveHand hand)
	{
		FVRInteractiveObject currentInteractable = hand.CurrentInteractable;
		if ((Object)(object)currentInteractable == (Object)null)
		{
			return null;
		}
		FVRFireArm val = (FVRFireArm)(object)((currentInteractable is FVRFireArm) ? currentInteractable : null);
		if ((Object)(object)val != (Object)null)
		{
			return val;
		}
		FVRAlternateGrip val2 = (FVRAlternateGrip)(object)((currentInteractable is FVRAlternateGrip) ? currentInteractable : null);
		if ((Object)(object)val2 != (Object)null)
		{
			FVRPhysicalObject primaryObject = val2.PrimaryObject;
			return (FVRFireArm)(object)((primaryObject is FVRFireArm) ? primaryObject : null);
		}
		val = ((Component)currentInteractable).GetComponentInParent<FVRFireArm>();
		return ((Object)(object)val != (Object)null && (Object)(object)((FVRInteractiveObject)val).m_hand == (Object)(object)hand) ? val : null;
	}

	private static Renderer Lens(Component scope, string name)
	{
		FieldInfo field = ((object)scope).GetType().GetField(name, BindingFlags.Instance | BindingFlags.Public);
		return (Renderer)(((object)field == null) ? null : /*isinst with value type is only supported in some contexts*/);
	}

	private bool NearScope(Vector3 head)
	{
		//IL_00a9: 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_009c: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: 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_0101: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0112: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		float num = scopeDistance.Value * scopeDistance.Value;
		foreach (Component scope in scopes)
		{
			if ((Object)(object)scope == (Object)null || !scope.gameObject.activeInHierarchy)
			{
				continue;
			}
			Behaviour val = (Behaviour)(object)((scope is Behaviour) ? scope : null);
			if ((Object)(object)val != (Object)null && !val.enabled)
			{
				continue;
			}
			Renderer val2 = Lens(scope, "lensRenderer");
			Bounds bounds;
			Vector3 val3;
			if (!((Object)(object)val2 == (Object)null))
			{
				bounds = val2.bounds;
				val3 = ((Bounds)(ref bounds)).center;
			}
			else
			{
				val3 = scope.transform.position;
			}
			Vector3 val4 = val3;
			Vector3 val5 = head - val4;
			if (((Vector3)(ref val5)).sqrMagnitude <= num)
			{
				return true;
			}
			Renderer val6 = Lens(scope, "biocularSecondaryLensRenderer");
			if ((Object)(object)val6 != (Object)null && val6.enabled && ((Component)val6).gameObject.activeInHierarchy)
			{
				bounds = val6.bounds;
				val5 = head - ((Bounds)(ref bounds)).center;
				if (((Vector3)(ref val5)).sqrMagnitude <= num)
				{
					return true;
				}
			}
		}
		return false;
	}

	private void StopEffects()
	{
		foreach (IronSightBlurrEffect effect in effects)
		{
			if ((Object)(object)effect != (Object)null)
			{
				effect.Active = false;
			}
		}
	}

	private void OnDisable()
	{
		UnregisterSceneEvents();
		ClearRuntimeBindings();
	}

	private void OnDestroy()
	{
		UnregisterSceneEvents();
		ClearRuntimeBindings();
		if (button != null)
		{
			WristMenuAPI.Buttons.Remove(button);
		}
		if (diagnosticsButton != null)
		{
			WristMenuAPI.Buttons.Remove(diagnosticsButton);
		}
		if ((Object)(object)bundle != (Object)null)
		{
			bundle.Unload(false);
		}
	}
}
[RequireComponent(typeof(Camera))]
public sealed class IronSightBlurrEffect : MonoBehaviour
{
	public bool Active;

	public bool VisualizeNear;

	public int RenderCalls;

	public int ProcessedCalls;

	public int DebugView;

	public string DepthTextureInfo = "Not rendered";

	public float Distance;

	public float Transition;

	public float Radius;

	private Material material;

	private Camera owner;

	public void Initialize(Shader shader)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Expected O, but got Unknown
		owner = ((Component)this).GetComponent<Camera>();
		material = new Material(shader);
		((Object)material).hideFlags = (HideFlags)61;
	}

	private void OnPreCull()
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		if (Active && (Object)(object)owner != (Object)null)
		{
			Camera obj = owner;
			obj.depthTextureMode = (DepthTextureMode)(obj.depthTextureMode | 1);
		}
	}

	private void OnRenderImage(RenderTexture source, RenderTexture destination)
	{
		RenderCalls++;
		if (!Active || (Object)(object)material == (Object)null || (Radius <= 0f && !VisualizeNear && DebugView == 0))
		{
			Graphics.Blit((Texture)(object)source, destination);
			return;
		}
		ProcessedCalls++;
		if (ProcessedCalls == 1 || ProcessedCalls % 120 == 0)
		{
			Texture globalTexture = Shader.GetGlobalTexture("_CameraDepthTexture");
			DepthTextureInfo = (((Object)(object)globalTexture == (Object)null) ? "null" : (((Object)globalTexture).name + " " + globalTexture.width + "x" + globalTexture.height));
		}
		material.SetFloat("_BlurEnd", Distance);
		material.SetFloat("_Transition", Mathf.Max(0.001f, Transition));
		material.SetFloat("_Radius", Radius);
		material.SetFloat("_VisualizeNear", VisualizeNear ? 1f : 0f);
		material.SetFloat("_DebugView", (float)DebugView);
		Graphics.Blit((Texture)(object)source, destination, material);
	}

	private void OnDisable()
	{
		Active = false;
	}

	private void OnDestroy()
	{
		if ((Object)(object)material != (Object)null)
		{
			Object.Destroy((Object)(object)material);
		}
	}
}