Decompiled source of PsychologicalHorrorMod2 v1.0.0

BepInEx\plugins\PsychologicalHorrorMod\PsychologicalHorrorMod.dll

Decompiled a day 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 GameNetcodeStuff;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("PsychologicalHorrorMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Lethal Company - Psikolojik korku modu: ses takibi, fener bozulması, mikrofon tepkisi, rastgele olaylar, jump scare.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PsychologicalHorrorMod")]
[assembly: AssemblyTitle("PsychologicalHorrorMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace PsychologicalHorrorMod;

public class JumpScareSystem : MonoBehaviour
{
	private Light[] _playerLights;

	private float[] _originalIntensities;

	private bool _isPlaying;

	private Canvas _overlayCanvas;

	private float _overlayAlpha;

	public void TriggerJumpScare()
	{
		if (!_isPlaying)
		{
			((MonoBehaviour)this).StartCoroutine(DoJumpScare());
		}
	}

	private IEnumerator DoJumpScare()
	{
		_isPlaying = true;
		GameObject localPlayerObject = PluginPatches.GetLocalPlayerObject();
		if ((Object)(object)localPlayerObject == (Object)null)
		{
			_isPlaying = false;
			yield break;
		}
		Light[] componentsInChildren = localPlayerObject.GetComponentsInChildren<Light>(true);
		if (componentsInChildren != null && componentsInChildren.Length != 0)
		{
			_playerLights = componentsInChildren;
			_originalIntensities = new float[componentsInChildren.Length];
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				_originalIntensities[i] = componentsInChildren[i].intensity;
				componentsInChildren[i].intensity = 0f;
			}
		}
		float flashDuration = 0.08f;
		float num = 0.15f;
		yield return (object)new WaitForSeconds(num);
		if (_playerLights != null)
		{
			for (int j = 0; j < _playerLights.Length; j++)
			{
				if ((Object)(object)_playerLights[j] != (Object)null)
				{
					_playerLights[j].intensity = ((_originalIntensities != null && j < _originalIntensities.Length) ? _originalIntensities[j] : 1f) * 1.4f;
				}
			}
		}
		AudioClip val = SoundBank.GetRandomFrom("scream", "fenere_vurma");
		if ((Object)(object)val == (Object)null)
		{
			val = CreateJumpScareSound();
		}
		if ((Object)(object)val != (Object)null)
		{
			GameObject val2 = new GameObject("PsychHorror_JumpScare");
			AudioSource obj = val2.AddComponent<AudioSource>();
			obj.spatialBlend = 0f;
			obj.volume = 0.7f;
			obj.pitch = Random.Range(0.95f, 1.05f);
			obj.clip = val;
			obj.Play();
			Object.Destroy((Object)val2, val.length + 0.5f);
		}
		yield return (object)new WaitForSeconds(flashDuration);
		if (_playerLights != null && _originalIntensities != null)
		{
			for (int k = 0; k < _playerLights.Length; k++)
			{
				if ((Object)(object)_playerLights[k] != (Object)null && k < _originalIntensities.Length)
				{
					_playerLights[k].intensity = _originalIntensities[k];
				}
			}
		}
		yield return (object)new WaitForSeconds(0.3f);
		_isPlaying = false;
	}

	private static AudioClip CreateJumpScareSound()
	{
		int num = 44100;
		float num2 = 0.4f;
		int num3 = Mathf.RoundToInt((float)num * num2);
		float[] array = new float[num3];
		for (int i = 0; i < num3; i++)
		{
			float num4 = (float)i / (float)num;
			float num5 = Mathf.Exp((0f - num4) * 8f);
			float num6 = (((float)i < (float)num * 0.05f) ? (Mathf.Sin(num4 * 400f) * 0.5f) : 0f);
			float num7 = Mathf.Sin(num4 * 60f) * 0.15f + Mathf.Sin(num4 * 97f) * 0.08f;
			array[i] = (num6 + num7) * num5;
		}
		AudioClip obj = AudioClip.Create("PsychJumpScare", num3, 1, num, false);
		obj.SetData(array, 0);
		return obj;
	}
}
public class LightDistortionSystem : MonoBehaviour
{
	private Light[] _playerLights;

	private float[] _originalIntensities;

	private float _flickerTimer;

	private float _nextBlackoutTime;

	private bool _blackoutActive;

	private AudioSource _ambientSource;

	private void Start()
	{
		_nextBlackoutTime = Time.time + Random.Range(20f, 45f);
		((MonoBehaviour)this).StartCoroutine(RefreshLightsRoutine());
		((MonoBehaviour)this).StartCoroutine(BlackoutRoutine());
	}

	private void Update()
	{
		ConfigEntry<bool> lightDistortionEnabled = PluginConfig.LightDistortionEnabled;
		if (lightDistortionEnabled != null && lightDistortionEnabled.Value && !_blackoutActive)
		{
			FlickerLights();
		}
	}

	private void FlickerLights()
	{
		RefreshPlayerLightsIfNeeded();
		if (_playerLights == null || _playerLights.Length == 0)
		{
			return;
		}
		_flickerTimer -= Time.deltaTime;
		if (_flickerTimer > 0f)
		{
			return;
		}
		_flickerTimer = Random.Range(0.02f, 0.12f);
		float num = PluginConfig.FlickerIntensity?.Value ?? 0.3f;
		for (int i = 0; i < _playerLights.Length; i++)
		{
			if (!((Object)(object)_playerLights[i] == (Object)null) && _originalIntensities != null && i < _originalIntensities.Length)
			{
				float num2 = _originalIntensities[i];
				_playerLights[i].intensity = num2 * (1f + (Random.value - 0.5f) * num * 2f);
			}
		}
	}

	private void RefreshPlayerLightsIfNeeded()
	{
		GameObject localPlayerObject = PluginPatches.GetLocalPlayerObject();
		if ((Object)(object)localPlayerObject == (Object)null)
		{
			return;
		}
		Light[] componentsInChildren = localPlayerObject.GetComponentsInChildren<Light>(true);
		if (componentsInChildren != null && componentsInChildren.Length != 0 && (_playerLights == null || _playerLights.Length != componentsInChildren.Length))
		{
			_playerLights = componentsInChildren;
			_originalIntensities = new float[componentsInChildren.Length];
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				_originalIntensities[i] = componentsInChildren[i].intensity;
			}
		}
	}

	private IEnumerator RefreshLightsRoutine()
	{
		while (true)
		{
			yield return (object)new WaitForSeconds(2f);
			if (!_blackoutActive)
			{
				RefreshPlayerLightsIfNeeded();
			}
		}
	}

	public void TriggerBlackoutOnce()
	{
		if (!_blackoutActive)
		{
			((MonoBehaviour)this).StartCoroutine(DoBlackout());
		}
	}

	private IEnumerator BlackoutRoutine()
	{
		while (true)
		{
			yield return (object)new WaitForSeconds(1f);
			ConfigEntry<bool> lightDistortionEnabled = PluginConfig.LightDistortionEnabled;
			if (lightDistortionEnabled != null && lightDistortionEnabled.Value && !((Object)(object)PluginPatches.GetLocalPlayerObject() == (Object)null) && !(Time.time < _nextBlackoutTime))
			{
				_nextBlackoutTime = Time.time + Random.Range(25f, 55f);
				yield return DoBlackout();
			}
		}
	}

	private IEnumerator DoBlackout()
	{
		_blackoutActive = true;
		RefreshPlayerLightsIfNeeded();
		float num = PluginConfig.BlackoutDuration?.Value ?? 1.5f;
		if (_playerLights != null)
		{
			for (int i = 0; i < _playerLights.Length; i++)
			{
				if ((Object)(object)_playerLights[i] != (Object)null)
				{
					if (_originalIntensities == null || i >= _originalIntensities.Length)
					{
						_ = _playerLights[i].intensity;
					}
					else
					{
						_ = _originalIntensities[i];
					}
					_playerLights[i].intensity = 0f;
				}
			}
		}
		if ((Object)(object)_ambientSource == (Object)null)
		{
			GameObject val = new GameObject("PsychHorror_Ambient");
			_ambientSource = val.AddComponent<AudioSource>();
			_ambientSource.spatialBlend = 0f;
			_ambientSource.loop = false;
			_ambientSource.volume = 0.5f;
		}
		AudioClip val2 = SoundBank.GetClip("ambians");
		if ((Object)(object)val2 == (Object)null)
		{
			val2 = CreateAmbientHorrorClip(num);
		}
		if ((Object)(object)val2 != (Object)null)
		{
			_ambientSource.clip = val2;
			_ambientSource.Play();
		}
		yield return (object)new WaitForSeconds(num);
		if (_playerLights != null && _originalIntensities != null)
		{
			for (int j = 0; j < _playerLights.Length; j++)
			{
				if ((Object)(object)_playerLights[j] != (Object)null && j < _originalIntensities.Length)
				{
					_playerLights[j].intensity = _originalIntensities[j];
				}
			}
		}
		_blackoutActive = false;
	}

	private static AudioClip CreateAmbientHorrorClip(float duration)
	{
		int num = 44100;
		int num2 = Mathf.RoundToInt((float)num * duration);
		float[] array = new float[num2];
		for (int i = 0; i < num2; i++)
		{
			float num3 = (float)i / (float)num;
			float num4 = Mathf.Sin(num3 * MathF.PI / duration) * 0.4f;
			array[i] = (Mathf.Sin(num3 * 80f) * 0.5f + Mathf.Sin(num3 * 190f) * 0.2f + Random.Range(-0.05f, 0.05f)) * num4;
		}
		AudioClip obj = AudioClip.Create("PsychAmbient", num2, 1, num, false);
		obj.SetData(array, 0);
		return obj;
	}
}
public class MicrophoneResponseSystem : MonoBehaviour
{
	private enum ResponseType
	{
		Whisper,
		Normal,
		Scream
	}

