Decompiled source of Slapsong v1.0.1

Slapsong.dll

Decompiled 3 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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 Silksong.AssetHelper.ManagedAssets;
using UnityEngine;
using UnityEngine.SceneManagement;

[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.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;
		}
	}
}
[BepInPlugin("lagerthon.Slapsong", "Slapsong", "1.0.1")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Slapsong : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	public static Slapsong Instance;

	private ConfigEntry<KeyCode> slapKey;

	public static ConfigEntry<int> dmg;

	private static ConfigEntry<float> slapVol;

	private static ConfigEntry<float> runToSlap;

	public static ManagedAsset<GameObject> nuuGO;

	public static GameObject nuu;

	private static bool nuuBool;

	public static bool isSlapHit;

	private AudioClip cachedSlapAudio;

	private AudioSource slapAudioSource;

	public static BoxCollider2D slapBoxFront;

	public static BoxCollider2D slapBoxBack;

	private HeroController hero;

	private bool isHeroSlapping = false;

	private void Awake()
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Expected O, but got Unknown
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Expected O, but got Unknown
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Expected O, but got Unknown
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Expected O, but got Unknown
		//IL_013d: Unknown result type (might be due to invalid IL or missing references)
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		slapKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Input", "SlapKey", (KeyCode)108, "Key to slap.");
		dmg = ((BaseUnityPlugin)this).Config.Bind<int>("Slap", "Damage dealt to enemies", 9999, new ConfigDescription("Damage dealt to enemies", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 9999), Array.Empty<object>()));
		slapVol = ((BaseUnityPlugin)this).Config.Bind<float>("Slap", "Slap Volume", 1f, new ConfigDescription("Volume of the slap", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 1f), Array.Empty<object>()));
		runToSlap = ((BaseUnityPlugin)this).Config.Bind<float>("Slap", "Run to slap time (secs)", 0.2f, new ConfigDescription("Time that Hornet takes in stopping if slap is triggered while running", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 0.3f), Array.Empty<object>()));
		nuuGO = ManagedAsset<GameObject>.FromSceneAsset("halfway_01", "_NPCs/Hunter Fan Control/Nuu");
		GameObject val = new GameObject("SlapsongAudio");
		Object.DontDestroyOnLoad((Object)(object)val);
		slapAudioSource = val.AddComponent<AudioSource>();
		slapAudioSource.playOnAwake = false;
		SceneManager.sceneLoaded += OnSceneLoaded;
		new Harmony("lagerthon.Slapsong").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Slapsong loaded.");
	}

	private void Update()
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)hero == (Object)null) && Input.GetKeyDown(slapKey.Value) && !isHeroSlapping && !hero.controlReqlinquished)
		{
			((MonoBehaviour)this).StartCoroutine(Slap(hero));
		}
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		//IL_00bb: 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_0113: Unknown result type (might be due to invalid IL or missing references)
		hero = HeroController.instance;
		if ((Object)(object)hero == (Object)null)
		{
			return;
		}
		SlapHitBox(hero);
		if ((Object)(object)hero != (Object)null && !nuuBool)
		{
			((MonoBehaviour)this).StartCoroutine(Nuu());
		}
		if (!PlayerData.instance.CaravanLechSpaAcceptState)
		{
			return;
		}
		GameObject val = GameObject.Find("Caravan Lech");
		if (!((Object)(object)val == (Object)null))
		{
			Transform val2 = val.transform.Find("Caravan Lech Wounded");
			if ((Object)(object)val2 != (Object)null)
			{
				GameObject val3 = Object.Instantiate<GameObject>(((Component)val2).gameObject, val.transform.position, val.transform.rotation);
				((Object)val3).name = "Caravan Lech Wounded Clone";
				val3.layer = LayerMask.NameToLayer("Enemies");
				CircleCollider2D val4 = val3.AddComponent<CircleCollider2D>();
				val4.radius = 1f;
				((Collider2D)val4).offset = new Vector2(0f, 0.5f);
				val3.transform.SetParent((Transform)null);
				val.SetActive(false);
			}
		}
	}

	private IEnumerator Nuu()
	{
		nuuBool = true;
		((ManagedAssetBase<GameObject>)(object)nuuGO).Load();
		yield return (object)new WaitUntil((Func<bool>)(() => ((ManagedAssetBase<GameObject>)(object)nuuGO).IsLoaded));
		nuu = ManagedAssetExtensions.InstantiateAsset<GameObject>(nuuGO);
		Object.DontDestroyOnLoad((Object)(object)nuu);
		nuu.SetActive(false);
		CacheSlapAudio();
	}

	private void CacheSlapAudio()
	{
		if ((Object)(object)cachedSlapAudio != (Object)null)
		{
			return;
		}
		AudioClip[] array = Resources.FindObjectsOfTypeAll<AudioClip>();
		foreach (AudioClip val in array)
		{
			if (((val != null) ? ((Object)val).name : null) == "hornet_slap_B_2")
			{
				cachedSlapAudio = val;
				break;
			}
		}
	}

	private IEnumerator Slap(HeroController hero)
	{
		if ((int)hero.hero_state != 1 && (int)hero.hero_state != 2)
		{
			yield break;
		}
		isHeroSlapping = true;
		CancelSlapOnDamage.heroDamaged = false;
		HeroAnimationController heroAnim = ((Component)hero).GetComponent<HeroAnimationController>();
		Rigidbody2D rb = ((Component)hero).GetComponent<Rigidbody2D>();
		if ((Object)(object)rb != (Object)null && (int)hero.hero_state == 2)
		{
			Vector2 startVelocity = rb.velocity;
			float stopTime = runToSlap.Value;
			float t = 0f;
			while (t < stopTime)
			{
				if (CancelSlapOnDamage.heroDamaged)
				{
					EndSlap();
					yield break;
				}
				t += Time.deltaTime;
				float progress = Mathf.Clamp01(t / stopTime);
				rb.velocity = Vector2.Lerp(startVelocity, Vector2.zero, progress);
				yield return null;
			}
			rb.velocity = Vector2.zero;
		}
		hero.IgnoreInput();
		hero.StopAnimationControl();
		((Behaviour)slapBoxFront).enabled = false;
		((Behaviour)slapBoxBack).enabled = false;
		float totalLength = heroAnim.GetClipDuration("Idle Slap");
		heroAnim.PlayClipForced("Idle Slap");
		tk2dSpriteAnimationClip animClip = heroAnim.animator.CurrentClip;
		float slapBoxStart = totalLength * 0.25f;
		float slapBoxMid = totalLength * 0.4f;
		float slapBoxEnd = totalLength * 0.85f;
		float elapsed = 0f;
		while (heroAnim.animator.IsPlaying(animClip) && elapsed < totalLength)
		{
			if (CancelSlapOnDamage.heroDamaged)
			{
				EndSlap();
				yield break;
			}
			elapsed += Time.deltaTime;
			((Behaviour)slapBoxFront).enabled = elapsed >= slapBoxStart && elapsed < slapBoxMid;
			((Behaviour)slapBoxBack).enabled = elapsed >= slapBoxMid && elapsed < slapBoxEnd;
			yield return null;
		}
		EndSlap();
	}

	private void EndSlap()
	{
		((Behaviour)slapBoxFront).enabled = false;
		((Behaviour)slapBoxBack).enabled = false;
		if ((Object)(object)hero != (Object)null)
		{
			hero.AcceptInput();
			hero.StartAnimationControl();
		}
		isHeroSlapping = false;
	}

	public void PlaySlapSound()
	{
		slapAudioSource.PlayOneShot(cachedSlapAudio, slapVol.Value);
	}

	private void SlapHitBox(HeroController hero)
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Expected O, but got Unknown
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Expected O, but got Unknown
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)slapBoxFront != (Object)null) || !((Object)(object)slapBoxBack != (Object)null))
		{
			GameObject val = new GameObject("SlapHitbox");
			val.transform.SetParent(((Component)hero).transform, false);
			val.transform.localPosition = Vector3.zero;
			Rigidbody2D val2 = val.AddComponent<Rigidbody2D>();
			val2.bodyType = (RigidbodyType2D)1;
			GameObject val3 = new GameObject("Slap Hit Box");
			val3.transform.SetParent(((Component)hero).transform, false);
			slapBoxFront = val3.AddComponent<BoxCollider2D>();
			((Collider2D)slapBoxFront).isTrigger = true;
			((Behaviour)slapBoxFront).enabled = false;
			slapBoxFront.size = new Vector2(1.25f, 0.5f);
			((Collider2D)slapBoxFront).offset = new Vector2(-0.675f, 0f);
			slapBoxBack = val3.AddComponent<BoxCollider2D>();
			((Collider2D)slapBoxBack).isTrigger = true;
			((Behaviour)slapBoxBack).enabled = false;
			slapBoxBack.size = new Vector2(1.25f, 0.5f);
			((Collider2D)slapBoxBack).offset = new Vector2(0.575f, 0f);
			val3.AddComponent<SlapDamageHandler>();
		}
	}
}
public class SlapDamageHandler : MonoBehaviour
{
	public bool isFront;

