Decompiled source of SORPRESA v1.2.0

SORPRESA.dll

Decompiled 4 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using HarmonyLib;
using JumpScaresMod;
using MelonLoader;
using Mimic;
using Mimic.Animation;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(JumpScares), "SORPRESA", "1.2.0", "xaxiflopi", null)]
[assembly: MelonGame("ReLUGames", "MIMESIS")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace JumpScaresMod;

public static class JumpScareManager
{
	private static float _lastJumpscareTime = -999f;

	private const float Cooldown = 120f;

	private static bool _initialized;

	private static bool _running;

	private static GameObject _overlayRoot;

	private static RawImage _faceImage;

	private static AudioSource _audioSource;

	private static CanvasGroup _canvasGroup;

	private static readonly List<Texture2D> _faces = new List<Texture2D>();

	private static readonly List<AudioClip> _screams = new List<AudioClip>();

	private static string _modDir;

	private static readonly Random Rng = new Random();

	public static void OnSceneLoad(string sceneName)
	{
		_initialized = false;
		_overlayRoot = null;
		_faceImage = null;
		_audioSource = null;
		_canvasGroup = null;
		_running = false;
	}

	public static void OnUpdate()
	{
		if (!_initialized)
		{
			TryInitialize();
		}
	}

	public static bool ShouldTrigger(float chance)
	{
		if (!_initialized || _running)
		{
			return false;
		}
		if ((Object)(object)_overlayRoot == (Object)null || _overlayRoot.activeSelf)
		{
			return false;
		}
		if (Time.time - _lastJumpscareTime < 120f)
		{
			return false;
		}
		if (_faces.Count == 0 || _screams.Count == 0)
		{
			return false;
		}
		return Rng.NextDouble() < (double)chance;
	}

	public static void TriggerJumpscare()
	{
		_lastJumpscareTime = Time.time;
		MelonCoroutines.Start(RunJumpscare());
	}

	private static IEnumerator RunJumpscare()
	{
		_running = true;
		yield return (object)new WaitForSecondsRealtime((float)(Rng.NextDouble() * 2.0));
		if ((Object)(object)_overlayRoot == (Object)null)
		{
			_running = false;
			yield break;
		}
		_faceImage.texture = (Texture)(object)_faces[Rng.Next(_faces.Count)];
		_audioSource.clip = _screams[Rng.Next(_screams.Count)];
		_overlayRoot.SetActive(true);
		if ((Object)(object)_canvasGroup != (Object)null)
		{
			_canvasGroup.alpha = 1f;
		}
		_audioSource.volume = 1f;
		_audioSource.Play();
		yield return (object)new WaitForSecondsRealtime(1.5f + (float)(Rng.NextDouble() * 1.0));
		float fadeTime = 0.3f;
		float elapsed = 0f;
		while (elapsed < fadeTime)
		{
			elapsed += Time.unscaledDeltaTime;
			if ((Object)(object)_canvasGroup != (Object)null)
			{
				_canvasGroup.alpha = 1f - elapsed / fadeTime;
			}
			yield return null;
		}
		_overlayRoot.SetActive(false);
		_running = false;
	}

	private static void TryInitialize()
	{
		if (!((Object)(object)Camera.main == (Object)null))
		{
			_modDir = FindModDirectory();
			CreateOverlay();
			MelonCoroutines.Start(LoadAssets());
			_initialized = true;
			((MelonBase)JumpScares.Instance).LoggerInstance.Msg("[SORPRESA] Inicializado. Directorio: " + _modDir);
		}
	}

	private static string FindModDirectory()
	{
		return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? AppDomain.CurrentDomain.BaseDirectory;
	}

	private static IEnumerator LoadAssets()
	{
		yield return MelonCoroutines.Start(LoadImages());
		yield return MelonCoroutines.Start(LoadSounds());
		((MelonBase)JumpScares.Instance).LoggerInstance.Msg($"[SORPRESA] Assets cargados: {_faces.Count} imágenes, {_screams.Count} sonidos.");
	}

	private static IEnumerator LoadImages()
	{
		string[] obj = new string[4] { "*.jfif", "*.jpg", "*.jpeg", "*.png" };
		List<string> list = new List<string>();
		string[] array = obj;
		foreach (string searchPattern in array)
		{
			list.AddRange(Directory.GetFiles(_modDir, searchPattern));
		}
		foreach (string path in list)
		{
			UnityWebRequest req = UnityWebRequestTexture.GetTexture("file:///" + path.Replace('\\', '/'));
			try
			{
				yield return req.SendWebRequest();
				if ((int)req.result == 1)
				{
					Texture2D content = DownloadHandlerTexture.GetContent(req);
					((Object)content).name = Path.GetFileNameWithoutExtension(path);
					_faces.Add(content);
					((MelonBase)JumpScares.Instance).LoggerInstance.Msg("[SORPRESA] Imagen cargada: " + ((Object)content).name);
				}
				else
				{
					((MelonBase)JumpScares.Instance).LoggerInstance.Warning("[SORPRESA] Error imagen " + path + ": " + req.error);
				}
			}
			finally
			{
				((IDisposable)req)?.Dispose();
			}
		}
	}

	private static IEnumerator LoadSounds()
	{
		string[] files = Directory.GetFiles(_modDir, "*.mp3");
		string[] array = files;
		foreach (string path in array)
		{
			UnityWebRequest req = UnityWebRequestMultimedia.GetAudioClip("file:///" + path.Replace('\\', '/'), (AudioType)13);
			try
			{
				((DownloadHandlerAudioClip)req.downloadHandler).streamAudio = false;
				yield return req.SendWebRequest();
				if ((int)req.result == 1)
				{
					AudioClip content = DownloadHandlerAudioClip.GetContent(req);
					((Object)content).name = Path.GetFileNameWithoutExtension(path);
					_screams.Add(content);
					((MelonBase)JumpScares.Instance).LoggerInstance.Msg("[SORPRESA] Sonido cargado: " + ((Object)content).name);
				}
				else
				{
					((MelonBase)JumpScares.Instance).LoggerInstance.Warning("[SORPRESA] Error sonido " + path + ": " + req.error);
				}
			}
			finally
			{
				((IDisposable)req)?.Dispose();
			}
		}
	}

	private static void CreateOverlay()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Expected O, but got Unknown
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		_overlayRoot = new GameObject("SORPRESA_Overlay");
		Object.DontDestroyOnLoad((Object)(object)_overlayRoot);
		Canvas obj = _overlayRoot.AddComponent<Canvas>();
		obj.renderMode = (RenderMode)0;
		obj.sortingOrder = 9999;
		_canvasGroup = _overlayRoot.AddComponent<CanvasGroup>();
		_canvasGroup.blocksRaycasts = false;
		_canvasGroup.interactable = false;
		CanvasScaler obj2 = _overlayRoot.AddComponent<CanvasScaler>();
		obj2.uiScaleMode = (ScaleMode)1;
		obj2.referenceResolution = new Vector2(1920f, 1080f);
		GameObject val = new GameObject("BG");
		val.transform.SetParent(_overlayRoot.transform, false);
		((Graphic)val.AddComponent<RawImage>()).color = Color.black;
		StretchRect(val.GetComponent<RectTransform>());
		GameObject val2 = new GameObject("Face");
		val2.transform.SetParent(_overlayRoot.transform, false);
		_faceImage = val2.AddComponent<RawImage>();
		((Graphic)_faceImage).color = Color.white;
		_faceImage.uvRect = new Rect(0f, 0f, 1f, 1f);
		StretchRect(val2.GetComponent<RectTransform>());
		GameObject val3 = new GameObject("Audio");
		val3.transform.SetParent(_overlayRoot.transform, false);
		_audioSource = val3.AddComponent<AudioSource>();
		_audioSource.spatialBlend = 0f;
		_audioSource.volume = 1f;
		_audioSource.priority = 0;
		_overlayRoot.SetActive(false);
	}

	private static void StretchRect(RectTransform rt)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: 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)
		rt.anchorMin = Vector2.zero;
		rt.anchorMax = Vector2.one;
		rt.offsetMin = Vector2.zero;
		rt.offsetMax = Vector2.zero;
	}
}
[HarmonyPatch(typeof(LootingEventFeedbackTrigger), "Trigger")]
public static class LootPickupPatch
{
	[HarmonyPostfix]
	public static void Postfix()
	{
		if (JumpScareManager.ShouldTrigger(0.4f))
		{
			((MelonBase)JumpScares.Instance).LoggerInstance.Msg("[SORPRESA] Objeto recogido - ¡SUSTO!");
			JumpScareManager.TriggerJumpscare();
		}
	}
}
[HarmonyPatch(typeof(MonsterHummingSoundPlayer), "OnStateEnter", new Type[]
{
	typeof(Animator),
	typeof(AnimatorStateInfo),
	typeof(int)
})]
public static class MonsterHummingPatch
{
	[HarmonyPostfix]
	public static void Postfix(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
	{
		if (JumpScareManager.ShouldTrigger(0.35f))
		{
			((MelonBase)JumpScares.Instance).LoggerInstance.Msg("[SORPRESA] Monstruo activo cerca - ¡SUSTO!");
			JumpScareManager.TriggerJumpscare();
		}
	}
}
public class JumpScares : MelonMod
{
	internal static JumpScares Instance { get; private set; }

	public override void OnInitializeMelon()
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		Instance = this;
		new Harmony("com.xaxiflopi.sorpresa").PatchAll();
		((MelonBase)this).LoggerInstance.Msg("SORPRESA v1.2.0 activado. ¡Que empiece la diversión!");
	}

	public override void OnSceneWasLoaded(int buildIndex, string sceneName)
	{
		JumpScareManager.OnSceneLoad(sceneName);
	}

	public override void OnUpdate()
	{
		JumpScareManager.OnUpdate();
	}
}