Decompiled source of DisablePvpInShip Standalone v1.0.3

DisablePvpInShip_Standalone.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("DisablePvpInShip_Standalone")]
[assembly: AssemblyDescription("HostFixes DisablePvpInShip Standalone")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3")]
[assembly: AssemblyProduct("DisablePvpInShip_Standalone")]
[assembly: AssemblyTitle("DisablePvpInShip_Standalone")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("1.0.3.0")]
namespace DisablePvpInShip_Standalone;

[BepInPlugin("DisablePvpInShip_Standalone", "DisablePvpInShip_Standalone", "1.0.3")]
public class DisablePvpInShip_StandalonePlugin : BaseUnityPlugin
{
	public const string PLUGIN_GUID = "DisablePvpInShip_Standalone";

	public const string PLUGIN_NAME = "DisablePvpInShip_Standalone";

	public const string PLUGIN_VERSION = "1.0.3";

	public const string PLUGIN_VERSION_FULL = "1.0.3.0";

	private readonly Harmony harmony = new Harmony("DisablePvpInShip_Standalone");

	public static ManualLogSource logger;

	public static ConfigEntry<bool> NoPVP;

	public static DisablePvpInShip_StandalonePlugin Instance;

	private void Awake()
	{
		Instance = this;
		logger = ((BaseUnityPlugin)this).Logger;
		NoPVP = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "NoPVP", false, "Disable PVP completely when you hosting");
		DisablePvpInShip_StandaloneServerReceiveRpcs.Instance = new DisablePvpInShip_StandaloneServerReceiveRpcs();
		harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"DisablePvpInShip Standalone is loaded!");
	}
}
internal class DisablePvpInShip_StandaloneServerReceiveRpcs
{
	public static DisablePvpInShip_StandaloneServerReceiveRpcs Instance;

	public void DamagePlayerFromOtherClientServerRpc(int damageAmount, Vector3 hitDirection, int playerWhoHit, PlayerControllerB instance, __RpcParams RpcParams)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: 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)
		ulong senderClientId = RpcParams.Server.Receive.SenderClientId;
		if (!StartOfRound.Instance.ClientPlayerList.TryGetValue(senderClientId, out var value))
		{
			DisablePvpInShip_StandalonePlugin.logger.LogError((object)$"[DamagePlayerFromOtherClientServerRpc] Failed to get the playerId from senderClientId: {senderClientId}");
			return;
		}
		string playerUsername = StartOfRound.Instance.allPlayerScripts[value].playerUsername;
		if (DisablePvpInShip_StandalonePlugin.NoPVP.Value)
		{
			DisablePvpInShip_StandalonePlugin.logger.LogInfo((object)$"Player #{value} ({playerUsername}) tried to damage ({instance.playerUsername}) for ({damageAmount}) damage.");
			return;
		}
		if ((Object)(object)StartOfRound.Instance.shipInnerRoomBounds != (Object)null)
		{
			Bounds bounds = StartOfRound.Instance.shipInnerRoomBounds.bounds;
			if (((Bounds)(ref bounds)).Contains(((Component)instance).transform.position))
			{
				DisablePvpInShip_StandalonePlugin.logger.LogInfo((object)$"Player #{value} ({playerUsername}) tried to damage ({instance.playerUsername}) inside the ship.");
				return;
			}
		}
		instance.DamagePlayerFromOtherClientServerRpc(damageAmount, hitDirection, playerWhoHit);
	}
}
[HarmonyPatch]
internal static class Patches
{
	[HarmonyPatch(typeof(PlayerControllerB), "__rpc_handler_638895557")]
	[HarmonyTranspiler]
	public static IEnumerable<CodeInstruction> RedirectDamageRpc(IEnumerable<CodeInstruction> instructions)
	{
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Expected O, but got Unknown
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Expected O, but got Unknown
		bool flag = false;
		int num = -1;
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].opcode == OpCodes.Callvirt && list[i].operand is MethodInfo { Name: "DamagePlayerFromOtherClientServerRpc" })
			{
				num = i;
				flag = true;
				break;
			}
		}
		if (flag)
		{
			list.Insert(num, new CodeInstruction(OpCodes.Ldarg_0, (object)null));
			list.Insert(num + 1, new CodeInstruction(OpCodes.Ldarg_2, (object)null));
			list[num + 2].operand = AccessTools.Method(typeof(DisablePvpInShip_StandaloneServerReceiveRpcs), "DamagePlayerFromOtherClientServerRpc", (Type[])null, (Type[])null);
		}
		else
		{
			DisablePvpInShip_StandalonePlugin.logger.LogError((object)"Could not patch DamagePlayerFromOtherClientServerRpc");
		}
		return list.AsEnumerable();
	}
}