Decompiled source of RepoMeleeContact v0.3.1

BepInEx/plugins/RepoMeleeContact/RepoMeleeContact.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Photon.Pun;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("RepoMeleeContact")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.3.1.0")]
[assembly: AssemblyInformationalVersion("0.3.1")]
[assembly: AssemblyProduct("RepoMeleeContact")]
[assembly: AssemblyTitle("RepoMeleeContact")]
[assembly: AssemblyVersion("0.3.1.0")]
namespace RepoMeleeContact;

internal static class EnemyRegistry
{
	private static readonly List<Component> _list = new List<Component>();

	private static float _nextRefresh;

	internal static List<Component> All => _list;

	internal static void Tick(float interval)
	{
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		if (Time.time < _nextRefresh)
		{
			return;
		}
		_nextRefresh = Time.time + Mathf.Max(0.5f, interval);
		_list.Clear();
		if (GameRefs.EnemyHealth == null)
		{
			return;
		}
		Scene activeScene = SceneManager.GetActiveScene();
		if (!((Scene)(ref activeScene)).IsValid())
		{
			return;
		}
		GameObject[] rootGameObjects = ((Scene)(ref activeScene)).GetRootGameObjects();
		for (int i = 0; i < rootGameObjects.Length; i++)
		{
			if ((Object)(object)rootGameObjects[i] == (Object)null)
			{
				continue;
			}
			Component[] componentsInChildren = rootGameObjects[i].GetComponentsInChildren(GameRefs.EnemyHealth, true);
			for (int j = 0; j < componentsInChildren.Length; j++)
			{
				if ((Object)(object)componentsInChildren[j] != (Object)null)
				{
					_list.Add(componentsInChildren[j]);
				}
			}
		}
	}
}
internal static class GameRefs
{
	internal static Type ItemMelee;

	internal static Type HurtCollider;

	internal static Type EnemyHealth;

	internal static Type PlayerHealth;

	internal static Type PhysGrabObject;

	internal static Type ItemBattery;

	private static MethodInfo _hurtColliderGet;

	private static MethodInfo _enemyHurt;

	private static MethodInfo _swingHitRpc;

	private static FieldInfo _enemyDamage;

	private static FieldInfo _dead;

	private static FieldInfo _healthCurrent;

	private static FieldInfo _heldByLocal;

	private static FieldInfo _photonView;

	internal static string LastColliderError;

	internal static bool Ready { get; private set; }

	internal static MethodInfo SwingHitRpc => _swingHitRpc;