	private AudioSource _micSource;

	private AudioSource _responseSource;

	private string _micDevice;

	private const int SampleWindow = 128;

	private float _loudnessAccum;

	private float _responseDelay;

	private bool _micStarted;

	private void Start()
	{
		if (Microphone.devices == null || Microphone.devices.Length == 0)
		{
			ManualLogSource logger = PsychologicalHorrorPlugin.Logger;
			if (logger != null)
			{
				logger.LogWarning((object)"Mikrofon bulunamadı. Mikrofon tepkisi devre dışı.");
			}
		}
		else
		{
			_micDevice = Microphone.devices[0];
			_responseDelay = Time.time + Random.Range(2f, 5f);
			((MonoBehaviour)this).StartCoroutine(ProcessMicRoutine());
		}
	}

	private void OnDestroy()
	{
		if ((Object)(object)_micSource != (Object)null && (Object)(object)_micSource.clip != (Object)null && _micDevice != null)
		{
			Microphone.End(_micDevice);
		}
	}

	private float GetMicLoudness()
	{
		if ((Object)(object)_micSource == (Object)null || (Object)(object)_micSource.clip == (Object)null)
		{
			return 0f;
		}
		float[] array = new float[128];
		int num = Microphone.GetPosition(_micDevice) - 128;
		if (num < 0)
		{
			return 0f;
		}
		_micSource.clip.GetData(array, num);
		float num2 = 0f;
		for (int i = 0; i < array.Length; i++)
		{
			num2 += Mathf.Abs(array[i]);
		}
		return num2 / (float)array.Length;
	}

