Decompiled source of CustomFOV v1.0.0

plugins/CWFov/CWFov.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Rendering.Universal;
using UnityEngine.XR;
using Zorro.Settings;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("CWFov")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CWFov")]
[assembly: AssemblyTitle("CWFov")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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;
		}
	}
}
public static class CustomFovApi
{
	private static readonly HashSet<string> _suspenders = new HashSet<string>();

	private static readonly HashSet<Transform> _excluded = new HashSet<Transform>();

	public static bool Suspended => _suspenders.Count > 0;

	public static float CurrentFov
	{
		get
		{
			try
			{
				GameHandler instance = GameHandler.Instance;
				object obj;
				if (instance == null)
				{
					obj = null;
				}
				else
				{
					SettingsHandler settingsHandler = instance.SettingsHandler;
					obj = ((settingsHandler != null) ? settingsHandler.GetSetting<FovSetting>() : null);
				}
				FovSetting fovSetting = (FovSetting)obj;
				return (fovSetting != null && !FovPlugin.Off) ? ((FloatSetting)fovSetting).Value : 70f;
			}
			catch
			{
				return 70f;
			}
		}
	}

	public static void SetSuspended(string key, bool suspended)
	{
		if (!string.IsNullOrEmpty(key))
		{
			if (suspended)
			{
				_suspenders.Add(key);
			}
			else
			{
				_suspenders.Remove(key);
			}
		}
	}

	public static void ExcludeFromHud(Transform t)
	{
		if (!((Object)(object)t == (Object)null))
		{
			_excluded.Add(t);
			HudFovPatch.Release(t);
		}
	}

	internal static bool IsExcluded(Transform t)
	{
		if (_excluded.Count == 0)
		{
			return false;
		}
		while ((Object)(object)t != (Object)null)
		{
			if (_excluded.Contains(t))
			{
				return true;
			}
			t = t.parent;
		}
		return false;
	}
}
[ContentWarningPlugin("com.ddk.cwfov", "1.0.0", true)]
[BepInPlugin("com.ddk.cwfov", "Custom FOV", "1.0.0")]
public class FovPlugin : BaseUnityPlugin
{
	public const string Guid = "com.ddk.cwfov";

	public const string Version = "1.0.0";

	internal static ConfigEntry<bool> CfgEnabled;

	internal static ConfigEntry<bool> CfgFixHud;

	internal static bool HudBroken;

	public static bool VrPresent
	{
		get
		{
			try
			{
				if (XRSettings.isDeviceActive)
				{
					return true;
				}
				if (!Chainloader.PluginInfos.TryGetValue("io.daxcess.cwvr", out var value))
				{
					return false;
				}
				ConfigEntry<bool> val = default(ConfigEntry<bool>);
				if ((Object)(object)value.Instance != (Object)null && value.Instance.Config.TryGetEntry<bool>("General", "DisableVR", ref val))
				{
					return !val.Value;
				}
				return true;
			}
			catch
			{
				return false;
			}
		}
	}

	internal static bool Off
	{
		get
		{
			if ((CfgEnabled == null || CfgEnabled.Value) && !CustomFovApi.Suspended)
			{
				return VrPresent;
			}
			return true;
		}
	}

	private void Awake()
	{
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		CfgEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Turn the whole mod off (vanilla FOV and HUD) without removing it. Useful if it conflicts with another mod.");
		CfgFixHud = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FixHud", true, "Draw the helmet HUD with a separate camera so it does not stretch at a wide FOV. Turn off if it conflicts with another camera/UI mod.");
		new Harmony("com.ddk.cwfov").PatchAll(typeof(FovPlugin).Assembly);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Custom FOV 1.0.0 loaded.");
	}
}
[ContentWarningSetting]
public class FovSetting : FloatSetting, IExposedSetting
{
	public const float VanillaFov = 70f;

	public FovSetting()
	{
		((FloatSetting)this).SliderAmount = 5f;
	}

	public override void ApplyValue()
	{
	}

	protected override float GetDefaultValue()
	{
		return 70f;
	}

