Decompiled source of ShakeALL v1.0.0

BepInEx/plugins/DuckSqueak.dll

Decompiled 25 minutes ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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 GameNetcodeStuff;
using Microsoft.CodeAnalysis;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;

[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: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[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 DuckSqueak
{
	internal static class DuckNetwork
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static HandleNamedMessageDelegate <0>__OnMessage;
		}

		private const string MessageName = "PRTS_DuckSqueak";

		private static bool _registered;

		public static void Tick()
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			if (_registered)
			{
				return;
			}
			NetworkManager singleton = NetworkManager.Singleton;
			if ((Object)(object)singleton == (Object)null || !singleton.IsListening || singleton.CustomMessagingManager == null)
			{
				return;
			}
			try
			{
				CustomMessagingManager customMessagingManager = singleton.CustomMessagingManager;
				object obj = <>O.<0>__OnMessage;
				if (obj == null)
				{
					HandleNamedMessageDelegate val = OnMessage;
					<>O.<0>__OnMessage = val;
					obj = (object)val;
				}
				customMessagingManager.RegisterNamedMessageHandler("PRTS_DuckSqueak", (HandleNamedMessageDelegate)obj);
				_registered = true;
				DuckSqueakPlugin.Log.LogInfo((object)"网络通道已注册");
			}
			catch (Exception ex)
			{
				DuckSqueakPlugin.Log.LogWarning((object)("注册网络通道失败:" + ex.Message));
			}
		}

		public unsafe static void Broadcast(ulong senderId, float pitch)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager singleton = NetworkManager.Singleton;
			if ((Object)(object)singleton == (Object)null || !singleton.IsListening || singleton.CustomMessagingManager == null)
			{
				return;
			}
			try
			{
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(16, (Allocator)2, -1);
				try
				{
					((FastBufferWriter)(ref val)).WriteValueSafe<ulong>(ref senderId, default(ForPrimitives));
					((FastBufferWriter)(ref val)).WriteValueSafe<float>(ref pitch, default(ForPrimitives));
					singleton.CustomMessagingManager.SendNamedMessageToAll("PRTS_DuckSqueak", val, (NetworkDelivery)3);
				}
				finally
				{
					((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
				}
			}
			catch (Exception ex)
			{
				DuckSqueakPlugin.Log.LogWarning((object)("发送网络消息失败:" + ex.Message));
			}
		}

		private static void OnMessage(ulong senderClientId, FastBufferReader reader)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				ulong num = default(ulong);
				((FastBufferReader)(ref reader)).ReadValueSafe<ulong>(ref num, default(ForPrimitives));
				float pitch = default(float);
				((FastBufferReader)(ref reader)).ReadValueSafe<float>(ref pitch, default(ForPrimitives));
				if (num == GetLocalClientId())
				{
					return;
				}
				GrabbableObject val = FindPlayerItem(num);
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				AudioClip val2 = DuckSqueakBehaviour.PickVanillaClip(val);
				if (!((Object)(object)val2 == (Object)null))
				{
					AudioSource val3 = ((Component)val).GetComponent<AudioSource>();
					if ((Object)(object)val3 == (Object)null)
					{
						val3 = ((Component)val).gameObject.AddComponent<AudioSource>();
						val3.playOnAwake = false;
						val3.spatialBlend = 1f;
						val3.minDistance = 1f;
						val3.maxDistance = 45f;
						val3.rolloffMode = (AudioRolloffMode)1;
					}
					val3.pitch = pitch;
					val3.PlayOneShot(val2, 0.9f);
				}
			}
			catch (Exception ex)
			{
				DuckSqueakPlugin.Log.LogWarning((object)("处理网络消息失败:" + ex.Message));
			}
		}

		private static ulong GetLocalClientId()
		{
			try
			{
				StartOfRound instance = StartOfRound.Instance;
				PlayerControllerB val = (((Object)(object)instance != (Object)null) ? instance.localPlayerController : null);
				return ((Object)(object)val != (Object)null) ? val.actualClientId : 0;
			}
			catch
			{
				return 0uL;
			}
		}

		private static GrabbableObject FindPlayerItem(ulong clientId)
		{
			try
			{
				StartOfRound instance = StartOfRound.Instance;
				if ((Object)(object)instance == (Object)null || instance.allPlayerScripts == null)
				{
					return null;
				}
				for (int i = 0; i < instance.allPlayerScripts.Length; i++)
				{
					PlayerControllerB val = instance.allPlayerScripts[i];
					if (!((Object)(object)val == (Object)null) && val.actualClientId == clientId)
					{
						GrabbableObject currentlyHeldObjectServer = val.currentlyHeldObjectServer;
						if ((Object)(object)currentlyHeldObjectServer != (Object)null && (Object)(object)currentlyHeldObjectServer.itemProperties != (Object)null)
						{
							return currentlyHeldObjectServer;
						}
					}
				}
			}
			catch
			{
			}
			return null;
		}
	}
	public class DuckSqueakBehaviour : MonoBehaviour
	{
		private float _lastPlay = -999f;

		private bool _loggedCursor;

		private static bool _legacyInputChecked;

		private static bool _legacyInputBroken;

		private void Update()
		{
			DuckNetwork.Tick();
			if (!DuckSqueakPlugin.Enabled.Value || !LeftClickDown() || ((Object)(object)EventSystem.current != (Object)null && EventSystem.current.IsPointerOverGameObject()))
			{
				return;
			}
			StartOfRound instance = StartOfRound.Instance;
			PlayerControllerB val = (((Object)(object)instance != (Object)null) ? instance.localPlayerController : null);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			GrabbableObject currentlyHeldObjectServer = val.currentlyHeldObjectServer;
			if ((Object)(object)currentlyHeldObjectServer == (Object)null || (Object)(object)currentlyHeldObjectServer.itemProperties == (Object)null)
			{
				return;
			}
			string text = currentlyHeldObjectServer.itemProperties.itemName ?? "";
			if (text.Length == 0)
			{
				return;
			}
			if (!_loggedCursor)
			{
				_loggedCursor = true;
				DuckSqueakPlugin.Log.LogInfo((object)("左键检测正常,手持:" + text));
			}
			if (!Matches(text))
			{
				NoteOnce("nomatch:" + text, "未匹配关键词:" + text + "(当前: " + DuckSqueakPlugin.Keywords.Value + ")");
			}
			else
			{
				if (Time.unscaledTime - _lastPlay < DuckSqueakPlugin.Cooldown.Value)
				{
					return;
				}
				_lastPlay = Time.unscaledTime;
				try
				{
					NoisemakerProp component = ((Component)currentlyHeldObjectServer).GetComponent<NoisemakerProp>();
					if ((Object)(object)component != (Object)null)
					{
						((GrabbableObject)component).ItemActivate(false, false);
						NoteOnce("via:nm", "播放方式:NoisemakerProp");
					}
					else
					{
						PlayVanilla(currentlyHeldObjectServer, text, val.actualClientId);
					}
				}
				catch (Exception ex)
				{
					DuckSqueakPlugin.Log.LogWarning((object)("播放失败:" + ex));
				}
			}
		}

		private static bool LeftClickDown()
		{
			try
			{
				Mouse current = Mouse.current;
				if (current != null)
				{
					return current.leftButton.wasPressedThisFrame;
				}
			}
			catch
			{
			}
			if (_legacyInputBroken)
			{
				return false;
			}
			if (!_legacyInputChecked)
			{
				_legacyInputChecked = true;
				try
				{
					Input.GetMouseButton(0);
				}
				catch
				{
					_legacyInputBroken = true;
					DuckSqueakPlugin.Log.LogInfo((object)"旧版 Input 不可用(游戏使用 Input System),已禁用该通道");
					return false;
				}
			}
			try
			{
				return Input.GetMouseButtonDown(0);
			}
			catch
			{
				_legacyInputBroken = true;
				return false;
			}
		}

		private static bool Matches(string itemName)
		{
			string value = DuckSqueakPlugin.Keywords.Value;
			if (string.IsNullOrEmpty(value))
			{
				return false;
			}
			string[] array = value.Split(',');
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				if (text.Length != 0 && itemName.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return true;
				}
			}
			return false;
		}

		public static AudioClip PickVanillaClip(GrabbableObject item)
		{
			AudioSource source;
			AudioClip val = PickFromComponents(item, out source);
			if ((Object)(object)val != (Object)null)
			{
				return val;
			}
			return PickFromProperties(item);
		}

		private static void PlayVanilla(GrabbableObject item, string itemName, ulong senderId)
		{
			float pitch = (DuckSqueakPlugin.RandomPitch.Value ? Random.Range(DuckSqueakPlugin.MinPitch.Value, DuckSqueakPlugin.MaxPitch.Value) : 1f);
			AudioSource source = null;
			AudioClip val = PickFromComponents(item, out source);
			if ((Object)(object)val != (Object)null)
			{
				AudioSource val2 = (((Object)(object)source != (Object)null) ? source : EnsureSource(item));
				val2.pitch = pitch;
				val2.PlayOneShot(val, 0.9f);
				NoteOnce("clip:own:" + ((Object)val).name, "原版音效(组件):" + ((Object)val).name);
				DuckNetwork.Broadcast(senderId, pitch);
				return;
			}
			AudioClip val3 = PickFromProperties(item);
			if ((Object)(object)val3 != (Object)null)
			{
				AudioSource val4 = EnsureSource(item);
				val4.pitch = pitch;
				val4.PlayOneShot(val3, 0.9f);
				NoteOnce("clip:props:" + ((Object)val3).name, "原版音效(属性):" + ((Object)val3).name);
				DuckNetwork.Broadcast(senderId, pitch);
			}
			else
			{
				NoteOnce("noclip:" + itemName, "该物品没有任何原版音效:" + itemName);
			}
		}

		private static AudioClip PickFromComponents(GrabbableObject item, out AudioSource source)
		{
			source = null;
			AudioSource[] componentsInChildren = ((Component)item).GetComponentsInChildren<AudioSource>(true);
			if (componentsInChildren == null)
			{
				return null;
			}
			AudioClip val = null;
			foreach (AudioSource val2 in componentsInChildren)
			{
				if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val2.clip == (Object)null))
				{
					string text = ((Object)val2.clip).name.ToLowerInvariant();
					if (text.Contains("quack") || text.Contains("squeak") || text.Contains("duck"))
					{
						source = val2;
						return val2.clip;
					}
					if ((Object)(object)val == (Object)null)
					{
						val = val2.clip;
						source = val2;
					}
				}
			}
			return val;
		}

		private static AudioClip PickFromProperties(GrabbableObject item)
		{
			Item itemProperties = item.itemProperties;
			if ((Object)(object)itemProperties.grabSFX != (Object)null)
			{
				return itemProperties.grabSFX;
			}
			if ((Object)(object)itemProperties.dropSFX != (Object)null)
			{
				return itemProperties.dropSFX;
			}
			if ((Object)(object)itemProperties.pocketSFX != (Object)null)
			{
				return itemProperties.pocketSFX;
			}
			if ((Object)(object)itemProperties.throwSFX != (Object)null)
			{
				return itemProperties.throwSFX;
			}
			if (itemProperties.clinkAudios != null && itemProperties.clinkAudios.Length != 0)
			{
				return itemProperties.clinkAudios[0];
			}
			return null;
		}

		private static AudioSource EnsureSource(GrabbableObject item)
		{
			AudioSource val = ((Component)item).GetComponent<AudioSource>();
			if ((Object)(object)val == (Object)null)
			{
				val = ((Component)item).gameObject.AddComponent<AudioSource>();
				val.playOnAwake = false;
				val.spatialBlend = 1f;
				val.minDistance = 1f;
				val.maxDistance = 45f;
				val.rolloffMode = (AudioRolloffMode)1;
			}
			return val;
		}

		private static void NoteOnce(string key, string message)
		{
			if (DuckSqueakPlugin.Notes.Add(key))
			{
				DuckSqueakPlugin.Log.LogInfo((object)message);
			}
		}
	}
	[BepInPlugin("com.prz.ducksqueak", "Duck Squeak", "1.0.2")]
	public class DuckSqueakPlugin : BaseUnityPlugin
	{
		public const string Guid = "com.prz.ducksqueak";

		internal static ManualLogSource Log;

		internal static readonly HashSet<string> Notes = new HashSet<string>();

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<float> Cooldown;

		internal static ConfigEntry<float> MinPitch;

		internal static ConfigEntry<float> MaxPitch;

		internal static ConfigEntry<string> Keywords;

		internal static ConfigEntry<bool> UseFallbackClip;

		internal static ConfigEntry<bool> RandomPitch;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "是否启用鸭子叫声");
			Cooldown = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Cooldown", 0.35f, "两次叫声最小间隔(秒)");
			Keywords = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Keywords", "duck,鸭,ducky,橡皮,橡胶", "物品名包含这些关键词时才触发,用英文逗号分隔(不区分大小写)");
			UseFallbackClip = ((BaseUnityPlugin)this).Config.Bind<bool>("Sound", "UseFallbackClip", true, "物品不是 NoisemakerProp 时,用它的抓取音效兜底播放");
			RandomPitch = ((BaseUnityPlugin)this).Config.Bind<bool>("Sound", "RandomPitch", false, "是否随机音高(false = 始终原始音高 1.0)");
			MinPitch = ((BaseUnityPlugin)this).Config.Bind<float>("Sound", "MinPitch", 1.15f, "随机音高时的最低值(RandomPitch=true 才生效)");
			MaxPitch = ((BaseUnityPlugin)this).Config.Bind<float>("Sound", "MaxPitch", 1.45f, "随机音高时的最高值(RandomPitch=true 才生效)");
			((Component)this).gameObject.AddComponent<DuckSqueakBehaviour>();
			Log.LogInfo((object)"Duck Squeak 已加载");
		}
	}
}