Decompiled source of XH DamageShow EnemyHealthBar v1.0.0

XH_DamageShow&EnemyHealthBar.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
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.0")]
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!");
	}
}
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_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		((WorldSpaceUIChild)this).Start();
		((Graphic)textComponent).color = Color.red;
		((TMP_Text)textComponent).fontSize = 40f;
		((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)
	{
		if ((Object)(object)textComponent == (Object)null)
		{
			textComponent = ((Component)this).GetComponent<TextMeshProUGUI>();
		}
		((TMP_Text)textComponent).text = $"-{damage}";
	}
}
public class WorldSpaceUIHealthText : WorldSpaceUIChild
{
	private TextMeshProUGUI textComponent;

	private float timer = 5f;

	private EnemyHealth targetEnemy;

	private static Dictionary<EnemyHealth, WorldSpaceUIHealthText> activeTexts = new Dictionary<EnemyHealth, WorldSpaceUIHealthText>();

	protected void Awake()
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		textComponent = ((Component)this).GetComponent<TextMeshProUGUI>();
		((Graphic)textComponent).color = Color.red;
		((TMP_Text)textComponent).fontSize = 15f;
		((TMP_Text)textComponent).alignment = (TextAlignmentOptions)514;
	}

	public override void Update()
	{
		//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_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: 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_0070: Unknown result type (might be due to invalid IL or missing references)
		((WorldSpaceUIChild)this).Update();
		if ((Object)(object)targetEnemy != (Object)null)
		{
			Collider component = ((Component)targetEnemy).GetComponent<Collider>();
			float num;
			if (!((Object)(object)component != (Object)null))
			{
				num = 3f;
			}
			else
			{
				Bounds bounds = component.bounds;
				num = ((Bounds)(ref bounds)).size.y + 3f;
			}
			float num2 = num;
			base.worldPosition = ((Component)targetEnemy).transform.position + Vector3.up * num2;
		}
		timer -= Time.deltaTime;
		if (timer <= 0f)
		{
			Cleanup();
		}
		if ((Object)(object)targetEnemy != (Object)null)
		{
			string text = targetEnemy.enemy.EnemyParent.enemyName;
			if (text == null)
			{
				text = "未知敌人";
			}
			((TMP_Text)textComponent).text = $"{text}\r\n{targetEnemy.healthCurrent}/{targetEnemy.health}";
		}
	}

	public void Initialize(EnemyHealth enemy)
	{
		targetEnemy = enemy;
		((TMP_Text)textComponent).text = $"{enemy.healthCurrent}/{enemy.health}";
		if (activeTexts.ContainsKey(enemy))
		{
			activeTexts[enemy].RefreshTimer();
			Object.Destroy((Object)(object)((Component)this).gameObject);
		}
		else
		{
			activeTexts.Add(enemy, this);
		}
	}

	public void RefreshTimer()
	{
		timer = 5f;
		((TMP_Text)textComponent).text = $"{targetEnemy.healthCurrent}/{targetEnemy.health}";
	}

	private void Cleanup()
	{
		if ((Object)(object)targetEnemy != (Object)null)
		{
			activeTexts.Remove(targetEnemy);
		}
		Object.Destroy((Object)(object)((Component)this).gameObject);
	}

	public static void CreateOrUpdate(EnemyHealth enemy)
	{
		if (activeTexts.TryGetValue(enemy, out var value))
		{
			value.RefreshTimer();
		}
		else
		{
			CreateNewText(enemy);
		}
	}

	private static void CreateNewText(EnemyHealth enemy)
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Expected O, but got Unknown
		if (!((Object)(object)WorldSpaceUIParent.instance == (Object)null))
		{
			GameObject val = new GameObject("HealthText", new Type[3]
			{
				typeof(RectTransform),
				typeof(TextMeshProUGUI),
				typeof(WorldSpaceUIHealthText)
			});
			val.transform.SetParent(((Component)WorldSpaceUIParent.instance).transform);
			WorldSpaceUIHealthText component = val.GetComponent<WorldSpaceUIHealthText>();
			component.Initialize(enemy);
		}
	}
}
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_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: 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)
		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>();
		component.Initialize(damage);
		((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, 2.5f, 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)("血条初始化完成,敌人:" + ((Object)enemyHealth).name));
	}

	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()
	{
		//IL_0039: 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_004a: 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)
		if (_isActive)
		{
			if (Time.time - _lastDamageTime > 5f)
			{
				DestroyHealthBar();
				return;
			}
			Debug.DrawLine(((Component)this).transform.position, ((Component)this).transform.position + worldOffset, Color.green);
			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(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), "Hurt")]
	[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)("初始化血条:" + ((Object)__instance).name));
			}
		}
		catch (Exception arg)
		{
			Debug.LogError((object)$"血条初始化失败:{arg}");
		}
	}

	private static float GetEnemyHeight(EnemyHealth enemy)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		Collider component = ((Component)enemy).GetComponent<Collider>();
		float result;
		if (!((Object)(object)component != (Object)null))
		{
			result = 2.5f;
		}
		else
		{
			Bounds bounds = component.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);
		}
	}
}