	protected override float2 GetMinMaxValue()
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		return new float2(50f, 120f);
	}

	public override string Expose(float result)
	{
		return Mathf.RoundToInt(result).ToString();
	}

	public SettingCategory GetSettingCategory()
	{
		return (SettingCategory)0;
	}

	public string GetDisplayName()
	{
		//IL_001d: 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_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Invalid comparison between Unknown and I4
		bool flag = false;
		try
		{
			Locale selectedLocale = LocalizationSettings.SelectedLocale;
			bool num;
			if (!((Object)(object)selectedLocale != (Object)null))
			{
				num = (int)Application.systemLanguage == 30;
			}
			else
			{
				LocaleIdentifier identifier = selectedLocale.Identifier;
				num = ((LocaleIdentifier)(ref identifier)).Code.StartsWith("ru");
			}
			flag = num;
		}
		catch
		{
		}
		if (!flag)
		{
			return "Field of View";
		}
		return "Поле зрения (FOV)";
	}
}
[HarmonyPatch(typeof(MainCamera), "GetFov")]
internal static class MainCameraFovPatch
{
	private static void Postfix(MainCamera __instance, ref float __result)
	{
		try
		{
			if (FovPlugin.Off || !Object.op_Implicit((Object)(object)Player.localPlayer))
			{
				return;
			}
			GameHandler instance = GameHandler.Instance;
			object obj;
			if (instance == null)
			{
				obj = null;
			}
			else
			{
				SettingsHandler settingsHandler = instance.SettingsHandler;
				obj = ((settingsHandler != null) ? settingsHandler.GetSetting<FovSetting>() : null);
			}
			FovSetting fovSetting = (FovSetting)obj;
			if (fovSetting != null)
			{
				float value = Traverse.Create((object)__instance).Field("baseFOV").GetValue<float>();
				if (value > 1f)
				{
					__result = __result / value * ((FloatSetting)fovSetting).Value;
				}
			}
		}
		catch
		{
		}
	}
}
[HarmonyPatch(typeof(UserInterface), "LateUpdate")]
internal static class HudFovPatch
{
	private const int OrigLayer = 5;

	private static int _layer = -1;

	private static readonly Dictionary<Transform, int> _saved = new Dictionary<Transform, int>();

	private static Camera _hud;

	private static Canvas[] _canvases;

	private static UserInterface _inst;

	private static int _fails;

