Decompiled source of KickBecomeBonesFix v1.1.1

BepInEx/plugins/KickBecomeBonesFix/KickBecomeBonesFix.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Peak.Afflictions;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

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

[BepInPlugin("com.kickbecomebonesfix.plugin", "KickBecomeBonesFix", "1.1.1")]
public class Plugin : BaseUnityPlugin
{
	internal class Notice
	{
		internal string Text;

		internal float ShownAt;

		internal Color TextColor;
	}

	internal class NoticeRenderer : MonoBehaviour
	{
		private Font _font;

		private GUIStyle _style;

		private GUIStyle _small;

		private Texture2D _white;

		private bool _guiLogged = false;

		private void EnsureStyle()
		{
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Expected O, but got Unknown
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Expected O, but got Unknown
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Expected O, but got Unknown
			if ((Object)(object)_white == (Object)null)
			{
				_white = Texture2D.whiteTexture;
			}
			if (_style != null)
			{
				return;
			}
			if ((Object)(object)_font == (Object)null)
			{
				try
				{
					_font = Font.CreateDynamicFontFromOSFont(new string[6] { "Microsoft YaHei", "Microsoft YaHei UI", "SimHei", "SimSun", "Noto Sans CJK SC", "Arial" }, 24);
				}
				catch (Exception ex)
				{
					Log.LogError((object)("Dynamic font creation failed: " + ex.Message));
					_font = null;
				}
			}
			try
			{
				_style = new GUIStyle(GUI.skin.label);
			}
			catch
			{
				_style = new GUIStyle();
			}
			_style.fontSize = 24;
			_style.fontStyle = (FontStyle)1;
			_style.alignment = (TextAnchor)4;
			_style.wordWrap = false;
			_style.clipping = (TextClipping)0;
			if ((Object)(object)_font != (Object)null)
			{
				_style.font = _font;
			}
			_small = new GUIStyle(_style);
			_small.fontSize = 16;
			_small.fontStyle = (FontStyle)0;
		}

		private void OnGUI()
		{
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0201: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_0274: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b9: Unknown result type (might be due to invalid IL or missing references)
			if (!_guiLogged)
			{
				_guiLogged = true;
				Log.LogInfo((object)"NoticeRenderer OnGUI is active (IMGUI works).");
			}
			List<Notice> notices = Plugin.notices;
			if (notices.Count == 0)
			{
				return;
			}
			float time = Time.time;
			for (int num = notices.Count - 1; num >= 0; num--)
			{
				if (time - notices[num].ShownAt > 8f)
				{
					notices.RemoveAt(num);
				}
			}
			if (notices.Count == 0)
			{
				return;
			}
			try
			{
				EnsureStyle();
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Notice style setup failed: " + ex.Message));
				return;
			}
			float num2 = Mathf.Min((float)Screen.width * 0.92f, 1000f);
			float num3 = 40f;
			float num4 = (float)Screen.height * 0.68f;
			Color color = GUI.color;
			Rect val = default(Rect);
			foreach (Notice item in notices)
			{
				float num5 = 1f;
				float num6 = time - item.ShownAt;
				if (num6 > 6.5f)
				{
					num5 = Mathf.Clamp01(1f - (num6 - 6.5f) / 1.5f);
				}
				((Rect)(ref val))..ctor(((float)Screen.width - num2) / 2f, num4, num2, num3);
				GUI.color = new Color(0.03f, 0.03f, 0.06f, 0.78f * num5);
				GUI.DrawTexture(val, (Texture)(object)_white);
				GUI.color = new Color(item.TextColor.r, item.TextColor.g, item.TextColor.b, 0.95f * num5);
				GUI.DrawTexture(new Rect(((Rect)(ref val)).x, ((Rect)(ref val)).y, ((Rect)(ref val)).width, 2f), (Texture)(object)_white);
				GUI.DrawTexture(new Rect(((Rect)(ref val)).x, ((Rect)(ref val)).yMax - 2f, ((Rect)(ref val)).width, 2f), (Texture)(object)_white);
				GUI.color = new Color(item.TextColor.r, item.TextColor.g, item.TextColor.b, num5);
				GUI.Label(val, item.Text, _style);
				num4 -= num3 + 6f;
			}
			GUI.color = color;
		}
	}

	[HarmonyPatch(typeof(CharacterGrabbing), "RPCA_Kick")]
	internal static class KickPressedPatch
	{
		[HarmonyPostfix]
		internal static void Postfix(PhotonMessageInfo info)
		{
			try
			{
				if (info.Sender == null)
				{
					return;
				}
				recentKickSenders[info.Sender.ActorNumber] = Time.time;
				Log.LogInfo((object)("[踢击检测] 收到踢击广播,actor=" + info.Sender.ActorNumber + " (" + info.Sender.NickName + ")"));
				if (recentKickSenders.Count <= 16)
				{
					return;
				}
				List<int> list = new List<int>();
				foreach (KeyValuePair<int, float> recentKickSender in recentKickSenders)
				{
					if (Time.time - recentKickSender.Value > 5f)
					{
						list.Add(recentKickSender.Key);
					}
				}
				for (int i = 0; i < list.Count; i++)
				{
					recentKickSenders.Remove(list[i]);
				}
			}
			catch (Exception ex)
			{
				Log.LogError((object)("KickPressedPatch error: " + ex));
			}
		}
	}