	private IEnumerator ProcessMicRoutine()
	{
		while (true)
		{
			yield return (object)new WaitForSeconds(0.2f);
			ConfigEntry<bool> microphoneEnabled = PluginConfig.MicrophoneEnabled;
			if (microphoneEnabled == null || !microphoneEnabled.Value)
			{
				continue;
			}
			if (!_micStarted)
			{
				bool flag = false;
				try
				{
					_micSource = ((Component)this).gameObject.AddComponent<AudioSource>();
					_micSource.clip = Microphone.Start(_micDevice, true, 1, 44100);
					_micSource.loop = true;
					_micSource.mute = true;
					_micSource.Play();
					_micStarted = true;
					flag = true;
				}
				catch (Exception ex)
				{
					ManualLogSource logger = PsychologicalHorrorPlugin.Logger;
					if (logger != null)
					{
						logger.LogWarning((object)("Mikrofon başlatılamadı: " + ex.Message));
					}
				}
				if (!flag)
				{
					yield return (object)new WaitForSeconds(5f);
					continue;
				}
			}
			float micLoudness = GetMicLoudness();
			_loudnessAccum = Mathf.Lerp(_loudnessAccum, micLoudness, 0.3f);
			if (!(Time.time < _responseDelay) && !(_loudnessAccum < 0.01f))
			{
				_responseDelay = Time.time + Random.Range(3f, 8f);
				if (_loudnessAccum > 0.08f)
				{
					PlayResponse(ResponseType.Scream);
				}
				else if (_loudnessAccum > 0.04f)
				{
					PlayResponse(ResponseType.Normal);
				}
				else
				{
					PlayResponse(ResponseType.Whisper);
				}
			}
		}
	}

