Decompiled source of FungusFart v1.0.0

FungusFart.dll

Decompiled 2 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 ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.Networking;

[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("FungusFart")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FungusFart")]
[assembly: AssemblyTitle("FungusFart")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 FungusFart
{
	public class FartSync : MonoBehaviourPun, IOnEventCallback
	{
		public void OnEnable()
		{
			PhotonNetwork.AddCallbackTarget((object)this);
		}

		public void OnDisable()
		{
			PhotonNetwork.RemoveCallbackTarget((object)this);
		}

		public void OnEvent(EventData photonEvent)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: 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)
			if (photonEvent.Code == Plugin.ModEventCodes.FartSoundEvent)
			{
				object customData = photonEvent.CustomData;
				if (customData is Vector3)
				{
					Vector3 position = (Vector3)customData;
					Plugin.PlayFartSound(position);
				}
			}
		}
	}
	[HarmonyPatch(typeof(ItemImpactSFX), "OnCollisionEnter")]
	public static class PatchItemImpactSFX
	{
		private static bool Prefix(ItemImpactSFX __instance)
		{
			if (((Object)((Component)__instance).gameObject).name.Contains("HealingPuffShroom"))
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(SFX_PlayOneShot), "Play")]
	public static class PatchSFXPlayOneShot
	{
		private static bool Prefix(SFX_PlayOneShot __instance)
		{
			Transform val = ((Component)__instance).transform;
			while ((Object)(object)val != (Object)null)
			{
				if (((Object)((Component)val).gameObject).name.Contains("HealingPuffShroomSpawn"))
				{
					return false;
				}
				val = val.parent;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ShelfShroom), "Break")]
	public static class PatchShelfShroomBreak
	{
		private static void Prefix(ShelfShroom __instance, ref bool __state)
		{
			__state = Traverse.Create((object)__instance).Field<bool>("alreadyBroke").Value;
		}

		private static void Postfix(ShelfShroom __instance, bool __state)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!__state && ((Object)((Component)__instance).gameObject).name.Contains("HealingPuffShroom") && !((Object)(object)Plugin.FartSound == (Object)null) && Plugin.FartSound.clips != null && Plugin.FartSound.clips.Length != 0)
				{
					Plugin.RaiseFartSoundEvent(((Component)__instance).transform.position);
				}
			}
			catch (Exception arg)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogError((object)$"FungusFart patch error: {arg}");
				}
			}
		}
	}
	public class ModConfig
	{
		public readonly ConfigEntry<float> SFXVolume;

		public readonly ConfigEntry<float> SFXRange;

		public readonly ConfigEntry<float> SFXSpatialBlend;

		public ModConfig(ConfigFile cfg)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Expected O, but got Unknown
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected O, but got Unknown
			cfg.SaveOnConfigSet = false;
			SFXVolume = cfg.Bind<float>("General", "Fart Volume", 1f, new ConfigDescription("Volume of the fart sound.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			SFXRange = cfg.Bind<float>("General", "Fart Sound Range", 100f, new ConfigDescription("Maximum distance the fart sound can be heard from.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 200f), Array.Empty<object>()));
			SFXSpatialBlend = cfg.Bind<float>("General", "Spatial Blend", 0.8f, new ConfigDescription("How 3D the sound is. 0 = heard equally everywhere, 1 = full positional audio.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			ClearOrphanedEntries(cfg);
			cfg.Save();
			cfg.SaveOnConfigSet = true;
		}

		private static void ClearOrphanedEntries(ConfigFile cfg)
		{
			((Dictionary<ConfigDefinition, string>)AccessTools.Property(typeof(ConfigFile), "OrphanedEntries").GetValue(cfg)).Clear();
		}
	}
	[BepInPlugin("FungusFart", "FungusFart", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static class ModEventCodes
		{
			internal static readonly byte FartSoundEvent = 80;
		}

		public const string Id = "FungusFart";

		internal static SFX_Instance FartSound;

		internal static FartSync syncInstance;

		private readonly Harmony harmony = new Harmony("FungusFart");

		private const int SFXLimit = 10;

		internal static ManualLogSource Log { get; private set; }

		internal static Plugin Instance { get; private set; }

		internal static ModConfig BoundConfig { get; private set; }

		private void Awake()
		{
			if ((Object)(object)Instance != (Object)null && (Object)(object)Instance != (Object)(object)this)
			{
				Object.Destroy((Object)(object)this);
				return;
			}
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			BoundConfig = new ModConfig(((BaseUnityPlugin)this).Config);
			GetSyncInstance();
			LoadFartSound();
			harmony.PatchAll();
			Log.LogInfo((object)"FungusFart loaded.");
		}

		internal static void PlayFartSound(Vector3 position)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)FartSound == (Object)null))
			{
				FartSound.settings.volume = BoundConfig.SFXVolume.Value;
				FartSound.settings.range = BoundConfig.SFXRange.Value;
				FartSound.settings.spatialBlend = BoundConfig.SFXSpatialBlend.Value;
				FartSound.Play(position);
			}
		}

		internal static void RaiseFartSoundEvent(Vector3 position)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			if (PhotonNetwork.OfflineMode)
			{
				PlayFartSound(position);
				return;
			}
			PhotonNetwork.RaiseEvent(ModEventCodes.FartSoundEvent, (object)position, new RaiseEventOptions
			{
				Receivers = (ReceiverGroup)1
			}, SendOptions.SendReliable);
		}

		internal static FartSync GetSyncInstance()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)syncInstance != (Object)null)
			{
				return syncInstance;
			}
			syncInstance = Object.FindFirstObjectByType<FartSync>();
			if ((Object)(object)syncInstance == (Object)null)
			{
				syncInstance = new GameObject("FartSync Listener Instance").AddComponent<FartSync>();
				Object.DontDestroyOnLoad((Object)(object)((Component)syncInstance).gameObject);
				ManualLogSource log = Log;
				if (log != null)
				{
					log.LogInfo((object)"Created FartSync listener instance.");
				}
			}
			return syncInstance;
		}

		private void LoadFartSound()
		{
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Expected O, but got Unknown
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			List<string> list = Directory.GetFiles(directoryName, "*.wav").Take(10).ToList();
			if (list.Count == 0)
			{
				Log.LogWarning((object)("No .wav files found in " + directoryName));
				return;
			}
			List<AudioClip> list2 = new List<AudioClip>();
			foreach (string item in list)
			{
				try
				{
					AudioClip val = LoadAudioClip(item);
					if ((Object)(object)val != (Object)null)
					{
						list2.Add(val);
					}
				}
				catch (Exception ex)
				{
					Log.LogError((object)("Failed to load sound file " + item + ": " + ex.Message));
				}
			}
			if (list2.Count == 0)
			{
				Log.LogError((object)"No fart audio clips were loaded.");
				return;
			}
			FartSound = ScriptableObject.CreateInstance<SFX_Instance>();
			FartSound.clips = list2.ToArray();
			FartSound.settings = new SFX_Settings
			{
				volume = BoundConfig.SFXVolume.Value,
				range = BoundConfig.SFXRange.Value,
				spatialBlend = BoundConfig.SFXSpatialBlend.Value
			};
			Log.LogInfo((object)$"Created fart SFX with {list2.Count} clip(s).");
		}

		private AudioClip LoadAudioClip(string filePath)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Invalid comparison between Unknown and I4
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Invalid comparison between Unknown and I4
			UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip(new Uri(filePath).AbsoluteUri, (AudioType)20);
			try
			{
				UnityWebRequestAsyncOperation val = audioClip.SendWebRequest();
				while (!((AsyncOperation)val).isDone)
				{
				}
				if ((int)audioClip.result == 2 || (int)audioClip.result == 3)
				{
					Log.LogError((object)("Error loading " + filePath + ": " + audioClip.error));
					return null;
				}
				return DownloadHandlerAudioClip.GetContent(audioClip);
			}
			finally
			{
				((IDisposable)audioClip)?.Dispose();
			}
		}
	}
}