Decompiled source of AllPlayerCommands v0.1.0

AllPlayerCommands.dll

Decompiled 8 hours ago
using System;
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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
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: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("test")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("test")]
[assembly: AssemblyTitle("test")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 RepoAllPlayerCommands
{
	[BepInPlugin("jp.repo.allplayercommands", "AllPlayerCommands", "0.3.2")]
	public class AllPlayerCommandsPlugin : BaseUnityPlugin
	{
		internal static class PlayerActions
		{
			public static void Kill(PlayerAvatar avatar)
			{
				if (IsNullUnity(avatar))
				{
					LogWarn("Kill null target");
					return;
				}
				LogInfo("Kill execute " + PlayerLabel(avatar));
				avatar.PlayerDeath(-1);
				LogInfo("Kill complete " + PlayerLabel(avatar));
			}

			public static void Revive(PlayerAvatar avatar)
			{
				if (IsNullUnity(avatar))
				{
					LogWarn("Revive null target");
					return;
				}
				LogInfo("Revive execute " + PlayerLabel(avatar));
				if (avatar.deadSet && !IsNullUnity(avatar.playerDeathHead))
				{
					PlayerDeathHead playerDeathHead = avatar.playerDeathHead;
					playerDeathHead.inExtractionPoint = true;
					avatar.Revive(false);
					playerDeathHead.inExtractionPoint = false;
					LogInfo("Revive complete " + PlayerLabel(avatar));
				}
				else
				{
					LogInfo("Revive skipped not dead " + PlayerLabel(avatar));
				}
			}

			public static void Heal(PlayerAvatar avatar)
			{
				if (IsNullUnity(avatar))
				{
					LogWarn("Heal null target");
					return;
				}
				LogInfo("Heal execute " + PlayerLabel(avatar));
				PlayerHealth val = null;
				try
				{
					val = avatar.playerHealth;
				}
				catch
				{
					val = null;
				}
				if (IsNullUnity(val))
				{
					LogWarn("Heal skipped playerHealth null " + PlayerLabel(avatar));
					return;
				}
				avatar.playerHealth.HealOther(val.maxHealth, true);
				LogInfo("Heal complete " + PlayerLabel(avatar));
			}

			public static void SummonToLocal(PlayerAvatar target)
			{
				if (IsNullUnity(target))
				{
					LogWarn("Summon null target");
					return;
				}
				PlayerAvatar instance = PlayerAvatar.instance;
				if (IsNullUnity(instance))
				{
					LogWarn("Summon skipped local null");
					return;
				}
				LogInfo("Summon execute target=" + PlayerLabel(target) + " toLocal=" + PlayerLabel(instance));
				Teleport(target, instance);
				LogInfo("Summon complete target=" + PlayerLabel(target));
			}

			public static void ReturnToTruck(PlayerAvatar avatar)
			{
				//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_004f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0054: Unknown result type (might be due to invalid IL or missing references)
				//IL_006c: Unknown result type (might be due to invalid IL or missing references)
				//IL_006d: Unknown result type (might be due to invalid IL or missing references)
				if (IsNullUnity(avatar))
				{
					LogWarn("Truck null target");
					return;
				}
				if (IsNullUnity(TruckSafetySpawnPoint.instance))
				{
					LogWarn("Truck skipped spawnpoint null");
					return;
				}
				Vector3 position = ((Component)TruckSafetySpawnPoint.instance).transform.position;
				Quaternion rotation = ((Component)TruckSafetySpawnPoint.instance).transform.rotation;
				LogInfo("Truck execute " + PlayerLabel(avatar));
				Teleport(avatar, position, rotation);
				LogInfo("Truck complete " + PlayerLabel(avatar));
			}

			private static void Teleport(PlayerAvatar from, PlayerAvatar to)
			{
				//IL_002a: Unknown result type (might be due to invalid IL or missing references)
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0036: Unknown result type (might be due to invalid IL or missing references)
				//IL_003b: Unknown result type (might be due to invalid IL or missing references)
				//IL_009a: 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)
				//IL_007c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0081: Unknown result type (might be due to invalid IL or missing references)
				//IL_0092: Unknown result type (might be due to invalid IL or missing references)
				//IL_0097: Unknown result type (might be due to invalid IL or missing references)
				if (IsNullUnity(from) || IsNullUnity(to))
				{
					LogWarn("Teleport(from,to) null");
					return;
				}
				Vector3 position = ((Component)to).transform.position;
				Quaternion rotation = ((Component)to).transform.rotation;
				if (to.deadSet && !IsNullUnity(to.playerDeathHead) && !IsNullUnity(to.playerDeathHead.physGrabObject))
				{
					position = ((Component)to.playerDeathHead.physGrabObject).transform.position;
					rotation = ((Component)to.playerDeathHead.physGrabObject).transform.rotation;
				}
				Teleport(from, position, rotation);
			}

			private static void Teleport(PlayerAvatar from, Vector3 pos, Quaternion rot)
			{
				//IL_0039: 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_00f8: Unknown result type (might be due to invalid IL or missing references)
				//IL_0101: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
				if (IsNullUnity(from))
				{
					LogWarn("Teleport(from,pos,rot) null");
					return;
				}
				bool flag = false;
				try
				{
					flag = SemiFunc.IsMultiplayer();
				}
				catch
				{
					flag = false;
				}
				if (!flag)
				{
					from.Spawn(pos, rot);
					LogInfo("Teleport single Spawn " + PlayerLabel(from));
					return;
				}
				if (IsNullUnity(from.photonView))
				{
					LogWarn("Teleport skipped photonView null " + PlayerLabel(from));
					return;
				}
				if (from.deadSet && !IsNullUnity(from.playerDeathHead) && !IsNullUnity(from.playerDeathHead.physGrabObject))
				{
					from.playerDeathHead.physGrabObject.Teleport(pos, rot);
					LogInfo("Teleport dead physGrabObject " + PlayerLabel(from));
					return;
				}
				from.photonView.RPC("SpawnRPC", (RpcTarget)0, new object[2] { pos, rot });
				LogInfo("Teleport RPC SpawnRPC " + PlayerLabel(from));
			}
		}

		[HarmonyPatch(typeof(ChatManager), "MessageSend")]
		public static class Patch_ChatManager_MessageSend
		{
			private static readonly string[] CmdKillAll = new string[4] { "/ka", "/killall", "/killallplayers", "/killplayerall" };

			private static readonly string[] CmdReviveAll = new string[4] { "/ra", "/reviveall", "/reviveallplayers", "/reviveplayerall" };

			private static readonly string[] CmdHealAll = new string[4] { "/ha", "/healall", "/healallplayers", "/healplayerall" };

			private static readonly string[] CmdSummonAll = new string[4] { "/sa", "/summonall", "/summonallplayers", "/summonplayerall" };

			private static readonly string[] CmdTruckAll = new string[4] { "/ta", "/truckall", "/truckallplayers", "/returntotruckall" };

			private static void Prefix(ChatManager __instance)
			{
				if (IsNullUnity(__instance))
				{
					LogWarn("MessageSend Prefix __instance null");
				}
				else if (IsNullUnity(__instance.playerAvatar))
				{
					LogWarn("MessageSend Prefix playerAvatar missing");
				}
				else
				{
					if (!__instance.playerAvatar.isLocal || !IsMasterClientOrSingleplayer())
					{
						return;
					}
					string chatMessage = __instance.chatMessage;
					LogInfo("Chat received raw=" + (chatMessage ?? "<null>"));
					if (string.IsNullOrWhiteSpace(chatMessage))
					{
						return;
					}
					string text = chatMessage.Trim().ToLowerInvariant();
					if (text.StartsWith("/"))
					{
						LogInfo("Chat normalized cmd=" + text);
					}
					if (IsAnyCmd(text, CmdKillAll))
					{
						LogInfo("Command KillAll received");
						ExecuteForAll("KillAll", delegate(PlayerAvatar p)
						{
							PlayerActions.Kill(p);
						});
						__instance.chatMessage = "";
					}
					else if (IsAnyCmd(text, CmdReviveAll))
					{
						LogInfo("Command ReviveAll received");
						ExecuteForAll("ReviveAll", delegate(PlayerAvatar p)
						{
							PlayerActions.Revive(p);
						});
						__instance.chatMessage = "";
					}
					else if (IsAnyCmd(text, CmdHealAll))
					{
						LogInfo("Command HealAll received");
						ExecuteForAll("HealAll", delegate(PlayerAvatar p)
						{
							PlayerActions.Heal(p);
						});
						__instance.chatMessage = "";
					}
					else if (IsAnyCmd(text, CmdSummonAll))
					{
						LogInfo("Command SummonAll received");
						ExecuteForAll("SummonAll", delegate(PlayerAvatar p)
						{
							PlayerActions.SummonToLocal(p);
						});
						__instance.chatMessage = "";
					}
					else if (IsAnyCmd(text, CmdTruckAll))
					{
						LogInfo("Command TruckAll received");
						ExecuteForAll("TruckAll", delegate(PlayerAvatar p)
						{
							PlayerActions.ReturnToTruck(p);
						});
						__instance.chatMessage = "";
					}
				}
			}
		}

		internal static AllPlayerCommandsPlugin Instance;

		internal static ManualLogSource LogSource;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			Instance = this;
			LogSource = ((BaseUnityPlugin)this).Logger;
			LogInfo("Loaded");
			_harmony = new Harmony("jp.repo.allplayercommands.harmony");
			_harmony.PatchAll();
			LogInfo("Harmony patched");
		}

		internal static void LogInfo(string message)
		{
			Debug.Log((object)("[AllPlayerCommands] " + message));
			ManualLogSource logSource = LogSource;
			if (logSource != null)
			{
				logSource.LogInfo((object)message);
			}
		}

		internal static void LogWarn(string message)
		{
			Debug.LogWarning((object)("[AllPlayerCommands] " + message));
			ManualLogSource logSource = LogSource;
			if (logSource != null)
			{
				logSource.LogWarning((object)message);
			}
		}

		internal static void LogError(string message)
		{
			Debug.LogError((object)("[AllPlayerCommands] " + message));
			ManualLogSource logSource = LogSource;
			if (logSource != null)
			{
				logSource.LogError((object)message);
			}
		}

		private static bool IsNullUnity(object obj)
		{
			if (obj == null)
			{
				return true;
			}
			Object val = (Object)((obj is Object) ? obj : null);
			if (val != null)
			{
				return val == (Object)null;
			}
			return false;
		}

		private static bool IsCmd(string msg, string cmd)
		{
			if (string.IsNullOrWhiteSpace(msg) || string.IsNullOrWhiteSpace(cmd))
			{
				return false;
			}
			if (msg == cmd)
			{
				return true;
			}
			if (msg.StartsWith(cmd + " "))
			{
				return true;
			}
			return false;
		}

		private static bool IsAnyCmd(string msg, IEnumerable<string> cmds)
		{
			foreach (string cmd in cmds)
			{
				if (IsCmd(msg, cmd))
				{
					return true;
				}
			}
			return false;
		}

		private static string PlayerLabel(PlayerAvatar avatar)
		{
			if (IsNullUnity(avatar))
			{
				return "<NullPlayer>";
			}
			try
			{
				string text = SemiFunc.PlayerGetName(avatar);
				string text2 = SemiFunc.PlayerGetSteamID(avatar);
				return text + " (" + text2 + ")";
			}
			catch
			{
				try
				{
					return ((Object)avatar).name;
				}
				catch
				{
					return "<UnknownPlayer>";
				}
			}
		}

		private static List<PlayerAvatar> GetAllPlayers()
		{
			//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)
			Dictionary<int, PlayerAvatar> dictionary = new Dictionary<int, PlayerAvatar>();
			try
			{
				PlayerAvatar[] array = Resources.FindObjectsOfTypeAll<PlayerAvatar>();
				foreach (PlayerAvatar val in array)
				{
					if (IsNullUnity(val))
					{
						continue;
					}
					GameObject val2 = null;
					try
					{
						val2 = ((Component)val).gameObject;
					}
					catch
					{
						val2 = null;
					}
					if ((Object)(object)val2 == (Object)null)
					{
						continue;
					}
					try
					{
						Scene scene = val2.scene;
						if (!((Scene)(ref scene)).IsValid() || !((Scene)(ref scene)).isLoaded)
						{
							continue;
						}
					}
					catch
					{
						continue;
					}
					int num = 0;
					try
					{
						num = ((Object)val).GetInstanceID();
					}
					catch
					{
						continue;
					}
					dictionary[num] = val;
				}
			}
			catch (Exception ex)
			{
				LogError("GetAllPlayers failed\n" + ex);
			}
			List<PlayerAvatar> list = dictionary.Values.ToList();
			LogInfo("GetAllPlayers count=" + list.Count);
			for (int j = 0; j < list.Count; j++)
			{
				LogInfo("Player[" + (j + 1) + "] " + PlayerLabel(list[j]));
			}
			return list;
		}

		private static bool IsMasterClientOrSingleplayer()
		{
			try
			{
				return SemiFunc.IsMasterClientOrSingleplayer();
			}
			catch (Exception ex)
			{
				LogError("IsMasterClientOrSingleplayer failed\n" + ex);
				return false;
			}
		}

		private static void ExecuteForAll(string actionName, Action<PlayerAvatar> action)
		{
			DateTime now = DateTime.Now;
			LogInfo("Command start " + actionName);
			if (!IsMasterClientOrSingleplayer())
			{
				LogWarn("Not master or singleplayer aborted " + actionName);
				return;
			}
			PlayerAvatar instance = PlayerAvatar.instance;
			LogInfo("Local " + PlayerLabel(instance));
			bool flag = false;
			try
			{
				flag = SemiFunc.IsMultiplayer();
			}
			catch
			{
				flag = false;
			}
			LogInfo("IsMultiplayer " + flag);
			List<PlayerAvatar> allPlayers = GetAllPlayers();
			LogInfo("Targets " + allPlayers.Count);
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			for (int i = 0; i < allPlayers.Count; i++)
			{
				PlayerAvatar val = allPlayers[i];
				num++;
				if (IsNullUnity(val))
				{
					num3++;
					LogWarn($"[{i + 1}/{allPlayers.Count}] null target skipped");
					continue;
				}
				string text = PlayerLabel(val);
				bool flag2 = false;
				try
				{
					flag2 = val.deadSet;
				}
				catch
				{
					flag2 = false;
				}
				LogInfo($"[{i + 1}/{allPlayers.Count}] begin {actionName} target={text} deadSet={flag2}");
				try
				{
					action(val);
					num2++;
					LogInfo($"[{i + 1}/{allPlayers.Count}] done {actionName} target={text}");
				}
				catch (Exception ex)
				{
					num3++;
					LogError($"[{i + 1}/{allPlayers.Count}] failure {actionName} target={text}" + "\n" + ex);
				}
			}
			TimeSpan timeSpan = DateTime.Now - now;
			LogInfo($"Command end {actionName} total={num} ok={num2} ng={num3} elapsed={timeSpan:hh\\:mm\\:ss\\.fff}");
		}
	}
}