	private void PlayResponse(ResponseType type)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Expected O, but got Unknown
		if ((Object)(object)_responseSource == (Object)null)
		{
			GameObject val = new GameObject("PsychHorror_Response");
			_responseSource = val.AddComponent<AudioSource>();
			_responseSource.spatialBlend = 0f;
			_responseSource.playOnAwake = false;
		}
		float delay = Random.Range(0.5f, 1.5f);
		((MonoBehaviour)this).StartCoroutine(PlayResponseDelayed(type, delay));
	}

	private IEnumerator PlayResponseDelayed(ResponseType type, float delay)
	{
		yield return (object)new WaitForSeconds(delay);
		AudioClip responseClip = GetResponseClip(type);
		if ((Object)(object)responseClip != (Object)null)
		{
			_responseSource.pitch = Random.Range(0.9f, 1.1f);
			_responseSource.volume = Random.Range(0.35f, 0.55f);
			_responseSource.clip = responseClip;
			_responseSource.Play();
		}
	}

	private static AudioClip GetResponseClip(ResponseType type)
	{
		AudioClip clip = SoundBank.GetClip(type switch
		{
			ResponseType.Whisper => "fısıldama", 
			ResponseType.Scream => "scream", 
			_ => "paranormal_efekt", 
		});
		if ((Object)(object)clip != (Object)null)
		{
			return clip;
		}
		return CreateResponseClip(type);
	}

	private static AudioClip CreateResponseClip(ResponseType type)
	{
		int num = 44100;
		float num2 = type switch
		{
			ResponseType.Whisper => Random.Range(0.8f, 1.5f), 
			ResponseType.Scream => Random.Range(0.6f, 1.2f), 
			_ => Random.Range(0.4f, 0.9f), 
		};
		int num3 = Mathf.RoundToInt((float)num * num2);
		float[] array = new float[num3];
		float num4 = type switch
		{
			ResponseType.Whisper => 320f, 
			ResponseType.Scream => 180f, 
			_ => 120f, 
		};
		float num5 = type switch
		{
			ResponseType.Whisper => 0.12f, 
			ResponseType.Scream => 0.25f, 
			_ => 0.18f, 
		};
		for (int i = 0; i < num3; i++)
		{
			float num6 = (float)i / (float)num;
			float num7 = Mathf.Sin(num6 * MathF.PI / num2);
			float num8 = Mathf.Sin(num6 * num4) + Mathf.Sin(num6 * num4 * 1.7f) * 0.4f;
			if (type == ResponseType.Whisper)
			{
				num8 += (Mathf.PerlinNoise(num6 * 30f, 0f) - 0.5f) * 0.5f;
			}
			array[i] = num8 * num7 * num5;
		}
		AudioClip obj = AudioClip.Create("PsychResponse", num3, 1, num, false);
		obj.SetData(array, 0);
		return obj;
	}
}
public static class PluginPatches
{
	private static Transform _localPlayerTransform;

