Decompiled source of KnightSize v1.0.1

BepInEx\plugins\KnightSize.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace KnightSize;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("local.knightsize", "KnightSize", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "local.knightsize";

	public const string Name = "KnightSize";

	public const string Version = "1.0.0";

	internal static ConfigEntry<float> Scale;

	internal static ConfigEntry<bool> OnlyWithSuit;

	internal static ConfigEntry<string> SuitMatch;

	internal static ConfigEntry<bool> ScaleOthers;

	internal static ConfigEntry<float> JumpMultiplier;

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Expected O, but got Unknown
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Expected O, but got Unknown
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Expected O, but got Unknown
		//IL_0110: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		Scale = ((BaseUnityPlugin)this).Config.Bind<float>("Size", "Scale", 0.5f, new ConfigDescription("Player size while Knight-sized (1 = normal). Camera height follows it.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.2f, 1.5f), new object[0]));
		OnlyWithSuit = ((BaseUnityPlugin)this).Config.Bind<bool>("Size", "OnlyWithKnightSuit", true, "Only shrink players wearing the Knight suit. If false, everyone is always shrunk.");
		SuitMatch = ((BaseUnityPlugin)this).Config.Bind<string>("Size", "SuitNameContains", "knight", "A suit counts as the Knight suit if its name contains this text (not case-sensitive).");
		ScaleOthers = ((BaseUnityPlugin)this).Config.Bind<bool>("Size", "ScaleOtherPlayers", true, "Also shrink other players wearing the suit, so you see them Knight-sized too.");
		JumpMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Movement", "JumpMultiplier", 1f, new ConfigDescription("Multiplies jump force while Knight-sized.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.3f, 2f), new object[0]));
		GameObject val = new GameObject("KnightSizeRunner");
		((Object)val).hideFlags = (HideFlags)61;
		Object.DontDestroyOnLoad((Object)(object)val);
		val.AddComponent<Runner>();
		new Harmony("local.knightsize").PatchAll(typeof(HoverPatch));
		Log.LogInfo((object)"KnightSize loaded.");
	}
}
public class Runner : MonoBehaviour
{
	private class State
	{
		public float BaseNearClip;

		public float BaseJump;

		public float BaseGrab;

		public float AppliedScale = 1f;
	}

	private readonly Dictionary<PlayerControllerB, State> states = new Dictionary<PlayerControllerB, State>();

