Decompiled source of Random Jumpscares v1.0.0

plugins/RandomJumpscare.dll

Decompiled 3 weeks 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.Logging;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RandomJumpscare")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+374ec9dd4d248428c6589ede0d49e835278060d9")]
[assembly: AssemblyProduct("Random Jumpscare")]
[assembly: AssemblyTitle("RandomJumpscare")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.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 RandomJumpscare
{
	[BepInPlugin("Random_Jumpscare", "Random Jumpscare", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <LoadClipCoroutine>d__17 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public string path;

			public Plugin <>4__this;

			private string <uri>5__1;

			private UnityWebRequest <uwr>5__2;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <LoadClipCoroutine>d__17(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || num == 1)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<uri>5__1 = null;
				<uwr>5__2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0093: Unknown result type (might be due to invalid IL or missing references)
				//IL_0099: Invalid comparison between Unknown and I4
				bool result;
				try
				{
					switch (<>1__state)
					{
					default:
						result = false;
						break;
					case 0:
						<>1__state = -1;
						<uri>5__1 = "file:///" + path.Replace("\\", "/");
						<uwr>5__2 = UnityWebRequestMultimedia.GetAudioClip(<uri>5__1, (AudioType)14);
						<>1__state = -3;
						<>2__current = <uwr>5__2.SendWebRequest();
						<>1__state = 1;
						result = true;
						break;
					case 1:
						<>1__state = -3;
						if ((int)<uwr>5__2.result != 1)
						{
							Logger.LogError((object)("Failed to load jumpscare sound: " + <uwr>5__2.error));
							result = false;
							<>m__Finally1();
						}
						else
						{
							<>4__this.scareSound = DownloadHandlerAudioClip.GetContent(<uwr>5__2);
							Logger.LogInfo((object)"Jumpscare sound loaded successfully");
							<>m__Finally1();
							<uwr>5__2 = null;
							result = false;
						}
						break;
					}
				}
				catch
				{
					//try-fault
					((IDisposable)this).Dispose();
					throw;
				}
				return result;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			private void <>m__Finally1()
			{
				<>1__state = -1;
				if (<uwr>5__2 != null)
				{
					((IDisposable)<uwr>5__2).Dispose();
				}
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		[CompilerGenerated]
		private sealed class <LoadImageCoroutine>d__20 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public string filePath;

			public int index;

			public Plugin <>4__this;

			private string <uri>5__1;

			private UnityWebRequest <uwr>5__2;

			private Texture2D <tex>5__3;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <LoadImageCoroutine>d__20(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || num == 1)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<uri>5__1 = null;
				<uwr>5__2 = null;
				<tex>5__3 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0092: Unknown result type (might be due to invalid IL or missing references)
				//IL_0098: Invalid comparison between Unknown and I4
				//IL_013b: Unknown result type (might be due to invalid IL or missing references)
				//IL_014a: Unknown result type (might be due to invalid IL or missing references)
				bool result;
				try
				{
					switch (<>1__state)
					{
					default:
						result = false;
						break;
					case 0:
						<>1__state = -1;
						<uri>5__1 = "file:///" + filePath.Replace("\\", "/");
						<uwr>5__2 = UnityWebRequestTexture.GetTexture(<uri>5__1, true);
						<>1__state = -3;
						<>2__current = <uwr>5__2.SendWebRequest();
						<>1__state = 1;
						result = true;
						break;
					case 1:
						<>1__state = -3;
						if ((int)<uwr>5__2.result != 1)
						{
							Logger.LogError((object)("Failed to load jumpscare image: " + <uwr>5__2.error));
							result = false;
							<>m__Finally1();
							break;
						}
						<tex>5__3 = DownloadHandlerTexture.GetContent(<uwr>5__2);
						((Texture)<tex>5__3).filterMode = (FilterMode)0;
						((Texture)<tex>5__3).wrapMode = (TextureWrapMode)1;
						((Texture)<tex>5__3).anisoLevel = 0;
						<>4__this.jumpscareImages[index].sprite = Sprite.Create(<tex>5__3, new Rect(0f, 0f, (float)((Texture)<tex>5__3).width, (float)((Texture)<tex>5__3).height), new Vector2(0.5f, 0.5f), 100f, 0u, (SpriteMeshType)0);
						<tex>5__3 = null;
						<>m__Finally1();
						<uwr>5__2 = null;
						result = false;
						break;
					}
				}
				catch
				{
					//try-fault
					((IDisposable)this).Dispose();
					throw;
				}
				return result;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			private void <>m__Finally1()
			{
				<>1__state = -1;
				if (<uwr>5__2 != null)
				{
					((IDisposable)<uwr>5__2).Dispose();
				}
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		public const string GUID = "Random_Jumpscare";

		public const string PLUGIN_NAME = "Random Jumpscare";

		public const string PLUGIN_VERSION = "1.0.0";

		private GameObject jumpscareCanvas;

		private AudioClip scareSound;

		private int slideCount = 0;

		private Image[] jumpscareImages;

		internal bool isInLevel = false;

		internal float passedTime = 0f;

		internal int jumpscareChance = 10;

		internal float interval = 1f;

		internal static ManualLogSource Logger;

		internal bool showingJumpscare = false;

		internal float timeBetweenFrames = 0.06f;

		internal float passedAnimationTime = 0f;

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			jumpscareChance = ((BaseUnityPlugin)this).Config.Bind<int>("Jumpscare", "jumpscareChance", 1, "The chance of a jumpscare happening at the end of an interval.").Value;
			interval = ((BaseUnityPlugin)this).Config.Bind<float>("Jumpscare", "interval", 60f, "After how many seconds the jumpscare chance should be checked.").Value;
			timeBetweenFrames = ((BaseUnityPlugin)this).Config.Bind<float>("Jumpscare", "timeBetweenFrames", 0.06f, "How long each frame of the animation should last.").Value;
			string text = Path.Combine(Paths.PluginPath, "Jumpscare_Slides");
			if (!Directory.Exists(text))
			{
				Logger.LogError((object)("Jumpscare image folder not found: " + text));
				return;
			}
			string path = Path.Combine(Paths.PluginPath, "jumpscare.ogg");
			if (!File.Exists(path))
			{
				Logger.LogError((object)"Could not find jumpscare sound file.");
			}
			else
			{
				((MonoBehaviour)this).StartCoroutine(LoadClipCoroutine(path));
			}
			slideCount = Directory.GetFiles(text, "*", SearchOption.TopDirectoryOnly).Length;
			Logger.LogInfo((object)("Detected " + slideCount + " files."));
			jumpscareImages = (Image[])(object)new Image[slideCount];
			CreateJumpscareOverlay();
			LoadJumpscareImages();
			Logger.LogInfo((object)"Plugin Random_Jumpscare is loaded!");
		}

		[PunRPC]
		private void RPC_StartJumpscare()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			if (!showingJumpscare)
			{
				showingJumpscare = true;
				if ((Object)(object)scareSound != (Object)null)
				{
					AudioSource.PlayClipAtPoint(scareSound, Character.localCharacter.Center);
				}
			}
		}

		[IteratorStateMachine(typeof(<LoadClipCoroutine>d__17))]
		private IEnumerator LoadClipCoroutine(string path)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <LoadClipCoroutine>d__17(0)
			{
				<>4__this = this,
				path = path
			};
		}

		private void Update()
		{
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			if (showingJumpscare)
			{
				passedAnimationTime += Time.deltaTime;
			}
			passedTime += Time.deltaTime;
			while (passedTime >= interval)
			{
				if (isInLevel && !showingJumpscare)
				{
					showingJumpscare = Random.Range(0, 100) < jumpscareChance;
					if (showingJumpscare)
					{
						PhotonView photonView = ((MonoBehaviourPun)Character.localCharacter).photonView;
						photonView.RPC("RPC_StartJumpscare", (RpcTarget)0, Array.Empty<object>());
					}
					if (showingJumpscare && (Object)(object)scareSound != (Object)null)
					{
						AudioSource.PlayClipAtPoint(scareSound, Character.localCharacter.Center);
					}
				}
				passedTime -= interval;
			}
			if (!showingJumpscare)
			{
				return;
			}
			if (!isInLevel)
			{
				for (int i = 0; i < slideCount; i++)
				{
					((Behaviour)jumpscareImages[i]).enabled = false;
				}
				showingJumpscare = false;
				passedAnimationTime = 0f;
				return;
			}
			int num = (int)(passedAnimationTime / timeBetweenFrames);
			if (num >= slideCount)
			{
				Logger.LogInfo((object)"Finished animation");
				for (int j = 0; j < slideCount; j++)
				{
					((Behaviour)jumpscareImages[j]).enabled = false;
				}
				showingJumpscare = false;
				passedAnimationTime = 0f;
			}
			else
			{
				Logger.LogInfo((object)("Displaying slide " + num));
				for (int k = 0; k < slideCount; k++)
				{
					((Behaviour)jumpscareImages[k]).enabled = k == num;
				}
			}
		}

		private void LoadJumpscareImages()
		{
			string text = Path.Combine(Paths.PluginPath, "Jumpscare_Slides");
			int num = slideCount;
			for (int i = 0; i < num; i++)
			{
				string path = "slide" + (i + 1) + ".png";
				string filePath = Path.Combine(Paths.PluginPath, "Jumpscare_Slides", path);
				((MonoBehaviour)this).StartCoroutine(LoadImageCoroutine(filePath, i));
			}
		}

		[IteratorStateMachine(typeof(<LoadImageCoroutine>d__20))]
		private IEnumerator LoadImageCoroutine(string filePath, int index)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <LoadImageCoroutine>d__20(0)
			{
				<>4__this = this,
				filePath = filePath,
				index = index
			};
		}

		private void CreateJumpscareOverlay()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			jumpscareCanvas = new GameObject("JumpscareCanvas");
			Object.DontDestroyOnLoad((Object)(object)jumpscareCanvas);
			Canvas val = jumpscareCanvas.AddComponent<Canvas>();
			val.renderMode = (RenderMode)0;
			val.sortingOrder = 32767;
			CanvasScaler val2 = jumpscareCanvas.AddComponent<CanvasScaler>();
			val2.uiScaleMode = (ScaleMode)0;
			val2.scaleFactor = 1f;
			jumpscareCanvas.AddComponent<GraphicRaycaster>();
			for (int i = 0; i < slideCount; i++)
			{
				GameObject val3 = new GameObject("JumpscareImage" + i);
				val3.transform.SetParent(jumpscareCanvas.transform, false);
				jumpscareImages[i] = val3.AddComponent<Image>();
				RectTransform rectTransform = ((Graphic)jumpscareImages[i]).rectTransform;
				rectTransform.anchorMin = Vector2.zero;
				rectTransform.anchorMax = Vector2.one;
				rectTransform.offsetMin = Vector2.zero;
				rectTransform.offsetMax = Vector2.zero;
				((Behaviour)jumpscareImages[i]).enabled = false;
			}
		}

		private void OnEnable()
		{
			Logger.LogInfo((object)"Added scene load call.");
			SceneManager.sceneLoaded += OnSceneLoaded;
		}

		private void OnDisable()
		{
			Logger.LogInfo((object)"Removed scene load call.");
			SceneManager.sceneLoaded -= OnSceneLoaded;
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
		{
			Logger.LogInfo((object)"New scene.");
			if (((Scene)(ref scene)).name.StartsWith("Airport") || ((Scene)(ref scene)).name.StartsWith("Level") || ((Scene)(ref scene)).name.StartsWith("WilIsland"))
			{
				isInLevel = true;
			}
			else
			{
				isInLevel = false;
			}
		}
	}
}