Decompiled source of XH DamageShow EnemyHealthBar v1.0.2

XH_DamageShow&EnemyHealthBar1.0.2.dll

Decompiled 3 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("REPO MOD")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("微软中国")]
[assembly: AssemblyProduct("REPO MOD")]
[assembly: AssemblyCopyright("Copyright © 微软中国 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("48060547-bfdb-47cd-aac0-71116dcfa012")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace REPO_MOD;

[BepInPlugin("com.XhPlugin.Repo_Mod", "XH_REPO_DamageShow&EnemyHealthBar_Mod", "1.0.1")]
public class Plugin : BaseUnityPlugin
{
	public static ManualLogSource Logger;

	private readonly Harmony _harmony = new Harmony("com.XhPlugin.Repo_Mod");

	private void Awake()
	{
		Logger = ((BaseUnityPlugin)this).Logger;
		_harmony.PatchAll();
		Logger.LogInfo((object)"小海怪物血量MOD加载成功!Xiaohai DamageShow&EnemyHealthBar MOD loaded successfully ! v1.0.2");
	}
}
public class WorldSpaceUIDamageText : WorldSpaceUIChild
{
	private TextMeshProUGUI textComponent;

	private float timer = 1.5f;

	private Vector3 baseWorldPosition;

	private float floatHeight;

	public void Awake()
	{
		textComponent = ((Component)this).GetComponent<TextMeshProUGUI>();
		if ((Object)(object)textComponent == (Object)null)
		{
			textComponent = ((Component)this).gameObject.AddComponent<TextMeshProUGUI>();
		}
	}

	public override void Start()
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		((WorldSpaceUIChild)this).Start();
		((TMP_Text)textComponent).alignment = (TextAlignmentOptions)514;
		baseWorldPosition = base.worldPosition;
	}

	public override void Update()
	{
		//IL_0022: 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_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		((WorldSpaceUIChild)this).Update();
		floatHeight += Time.deltaTime * 0.8f;
		base.worldPosition = baseWorldPosition + Vector3.up * floatHeight;
		timer -= Time.deltaTime;
		((TMP_Text)textComponent).alpha = Mathf.Clamp01(timer * 2f);
		if (timer <= 0f)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
		}
	}

	public void Initialize(int damage, Color color, float fontSize, float time)
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)textComponent == (Object)null)
		{
			textComponent = ((Component)this).GetComponent<TextMeshProUGUI>();
		}
		((TMP_Text)textComponent).text = $"-{damage}";
		((Graphic)textComponent).color = color;
		((TMP_Text)textComponent).fontSize = fontSize;
		timer = time;
	}
}
public static class DamageTextManager
{
	public static void CreateDamageText(Vector3 position, int damage)
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		//IL_0065: 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_008c: 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_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("DamageText", new Type[3]
		{
			typeof(RectTransform),
			typeof(TextMeshProUGUI),
			typeof(WorldSpaceUIDamageText)
		});
		val.transform.SetParent(((Component)WorldSpaceUIParent.instance).transform);
		WorldSpaceUIDamageText component = val.GetComponent<WorldSpaceUIDamageText>();
		if (damage >= 50)
		{
			component.Initialize(damage, Color.red, 43f, 3f);
		}
		else if (damage >= 25)
		{
			component.Initialize(damage, Color.yellow, 35f, 2f);
		}
		else
		{
			component.Initialize(damage, new Color(1f, 23f / 85f, 0f), 25f, 1.5f);
		}
		((WorldSpaceUIChild)component).worldPosition = position;
		Plugin.Logger.LogInfo((object)$"生成伤害文本:-{damage} 位置:{position}");
	}
}
public class EnemyHealthBar : MonoBehaviour
{
	public class Billboard : MonoBehaviour
	{
		public Vector3 offset;

		private Camera mainCamera;

		private void Start()
		{
			mainCamera = Camera.main;
		}