	private static GameObject _localPlayerObject;

	private static float _nextSearchTime;

	public static Transform GetLocalPlayerTransform()
	{
		RefreshLocalPlayerIfNeeded();
		return _localPlayerTransform;
	}

	public static GameObject GetLocalPlayerObject()
	{
		RefreshLocalPlayerIfNeeded();
		return _localPlayerObject;
	}

	private static void RefreshLocalPlayerIfNeeded()
	{
		if (Time.time < _nextSearchTime)
		{
			return;
		}
		_nextSearchTime = Time.time + 0.5f;
		StartOfRound val = Object.FindObjectOfType<StartOfRound>();
		if ((Object)(object)val == (Object)null)
		{
			ClearPlayer();
			return;
		}
		PlayerControllerB localPlayerController = val.localPlayerController;
		if ((Object)(object)localPlayerController == (Object)null)
		{
			ClearPlayer();
			return;
		}
		_localPlayerTransform = ((Component)localPlayerController).transform;
		_localPlayerObject = ((Component)localPlayerController).gameObject;
	}

	private static void ClearPlayer()
	{
		_localPlayerTransform = null;
		_localPlayerObject = null;
	}
}
[BepInPlugin("com.psychologicalhorror.lc", "Psychological Horror Mod", "1.0.0")]
public class PsychologicalHorrorPlugin : BaseUnityPlugin
{
	internal static ManualLogSource Logger;

	internal static ConfigFile ConfigFile;

	internal static Harmony HarmonyInstance;

	private SoundTrackingSystem _soundTracking;

	private LightDistortionSystem _lightDistortion;

	private MicrophoneResponseSystem _microphoneResponse;

	private RandomHorrorEventSystem _randomHorror;

	private JumpScareSystem _jumpScare;

	private void Awake()
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Expected O, but got Unknown
		Logger = ((BaseUnityPlugin)this).Logger;
		ConfigFile = ((BaseUnityPlugin)this).Config;
		HarmonyInstance = new Harmony("com.psychologicalhorror.lc");
		BindConfig();
		SoundBank.SetPluginPath(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location));
		((MonoBehaviour)this).StartCoroutine(SoundBank.LoadAll());
		_soundTracking = ((Component)this).gameObject.AddComponent<SoundTrackingSystem>();
		_lightDistortion = ((Component)this).gameObject.AddComponent<LightDistortionSystem>();
		_microphoneResponse = ((Component)this).gameObject.AddComponent<MicrophoneResponseSystem>();
		_randomHorror = ((Component)this).gameObject.AddComponent<RandomHorrorEventSystem>();
		_jumpScare = ((Component)this).gameObject.AddComponent<JumpScareSystem>();
		Logger.LogInfo((object)"Psikolojik Korku Modu v1.0.0 yüklendi.");
	}

	private void BindConfig()
	{
		PluginConfig.SoundTrackingEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1_SesTakibi", "Aktif", true, "Ses takibi (adım/konuşma yönlendirme) açık.");
		PluginConfig.LightDistortionEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("2_Fener", "Aktif", true, "Fener titreme ve kararma açık.");
		PluginConfig.MicrophoneEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("3_Mikrofon", "Aktif", true, "Mikrofon tepkisi (bağırma/fısıldama) açık.");
		PluginConfig.RandomEventsEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("4_RastgeleOlaylar", "Aktif", true, "Rastgele korku olayları açık.");
		PluginConfig.JumpScareEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("5_JumpScare", "Aktif", true, "Jump scare (ses+ışık) açık.");
		PluginConfig.EventMinInterval = ((BaseUnityPlugin)this).Config.Bind<int>("4_RastgeleOlaylar", "MinAralikSaniye", 5, "Olaylar arası minimum süre (saniye).");
		PluginConfig.EventMaxInterval = ((BaseUnityPlugin)this).Config.Bind<int>("4_RastgeleOlaylar", "MaxAralikSaniye", 15, "Olaylar arası maksimum süre (saniye).");
		PluginConfig.BlackoutDuration = ((BaseUnityPlugin)this).Config.Bind<float>("2_Fener", "KararmaSuresi", 1.5f, "Kararma süresi (saniye).");
		PluginConfig.FlickerIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("2_Fener", "TitremeSiddeti", 0.3f, "Fener titreme şiddeti (0-1).");
	}
}
public static class PluginConfig
{
	public static ConfigEntry<bool> SoundTrackingEnabled;