	private string slapEvent;

	private float dir;

	private void OnTriggerEnter2D(Collider2D other)
	{
		//IL_019e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0211: Unknown result type (might be due to invalid IL or missing references)
		//IL_0213: Unknown result type (might be due to invalid IL or missing references)
		//IL_021b: Unknown result type (might be due to invalid IL or missing references)
		//IL_021c: Unknown result type (might be due to invalid IL or missing references)
		bool flag = (Object)(object)HeroController.instance != (Object)null && HeroController.instance.cState.facingRight;
		if (((Object)((Component)((Component)other).transform.root).gameObject).name == "Caravan Lech Wounded Clone")
		{
			GameObject gameObject = ((Component)((Component)other).transform.root).gameObject;
			PlayMakerFSM[] componentsInChildren = gameObject.GetComponentsInChildren<PlayMakerFSM>(true);
			PlayMakerFSM val = ((IEnumerable<PlayMakerFSM>)componentsInChildren).FirstOrDefault((Func<PlayMakerFSM, bool>)((PlayMakerFSM f) => f.FsmName == "Wounded Behaviour"));
			if ((Object)(object)val != (Object)null)
			{
				isFront = ((Behaviour)Slapsong.slapBoxFront).enabled;
				if (flag)
				{
					slapEvent = (isFront ? "HIT_RIGHT" : "HIT_LEFT");
				}
				else
				{
					slapEvent = (isFront ? "HIT_LEFT" : "HIT_RIGHT");
				}
				val.SendEvent(slapEvent);
				Slapsong.Instance.PlaySlapSound();
			}
		}
		else
		{
			if (((Component)other).gameObject.layer != LayerMask.NameToLayer("Enemies"))
			{
				return;
			}
			HealthManager val2 = ((Component)other).GetComponent<HealthManager>() ?? ((Component)other).GetComponentInParent<HealthManager>();
			if (!((Object)(object)val2 == (Object)null))
			{
				isFront = ((Behaviour)Slapsong.slapBoxFront).enabled;
				if (flag)
				{
					dir = (isFront ? 0f : 180f);
				}
				else
				{
					dir = (isFront ? 180f : 0f);
				}
				HitInstance val3 = new HitInstance
				{
					Source = ((Component)this).gameObject,
					DamageDealt = Slapsong.dmg.Value,
					Multiplier = 1f,
					AttackType = (AttackTypes)1,
					HitEffectsType = (EffectsTypes)1,
					Direction = dir,
					IgnoreInvulnerable = false,
					MagnitudeMultiplier = 1.5f + (float)Slapsong.dmg.Value / 9999f
				};
				Slapsong.isSlapHit = true;
				val2.Hit(val3);
				Slapsong.isSlapHit = false;
				Slapsong.Instance.PlaySlapSound();
			}
		}
	}
}
[HarmonyPatch(typeof(HeroController), "HeroDamaged")]
internal class CancelSlapOnDamage
{
	public static bool heroDamaged;

	private static void Prefix()
	{
		heroDamaged = true;
	}
}