Decompiled source of FartCompany v1.0.0

BepInEx/plugins/FartCompany.dll

Decompiled 14 hours 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.Configuration;
using BepInEx.Logging;
using FartCompany.Patches;
using HarmonyLib;
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: AssemblyTitle("FartCompany")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FartCompany")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5be38ec3-efcc-427e-88a8-13c6767d0589")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace FartCompany
{
	public class Config
	{
		public static ConfigEntry<float> volume;

		public static ConfigEntry<float> chance;

		public Config(ConfigFile configFile)
		{
			volume = configFile.Bind<float>("General", "volume", 1f, "Volume multiplier of the fart sound effects.");
			chance = configFile.Bind<float>("General", "chance", 10f, "The odds of a sound getting replaced with a fart in %.");
		}
	}
	[BepInPlugin("KamenVacuumCleaner.FartCompany", "FartCompany", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const string GUID = "KamenVacuumCleaner.FartCompany";

		private const string NAME = "FartCompany";

		private const string VERSION = "1.0.0";

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

		public static Plugin instance;

		private static string location;

		internal ManualLogSource mls;

		public static AudioClip[] shortFarts;

		public static AudioClip[] mediumFarts;

		public static AudioClip[] longFarts;

		private static int nextShort = -1;

		private static int nextMedium = -1;

		private static int nextLong = -1;

		private int loadingErrors = 0;

		public static Config config { get; internal set; }

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			mls = Logger.CreateLogSource("KamenVacuumCleaner.FartCompany");
			config = new Config(((BaseUnityPlugin)this).Config);
			location = ((BaseUnityPlugin)this).Info.Location.TrimEnd("FartCompany.dll".ToCharArray());
			Log("Loading FartCompany v1.0.0...");
			string path = location + "Farts/Short/";
			string path2 = location + "Farts/Medium/";
			string path3 = location + "Farts/Long/";
			string[] files = Directory.GetFiles(path);
			string[] files2 = Directory.GetFiles(path2);
			string[] files3 = Directory.GetFiles(path3);
			List<AudioClip> list = new List<AudioClip>();
			List<AudioClip> list2 = new List<AudioClip>();
			List<AudioClip> list3 = new List<AudioClip>();
			string[] array = files;
			foreach (string path4 in array)
			{
				AudioClip val = LoadAudio(path4, (AudioType)20);
				if ((Object)(object)val != (Object)null)
				{
					list.Add(val);
				}
			}
			shortFarts = list.ToArray();
			if (shortFarts.Length > 1)
			{
				nextShort = Random.Range(0, shortFarts.Length);
			}
			string[] array2 = files2;
			foreach (string path5 in array2)
			{
				AudioClip val2 = LoadAudio(path5, (AudioType)20);
				if ((Object)(object)val2 != (Object)null)
				{
					list2.Add(val2);
				}
			}
			mediumFarts = list2.ToArray();
			if (mediumFarts.Length > 1)
			{
				nextMedium = Random.Range(0, mediumFarts.Length);
			}
			string[] array3 = files3;
			foreach (string path6 in array3)
			{
				AudioClip val3 = LoadAudio(path6, (AudioType)20);
				if ((Object)(object)val3 != (Object)null)
				{
					list3.Add(val3);
				}
			}
			longFarts = list3.ToArray();
			if (longFarts.Length > 1)
			{
				nextLong = Random.Range(0, longFarts.Length);
			}
			harmony.PatchAll(typeof(AudioSourcePatch));
			Log(string.Format("{0} v{1} has been loaded up with {2} errors!", "FartCompany", "1.0.0", loadingErrors));
		}

		private AudioClip LoadAudio(string path, AudioType type)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Invalid comparison between Unknown and I4
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			AudioClip result = null;
			UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip("File://" + path, type);
			audioClip.SendWebRequest();
			while (!audioClip.isDone)
			{
			}
			if ((int)audioClip.result == 1)
			{
				result = DownloadHandlerAudioClip.GetContent(audioClip);
				Log("Successfully loaded sound: " + path.TrimStart(location.ToCharArray()));
			}
			else
			{
				loadingErrors++;
				LogError($"Could not load audio file of type {type} in {path}");
			}
			return result;
		}

		public static void Log(string message)
		{
			instance.mls.LogInfo((object)message);
		}

		public static void LogError(string message)
		{
			instance.mls.LogError((object)message);
		}

		public static AudioClip GetShortFart()
		{
			if (shortFarts.Length == 0 || nextShort < 0)
			{
				return null;
			}
			int num = nextShort;
			while (shortFarts.Length > 1 && num == nextShort)
			{
				nextShort = Random.Range(0, shortFarts.Length);
			}
			return shortFarts[num];
		}

		public static AudioClip GetMediumFart()
		{
			if (mediumFarts.Length == 0 || nextMedium < 0)
			{
				return null;
			}
			int num = nextMedium;
			while (mediumFarts.Length > 1 && num == nextMedium)
			{
				nextMedium = Random.Range(0, mediumFarts.Length);
			}
			return mediumFarts[num];
		}

		public static AudioClip GetLongFart()
		{
			if (longFarts.Length == 0 || nextLong < 0)
			{
				return null;
			}
			int num = nextLong;
			while (longFarts.Length > 1 && num == nextLong)
			{
				nextLong = Random.Range(0, longFarts.Length);
			}
			return longFarts[num];
		}
	}
}
namespace FartCompany.Patches
{
	[HarmonyPatch(typeof(AudioSource))]
	internal static class AudioSourcePatch
	{
		[HarmonyPatch("Play", new Type[] { })]
		[HarmonyPrefix]
		public static void PlayPatch(AudioSource __instance)
		{
			ProcessSound(ref __instance);
		}

		[HarmonyPatch("Play", new Type[] { typeof(ulong) })]
		[HarmonyPrefix]
		public static void PlayPatchUlong(AudioSource __instance)
		{
			ProcessSound(ref __instance);
		}

		[HarmonyPatch("Play", new Type[] { typeof(double) })]
		[HarmonyPrefix]
		public static void PlayPatchDouble(AudioSource __instance)
		{
			ProcessSound(ref __instance);
		}

		[HarmonyPatch("PlayDelayed", new Type[] { typeof(float) })]
		[HarmonyPrefix]
		public static void PlayDelayedPatch(AudioSource __instance)
		{
			ProcessSound(ref __instance);
		}

		[HarmonyPatch("PlayClipAtPoint", new Type[]
		{
			typeof(AudioClip),
			typeof(Vector3),
			typeof(float)
		})]
		[HarmonyPrefix]
		public static void PlayClipAtPointPatch(AudioClip clip, Vector3 position, float volume)
		{
			clip = ProcessClip(clip);
		}

		[HarmonyPatch("PlayOneShotHelper", new Type[]
		{
			typeof(AudioSource),
			typeof(AudioClip),
			typeof(float)
		})]
		[HarmonyPrefix]
		public static void PlayOneShotHelperPatch(AudioSource source, ref AudioClip clip, float volumeScale)
		{
			clip = ProcessClip(clip);
		}

		private static void ProcessSound(ref AudioSource source)
		{
			source.clip = ProcessClip(source.clip);
		}

		private static AudioClip ProcessClip(AudioClip clip)
		{
			if (clip.length > 6f || Random.Range(0f, 1f) >= Config.chance.Value * 0.01f)
			{
				return clip;
			}
			if (clip.length >= 4f)
			{
				return Plugin.GetLongFart();
			}
			if (clip.length >= 2f)
			{
				return Plugin.GetMediumFart();
			}
			return Plugin.GetShortFart();
		}
	}
}