	internal static bool IsSmallLocal(PlayerControllerB p)
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)p != (Object)null && (Object)(object)GameNetworkManager.Instance != (Object)null && (Object)(object)p == (Object)(object)GameNetworkManager.Instance.localPlayerController)
		{
			return ((Component)p).transform.localScale.y < 0.99f;
		}
		return false;
	}

	private void LateUpdate()
	{
		StartOfRound instance = StartOfRound.Instance;
		if ((Object)(object)instance == (Object)null || instance.allPlayerScripts == null)
		{
			return;
		}
		PlayerControllerB val = (((Object)(object)GameNetworkManager.Instance != (Object)null) ? GameNetworkManager.Instance.localPlayerController : null);
		for (int i = 0; i < instance.allPlayerScripts.Length; i++)
		{
			PlayerControllerB val2 = instance.allPlayerScripts[i];
			if (!((Object)(object)val2 == (Object)null))
			{
				bool flag = (Object)(object)val2 == (Object)(object)val;
				float scale = 1f;
				if ((flag || Plugin.ScaleOthers.Value) && WantsKnightSize(instance, val2))
				{
					scale = Plugin.Scale.Value;
				}
				Apply(val2, flag, scale);
			}
		}
	}

	private static bool WantsKnightSize(StartOfRound round, PlayerControllerB p)
	{
		if (!p.isPlayerControlled && !p.isPlayerDead)
		{
			return false;
		}
		if (!Plugin.OnlyWithSuit.Value)
		{
			return true;
		}
		List<UnlockableItem> list = (((Object)(object)round.unlockablesList != (Object)null) ? round.unlockablesList.unlockables : null);
		int currentSuitID = p.currentSuitID;
		if (list == null || currentSuitID < 0 || currentSuitID >= list.Count || list[currentSuitID] == null)
		{
			return false;
		}
		string text = list[currentSuitID].unlockableName ?? "";
		string text2 = Plugin.SuitMatch.Value ?? "";
		if (text2.Length > 0)
		{
			return text.IndexOf(text2, StringComparison.OrdinalIgnoreCase) >= 0;
		}
		return false;
	}

	private void Apply(PlayerControllerB p, bool isLocal, float scale)
	{
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		if (!states.TryGetValue(p, out var value))
		{
			value = new State();
			value.BaseNearClip = (((Object)(object)p.gameplayCamera != (Object)null) ? p.gameplayCamera.nearClipPlane : 0.05f);
			value.BaseJump = p.jumpForce;
			value.BaseGrab = p.grabDistance;
			states[p] = value;
		}
		if (Mathf.Approximately(value.AppliedScale, scale) && Mathf.Approximately(((Component)p).transform.localScale.y, scale))
		{
			return;
		}
		((Component)p).transform.localScale = new Vector3(scale, scale, scale);
		if (isLocal)
		{
			if ((Object)(object)p.gameplayCamera != (Object)null)
			{
				p.gameplayCamera.nearClipPlane = Mathf.Max(0.01f, value.BaseNearClip * scale);
			}
			p.jumpForce = ((scale < 1f) ? (value.BaseJump * Plugin.JumpMultiplier.Value) : value.BaseJump);
			p.grabDistance = ((scale < 1f) ? (value.BaseGrab / scale) : value.BaseGrab);
		}
		value.AppliedScale = scale;
	}
}
[HarmonyPatch(typeof(PlayerControllerB), "SetHoverTipAndCurrentInteractTrigger")]
internal static class HoverPatch
{
	private static int mask = -1;

	private static void Postfix(PlayerControllerB __instance)
	{
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!Runner.IsSmallLocal(__instance) || (Object)(object)__instance.hoveringOverTrigger != (Object)null || __instance.isPlayerDead || __instance.inSpecialInteractAnimation || __instance.isGrabbingObjectAnimation || (Object)(object)__instance.gameplayCamera == (Object)null)
			{
				return;
			}
			if (mask == -1)
			{
				mask = LayerMask.GetMask(new string[1] { "InteractableObject" });
				if (mask == 0)
				{
					mask = 512;
				}
			}
			Transform transform = ((Component)__instance.gameplayCamera).transform;
			RaycastHit[] array = Physics.RaycastAll(new Ray(transform.position, transform.forward), __instance.grabDistance, mask, (QueryTriggerInteraction)2);
			if (array.Length == 0)
			{
				return;
			}
			Array.Sort(array, (RaycastHit a, RaycastHit b) => ((RaycastHit)(ref a)).distance.CompareTo(((RaycastHit)(ref b)).distance));
			for (int num = 0; num < array.Length; num++)
			{
				InteractTrigger component = ((Component)((RaycastHit)(ref array[num])).collider).GetComponent<InteractTrigger>();
				if ((Object)(object)component == (Object)null || !((Behaviour)component).enabled)
				{
					continue;
				}
				if (!component.interactable)
				{
					if ((Object)(object)__instance.cursorTip != (Object)null && !string.IsNullOrEmpty(component.disabledHoverTip))
					{
						((TMP_Text)__instance.cursorTip).text = component.disabledHoverTip;
					}
					break;
				}
				__instance.hoveringOverTrigger = component;
				if ((Object)(object)__instance.cursorIcon != (Object)null && (Object)(object)component.hoverIcon != (Object)null)
				{
					((Behaviour)__instance.cursorIcon).enabled = true;
					__instance.cursorIcon.sprite = component.hoverIcon;
				}
				if ((Object)(object)__instance.cursorTip != (Object)null)
				{
					((TMP_Text)__instance.cursorTip).text = component.hoverTip;
				}
				break;
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Hover fallback failed: " + ex.Message));
		}
	}
}