Decompiled source of Knockout Scripts v1.0.2

KnockoutScripts.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BepInEx;
using BepInEx.Configuration;
using FistVR;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("KnockoutScripts")]
[assembly: AssemblyDescription("Scripts for use with non-lethal sleeping ammunition, adding some togglable features to expand on how the darts operate.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Knockout Scripts")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("b3b41b10-ddc7-46ff-8f79-0e3d91f8e72b")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: AssemblyVersion("1.0.1.0")]
namespace mehongo;

[BepInPlugin("h3vr.mehongo.KnockoutScripts", "Knockout Scripts", "1.0.1")]
public class KnockoutSyringeScripts : BaseUnityPlugin
{
	internal static ConfigEntry<float> headshotSFXVolume;

	internal static ConfigEntry<bool> headshotKnocksout;

	internal static ConfigEntry<bool> multiShotTimerDecay;

	internal static ConfigEntry<bool> lowPressureCycle;

	private const string knockoutScriptsCatName = "Knockout Syringe Scripts";

	private static AudioEvent tranqHeadshotSound = new AudioEvent
	{
		PitchRange = new Vector2(0.8f, 1.2f)
	};

	private const string HarmonyID = "MeHongo-KnockoutScripts";

	internal static KnockoutSyringeScripts Instance { get; private set; }

