Decompiled source of HelAutoSentryFix v0.0.4

HelAutoSentryFix.dll

Decompiled 2 years ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using API;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using FX_EffectSystem;
using GameData;
using Gear;
using HarmonyLib;
using HelSentryFix;
using Il2CppSystem;
using Microsoft.CodeAnalysis;
using Player;
using UnityEngine;
using UnityEngine.Analytics;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("HelAutoSentryFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("HelAutoSentryFix")]
[assembly: AssemblyTitle("HelAutoSentryFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace HelSentryFix
{
	public static class Module
	{
		public const string GUID = "randomuserhi.HelAutoSentryFix";

		public const string Name = "HelAutoSentryFix";

		public const string Version = "0.0.4";
	}
	internal static class ConfigManager
	{
		private static ConfigEntry<bool> debug;

		private static ConfigEntry<bool> sniperSentry_forceNoPentration;

		private static ConfigEntry<int> sniperSentry_persistentID;

		public static bool Debug
		{
			get
			{
				return debug.Value;
			}
			set
			{
				debug.Value = value;
			}
		}

		public static bool SniperSentry_forceNoPentration
		{
			get
			{
				return sniperSentry_forceNoPentration.Value;
			}
			set
			{
				sniperSentry_forceNoPentration.Value = value;
			}
		}

		public static int SniperSentry_persistentID
		{
			get
			{
				return sniperSentry_persistentID.Value;
			}
			set
			{
				sniperSentry_persistentID.Value = value;
			}
		}

		static ConfigManager()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			ConfigFile val = new ConfigFile(Path.Combine(Paths.ConfigPath, "HelAutoSentryFix.cfg"), true);
			debug = val.Bind<bool>("Debug", "enable", false, "Enables debug messages when true.");
			sniperSentry_persistentID = val.Bind<int>("Sniper Sentry", "persistentID", 54, "PersistentID of sniper sentry item");
			sniperSentry_forceNoPentration = val.Bind<bool>("Sniper Sentry", "forceNoPentration", true, "Ignores the penetration on datablock and forces it to not penetrate.");
		}
	}
	[BepInPlugin("randomuserhi.HelAutoSentryFix", "HelAutoSentryFix", "0.0.4")]
	public class Plugin : BasePlugin
	{
		private static Harmony? harmony;

		public override void Load()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			APILogger.Log("Plugin is loaded!");
			harmony = new Harmony("randomuserhi.HelAutoSentryFix");
			harmony.PatchAll();
			APILogger.Log("Debug is " + (ConfigManager.Debug ? "Enabled" : "Disabled"));
		}
	}
	public static class SentryFirePatches
	{
		public delegate void AnySentryFire_Prefix(SentryGunInstance_Firing_Bullets __instance, bool doDamage, bool targetIsTagged);

		public delegate void AnySentryFire_Postfix(SentryGunInstance_Firing_Bullets __instance, bool doDamage, bool targetIsTagged);

		public delegate void SentryFire_Prefix(SentryGunInstance_Firing_Bullets __instance, bool doDamage, bool targetIsTagged);

		public delegate void SentryFire_Postfix(SentryGunInstance_Firing_Bullets __instance, bool doDamage, bool targetIsTagged);

		public delegate void ShotgunSentryFire_Prefix(SentryGunInstance_Firing_Bullets __instance, bool doDamage, bool targetIsTagged);

		public delegate void ShotgunSentryFire_Postfix(SentryGunInstance_Firing_Bullets __instance, bool doDamage, bool targetIsTagged);

		public static AnySentryFire_Prefix? anySentryFire_Prefix;

		public static AnySentryFire_Postfix? anySentryFire_Postfix;

		public static SentryFire_Prefix? sentryFire_Prefix;

		public static SentryFire_Prefix? sentryFire_Postfix;

		public static ShotgunSentryFire_Prefix? shotgunSentryFire_Prefix;

		public static ShotgunSentryFire_Prefix? shotgunSentryFire_Postfix;
	}
}
namespace HelSentryFix.Patches
{
	[HarmonyPatch]
	internal class PlayerSentryPatches
	{
		private static void MakeShot(SentryGunInstance_Firing_Bullets __instance, bool doDamage, bool targetIsTagged)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0436: Unknown result type (might be due to invalid IL or missing references)
			//IL_0441: Unknown result type (might be due to invalid IL or missing references)
			//IL_044b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0450: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0221: Unknown result type (might be due to invalid IL or missing references)
			//IL_0224: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			//IL_0238: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_025e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0263: Unknown result type (might be due to invalid IL or missing references)
			//IL_0266: Unknown result type (might be due to invalid IL or missing references)
			//IL_0270: Unknown result type (might be due to invalid IL or missing references)
			//IL_027a: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0292: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
			SentryGunInstance_Firing_Bullets.s_weaponRayData.owner = __instance.m_core.Owner;
			WeaponHitData s_weaponRayData = SentryGunInstance_Firing_Bullets.s_weaponRayData;
			ArchetypeDataBlock archetypeData = __instance.m_archetypeData;
			PlayerAgent owner = SentryGunInstance_Firing_Bullets.s_weaponRayData.owner;
			RaycastHit rayHit = SentryGunInstance_Firing_Bullets.s_weaponRayData.rayHit;
			s_weaponRayData.damage = archetypeData.GetSentryDamage(owner, ((RaycastHit)(ref rayHit)).distance, targetIsTagged);
			SentryGunInstance_Firing_Bullets.s_weaponRayData.staggerMulti = __instance.m_archetypeData.GetSentryStaggerDamage(targetIsTagged);
			SentryGunInstance_Firing_Bullets.s_weaponRayData.precisionMulti = __instance.m_archetypeData.PrecisionDamageMulti;
			SentryGunInstance_Firing_Bullets.s_weaponRayData.damageFalloff = __instance.m_archetypeData.DamageFalloff;
			SentryGunInstance_Firing_Bullets.s_weaponRayData.vfxBulletHit = __instance.m_vfxBulletHit;
			WeaponHitData s_weaponRayData2 = SentryGunInstance_Firing_Bullets.s_weaponRayData;
			bool flag = ((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).persistentID == ConfigManager.SniperSentry_persistentID && ConfigManager.SniperSentry_forceNoPentration;
			if (flag)
			{
				APILogger.Debug($"Sniper sentry [{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).name}:{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).persistentID}] is ignored.");
			}
			if (__instance.m_archetypeData.PiercingBullets && !flag)
			{
				APILogger.Debug($"Sentry has pierce! [{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).name}:{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).persistentID}]");
				int num = 5;
				int num2 = 0;
				bool flag2 = false;
				float num3 = 0f;
				int num4 = 0;
				Vector3 val = __instance.MuzzleAlign.position;
				while (!flag2 && num2 < num && SentryGunInstance_Firing_Bullets.s_weaponRayData.maxRayDist > 0f && num4 < __instance.m_archetypeData.PiercingDamageCountLimit)
				{
					APILogger.Debug($"Pierced {num2}");
					if (Weapon.CastWeaponRay(__instance.MuzzleAlign, ref s_weaponRayData2, val, LayerManager.MASK_SENTRYGUN_RAY))
					{
						SentryGunInstance_Firing_Bullets.s_weaponRayData = s_weaponRayData2;
						if (BulletWeapon.BulletHit(SentryGunInstance_Firing_Bullets.s_weaponRayData, doDamage, 0f, 0u, false))
						{
							num4++;
						}
						rayHit = SentryGunInstance_Firing_Bullets.s_weaponRayData.rayHit;
						FX_Manager.EffectTargetPosition = ((RaycastHit)(ref rayHit)).point;
						rayHit = SentryGunInstance_Firing_Bullets.s_weaponRayData.rayHit;
						flag2 = !CustomExtensions.IsInLayerMask(((Component)((RaycastHit)(ref rayHit)).collider).gameObject, LayerMask.op_Implicit(LayerManager.MASK_BULLETWEAPON_PIERCING_PASS));
						rayHit = SentryGunInstance_Firing_Bullets.s_weaponRayData.rayHit;
						val = ((RaycastHit)(ref rayHit)).point + SentryGunInstance_Firing_Bullets.s_weaponRayData.fireDir * 0.1f;
						float num5 = num3;
						rayHit = SentryGunInstance_Firing_Bullets.s_weaponRayData.rayHit;
						num3 = num5 + ((RaycastHit)(ref rayHit)).distance;
						WeaponHitData s_weaponRayData3 = SentryGunInstance_Firing_Bullets.s_weaponRayData;
						float maxRayDist = s_weaponRayData3.maxRayDist;
						rayHit = SentryGunInstance_Firing_Bullets.s_weaponRayData.rayHit;
						s_weaponRayData3.maxRayDist = maxRayDist - ((RaycastHit)(ref rayHit)).distance;
					}
					else
					{
						flag2 = true;
						FX_Manager.EffectTargetPosition = __instance.MuzzleAlign.position + __instance.MuzzleAlign.forward * 50f;
					}
					num2++;
				}
			}
			else if (Weapon.CastWeaponRay(__instance.MuzzleAlign, ref s_weaponRayData2, LayerManager.MASK_SENTRYGUN_RAY))
			{
				APILogger.Debug($"Sentry has no pierce! [{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).name}:{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).persistentID}]");
				SentryGunInstance_Firing_Bullets.s_weaponRayData = s_weaponRayData2;
				BulletWeapon.BulletHit(SentryGunInstance_Firing_Bullets.s_weaponRayData, doDamage, 0f, 0u, false);
				rayHit = SentryGunInstance_Firing_Bullets.s_weaponRayData.rayHit;
				FX_Manager.EffectTargetPosition = ((RaycastHit)(ref rayHit)).point;
			}
			else
			{
				APILogger.Debug($"Sentry missed! [{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).name}:{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).persistentID}]");
				FX_Manager.EffectTargetPosition = __instance.MuzzleAlign.position + __instance.MuzzleAlign.forward * 50f;
			}
		}

		[HarmonyPatch(typeof(SentryGunInstance_Firing_Bullets), "FireBullet")]
		[HarmonyPrefix]
		private static bool SentryGunFiringBullet(SentryGunInstance_Firing_Bullets __instance, bool doDamage, bool targetIsTagged)
		{
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Expected O, but got Unknown
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: 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_0128: Unknown result type (might be due to invalid IL or missing references)
			APILogger.Debug($"Sentry Shoot [{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).name}:{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).persistentID}]");
			SentryFirePatches.sentryFire_Prefix?.Invoke(__instance, doDamage, targetIsTagged);
			SentryFirePatches.anySentryFire_Prefix?.Invoke(__instance, doDamage, targetIsTagged);
			SentryGunInstance_Firing_Bullets.s_weaponRayData = new WeaponHitData
			{
				randomSpread = __instance.m_archetypeData.HipFireSpread,
				fireDir = __instance.MuzzleAlign.forward
			};
			Vector3 val = default(Vector3);
			if (__instance.m_archetypeData.Sentry_FireTowardsTargetInsteadOfForward && __instance.m_core.TryGetTargetAimPos(ref val))
			{
				WeaponHitData s_weaponRayData = SentryGunInstance_Firing_Bullets.s_weaponRayData;
				Vector3 val2 = val - __instance.MuzzleAlign.position;
				s_weaponRayData.fireDir = ((Vector3)(ref val2)).normalized;
			}
			MakeShot(__instance, doDamage, targetIsTagged);
			Action onBulletFired = __instance.OnBulletFired;
			if (onBulletFired != null)
			{
				onBulletFired.Invoke();
			}
			((FX_EffectBase)SentryGunInstance_Firing_Bullets.s_tracerPool.AquireEffect()).Play((FX_Trigger)null, __instance.MuzzleAlign.position, Quaternion.LookRotation(SentryGunInstance_Firing_Bullets.s_weaponRayData.fireDir));
			EX_SpriteMuzzleFlash muzzleFlash = __instance.m_muzzleFlash;
			if (muzzleFlash != null)
			{
				muzzleFlash.Play();
			}
			WeaponShellManager.EjectShell((ShellTypes)3, 1f, 1f, __instance.ShellEjectAlign);
			__instance.m_fireBulletTimer = Clock.Time + __instance.m_archetypeData.GetSentryShotDelay(__instance.m_core.Owner, targetIsTagged);
			SentryFirePatches.sentryFire_Postfix?.Invoke(__instance, doDamage, targetIsTagged);
			SentryFirePatches.anySentryFire_Postfix?.Invoke(__instance, doDamage, targetIsTagged);
			return false;
		}

		[HarmonyPatch(typeof(SentryGunInstance_Firing_Bullets), "UpdateFireShotgunSemi")]
		[HarmonyPrefix]
		private static bool ShotgunSentryFiring(SentryGunInstance_Firing_Bullets __instance, bool isMaster, bool targetIsTagged)
		{
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: 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_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Expected O, but got Unknown
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			if (!(Clock.Time > __instance.m_fireBulletTimer))
			{
				return false;
			}
			APILogger.Debug($"Shotgun Sentry Shoot [{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).name}:{((GameDataBlockBase<ArchetypeDataBlock>)(object)__instance.m_archetypeData).persistentID}]");
			SentryFirePatches.shotgunSentryFire_Prefix?.Invoke(__instance, isMaster, targetIsTagged);
			SentryFirePatches.anySentryFire_Prefix?.Invoke(__instance, isMaster, targetIsTagged);
			_ = __instance.MuzzleAlign.position;
			__instance.TriggerSingleFireAudio();
			for (int i = 0; i < __instance.m_archetypeData.ShotgunBulletCount; i++)
			{
				float num = __instance.m_segmentSize * (float)i;
				float num2 = 0f;
				float num3 = 0f;
				if (i > 0)
				{
					num2 += (float)__instance.m_archetypeData.ShotgunConeSize * Mathf.Cos(num);
					num3 += (float)__instance.m_archetypeData.ShotgunConeSize * Mathf.Sin(num);
				}
				SentryGunInstance_Firing_Bullets.s_weaponRayData = new WeaponHitData
				{
					maxRayDist = __instance.MaxRayDist,
					angOffsetX = num2,
					angOffsetY = num3,
					randomSpread = __instance.m_archetypeData.ShotgunBulletSpread,
					fireDir = __instance.MuzzleAlign.forward
				};
				MakeShot(__instance, isMaster, targetIsTagged);
				FX_Manager.PlayLocalVersion = false;
				((FX_EffectBase)SentryGunInstance_Firing_Bullets.s_tracerPool.AquireEffect()).Play((FX_Trigger)null, __instance.MuzzleAlign.position, Quaternion.LookRotation(SentryGunInstance_Firing_Bullets.s_weaponRayData.fireDir));
			}
			__instance.UpdateAmmo(-1);
			Action onBulletFired = __instance.OnBulletFired;
			if (onBulletFired != null)
			{
				onBulletFired.Invoke();
			}
			__instance.m_fireBulletTimer = Clock.Time + __instance.m_archetypeData.GetSentryShotDelay(__instance.m_core.Owner, targetIsTagged);
			SentryFirePatches.shotgunSentryFire_Postfix?.Invoke(__instance, isMaster, targetIsTagged);
			SentryFirePatches.anySentryFire_Postfix?.Invoke(__instance, isMaster, targetIsTagged);
			return false;
		}
	}
}
namespace API
{
	[HarmonyPatch(typeof(GameDataInit))]
	internal class GameDataInit_Patches
	{
		[HarmonyPatch("Initialize")]
		[HarmonyWrapSafe]
		[HarmonyPostfix]
		public static void Initialize_Postfix()
		{
			Analytics.enabled = false;
		}
	}
	internal static class APILogger
	{
		private static readonly ManualLogSource logger;

		static APILogger()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Expected O, but got Unknown
			logger = new ManualLogSource("Rand-API");
			Logger.Sources.Add((ILogSource)(object)logger);
		}

		private static string Format(string module, object msg)
		{
			return $"[{module}]: {msg}";
		}

		public static void Info(string module, object data)
		{
			logger.LogMessage((object)Format(module, data));
		}

		public static void Verbose(string module, object data)
		{
		}

		public static void Log(object data)
		{
			logger.LogDebug((object)Format("HelAutoSentryFix", data));
		}

		public static void Debug(object data)
		{
			if (ConfigManager.Debug)
			{
				Log(data);
			}
		}

		public static void Warn(object data)
		{
			logger.LogWarning((object)Format("HelAutoSentryFix", data));
		}

		public static void Error(object data)
		{
			logger.LogError((object)Format("HelAutoSentryFix", data));
		}
	}
}