	public static ConfigEntry<bool> LightDistortionEnabled;

	public static ConfigEntry<bool> MicrophoneEnabled;

	public static ConfigEntry<bool> RandomEventsEnabled;

	public static ConfigEntry<bool> JumpScareEnabled;

	public static ConfigEntry<int> EventMinInterval;

	public static ConfigEntry<int> EventMaxInterval;

	public static ConfigEntry<float> BlackoutDuration;

	public static ConfigEntry<float> FlickerIntensity;
}
public class RandomHorrorEventSystem : MonoBehaviour
{
	private JumpScareSystem _jumpScare;

	private LightDistortionSystem _lightDistortion;

	private SoundTrackingSystem _soundTracking;

	private float _nextEventTime;

	private void Start()
	{
		_jumpScare = ((Component)this).GetComponent<JumpScareSystem>();
		_lightDistortion = ((Component)this).GetComponent<LightDistortionSystem>();
		_soundTracking = ((Component)this).GetComponent<SoundTrackingSystem>();
		ScheduleNextEvent();
		((MonoBehaviour)this).StartCoroutine(EventLoop());
	}

	private void ScheduleNextEvent()
	{
		int num = PluginConfig.EventMinInterval?.Value ?? 5;
		int num2 = PluginConfig.EventMaxInterval?.Value ?? 15;
		num = Mathf.Clamp(num, 1, 60);
		num2 = Mathf.Clamp(num2, num, 120);
		_nextEventTime = Time.time + (float)Random.Range(num, num2);
	}

	private IEnumerator EventLoop()
	{
		while (true)
		{
			yield return (object)new WaitForSeconds(1f);
			ConfigEntry<bool> randomEventsEnabled = PluginConfig.RandomEventsEnabled;
			if (randomEventsEnabled != null && randomEventsEnabled.Value && !(Time.time < _nextEventTime))
			{
				ScheduleNextEvent();
				TriggerRandomEvent();
			}
		}
	}

	private void TriggerRandomEvent()
	{
		float value = Random.value;
		if (value < 0.35f)
		{
			ConfigEntry<bool> jumpScareEnabled = PluginConfig.JumpScareEnabled;
			if (jumpScareEnabled != null && jumpScareEnabled.Value && (Object)(object)_jumpScare != (Object)null)
			{
				_jumpScare.TriggerJumpScare();
				return;
			}
		}
		if (value < 0.55f && (Object)(object)_soundTracking != (Object)null)
		{
			_soundTracking.PlayDirectionalCreepySound();
		}
		else if (value < 0.8f && (Object)(object)_lightDistortion != (Object)null)
		{
			ConfigEntry<bool> lightDistortionEnabled = PluginConfig.LightDistortionEnabled;
			if (lightDistortionEnabled != null && lightDistortionEnabled.Value)
			{
				_lightDistortion.TriggerBlackoutOnce();
			}
		}
	}
}
public static class SoundBank
{
	private static string _soundsPath;

	private static readonly Dictionary<string, AudioClip> Clips = new Dictionary<string, AudioClip>();

