Decompiled source of GermanNutcrackerWithLoudShotgunFixes v1.0.0

GerNutLoudShotgunFixes.dll

Decompiled an hour ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
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("GerNutLoudShotgunFixes")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("GerNutLoudShotgunFixes")]
[assembly: AssemblyTitle("GerNutLoudShotgunFixes")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace GerNutLoudShotgunFixes
{
	[BepInPlugin("gungabunga.gernutfix", "German Nutcracker Loud Shotgun Fixes", "1.0.0")]
	public class GermanNutFix : BaseUnityPlugin
	{
		[HarmonyPatch]
		private static class NutcrackerAimGunPatch
		{
			private static MethodBase TargetMethod()
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(NutcrackerEnemyAI), "AimGun", (Type[])null, (Type[])null);
				return AccessTools.EnumeratorMoveNext((MethodBase)methodInfo);
			}

			private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
			{
				List<CodeInstruction> list = new List<CodeInstruction>(instructions);
				MethodInfo playOneShotMethod = GetPlayOneShotMethod();
				MethodInfo operand = AccessTools.Method(typeof(GermanNutFix), "PlayReplacementAlert", (Type[])null, (Type[])null);
				int num = 0;
				foreach (CodeInstruction item in list)
				{
					if (CodeInstructionExtensions.Calls(item, playOneShotMethod))
					{
						item.opcode = OpCodes.Call;
						item.operand = operand;
						num++;
					}
				}
				if (num != 1)
				{
					((BaseUnityPlugin)Instance).Logger.LogError((object)("AimGun patch expected 1 sound call but found " + num));
					return instructions;
				}
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)"GERNUTFIX: AimGun sound patched.");
				return list;
			}
		}

		private Harmony harmony = null;

		private static GermanNutFix Instance;

		private AudioClip[] alertedClips = (AudioClip[])(object)new AudioClip[3];

		private int lastPlayedClipIndex = -1;

		private void Awake()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			Instance = this;
			harmony = new Harmony("gungabunga.gernutfix");
			harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"German Nutcracker Loud Shotgun Fixes loaded!");
			((MonoBehaviour)this).StartCoroutine(LoadAlertedSounds());
		}

		private IEnumerator LoadAlertedSounds()
		{
			string soundFolder = Path.Combine(Paths.PluginPath, "CeskeRucicky-German_Nutcracker_with_loud_shotgun", "CustomSounds");
			string[] files = new string[3] { "AlertedSFX-31.ogg", "AlertedSFX-33.ogg", "AlertedSFX-36.ogg" };
			for (int i = 0; i < files.Length; i++)
			{
				string fullPath = Path.Combine(soundFolder, files[i]);
				if (!File.Exists(fullPath))
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("Alerted sound file not found: " + fullPath));
					continue;
				}
				string uri = new Uri(fullPath).AbsoluteUri;
				UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(uri, (AudioType)14);
				try
				{
					yield return request.SendWebRequest();
					if ((int)request.result != 1)
					{
						((BaseUnityPlugin)this).Logger.LogError((object)("Failed to load " + files[i] + ": " + request.error));
						continue;
					}
					alertedClips[i] = DownloadHandlerAudioClip.GetContent(request);
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Loaded alerted sound: " + files[i]));
				}
				finally
				{
					((IDisposable)request)?.Dispose();
				}
			}
			int loadedCount = 0;
			AudioClip[] array = alertedClips;
			foreach (AudioClip clip in array)
			{
				if ((Object)(object)clip != (Object)null)
				{
					loadedCount++;
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Alerted sound loading finished. Loaded " + loadedCount + " of 3 clips."));
		}

		private static MethodInfo GetPlayOneShotMethod()
		{
			return AccessTools.Method(typeof(AudioSource), "PlayOneShot", new Type[1] { typeof(AudioClip) }, (Type[])null);
		}

		public static void PlayReplacementAlert(AudioSource source, AudioClip originalClip)
		{
			AudioClip[] array = Instance.alertedClips;
			int num = 0;
			AudioClip[] array2 = array;
			foreach (AudioClip val in array2)
			{
				if ((Object)(object)val != (Object)null)
				{
					num++;
				}
			}
			switch (num)
			{
			case 0:
				source.PlayOneShot(originalClip);
				return;
			case 1:
			{
				AudioClip[] array3 = array;
				foreach (AudioClip val2 in array3)
				{
					if ((Object)(object)val2 != (Object)null)
					{
						source.PlayOneShot(val2);
						return;
					}
				}
				break;
			}
			}
			int num2 = 0;
			for (int k = 0; k < array.Length; k++)
			{
				if ((Object)(object)array[k] != (Object)null && k != Instance.lastPlayedClipIndex)
				{
					num2++;
				}
			}
			int num3 = Random.Range(0, num2);
			for (int l = 0; l < array.Length; l++)
			{
				if (!((Object)(object)array[l] == (Object)null) && l != Instance.lastPlayedClipIndex)
				{
					if (num3 == 0)
					{
						Instance.lastPlayedClipIndex = l;
						source.PlayOneShot(array[l]);
						break;
					}
					num3--;
				}
			}
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			AudioClip[] array = alertedClips;
			foreach (AudioClip val in array)
			{
				if ((Object)(object)val != (Object)null)
				{
					Object.Destroy((Object)(object)val);
				}
			}
			Instance = null;
		}
	}
}