Decompiled source of Configurable Random Jumpscare v1.1.1

jumpscareVeryScary/ConfigurableRandomJumpscare.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using UnityEngine;
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(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ConfigurableRandomJumpscare")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.2.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ConfigurableRandomJumpscare")]
[assembly: AssemblyTitle("Configurable Random Jumpscare Mod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.0.0")]
[module: UnverifiableCode]
namespace ConfigurableRandomJumpscare;

[BepInPlugin("com.Error72890.configurableRandomJumpscare", "Configurable Random Jumpscare", "1.2.0")]
[BepInProcess("PATAPON12_REPLAY.exe")]
public class Plugin : BasePlugin
{
	internal static ManualLogSource Log;

	internal static string ModDirectory;

	public static ConfigEntry<int> jumpscareChance;

	public static ConfigEntry<float> audioVolume;

	public static ConfigEntry<bool> enableMod;

	public static ConfigEntry<string> imageFileNames;

	public static ConfigEntry<string> audioFileNames;

	public static ConfigEntry<string> imageDurations;

	public static ConfigEntry<string> jumpscareWeights;

	public static string[] imageFiles;

	public static string[] audioFiles;

	public static float[] durations;

	public static int[] weights;

	public override void Load()
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		Log = ((BasePlugin)this).Log;
		ModDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		SetupConfig();
		ParseConfigArrays();
		Harmony val = new Harmony("com.Error72890.configurableRandomJumpscare");
		val.PatchAll();
	}

	private void SetupConfig()
	{
		enableMod = ((BasePlugin)this).Config.Bind<bool>("General", "EnableMod", true, "Enable/disable the mod");
		jumpscareChance = ((BasePlugin)this).Config.Bind<int>("General", "JumpscareChance", 200000, "Probability of jumpscare per frame (1/X). Lower = more frequent");
		audioVolume = ((BasePlugin)this).Config.Bind<float>("General", "AudioVolume", 1f, "Audio volume (0.0 - 1.0)");
		imageFileNames = ((BasePlugin)this).Config.Bind<string>("Files", "ImageFileNames", "blueLobster.png,redLobster.png", "Comma-separated list of image filenames (must be in the same folder as the mod)");
		audioFileNames = ((BasePlugin)this).Config.Bind<string>("Files", "AudioFileNames", "blueLobster.wav,redLobster.wav", "Comma-separated list of audio filenames (must be in the same folder as the mod)");
		imageDurations = ((BasePlugin)this).Config.Bind<string>("Files", "ImageDurations", "5.0,10.0", "Comma-separated list of durations for each image (seconds)");
		jumpscareWeights = ((BasePlugin)this).Config.Bind<string>("Files", "JumpscareWeights", "9,1", "Comma-separated list of weights for each jumpscare (higher = more frequent)");
	}

	private void ParseConfigArrays()
	{
		//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01be: Expected O, but got Unknown
		//IL_0173: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Expected O, but got Unknown
		bool flag = default(bool);
		try
		{
			imageFiles = (from s in imageFileNames.Value.Split(new char[1] { ',' })
				select s.Trim()).ToArray();
			audioFiles = (from s in audioFileNames.Value.Split(new char[1] { ',' })
				select s.Trim()).ToArray();
			durations = (from s in imageDurations.Value.Split(new char[1] { ',' })
				select float.Parse(s.Trim())).ToArray();
			weights = (from s in jumpscareWeights.Value.Split(new char[1] { ',' })
				select int.Parse(s.Trim())).ToArray();
			int num = imageFiles.Length;
			if (audioFiles.Length != num || durations.Length != num || weights.Length != num)
			{
				Log.LogError((object)"All jumpscare arrays must have the same length!");
				return;
			}
			ManualLogSource log = Log;
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(19, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Loaded ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(num);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" jumpscares:");
			}
			log.LogInfo(val);
		}
		catch (Exception ex)
		{
			ManualLogSource log2 = Log;
			BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(29, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Error parsing config arrays: ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
			}
			log2.LogError(val2);
		}
	}

	public static string GetImagePath(int index)
	{
		if (index < 0 || index >= imageFiles.Length)
		{
			return null;
		}
		return Path.Combine(ModDirectory, imageFiles[index]);
	}

	public static string GetAudioPath(int index)
	{
		if (index < 0 || index >= audioFiles.Length)
		{
			return null;
		}
		return Path.Combine(ModDirectory, audioFiles[index]);
	}
}
[HarmonyPatch(typeof(MultiPlatformInputManager), "Update")]
public static class JumpscareTrigger
{
	private static bool isShowingJumpscare = false;

	private static float jumpscareStartTime = 0f;

	private static float currentJumpscareDuration = 0f;

	private static List<Texture2D> jumpscareTextures = new List<Texture2D>();

	private static Canvas canvasObject;

	private static Image imageComponent;

	private static bool initialized = false;

