Decompiled source of Silent PEAK v1.0.0

tony4twentys-Silent PEAK.dll

Decompiled 3 months ago
using System;
using System.Collections;
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 HarmonyLib;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Silent PEAK")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Silent PEAK")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f2481b33-e0f3-4ef9-90de-62fe65db12f6")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace tony4twentys.Silent_PEAK;

[BepInPlugin("tony4twentys.Silent_PEAK", "Silent PEAK", "1.0.0")]
public class SilentPEAKPlugin : BaseUnityPlugin
{
	[CompilerGenerated]
	private sealed class <LoadSilentHillMusic>d__11 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public SilentPEAKPlugin <>4__this;

		private string <pluginLocation>5__1;

		private string <pluginDirectory>5__2;

		private string[] <audioFiles>5__3;

		private string <musicFile>5__4;

		private string <url>5__5;

		private UnityWebRequest <www>5__6;

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

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

		[DebuggerHidden]
		public <LoadSilentHillMusic>d__11(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();
				}
			}
			<pluginLocation>5__1 = null;
			<pluginDirectory>5__2 = null;
			<audioFiles>5__3 = null;
			<musicFile>5__4 = null;
			<url>5__5 = null;
			<www>5__6 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Invalid comparison between Unknown and I4
			bool result;
			try
			{
				switch (<>1__state)
				{
				default:
					result = false;
					break;
				case 0:
					<>1__state = -1;
					<pluginLocation>5__1 = ((BaseUnityPlugin)<>4__this).Info.Location;
					<pluginDirectory>5__2 = Path.GetDirectoryName(<pluginLocation>5__1);
					((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)("=== SILENT PEAK: Looking for Silent Hill music in: " + <pluginDirectory>5__2 + " ==="));
					<audioFiles>5__3 = Directory.GetFiles(<pluginDirectory>5__2, "*.ogg", SearchOption.AllDirectories).ToArray();
					if (<audioFiles>5__3.Length == 0)
					{
						((BaseUnityPlugin)<>4__this).Logger.LogWarning((object)("=== SILENT PEAK: No .ogg files found in " + <pluginDirectory>5__2 + " ==="));
						((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)"=== SILENT PEAK: Music disabled - no audio file found ===");
						result = false;
						break;
					}
					<musicFile>5__4 = <audioFiles>5__3[0];
					((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)("=== SILENT PEAK: Loading Silent Hill music from " + <musicFile>5__4 + " ==="));
					<url>5__5 = "file://" + Path.GetFullPath(<musicFile>5__4).Replace("\\", "/");
					<www>5__6 = UnityWebRequestMultimedia.GetAudioClip(<url>5__5, (AudioType)14);
					<>1__state = -3;
					<>2__current = <www>5__6.SendWebRequest();
					<>1__state = 1;
					result = true;
					break;
				case 1:
					<>1__state = -3;
					if ((int)<www>5__6.result != 1)
					{
						((BaseUnityPlugin)<>4__this).Logger.LogError((object)("=== SILENT PEAK: Failed to load music: " + <www>5__6.error + " ==="));
						result = false;
					}
					else
					{
						silentHillMix = DownloadHandlerAudioClip.GetContent(<www>5__6);
						if (!((Object)(object)silentHillMix == (Object)null))
						{
							((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)$"=== SILENT PEAK: AudioClip created - Name: {((Object)silentHillMix).name}, Length: {silentHillMix.length}s, Channels: {silentHillMix.channels}, Frequency: {silentHillMix.frequency}Hz ===");
							<>4__this.SetupMusicSource();
							musicLoaded = true;
							((BaseUnityPlugin)<>4__this).Logger.LogInfo((object)"=== SILENT PEAK: Silent Hill music loaded successfully ===");
							<>m__Finally1();
							<www>5__6 = null;
							result = false;
							break;
						}
						((BaseUnityPlugin)<>4__this).Logger.LogError((object)"=== SILENT PEAK: Failed to create audio clip ===");
						result = false;
					}
					<>m__Finally1();
					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 (<www>5__6 != null)
			{
				((IDisposable)<www>5__6).Dispose();
			}
		}

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

	public static ConfigEntry<float> musicVolume;

	public static bool AlwaysActiveValue = true;

	public static float lastApplicationTime = 0f;

	public static bool hasAppliedEffect = false;

	public static AudioSource musicSource;

	private static AudioClip silentHillMix;

	private static bool musicLoaded = false;

	private static Coroutine musicLoadCoroutine;

	private void Awake()
	{
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Expected O, but got Unknown
		musicVolume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MusicVolume", 50f, "Volume of Silent Hill music (0 to 100)");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Silent PEAK mod loaded successfully!");
		Harmony val = new Harmony("tony4twentys.Silent_PEAK");
		val.PatchAll();
		SceneManager.sceneLoaded += OnSceneLoaded;
		musicLoadCoroutine = ((MonoBehaviour)this).StartCoroutine(LoadSilentHillMusic());
	}

	private void OnDestroy()
	{
		SceneManager.sceneLoaded -= OnSceneLoaded;
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		((BaseUnityPlugin)this).Logger.LogInfo((object)("=== SILENT PEAK: Scene loaded - " + ((Scene)(ref scene)).name + " ==="));
		if (((Scene)(ref scene)).name.Contains("Level_"))
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"=== SILENT PEAK: Level scene detected - starting music ===");
			StartMusic();
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"=== SILENT PEAK: Non-level scene detected - stopping music ===");
			StopMusic();
		}
	}

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

	private void SetupMusicSource()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		GameObject val = new GameObject("SilentPEAKMusicHost");
		musicSource = val.AddComponent<AudioSource>();
		musicSource.clip = silentHillMix;
		musicSource.loop = true;
		musicSource.playOnAwake = false;
		musicSource.volume = Mathf.Clamp(musicVolume.Value, 0f, 100f) / 100f;
		musicSource.spatialBlend = 0f;
		musicSource.dopplerLevel = 0f;
		musicSource.rolloffMode = (AudioRolloffMode)1;
		musicSource.minDistance = 1f;
		musicSource.maxDistance = 500f;
		musicSource.ignoreListenerPause = true;
		musicSource.bypassEffects = false;
		musicSource.bypassListenerEffects = false;
		musicSource.bypassReverbZones = true;
		AudioMixerGroup val2 = TryGetGameMixerGroup();
		if ((Object)(object)val2 != (Object)null)
		{
			musicSource.outputAudioMixerGroup = val2;
		}
		Object.DontDestroyOnLoad((Object)(object)val);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"=== SILENT PEAK: Music source setup complete ===");
	}

	private static AudioMixerGroup TryGetGameMixerGroup()
	{
		AudioSource val = ((IEnumerable<AudioSource>)Object.FindObjectsByType<AudioSource>((FindObjectsSortMode)0)).FirstOrDefault((Func<AudioSource, bool>)((AudioSource a) => (Object)(object)a != (Object)null && ((Behaviour)a).isActiveAndEnabled && (Object)(object)a.outputAudioMixerGroup != (Object)null));
		return ((Object)(object)val != (Object)null) ? val.outputAudioMixerGroup : null;
	}

	public static void StartMusic()
	{
		if ((Object)(object)musicSource != (Object)null && musicLoaded)
		{
			musicSource.volume = Mathf.Clamp(musicVolume.Value, 0f, 100f) / 100f;
			musicSource.Play();
			Debug.Log((object)"=== SILENT PEAK: Silent Hill music started ===");
		}
		else
		{
			Debug.LogWarning((object)"=== SILENT PEAK: Cannot start music - not loaded ===");
		}
	}

	public static void StopMusic()
	{
		if ((Object)(object)musicSource != (Object)null && musicSource.isPlaying)
		{
			musicSource.Stop();
			Debug.Log((object)"=== SILENT PEAK: Silent Hill music stopped ===");
		}
	}

	public static void UpdateMusicVolume()
	{
		if ((Object)(object)musicSource != (Object)null)
		{
			musicSource.volume = Mathf.Clamp(musicVolume.Value, 0f, 100f) / 100f;
		}
	}
}
[HarmonyPatch(typeof(IllegalScreenEffect), "Update")]
public static class IllegalScreenEffectUpdatePatch
{
	public static bool Prefix(IllegalScreenEffect __instance)
	{
		if (!SilentPEAKPlugin.AlwaysActiveValue)
		{
			return true;
		}
		if ((Object)(object)RunManager.Instance == (Object)null || !RunManager.Instance.runStarted)
		{
			return true;
		}
		if (SilentPEAKPlugin.hasAppliedEffect && (Object)(object)__instance.character != (Object)null && (__instance.character.data.fullyPassedOut || __instance.character.data.dead) && __instance.activeForSeconds > 100000f)
		{
			return false;
		}
		return true;
	}

