Decompiled source of ReloadTriggerWellEnlarger v1.0.3

Niko666.ReloadTriggerWellEnlarger.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FistVR;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("Niko666")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Makes reload trigger zones larger so you can reload with ease without enabling Easy Mag Loading.")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3")]
[assembly: AssemblyProduct("Niko666.ReloadTriggerWellEnlarger")]
[assembly: AssemblyTitle("ReloadTriggerWellEnlarger")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.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;
		}
	}
}
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 Niko666
{
	[BepInProcess("h3vr.exe")]
	[BepInPlugin("Niko666.ReloadTriggerWellEnlarger", "ReloadTriggerWellEnlarger", "1.0.3")]
	public class ReloadTriggerWellEnlarger : BaseUnityPlugin
	{
		public class WeaponSettings
		{
			public float? ScaleXMultiplyAmount { get; set; }

			public float? ScaleYMultiplyAmount { get; set; }

			public float? ScaleZMultiplyAmount { get; set; }
		}

		public class RecoilConfigData
		{
			public Dictionary<string, WeaponSettings> WeaponOverrides { get; set; }
		}

		public static Dictionary<string, WeaponSettings> WeaponOverrides = new Dictionary<string, WeaponSettings>();

		public ConfigEntry<bool> configLogDebug;

		public ConfigEntry<float> configScaleXMultiplyAmount;

		public ConfigEntry<float> configScaleYMultiplyAmount;

		public ConfigEntry<float> configScaleZMultiplyAmount;

		private const float DefaultScaleXMultiplyAmount = 1.1f;

		private const float DefaultScaleYMultiplyAmount = 2f;

		private const float DefaultScaleZMultiplyAmount = 1.1f;

		public const string Id = "Niko666.ReloadTriggerWellEnlarger";

		public static ReloadTriggerWellEnlarger Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		public static string Name => "ReloadTriggerWellEnlarger";

		public static string Version => "1.0.3";

		public void Awake()
		{
			Instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			configScaleXMultiplyAmount = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ScaleXMultiplyAmount", 1.1f, "Multiply the reload trigger zone on X-axis (width/radius). 1 being weapons' defaults.");
			configScaleYMultiplyAmount = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ScaleYMultiplyAmount", 2f, "Multiply the reload trigger zone on Y-axis (height, most effective). 1 being weapons' defaults.");
			configScaleZMultiplyAmount = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ScaleZMultiplyAmount", 1.1f, "Multiply the reload trigger zone on Z-axis (length along the muzzle). 1 being weapons' defaults.");
			Instance.LoadJsonConfig();
			Harmony.CreateAndPatchAll(typeof(ReloadTriggerWellEnlargerPatch), (string)null);
			Logger.LogMessage((object)("Fuck this world! Sent from Niko666.ReloadTriggerWellEnlarger " + Version));
		}

		public void CreateDefaultJsonConfig(string path)
		{
			try
			{
				string contents = JsonConvert.SerializeObject((object)new RecoilConfigData
				{
					WeaponOverrides = new Dictionary<string, WeaponSettings>
					{
						{
							"InsertFirearmObjectIDHere",
							new WeaponSettings
							{
								ScaleXMultiplyAmount = 1.1f,
								ScaleYMultiplyAmount = 0.9f,
								ScaleZMultiplyAmount = 10f
							}
						},
						{
							"ForExampleM1911Classic",
							new WeaponSettings
							{
								ScaleXMultiplyAmount = 1f,
								ScaleYMultiplyAmount = 1f,
								ScaleZMultiplyAmount = 1f
							}
						}
					}
				}, (Formatting)1);
				File.WriteAllText(path, contents);
				Logger.LogInfo((object)("Created default config at: " + path));
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Error creating default config: " + ex.Message + "\n" + ex.StackTrace));
			}
		}

		public void LoadJsonConfig()
		{
			try
			{
				string directoryName = Path.GetDirectoryName(Paths.BepInExConfigPath);
				if (string.IsNullOrEmpty(directoryName))
				{
					Logger.LogError((object)"Failed to get config directory path.");
					return;
				}
				string path = Path.Combine(directoryName, "Niko666.ReloadTriggerWellEnlarger.json");
				if (!File.Exists(path))
				{
					Logger.LogWarning((object)"Config file not found. Creating default config...");
					CreateDefaultJsonConfig(path);
				}
				RecoilConfigData recoilConfigData = JsonConvert.DeserializeObject<RecoilConfigData>(File.ReadAllText(path));
				if (recoilConfigData != null && recoilConfigData.WeaponOverrides != null)
				{
					WeaponOverrides.Clear();
					foreach (KeyValuePair<string, WeaponSettings> weaponOverride in recoilConfigData.WeaponOverrides)
					{
						WeaponOverrides[weaponOverride.Key] = weaponOverride.Value;
					}
					Logger.LogInfo((object)$"Successfully loaded {WeaponOverrides.Count} weapon overrides.");
					{
						foreach (KeyValuePair<string, WeaponSettings> weaponOverride2 in WeaponOverrides)
						{
							Logger.LogDebug((object)("  - " + weaponOverride2.Key));
						}
						return;
					}
				}
				Logger.LogWarning((object)"No weapon overrides found in config file.");
				WeaponOverrides.Clear();
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Error loading config: " + ex.Message + "\n" + ex.StackTrace));
				WeaponOverrides.Clear();
			}
		}

		public static void CopyAndAdjustTrigger(FVRFireArm orig, string weaponID)
		{
			//IL_00ff: 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_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: 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_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)orig == (Object)null)
			{
				Logger.LogError((object)"CopyAndAdjustTrigger: FVRFireArm instance is null");
				return;
			}
			float num = Instance.configScaleXMultiplyAmount.Value;
			float num2 = Instance.configScaleYMultiplyAmount.Value;
			float num3 = Instance.configScaleZMultiplyAmount.Value;
			if (WeaponOverrides.TryGetValue(weaponID, out var value))
			{
				Logger.LogInfo((object)("Applying weapon-specific override for " + weaponID));
				num = ApplyConfig(value.ScaleXMultiplyAmount, num, "ScaleXMultiplyAmount");
				num2 = ApplyConfig(value.ScaleYMultiplyAmount, num2, "ScaleYMultiplyAmount");
				num3 = ApplyConfig(value.ScaleZMultiplyAmount, num3, "ScaleZMultiplyAmount");
			}
			FVRFireArmReloadTriggerWell[] componentsInChildren = ((Component)orig).GetComponentsInChildren<FVRFireArmReloadTriggerWell>();
			if (componentsInChildren == null || componentsInChildren.Length == 0)
			{
				return;
			}
			Vector3 size2 = default(Vector3);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Collider component = ((Component)componentsInChildren[i]).GetComponent<Collider>();
				BoxCollider val = (BoxCollider)(object)((component is BoxCollider) ? component : null);
				if (val == null)
				{
					SphereCollider val2 = (SphereCollider)(object)((component is SphereCollider) ? component : null);
					if (val2 == null)
					{
						CapsuleCollider val3 = (CapsuleCollider)(object)((component is CapsuleCollider) ? component : null);
						if (val3 != null)
						{
							float radius = val3.radius;
							float radius2 = ValidateFloatNoZero(num * radius, radius, "CapsuleCollider.radius");
							val3.radius = radius2;
							float height = val3.height;
							float height2 = ValidateFloatNoZero(num2 * height, height, "CapsuleCollider.height");
							val3.height = height2;
						}
					}
					else
					{
						float radius3 = val2.radius;
						float radius4 = ValidateFloatNoZero(num * radius3, radius3, "SphereCollider.radius");
						val2.radius = radius4;
					}
				}
				else
				{
					Vector3 size = val.size;
					((Vector3)(ref size2))..ctor(ValidateFloatNoZero(num * size.x, size.x, "BoxCollider.size.x"), ValidateFloatNoZero(num2 * size.y, size.y, "BoxCollider.size.y"), ValidateFloatNoZero(num3 * size.z, size.z, "BoxCollider.size.z"));
					val.size = size2;
				}
			}
		}

		private static float ValidateFloatNoZero(float value, float defaultValue, string paramName = "")
		{
			if (float.IsNaN(value) || float.IsInfinity(value) || value == 0f)
			{
				Logger.LogWarning((object)$"Invalid calculated value for {paramName}: {value}. Using default value {defaultValue}");
				return defaultValue;
			}
			return value;
		}

		private static T ApplyConfig<T>(T? configValue, T defaultValue, string paramName) where T : struct
		{
			if (configValue.HasValue)
			{
				Logger.LogDebug((object)$"  {paramName}: {configValue.Value}");
				return configValue.Value;
			}
			return defaultValue;
		}
	}
	public class ReloadTriggerWellEnlargerPatch
	{
		[HarmonyPatch(typeof(FVRFireArm), "Awake")]
		[HarmonyPostfix]
		public static void AwakePatch(FVRFireArm __instance)
		{
			if ((Object)(object)((FVRPhysicalObject)__instance).ObjectWrapper == (Object)null)
			{
				ReloadTriggerWellEnlarger.Logger.LogWarning((object)("ObjectWrapper is null for " + ((Object)__instance).name + ", how is that possible? Using global scaling."));
				ReloadTriggerWellEnlarger.CopyAndAdjustTrigger(__instance, "");
			}
			else if (string.IsNullOrEmpty(((FVRPhysicalObject)__instance).ObjectWrapper.ItemID))
			{
				ReloadTriggerWellEnlarger.Logger.LogWarning((object)("ItemID is null or empty for " + ((Object)__instance).name + ", how is that possible? Using global scaling."));
				ReloadTriggerWellEnlarger.CopyAndAdjustTrigger(__instance, "");
			}
			else
			{
				ReloadTriggerWellEnlarger.CopyAndAdjustTrigger(__instance, ((FVRPhysicalObject)__instance).ObjectWrapper.ItemID);
			}
		}

		[HarmonyPatch(typeof(SteamVR_LoadLevel), "Begin")]
		[HarmonyPrefix]
		public static bool BeginPatch()
		{
			ReloadTriggerWellEnlarger.Instance.LoadJsonConfig();
			((BaseUnityPlugin)ReloadTriggerWellEnlarger.Instance).Config.Reload();
			return true;
		}
	}
}