Decompiled source of KnockOutWarning v1.1.5

KnockOutWarning.dll

Decompiled 7 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Voice.Unity;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("KnockOutWarning")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("With this mod, players can now knock themselves or their friends out by hitting their heads too hard or by being struck by thrown items.")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyInformationalVersion("1.1.4")]
[assembly: AssemblyProduct("KnockOutWarning")]
[assembly: AssemblyTitle("KnockOutWarning")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.4.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 KnockOutWarning
{
	[BepInPlugin("KnockOutWarning", "KnockOutWarning", "1.1.4")]
	public class Plugin : BaseUnityPlugin
	{
		private GameObject myPlayerObject;

		private Player myPlayer;

		private Recorder myAudioRecorder;

		private GameObject myPostPlayerObject;

		private GameObject myPostSleepObject;

		private bool attached;

		private static ConfigEntry<bool> configRagdollMuteMic { get; set; }

		private static ConfigEntry<bool> configRagdollTriggerByWorld { get; set; }

		private static ConfigEntry<bool> configRagdollTriggerByItems { get; set; }

		private static ConfigEntry<int> configRagdollItemBehaviour { get; set; }

		private static ConfigEntry<int> configRagdollBlurMode { get; set; }

		private static ConfigEntry<float> configRagdollTriggerForceWorld { get; set; }

		private static ConfigEntry<float> configRagdollTriggerForceItems { get; set; }

		private static ConfigEntry<float> configRagdollMinimumDuration { get; set; }

		private static ConfigEntry<float> configRagdollMaximumDuration { get; set; }

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin KnockOutWarning is loaded!");
			configRagdollMuteMic = ((BaseUnityPlugin)this).Config.Bind<bool>("General.Toggles", "RagdollMuteMic", true, "Dictates if the mic is switched off when being knocked out.");
			configRagdollTriggerByWorld = ((BaseUnityPlugin)this).Config.Bind<bool>("General.Toggles", "RagdollTriggerByWorld", true, "Dictates if the ragdoll is triggered when being hit by terrain and static objects.");
			configRagdollTriggerByItems = ((BaseUnityPlugin)this).Config.Bind<bool>("General.Toggles", "RagdollTriggerByItems", true, "Dictates if the ragdoll is triggered when being hit by items and dynamic objects.");
			configRagdollItemBehaviour = ((BaseUnityPlugin)this).Config.Bind<int>("General", "RagdollItemBehaviour", 2, "Dictates if held item is 0:kept in hand (may cause bugs), 1:droped, 2:unequiped when being knocked out.");
			configRagdollBlurMode = ((BaseUnityPlugin)this).Config.Bind<int>("General", "RagdollBlurMode", 2, "Dictates if the vision is 0:normal, 1:blurry, 2:blacked out when being knocked out.");
			configRagdollTriggerForceWorld = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RagdollTriggerForceWorld", 250f, "The amount of force to trigger the ragdoll when hit by terrain and static objects.");
			configRagdollTriggerForceItems = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RagdollForceTriggerItems", 25f, "The amount of force to trigger the ragdoll when hit by items and dynamic objetcs.");
			configRagdollMinimumDuration = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RagdollMinimumDuration", 1.5f, "The minimum amount of time to trigger the ragdoll for.");
			configRagdollMaximumDuration = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RagdollMAximumDuration", 6f, "The maximum amount of time to trigger the ragdoll for.");
		}

		private void Start()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin KnockOutWarning started!");
			SceneManager.sceneLoaded += SetupScene;
		}

		private void SetupScene(Scene scene, LoadSceneMode mode)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Setting up mod for scene {((Scene)(ref scene)).buildIndex}");
			Scene activeScene = SceneManager.GetActiveScene();
			if (((Scene)(ref activeScene)).buildIndex == 0)
			{
				attached = true;
			}
			else
			{
				attached = false;
			}
		}

		public void Update()
		{
			if ((Object)(object)myPlayerObject == (Object)null && !attached)
			{
				ScanComponentsInScene();
			}
			if ((Object)(object)myPostPlayerObject != (Object)null && (Object)(object)myPostSleepObject != (Object)null && !attached)
			{
				PatchBlurRenderer();
			}
			if ((Object)(object)myPlayerObject != (Object)null && !attached)
			{
				AttachKOHandler();
			}
		}

		private void AttachKOHandler()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Attaching KOHandler to {((Object)myPlayerObject).GetInstanceID()}");
			if ((Object)(object)myPlayerObject != (Object)null)
			{
				GameObject gameObject = ((Component)((Component)myPlayerObject.transform.Find("HeadPosition")).gameObject.transform.Find("Voice")).gameObject;
				myAudioRecorder = gameObject.GetComponent<Recorder>();
				GameObject gameObject2 = ((Component)((Component)((Component)((Component)((Component)((Component)myPlayerObject.transform.Find("RigCreator")).gameObject.transform.Find("Rig")).gameObject.transform.Find("Armature")).gameObject.transform.Find("Hip")).gameObject.transform.Find("Torso")).gameObject.transform.Find("Head")).gameObject;
				myPlayer = myPlayerObject.GetComponent<Player>();
				KnockOutHandler knockOutHandler = gameObject2.AddComponent<KnockOutHandler>();
				SphereCollider obj = gameObject2.AddComponent<SphereCollider>();
				((Collider)obj).isTrigger = true;
				obj.radius = 1f;
				knockOutHandler.MyPlayer = myPlayer;
				knockOutHandler.MyPlayerObject = myPlayerObject;
				knockOutHandler.MyAudioRecorder = myAudioRecorder;
				knockOutHandler.MyAudioRecorder.VoiceDetection = true;
				knockOutHandler.RagdollItemBehaviour = configRagdollItemBehaviour;
				knockOutHandler.RagdollBlurMode = configRagdollBlurMode;
				knockOutHandler.RagdollMuteMic = configRagdollMuteMic;
				knockOutHandler.RagdollTriggerByWorld = configRagdollTriggerByWorld;
				knockOutHandler.RagdollTriggerByItems = configRagdollTriggerByItems;
				knockOutHandler.RagdollTriggerForceWorld = configRagdollTriggerForceWorld;
				knockOutHandler.RagdollTriggerForceItems = configRagdollTriggerForceItems;
				knockOutHandler.RagdollMinimumDuration = configRagdollMinimumDuration;
				knockOutHandler.RagdollMaximumDuration = configRagdollMaximumDuration;
				attached = true;
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Player is null");
			}
		}

		private void PatchBlurRenderer()
		{
			try
			{
				MoveComponent((Component)(object)myPostSleepObject.GetComponent<Post_Sleep>(), myPostPlayerObject);
				if (configRagdollBlurMode.Value == 2)
				{
					MoveComponent((Component)(object)myPostSleepObject.GetComponent<Volume>(), myPostPlayerObject);
				}
				myPostPlayerObject.GetComponent<Volume>().weight = 1f;
				myPostSleepObject.SetActive(false);
				myPostPlayerObject.SetActive(true);
			}
			catch
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Post_Player or Post_Sleep is null");
			}
		}

		private Component MoveComponent(Component original, GameObject destination)
		{
			Type type = ((object)original).GetType();
			Component val = destination.GetComponent(type);
			if ((Object)(object)val == (Object)null)
			{
				val = destination.AddComponent(type);
			}
			FieldInfo[] fields = type.GetFields();
			foreach (FieldInfo fieldInfo in fields)
			{
				fieldInfo.SetValue(val, fieldInfo.GetValue(original));
			}
			return val;
		}

		private void ScanComponentsInScene()
		{
			GameObject[] array = Object.FindObjectsOfType<GameObject>(true);
			try
			{
				GameObject[] array2 = array;
				foreach (GameObject val in array2)
				{
					if (((Object)val).name == "Player(Clone)" && val.GetComponent<PhotonView>().IsMine)
					{
						myPlayerObject = val;
					}
					if (((Object)val).name == "GAME")
					{
						GameObject gameObject = ((Component)val.transform.Find("Rendering")).gameObject;
						myPostPlayerObject = ((Component)gameObject.transform.Find("Post_Player")).gameObject;
						myPostSleepObject = ((Component)gameObject.transform.Find("Post_Sleep")).gameObject;
					}
				}
			}
			catch
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Player not initialized");
			}
		}
	}
	public class KnockOutHandler : MonoBehaviour
	{
		private ConfigEntry<bool> ragdollMuteMic;

		private ConfigEntry<bool> ragdollTriggerByWorld;

		private ConfigEntry<bool> ragdollTriggerByItems;

		private ConfigEntry<int> ragdollItemBehaviour;

		private ConfigEntry<int> ragdollBlurMode;

		private ConfigEntry<float> ragdollTriggerForceWorld;

		private ConfigEntry<float> ragdollTriggerForceItems;

		private ConfigEntry<float> ragdollMinimumDuration;

		private ConfigEntry<float> ragdollMaximumDuration;

		private Player myPlayer;

		private GameObject myPlayerObject;

		private Recorder myAudioRecorder;

		private float ragdollCooldown;

		private static float offset = 1f;

		private bool knocked;

		public Player MyPlayer
		{
			get
			{
				return myPlayer;
			}
			set
			{
				myPlayer = value;
			}
		}

		public GameObject MyPlayerObject
		{
			get
			{
				return myPlayerObject;
			}
			set
			{
				myPlayerObject = value;
			}
		}

		public Recorder MyAudioRecorder
		{
			get
			{
				return myAudioRecorder;
			}
			set
			{
				myAudioRecorder = value;
			}
		}

		public ConfigEntry<bool> RagdollMuteMic
		{
			get
			{
				return ragdollMuteMic;
			}
			set
			{
				ragdollMuteMic = value;
			}
		}

		public ConfigEntry<bool> RagdollTriggerByWorld
		{
			get
			{
				return ragdollTriggerByWorld;
			}
			set
			{
				ragdollTriggerByWorld = value;
			}
		}

		public ConfigEntry<bool> RagdollTriggerByItems
		{
			get
			{
				return ragdollTriggerByItems;
			}
			set
			{
				ragdollTriggerByItems = value;
			}
		}

		public ConfigEntry<int> RagdollItemBehaviour
		{
			get
			{
				return ragdollItemBehaviour;
			}
			set
			{
				ragdollItemBehaviour = value;
			}
		}

		public ConfigEntry<int> RagdollBlurMode
		{
			get
			{
				return ragdollBlurMode;
			}
			set
			{
				ragdollBlurMode = value;
			}
		}

		public ConfigEntry<float> RagdollTriggerForceWorld
		{
			get
			{
				return ragdollTriggerForceWorld;
			}
			set
			{
				ragdollTriggerForceWorld = value;
			}
		}

		public ConfigEntry<float> RagdollTriggerForceItems
		{
			get
			{
				return ragdollTriggerForceItems;
			}
			set
			{
				ragdollTriggerForceItems = value;
			}
		}

		public ConfigEntry<float> RagdollMinimumDuration
		{
			get
			{
				return ragdollMinimumDuration;
			}
			set
			{
				ragdollMinimumDuration = value;
			}
		}

		public ConfigEntry<float> RagdollMaximumDuration
		{
			get
			{
				return ragdollMaximumDuration;
			}
			set
			{
				ragdollMaximumDuration = value;
			}
		}

		private void OnCollisionEnter(Collision collider)
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			Vector3 impulse;
			if ((Object)(object)collider.gameObject.GetComponent<ItemInstance>() != (Object)null && ragdollTriggerByItems.Value && !knocked)
			{
				impulse = collider.impulse;
				if (((Vector3)(ref impulse)).magnitude > ragdollTriggerForceItems.Value)
				{
					impulse = collider.impulse;
					Ragdoll(((Vector3)(ref impulse)).magnitude);
				}
			}
			else if (ragdollTriggerByWorld.Value && !knocked)
			{
				impulse = collider.impulse;
				if (((Vector3)(ref impulse)).magnitude > ragdollTriggerForceWorld.Value)
				{
					impulse = collider.impulse;
					Ragdoll(((Vector3)(ref impulse)).magnitude);
				}
			}
		}

		private void Ragdoll(float magnitude)
		{
			knocked = true;
			ragdollCooldown = magnitude / 150f;
			if (ragdollBlurMode.Value != 0)
			{
				myPlayer.data.sleepAmount = ragdollCooldown + offset * 1.5f;
			}
			if (ragdollMuteMic.Value)
			{
				myAudioRecorder.RecordingEnabled = false;
			}
			if (ragdollCooldown < RagdollMinimumDuration.Value)
			{
				ragdollCooldown = RagdollMinimumDuration.Value;
			}
			if (ragdollCooldown > RagdollMaximumDuration.Value)
			{
				ragdollCooldown = RagdollMaximumDuration.Value;
			}
			myPlayer.data.inputOverideAmount = 1f;
			Fall(ragdollCooldown);
		}

		public void Update()
		{
			if (myPlayer.data.fallTime <= 0.5f && knocked)
			{
				if (RagdollItemBehaviour.Value == 2)
				{
					myPlayer.RPC_SelectSlot(-1);
				}
				else if (RagdollItemBehaviour.Value == 1)
				{
					myPlayer.data.dropItemsFor = 0.1f;
				}
				myPlayer.data.fallTime = 0f;
			}
			if (myPlayer.data.fallTime <= 0f && knocked)
			{
				Recover();
			}
		}

		private void Fall(float duration)
		{
			((MonoBehaviour)myPlayer.refs.ragdoll).StartCoroutine("CallFall", (object)duration);
			myPlayer.data.fallTime = duration;
		}

		private void Recover()
		{
			myPlayer.data.inputOverideAmount = 0f;
			myAudioRecorder.RecordingEnabled = true;
			knocked = false;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "KnockOutWarning";

		public const string PLUGIN_NAME = "KnockOutWarning";

		public const string PLUGIN_VERSION = "1.1.4";
	}
}