	private static bool resourcesLoadAttempted = false;

	private static GameObject jumpscareGameObject;

	private static float resourceLoadTimer = 20f;

	public static void Postfix()
	{
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Expected O, but got Unknown
		if (!Plugin.enableMod.Value)
		{
			return;
		}
		try
		{
			if (!initialized)
			{
				InitializeUI();
				initialized = true;
			}
			if (resourceLoadTimer > 0f)
			{
				resourceLoadTimer -= Time.unscaledDeltaTime;
				return;
			}
			if (!resourcesLoadAttempted)
			{
				LoadResources();
				resourcesLoadAttempted = true;
			}
			HandleJumpscareDisplay();
			if (!isShowingJumpscare && jumpscareTextures.Any((Texture2D t) => (Object)(object)t != (Object)null))
			{
				int num = Random.Range(1, Plugin.jumpscareChance.Value + 1);
				if (num == 1)
				{
					TriggerJumpscare();
				}
			}
		}
		catch (Exception ex)
		{
			ManualLogSource log = Plugin.Log;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(35, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Error in JumpscareTrigger.Postfix: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
			}
			log.LogError(val);
		}
	}

	private static void InitializeUI()
	{
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0135: Expected O, but got Unknown
		//IL_0007: 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_0027: Expected O, but got Unknown
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Expected O, but got Unknown
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			jumpscareGameObject = new GameObject("JumpscareMod");
			Object.DontDestroyOnLoad((Object)(object)jumpscareGameObject);
			GameObject val = new GameObject("JumpscareCanvas");
			val.transform.SetParent(jumpscareGameObject.transform);
			canvasObject = val.AddComponent<Canvas>();
			canvasObject.renderMode = (RenderMode)0;
			canvasObject.sortingOrder = 32767;
			CanvasScaler val2 = val.AddComponent<CanvasScaler>();
			val2.uiScaleMode = (ScaleMode)1;
			val2.referenceResolution = new Vector2(1920f, 1080f);
			val.AddComponent<GraphicRaycaster>();
			GameObject val3 = new GameObject("JumpscareImage");
			val3.transform.SetParent(val.transform, false);
			imageComponent = val3.AddComponent<Image>();
			((Behaviour)imageComponent).enabled = false;
			((Graphic)imageComponent).color = Color.white;
			RectTransform component = ((Component)imageComponent).GetComponent<RectTransform>();
			component.anchorMin = Vector2.zero;
			component.anchorMax = Vector2.one;
			component.sizeDelta = Vector2.zero;
			component.anchoredPosition = Vector2.zero;
			((Behaviour)canvasObject).enabled = false;
		}
		catch (Exception ex)
		{
			ManualLogSource log = Plugin.Log;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val4 = new BepInExErrorLogInterpolatedStringHandler(25, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("Failed to initialize UI: ");
				((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(ex.Message);
			}
			log.LogError(val4);
		}
	}

	private static void LoadResources()
	{
		//IL_0211: Unknown result type (might be due to invalid IL or missing references)
		//IL_0218: Expected O, but got Unknown
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Expected O, but got Unknown
		//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b2: Expected O, but got Unknown
		//IL_0114: Unknown result type (might be due to invalid IL or missing references)
		//IL_011b: Expected O, but got Unknown
		bool flag = default(bool);
		try
		{
			Plugin.Log.LogInfo((object)"Loading resources...");
			if (Plugin.imageFiles == null || Plugin.imageFiles.Length == 0)
			{
				Plugin.Log.LogError((object)"No image files configured!");
				return;
			}
			jumpscareTextures.Clear();
			for (int i = 0; i < Plugin.imageFiles.Length; i++)
			{
				jumpscareTextures.Add(null);
			}
			for (int j = 0; j < Plugin.imageFiles.Length; j++)
			{
				string imagePath = Plugin.GetImagePath(j);
				if (File.Exists(imagePath))
				{
					LoadImageDirect(imagePath, j);
				}
				else
				{
					ManualLogSource log = Plugin.Log;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(23, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Image file ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(j + 1);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" not found: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(imagePath);
					}
					log.LogWarning(val);
				}
				string audioPath = Plugin.GetAudioPath(j);
				if (!File.Exists(audioPath))
				{
					ManualLogSource log2 = Plugin.Log;
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(23, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Audio file ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(j + 1);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" not found: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(audioPath);
					}
					log2.LogWarning(val);
				}
			}
			int num = jumpscareTextures.Count((Texture2D t) => (Object)(object)t != (Object)null);
			ManualLogSource log3 = Plugin.Log;
			BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(45, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Resources loading completed. ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(num);
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("/");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(Plugin.imageFiles.Length);
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" images loaded.");
			}
			log3.LogInfo(val2);
		}
		catch (Exception ex)
		{
			ManualLogSource log4 = Plugin.Log;
			BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(24, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("Error in LoadResources: ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex.Message);
			}
			log4.LogError(val3);
		}
	}

	private static void LoadImageDirect(string imagePath, int index)
	{
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Expected O, but got Unknown
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Expected O, but got Unknown
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Expected O, but got Unknown
		bool flag = default(bool);
		try
		{
			byte[] array = File.ReadAllBytes(imagePath);
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
			if (ImageConversion.LoadImage(val, Il2CppStructArray<byte>.op_Implicit(array)))
			{
				jumpscareTextures[index] = val;
				return;
			}
			ManualLogSource log = Plugin.Log;
			BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(37, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Texture2D.LoadImage failed for image ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(index + 1);
			}
			log.LogWarning(val2);
			Object.Destroy((Object)(object)val);
		}
		catch (Exception ex)
		{
			ManualLogSource log2 = Plugin.Log;
			BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(22, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("Error loading image ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<int>(index + 1);
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex.Message);
			}
			log2.LogError(val3);
		}
	}

	private static int SelectWeightedJumpscare()
	{
		if (Plugin.weights == null || Plugin.weights.Length == 0)
		{
			return -1;
		}
		int num = 0;
		for (int i = 0; i < Plugin.weights.Length; i++)
		{
			if ((Object)(object)jumpscareTextures[i] != (Object)null)
			{
				num += Plugin.weights[i];
			}
		}
		if (num == 0)
		{
			return -1;
		}
		int num2 = Random.Range(0, num);
		int num3 = 0;
		for (int j = 0; j < Plugin.weights.Length; j++)
		{
			if ((Object)(object)jumpscareTextures[j] != (Object)null)
			{
				num3 += Plugin.weights[j];
				if (num2 < num3)
				{
					return j;
				}
			}
		}
		return -1;
	}

	private static void HandleJumpscareDisplay()
	{
		if (isShowingJumpscare)
		{
			float num = Time.realtimeSinceStartup - jumpscareStartTime;
			if (num >= currentJumpscareDuration)
			{
				HideJumpscare();
			}
		}
	}

	private static void TriggerJumpscare()
	{
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Expected O, but got Unknown
		//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01be: Expected O, but got Unknown
		//IL_00d3: 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_010f: Unknown result type (might be due to invalid IL or missing references)
		bool flag = default(bool);
		try
		{
			if (isShowingJumpscare || !initialized)
			{
				return;
			}
			int num = SelectWeightedJumpscare();
			if (num == -1)
			{
				return;
			}
			Texture2D val = jumpscareTextures[num];
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			isShowingJumpscare = true;
			jumpscareStartTime = Time.realtimeSinceStartup;
			currentJumpscareDuration = Plugin.durations[num];
			string audioPath = Plugin.GetAudioPath(num);
			if (File.Exists(audioPath))
			{
				PlayAudioNative(audioPath);
			}
			if ((Object)(object)imageComponent != (Object)null && (Object)(object)canvasObject != (Object)null)
			{
				((Behaviour)canvasObject).enabled = true;
				try
				{
					Sprite val2 = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
					if (!((Object)(object)val2 != (Object)null))
					{
						Plugin.Log.LogError((object)"Failed to create sprite");
						HideJumpscare();
						return;
					}
					imageComponent.sprite = val2;
					((Graphic)imageComponent).color = Color.white;
				}
				catch (Exception ex)
				{
					ManualLogSource log = Plugin.Log;
					BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(23, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("Error creating sprite: ");
						((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex.Message);
					}
					log.LogError(val3);
					HideJumpscare();
					return;
				}
				((Behaviour)imageComponent).enabled = true;
			}
			else
			{
				Plugin.Log.LogError((object)"imageComponent or canvasObject is null!");
			}
		}
		catch (Exception ex2)
		{
			ManualLogSource log2 = Plugin.Log;
			BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(27, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("Error in TriggerJumpscare: ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex2.Message);
			}
			log2.LogError(val3);
		}
	}

	private static void HideJumpscare()
	{
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Expected O, but got Unknown
		try
		{
			isShowingJumpscare = false;
			if ((Object)(object)imageComponent != (Object)null)
			{
				((Behaviour)imageComponent).enabled = false;
			}
			if ((Object)(object)canvasObject != (Object)null)
			{
				((Behaviour)canvasObject).enabled = false;
			}
		}
		catch (Exception ex)
		{
			ManualLogSource log = Plugin.Log;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(24, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Error in HideJumpscare: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
			}
			log.LogError(val);
		}
	}

	[DllImport("winmm.dll", SetLastError = true)]
	private static extern bool PlaySound(string pszSound, IntPtr hmod, uint fdwSound);

	private static void PlayAudioNative(string audioPath)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		try
		{
			PlaySound(audioPath, IntPtr.Zero, 131073u);
		}
		catch (Exception ex)
		{
			ManualLogSource log = Plugin.Log;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(29, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Native audio playback error: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
			}
			log.LogError(val);
		}
	}
}