Decompiled source of ReloadTriggerWellEnlarger v1.0.2

Niko666.ReloadTriggerWellEnlarger.dll

Decompiled 5 days 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.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("Niko666.ReloadTriggerWellEnlarger")]
[assembly: AssemblyTitle("ReloadTriggerWellEnlarger")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.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 Niko666
{
	[BepInProcess("h3vr.exe")]
	[BepInPlugin("Niko666.ReloadTriggerWellEnlarger", "ReloadTriggerWellEnlarger", "1.0.2")]
	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.2";

		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). 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_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: 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_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: 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 localScale2 = default(Vector3);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Transform transform = ((Component)componentsInChildren[i]).transform;
				if ((Object)(object)transform != (Object)null)
				{
					Vector3 localScale = transform.localScale;
					((Vector3)(ref localScale2))..ctor(ValidateFloatNoZero(num * localScale.x, localScale.x, "localScale.x"), ValidateFloatNoZero(num2 * localScale.y, localScale.y, "localScale.y"), ValidateFloatNoZero(num3 * localScale.z, localScale.z, "localScale.z"));
					transform.localScale = localScale2;
				}
			}
		}

		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;
		}
	}
}