Decompiled source of LethalFarts v1.0.2

LethalFarts.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LCSoundTool;
using LethalFarts.NetcodePatcher;
using LethalFarts.Patches;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LethalFarts")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LethalFarts")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("cec75e91-616f-48da-bd7b-e56ab8750399")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: NetcodePatchedAssembly]
internal class <Module>
{
	static <Module>()
	{
	}
}
namespace LethalFarts
{
	[BepInPlugin("BradStar879.LethalFarts", "Lethal Farts", "1.0.2")]
	public class LethalFartsBase : BaseUnityPlugin
	{
		private const string PLUGIN_GUID = "BradStar879.LethalFarts";

		private const string PLUGIN_NAME = "Lethal Farts";

		private const string PLUGIN_VERSION = "1.0.2";

		private readonly Harmony harmony = new Harmony("BradStar879.LethalFarts");

		internal static LethalFartsBase Instance;

		private ManualLogSource mls;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			mls = Logger.CreateLogSource("BradStar879.LethalFarts");
			mls.LogInfo((object)"Lethal Farts Initialized");
			harmony.PatchAll(typeof(LethalFartsBase));
			harmony.PatchAll(typeof(PlayerFartPatch));
			harmony.PatchAll(typeof(DeathFartPatch));
			harmony.PatchAll(typeof(LethalFartsNetworkObjectManager));
			NetcodePatcher();
		}

		internal static void Log(string message)
		{
			Instance.mls.LogInfo((object)message);
		}

		private static void NetcodePatcher()
		{
			Type[] types = Assembly.GetExecutingAssembly().GetTypes();
			Type[] array = types;
			foreach (Type type in array)
			{
				MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
				MethodInfo[] array2 = methods;
				foreach (MethodInfo methodInfo in array2)
				{
					object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), inherit: false);
					if (customAttributes.Length != 0)
					{
						methodInfo.Invoke(null, null);
					}
				}
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "BradStar879.LethalFarts";

		public const string PLUGIN_NAME = "Lethal Farts";

