Decompiled source of ClownShoes v1.0.0

BepInEx/plugins/ClownShoes/ClownShoes.dll

Decompiled 11 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("ClownShoes")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+09118b95a404c421d0105d201e4bc586ff8961a1")]
[assembly: AssemblyProduct("ClownShoes")]
[assembly: AssemblyTitle("ClownShoes")]
[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.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;
		}
	}
	[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 ClownShoes
{
	[HarmonyPatch(typeof(EnemyParent), "Setup")]
	internal static class EnemyParentSetupPatch
	{
		private static void Postfix(string ___enemyName, Enemy ___Enemy)
		{
			string text = ___enemyName ?? "";
			if (Plugin.Debug.Value)
			{
				Plugin.Log.LogInfo((object)("Enemy setup: '" + text + "'"));
			}
			if (text.IndexOf(Plugin.ClownName.Value, StringComparison.OrdinalIgnoreCase) < 0)
			{
				return;
			}
			if ((Object)(object)___Enemy == (Object)null)
			{
				Plugin.Log.LogWarning((object)("'" + text + "' matched but its Enemy reference was null; no squeaker attached."));
				return;
			}
			GameObject gameObject = ((Component)___Enemy).gameObject;
			if (!((Object)(object)gameObject.GetComponent<Squeaker>() != (Object)null))
			{
				gameObject.AddComponent<Squeaker>().Init(___Enemy, text);
				Plugin.Log.LogInfo((object)("Attached squeaker to '" + text + "'."));
			}
		}
	}
	[BepInPlugin("ClownShoes", "ClownShoes", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<float> Volume;

		internal static ConfigEntry<float> StepDistance;

		internal static ConfigEntry<float> MinWalkSpeed;

		internal static ConfigEntry<float> PitchVariation;

		internal static ConfigEntry<float> MaxHearingDistance;

		internal static ConfigEntry<bool> HostOnly;

		internal static ConfigEntry<string> ClownName;

		internal static ConfigEntry<bool> Debug;

		private void Awake()
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Expected O, but got Unknown
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Expected O, but got Unknown
			//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Volume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Volume", 0.35f, new ConfigDescription("Loudness of each shoe squeak.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			StepDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "StepDistance", 1.8f, new ConfigDescription("Distance (in meters) the clown walks between squeaks. Smaller = squeakier.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 5f), Array.Empty<object>()));
			MinWalkSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MinWalkSpeed", 0.4f, new ConfigDescription("Minimum horizontal speed (m/s) before squeaks start, so a standing-still clown stays quiet and tiny network jitter is ignored.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 5f), Array.Empty<object>()));
			PitchVariation = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PitchVariation", 0.2f, new ConfigDescription("Random pitch wobble per squeak (0 = every squeak identical).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 0.9f), Array.Empty<object>()));
			MaxHearingDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MaxHearingDistance", 25f, new ConfigDescription("How far away (meters) the squeaks can be heard.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(2f, 100f), Array.Empty<object>()));
			HostOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HostOnly", false, "If true, only the host (or singleplayer) hears the squeaks. If false, every player who has the mod hears the clowns near them.");
			ClownName = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ClownName", "Clown", "Case-insensitive substring matched against each enemy's name to decide which enemy gets squeaky shoes. With Debug on, every enemy name is logged so you can find the right value.");
			Debug = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Debug", false, "Log every enemy's name at spawn and each squeak. Turn on to find the right ClownName.");
			SqueakClip.Build();
			new Harmony("ClownShoes").PatchAll();
			Log.LogInfo((object)"ClownShoes 1.0.0 loaded. The clowns squeak now.");
		}
	}
	internal static class SqueakClip
	{
		public static AudioClip Clip;

		public static void Build()
		{
			int num = 5733;
			float[] array = new float[num];
			float num2 = 0f;
			for (int i = 0; i < num; i++)
			{
				float num3 = (float)i / (float)num;
				float num4 = Mathf.Sin(MathF.PI * num3);
				float num5 = ((num3 < 0.5f) ? Mathf.Lerp(760f, 1700f, num4) : Mathf.Lerp(900f, 1700f, num4));
				num2 += MathF.PI * 2f * num5 / 44100f;
				float num6 = Mathf.Sin(num2);
				float num7 = num6 * 0.82f + Mathf.Sign(num6) * 0.18f;
				float num8 = Mathf.Sin(MathF.PI * num3);
				num8 *= num8;
				array[i] = num7 * num8 * 0.6f;
			}
			Clip = AudioClip.Create("ClownShoeSqueak", num, 1, 44100, false);
			Clip.SetData(array, 0);
		}
	}
	internal sealed class Squeaker : MonoBehaviour
	{
		private const float TeleportStep = 1.5f;

		private static readonly FieldInfo? CenterField = AccessTools.Field(typeof(Enemy), "CenterTransform");

		private string _name = "clown";

		private Transform _follow;

		private AudioSource _source;

		private Vector3 _lastPos;

		private float _distance;

		private int _squeakCount;

		private bool _ready;

		public void Init(Enemy enemy, string name)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			_name = name;
			object? obj = CenterField?.GetValue(enemy);
			Transform val = (Transform)((obj is Transform) ? obj : null);
			_follow = (((Object)(object)val != (Object)null) ? val : ((Component)enemy).transform);
			GameObject val2 = new GameObject("ClownShoes_Audio");
			val2.transform.SetParent(_follow, false);
			val2.transform.localPosition = Vector3.zero;
			_source = val2.AddComponent<AudioSource>();
			_source.clip = SqueakClip.Clip;
			_source.playOnAwake = false;
			_source.spatialBlend = 1f;
			_source.dopplerLevel = 0f;
			_source.rolloffMode = (AudioRolloffMode)1;
			_source.minDistance = 1.5f;
			_source.maxDistance = Plugin.MaxHearingDistance.Value;
			_lastPos = _follow.position;
			_ready = true;
			if (Plugin.Debug.Value)
			{
				Plugin.Log.LogInfo((object)("Squeaker ready for '" + _name + "', tracking '" + ((Object)_follow).name + "'."));
			}
		}

		private void Update()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: 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_0046: 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_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			if (!_ready || (Plugin.HostOnly.Value && !SemiFunc.IsMasterClientOrSingleplayer()) || (Object)(object)_follow == (Object)null)
			{
				return;
			}
			Vector3 position = _follow.position;
			Vector3 val = position - _lastPos;
			_lastPos = position;
			Vector2 val2 = new Vector2(val.x, val.z);
			float magnitude = ((Vector2)(ref val2)).magnitude;
			if (magnitude > 1.5f)
			{
				_distance = 0f;
			}
			else if (!(magnitude / Mathf.Max(Time.deltaTime, 0.0001f) < Plugin.MinWalkSpeed.Value))
			{
				_distance += magnitude;
				while (_distance >= Plugin.StepDistance.Value)
				{
					_distance -= Plugin.StepDistance.Value;
					Squeak();
				}
			}
		}

		private void Squeak()
		{
			float value = Plugin.PitchVariation.Value;
			_source.pitch = 1f + Random.Range(0f - value, value);
			_source.PlayOneShot(SqueakClip.Clip, Plugin.Volume.Value);
			if (Plugin.Debug.Value && _squeakCount < 5)
			{
				_squeakCount++;
				Plugin.Log.LogInfo((object)("squeak! (" + _name + ")"));
			}
		}

		private void OnDestroy()
		{
			if ((Object)(object)_source != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)_source).gameObject);
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "ClownShoes";

		public const string PLUGIN_NAME = "ClownShoes";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}