	private void Awake()
	{
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Expected O, but got Unknown
		Instance = this;
		Harmony.CreateAndPatchAll(((object)this).GetType(), "MeHongo-KnockoutScripts");
		headshotSFXVolume = ((BaseUnityPlugin)this).Config.Bind<float>("Knockout Syringe Scripts", "Headshot SFX Volume", 1f, "Volume of the headshot sound effect that plays when shooting a tranquilizer round into a sosig's dumb long head");
		tranqHeadshotSound.VolumeRange = Vector2.one * headshotSFXVolume.Value;
		headshotSFXVolume.SettingChanged += delegate
		{
			//IL_0006: 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_001a: Unknown result type (might be due to invalid IL or missing references)
			tranqHeadshotSound.VolumeRange = Vector2.one * headshotSFXVolume.Value;
		};
		headshotKnocksout = ((BaseUnityPlugin)this).Config.Bind<bool>("Knockout Syringe Scripts", "Headshot Instantly Knocks Out", true, "Enables/Disables enemies being instantly knocked out when headshot with a tranquilizer.");
		lowPressureCycle = ((BaseUnityPlugin)this).Config.Bind<bool>("Knockout Syringe Scripts", "Cycle Low Pressure Tranquilizer Rounds", false, "Enables/Disables low pressure tranquilizer rounds auto-cycling the chamber in semi-auto firearms. If off, it will mean that the chamber must be manually cycled (Recommended off due to High Pressure variants existing)");
		multiShotTimerDecay = ((BaseUnityPlugin)this).Config.Bind<bool>("Knockout Syringe Scripts", "Multi Shot Timer Decay", true, "Enable/Disable shooting an enemy with a tranquilizer while it is already counting down to sleep will decrease the timer");
		string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		if (Directory.Exists(directoryName))
		{
			string[] files = Directory.GetFiles(directoryName);
			foreach (string text in files)
			{
				if (text.Contains(".wav"))
				{
					WWW www = new WWW("file://" + text);
					((MonoBehaviour)this).StartCoroutine(LoadHeadshotSound(www));
				}
			}
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"Specified path doesn't exist");
		}
	}

	private IEnumerator LoadHeadshotSound(WWW www)
	{
		while (!www.isDone)
		{
			((BaseUnityPlugin)this).Logger.LogMessage((object)$"Tranq Headshot sound effect loading Progress: {www.progress}");
			yield return null;
		}
		AudioClip audioClip = WWWAudioExtensions.GetAudioClip(www, false, true, (AudioType)20);
		((Object)audioClip).name = "TranqHeadshotSound";
		tranqHeadshotSound.Clips.Add(audioClip);
	}

	private static void DisableSyringeCollisions(SyringeProjectile syringe)
	{
		Transform val = ((Component)syringe).transform.Find("Blade");
		if (Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)((Component)val).GetComponent<Collider>()))
		{
			((Component)val).GetComponent<Collider>().enabled = false;
		}
		Transform val2 = ((Component)syringe).transform.Find("Phys");
		if (Object.op_Implicit((Object)(object)val2) && Object.op_Implicit((Object)(object)((Component)val2).GetComponent<Collider>()))
		{
			((Component)val2).GetComponent<Collider>().enabled = false;
		}
	}

	[HarmonyPatch(typeof(Handgun), "Fire")]
	[HarmonyPrefix]
	private static void LowPressureSlidePrefix(Handgun __instance, ref bool ___m_isSlideLockMechanismEngaged, ref bool __state)
	{
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Invalid comparison between Unknown and I4
		if (!lowPressureCycle.Value && Object.op_Implicit((Object)(object)__instance.Chamber) && Object.op_Implicit((Object)(object)__instance.Chamber.GetRound()))
		{
			__state = ___m_isSlideLockMechanismEngaged;
			if (!__instance.Chamber.GetRound().IsHighPressure && (int)__instance.Chamber.GetRound().RoundClass == 201)
			{
				___m_isSlideLockMechanismEngaged = true;
			}
		}
	}

	[HarmonyPatch(typeof(Handgun), "Fire")]
	[HarmonyPostfix]
	private static void LowPressureSlidePostfix(Handgun __instance, ref bool ___m_isSlideLockMechanismEngaged, ref bool __state)
	{
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Invalid comparison between Unknown and I4
		if (!lowPressureCycle.Value && Object.op_Implicit((Object)(object)__instance.Chamber) && Object.op_Implicit((Object)(object)__instance.Chamber.GetRound()) && !__instance.Chamber.GetRound().IsHighPressure && (int)__instance.Chamber.GetRound().RoundClass == 201)
		{
			___m_isSlideLockMechanismEngaged = __state;
		}
	}

	[HarmonyPatch(typeof(ClosedBolt), "ImpartFiringImpulse")]
	[HarmonyPrefix]
	public static void LowPressureClosedBolt(ref bool __runOriginal, ClosedBolt __instance)
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Invalid comparison between Unknown and I4
		if ((!lowPressureCycle.Value & __runOriginal) && Object.op_Implicit((Object)(object)__instance.Weapon) && Object.op_Implicit((Object)(object)__instance.Weapon.Chamber))
		{
			FVRFireArmRound round = __instance.Weapon.Chamber.GetRound();
			if (Object.op_Implicit((Object)(object)round) && !round.IsHighPressure && (int)round.RoundClass == 201)
			{
				__runOriginal = false;
			}
		}
	}

	[HarmonyPatch(typeof(OpenBoltReceiverBolt), "ImpartFiringImpulse")]
	[HarmonyPrefix]
	public static void LowPressureOpenBolt(ref bool __runOriginal, OpenBoltReceiverBolt __instance)
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Invalid comparison between Unknown and I4
		if ((!lowPressureCycle.Value & __runOriginal) && Object.op_Implicit((Object)(object)__instance.Receiver) && Object.op_Implicit((Object)(object)__instance.Receiver.Chamber))
		{
			FVRFireArmRound round = __instance.Receiver.Chamber.GetRound();
			if (Object.op_Implicit((Object)(object)round) && !round.IsHighPressure && (int)round.RoundClass == 201)
			{
				__runOriginal = false;
			}
		}
	}

	[HarmonyPatch(typeof(Sosig), "DelayedKnockout")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> SosigMultiSyringeKnockoutPostFix(IEnumerable<CodeInstruction> instructions, ILGenerator generator, MethodBase __originalMethod)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Expected O, but got Unknown
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Expected O, but got Unknown
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Expected O, but got Unknown
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Expected O, but got Unknown
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Expected O, but got Unknown
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Expected O, but got Unknown
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Expected O, but got Unknown
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Expected O, but got Unknown
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Expected O, but got Unknown
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d5: Expected O, but got Unknown
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Expected O, but got Unknown
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Expected O, but got Unknown
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0151: Expected O, but got Unknown
		//IL_015e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Expected O, but got Unknown
		//IL_016c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Expected O, but got Unknown
		//IL_018d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0193: Expected O, but got Unknown
		//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Expected O, but got Unknown
		//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Expected O, but got Unknown
		//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Expected O, but got Unknown
		//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e3: Expected O, but got Unknown
		//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fb: Expected O, but got Unknown
		//IL_0204: Unknown result type (might be due to invalid IL or missing references)
		//IL_020a: Expected O, but got Unknown
		//IL_0226: Unknown result type (might be due to invalid IL or missing references)
		//IL_022c: Expected O, but got Unknown
		//IL_023a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0240: Expected O, but got Unknown
		Label label = default(Label);
		CodeMatcher val = new CodeMatcher(instructions, generator);
		Label label2 = default(Label);
		val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[4]
		{
			new CodeMatch(new CodeInstruction(OpCodes.Ldarg_1, (object)null), (string)null),
			new CodeMatch(new CodeInstruction(OpCodes.Add, (object)null), (string)null),
			new CodeMatch(new CodeInstruction(OpCodes.Stfld, (object)AccessTools.Field(typeof(Sosig), "m_unconsciousTime")), (string)null),
			new CodeMatch(new CodeInstruction(OpCodes.Ldarg_0, (object)null), (string)null)
		}).CreateLabel(ref label).End()
			.CreateLabel(ref label2)
			.MatchBack(true, (CodeMatch[])(object)new CodeMatch[2]
			{
				new CodeMatch(new CodeInstruction(OpCodes.Stfld, (object)AccessTools.Field(typeof(Sosig), "m_unconsciousTime")), (string)null),
				new CodeMatch(new CodeInstruction(OpCodes.Ldarg_0, (object)null), (string)null)
			});
		if (!val.ReportFailure(__originalMethod, (Action<string>)((BaseUnityPlugin)Instance).Logger.LogFatal))
		{
			val.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[13]
			{
				new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(KnockoutSyringeScripts), "multiShotTimerDecay")),
				new CodeInstruction(OpCodes.Call, (object)AccessTools.PropertyGetter(typeof(ConfigEntry<bool>), "Value")),
				new CodeInstruction(OpCodes.Brfalse, (object)label),
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(Sosig), "m_isDelayedKnockingOut")),
				new CodeInstruction(OpCodes.Brfalse_S, (object)label),
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(Sosig), "m_timeTilKnockout")),
				new CodeInstruction(OpCodes.Ldc_R4, (object)2f),
				new CodeInstruction(OpCodes.Div, (object)null),
				new CodeInstruction(OpCodes.Stfld, (object)AccessTools.Field(typeof(Sosig), "m_timeTilKnockout")),
				new CodeInstruction(OpCodes.Br_S, (object)label2)
			});
		}
		List<CodeInstruction> list = val.Instructions();
		return val.InstructionEnumeration();
	}

	[HarmonyPatch(typeof(SyringeProjectile), "FVRUpdate")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> MakeSyringeHeadshotsInstaKnockTranspiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator, MethodBase __originalMethod)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Expected O, but got Unknown
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Expected O, but got Unknown
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Expected O, but got Unknown
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Expected O, but got Unknown
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Expected O, but got Unknown
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Expected O, but got Unknown
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Expected O, but got Unknown
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Expected O, but got Unknown
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_010f: Expected O, but got Unknown
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Expected O, but got Unknown
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0144: Expected O, but got Unknown
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Expected O, but got Unknown
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0173: Expected O, but got Unknown
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Expected O, but got Unknown
		//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b7: Expected O, but got Unknown
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cc: Expected O, but got Unknown
		//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Expected O, but got Unknown
		//IL_020a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0210: Expected O, but got Unknown
		//IL_022c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0232: Expected O, but got Unknown
		//IL_024e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0254: Expected O, but got Unknown
		//IL_0272: Unknown result type (might be due to invalid IL or missing references)
		//IL_0278: Expected O, but got Unknown
		//IL_0281: Unknown result type (might be due to invalid IL or missing references)
		//IL_0287: Expected O, but got Unknown
		//IL_0290: Unknown result type (might be due to invalid IL or missing references)
		//IL_0296: Expected O, but got Unknown
		//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b8: Expected O, but got Unknown
		//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02dc: Expected O, but got Unknown
		//IL_02eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f1: Expected O, but got Unknown
		CodeMatcher val = new CodeMatcher(instructions, generator);
		val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[3]
		{
			new CodeMatch(new CodeInstruction(OpCodes.Ldarg_0, (object)null), (string)null),
			new CodeMatch(new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(SyringeProjectile), "DoesKnockoutDamage")), (string)null),
			new CodeMatch(new CodeInstruction(OpCodes.Brfalse, (object)null), (string)null)
		});
		if (!val.ReportFailure(__originalMethod, (Action<string>)((BaseUnityPlugin)Instance).Logger.LogFatal))
		{
			Label label = (Label)val.Instruction.operand;
			List<CodeInstruction> list = new List<CodeInstruction>();
			Label label2 = default(Label);
			val.Advance(1).CreateLabel(ref label2).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[20]
			{
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Callvirt, (object)AccessTools.Method(typeof(KnockoutSyringeScripts), "DisableSyringeCollisions", (Type[])null, (Type[])null)),
				new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(KnockoutSyringeScripts), "headshotKnocksout")),
				new CodeInstruction(OpCodes.Call, (object)AccessTools.PropertyGetter(typeof(ConfigEntry<bool>), "Value")),
				new CodeInstruction(OpCodes.Brfalse, (object)label2),
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(FVRPhysicalObject), "MP")),
				new CodeInstruction(OpCodes.Callvirt, (object)AccessTools.Method(typeof(MeleeParams), "GetStabLink", (Type[])null, (Type[])null)),
				new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(SosigLink), "BodyPart")),
				new CodeInstruction(OpCodes.Brtrue_S, (object)label2),
				new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(KnockoutSyringeScripts), "tranqHeadshotSound")),
				new CodeInstruction(OpCodes.Call, (object)AccessTools.PropertyGetter(typeof(GM), "CurrentPlayerBody")),
				new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(FVRPlayerBody), "Head")),
				new CodeInstruction(OpCodes.Callvirt, (object)AccessTools.PropertyGetter(typeof(Transform), "position")),
				new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(SM), "PlayGenericSound", (Type[])null, (Type[])null)),
				new CodeInstruction(OpCodes.Ldloc_0, (object)null),
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(SyringeProjectile), "KnockoutDamage_Amount")),
				new CodeInstruction(OpCodes.Callvirt, (object)AccessTools.Method(typeof(Sosig), "KnockUnconscious", (Type[])null, (Type[])null)),
				new CodeInstruction(OpCodes.Br_S, (object)label)
			});
		}
		List<CodeInstruction> list2 = val.Instructions();
		return val.InstructionEnumeration();
	}
}