Decompiled source of SquiiishyDuck v1.0.0

SquiiishyDuck.dll

Decompiled 15 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("SquiiishyDuck")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SquiiishyDuck")]
[assembly: AssemblyTitle("SquiiishyDuck")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 SquiiishyDuck
{
	[BepInPlugin("local.squiiishyduck", "Squiiishy Duck", "1.0.0")]
	public class SquiiishyDuckPlugin : BaseUnityPlugin
	{
		private const string PluginGuid = "local.squiiishyduck";

		private const string PluginName = "Squiiishy Duck";

		private const string PluginVersion = "1.0.0";

		private const string EmbeddedWavResourceName = "squiiishy.wav";

		private static readonly string[] DuckSoundFieldNames = new string[15]
		{
			"quackSound", "stunSound", "stunStopSound", "biteSound", "transformSound", "jumpSound", "footstepSound", "mouthExtendSound", "mouthRetractSound", "attackLoopSound",
			"hurtSound", "deathSound", "noticeSound", "flyFlapSound", "flyLoopSound"
		};

		private AudioClip _customClip;

		private FieldInfo[] _duckSoundFields;

		private FieldInfo _soundClipsField;

		private FieldInfo _soundLoopClipField;

		private FieldInfo _soundInfoFetchedField;

		private readonly HashSet<int> _patchedInstanceIds = new HashSet<int>();

		private float _rescanTimer;

		private void Awake()
		{
			try
			{
				_customClip = LoadClipFromEmbeddedWav("squiiishy.wav", "SquiiishyClip");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("SquiiishyDuck: Konnte eingebettete WAV-Datei nicht laden: " + ex));
				return;
			}
			Type typeFromHandle = typeof(EnemyDuckAnim);
			_duckSoundFields = new FieldInfo[DuckSoundFieldNames.Length];
			for (int i = 0; i < DuckSoundFieldNames.Length; i++)
			{
				_duckSoundFields[i] = typeFromHandle.GetField(DuckSoundFieldNames[i], BindingFlags.Instance | BindingFlags.Public);
				if (_duckSoundFields[i] == null)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("SquiiishyDuck: Feld '" + DuckSoundFieldNames[i] + "' an EnemyDuckAnim nicht gefunden - Spielversion evtl. geändert."));
				}
			}
			Type typeFromHandle2 = typeof(Sound);
			_soundClipsField = typeFromHandle2.GetField("Sounds", BindingFlags.Instance | BindingFlags.Public);
			_soundLoopClipField = typeFromHandle2.GetField("LoopClip", BindingFlags.Instance | BindingFlags.NonPublic);
			_soundInfoFetchedField = typeFromHandle2.GetField("AudioInfoFetched", BindingFlags.Instance | BindingFlags.NonPublic);
			if (_soundClipsField == null || _soundLoopClipField == null || _soundInfoFetchedField == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"SquiiishyDuck: Erwartete Felder an Sound wurden nicht gefunden - die Spielversion hat sich vermutlich geändert und der Mod muss angepasst werden.");
			}
		}

		private void Update()
		{
			if ((Object)(object)_customClip == (Object)null || _soundClipsField == null)
			{
				return;
			}
			_rescanTimer -= Time.unscaledDeltaTime;
			if (_rescanTimer > 0f)
			{
				return;
			}
			_rescanTimer = 2f;
			EnemyDuckAnim[] array = Object.FindObjectsOfType<EnemyDuckAnim>();
			foreach (EnemyDuckAnim val in array)
			{
				if (!((Object)(object)val == (Object)null))
				{
					int instanceID = ((Object)val).GetInstanceID();
					if (!_patchedInstanceIds.Contains(instanceID))
					{
						PatchDuckSounds(val);
						_patchedInstanceIds.Add(instanceID);
					}
				}
			}
		}

		private void PatchDuckSounds(EnemyDuckAnim duckAnim)
		{
			AudioClip[] value = (AudioClip[])(object)new AudioClip[1] { _customClip };
			FieldInfo[] duckSoundFields = _duckSoundFields;
			foreach (FieldInfo fieldInfo in duckSoundFields)
			{
				if (!(fieldInfo == null))
				{
					object value2 = fieldInfo.GetValue(duckAnim);
					if (value2 != null)
					{
						_soundClipsField.SetValue(value2, value);
						_soundLoopClipField.SetValue(value2, _customClip);
						_soundInfoFetchedField.SetValue(value2, false);
					}
				}
			}
		}

		private static AudioClip LoadClipFromEmbeddedWav(string resourceName, string clipName)
		{
			using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
			if (stream == null)
			{
				throw new FileNotFoundException("Eingebettete Ressource nicht gefunden: " + resourceName);
			}
			using BinaryReader binaryReader = new BinaryReader(stream);
			if (new string(binaryReader.ReadChars(4)) != "RIFF")
			{
				throw new InvalidDataException("Keine gültige RIFF/WAV-Datei.");
			}
			binaryReader.ReadInt32();
			if (new string(binaryReader.ReadChars(4)) != "WAVE")
			{
				throw new InvalidDataException("Keine gültige WAVE-Datei.");
			}
			int num = 1;
			int num2 = 44100;
			int num3 = 16;
			byte[] array = null;
			while (stream.Position < stream.Length)
			{
				string text = new string(binaryReader.ReadChars(4));
				int num4 = binaryReader.ReadInt32();
				if (text == "fmt ")
				{
					binaryReader.ReadInt16();
					num = binaryReader.ReadInt16();
					num2 = binaryReader.ReadInt32();
					binaryReader.ReadInt32();
					binaryReader.ReadInt16();
					num3 = binaryReader.ReadInt16();
					int num5 = num4 - 16;
					if (num5 > 0)
					{
						binaryReader.ReadBytes(num5);
					}
				}
				else if (text == "data")
				{
					array = binaryReader.ReadBytes(num4);
				}
				else
				{
					binaryReader.ReadBytes(num4);
				}
			}
			if (array == null)
			{
				throw new InvalidDataException("Kein 'data'-Chunk in der WAV-Datei gefunden.");
			}
			if (num3 != 16)
			{
				throw new InvalidDataException("Nur 16-Bit-PCM-WAV wird unterstützt.");
			}
			int num6 = array.Length / 2;
			float[] array2 = new float[num6];
			for (int i = 0; i < num6; i++)
			{
				short num7 = BitConverter.ToInt16(array, i * 2);
				array2[i] = (float)num7 / 32768f;
			}
			AudioClip obj = AudioClip.Create(clipName, num6 / num, num, num2, false);
			obj.SetData(array2, 0);
			return obj;
		}
	}
}