Decompiled source of YetAnotherLethalLibrary v0.0.1

YetAnotherLethalLibrary.dll

Decompiled 9 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.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("YetAnotherLethalLibrary")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("YetAnotherLethalLibrary")]
[assembly: AssemblyTitle("YetAnotherLethalLibrary")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace YetAnotherLethalLibrary
{
	public static class AudioManager
	{
		private const string AUDIO_PLAYER_NAME = "YALLAudioSource";

		public static List<AudioSource> allAudioSources = new List<AudioSource>();

		public static void AddAudioSourceToPlayer(PlayerControllerB controller)
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: 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_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Expected O, but got Unknown
			Utility.ConditionLog($"Client Spawned: {controller.actualClientId}", condition: true, (LogLevel)16);
			GameObject gameObject = ((Component)controller).gameObject;
			Transform val = gameObject.transform.Find("YALLAudioSource");
			if ((Object)(object)val == (Object)null)
			{
				Utility.ConditionLog("Creating audio player...", condition: true, (LogLevel)16);
				val = new GameObject("YALLAudioSource").transform;
				val.SetParent(gameObject.transform);
				val.localPosition = Vector3.zero;
				val.localRotation = Quaternion.identity;
				val.localScale = Vector3.one;
				AudioSource val2 = ((Component)val).gameObject.AddComponent<AudioSource>();
				val2.rolloffMode = (AudioRolloffMode)2;
				val2.playOnAwake = false;
				val2.spatialBlend = 1f;
				val2.maxDistance = 20f;
				AnimationCurve val3 = new AnimationCurve((Keyframe[])(object)new Keyframe[5]
				{
					new Keyframe(0f, 1f, -4.666667f, -4.666667f),
					new Keyframe(0.04f, 0.84f, -3.708333f, -3.708333f),
					new Keyframe(0.2f, 0.42f, -1.975f, -1.975f),
					new Keyframe(0.4f, 0.18f, -17f / 24f, -17f / 24f),
					new Keyframe(1f, 0.05f, 0f, 0f)
				});
				val2.SetCustomCurve((AudioSourceCurveType)0, val3);
				Utility.ConditionLog("Adding audio player to list...", condition: true, (LogLevel)16);
				allAudioSources.Add(val2);
			}
		}

		public static List<AudioClip> LoadAudioClipsFromFolder(string folderPath)
		{
			string[] files = Directory.GetFiles(folderPath);
			List<AudioClip> list = new List<AudioClip>();
			string[] array = files;
			foreach (string text in array)
			{
				Utility.ConditionLog("Loading audio clip: " + text, condition: true, (LogLevel)16);
				AudioClip val = LoadAudioClipFromPath(text);
				if ((Object)(object)val != (Object)null)
				{
					list.Add(val);
				}
			}
			return list;
		}

		public static AudioClip LoadAudioClipFromPath(string path)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			return LoadAudioClipFromPath(path, AudioTypeFromPath(path));
		}

		public static AudioClip LoadAudioClipFromPath(string path, AudioType type)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Invalid comparison between Unknown and I4
			UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip(path, type);
			try
			{
				audioClip.SendWebRequest();
				string text;
				try
				{
					while (!audioClip.isDone)
					{
					}
					if ((int)audioClip.result == 1)
					{
						AudioClip content = DownloadHandlerAudioClip.GetContent(audioClip);
						((Object)content).name = Path.GetFileNameWithoutExtension(path);
						return content;
					}
					text = audioClip.error;
				}
				catch (Exception ex)
				{
					text = ex.ToString();
				}
				Utility.ConditionLog("Failed to load audio clip from " + path + ".\nError: " + text, condition: true, (LogLevel)2);
				return null;
			}
			finally
			{
				((IDisposable)audioClip)?.Dispose();
			}
		}

		public static AudioType AudioTypeFromPath(string path)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			return AudioTypeFromExtension(Path.GetExtension(path).ToLower());
		}

		public static AudioType AudioTypeFromExtension(string extension)
		{
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			return (AudioType)(extension switch
			{
				".wav" => 20, 
				".mp3" => 13, 
				".ogg" => 14, 
				".aiff" => 2, 
				".aif" => 2, 
				".aifc" => 2, 
				".flac" => 24, 
				_ => 0, 
			});
		}

		public static void PlayAudio(ulong clientID, AudioClip clip, float volume = 1f, float range = 20f, float loudness = 0.5f, bool audibleToEnemies = true)
		{
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			if (Utility.ConditionLog("Attempting to play a null audioclip!", (Object)(object)clip == (Object)null, (LogLevel)2))
			{
				return;
			}
			if (!TryGetPlayerCustomAudioSource(clientID, out var audioSource))
			{
				Utility.ConditionLog($"Audio Source not found for ID: {clientID}", condition: true, (LogLevel)2);
				return;
			}
			Utility.ConditionLog($"Playing clip {((Object)clip).name} for {clientID}", condition: true, (LogLevel)32);
			audioSource.volume = volume;
			audioSource.minDistance = 0f;
			audioSource.maxDistance = Mathf.Max(0.01f, range);
			audioSource.PlayOneShot(clip);
			if (!audibleToEnemies)
			{
				return;
			}
			Utility.ConditionLog("Playing audible noise...", condition: true, (LogLevel)16);
			if (GameManager.TryGetPlayerController(clientID, out var player))
			{
				bool flag = player.isInHangarShipRoom && StartOfRound.Instance.hangarDoorsClosed;
				Utility.ConditionLog($"Audible noise playing for {player.playerUsername}. (In Closed Ship: {flag})", condition: true, (LogLevel)32);
				if (clip.length <= 1f)
				{
					RoundManager.Instance.PlayAudibleNoise(((Component)player).transform.position, range, loudness, 0, flag, 0);
				}
				else
				{
					((MonoBehaviour)RoundManager.Instance).StartCoroutine(PlayAudibleNoiseForTime(clip.length - 0.5f, ((Component)player).transform.position, range / 2f, loudness, flag));
				}
			}
			else
			{
				Utility.ConditionLog($"Couldn't match ID {clientID} with a controller!", condition: true, (LogLevel)16);
			}
		}

		public static IEnumerator PlayAudibleNoiseForTime(float time, Vector3 position, float range = 20f, float loudness = 0.5f, bool isInClosedShip = false, float frequencyInSeconds = 0.5f)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			if (frequencyInSeconds < 0.2f)
			{
				frequencyInSeconds = 0.2f;
				Utility.ConditionLog("Adjust audible noise frequency to 0.2 seconds.", condition: true, (LogLevel)4);
			}
			while (time > 0f)
			{
				RoundManager.Instance.PlayAudibleNoise(position, range, loudness, 0, isInClosedShip, 0);
				time -= frequencyInSeconds;
				yield return (object)new WaitForSeconds(frequencyInSeconds);
			}
		}

		public static bool TryGetPlayerCustomAudioSource(ulong clientID, out AudioSource audioSource)
		{
			audioSource = null;
			if (GameManager.TryGetPlayerController(clientID, out var player))
			{
				Transform val = ((Component)player).transform.Find("YALLAudioSource");
				return ((Component)val).TryGetComponent<AudioSource>(ref audioSource);
			}
			Utility.ConditionLog($"Failed to get player controller for client {clientID}!", condition: true, (LogLevel)2);
			return false;
		}

		public static void Initialize()
		{
			Utility.ConditionLog("Initializing AudioManager...", condition: true, (LogLevel)16);
			GameManager.OnPlayerSpawned = (Action<PlayerControllerB>)Delegate.Remove(GameManager.OnPlayerSpawned, new Action<PlayerControllerB>(AddAudioSourceToPlayer));
			GameManager.OnPlayerSpawned = (Action<PlayerControllerB>)Delegate.Combine(GameManager.OnPlayerSpawned, new Action<PlayerControllerB>(AddAudioSourceToPlayer));
		}
	}
	public static class GameManager
	{
		public static Action<PlayerControllerB> OnPlayerSpawned;

		public static Action<PlayerControllerB> OnClientPlayerConnected;

		public static Action OnClientPlayerDisconnected;

		public static Action<PlayerControllerB> OnPlayerLateUpdate;

		public static Action<StartOfRound> OnRoundStart;

		public static Action<StartOfRound> OnRoundEnd;

		public static PlayerControllerB localPlayer;

		[HarmonyPatch(typeof(PlayerControllerB), "Start")]
		[HarmonyPostfix]
		private static void PlayerControllerBStartPostFix(PlayerControllerB __instance)
		{
			OnPlayerSpawned?.Invoke(__instance);
		}

		[HarmonyPatch(typeof(PlayerControllerB), "LateUpdate")]
		[HarmonyPostfix]
		private static void PlayerControllerBLateUpdatePostFix(PlayerControllerB __instance)
		{
			OnPlayerLateUpdate?.Invoke(__instance);
		}

		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		[HarmonyPostfix]
		private static void PlayerControllerBConnectClientToPlayerObjectPostFix(PlayerControllerB __instance)
		{
			localPlayer = __instance;
			OnClientPlayerConnected?.Invoke(__instance);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
		private static void GameNetworkManagerStartDisconnectPostFix()
		{
			OnClientPlayerDisconnected?.Invoke();
		}

		[HarmonyPatch(typeof(StartOfRound), "OnShipLandedMiscEvents")]
		[HarmonyPostfix]
		private static void StartOfOnShipLandedMiscEventsPostFix(StartOfRound __instance)
		{
			OnRoundStart?.Invoke(__instance);
		}

		[HarmonyPatch(typeof(StartOfRound), "ShipHasLeft")]
		[HarmonyPostfix]
		private static void StartOfRoundEndOfGamePostFix(StartOfRound __instance)
		{
			OnRoundEnd?.Invoke(__instance);
		}

		public static bool TryGetPlayerController(ulong id, out PlayerControllerB player)
		{
			player = null;
			StartOfRound instance = StartOfRound.Instance;
			if (Utility.ConditionLog("StartOfRound.Instance is null! Can't get player controller.", (Object)(object)instance == (Object)null, (LogLevel)2))
			{
				return false;
			}
			player = ((IEnumerable<PlayerControllerB>)StartOfRound.Instance.allPlayerScripts).FirstOrDefault((Func<PlayerControllerB, bool>)((PlayerControllerB pc) => pc.actualClientId == id));
			return (Object)(object)player != (Object)null;
		}
	}
	public static class Utility
	{
		public struct FloatRange
		{
			public float min;

			public float max;

			public FloatRange(float min, float max)
			{
				this.min = min;
				this.max = max;
			}

			public float RandomInRange()
			{
				return Random.Range(min, max);
			}

			public bool HasInRange(float value)
			{
				return value >= min && value <= max;
			}
		}

		internal static bool ConditionLog(string message, bool condition = true, LogLevel logLevel = 16)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			if (condition)
			{
				YALLPlugin.instance.logSource.Log(logLevel, (object)message);
			}
			return condition;
		}

		public static float[] NormalizeChances(params float[] chances)
		{
			float[] array = new float[chances.Length];
			float num = chances.Sum();
			for (int i = 0; i < chances.Length; i++)
			{
				array[i] = chances[i] / num;
			}
			return array;
		}

		public static FloatRange[] SpreadNormalizedChances(float[] normalizedChances, float maxRange = 100f)
		{
			FloatRange[] array = new FloatRange[normalizedChances.Length];
			float num = 0f;
			for (int i = 0; i < normalizedChances.Length; i++)
			{
				float num2 = normalizedChances[i] * maxRange;
				array[i] = new FloatRange(num, num + num2);
				num += num2;
			}
			return array;
		}
	}
	[BepInPlugin("YetAnotherLethalLibrary", "YetAnotherLethalLibrary", "1.0.0")]
	public class YALLPlugin : BaseUnityPlugin
	{
		internal static readonly Harmony _harmony = new Harmony("YetAnotherLethalLibrary");

		public static YALLPlugin instance;

		internal ManualLogSource logSource => ((BaseUnityPlugin)this).Logger;

		private void Awake()
		{
			instance = this;
			logSource.LogInfo((object)"Plugin YetAnotherLethalLibrary is loaded!");
			_harmony.PatchAll(typeof(GameManager));
			AudioManager.Initialize();
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "YetAnotherLethalLibrary";

		public const string PLUGIN_NAME = "YetAnotherLethalLibrary";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}