	private static bool _loading;

	private static bool _loaded;

	public static bool IsLoaded => _loaded;

	public static void SetPluginPath(string pluginDirectory)
	{
		_soundsPath = Path.Combine(pluginDirectory, "sounds");
	}

	public static IEnumerator LoadAll()
	{
		if (_loading || _loaded || string.IsNullOrEmpty(_soundsPath))
		{
			yield break;
		}
		_loading = true;
		if (!Directory.Exists(_soundsPath))
		{
			ManualLogSource logger = PsychologicalHorrorPlugin.Logger;
			if (logger != null)
			{
				logger.LogInfo((object)("Ses klasörü yok, procedürel sesler kullanılacak: " + _soundsPath));
			}
			_loading = false;
			_loaded = true;
			yield break;
		}
		string[] files = Directory.GetFiles(_soundsPath, "*.ogg");
		string[] array = files;
		foreach (string text in array)
		{
			string key = Path.GetFileNameWithoutExtension(text);
			string text2 = "file:///" + text.Replace("\\", "/");
			UnityWebRequest req = UnityWebRequestMultimedia.GetAudioClip(text2, (AudioType)14);
			try
			{
				yield return req.SendWebRequest();
				if ((int)req.result == 1)
				{
					AudioClip content = DownloadHandlerAudioClip.GetContent(req);
					if ((Object)(object)content != (Object)null)
					{
						Clips[key] = content;
						ManualLogSource logger2 = PsychologicalHorrorPlugin.Logger;
						if (logger2 != null)
						{
							logger2.LogDebug((object)("Ses yüklendi: " + key));
						}
					}
				}
				else
				{
					ManualLogSource logger3 = PsychologicalHorrorPlugin.Logger;
					if (logger3 != null)
					{
						logger3.LogWarning((object)("Ses yüklenemedi: " + key + " - " + req.error));
					}
				}
			}
			finally
			{
				((IDisposable)req)?.Dispose();
			}
		}
		ManualLogSource logger4 = PsychologicalHorrorPlugin.Logger;
		if (logger4 != null)
		{
			logger4.LogInfo((object)$"SoundBank: {Clips.Count} ses dosyası yüklendi.");
		}
		_loading = false;
		_loaded = true;
	}

	public static AudioClip GetClip(string name)
	{
		if (name == null)
		{
			return null;
		}
		if (!Clips.TryGetValue(name, out var value))
		{
			return null;
		}
		return value;
	}

	public static AudioClip GetRandomFrom(params string[] names)
	{
		if (names == null || names.Length == 0)
		{
			return null;
		}
		List<AudioClip> list = new List<AudioClip>();
		for (int i = 0; i < names.Length; i++)
		{
			AudioClip clip = GetClip(names[i]);
			if ((Object)(object)clip != (Object)null)
			{
				list.Add(clip);
			}
		}
		if (list.Count == 0)
		{
			return null;
		}
		return list[Random.Range(0, list.Count)];
	}
}
public class SoundTrackingSystem : MonoBehaviour
{
	private Transform _player;

	private Vector3 _lastPosition;

	private float _moveAccumulator;

	private float _nextSoundTime;

	private AudioSource _poolSource;

	private const float MoveThreshold = 0.15f;

	private const float MinInterval = 1.5f;

	private const float MaxInterval = 4f;

