Decompiled source of Musical Shotgun v1.0.0

Smonglaren.MusicalShotgun.dll

Decompiled 3 weeks 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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Sirenix.Utilities;
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("Smonglaren.MusicalShotgun")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Plays a tune to clearly indicate that you shouldn't be handling the shotgun like that.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+50274266c8a900f27c81f59b44f2c4c32be97281")]
[assembly: AssemblyProduct("MusicalShotgun")]
[assembly: AssemblyTitle("Smonglaren.MusicalShotgun")]
[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 MusicalShotgun
{
	[BepInPlugin("Smonglaren.MusicalShotgun", "MusicalShotgun", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <LoadSound>d__8 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			private Uri <uri>5__2;

			private UnityWebRequest <request>5__3;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <LoadSound>d__8(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<uri>5__2 = null;
				<request>5__3 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_00b6: 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)
				//IL_00e6: Invalid comparison between Unknown and I4
				//IL_0110: Unknown result type (might be due to invalid IL or missing references)
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<uri>5__2 = new Uri(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), ConfigSoundfile.Value) ?? "");
					Logger.LogInfo((object)$"Trying to load audio file at {<uri>5__2}");
					<request>5__3 = UnityWebRequestMultimedia.GetAudioClip(<uri>5__2, (AudioType)13);
					<request>5__3.timeout = 1;
					<>2__current = <request>5__3.SendWebRequest();
					<>1__state = 1;
					return true;
				case 1:
					<>1__state = -1;
					Logger.LogInfo((object)$"Request finished with {<request>5__3.result} (code {<request>5__3.responseCode})");
					if ((int)<request>5__3.result == 1)
					{
						Audio = DownloadHandlerAudioClip.GetContent(<request>5__3);
					}
					else
					{
						Logger.LogError((object)$"Request for URI {<uri>5__2} returned {<request>5__3.result} (code {<request>5__3.responseCode})");
					}
					return false;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		internal static ManualLogSource Logger;

		internal static Harmony _Harmony;

		internal static ConfigEntry<string> ConfigSoundfile;

		internal static AudioClip Audio;

		internal static ConfigEntry<float> ConfigAudioStartThreshold;

		internal static ConfigEntry<bool> ConfigShotgunOnly;

		internal static ConfigEntry<bool> ConfigAdditionalDebugLogging;

		private void Awake()
		{
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Smonglaren.MusicalShotgun loaded.");
			ConfigSoundfile = ((BaseUnityPlugin)this).Config.Bind<string>("Configuration", "SoundFilename", "audio.mp3", "ONLY .MP3 FILES ARE SUPPORTED! Audio file name to use. Place in the same directory as the mod .dll");
			ConfigAudioStartThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("Configuration", "AudioStartThreshold", 0f, "How far away the weapon needs to turned (between 0 = facing foward and 1 = facing player) for the audio to start playing");
			ConfigShotgunOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("Configuration", "ShotgunOnly", true, "If true, will only apply to the shotgun. If false, will apply to all guns, including the tranquilizer and pulse pistol, among others.");
			ConfigAdditionalDebugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "AdditionalDebugLogging", false, "Enable extra debug logging. This floods the console with messages, and is most likely unnecessary if you aren't a developer.");
			((MonoBehaviour)this).StartCoroutine(LoadSound());
			_Harmony = new Harmony("Smonglaren.MusicalShotgun");
			_Harmony.PatchAll();
		}

		[IteratorStateMachine(typeof(<LoadSound>d__8))]
		public IEnumerator LoadSound()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <LoadSound>d__8(0);
		}
	}
	[Harmony]
	public class ItemGunPatch
	{
		[HarmonyPatch(typeof(ItemGun), "Start")]
		[HarmonyPostfix]
		private static void StartPostfix(ItemGun __instance)
		{
			if (!Plugin.ConfigShotgunOnly.Value || !(((Object)__instance).name != "Item Gun Shotgun"))
			{
				Plugin.Logger.LogDebug((object)__instance);
				((Component)__instance).gameObject.AddComponent<ItemGunMusicComponent>();
			}
		}
	}
	public class ItemGunMusicComponent : MonoBehaviour
	{
		private PhysGrabObject grabObject;

		private AudioSource audioPlayer;

		private bool isAudioPlaying;

		private void Start()
		{
			grabObject = ((Component)this).gameObject.GetComponent<PhysGrabObject>();
			if (!Object.op_Implicit((Object)(object)grabObject))
			{
				Plugin.Logger.LogWarning((object)"Fuh ohCK!!!! THERENS NO DAMN GRABOBJEXCT ON THIS THANG??? ABORT! ABORT!!!");
				((Behaviour)((Component)this).gameObject.GetComponent<ItemGunMusicComponent>()).enabled = false;
			}
			else if (!Object.op_Implicit((Object)(object)Plugin.Audio))
			{
				Plugin.Logger.LogWarning((object)"No audio loaded? Not fun k cc");
				((Behaviour)((Component)this).gameObject.GetComponent<ItemGunMusicComponent>()).enabled = false;
			}
			else
			{
				audioPlayer = ((Component)this).gameObject.AddComponent<AudioSource>();
				audioPlayer.clip = Plugin.Audio;
				audioPlayer.loop = true;
			}
		}

		private void Update()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			if (grabObject.grabbedLocal)
			{
				Quaternion rotation = ((Component)grabObject.playerGrabbing.Last()).transform.rotation;
				float y = MathUtilities.Abs(((Quaternion)(ref rotation)).eulerAngles).y;
				rotation = ((Component)this).transform.rotation;
				float num = y - MathUtilities.Abs(((Quaternion)(ref rotation)).eulerAngles).y;
				float num2 = 1f - Math.Abs((Math.Abs(num) - 180f) / 180f);
				if (Plugin.ConfigAdditionalDebugLogging.Value)
				{
					ManualLogSource logger = Plugin.Logger;
					object[] obj = new object[4] { num, num2, null, null };
					rotation = ((Component)grabObject.playerGrabbing.Last()).transform.rotation;
					obj[2] = MathUtilities.Abs(((Quaternion)(ref rotation)).eulerAngles).y;
					obj[3] = isAudioPlaying;
					logger.LogDebug((object)string.Format("DIFF: {0}, VOL: {1}, PLAYER: {2}, PLAYING {3}", obj));
				}
				if (!isAudioPlaying && num2 > Plugin.ConfigAudioStartThreshold.Value)
				{
					audioPlayer.Play();
					isAudioPlaying = true;
				}
				else if (isAudioPlaying && num2 < Plugin.ConfigAudioStartThreshold.Value)
				{
					audioPlayer.Stop();
					isAudioPlaying = false;
				}
				audioPlayer.volume = num2;
			}
			else if (isAudioPlaying)
			{
				isAudioPlaying = false;
				audioPlayer.Stop();
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Smonglaren.MusicalShotgun";

		public const string PLUGIN_NAME = "MusicalShotgun";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}