Decompiled source of Hiccups v0.1.1

Hiccups.dll

Decompiled an hour ago
using System;
using System.Collections;
using System.Collections.Generic;
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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Hiccups")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1")]
[assembly: AssemblyProduct("Hiccups Mod")]
[assembly: AssemblyTitle("Hiccups")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 hiccups
{
	public class hiccupsRunner : MonoBehaviour
	{
		[HarmonyPatch(typeof(PlayerTumble), "TumbleImpactRPC")]
		private static class tumbleImpactPatch
		{
			private static void Postfix(PlayerTumble __instance, int _playerID)
			{
				if (!((Object)(object)instance == (Object)null) && !(instance.passCooldown > 0f) && SemiFunc.IsMasterClientOrSingleplayer() && !((Object)(object)instance.holder == (Object)null) && !((Object)(object)__instance.playerAvatar != (Object)(object)instance.holder))
				{
					PlayerAvatar val = instance.findPlayer(_playerID);
					if (!((Object)(object)val == (Object)null) && !val.isDisabled)
					{
						instance.sendCode(169, _playerID);
						instance.hiccupTimer = Random.Range(0.75f, 1.25f) * (60f / Plugin.frequency.Value);
						instance.holderTimer = Random.Range(30f, 60f);
						instance.passCooldown = 5f;
					}
				}
			}
		}

		public static hiccupsRunner instance;

		private const byte resultEventCode = 170;

		private const byte holderEventCode = 169;

		private const byte hiccupEventCode = 167;

		private AudioClip inhaleClip;

		private AudioClip hicClip;

		private bool prevActive;

		private float hiccupTimer;

		private float holderTimer;

		private float phaseTimer;

		private bool isHiccupPhase;

		private float tellTime = 0.8f;

		private PlayerAvatar holder;

		private bool windowOpen;

		private bool jumpedInWindow;

		private bool crouchedInWindow;

		private bool alreadyCrouched;

		private bool isTumblingInWindow;

		private const float boutMin = 90f;

		private const float boutMax = 150f;

		private const float breakMin = 60f;

		private const float breakMax = 120f;

		private const float rotationMin = 30f;

		private const float rotationMax = 60f;

		private const float defaultHiccupForce = 1f;

		private ParticleScriptExplosion boom;

		private float passCooldown;

		private void Start()
		{
			((MonoBehaviour)this).StartCoroutine(loadSounds());
			PhotonNetwork.NetworkingClient.EventReceived += onEvent;
			boom = ((Component)this).gameObject.AddComponent<ParticleScriptExplosion>();
			instance = this;
		}

		private static bool isLevelActive()
		{
			if (!((Object)(object)RunManager.instance != (Object)null) || !Object.op_Implicit((Object)(object)LevelGenerator.Instance))
			{
				return false;
			}
			if (SemiFunc.RunIsLevel())
			{
				return SemiFunc.LevelGenDone();
			}
			return false;
		}

		private void Update()
		{
			if (windowOpen && (Object)(object)holder != (Object)null && holder.isLocal)
			{
				if (SemiFunc.InputDown((InputKey)1))
				{
					jumpedInWindow = true;
				}
				if (SemiFunc.InputDown((InputKey)12))
				{
					crouchedInWindow = true;
				}
				if (SemiFunc.InputDown((InputKey)14))
				{
					isTumblingInWindow = true;
				}
			}
			bool flag = isLevelActive();
			if (flag && !prevActive)
			{
				startingLevel();
			}
			if (!flag && prevActive)
			{
				endingLevel();
			}
			prevActive = flag;
			if (!flag || !SemiFunc.IsMasterClientOrSingleplayer())
			{
				return;
			}
			if (passCooldown >= 0f)
			{
				passCooldown -= Time.deltaTime;
			}
			hiccupTimer -= Time.deltaTime;
			if (hiccupTimer <= 0f && isHiccupPhase)
			{
				if ((Object)(object)holder != (Object)null)
				{
					sendCode(167, holder.photonView.ViewID);
					hiccupTimer = Random.Range(0.75f, 1.25f) * (60f / Plugin.frequency.Value);
				}
				else
				{
					hiccupTimer = 5f;
					Debug.Log((object)"No players available to hiccup.");
				}
			}
			holderTimer -= Time.deltaTime;
			if (holderTimer <= 0f && isHiccupPhase)
			{
				PlayerAvatar val = chooseRandomPlayer();
				if ((Object)(object)val != (Object)null)
				{
					holderTimer = Random.Range(30f, 60f);
					sendCode(169, val.photonView.ViewID);
				}
				else
				{
					Debug.Log((object)"No players available to have the hiccups.");
					holderTimer = 5f;
				}
			}
			phaseTimer -= Time.deltaTime;
			if (!(phaseTimer <= 0f))
			{
				return;
			}
			if (isHiccupPhase)
			{
				sendCode(169, -1);
				phaseTimer = Random.Range(60f, 120f);
				isHiccupPhase = false;
				return;
			}
			PlayerAvatar val2 = chooseRandomPlayer();
			if ((Object)(object)val2 != (Object)null)
			{
				sendCode(169, val2.photonView.ViewID);
				holderTimer = Random.Range(30f, 60f);
				hiccupTimer = Random.Range(0.75f, 1.25f) * (60f / Plugin.frequency.Value);
				phaseTimer = Random.Range(90f, 150f);
				isHiccupPhase = true;
			}
			else
			{
				phaseTimer = 5f;
				Debug.Log((object)"No players available to have the hiccups.");
			}
		}

		private void startingLevel()
		{
			float num = Random.Range(1f, 5f) * (60f / Plugin.frequency.Value);
			hiccupTimer = num;
			phaseTimer = Random.Range(5f, 15f);
			Debug.Log((object)"Starting level, starting hiccups");
			isHiccupPhase = false;
		}

		private void endingLevel()
		{
			Debug.Log((object)"Ending level, stopping hiccups");
			holder = null;
			isHiccupPhase = false;
			((MonoBehaviour)this).StopAllCoroutines();
		}

		private PlayerAvatar chooseRandomPlayer()
		{
			List<PlayerAvatar> list = SemiFunc.PlayerGetList();
			if (list == null || list.Count == 0)
			{
				return null;
			}
			List<PlayerAvatar> list2 = new List<PlayerAvatar>();
			foreach (PlayerAvatar item in list)
			{
				if ((Object)(object)item != (Object)null && !item.isDisabled)
				{
					list2.Add(item);
				}
			}
			if (list2.Count == 0)
			{
				return null;
			}
			int index = Random.Range(0, list2.Count);
			return list2[index];
		}

		private IEnumerator playerHiccup(PlayerAvatar player, float volume)
		{
			if ((Object)(object)player == (Object)null || player.isDisabled)
			{
				yield break;
			}
			windowOpen = true;
			jumpedInWindow = false;
			crouchedInWindow = false;
			alreadyCrouched = player.isCrouching;
			isTumblingInWindow = false;
			if ((Object)(object)inhaleClip != (Object)null)
			{
				AudioSource.PlayClipAtPoint(inhaleClip, ((Component)player).transform.position, volume / 100f);
			}
			yield return (object)new WaitForSeconds(tellTime);
			if ((Object)(object)player == (Object)null || player.isDisabled)
			{
				windowOpen = false;
				yield break;
			}
			windowOpen = false;
			if (player.isLocal)
			{
				int num = ((jumpedInWindow && (crouchedInWindow || alreadyCrouched)) ? 5 : (crouchedInWindow ? 4 : (isTumblingInWindow ? 3 : (jumpedInWindow ? 2 : (alreadyCrouched ? 1 : 0)))));
				sendCode(170, player.photonView.ViewID * 10 + num);
			}
		}

		private IEnumerator loadSounds()
		{
			string text = Path.Combine(Plugin.pluginPath, "assets", "hic.wav");
			if (!File.Exists(text))
			{
				text = Path.Combine(Plugin.pluginPath, "hic.wav");
			}
			string absoluteUri = new Uri(text).AbsoluteUri;
			UnityWebRequest req = UnityWebRequestMultimedia.GetAudioClip(absoluteUri, (AudioType)20);
			try
			{
				yield return req.SendWebRequest();
				if ((int)req.result == 3 || (int)req.result != 1)
				{
					Debug.LogError((object)("Error loading inhale sound: " + req.error));
				}
				else
				{
					inhaleClip = DownloadHandlerAudioClip.GetContent(req);
				}
				text = Path.Combine(Plugin.pluginPath, "assets", "cup.wav");
				if (!File.Exists(text))
				{
					text = Path.Combine(Plugin.pluginPath, "cup.wav");
				}
				absoluteUri = new Uri(text).AbsoluteUri;
				UnityWebRequest req2 = UnityWebRequestMultimedia.GetAudioClip(absoluteUri, (AudioType)20);
				try
				{
					yield return req2.SendWebRequest();
					if ((int)req2.result == 3 || (int)req2.result != 1)
					{
						Debug.LogError((object)("Error loading hic sound: " + req2.error));
					}
					else
					{
						hicClip = DownloadHandlerAudioClip.GetContent(req2);
					}
				}
				finally
				{
					((IDisposable)req2)?.Dispose();
				}
			}
			finally
			{
				((IDisposable)req)?.Dispose();
			}
		}

		private PlayerAvatar findPlayer(int id)
		{
			List<PlayerAvatar> list = SemiFunc.PlayerGetList();
			if (list == null || list.Count == 0)
			{
				return null;
			}
			foreach (PlayerAvatar item in list)
			{
				if ((Object)(object)item != (Object)null && (Object)(object)item.photonView != (Object)null && item.photonView.ViewID == id)
				{
					return item;
				}
			}
			return null;
		}

		private void sendCode(byte code, int id)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			handle(code, id);
			if (PhotonNetwork.InRoom)
			{
				PhotonNetwork.RaiseEvent(code, (object)id, new RaiseEventOptions
				{
					Receivers = (ReceiverGroup)0
				}, SendOptions.SendReliable);
			}
		}

		private void handle(byte code, int id)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			switch (code)
			{
			case 169:
				holder = findPlayer(id);
				if ((Object)(object)holder != (Object)null)
				{
					SemiFunc.UIFocusText(holder.playerName + " has the hiccups", Color.green, Color.white, 3f);
				}
				else
				{
					SemiFunc.UIFocusText("", Color.green, Color.white, 0f);
				}
				break;
			case 167:
			{
				PlayerAvatar val2 = findPlayer(id);
				if ((Object)(object)val2 != (Object)null)
				{
					((MonoBehaviour)this).StartCoroutine(playerHiccup(val2, Plugin.volume.Value));
				}
				break;
			}
			case 170:
			{
				PlayerAvatar val = findPlayer(id / 10);
				if ((Object)(object)val != (Object)null)
				{
					int outcome = id % 10;
					playOutcome(val, outcome, Plugin.volume.Value);
				}
				break;
			}
			}
		}

		private void onEvent(EventData photonEvent)
		{
			if (photonEvent.Code == 169 || photonEvent.Code == 167 || photonEvent.Code == 170)
			{
				int id = (int)photonEvent.CustomData;
				handle(photonEvent.Code, id);
			}
		}

		private void OnDestroy()
		{
			PhotonNetwork.NetworkingClient.EventReceived -= onEvent;
		}

		private IEnumerator WaitForTumbleAndLaunch(PlayerAvatar player)
		{
			float timeout = 2f;
			float elapsedTime = 0f;
			while (!player.tumble.isTumbling && elapsedTime < timeout)
			{
				elapsedTime += Time.deltaTime;
				yield return null;
			}
			if (player.tumble.isTumbling)
			{
				Vector3 val = ((Component)Camera.main).transform.forward + Vector3.up * 0.5f;
				float num = 50f;
				player.tumble.TumbleForce(((Vector3)(ref val)).normalized * num);
			}
		}

		private void playOutcome(PlayerAvatar player, int outcome, float volume)
		{
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_0217: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_02de: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0265: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: 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_0254: Unknown result type (might be due to invalid IL or missing references)
			switch (outcome)
			{
			case 5:
			{
				if ((Object)(object)boom.explosionPreset == (Object)null)
				{
					ExplosionPreset[] array = Resources.FindObjectsOfTypeAll<ExplosionPreset>();
					if (array.Length == 0)
					{
						Debug.LogError((object)"No ExplosionPreset found in resources.");
						break;
					}
					boom.explosionPreset = array[0];
				}
				if ((Object)(object)hicClip != (Object)null)
				{
					AudioSource.PlayClipAtPoint(hicClip, ((Component)player).transform.position, volume / 100f);
				}
				int num = Random.Range(10, 21);
				int num2 = Random.Range(100, 301);
				boom.Spawn(((Component)player).transform.position, 0.5f, num, num2, 20f, false, false, 1f, (float)num2 / 4f);
				break;
			}
			case 4:
				break;
			case 3:
				if ((Object)(object)hicClip != (Object)null)
				{
					AudioSource.PlayClipAtPoint(hicClip, ((Component)player).transform.position, volume / 100f);
				}
				if (player.isLocal)
				{
					player.tumble.TumbleRequest(true, false);
					player.tumble.TumbleOverrideTime(1.5f);
					((MonoBehaviour)this).StartCoroutine(WaitForTumbleAndLaunch(player));
				}
				if ((Object)(object)player.tumble.hurtCollider != (Object)null)
				{
					HurtCollider hurtCollider = player.tumble.hurtCollider;
					hurtCollider.enemyDamage *= 5;
					hurtCollider.enemyHitForce *= 3f;
					((MonoBehaviour)this).StartCoroutine(waitForHurtColliderReset(hurtCollider));
				}
				SemiFunc.EnemyInvestigate(((Component)player).transform.position, 15f, false);
				break;
			case 2:
				if ((Object)(object)hicClip != (Object)null)
				{
					AudioSource.PlayClipAtPoint(hicClip, ((Component)player).transform.position, volume / 100f);
				}
				if (player.isLocal)
				{
					PlayerController.instance.rb.AddForce(Vector3.up * 10f * 1f, (ForceMode)2);
				}
				SemiFunc.EnemyInvestigate(((Component)player).transform.position, 10f, false);
				break;
			case 1:
				if ((Object)(object)hicClip != (Object)null)
				{
					AudioSource.PlayClipAtPoint(hicClip, ((Component)player).transform.position, volume * 0.4f / 100f);
				}
				if (player.isLocal)
				{
					PlayerController.instance.rb.AddForce(Vector3.up * 2.5f * 1f, (ForceMode)2);
				}
				SemiFunc.EnemyInvestigate(((Component)player).transform.position, 3f, false);
				break;
			default:
				if ((Object)(object)hicClip != (Object)null)
				{
					AudioSource.PlayClipAtPoint(hicClip, ((Component)player).transform.position, volume * 0.7f / 100f);
				}
				if (player.isLocal)
				{
					PlayerController.instance.rb.AddForce(Vector3.up * 5f * 1f, (ForceMode)2);
				}
				SemiFunc.EnemyInvestigate(((Component)player).transform.position, 5f, false);
				break;
			}
		}

		private IEnumerator waitForHurtColliderReset(HurtCollider hit)
		{
			yield return (object)new WaitForSeconds(3f);
			hit.enemyDamage /= 5;
			hit.enemyHitForce /= 3f;
		}
	}
	[BepInPlugin("f4ulter.hiccups", "Hiccups Mod", "0.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		public static ConfigEntry<float> frequency;

		public static ConfigEntry<float> volume;

		internal static ManualLogSource Logger;

		public static string pluginPath => Path.GetDirectoryName(typeof(Plugin).Assembly.Location);

		private void Awake()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Expected O, but got Unknown
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin f4ulter.hiccups is loaded!");
			frequency = ((BaseUnityPlugin)this).Config.Bind<float>("hiccups", "frequency", 5f, new ConfigDescription("hics per minute while hiccuping", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 14f), Array.Empty<object>()));
			volume = ((BaseUnityPlugin)this).Config.Bind<float>("sound", "volume", 100f, new ConfigDescription("hiccups volume", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			new Harmony("f4ulter.hiccups").PatchAll();
			GameObject val = new GameObject("Hiccups");
			val.AddComponent<hiccupsRunner>();
			Object.DontDestroyOnLoad((Object)val);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "f4ulter.hiccups";

		public const string PLUGIN_NAME = "Hiccups Mod";

		public const string PLUGIN_VERSION = "0.1.1";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}