	private void Start()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		_lastPosition = Vector3.zero;
		_nextSoundTime = Time.time + Random.Range(1.5f, 4f);
		((MonoBehaviour)this).StartCoroutine(PlayDirectionalSoundsRoutine());
	}

	private void Update()
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: 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)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		ConfigEntry<bool> soundTrackingEnabled = PluginConfig.SoundTrackingEnabled;
		if (soundTrackingEnabled == null || !soundTrackingEnabled.Value)
		{
			return;
		}
		_player = PluginPatches.GetLocalPlayerTransform();
		if (!((Object)(object)_player == (Object)null))
		{
			Vector3 position = _player.position;
			float num = Vector3.Distance(position, _lastPosition);
			_lastPosition = position;
			if (num > 0.15f)
			{
				_moveAccumulator += num;
			}
			else
			{
				_moveAccumulator = Mathf.Max(0f, _moveAccumulator - Time.deltaTime * 2f);
			}
		}
	}

	private IEnumerator PlayDirectionalSoundsRoutine()
	{
		while (true)
		{
			yield return (object)new WaitForSeconds(0.5f);
			ConfigEntry<bool> soundTrackingEnabled = PluginConfig.SoundTrackingEnabled;
			if (soundTrackingEnabled != null && soundTrackingEnabled.Value)
			{
				_player = PluginPatches.GetLocalPlayerTransform();
				if (!((Object)(object)_player == (Object)null) && !(Time.time < _nextSoundTime) && !(_moveAccumulator < 0.3f))
				{
					_moveAccumulator = 0f;
					_nextSoundTime = Time.time + Random.Range(1.5f, 4f);
					PlayDirectionalCreepySound();
				}
			}
		}
	}

	public void PlayDirectionalCreepySound()
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: 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)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: 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_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: 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: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Expected O, but got Unknown
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_player == (Object)null))
		{
			Vector3 forward = _player.forward;
			forward.y = 0f;
			((Vector3)(ref forward)).Normalize();
			Vector3 val = Vector3.Cross(Vector3.up, forward);
			_ = ((Vector3)(ref val)).normalized;
			float num = Random.Range(0f, 360f) * (MathF.PI / 180f);
			Vector3 val2 = new Vector3(Mathf.Cos(num), 0f, Mathf.Sin(num)) * Random.Range(3f, 8f);
			Vector3 position = _player.position + val2;
			GameObject val3 = new GameObject("PsychHorror_DirSound");
			val3.transform.position = position;
			AudioSource val4 = val3.AddComponent<AudioSource>();
			val4.spatialBlend = 1f;
			val4.minDistance = 2f;
			val4.maxDistance = 25f;
			val4.rolloffMode = (AudioRolloffMode)1;
			val4.pitch = Random.Range(0.85f, 1.15f);
			val4.volume = Random.Range(0.4f, 0.7f);
			val4.playOnAwake = false;
			val4.clip = GetCreepyClip();
			if ((Object)(object)val4.clip != (Object)null)
			{
				val4.Play();
				Object.Destroy((Object)(object)val3, val4.clip.length + 0.5f);
			}
			else
			{
				Object.Destroy((Object)(object)val3, 2f);
			}
		}
	}

	private static AudioClip GetCreepyClip()
	{
		AudioClip randomFrom = SoundBank.GetRandomFrom("ayaksesi1", "ayaksesi2", "gıcırtı", "paranormal_efekt", "elektrik_titreme", "fenere_vurma");
		if (!((Object)(object)randomFrom != (Object)null))
		{
			return CreateProceduralBreath();
		}
		return randomFrom;
	}

	private static AudioClip CreateProceduralBreath()
	{
		int num = 44100;
		float num2 = Random.Range(0.3f, 0.8f);
		int num3 = Mathf.RoundToInt((float)num * num2);
		float[] array = new float[num3];
		for (int i = 0; i < num3; i++)
		{
			float num4 = (float)i / (float)num;
			float num5 = Mathf.Exp((0f - num4) * 2f) * (1f - (float)i / (float)num3);
			array[i] = (Mathf.Sin(num4 * 120f) + Mathf.Sin(num4 * 237f) * 0.3f) * num5 * 0.15f;
		}
		AudioClip obj = AudioClip.Create("PsychBreath", num3, 1, num, false);
		obj.SetData(array, 0);
		return obj;
	}
}
public static class PluginInfo
{
	public const string PLUGIN_GUID = "com.psychologicalhorror.lc";

	public const string PLUGIN_NAME = "Psychological Horror Mod";

	public const string PLUGIN_VERSION = "1.0.0";
}