Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of EnemyHealthBar v1.0.0
RepoEnemyHealthBar.dll
Decompiled 21 minutes agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using ExitGames.Client.Photon; using HarmonyLib; using Microsoft.CodeAnalysis; using Photon.Pun; using Photon.Realtime; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("RepoEnemyHealthBar")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0")] [assembly: AssemblyProduct("RepoEnemyHealthBar")] [assembly: AssemblyTitle("RepoEnemyHealthBar")] [assembly: AssemblyVersion("1.0.0.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [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 RepoEnemyHealthBar { public sealed class EnemyHealthBarRunner : MonoBehaviour { private struct Bar { internal Vector3 Anchor; internal int Current; internal int Max; internal bool Estimate; } private const float MaxDistance = 20f; private const float BarWidth = 90f; private const float BarHeight = 14f; private const float HeadOffset = 0.6f; private readonly List<Bar> _bars = new List<Bar>(); private readonly FieldInfo _healthCurrentField = typeof(EnemyHealth).GetField("healthCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); private readonly FieldInfo _spawnedField = typeof(EnemyParent).GetField("Spawned", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); private readonly FieldInfo _onScreenField = typeof(EnemyOnScreen).GetField("OnScreenLocal", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); private Texture2D _white; private GUIStyle _labelStyle; private bool _subscribed; private bool _wasInRoom; private void OnEnable() { EnsureSubscribed(); } private void OnDisable() { if (_subscribed && PhotonNetwork.NetworkingClient != null) { PhotonNetwork.NetworkingClient.EventReceived -= EnemyHealthSync.OnEventReceived; } _subscribed = false; } private void EnsureSubscribed() { if (!_subscribed && PhotonNetwork.NetworkingClient != null) { PhotonNetwork.NetworkingClient.EventReceived += EnemyHealthSync.OnEventReceived; _subscribed = true; } } private void Update() { //IL_01ca: Unknown result type (might be due to invalid IL or missing references) //IL_01bc: Unknown result type (might be due to invalid IL or missing references) //IL_01cf: Unknown result type (might be due to invalid IL or missing references) //IL_01d1: Unknown result type (might be due to invalid IL or missing references) //IL_01d3: Unknown result type (might be due to invalid IL or missing references) //IL_01dd: Unknown result type (might be due to invalid IL or missing references) //IL_01e2: Unknown result type (might be due to invalid IL or missing references) //IL_01e7: Unknown result type (might be due to invalid IL or missing references) //IL_024e: Unknown result type (might be due to invalid IL or missing references) //IL_0250: Unknown result type (might be due to invalid IL or missing references) //IL_022b: Unknown result type (might be due to invalid IL or missing references) //IL_0230: Unknown result type (might be due to invalid IL or missing references) EnsureSubscribed(); bool inRoom = PhotonNetwork.InRoom; if (inRoom != _wasInRoom) { _wasInRoom = inRoom; EnemyHealthSync.Reset(); if (inRoom) { EnemyHealthSync.SendInitialRequest(); } } if (Plugin.Enabled == null || !Plugin.Enabled.Value) { _bars.Clear(); return; } if (inRoom && PhotonNetwork.IsMasterClient) { EnemyHealthSync.EnsureHostFlag(); } Camera val = (((Object)(object)CameraUtils.Instance != (Object)null) ? CameraUtils.Instance.MainCamera : Camera.main); if ((Object)(object)val == (Object)null) { _bars.Clear(); return; } _bars.Clear(); bool flag = GameManager.Multiplayer(); bool flag2 = !flag || PhotonNetwork.IsMasterClient || EnemyHealthSync.HostHasMod(); EnemyHealth[] array = Object.FindObjectsByType<EnemyHealth>((FindObjectsSortMode)0); foreach (EnemyHealth val2 in array) { if ((Object)(object)val2 == (Object)null) { continue; } Enemy component = ((Component)val2).GetComponent<Enemy>(); if ((Object)(object)component == (Object)null || _spawnedField == null) { continue; } EnemyParent componentInParent = ((Component)component).GetComponentInParent<EnemyParent>(); if ((Object)(object)componentInParent == (Object)null || !(bool)_spawnedField.GetValue(componentInParent)) { continue; } int max = EnemyHealthSync.MaxOf(val2); if (max <= 0) { continue; } int viewId = EnemyHealthSync.ViewIdOf(val2); int current; bool estimate; if (flag2) { if (flag && !PhotonNetwork.IsMasterClient) { if (!EnemyHealthSync.TryGetAuthoritative(viewId, out current, out max)) { continue; } } else { current = ((_healthCurrentField != null) ? ((int)_healthCurrentField.GetValue(val2)) : max); } estimate = false; } else { estimate = true; if (!EnemyHealthSync.TryGetEstimate(viewId, out current)) { current = max; EnemyHealthSync.SetEstimate(val2, current); } } Vector3 val3 = (((Object)(object)component.CenterTransform != (Object)null) ? component.CenterTransform.position : ((Component)val2).transform.position); val3 += Vector3.up * 0.6f; if (!Plugin.IsAllSpawnedMode()) { EnemyOnScreen component2 = ((Component)component).GetComponent<EnemyOnScreen>(); if ((Object)(object)component2 == (Object)null || _onScreenField == null || !(bool)_onScreenField.GetValue(component2) || Vector3.Distance(((Component)val).transform.position, val3) > 20f) { continue; } } _bars.Add(new Bar { Anchor = val3, Current = current, Max = max, Estimate = estimate }); } } private static Vector2 GetScreenScale(Camera camera) { //IL_00a0: 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) float num = 0f; float num2 = 0f; if ((Object)(object)RenderTextureMain.instance != (Object)null && (Object)(object)RenderTextureMain.instance.renderTexture != (Object)null) { num = ((Texture)RenderTextureMain.instance.renderTexture).width; num2 = ((Texture)RenderTextureMain.instance.renderTexture).height; } else if ((Object)(object)camera != (Object)null && (Object)(object)camera.targetTexture != (Object)null) { num = ((Texture)camera.targetTexture).width; num2 = ((Texture)camera.targetTexture).height; } if (num <= 0f || num2 <= 0f || Screen.width <= 0 || Screen.height <= 0) { return Vector2.one; } return new Vector2((float)Screen.width / num, (float)Screen.height / num2); } private void OnGUI() { //IL_005d: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Expected O, but got Unknown //IL_006f: Unknown result type (might be due to invalid IL or missing references) //IL_00c6: Unknown result type (might be due to invalid IL or missing references) //IL_00cb: Unknown result type (might be due to invalid IL or missing references) //IL_00d3: 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) //IL_009c: 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_00b0: Expected O, but got Unknown //IL_00bb: Unknown result type (might be due to invalid IL or missing references) //IL_011c: Unknown result type (might be due to invalid IL or missing references) //IL_0121: Unknown result type (might be due to invalid IL or missing references) //IL_0126: Unknown result type (might be due to invalid IL or missing references) //IL_0131: Unknown result type (might be due to invalid IL or missing references) //IL_0142: Unknown result type (might be due to invalid IL or missing references) //IL_014a: Unknown result type (might be due to invalid IL or missing references) //IL_0160: Unknown result type (might be due to invalid IL or missing references) //IL_016e: Unknown result type (might be due to invalid IL or missing references) //IL_0179: Unknown result type (might be due to invalid IL or missing references) //IL_018f: 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_01bc: Unknown result type (might be due to invalid IL or missing references) //IL_01c6: Unknown result type (might be due to invalid IL or missing references) //IL_0247: Unknown result type (might be due to invalid IL or missing references) //IL_0292: Unknown result type (might be due to invalid IL or missing references) //IL_022c: Unknown result type (might be due to invalid IL or missing references) //IL_0211: Unknown result type (might be due to invalid IL or missing references) //IL_02e0: Unknown result type (might be due to invalid IL or missing references) if (Plugin.Enabled == null || !Plugin.Enabled.Value || _bars.Count == 0) { return; } Camera val = (((Object)(object)CameraUtils.Instance != (Object)null) ? CameraUtils.Instance.MainCamera : Camera.main); if ((Object)(object)val == (Object)null) { return; } if ((Object)(object)_white == (Object)null) { _white = new Texture2D(1, 1, (TextureFormat)4, false); _white.SetPixel(0, 0, Color.white); _white.Apply(); } if (_labelStyle == null) { _labelStyle = new GUIStyle(GUI.skin.label) { fontSize = 12, alignment = (TextAnchor)4 }; _labelStyle.normal.textColor = Color.white; } Vector2 screenScale = GetScreenScale(val); int num = Mathf.Max(10, Mathf.RoundToInt(12f * screenScale.y)); if (_labelStyle.fontSize != num) { _labelStyle.fontSize = num; } Rect val3 = default(Rect); foreach (Bar bar in _bars) { Vector3 val2 = val.WorldToScreenPoint(bar.Anchor); val2.x *= screenScale.x; val2.y *= screenScale.y; if (!(val2.z <= 0f)) { float num2 = 90f * screenScale.x; float num3 = 14f * screenScale.y; ((Rect)(ref val3))..ctor(val2.x - num2 * 0.5f, (float)Screen.height - val2.y - num3, num2, num3); Color color = GUI.color; GUI.color = new Color(0f, 0f, 0f, 0.7f); GUI.DrawTexture(val3, (Texture)(object)_white); float num4 = Mathf.Clamp01((float)bar.Current / (float)bar.Max); GUI.color = ((num4 > 0.6f) ? new Color(0.2f, 0.8f, 0.25f, 0.95f) : ((num4 >= 0.3f) ? new Color(0.95f, 0.75f, 0.15f, 0.95f) : new Color(0.9f, 0.2f, 0.2f, 0.95f))); GUI.DrawTexture(new Rect(((Rect)(ref val3)).x + 1f, ((Rect)(ref val3)).y + 1f, Mathf.Max(0f, ((Rect)(ref val3)).width - 2f) * num4, ((Rect)(ref val3)).height - 2f), (Texture)(object)_white); GUI.color = color; string text = (bar.Estimate ? "~" : string.Empty) + bar.Current + "/" + bar.Max; GUI.Label(val3, text, _labelStyle); } } } private void OnDestroy() { if ((Object)(object)_white != (Object)null) { Object.Destroy((Object)(object)_white); } } } internal static class EnemyHealthSync { internal const byte EventHealth = 231; internal const byte EventRequest = 232; internal const string RoomKey = "RepoEnemyHPBarProtocol"; internal const int Protocol = 1; private const int MaxViewIdScan = 0; private static readonly FieldInfo HealthCurrentField = typeof(EnemyHealth).GetField("healthCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); internal static readonly Dictionary<int, int[]> Authoritative = new Dictionary<int, int[]>(); internal static readonly Dictionary<int, int> Estimates = new Dictionary<int, int>(); internal static void InstallPatches(Harmony harmony) { Patch(harmony, "Hurt", "HurtPostfix"); Patch(harmony, "Heal", "HealPostfix"); Patch(harmony, "OnSpawn", "OnSpawnPostfix"); Patch(harmony, "HurtRPC", "HurtRPCPostfix"); Patch(harmony, "DeathRPC", "DeathRPCPostfix"); } private static void Patch(Harmony harmony, string methodName, string postfixName) { //IL_0051: Unknown result type (might be due to invalid IL or missing references) //IL_005e: Expected O, but got Unknown MethodInfo method = typeof(EnemyHealth).GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (method == null) { ManualLogSource log = Plugin.Log; if (log != null) { log.LogWarning((object)("EnemyHealth." + methodName + " not found; patch skipped.")); } } else { harmony.Patch((MethodBase)method, (HarmonyMethod)null, new HarmonyMethod(typeof(EnemyHealthSync).GetMethod(postfixName, BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); } } private static void HurtPostfix(EnemyHealth __instance) { BroadcastIfHost(__instance); } private static void HealPostfix(EnemyHealth __instance) { BroadcastIfHost(__instance); } private static void OnSpawnPostfix(EnemyHealth __instance) { BroadcastIfHost(__instance); SetEstimate(__instance, MaxOf(__instance)); } private static void HurtRPCPostfix(EnemyHealth __instance, int _damage) { if (!IsClientEstimateMode()) { return; } int num = ViewIdOf(__instance); if (num > 0 && _damage > 0) { if (!Estimates.TryGetValue(num, out var value)) { value = MaxOf(__instance); } Estimates[num] = Mathf.Max(0, value - _damage); } } private static void DeathRPCPostfix(EnemyHealth __instance) { if (IsClientEstimateMode()) { int num = ViewIdOf(__instance); if (num > 0) { Estimates[num] = 0; } } } private static bool IsClientEstimateMode() { if (Plugin.Enabled != null && Plugin.Enabled.Value && GameManager.Multiplayer() && PhotonNetwork.InRoom) { return !PhotonNetwork.IsMasterClient; } return false; } internal static int MaxOf(EnemyHealth health) { if (!((Object)(object)health != (Object)null)) { return 0; } return health.health; } internal static int CurrentOf(EnemyHealth health) { if ((Object)(object)health == (Object)null || HealthCurrentField == null) { return 0; } return (int)HealthCurrentField.GetValue(health); } internal static int ViewIdOf(EnemyHealth health) { PhotonView val = (((Object)(object)health != (Object)null) ? ((Component)health).GetComponent<PhotonView>() : null); if (!((Object)(object)val != (Object)null)) { return 0; } return val.ViewID; } internal static void SetEstimate(EnemyHealth health, int value) { int num = ViewIdOf(health); if (num > 0) { Estimates[num] = value; } } internal static bool TryGetEstimate(int viewId, out int current) { return Estimates.TryGetValue(viewId, out current); } internal static bool TryGetAuthoritative(int viewId, out int current, out int max) { if (Authoritative.TryGetValue(viewId, out var value) && value.Length >= 2) { current = value[0]; max = value[1]; return true; } current = 0; max = 0; return false; } private static void BroadcastIfHost(EnemyHealth health) { //IL_0070: 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_0077: 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_0086: Expected O, but got Unknown if (Plugin.Enabled != null && Plugin.Enabled.Value && GameManager.Multiplayer() && PhotonNetwork.InRoom && PhotonNetwork.IsMasterClient && !((Object)(object)health == (Object)null)) { int num = ViewIdOf(health); if (num > 0) { object[] array = new object[3] { num, CurrentOf(health), MaxOf(health) }; PhotonNetwork.RaiseEvent((byte)231, (object)array, new RaiseEventOptions { Receivers = (ReceiverGroup)0 }, SendOptions.SendReliable); } } } private static void BroadcastAll() { if (PhotonNetwork.IsMasterClient && GameManager.Multiplayer()) { EnemyHealth[] array = Object.FindObjectsByType<EnemyHealth>((FindObjectsSortMode)0); for (int i = 0; i < array.Length; i++) { BroadcastIfHost(array[i]); } } } internal static void SendInitialRequest() { //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0015: 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_0024: Expected O, but got Unknown if (IsClientEstimateMode()) { PhotonNetwork.RaiseEvent((byte)232, (object)null, new RaiseEventOptions { Receivers = (ReceiverGroup)2 }, SendOptions.SendReliable); } } internal static void EnsureHostFlag() { //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) //IL_004d: Expected O, but got Unknown //IL_0054: Expected O, but got Unknown if (PhotonNetwork.InRoom && PhotonNetwork.IsMasterClient && PhotonNetwork.CurrentRoom != null) { Hashtable customProperties = ((RoomInfo)PhotonNetwork.CurrentRoom).CustomProperties; if (customProperties == null || !((Dictionary<object, object>)(object)customProperties).ContainsKey((object)"RepoEnemyHPBarProtocol")) { Room currentRoom = PhotonNetwork.CurrentRoom; Hashtable val = new Hashtable(); ((Dictionary<object, object>)val).Add((object)"RepoEnemyHPBarProtocol", (object)1); currentRoom.SetCustomProperties(val, (Hashtable)null, (WebFlags)null); } } } internal static bool HostHasMod() { if (!GameManager.Multiplayer() || !PhotonNetwork.InRoom || PhotonNetwork.CurrentRoom == null) { return false; } return ((Dictionary<object, object>)(object)((RoomInfo)PhotonNetwork.CurrentRoom).CustomProperties)?.ContainsKey((object)"RepoEnemyHPBarProtocol") ?? false; } internal static void OnEventReceived(EventData data) { if (data == null || Plugin.Enabled == null || !Plugin.Enabled.Value) { return; } if (data.Code == 232) { BroadcastAll(); } else { if (data.Code != 231 || !GameManager.Multiplayer() || PhotonNetwork.MasterClient == null || data.Sender != PhotonNetwork.MasterClient.ActorNumber || !(data.CustomData is object[] array) || array.Length < 3) { return; } try { int num = Convert.ToInt32(array[0]); if (num > 0) { Authoritative[num] = new int[2] { Convert.ToInt32(array[1]), Mathf.Max(1, Convert.ToInt32(array[2])) }; } } catch (Exception ex) { ManualLogSource log = Plugin.Log; if (log != null) { log.LogWarning((object)("Malformed health sync event: " + ex.Message)); } } } } internal static void Reset() { Authoritative.Clear(); Estimates.Clear(); } } [BepInPlugin("com.sangbigye.repo.enemyhealthbar", "REPO Enemy Health Bar", "1.0.0")] public class Plugin : BaseUnityPlugin { public const string Guid = "com.sangbigye.repo.enemyhealthbar"; public const string Name = "REPO Enemy Health Bar"; public const string Version = "1.0.0"; internal static ManualLogSource Log; internal static ConfigEntry<bool> Enabled; internal static ConfigEntry<string> VisibilityMode; private void Awake() { //IL_0065: Unknown result type (might be due to invalid IL or missing references) //IL_006f: Expected O, but got Unknown //IL_0079: Unknown result type (might be due to invalid IL or missing references) //IL_007e: Unknown result type (might be due to invalid IL or missing references) //IL_0084: Expected O, but got Unknown //IL_0084: 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) //IL_00a1: Expected O, but got Unknown Log = ((BaseUnityPlugin)this).Logger; Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch for the enemy health bar mod."); VisibilityMode = ((BaseUnityPlugin)this).Config.Bind<string>("Display", "VisibilityMode", "OnScreen", new ConfigDescription("OnScreen: only spawned enemies that are on screen, unobstructed and within 20 m. AllSpawned: every spawned enemy, including through walls.", (AcceptableValueBase)(object)new AcceptableValueList<string>(new string[2] { "OnScreen", "AllSpawned" }), Array.Empty<object>())); GameObject val = new GameObject("RepoEnemyHealthBar.Runner"); Object.DontDestroyOnLoad((Object)val); ((Object)val).hideFlags = (HideFlags)61; val.AddComponent<EnemyHealthBarRunner>(); try { EnemyHealthSync.InstallPatches(new Harmony("com.sangbigye.repo.enemyhealthbar")); } catch (Exception ex) { Log.LogError((object)("Failed to install health patches: " + ex)); } Log.LogInfo((object)("REPO Enemy Health Bar 1.0.0 loaded (visibility=" + VisibilityMode.Value + ").")); } internal static bool IsAllSpawnedMode() { return string.Equals((VisibilityMode != null) ? VisibilityMode.Value : "OnScreen", "AllSpawned", StringComparison.OrdinalIgnoreCase); } } }