	private static void Postfix(UserInterface __instance)
	{
		//IL_0247: Unknown result type (might be due to invalid IL or missing references)
		//IL_024d: Invalid comparison between Unknown and I4
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: Invalid comparison between Unknown and I4
		try
		{
			MainCamera instance = MainCamera.instance;
			if ((Object)(object)instance == (Object)null || (Object)(object)instance.Cam == (Object)null)
			{
				return;
			}
			Camera cam = instance.Cam;
			GameHandler instance2 = GameHandler.Instance;
			object obj;
			if (instance2 == null)
			{
				obj = null;
			}
			else
			{
				SettingsHandler settingsHandler = instance2.SettingsHandler;
				obj = ((settingsHandler != null) ? settingsHandler.GetSetting<FovSetting>() : null);
			}
			FovSetting fovSetting = (FovSetting)obj;
			float value = Traverse.Create((object)instance).Field("baseFOV").GetValue<float>();
			bool num = !FovPlugin.Off && FovPlugin.CfgFixHud.Value && !FovPlugin.HudBroken && Object.op_Implicit((Object)(object)Player.localPlayer) && fovSetting != null && value > 1f && Mathf.Abs(((FloatSetting)fovSetting).Value - value) > 0.5f;
			if ((Object)(object)_inst != (Object)(object)__instance)
			{
				_inst = __instance;
				_canvases = ((Component)__instance).GetComponentsInChildren<Canvas>(true);
			}
			if (num)
			{
				if ((Object)(object)_hud != (Object)null && (Object)(object)((Component)_hud).transform.parent != (Object)(object)((Component)cam).transform)
				{
					MoveHud(toHud: false);
					Object.Destroy((Object)(object)((Component)_hud).gameObject);
					_hud = null;
				}
				if ((Object)(object)_hud == (Object)null)
				{
					Create(cam);
				}
				if ((Object)(object)_hud == (Object)null)
				{
					return;
				}
				_hud.fieldOfView = value;
				((Behaviour)_hud).enabled = true;
				if (_layer < 0)
				{
					_layer = FreeLayer();
				}
				_hud.cullingMask = 1 << _layer;
				cam.cullingMask &= ~(1 << _layer);
				if (Time.frameCount % 5 == 0)
				{
					MoveHud(toHud: true);
				}
				Canvas[] canvases = _canvases;
				foreach (Canvas val in canvases)
				{
					if ((Object)(object)val != (Object)null && ((Object)val).name == "HelmetUI" && (int)val.renderMode == 1)
					{
						val.worldCamera = _hud;
					}
				}
			}
			else
			{
				if (!((Object)(object)_hud != (Object)null) || !((Behaviour)_hud).enabled)
				{
					return;
				}
				((Behaviour)_hud).enabled = false;
				MoveHud(toHud: false);
				Canvas[] canvases = _canvases;
				foreach (Canvas val2 in canvases)
				{
					if ((Object)(object)val2 != (Object)null && ((Object)val2).name == "HelmetUI" && (int)val2.renderMode == 1)
					{
						val2.worldCamera = cam;
					}
				}
			}
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("CWFov HUD: " + ex));
			if (++_fails >= 3)
			{
				Fail();
			}
		}
	}

	internal static void Release(Transform root)
	{
		Transform[] componentsInChildren = ((Component)root).GetComponentsInChildren<Transform>(true);
		foreach (Transform val in componentsInChildren)
		{
			if (_saved.TryGetValue(val, out var value))
			{
				((Component)val).gameObject.layer = value;
				_saved.Remove(val);
			}
		}
	}

	private static void Fail()
	{
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Invalid comparison between Unknown and I4
		FovPlugin.HudBroken = true;
		try
		{
			MoveHud(toHud: false);
			MainCamera instance = MainCamera.instance;
			if (_canvases != null && (Object)(object)instance != (Object)null)
			{
				Canvas[] canvases = _canvases;
				foreach (Canvas val in canvases)
				{
					if ((Object)(object)val != (Object)null && ((Object)val).name == "HelmetUI" && (int)val.renderMode == 1)
					{
						val.worldCamera = instance.Cam;
					}
				}
			}
			if ((Object)(object)_hud != (Object)null)
			{
				((Behaviour)_hud).enabled = false;
			}
		}
		catch
		{
		}
		Debug.LogWarning((object)"CWFov: HUD fix disabled after repeated errors (HUD may look stretched at wide FOV).");
	}

	private static int FreeLayer()
	{
		for (int num = 31; num > 7; num--)
		{
			if (string.IsNullOrEmpty(LayerMask.LayerToName(num)))
			{
				return num;
			}
		}
		return 31;
	}

	private static void MoveHud(bool toHud)
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Invalid comparison between Unknown and I4
		if (_canvases == null)
		{
			return;
		}
		Canvas[] canvases = _canvases;
		foreach (Canvas val in canvases)
		{
			if ((Object)(object)val == (Object)null || (int)val.renderMode != 1 || ((Object)val).name != "HelmetUI")
			{
				continue;
			}
			Transform[] componentsInChildren = ((Component)val).GetComponentsInChildren<Transform>(true);
			foreach (Transform val2 in componentsInChildren)
			{
				int value;
				if (toHud)
				{
					if (((Component)val2).gameObject.layer != _layer && !CustomFovApi.IsExcluded(val2))
					{
						_saved[val2] = ((Component)val2).gameObject.layer;
						((Component)val2).gameObject.layer = _layer;
					}
				}
				else if (_saved.TryGetValue(val2, out value))
				{
					((Component)val2).gameObject.layer = value;
				}
			}
		}
		if (!toHud)
		{
			_saved.Clear();
		}
	}

	private static void Create(Camera main)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: 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)
		GameObject val = new GameObject("CWFov_HudCamera");
		val.transform.SetParent(((Component)main).transform, false);
		_hud = val.AddComponent<Camera>();
		_hud.CopyFrom(main);
		_hud.cullingMask = 32;
		_hud.clearFlags = (CameraClearFlags)3;
		_hud.depth = main.depth + 1f;
		_hud.allowHDR = false;
		_hud.allowMSAA = false;
		UniversalAdditionalCameraData obj = val.AddComponent<UniversalAdditionalCameraData>();
		obj.renderType = (CameraRenderType)1;
		obj.renderPostProcessing = false;
		CameraExtensions.GetUniversalAdditionalCameraData(main).cameraStack.Add(_hud);
	}
}