	internal static void Init()
	{
		ItemMelee = FindType("ItemMelee");
		HurtCollider = FindType("HurtCollider");
		EnemyHealth = FindType("EnemyHealth");
		PlayerHealth = FindType("PlayerHealth");
		PhysGrabObject = FindType("PhysGrabObject");
		ItemBattery = FindType("ItemBattery");
		if (ItemMelee != null)
		{
			_hurtColliderGet = ItemMelee.GetMethod("HurtColliderGet", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			_swingHitRpc = ItemMelee.GetMethod("EnemyOrPVPSwingHitRPC", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		}
		if (HurtCollider != null)
		{
			_enemyDamage = HurtCollider.GetField("enemyDamage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		}
		if (EnemyHealth != null)
		{
			_enemyHurt = EnemyHealth.GetMethod("Hurt", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[2]
			{
				typeof(int),
				typeof(Vector3)
			}, null);
			_dead = EnemyHealth.GetField("dead", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			_healthCurrent = EnemyHealth.GetField("healthCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			_photonView = EnemyHealth.GetField("photonView", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		}
		if (PhysGrabObject != null)
		{
			_heldByLocal = PhysGrabObject.GetField("heldByLocalPlayer", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		}
		Ready = ItemMelee != null && EnemyHealth != null && _enemyHurt != null;
		LogSummary();
	}

	private static void LogSummary()
	{
		if (Plugin.Log != null)
		{
			Plugin.Log.LogInfo((object)$"[refs] melee={ItemMelee != null} hurt={_enemyHurt != null} enemy={EnemyHealth != null} player={PlayerHealth != null} grab={PhysGrabObject != null} rpc={_swingHitRpc != null}");
			if (!Ready)
			{
				Plugin.Log.LogWarning((object)"[refs] 关键类型缺失,接触伤害不会生效。");
			}
		}
	}

	private static Type FindType(string name)
	{
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly in assemblies)
		{
			try
			{
				Type type = assembly.GetType(name, throwOnError: false);
				if (type != null)
				{
					return type;
				}
			}
			catch
			{
			}
		}
		return null;
	}

	internal static Collider GetHurtCollider(Component item)
	{
		if (_hurtColliderGet == null || (Object)(object)item == (Object)null)
		{
			return null;
		}
		try
		{
			object? obj = _hurtColliderGet.Invoke(item, null);
			object? obj2 = ((obj is Collider) ? obj : null);
			if ((Object)obj2 == (Object)null)
			{
				LastColliderError = "返回 null";
			}
			return (Collider)obj2;
		}
		catch (Exception ex)
		{
			LastColliderError = ((ex.InnerException != null) ? ex.InnerException.Message : ex.Message);
			return null;
		}
	}

	internal static Collider FindHurtCollider(Component item)
	{
		Collider hurtCollider = GetHurtCollider(item);
		if ((Object)(object)hurtCollider != (Object)null)
		{
			return hurtCollider;
		}
		if (HurtCollider == null || (Object)(object)item == (Object)null)
		{
			return null;
		}
		Collider[] componentsInChildren = item.GetComponentsInChildren<Collider>(true);
		foreach (Collider val in componentsInChildren)
		{
			if ((Object)(object)val != (Object)null && (Object)(object)((Component)val).GetComponent(HurtCollider) != (Object)null)
			{
				return val;
			}
		}
		return null;
	}

	internal static float GetBaseDamage(Component item)
	{
		if ((Object)(object)item == (Object)null || _enemyDamage == null || HurtCollider == null)
		{
			return 0f;
		}
		try
		{
			Component[] componentsInChildren = item.GetComponentsInChildren(HurtCollider, true);
			foreach (Component val in componentsInChildren)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				object value = _enemyDamage.GetValue(val);
				if (value != null)
				{
					float num = Convert.ToSingle(value);
					if (num > 0f)
					{
						return num;
					}
				}
			}
		}
		catch (Exception ex)
		{
			LastColliderError = ex.Message;
		}
		return 0f;
	}

	internal static Component FindEnemyHealth(Component other)
	{
		if (EnemyHealth == null || (Object)(object)other == (Object)null)
		{
			return null;
		}
		try
		{
			return other.GetComponentInParent(EnemyHealth);
		}
		catch
		{
			return null;
		}
	}

	internal static int GetHealth(Component enemyHealth)
	{
		if ((Object)(object)enemyHealth == (Object)null || _healthCurrent == null)
		{
			return -1;
		}
		try
		{
			return Convert.ToInt32(_healthCurrent.GetValue(enemyHealth));
		}
		catch
		{
			return -1;
		}
	}

	internal static bool IsDead(Component enemyHealth)
	{
		if ((Object)(object)enemyHealth == (Object)null || _dead == null)
		{
			return false;
		}
		try
		{
			return (bool)_dead.GetValue(enemyHealth);
		}
		catch
		{
			return false;
		}
	}

	internal static bool IsHeldByLocalPlayer(GameObject go)
	{
		if ((Object)(object)go == (Object)null || PhysGrabObject == null || _heldByLocal == null)
		{
			return false;
		}
		try
		{
			Component component = go.GetComponent(PhysGrabObject);
			if ((Object)(object)component == (Object)null)
			{
				return false;
			}
			return (bool)_heldByLocal.GetValue(component);
		}
		catch
		{
			return false;
		}
	}

	internal static bool SendSwingRpc(GameObject weapon, bool arg)
	{
		if ((Object)(object)weapon == (Object)null)
		{
			return false;
		}
		try
		{
			PhotonView component = weapon.GetComponent<PhotonView>();
			if ((Object)(object)component == (Object)null)
			{
				if (Plugin.Verbose != null && Plugin.Verbose.Value)
				{
					Plugin.Log.LogWarning((object)"[rpc] 武器上没有 PhotonView");
				}
				return false;
			}
			component.RPC("EnemyOrPVPSwingHitRPC", (RpcTarget)2, new object[1] { arg });
			return true;
		}
		catch (Exception ex)
		{
			if (Plugin.Verbose != null && Plugin.Verbose.Value)
			{
				Plugin.Log.LogWarning((object)("[rpc] 发送失败: " + ex.Message));
			}
			return false;
		}
	}

	internal static void ApplyEnemyDamage(Component enemyHealth, int damage, Vector3 dir)
	{
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)enemyHealth == (Object)null || damage <= 0)
		{
			return;
		}
		bool isMasterClient = PhotonNetwork.IsMasterClient;
		int health = GetHealth(enemyHealth);
		try
		{
			_enemyHurt.Invoke(enemyHealth, new object[2] { damage, dir });
		}
		catch (Exception ex)
		{
			if (Plugin.Verbose != null && Plugin.Verbose.Value)
			{
				Plugin.Log.LogWarning((object)("[dmg] Hurt 调用异常: " + ((ex.InnerException != null) ? ex.InnerException.Message : ex.Message)));
			}
		}
		int health2 = GetHealth(enemyHealth);
		if (health >= 0 && health2 == health)
		{
			PhotonView val = (PhotonView)((_photonView == null) ? null : /*isinst with value type is only supported in some contexts*/);
			if ((Object)(object)val != (Object)null)
			{
				val.RPC("HurtRPC", (RpcTarget)2, new object[2] { damage, dir });
				if (Plugin.Verbose != null && Plugin.Verbose.Value)
				{
					Plugin.Log.LogInfo((object)"[dmg] 本地 Hurt 未扣血,已补发 HurtRPC(MasterClient)");
				}
			}
			else if (Plugin.Verbose != null && Plugin.Verbose.Value)
			{
				Plugin.Log.LogWarning((object)"[dmg] 本地 Hurt 未扣血且无 PhotonView");
			}
		}
		if (Plugin.Verbose != null && Plugin.Verbose.Value)
		{
			Plugin.Log.LogInfo((object)$"[dmg] master={isMasterClient} {((Object)enemyHealth).name} hp {health} -> {health2} (dmg {damage})");
		}
	}
}
internal class MeleeContactEmitter : MonoBehaviour
{
	private float _nextTick;

	private float _nextDiag;

	private readonly Dictionary<int, float> _nextHitTime = new Dictionary<int, float>();

	private float _nextRpc;

	private void Update()
	{
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: 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_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: 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_00c0: 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_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0166: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0175: Unknown result type (might be due to invalid IL or missing references)
		//IL_017a: Unknown result type (might be due to invalid IL or missing references)
		//IL_017f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0181: Unknown result type (might be due to invalid IL or missing references)
		//IL_0182: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02de: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0401: Unknown result type (might be due to invalid IL or missing references)
		if (!Plugin.Enabled.Value || !GameRefs.Ready || Time.time < _nextTick)
		{
			return;
		}
		_nextTick = Time.time + Mathf.Max(0.05f, Plugin.TickInterval.Value);
		bool flag = GameRefs.IsHeldByLocalPlayer(((Component)this).gameObject);
		if ((Plugin.RequireHeld.Value && !flag) || (!flag && !PhotonNetwork.IsMasterClient))
		{
			return;
		}
		EnemyRegistry.Tick(2f);
		Collider val = GameRefs.FindHurtCollider((Component)(object)this);
		Vector3 val2 = ((Component)this).transform.position;
		float num = Plugin.DetectRadius.Value;
		if ((Object)(object)val != (Object)null)
		{
			Bounds bounds = val.bounds;
			Vector3 val3 = ((Bounds)(ref bounds)).size;
			if (((Vector3)(ref val3)).sqrMagnitude > 0.0001f)
			{
				val2 = ((Bounds)(ref bounds)).center;
				float num2 = num;
				val3 = ((Bounds)(ref bounds)).extents;
				num = Mathf.Max(num2, ((Vector3)(ref val3)).magnitude + 0.05f);
			}
		}
		float num3 = GameRefs.GetBaseDamage((Component)(object)this);
		if (num3 <= 0f)
		{
			num3 = Plugin.FallbackDamage.Value;
		}
		int num4 = Mathf.Max(1, Mathf.RoundToInt(num3 * Plugin.DamageMultiplier.Value));
		int num5 = 0;
		int num6 = 0;
		int num7 = 0;
		int num8 = 0;
		Component val4 = null;
		float num9 = float.MaxValue;
		List<Component> all = EnemyRegistry.All;
		for (int i = 0; i < all.Count; i++)
		{
			Component val5 = all[i];
			if ((Object)(object)val5 == (Object)null || GameRefs.IsDead(val5))
			{
				continue;
			}
			Vector3 val6 = val5.transform.position + Vector3.up * 0.6f;
			float num10 = Vector3.Distance(val2, val6);
			if (num10 > num)
			{
				continue;
			}
			num5++;
			if (Plugin.RequireLineOfSight.Value && !HasLine(val2, val6, num10, val5))
			{
				num7++;
			}
			else if (Plugin.UseRpcForwarding.Value)
			{
				if (num10 < num9)
				{
					num9 = num10;
					val4 = val5;
				}
			}
			else if (Plugin.OnlyNearest.Value)
			{
				if (num10 < num9)
				{
					num9 = num10;
					val4 = val5;
				}
			}
			else if (TryDamage(val5, num4, val2, val6))
			{
				num6++;
			}
			else
			{
				num8++;
			}
		}
		if (Plugin.UseRpcForwarding.Value)
		{
			if ((Object)(object)val4 != (Object)null)
			{
				if (Time.time >= _nextRpc)
				{
					_nextRpc = Time.time + Mathf.Clamp(Plugin.PerTargetCooldown.Value, 0.05f, 5f);
					if (GameRefs.SendSwingRpc(((Component)this).gameObject, arg: false))
					{
						num6 = 1;
						if (Plugin.Verbose.Value)
						{
							Plugin.Log.LogInfo((object)$"[rpc] 请求房主结算 -> {((Object)val4).name} d={num9:F1}");
						}
					}
				}
				else
				{
					num8++;
				}
			}
		}
		else if (Plugin.OnlyNearest.Value && (Object)(object)val4 != (Object)null)
		{
			Vector3 target = val4.transform.position + Vector3.up * 0.6f;
			if (TryDamage(val4, num4, val2, target))
			{
				num6 = 1;
			}
			else
			{
				num8++;
			}
		}
		if (!Plugin.Diagnostics.Value || !(Time.time >= _nextDiag))
		{
			return;
		}
		_nextDiag = Time.time + 2f;
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.AppendFormat("[diag] master={0} held={1} col={2} dmg={3} r={4:F2} cd={5:F2} total={6} inRange={7} applied={8} losBlock={9} cool={10}", PhotonNetwork.IsMasterClient, flag, (Object)(object)val != (Object)null, num4, num, Plugin.PerTargetCooldown.Value, all.Count, num5, num6, num7, num8);
		int num11 = 0;
		for (int j = 0; j < all.Count; j++)
		{
			if (num11 >= 3)
			{
				break;
			}
			Component val7 = all[j];
			if (!((Object)(object)val7 == (Object)null))
			{
				float num12 = Vector3.Distance(val2, val7.transform.position);
				stringBuilder.Append(" | ").Append(((Object)val7).name).Append(" d=")
					.Append(num12.ToString("F1"));
				if (GameRefs.IsDead(val7))
				{
					stringBuilder.Append("(dead)");
				}
				num11++;
			}
		}
		Plugin.Log.LogInfo((object)stringBuilder.ToString());
	}

	private bool TryDamage(Component eh, int damage, Vector3 center, Vector3 target)
	{
		//IL_006b: 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)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		int instanceID = ((Object)eh).GetInstanceID();
		if (_nextHitTime.TryGetValue(instanceID, out var value) && Time.time < value)
		{
			return false;
		}
		if (_nextHitTime.Count > 300)
		{
			_nextHitTime.Clear();
		}
		_nextHitTime[instanceID] = Time.time + Mathf.Clamp(Plugin.PerTargetCooldown.Value, 0.05f, 5f);
		GameRefs.ApplyEnemyDamage(eh, damage, target - center);
		if (Plugin.Verbose.Value)
		{
			Plugin.Log.LogInfo((object)$"[hit] {((Object)this).name} -> {((Object)eh).name} x{damage}");
		}
		return true;
	}

	private bool HasLine(Vector3 from, Vector3 to, float dist, Component enemy)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0001: 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_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)
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		Vector3 val = to - from;
		float magnitude = ((Vector3)(ref val)).magnitude;
		if (magnitude < 0.01f)
		{
			return true;
		}
		RaycastHit val2 = default(RaycastHit);
		if (!Physics.Linecast(from + val / magnitude * 0.45f, to, ref val2, -1, (QueryTriggerInteraction)1))
		{
			return true;
		}
		if ((Object)(object)((RaycastHit)(ref val2)).collider == (Object)null)
		{
			return true;
		}
		if (((RaycastHit)(ref val2)).distance < 0.5f)
		{
			return true;
		}
		if (((Component)((RaycastHit)(ref val2)).collider).transform.IsChildOf(((Component)this).transform))
		{
			return true;
		}
		if ((Object)(object)GameRefs.FindEnemyHealth((Component)(object)((RaycastHit)(ref val2)).collider) == (Object)(object)enemy)
		{
			return true;
		}
		if (((RaycastHit)(ref val2)).distance >= dist - 0.6f)
		{
			return true;
		}
		return false;
	}
}
[BepInPlugin("local.repomeleecontact", "RepoMeleeContact", "0.3.1")]
[BepInProcess("REPO.exe")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "local.repomeleecontact";

	internal static Plugin Instance;

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<float> TickInterval;

	internal static ConfigEntry<bool> RequireHeld;

	internal static ConfigEntry<float> DetectRadius;

	internal static ConfigEntry<bool> Diagnostics;

	internal static ConfigEntry<bool> RequireLineOfSight;

	internal static ConfigEntry<bool> OnlyNearest;

	internal static ConfigEntry<bool> NoMeleeBatteryDrain;

	internal static ConfigEntry<bool> UseRpcForwarding;

	internal static ConfigEntry<float> DamageMultiplier;

	internal static ConfigEntry<float> PerTargetCooldown;

	internal static ConfigEntry<int> FallbackDamage;

	internal static ConfigEntry<bool> DisableFriendlyFire;

	internal static ConfigEntry<bool> Verbose;

	private static Harmony _harmony;

	internal static float SwingRpcUntil;

	private void Awake()
	{
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Expected O, but got Unknown
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Expected O, but got Unknown
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Expected O, but got Unknown
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c0: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1-通用", "启用接触伤害", true, "开启后,近战武器贴着怪物就会持续造成伤害,不需要挥砍。");
		TickInterval = ((BaseUnityPlugin)this).Config.Bind<float>("1-通用", "结算间隔(秒)", 0.3f, new ConfigDescription("每隔多久检测一次接触。越小越顺滑,但判定次数越多。", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.05f, 2f), Array.Empty<object>()));
		RequireHeld = ((BaseUnityPlugin)this).Config.Bind<bool>("1-通用", "仅在手持时生效", true, "关掉后,武器丢在地上也能造成伤害(只在房主端生效,容易误伤)。");
		RequireLineOfSight = ((BaseUnityPlugin)this).Config.Bind<bool>("1-通用", "需要视线", true, "开启后隔着墙不会生效;关掉可以隔墙打(不推荐)。");
		OnlyNearest = ((BaseUnityPlugin)this).Config.Bind<bool>("1-通用", "只打最近的一个", true, "开启时只打最近的一个目标;关掉后会同时攻击范围内所有敌人。");
		DetectRadius = ((BaseUnityPlugin)this).Config.Bind<float>("1-通用", "检测半径(米)", 2.5f, new ConfigDescription("以武器为中心搜索怪物的半径,越大越容易命中。", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 6f), Array.Empty<object>()));
		UseRpcForwarding = ((BaseUnityPlugin)this).Config.Bind<bool>("1-通用", "联机转发给房主", false, "开启后不再本地扣血,而是调用武器原生命中 RPC 让房主结算;单人时请关掉。");
		NoMeleeBatteryDrain = ((BaseUnityPlugin)this).Config.Bind<bool>("1-通用", "近战不耗电", true, "开启后近战武器不再消耗电量,只有枪械和无人机耗电。");
		DamageMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("2-伤害", "伤害倍率", 1f, new ConfigDescription("单次结算的伤害 = 武器单次挥砍伤害 × 此倍率。", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 20f), Array.Empty<object>()));
		PerTargetCooldown = ((BaseUnityPlugin)this).Config.Bind<float>("2-伤害", "同一目标冷却(秒)", 0.5f, new ConfigDescription("同一个怪物两次受伤的最小间隔,防止每帧重复结算。", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.05f, 5f), Array.Empty<object>()));
		FallbackDamage = ((BaseUnityPlugin)this).Config.Bind<int>("2-伤害", "兜底伤害", 50, "读不到武器自带伤害时使用的数值。");
		DisableFriendlyFire = ((BaseUnityPlugin)this).Config.Bind<bool>("3-友伤", "近战关闭队员伤害", false, "开启后,近战挥砍不再对队友造成伤害,方便乱挥。只影响近战,不改变怪物伤害。");
		Verbose = ((BaseUnityPlugin)this).Config.Bind<bool>("4-调试", "详细日志", false, "开启后每次命中都会写日志,排查问题用。");
		Diagnostics = ((BaseUnityPlugin)this).Config.Bind<bool>("4-调试", "诊断日志", false, "开启后每 2 秒输出一次状态(是否手持、半径、附近怪物数),用于确认判定是否生效。");
		GameRefs.Init();
		((Component)this).gameObject.AddComponent<ItemScanner>();
		ApplyPatches();
		Log.LogInfo((object)"RepoMeleeContact 0.3.1 已加载。");
	}

	private static void ApplyPatches()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Expected O, but got Unknown
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Expected O, but got Unknown
		//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Expected O, but got Unknown
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a8: Expected O, but got Unknown
		try
		{
			_harmony = new Harmony("local.repomeleecontact");
			MethodInfo swingHitRpc = GameRefs.SwingHitRpc;
			if (swingHitRpc != null)
			{
				_harmony.Patch((MethodBase)swingHitRpc, new HarmonyMethod(typeof(Plugin).GetMethod("SwingRpcPrefix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				Log.LogInfo((object)"[patch] 已挂接挥砍命中 RPC,用于友伤保护。");
			}
			else
			{
				Log.LogWarning((object)"[patch] 未找到 EnemyOrPVPSwingHitRPC,友伤保护不可用。");
			}
			Type playerHealth = GameRefs.PlayerHealth;
			if (playerHealth != null)
			{
				int num = 0;
				string[] array = new string[2] { "HurtOther", "Hurt" };
				foreach (string name in array)
				{
					MethodInfo method = playerHealth.GetMethod(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (!(method == null))
					{
						_harmony.Patch((MethodBase)method, new HarmonyMethod(typeof(Plugin).GetMethod("PlayerHurtPrefix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						num++;
					}
				}
				Log.LogInfo((object)("[patch] 玩家受伤入口已挂接 " + num + " 处。"));
			}
			else
			{
				Log.LogWarning((object)"[patch] 未找到 PlayerHealth,友伤保护不可用。");
			}
			Type itemBattery = GameRefs.ItemBattery;
			if (itemBattery != null)
			{
				int num2 = 0;
				string[] array = new string[3] { "Drain", "DrainContinuous", "TriggerSmallBatteryDeduction" };
				foreach (string name2 in array)
				{
					MethodInfo method2 = itemBattery.GetMethod(name2, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (!(method2 == null))
					{
						_harmony.Patch((MethodBase)method2, new HarmonyMethod(typeof(Plugin).GetMethod("BatteryDrainPrefix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						num2++;
					}
				}
				Log.LogInfo((object)("[patch] 近战不耗电已挂接 " + num2 + " 处。"));
			}
			else
			{
				Log.LogWarning((object)"[patch] 未找到 ItemBattery,近战不耗电不可用。");
			}
		}
		catch (Exception ex)
		{
			Log.LogError((object)("[patch] 应用补丁失败: " + ex));
		}
	}

	private static void SwingRpcPrefix()
	{
		SwingRpcUntil = Time.time + 0.25f;
	}

	private static bool BatteryDrainPrefix(Component __instance)
	{
		if (!NoMeleeBatteryDrain.Value)
		{
			return true;
		}
		if ((Object)(object)__instance == (Object)null || GameRefs.ItemMelee == null)
		{
			return true;
		}
		if ((Object)(object)__instance.GetComponentInParent(GameRefs.ItemMelee) == (Object)null)
		{
			return true;
		}
		return false;
	}

	private static bool PlayerHurtPrefix()
	{
		if (!DisableFriendlyFire.Value)
		{
			return true;
		}
		if (Time.time > SwingRpcUntil)
		{
			return true;
		}
		if (Verbose.Value)
		{
			Log.LogInfo((object)"[ff] 已拦截一次近战友伤。");
		}
		return false;
	}
}
internal class ItemScanner : MonoBehaviour
{
	private float _nextScan;

	private void Update()
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		if (!Plugin.Enabled.Value || Time.time < _nextScan)
		{
			return;
		}
		_nextScan = Time.time + 1f;
		if (GameRefs.ItemMelee == null)
		{
			return;
		}
		Scene activeScene = SceneManager.GetActiveScene();
		if (!((Scene)(ref activeScene)).IsValid())
		{
			return;
		}
		GameObject[] rootGameObjects = ((Scene)(ref activeScene)).GetRootGameObjects();
		foreach (GameObject val in rootGameObjects)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			Component[] componentsInChildren = val.GetComponentsInChildren(GameRefs.ItemMelee, true);
			foreach (Component val2 in componentsInChildren)
			{
				if (!((Object)(object)val2 == (Object)null) && (Object)(object)val2.GetComponent<MeleeContactEmitter>() == (Object)null)
				{
					val2.gameObject.AddComponent<MeleeContactEmitter>();
				}
			}
		}
	}
}