Decompiled source of Gun Sniper Rifle Config v1.0.1

TheSniperRifleConfig.dll

Decompiled a day ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;

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

[BepInPlugin("alpha.TheSniperRifleConfig", "TheSniperRifle Config", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	public static ConfigEntry<int> HipDamage;

	public static ConfigEntry<int> ZoomDamage;

	public static ConfigEntry<float> ZoomMagnification;

	public static ConfigEntry<float> ZoomStep;

	public static ConfigEntry<float> LaserDrainRate;

	public static ConfigEntry<float> ShootCooldown;

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ef: Expected O, but got Unknown
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Expected O, but got Unknown
		//IL_01db: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e8: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		HipDamage = ((BaseUnityPlugin)this).Config.Bind<int>("Damage", "HipDamage", 250, "Damage when NOT zoomed (hip-fire).");
		ZoomDamage = ((BaseUnityPlugin)this).Config.Bind<int>("Damage", "ZoomDamage", 355, "Damage when zoomed.");
		ZoomMagnification = ((BaseUnityPlugin)this).Config.Bind<float>("Zoom", "ZoomMagnification", 7f, "Default zoom magnification. FOV = 70 / magnification (7 = ~10 deg FOV, 14 = ~5 deg).");
		ZoomStep = ((BaseUnityPlugin)this).Config.Bind<float>("Zoom", "ZoomStep", 0.1f, "Zoom tuning step per scroll tick, in FOV degrees.");
		LaserDrainRate = ((BaseUnityPlugin)this).Config.Bind<float>("Laser", "LaserDrainRate", 0.1f, "Battery drained per frame while the laser dot is on.");
		ShootCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("Gun", "ShootCooldown", 5f, "Cooldown between shots (the reload/bolt-cycle time). The mod's default is 5 seconds.");
		Harmony val = new Harmony("alpha.TheSniperRifleConfig");
		Type type = AccessTools.TypeByName("TheSniperRifle.ItemSniperGun");
		if (type != null)
		{
			MethodInfo methodInfo = AccessTools.Method(type, "Awake", (Type[])null, (Type[])null);
			if (methodInfo != null)
			{
				val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(Patches).GetMethod("ItemSniperGun_Awake_Postfix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				Log.LogInfo((object)"Patched ItemSniperGun.Awake.");
			}
			else
			{
				Log.LogWarning((object)"ItemSniperGun.Awake not found.");
			}
		}
		else
		{
			Log.LogWarning((object)"TheSniperRifle.ItemSniperGun type not found.");
		}
		Type type2 = AccessTools.TypeByName("TheSniperRifle.SniperLaserDot");
		if (type2 != null)
		{
			MethodInfo methodInfo = AccessTools.Method(type2, "Awake", (Type[])null, (Type[])null);
			if (methodInfo != null)
			{
				val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(Patches).GetMethod("SniperLaserDot_Awake_Postfix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				Log.LogInfo((object)"Patched SniperLaserDot.Awake.");
			}
			else
			{
				Log.LogWarning((object)"SniperLaserDot.Awake not found.");
			}
		}
		else
		{
			Log.LogWarning((object)"TheSniperRifle.SniperLaserDot type not found.");
		}
		Log.LogInfo((object)"TheSniperRifle Config loaded.");
	}
}
public static class Patches
{
	public static void ItemSniperGun_Awake_Postfix(object __instance)
	{
		if (__instance == null)
		{
			return;
		}
		Type type = __instance.GetType();
		try
		{
			FieldInfo field = type.GetField("enemyHurtDamage", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field != null)
			{
				int[] array = (int[])field.GetValue(__instance);
				if (array != null && array.Length >= 2)
				{
					array[0] = Plugin.HipDamage.Value;
					array[1] = Plugin.ZoomDamage.Value;
				}
			}
			else
			{
				Plugin.Log.LogWarning((object)"enemyHurtDamage field not found.");
			}
			float num = 70f / ClampMagnification(Plugin.ZoomMagnification.Value);
			SetField(type, __instance, "cameraZoom_FOV1_ori", num, BindingFlags.Instance | BindingFlags.NonPublic);
			FieldInfo field2 = type.GetField("cameraZoom_FOV", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field2 != null)
			{
				float[] array2 = (float[])field2.GetValue(__instance);
				if (array2 != null && array2.Length >= 2)
				{
					array2[1] = num;
				}
			}
			else
			{
				Plugin.Log.LogWarning((object)"cameraZoom_FOV field not found.");
			}
			SetField(type, __instance, "ZoomTuningStrength", Plugin.ZoomStep.Value, BindingFlags.Instance | BindingFlags.Public);
			FieldInfo field3 = type.GetField("itemGun", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field3 != null)
			{
				object value = field3.GetValue(__instance);
				if (value != null)
				{
					FieldInfo field4 = value.GetType().GetField("shootCooldown", BindingFlags.Instance | BindingFlags.Public);
					if (field4 != null)
					{
						field4.SetValue(value, Plugin.ShootCooldown.Value);
					}
					else
					{
						Plugin.Log.LogWarning((object)"ItemGun.shootCooldown field not found.");
					}
				}
			}
			else
			{
				Plugin.Log.LogWarning((object)"itemGun field not found.");
			}
			MethodInfo method = type.GetMethod("UpdateBulletPrefabHurtDamage", BindingFlags.Instance | BindingFlags.NonPublic);
			if (method != null)
			{
				try
				{
					method.Invoke(__instance, new object[1] { false });
					return;
				}
				catch (Exception ex)
				{
					Plugin.Log.LogWarning((object)("UpdateBulletPrefabHurtDamage invoke failed: " + ex.Message));
					return;
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)("ItemSniperGun postfix failed: " + ex));
		}
	}

	public static void SniperLaserDot_Awake_Postfix(object __instance)
	{
		if (__instance != null)
		{
			FieldInfo field = __instance.GetType().GetField("drainRate", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field != null)
			{
				field.SetValue(__instance, Plugin.LaserDrainRate.Value);
			}
			else
			{
				Plugin.Log.LogWarning((object)"drainRate field not found.");
			}
		}
	}

	private static void SetField(Type t, object inst, string name, object value, BindingFlags flags)
	{
		FieldInfo field = t.GetField(name, flags);
		if (field != null)
		{
			field.SetValue(inst, value);
		}
		else
		{
			Plugin.Log.LogWarning((object)("Field not found: " + name));
		}
	}

	private static float ClampMagnification(float mag)
	{
		if (mag < 0.1f)
		{
			return 0.1f;
		}
		if (mag > 140f)
		{
			return 140f;
		}
		return mag;
	}
}