Decompiled source of JumpScream v1.0.12

JumpScream.dll

Decompiled 8 months 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.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using JumpScream;
using LCSoundTool;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
using UnityEngine;
using moose.dev;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("JumpScream")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("JumpScream")]
[assembly: AssemblyTitle("JumpScream")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace moose.dev
{
	[BepInPlugin("devmoose.jumpscream", "JumpScream", "1.0.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class JumpScreamPlugin : BaseUnityPlugin
	{
		public const string modGUID = "devmoose.jumpscream";

		public const string modName = "JumpScream";

		public const string modVersion = "1.0.0.0";

		public static JumpScreamPlugin Instance;

		internal static ManualLogSource Log;

		private readonly Harmony harmony = new Harmony("devmoose.jumpscream");

		public static List<AudioClip> Screams = new List<AudioClip>();

		public static string ScreamsDirectory;

		private static string[] AllowedSoundExtensions = new string[3] { ".mp3", ".wav", ".ogg" };

		public static ConfigEntry<int> ScreamTriggerPercent;

		public static ConfigEntry<double> ScreamTimeoutSeconds;

		public static Queue<AudioClip> MusicBoxClips;

		public static AssetBundle Assets;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			Log = Logger.CreateLogSource("devmoose.jumpscream");
			Log.LogMessage((object)"Applying patch.");
			LoadConfig();
			LoadAssetBundle();
			LoadScreams();
			ChangeMusicBox();
			LoadPeanutButter();
			harmony.PatchAll();
			Log.LogMessage((object)"Loaded succesfully.");
		}

		private void LoadConfig()
		{
			ScreamTriggerPercent = ((BaseUnityPlugin)this).Config.Bind<int>("Config", "ScreamTriggerPercent", 5, "The chance, in percent (0-100), that a door action will trigger a scream. Defaults to 5 percent.");
			ScreamTimeoutSeconds = ((BaseUnityPlugin)this).Config.Bind<double>("Config", "ScreamTimeout", 30.0, "The minimum amount of seconds between screams. Defaults to 30 seconds.");
			Log.LogMessage((object)"Configuration loaded.");
			Log.LogDebug((object)$"ScreamTriggerPercent: {ScreamTriggerPercent.Value}");
			Log.LogDebug((object)$"ScreamTimeout: {ScreamTimeoutSeconds.Value}");
		}

		private void LoadAssetBundle()
		{
			Assets = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "peanutbutter"));
		}

		private void ChangeMusicBox()
		{
			MusicBoxClips = new Queue<AudioClip>();
			string[] array = new string[10] { "Music1.mp3", "Music2.mp3", "Music3.mp3", "Music4.mp3", "Music5.mp3", "Music6.mp3", "Music7.mp3", "Music8.mp3", "Music9.mp3", "Music10.mp3" };
			foreach (string text in array)
			{
				AudioClip val = Assets.LoadAsset<AudioClip>("Assets/" + text);
				Log.LogDebug((object)("Loaded " + ((Object)val).name));
				((Object)val).name = text;
				MusicBoxClips.Enqueue(val);
			}
			foreach (AudioClip item in MusicBoxClips.ToList())
			{
				SoundTool.ReplaceAudioClip("RecordPlayerJazz", item, 1f / (float)MusicBoxClips.Count);
			}
		}

		private void LoadScreams()
		{
			if (ScreamsDirectory == null)
			{
				ScreamsDirectory = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)Instance).Info.Location));
				Log.LogMessage((object)("Sounds directory set to: " + ScreamsDirectory));
			}
			if (!Directory.Exists(ScreamsDirectory))
			{
				Log.LogMessage((object)"Sounds directory not found, creating");
				Directory.CreateDirectory(ScreamsDirectory);
			}
			foreach (string item in from file in Directory.GetFiles(ScreamsDirectory)
				where AllowedSoundExtensions.Any(file.ToLower().EndsWith)
				select file)
			{
				try
				{
					string fileName = Path.GetFileName(item);
					AudioClip audioClip = SoundTool.GetAudioClip(ScreamsDirectory, fileName);
					((Object)audioClip).name = "JumpScream";
					Screams.Add(audioClip);
					Log.LogDebug((object)("Loaded scream: " + item));
				}
				catch (Exception)
				{
					Log.LogError((object)("Failed to load scream: " + item));
				}
			}
		}

		private void LoadPeanutButter()
		{
			int num = 30;
			Item obj = Assets.LoadAsset<Item>("Assets/PeanutButter.asset");
			NetworkPrefabs.RegisterNetworkPrefab(obj.spawnPrefab);
			Items.RegisterScrap(obj, num, (LevelTypes)(-1));
			Item val = Assets.LoadAsset<Item>("Assets/BirthdayCake.asset");
			BirthdayCakeMusic birthdayCakeMusic = val.spawnPrefab.AddComponent<BirthdayCakeMusic>();
			((GrabbableObject)birthdayCakeMusic).grabbable = true;
			((GrabbableObject)birthdayCakeMusic).isInFactory = true;
			((GrabbableObject)birthdayCakeMusic).itemProperties = val;
			NetworkPrefabs.RegisterNetworkPrefab(val.spawnPrefab);
			Items.RegisterScrap(val, num, (LevelTypes)(-1));
		}
	}
	[HarmonyPatch(typeof(BoomboxItem), "Start")]
	internal class BoomboxItem_Start
	{
		[HarmonyPostfix]
		private static void Postfix(BoomboxItem __instance)
		{
			__instance.musicAudios = JumpScreamPlugin.MusicBoxClips.ToArray();
		}
	}
	[HarmonyPatch(typeof(BoomboxItem), "StartMusic")]
	internal class BoomboxItem_StartMusic
	{
		[HarmonyPostfix]
		private static void Postfix(BoomboxItem __instance, bool startMusic, ref int ___timesPlayedWithoutTurningOff)
		{
			if (startMusic)
			{
				Queue<AudioClip> musicBoxClips = JumpScreamPlugin.MusicBoxClips;
				AudioClip val = musicBoxClips.Dequeue();
				musicBoxClips.Enqueue(val);
				__instance.musicAudios = musicBoxClips.ToArray();
				__instance.boomboxAudio.clip = val;
				JumpScreamPlugin.Log.LogDebug((object)("Playing " + ((Object)__instance.boomboxAudio.clip).name));
				__instance.boomboxAudio.pitch = 1f;
				__instance.boomboxAudio.volume = 1f;
				__instance.boomboxAudio.Play();
			}
			else if (__instance.isPlayingMusic)
			{
				__instance.boomboxAudio.Stop();
				__instance.boomboxAudio.PlayOneShot(__instance.stopAudios[Random.Range(0, __instance.stopAudios.Length)]);
				___timesPlayedWithoutTurningOff = 0;
			}
			((GrabbableObject)__instance).isBeingUsed = startMusic;
			__instance.isPlayingMusic = startMusic;
		}
	}
	[HarmonyPatch(typeof(AudioSource))]
	internal class JumpScreamDoors
	{
		private static string[] doors = new string[2] { "DoorOpen", "EntranceTeleport" };

		public static DateTimeOffset lastScreamPlayed;

		public static object screamLock = new object();

		public static int lastPlayedIndex = -1;

		[HarmonyPatch("PlayOneShot", new Type[] { typeof(AudioClip) })]
		[HarmonyPatch("PlayOneShot", new Type[]
		{
			typeof(AudioClip),
			typeof(float)
		})]
		[HarmonyPostfix]
		public static void RandomAudioTrigger(AudioSource __instance, AudioClip clip)
		{
			string audioTrigger = ((clip != null) ? ((Object)clip).name : null) ?? "";
			DateTimeOffset now = DateTimeOffset.Now;
			JumpScreamPlugin.Log.LogDebug((object)("Audio trigger: " + ((clip != null) ? ((Object)clip).name : null)));
			if (((clip == null) ? null : ((Object)clip).name?.Equals("JumpScream", StringComparison.OrdinalIgnoreCase)).GetValueOrDefault() || !(now >= lastScreamPlayed.AddSeconds(JumpScreamPlugin.ScreamTimeoutSeconds.Value)) || !doors.Any((string door) => audioTrigger.Contains(door, StringComparison.OrdinalIgnoreCase)))
			{
				return;
			}
			bool flag = new Random().Next(100) < JumpScreamPlugin.ScreamTriggerPercent.Value;
			JumpScreamPlugin.Log.LogDebug((object)$"Door opened/closed - scream: {flag}");
			if (!flag || JumpScreamPlugin.Screams.Count <= 0)
			{
				return;
			}
			int num = -1;
			do
			{
				num = new Random().Next(JumpScreamPlugin.Screams.Count);
			}
			while (num == lastPlayedIndex);
			AudioClip val = JumpScreamPlugin.Screams[num];
			try
			{
				lock (screamLock)
				{
					HUDManager.Instance.UIAudio.PlayOneShot(val, 1f);
				}
				lastScreamPlayed = now;
				lastPlayedIndex = num;
			}
			catch
			{
			}
		}
	}
}
namespace JumpScream
{
	internal class BirthdayCakeMusic : PhysicsProp
	{
		private bool isPlaying;

		public override void ItemActivate(bool used, bool buttonDown = true)
		{
			((GrabbableObject)this).ItemActivate(used, buttonDown);
			if (Object.op_Implicit((Object)(object)((GrabbableObject)this).playerHeldBy))
			{
				AudioSource component = ((Component)this).gameObject.GetComponent<AudioSource>();
				if ((Object)(object)component.clip == (Object)null)
				{
					AudioClip clip = JumpScreamPlugin.Assets.LoadAsset<AudioClip>("Assets/birthday.mp3");
					component.clip = clip;
				}
				component.pitch = 1f;
				component.volume = 1f;
				if (buttonDown && !isPlaying)
				{
					component.Play();
					isPlaying = true;
				}
				else if (buttonDown && isPlaying)
				{
					component.Stop();
					isPlaying = false;
				}
			}
		}
	}
}