	[HarmonyPatch(typeof(Character), "RPCA_Fall", new Type[]
	{
		typeof(float),
		typeof(float),
		typeof(PhotonMessageInfo)
	})]
	internal static class KickedRagdollPatch
	{
		[HarmonyPrefix]
		internal static bool Prefix(Character __instance, PhotonMessageInfo info)
		{
			//IL_0029: 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_0119: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)__instance == (Object)null || (Object)(object)__instance != (Object)(object)Character.localCharacter)
				{
					return true;
				}
				if (!IsKickFrom(info))
				{
					if (info.Sender != null && (Object)(object)Character.localCharacter != (Object)null && info.Sender.ActorNumber != PhotonNetwork.LocalPlayer.ActorNumber)
					{
						Log.LogInfo((object)("[踢击检测] 本地角色被他人弄倒,但发送者不是近期踢击者,忽略。sender=" + info.Sender.ActorNumber + " (" + info.Sender.NickName + ")"));
					}
					return true;
				}
				Character characterFromInfo = GetCharacterFromInfo(info);
				if ((Object)(object)characterFromInfo == (Object)null || (Object)(object)characterFromInfo == (Object)(object)Character.localCharacter)
				{
					return true;
				}
				Log.LogInfo((object)("[踢击检测] 命中:被 " + GetCharacterDisplayName(characterFromInfo) + " 踢中。"));
				OnIWasKicked(characterFromInfo, info);
				return false;
			}
			catch (Exception ex)
			{
				Log.LogError((object)("KickedRagdollPatch error: " + ex));
				return true;
			}
		}
	}

	[HarmonyPatch(typeof(Character), "RPCA_AddForceAtPosition", new Type[]
	{
		typeof(Vector3),
		typeof(Vector3),
		typeof(float),
		typeof(PhotonMessageInfo)
	})]
	internal static class ShieldForceReceiverPatch
	{
		[HarmonyPrefix]
		internal static bool Prefix(Character __instance, Vector3 force, Vector3 point, float radius)
		{
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!IsShielded(__instance))
				{
					return true;
				}
				if (!forceNegationSent)
				{
					forceNegationSent = true;
					if (__instance.refs != null && (Object)(object)__instance.refs.view != (Object)null)
					{
						__instance.refs.view.RPC("RPCA_AddForceAtPosition", (RpcTarget)0, new object[3]
						{
							-force,
							point,
							radius
						});
					}
				}
				return false;
			}
			catch (Exception ex)
			{
				Log.LogError((object)("ShieldForceReceiverPatch error: " + ex));
				return true;
			}
		}
	}

	public const string Id = "com.kickbecomebonesfix.plugin";

	public const string Name = "KickBecomeBonesFix";

	public const string Version = "1.1.1";

	internal static readonly ManualLogSource Log = Logger.CreateLogSource("KickBecomeBonesFix");

	internal static Plugin instance;

	internal static readonly Dictionary<int, float> recentKickSenders = new Dictionary<int, float>();

	internal static float shieldUntil = 0f;

	internal static bool forceNegationSent = false;

	internal static readonly List<Notice> notices = new List<Notice>();

	internal static NoticeRenderer renderer;

	private void Awake()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		instance = this;
		Log.LogInfo((object)"KickBecomeBonesFix 1.1.1 loading.");
		Harmony harmony = new Harmony("com.kickbecomebonesfix.plugin");
		TryPatch(harmony, typeof(KickPressedPatch));
		TryPatch(harmony, typeof(KickedRagdollPatch));
		TryPatch(harmony, typeof(ShieldForceReceiverPatch));
		EnsureRenderer();
		Log.LogInfo((object)"KickBecomeBonesFix 1.1.1 ready. Only the kicked player punishes the kicker.");
	}

	internal static void TryPatch(Harmony harmony, Type type)
	{
		try
		{
			harmony.PatchAll(type);
			Log.LogInfo((object)("Patched " + type.Name + " OK."));
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Failed to patch " + type.Name + ": " + ex));
		}
	}

	internal static void ShowNotice(string text, Color color)
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		Notice notice = new Notice();
		notice.Text = text;
		notice.ShownAt = Time.time;
		notice.TextColor = color;
		notices.Add(notice);
		Log.LogInfo((object)("[踢击提示] " + text));
		EnsureRenderer();
	}

	internal static void EnsureRenderer()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		if ((Object)(object)renderer != (Object)null)
		{
			return;
		}
		try
		{
			GameObject val = new GameObject("KickBecomeBonesNoticeRenderer");
			Object.DontDestroyOnLoad((Object)(object)val);
			renderer = val.AddComponent<NoticeRenderer>();
			Log.LogInfo((object)"Notice renderer created.");
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Notice renderer creation failed: " + ex.Message));
		}
	}

	internal static Character GetCharacterFromInfo(PhotonMessageInfo info)
	{
		Player sender = info.Sender;
		if (sender == null)
		{
			return null;
		}
		for (int i = 0; i < Character.AllCharacters.Count; i++)
		{
			Character val = Character.AllCharacters[i];
			if ((Object)(object)val == (Object)null || val.refs == null)
			{
				continue;
			}
			PhotonView view = val.refs.view;
			if (!((Object)(object)view == (Object)null))
			{
				Player controller = view.Controller;
				if (controller != null && ((object)controller).Equals((object?)sender))
				{
					return val;
				}
			}
		}
		return null;
	}

	internal static string GetCharacterDisplayName(Character c)
	{
		if ((Object)(object)c == (Object)null)
		{
			return "未知玩家";
		}
		try
		{
			string characterName = c.characterName;
			if (!string.IsNullOrEmpty(characterName))
			{
				return characterName;
			}
		}
		catch
		{
		}
		return "未知玩家";
	}

	internal static bool IsShielded(Character c)
	{
		return (Object)(object)c != (Object)null && (Object)(object)c == (Object)(object)Character.localCharacter && Time.time < shieldUntil;
	}

	internal static void PunishKicker(Character kicker)
	{
		if (!((Object)(object)kicker == (Object)null) && kicker.refs != null && !((Object)(object)kicker.refs.view == (Object)null) && !((Object)(object)kicker.data == (Object)null) && !kicker.data.dead && !((Object)(object)kicker == (Object)(object)Character.localCharacter))
		{
			kicker.refs.view.RPC("RPCA_Die", (RpcTarget)0, new object[0]);
			Log.LogInfo((object)("[踢击提示] 已处死踢人者 " + GetCharacterDisplayName(kicker)));
		}
	}

	internal static void ApplyMilk(Character c)
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Expected O, but got Unknown
		if (!((Object)(object)c == (Object)null) && c.refs != null && !((Object)(object)c.refs.afflictions == (Object)null))
		{
			Affliction_Invincibility val = new Affliction_Invincibility();
			((Affliction)val).totalTime = 20f;
			((Affliction)val).timeElapsed = 0f;
			val.isFromMilk = true;
			c.refs.afflictions.AddAffliction((Affliction)(object)val, false);
			Log.LogInfo((object)("[踢击提示] 已给 " + GetCharacterDisplayName(c) + " 施加强化牛奶 20 秒。"));
		}
	}

	internal static void ArmShield()
	{
		shieldUntil = Time.time + 2f;
		forceNegationSent = false;
		if ((Object)(object)instance != (Object)null)
		{
			((MonoBehaviour)instance).StartCoroutine(UnFallRoutine());
		}
	}

	internal static IEnumerator UnFallRoutine()
	{
		yield return (object)new WaitForSeconds(0.25f);
		SendUnFall();
		yield return (object)new WaitForSeconds(0.8f);
		SendUnFall();
	}

	internal static void SendUnFall()
	{
		Character localCharacter = Character.localCharacter;
		if (!((Object)(object)localCharacter == (Object)null) && localCharacter.refs != null && !((Object)(object)localCharacter.refs.view == (Object)null))
		{
			localCharacter.refs.view.RPC("RPCA_UnFall", (RpcTarget)0, new object[0]);
		}
	}

	internal static bool IsKickFrom(PhotonMessageInfo info)
	{
		if (info.Sender == null)
		{
			return false;
		}
		if (recentKickSenders.TryGetValue(info.Sender.ActorNumber, out var value))
		{
			return Time.time - value < 3f;
		}
		return false;
	}

	internal static void OnIWasKicked(Character kicker, PhotonMessageInfo info)
	{
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		Character localCharacter = Character.localCharacter;
		string text = GetCharacterDisplayName(kicker);
		if (text == "未知玩家" && info.Sender != null)
		{
			text = info.Sender.NickName;
		}
		string text2 = GetCharacterDisplayName(localCharacter);
		if (text2 == "未知玩家")
		{
			text2 = "你";
		}
		ShowNotice("已施加强化牛奶(20 秒无敌)," + text + " 已变成骷髅", new Color(0.72f, 1f, 0.72f));
		ShowNotice("被踢的是:" + text2, new Color(0.92f, 0.92f, 0.92f));
		ShowNotice(text + " 踢了你!", new Color(1f, 0.82f, 0.35f));
		try
		{
			ApplyMilk(localCharacter);
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Milk failed: " + ex));
		}
		try
		{
			PunishKicker(kicker);
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Punish failed: " + ex));
		}
		try
		{
			ArmShield();
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Shield failed: " + ex));
		}
	}
}