Decompiled source of SleepPhonkMandrake v1.0.2

plugins/SleepPhonkMandrake/SleepPhonkMandrake.dll

Decompiled 6 days ago
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace SleepPhonkMandrake;

[BepInPlugin("local.peak.sleepphonkmandrake", "Sleep Phonk Mandrake", "1.0.2")]
public class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Mandrake), "Start")]
	private static class MandrakeStartPatch
	{
		private static void Postfix(Mandrake __instance)
		{
			if ((Object)(object)ScreamClip != (Object)null && (Object)(object)__instance.sfxScream != (Object)null)
			{
				__instance.sfxScream.clips = (AudioClip[])(object)new AudioClip[1] { ScreamClip };
			}
		}
	}

	[HarmonyPatch(typeof(MandrakeScreamFX), "StartScreamFX")]
	private static class MandrakeScreamFXPatch
	{
		private static void Prefix(MandrakeScreamFX __instance)
		{
			if ((Object)(object)ScreamClip != (Object)null && (Object)(object)__instance.sfxScream != (Object)null)
			{
				__instance.sfxScream.clips = (AudioClip[])(object)new AudioClip[1] { ScreamClip };
			}
			if ((Object)(object)__instance.source != (Object)null)
			{
				__instance.source.loop = true;
			}
		}
	}

	[HarmonyPatch(typeof(SFX_Instance), "PlayFromSource")]
	private static class SFXPlayFromSourcePatch
	{
		private static bool Prefix(Vector3 position, AudioSource source)
		{
			if ((Object)(object)ScreamClip != (Object)null && (Object)(object)source != (Object)null && (Object)(object)source.clip == (Object)(object)ScreamClip && source.isPlaying)
			{
				source.loop = true;
				return false;
			}
			return true;
		}
	}

	internal static AudioClip ScreamClip;

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		new Harmony("local.peak.sleepphonkmandrake").PatchAll();
		((MonoBehaviour)this).StartCoroutine(LoadScream());
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Sleep Phonk Mandrake loaded");
	}

	private IEnumerator LoadScream()
	{
		string path = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "sleepphonk.mp3");
		if (!File.Exists(path))
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"sleepphonk.mp3 not found next to the plugin");
			yield break;
		}
		UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(path, (AudioType)13);
		try
		{
			yield return request.SendWebRequest();
			if ((int)request.result != 1)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Could not load sleepphonk.mp3: " + request.error));
				yield break;
			}
			ScreamClip = DownloadHandlerAudioClip.GetContent(request);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Mandrake scream replaced with sleepphonk.mp3");
		}
		finally
		{
			((IDisposable)request)?.Dispose();
		}
	}
}