	public static void Postfix(IllegalScreenEffect __instance)
	{
		if (!SilentPEAKPlugin.AlwaysActiveValue || (Object)(object)RunManager.Instance == (Object)null || !RunManager.Instance.runStarted)
		{
			return;
		}
		if (RunManager.Instance.timeSinceRunStarted < 20f)
		{
			if (Time.time - SilentPEAKPlugin.lastApplicationTime >= 1f)
			{
				SilentPEAKPlugin.lastApplicationTime = Time.time;
				ApplyBlindEffect(__instance);
			}
		}
		else if (!SilentPEAKPlugin.hasAppliedEffect)
		{
			ApplyBlindEffect(__instance);
			SilentPEAKPlugin.hasAppliedEffect = true;
		}
	}

	private static void ApplyBlindEffect(IllegalScreenEffect __instance)
	{
		if (!((Object)(object)__instance.character != (Object)null) || (!__instance.character.data.fullyPassedOut && !__instance.character.data.dead))
		{
			__instance.activeForSeconds = 1000000f;
		}
	}
}
[HarmonyPatch(/*Could not decode attribute arguments.*/)]
public static class MusicVolumeUpdatePatch
{
	public static void Postfix(ConfigEntry<float> __instance)
	{
		if (__instance == SilentPEAKPlugin.musicVolume)
		{
			SilentPEAKPlugin.UpdateMusicVolume();
		}
	}
}