		public const string PLUGIN_VERSION = "1.0.2";
	}
}
namespace LethalFarts.Patches
{
	[HarmonyPatch(typeof(DeadBodyInfo))]
	internal class DeathFartPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void DeathPatch(ref AudioSource ___bodyAudio)
		{
			if ((Object)(object)___bodyAudio != (Object)null)
			{
				AudioClip val = PlayerFartPatch.farts[3];
				___bodyAudio.PlayOneShot(val);
			}
		}
	}
	internal class LethalFartsNetworkHandler : NetworkBehaviour
	{
		public static LethalFartsNetworkHandler Instance { get; private set; }

		public static event Action<string> FartEvent;

		public override void OnNetworkSpawn()
		{
			LethalFartsNetworkHandler.FartEvent = null;
			if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer)
			{
				LethalFartsNetworkHandler instance = Instance;
				if (instance != null)
				{
					((Component)instance).gameObject.GetComponent<NetworkObject>().Despawn(true);
				}
			}
			Instance = this;
			((NetworkBehaviour)this).OnNetworkSpawn();
		}

		[ServerRpc(RequireOwnership = false)]
		public void EventServerRpc(string eventName)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager == null || !networkManager.IsListening)
			{
				return;
			}
			if ((int)base.__rpc_exec_stage != 1 && (networkManager.IsClient || networkManager.IsHost))
			{
				ServerRpcParams val = default(ServerRpcParams);
				FastBufferWriter val2 = ((NetworkBehaviour)this).__beginSendServerRpc(4084531423u, val, (RpcDelivery)0);
				bool flag = eventName != null;
				((FastBufferWriter)(ref val2)).WriteValueSafe<bool>(ref flag, default(ForPrimitives));
				if (flag)
				{
					((FastBufferWriter)(ref val2)).WriteValueSafe(eventName, false);
				}
				((NetworkBehaviour)this).__endSendServerRpc(ref val2, 4084531423u, val, (RpcDelivery)0);
			}
			if ((int)base.__rpc_exec_stage == 1 && (networkManager.IsServer || networkManager.IsHost))
			{
				EventClientRpc(eventName);
			}
		}

		[ClientRpc]
		private void EventClientRpc(string eventName)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager == null || !networkManager.IsListening)
			{
				return;
			}
			if ((int)base.__rpc_exec_stage != 2 && (networkManager.IsServer || networkManager.IsHost))
			{
				ClientRpcParams val = default(ClientRpcParams);
				FastBufferWriter val2 = ((NetworkBehaviour)this).__beginSendClientRpc(934466365u, val, (RpcDelivery)0);
				bool flag = eventName != null;
				((FastBufferWriter)(ref val2)).WriteValueSafe<bool>(ref flag, default(ForPrimitives));
				if (flag)
				{
					((FastBufferWriter)(ref val2)).WriteValueSafe(eventName, false);
				}
				((NetworkBehaviour)this).__endSendClientRpc(ref val2, 934466365u, val, (RpcDelivery)0);
			}
			if ((int)base.__rpc_exec_stage == 2 && (networkManager.IsClient || networkManager.IsHost))
			{
				LethalFartsNetworkHandler.FartEvent?.Invoke(eventName);
			}
		}

		protected override void __initializeVariables()
		{
			((NetworkBehaviour)this).__initializeVariables();
		}

		[RuntimeInitializeOnLoadMethod]
		internal static void InitializeRPCS_LethalFartsNetworkHandler()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			NetworkManager.__rpc_func_table.Add(4084531423u, new RpcReceiveHandler(__rpc_handler_4084531423));
			NetworkManager.__rpc_func_table.Add(934466365u, new RpcReceiveHandler(__rpc_handler_934466365));
		}

		private static void __rpc_handler_4084531423(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				bool flag = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref flag, default(ForPrimitives));
				string eventName = null;
				if (flag)
				{
					((FastBufferReader)(ref reader)).ReadValueSafe(ref eventName, false);
				}
				target.__rpc_exec_stage = (__RpcExecStage)1;
				((LethalFartsNetworkHandler)(object)target).EventServerRpc(eventName);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		private static void __rpc_handler_934466365(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				bool flag = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref flag, default(ForPrimitives));
				string eventName = null;
				if (flag)
				{
					((FastBufferReader)(ref reader)).ReadValueSafe(ref eventName, false);
				}
				target.__rpc_exec_stage = (__RpcExecStage)2;
				((LethalFartsNetworkHandler)(object)target).EventClientRpc(eventName);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		protected internal override string __getTypeName()
		{
			return "LethalFartsNetworkHandler";
		}
	}
	[HarmonyPatch]
	internal class LethalFartsNetworkObjectManager
	{
		private static GameObject networkPrefab;

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameNetworkManager), "Start")]
		private static void Init()
		{
			if (!((Object)(object)networkPrefab != (Object)null))
			{
				LethalFartsBase.Log("Loading assets");
				string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
				AssetBundle val = AssetBundle.LoadFromFile(Path.Combine(directoryName, "LethalFartsAssets"));
				networkPrefab = val.LoadAsset<GameObject>("LethalFartsNetworkHandler");
				networkPrefab.AddComponent<LethalFartsNetworkHandler>();
				LethalFartsBase.Log("Added component");
				NetworkManager.Singleton.AddNetworkPrefab(networkPrefab);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartOfRound), "Awake")]
		private static void SpawnNetworkHandler()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer)
			{
				GameObject val = Object.Instantiate<GameObject>(networkPrefab, Vector3.zero, Quaternion.identity);
				val.GetComponent<NetworkObject>().Spawn(false);
				LethalFartsBase.Log("Spawned Network Object");
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerFartPatch
	{
		private static float timeToFart;

		public static AudioClip[] farts;

		private static Dictionary<string, AudioSource> audioSourceMap;

		private static GameObject networkPrefab;

		private static GameObject networkHandlerHost;

		private static readonly string eventDelimiter = ":||:";

		private static bool forceFart = false;

		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		public static void Init()
		{
			if (farts == null)
			{
				ResetFartTimer();
				farts = (AudioClip[])(object)new AudioClip[21];
				audioSourceMap = new Dictionary<string, AudioSource>();
				string text = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).Split(new char[1] { '\\' })[^1];
				for (int i = 0; i < 21; i++)
				{
					string text2 = "Fart-" + (i + 1) + ".wav";
					farts[i] = SoundTool.GetAudioClip(text, "Fart_Sounds", text2);
				}
				LethalFartsBase.Log("Farts Initialized");
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(RoundManager), "GenerateNewFloor")]
		private static void SubscribeToHandler()
		{
			LethalFartsNetworkHandler.FartEvent += ReceivedEventFromServer;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(RoundManager), "DespawnPropsAtEndOfRound")]
		private static void UnsubscribeFromHandler()
		{
			LethalFartsNetworkHandler.FartEvent -= ReceivedEventFromServer;
		}

		private static void ReceivedEventFromServer(string eventName)
		{
			string[] array = eventName.Split(new string[1] { eventDelimiter }, StringSplitOptions.None);
			string playerName = array[1];
			int fartIndex = int.Parse(array[2]);
			PlayFartFromPlayer(playerName, fartIndex);
		}

		private static void SendEventToClients(string eventName)
		{
			LethalFartsNetworkHandler.Instance.EventServerRpc(eventName);
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void FartPatch(ref AudioSource ___movementAudio, ref string ___playerUsername, ref bool ___isCameraDisabled, ref bool ___isInHangarShipRoom, ref StartOfRound ___playersManager, ref Transform ___thisPlayerBody, ref bool ___isCrouching)
		{
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			if (!audioSourceMap.ContainsKey(___playerUsername))
			{
				audioSourceMap[___playerUsername] = ___movementAudio;
			}
			if (!___isCameraDisabled && !StartOfRound.Instance.inShipPhase && TimeOfDay.Instance.currentDayTimeStarted)
			{
				float num = (___isCrouching ? (Time.deltaTime * 2f) : Time.deltaTime);
				timeToFart -= num;
				if (timeToFart <= 0f || forceFart)
				{
					bool noiseIsInsideClosedShip = ___isInHangarShipRoom && ___playersManager.hangarDoorsClosed;
					Fart(___thisPlayerBody.position, noiseIsInsideClosedShip, ___playerUsername);
				}
			}
		}

		[HarmonyPatch("Jump_performed")]
		[HarmonyPostfix]
		private static void JumpPatch(ref bool ___isCameraDisabled)
		{
			if (!___isCameraDisabled && !StartOfRound.Instance.inShipPhase && TimeOfDay.Instance.currentDayTimeStarted)
			{
				float num = Random.Range(0, 100);
				if (num < 12f)
				{
					forceFart = true;
				}
			}
		}

		private static void Fart(Vector3 position, bool noiseIsInsideClosedShip, string playerName)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			if (audioSourceMap.ContainsKey(playerName))
			{
				int num = Random.Range(0, 21);
				RoundManager.Instance.PlayAudibleNoise(position, 22f, 0.6f, 0, noiseIsInsideClosedShip, 0);
				string eventName = "Fart" + eventDelimiter + playerName + eventDelimiter + num;
				SendEventToClients(eventName);
			}
			ResetFartTimer();
			forceFart = false;
		}

		private static void PlayFartFromPlayer(string playerName, int fartIndex)
		{
			if (audioSourceMap.ContainsKey(playerName))
			{
				LethalFartsBase.Log("Player: " + playerName + " Farting with fart: " + fartIndex);
				AudioClip val = farts[fartIndex];
				audioSourceMap[playerName].PlayOneShot(val);
			}
		}

		private static void ResetFartTimer()
		{
			timeToFart = Random.Range(120, 360);
		}
	}
}
namespace LethalFarts.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}