Decompiled source of WeskerThumper v0.1.1

BepInEx/plugins/WeskerThumper.dll

Decompiled a month ago
using System;
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.Logging;
using HarmonyLib;
using NewerWeskerThumper;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.Video;
using WeskerThumper;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = ".NET Framework 4.6")]
[assembly: AssemblyCompany("WeskerThumper")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1")]
[assembly: AssemblyProduct("NexusRealities-WeskerThumper")]
[assembly: AssemblyTitle("WeskerThumper")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.1.0")]
[module: UnverifiableCode]
namespace NewerWeskerThumper
{
	public class mainmenu
	{
		private static AudioClip menuTheme;

		private static MenuManager mainMenuInstance;

		public static void Init()
		{
			Harmony.CreateAndPatchAll(typeof(mainmenu), (string)null);
			try
			{
				Plugin.MyLogger.LogInfo((object)"Logging custom music...");
				UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip("file://" + Plugin.BuildSoundPath("Ambient/main_menu.ogg"), (AudioType)14);
				UnityWebRequestAsyncOperation val = audioClip.SendWebRequest();
				((AsyncOperation)val).completed += MainMenuMusic;
			}
			catch (Exception ex)
			{
				Plugin.MyLogger.LogInfo((object)("Error: " + ex.Message));
			}
		}

		private static void MainMenuMusic(AsyncOperation operation)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			Plugin.MyLogger.LogInfo((object)"Attempt to patch in custom music");
			UnityWebRequestAsyncOperation val = (UnityWebRequestAsyncOperation)operation;
			menuTheme = DownloadHandlerAudioClip.GetContent(val.webRequest);
			Plugin.MyLogger.LogInfo((object)("Patching custom music " + (object)menuTheme));
			if ((Object)(object)mainMenuInstance != (Object)null)
			{
				PatchMenuManager(mainMenuInstance);
			}
			Debug.Log((object)"Main menu music loaded!");
		}

		private static void PatchMenuManager(MenuManager instance)
		{
			mainMenuInstance = instance;
			instance.menuMusic = menuTheme;
			if (!instance.isInitScene)
			{
				instance.MenuAudio = ((Component)instance).gameObject.AddComponent<AudioSource>();
				instance.MenuAudio.volume = 0.3f;
				instance.MenuAudio.loop = true;
				instance.MenuAudio.spatialize = false;
				instance.MenuAudio.spatialBlend = 0f;
				instance.MenuAudio.clip = menuTheme;
				instance.MenuAudio.Play();
			}
		}

		[HarmonyPatch(typeof(MenuManager), "Start")]
		[HarmonyPostfix]
		private static void Postfix_MenuManagerStart(MenuManager __instance)
		{
			PatchMenuManager(__instance);
		}
	}
	[BepInPlugin("WeskerThumper", "NexusRealities-WeskerThumper", "0.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource MyLogger = null;

		private static string AssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

		public static AssetBundle Assets { get; protected set; }

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Loading assets : " + Path.Combine(AssemblyPath, "modassets")));
			Assets = AssetBundle.LoadFromFile(Path.Combine(AssemblyPath, "modassets"));
			MyLogger = ((BaseUnityPlugin)this).Logger;
			CrawlerPatcher.Init();
			mainmenu.Init();
			LocalPlayer.Precache();
			tv.Init();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin NexusRealities-WeskerThumper is loaded!");
			SceneManager.sceneLoaded += OnSceneLoaded;
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			if ((Object)(object)LocalPlayer.Instance == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Creating local player...");
				LocalPlayer.Init();
			}
		}

		public static string BuildSoundPath(string path)
		{
			string text = Paths.PluginPath + "/NexusRealities-WeskerThumper/SFX/" + path;
			MyLogger.LogInfo((object)text);
			return text;
		}

		public static string BuildTVPath(string path)
		{
			string text = Paths.PluginPath + "/NexusRealities-WeskerThumper/TV/" + path + ".mp4";
			MyLogger.LogInfo((object)("Video: " + text));
			return text;
		}
	}
	public class TvCustomizer : MonoBehaviour
	{
		private static string[] videoslist = new string[25]
		{
			"gulp", "nooo", "cat", "p", "chicken", "dance", "horse", "sus_rage_cat", "fang", "fart",
			"coach", "wizardsinarbys", "markiplier", "catter", "heavy", "what_day_is_it", "iron", "hero", "fnaf", "bird",
			"oiiaoiia", "baby", "ceiling", "car", "ee"
		};

		private static VideoClip[] _videoClips = (VideoClip[])(object)new VideoClip[videoslist.Length];

		protected VideoPlayer[] videoPlayers = (VideoPlayer[])(object)new VideoPlayer[videoslist.Length];

		protected AudioSource[] audioClips = (AudioSource[])(object)new AudioSource[videoslist.Length];

		protected int lastPlayedClip = 0;

		public static void Precache()
		{
			for (int i = 0; i < videoslist.Length; i++)
			{
				string text = "Assets/Clips/" + videoslist[i] + ".mp4";
				_videoClips[i] = Plugin.Assets.LoadAsset<VideoClip>(text);
				if ((Object)(object)_videoClips[i] == (Object)null)
				{
					Plugin.MyLogger.LogError((object)("Failed to load : " + text));
				}
			}
		}

		public void Start()
		{
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			TVScript component = ((Component)this).gameObject.GetComponent<TVScript>();
			for (int i = 0; i < _videoClips.Length; i++)
			{
				VideoPlayer val = ((Component)this).gameObject.AddComponent<VideoPlayer>();
				val.clip = _videoClips[i];
				val.source = (VideoSource)0;
				val.controlledAudioTrackCount = 1;
				val.audioOutputMode = (VideoAudioOutputMode)1;
				val.isLooping = component.video.isLooping;
				AudioSource val2 = ((Component)this).gameObject.AddComponent<AudioSource>();
				val2.outputAudioMixerGroup = component.tvSFX.outputAudioMixerGroup;
				val2.mute = component.tvSFX.mute;
				val2.spatialize = component.tvSFX.spatialize;
				val2.spatializePostEffects = component.tvSFX.spatializePostEffects;
				val2.bypassEffects = component.tvSFX.bypassEffects;
				val2.bypassListenerEffects = component.tvSFX.bypassListenerEffects;
				val2.bypassReverbZones = component.tvSFX.bypassReverbZones;
				val2.playOnAwake = component.tvSFX.playOnAwake;
				val2.loop = component.tvSFX.loop;
				val2.priority = component.tvSFX.priority;
				val2.volume = component.tvSFX.volume;
				val2.pitch = component.tvSFX.pitch;
				val2.panStereo = component.tvSFX.panStereo;
				val2.spatialBlend = component.tvSFX.spatialBlend;
				val2.reverbZoneMix = component.tvSFX.reverbZoneMix;
				val2.rolloffMode = component.tvSFX.rolloffMode;
				val2.spread = component.tvSFX.spread;
				val2.dopplerLevel = component.tvSFX.dopplerLevel;
				val2.minDistance = component.tvSFX.minDistance;
				val2.maxDistance = component.tvSFX.maxDistance;
				val.SetTargetAudioSource((ushort)0, val2);
				val.Stop();
				val.Prepare();
				val.Pause();
				videoPlayers[i] = val;
				audioClips[i] = val2;
				Plugin.MyLogger.LogInfo((object)("E " + ((object)videoPlayers[i])?.ToString() + " " + (object)audioClips[i]));
			}
		}

		public void PlayClip(int index)
		{
			StopClip();
			lastPlayedClip = index;
			TVScript component = ((Component)this).gameObject.GetComponent<TVScript>();
			VideoPlayer val = videoPlayers[index];
			val.Play();
			val.targetTexture = component.video.targetTexture;
			val.targetMaterialRenderer = component.video.targetMaterialRenderer;
			val.targetCameraAlpha = component.video.targetCameraAlpha;
			val.targetCamera = component.video.targetCamera;
			SetTVScreenMaterial(component, b: true);
			component.tvSFX.PlayOneShot(component.switchTVOn);
		}

		public void StopClip()
		{
			TVScript component = ((Component)this).gameObject.GetComponent<TVScript>();
			for (int i = 1; i <= 2; i++)
			{
				VideoPlayer val = videoPlayers[lastPlayedClip];
				val.Pause();
				val.time = 0.0;
				val.frame = 0L;
				val.time = 0.0;
				val.targetTexture = null;
				val.targetMaterialRenderer = null;
				val.targetCamera = null;
				AudioSource val2 = audioClips[lastPlayedClip];
				val2.Stop(true);
				val2.Stop();
				val2.time = 0f;
			}
			component.tvSFX.Stop();
			SetTVScreenMaterial(component, b: false);
			component.tvSFX.PlayOneShot(component.switchTVOff);
		}

		public static int PlayRandomClip()
		{
			return Random.Range(0, videoslist.Length);
		}

		protected static void SetTVScreenMaterial(TVScript instance, bool b)
		{
			MethodInfo method = ((object)instance).GetType().GetMethod("SetTVScreenMaterial", BindingFlags.Instance | BindingFlags.NonPublic);
			method.Invoke(instance, new object[1] { b });
		}
	}
	public class tv
	{
		public static void Init()
		{
			TvCustomizer.Precache();
			Harmony.CreateAndPatchAll(typeof(tv), (string)null);
		}

		[HarmonyPatch(typeof(TVScript), "Update")]
		[HarmonyPrefix]
		public static bool Update(TVScript __instance)
		{
			if ((Object)(object)((Component)__instance).gameObject.GetComponent<TvCustomizer>() == (Object)null)
			{
				((Component)__instance).gameObject.AddComponent<TvCustomizer>();
			}
			return false;
		}

		[HarmonyPatch(typeof(TVScript), "TVFinishedClip")]
		[HarmonyPrefix]
		public static bool TVFinishedClip(VideoPlayer source)
		{
			return false;
		}

		[HarmonyPatch(typeof(TVScript), "TurnOnTVServerRpc")]
		[HarmonyPrefix]
		public static bool TurnOnTVServerRpc(TVScript __instance, ref int ___currentClip, ref float ___timeSinceTurningOffTV, int _____rpc_exec_stage)
		{
			if (_____rpc_exec_stage != 1 || (!((NetworkBehaviour)__instance).NetworkManager.IsServer && !((NetworkBehaviour)__instance).NetworkManager.IsHost))
			{
				return true;
			}
			Plugin.MyLogger.LogInfo((object)("Clip state: " + _____rpc_exec_stage));
			___timeSinceTurningOffTV = 9999f;
			___currentClip = TvCustomizer.PlayRandomClip();
			__instance.TurnOnTVAndSyncClientRpc(___currentClip, 9999f);
			return false;
		}

		[HarmonyPatch(typeof(TVScript), "TurnTVOnOff")]
		[HarmonyPrefix]
		public static bool TurnTVOnOff(bool on, TVScript __instance, int ___currentClip)
		{
			__instance.tvOn = on;
			TvCustomizer component = ((Component)__instance).gameObject.GetComponent<TvCustomizer>();
			if (on)
			{
				Plugin.MyLogger.LogInfo((object)"Turning on TV");
				component.PlayClip(___currentClip);
				WalkieTalkie.TransmitOneShotAudio(__instance.tvSFX, __instance.switchTVOn, 1f);
			}
			else
			{
				Plugin.MyLogger.LogInfo((object)"Turning off TV");
				component.StopClip();
				WalkieTalkie.TransmitOneShotAudio(__instance.tvSFX, __instance.switchTVOff, 1f);
			}
			return false;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "WeskerThumper";

		public const string PLUGIN_NAME = "NexusRealities-WeskerThumper";

		public const string PLUGIN_VERSION = "0.1.1";
	}
}
namespace WeskerThumper
{
	public static class CrawlerPatcher
	{
		private static Dictionary<UnityWebRequestAsyncOperation, int> requests = new Dictionary<UnityWebRequestAsyncOperation, int>();

		public static AudioClip[] weskerSpotted = (AudioClip[])(object)new AudioClip[3];

		public static AudioClip[] weskerBash = (AudioClip[])(object)new AudioClip[1];

		public static void Init()
		{
			Harmony.CreateAndPatchAll(typeof(CrawlerPatcher), (string)null);
			for (int i = 0; i < 3; i++)
			{
				UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip("file://" + Plugin.BuildSoundPath("Thumper/spotted_" + (i + 1) + ".ogg"), (AudioType)14);
				UnityWebRequestAsyncOperation val = audioClip.SendWebRequest();
				requests[val] = i;
				((AsyncOperation)val).completed += ThumperSpotted;
			}
			UnityWebRequest audioClip2 = UnityWebRequestMultimedia.GetAudioClip("file://" + Plugin.BuildSoundPath("Thumper/bash.ogg"), (AudioType)14);
			UnityWebRequestAsyncOperation val2 = audioClip2.SendWebRequest();
			((AsyncOperation)val2).completed += ThumperBash;
		}

		private static void ThumperSpotted(AsyncOperation operation)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			UnityWebRequestAsyncOperation val = (UnityWebRequestAsyncOperation)operation;
			weskerSpotted[requests[val]] = DownloadHandlerAudioClip.GetContent(val.webRequest);
			Debug.Log((object)("Sppotted file (" + requests[val] + ") loaded!"));
		}

		private static void ThumperBash(AsyncOperation operation)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			UnityWebRequestAsyncOperation val = (UnityWebRequestAsyncOperation)operation;
			weskerBash[0] = DownloadHandlerAudioClip.GetContent(val.webRequest);
			Debug.Log((object)"Bash file loaded!");
		}

		[HarmonyPatch(typeof(CrawlerAI), "CollideWithWallClientRpc")]
		[HarmonyPostfix]
		private static void Postfix_CollideWithWallClientRpc(CrawlerAI __instance)
		{
			RoundManager.PlayRandomClip(((EnemyAI)__instance).creatureVoice, weskerBash, true, 2f, 0, 1000);
		}

		[HarmonyPatch(typeof(CrawlerAI), "Start")]
		[HarmonyPostfix]
		private static void Postfix_Start(CrawlerAI __instance)
		{
			__instance.longRoarSFX = weskerSpotted;
			((EnemyAI)__instance).creatureVoice.volume = 2f;
		}

		[HarmonyPatch(typeof(CrawlerAI), "MakeScreech")]
		[HarmonyPrefix]
		private static void Prefix_MakeScreech(CrawlerAI __instance)
		{
			((EnemyAI)__instance).creatureVoice.volume = 2f;
		}

		[HarmonyPatch(typeof(CrawlerAI), "Update")]
		[HarmonyPostfix]
		private static void Postfix_Update(CrawlerAI __instance)
		{
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			if (((EnemyAI)__instance).isEnemyDead)
			{
				return;
			}
			Type typeFromHandle = typeof(CrawlerAI);
			if (!(bool)typeFromHandle.InvokeMember("hasEnteredChaseMode", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField, null, __instance, null) || (bool)typeFromHandle.InvokeMember("lostPlayerInChase", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField, null, __instance, null))
			{
				return;
			}
			if ((Object)(object)((EnemyAI)__instance).targetPlayer == (Object)(object)GameNetworkManager.Instance.localPlayerController)
			{
				LocalPlayer.Instance.UpdateChase(EnemyType.Thumper);
				return;
			}
			Vector3 position = ((Component)GameNetworkManager.Instance.localPlayerController.gameplayCamera).gameObject.transform.position;
			if ((double)Vector3.Distance(position, ((EnemyAI)__instance).eye.position) <= 200.0)
			{
				LocalPlayer.Instance.UpdateChase(EnemyType.Thumper);
			}
		}
	}
	public enum EnemyType : uint
	{
		Thumper,
		Max
	}
	public class LocalPlayer : MonoBehaviour
	{
		public static LocalPlayer Instance = null;

		public AudioSource chaseAudio = null;

		private float[] lastChaseTime = new float[1];

		private static AudioClip[] chaseMusic = (AudioClip[])(object)new AudioClip[1];

		public static void Init()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			new GameObject("LocalPlayer", new Type[1] { typeof(LocalPlayer) }).GetComponent<LocalPlayer>();
		}

		public static void Precache()
		{
			UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip("file://" + Plugin.BuildSoundPath("Thumper/chase.ogg"), (AudioType)14);
			UnityWebRequestAsyncOperation val = audioClip.SendWebRequest();
			((AsyncOperation)val).completed += ThumperChase;
		}

		private static void ThumperChase(AsyncOperation operation)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			chaseMusic[0] = DownloadHandlerAudioClip.GetContent(((UnityWebRequestAsyncOperation)operation).webRequest);
			Debug.Log((object)"Thumper music loaded!");
		}

		public virtual void Start()
		{
			if ((Object)(object)Instance != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)Instance).gameObject);
			}
			Instance = this;
			chaseAudio = ((Component)this).gameObject.AddComponent<AudioSource>();
			chaseAudio.loop = true;
			chaseAudio.spatialBlend = 0f;
			chaseAudio.spatialize = false;
			chaseAudio.volume = 0f;
			Object.DontDestroyOnLoad((Object)(object)this);
			Debug.Log((object)"Local player instantiated!");
		}

		public void UpdateChase(EnemyType type)
		{
			lastChaseTime[(uint)type] = Time.time;
		}

		public virtual void Update()
		{
			float time = Time.time;
			if ((double)time <= 1.0)
			{
				return;
			}
			if ((double)(time - lastChaseTime[0]) <= 1.0)
			{
				if (!chaseAudio.isPlaying)
				{
					chaseAudio.clip = chaseMusic[0];
					chaseAudio.volume = 0.1f;
					chaseAudio.Play();
				}
				else if (chaseAudio.volume < 0.6f)
				{
					AudioSource obj = chaseAudio;
					obj.volume += 0.2f * Time.deltaTime;
					if (chaseAudio.volume > 0.6f)
					{
						chaseAudio.volume = 0.6f;
					}
				}
			}
			else if (chaseAudio.isPlaying && (double)chaseAudio.volume > 0.0)
			{
				AudioSource obj2 = chaseAudio;
				obj2.volume -= 0.2f * Time.deltaTime;
				if (chaseAudio.volume < 0f)
				{
					chaseAudio.volume = 0f;
					chaseAudio.Stop();
				}
			}
		}
	}
}