		private void LateUpdate()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: 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)
			if ((Object)(object)mainCamera != (Object)null)
			{
				((Component)this).transform.rotation = Quaternion.Euler(((Component)mainCamera).transform.eulerAngles.x, ((Component)mainCamera).transform.eulerAngles.y, 0f);
				((Component)this).transform.position = ((Component)this).transform.parent.position + offset;
			}
		}
	}

	private float _lastDamageTime;

	private bool _isActive = true;

	public Vector3 worldOffset = new Vector3(0f, 1.25f, 0f);

	public Vector2 size = new Vector2(20f, 2.5f);

	public Color backgroundColor = new Color(0.3f, 0.3f, 0.3f, 0.9f);

	public Color healthColor = Color.red;

	public Canvas canvas;

	private Image foreground;

	private Image background;

	private EnemyHealth enemyHealth;

	private TextMeshProUGUI healthText;

	private void Awake()
	{
		enemyHealth = ((Component)this).GetComponent<EnemyHealth>();
		InitializeCanvas();
		UpdateHealthDisplay();
		ResetTimer();
		Debug.Log((object)("血条初始化完成,敌人:" + enemyHealth.enemy.EnemyParent.enemyName));
	}

	public void ResetTimer()
	{
		_lastDamageTime = Time.time;
		if (!_isActive)
		{
			ReactivateHealthBar();
		}
	}

	private void ReactivateHealthBar()
	{
		_isActive = true;
		((Component)canvas).gameObject.SetActive(true);
		UpdateHealthDisplay();
		UpdateCanvasVisibility();
	}

	private void DestroyHealthBar()
	{
		_isActive = false;
		((Component)canvas).gameObject.SetActive(false);
	}

	private void InitializeCanvas()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: 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_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("HealthCanvas");
		canvas = val.AddComponent<Canvas>();
		canvas.renderMode = (RenderMode)2;
		((Component)canvas).transform.SetParent(((Component)this).transform);
		((Component)canvas).transform.localPosition = worldOffset;
		RectTransform component = ((Component)canvas).GetComponent<RectTransform>();
		component.sizeDelta = size;
		((Transform)component).localScale = new Vector3(0.03f, 0.03f, 0.03f);
		val.AddComponent<GraphicRaycaster>();
		background = CreateBarElement(((Component)canvas).transform, backgroundColor, "Background");
		foreground = CreateBarElement(((Component)canvas).transform, healthColor, "Foreground");
		foreground.type = (Type)3;
		foreground.fillMethod = (FillMethod)0;
		foreground.fillOrigin = 0;
		Billboard billboard = val.AddComponent<Billboard>();
		billboard.offset = worldOffset;
		CreateHealthText(((Component)canvas).transform);
	}

	private void CreateHealthText(Transform parent)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("HealthText");
		val.transform.SetParent(parent, false);
		healthText = val.AddComponent<TextMeshProUGUI>();
		((TMP_Text)healthText).alignment = (TextAlignmentOptions)514;
		((TMP_Text)healthText).fontSize = 10f;
		((Graphic)healthText).color = Color.white;
		((TMP_Text)healthText).margin = new Vector4(0f, 0f, 0f, 10f);
		RectTransform component = ((Component)healthText).GetComponent<RectTransform>();
		component.anchorMin = new Vector2(0.5f, 1f);
		component.anchorMax = new Vector2(0.5f, 1f);
		component.pivot = new Vector2(0.5f, 0.5f);
		component.anchoredPosition = new Vector2(0f, 5f);
	}

	private void UpdateHealthText()
	{
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)healthText != (Object)null && (Object)(object)enemyHealth != (Object)null)
		{
			((TMP_Text)healthText).text = $"{enemyHealth.healthCurrent}/{enemyHealth.health}";
			if ((float)enemyHealth.healthCurrent / (float)enemyHealth.health < 0.3f)
			{
				((Graphic)healthText).color = Color.yellow;
			}
			else
			{
				((Graphic)healthText).color = Color.white;
			}
		}
	}

	private Image CreateBarElement(Transform parent, Color color, string name)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Expected O, but got Unknown
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Expected O, but got Unknown
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: 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)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject(name);
		val.transform.SetParent(parent, false);
		Texture2D val2 = new Texture2D(1, 1);
		val2.SetPixel(0, 0, Color.white);
		val2.Apply();
		Image val3 = val.AddComponent<Image>();
		val3.sprite = Sprite.Create(val2, new Rect(0f, 0f, 1f, 1f), Vector2.zero);
		((Graphic)val3).color = color;
		RectTransform component = val.GetComponent<RectTransform>();
		component.anchorMin = Vector2.zero;
		component.anchorMax = Vector2.one;
		component.offsetMin = Vector2.zero;
		component.offsetMax = Vector2.zero;
		return val3;
	}

	private void Update()
	{
		if (!((Object)(object)((Component)((Component)this).transform).gameObject == (Object)null) && _isActive)
		{
			if (Time.time - _lastDamageTime > 10f)
			{
				DestroyHealthBar();
				return;
			}
			UpdateHealthDisplay();
			UpdateCanvasVisibility();
		}
	}

	private void UpdateCanvasVisibility()
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		float num = Vector3.Distance(((Component)Camera.main).transform.position, ((Component)this).transform.position);
		float num2 = Mathf.Clamp01(1f - num / 50f);
		((Graphic)foreground).color = new Color(healthColor.r, healthColor.g, healthColor.b, num2);
		((Graphic)background).color = new Color(backgroundColor.r, backgroundColor.g, backgroundColor.b, num2 * 0.8f);
	}

	public void UpdateHealthDisplay()
	{
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)enemyHealth == (Object)null))
		{
			float num = Mathf.Clamp01((float)enemyHealth.healthCurrent / (float)enemyHealth.health);
			foreground.fillAmount = Mathf.Lerp(foreground.fillAmount, num, Time.deltaTime * 10f);
			if (num < 0.3f)
			{
				float num2 = Mathf.PingPong(Time.time * 2f, 1f);
				((Graphic)foreground).color = new Color(1f, num2, num2, ((Graphic)foreground).color.a);
			}
			UpdateHealthText();
		}
	}

	private void OnDestroy()
	{
		if ((Object)(object)canvas != (Object)null)
		{
			Debug.Log((object)("销毁血条:" + ((Object)enemyHealth).name));
			Object.Destroy((Object)(object)((Component)canvas).gameObject);
		}
	}
}
[HarmonyPatch]
public static class Patch
{
	[HarmonyPatch(typeof(HurtCollider), "EnemyHurt")]
	public static class HurtColliderPatch
	{
		[HarmonyPostfix]
		public static void EnemyHurtPatch(HurtCollider __instance, ref Enemy _enemy)
		{
			Plugin.Logger.LogInfo((object)$"敌人{_enemy.EnemyParent.enemyName}受伤,伤害{(__instance.enemyKill ? _enemy.Health.healthCurrent : __instance.enemyDamage)}");
		}
	}

	[HarmonyPatch(typeof(BigMessageUI))]
	internal class BigMessageUIPatch
	{
		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static bool UpdatePrefix(BigMessageUI __instance, ref float ___bigMessageTimer)
		{
			if (___bigMessageTimer > 0f)
			{
				___bigMessageTimer -= Time.deltaTime * 0.5f;
				return false;
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(EnemyHealth), "HurtRPC")]
	[HarmonyPostfix]
	private static void OnHurt(EnemyHealth __instance, int _damage)
	{
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: 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)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		if (_damage <= 0 || (Object)(object)__instance == (Object)null)
		{
			return;
		}
		try
		{
			EnemyHealthBar enemyHealthBar = ((Component)__instance).GetComponent<EnemyHealthBar>();
			if ((Object)(object)enemyHealthBar == (Object)null)
			{
				enemyHealthBar = ((Component)__instance).gameObject.AddComponent<EnemyHealthBar>();
			}
			else
			{
				enemyHealthBar.ResetTimer();
			}
			enemyHealthBar.UpdateHealthDisplay();
			Vector3 position = ((Component)__instance).transform.position + Vector3.up * 1.5f + Random.insideUnitSphere * 0.3f;
			DamageTextManager.CreateDamageText(position, _damage);
		}
		catch (Exception arg)
		{
			Plugin.Logger.LogError((object)$"处理伤害时出错: {arg}");
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(EnemyHealth), "Death")]
	private static void OnDeath(EnemyHealth __instance)
	{
		EnemyHealthBar component = ((Component)__instance).GetComponent<EnemyHealthBar>();
		if ((Object)(object)component != (Object)null)
		{
			Object.Destroy((Object)(object)component);
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(EnemyHealth), "Awake")]
	private static void PostStart(EnemyHealth __instance)
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if ((Object)(object)((Component)__instance).GetComponent<EnemyHealthBar>() == (Object)null)
			{
				EnemyHealthBar enemyHealthBar = ((Component)__instance).gameObject.AddComponent<EnemyHealthBar>();
				enemyHealthBar.worldOffset = new Vector3(0f, GetEnemyHeight(__instance), 0f);
				Plugin.Logger.LogInfo((object)("初始化血条:" + __instance.enemy.EnemyParent.enemyName));
			}
		}
		catch (Exception arg)
		{
			Debug.LogError((object)$"血条初始化失败:{arg}");
		}
	}

	private static float GetEnemyHeight(EnemyHealth enemy)
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		Collider componentInChildren = ((Component)((Component)enemy).transform.parent).GetComponentInChildren<Collider>();
		float result;
		if (!((Object)(object)componentInChildren != (Object)null))
		{
			result = 2.5f;
		}
		else
		{
			Bounds bounds = componentInChildren.bounds;
			result = ((Bounds)(ref bounds)).size.y + 0.5f;
		}
		return result;
	}
}
[HarmonyPatch(typeof(ExtractionPoint))]
public static class ExtractionPoint_Patches
{
	private static bool once;

	[HarmonyPrefix]
	[HarmonyPatch("StateActive")]
	private static void Postfix_MissionText()
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		if (!once)
		{
			once = true;
			SemiFunc.UIFocusText("欢迎使用由CO2赞助的怪物血量MOD by小海", Color.white, AssetManager.instance.colorYellow, 5f);
		}
	}
}