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 PatronHud v1.2.1
PatronHud/LevelInfo.dll
Decompiled an hour agousing System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Text; using BepInEx; using BepInEx.Configuration; using Microsoft.CodeAnalysis; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.InputSystem.Controls; [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("LevelInfo")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0")] [assembly: AssemblyProduct("LevelInfo")] [assembly: AssemblyTitle("LevelInfo")] [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 LevelInfo { [BepInPlugin("user.repo.levelinfo", "Level Info", "1.2.2")] public class Plugin : BaseUnityPlugin { private struct Entry { public Texture2D Icon; public string Text; public float Width; } private ConfigEntry<Key> _toggleKey; private ConfigEntry<float> _refreshSeconds; private ConfigEntry<int> _fontSize; private ConfigEntry<float> _offsetY; private ConfigEntry<int> _iconSize; private ConfigEntry<float> _readyDelay; private bool _visible = true; private bool _inLevel; private MethodInfo _runIsLevel; private bool _runIsLevelSearched; private float _nextRefresh; private string _valueLine = ""; private string _enemiesLine = ""; private List<KeyValuePair<string, int>> _groups = new List<KeyValuePair<string, int>>(); private GUIStyle _style; private GUIStyle _shadow; private Font _font; private readonly Dictionary<string, Texture2D> _icons = new Dictionary<string, Texture2D>(); private readonly HashSet<string> _loggedNoIcon = new HashSet<string>(); private FieldInfo _valueField; private bool _valueFieldSearched; private bool _dumpedFields; private float _readySince = -1f; private bool _lastGenerated; private string _lastDirectorState; private MemberInfo _gdInstance; private MemberInfo _gdState; private bool _gdSearched; private MemberInfo _lgInstance; private MemberInfo _lgGenerated; private bool _lgSearched; private readonly HashSet<string> _loggedTier = new HashSet<string>(); private FieldInfo _difficultyField; private bool _difficultyFieldSearched; private const BindingFlags AnyInstance = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private Type _healthType; private bool _healthTypeSearched; private void LoadFont() { //IL_0044: Unknown result type (might be due to invalid IL or missing references) //IL_004e: Expected O, but got Unknown try { string text = Directory.GetFiles(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "Teko*.*").FirstOrDefault((string f) => f.EndsWith(".ttf", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".otf", StringComparison.OrdinalIgnoreCase)); if (text != null) { _font = new Font(text); ((BaseUnityPlugin)this).Logger.LogInfo((object)("Font loaded: " + Path.GetFileName(text))); } else { ((BaseUnityPlugin)this).Logger.LogWarning((object)"Teko font not found next to the plugin, using default font"); } } catch (Exception ex) { ((BaseUnityPlugin)this).Logger.LogWarning((object)("Font load error: " + ex.Message)); } } private static string Normalize(string s) { StringBuilder stringBuilder = new StringBuilder(); foreach (char c in s) { if (char.IsLetterOrDigit(c)) { stringBuilder.Append(char.ToLowerInvariant(c)); } } return stringBuilder.ToString(); } private void LoadIcons() { //IL_0086: Unknown result type (might be due to invalid IL or missing references) //IL_008d: Expected O, but got Unknown try { Assembly executingAssembly = Assembly.GetExecutingAssembly(); string[] manifestResourceNames = executingAssembly.GetManifestResourceNames(); foreach (string text in manifestResourceNames) { if (!text.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) { continue; } string text2 = Path.GetFileNameWithoutExtension(text.Replace('\\', '/').Split('/').Last()); if (text2.Contains(".")) { text2 = text2.Substring(text2.LastIndexOf('.') + 1); } using Stream stream = executingAssembly.GetManifestResourceStream(text); using MemoryStream memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false); if (ImageConversion.LoadImage(val, memoryStream.ToArray())) { ((Texture)val).filterMode = (FilterMode)1; _icons[Normalize(text2)] = val; } } ((BaseUnityPlugin)this).Logger.LogInfo((object)("Icons loaded: " + _icons.Count)); } catch (Exception ex) { ((BaseUnityPlugin)this).Logger.LogWarning((object)("Icon load error: " + ex.Message)); } } private void Awake() { _toggleKey = ((BaseUnityPlugin)this).Config.Bind<Key>("General", "ToggleKey", (Key)99, "Show/hide the panel"); _refreshSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RefreshSeconds", 0.5f, "Update interval (seconds)"); _fontSize = ((BaseUnityPlugin)this).Config.Bind<int>("General", "FontSize", 18, "Font size"); _offsetY = ((BaseUnityPlugin)this).Config.Bind<float>("General", "OffsetY", 15f, "Distance from the top of the screen (pixels)"); _iconSize = ((BaseUnityPlugin)this).Config.Bind<int>("General", "IconSize", 48, "Enemy icon size (pixels)"); _readyDelay = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ShowDelaySeconds", 3f, "Extra delay after the level finishes loading before the panel appears (seconds)"); LoadFont(); LoadIcons(); ((BaseUnityPlugin)this).Logger.LogInfo((object)"Level Info loaded"); } private void Update() { //IL_0010: Unknown result type (might be due to invalid IL or missing references) Keyboard current = Keyboard.current; if (current != null && ((ButtonControl)current[_toggleKey.Value]).wasPressedThisFrame) { _visible = !_visible; } if (_visible && !(Time.unscaledTime < _nextRefresh)) { _nextRefresh = Time.unscaledTime + _refreshSeconds.Value; _inLevel = IsInLevel() && IsLevelReady(); if (_inLevel) { Refresh(); return; } _valueLine = ""; _enemiesLine = ""; _groups = new List<KeyValuePair<string, int>>(); } } private bool IsLevelReady() { bool flag = IsLevelGenerated() && IsDirectorMain(); if (flag != _lastGenerated) { _lastGenerated = flag; ((BaseUnityPlugin)this).Logger.LogInfo((object)("Level ready flag: " + flag)); } if (!flag) { _readySince = -1f; return false; } if (_readySince < 0f) { _readySince = Time.unscaledTime; } return Time.unscaledTime - _readySince >= _readyDelay.Value; } private bool IsDirectorMain() { try { if (!_gdSearched) { _gdSearched = true; Type type = typeof(ValuableObject).Assembly.GetType("GameDirector"); BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; BindingFlags bindingAttr2 = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; _gdInstance = (MemberInfo)(type?.GetField("instance", bindingAttr) ?? ((object)type?.GetField("Instance", bindingAttr)) ?? ((object)(type?.GetProperty("instance", bindingAttr) ?? type?.GetProperty("Instance", bindingAttr)))); _gdState = (MemberInfo)(((object)type?.GetField("currentState", bindingAttr2)) ?? ((object)type?.GetProperty("currentState", bindingAttr2))); Type type2 = (_gdState as FieldInfo)?.FieldType ?? (_gdState as PropertyInfo)?.PropertyType; bool flag = type2 != null && type2.IsEnum && Enum.GetNames(type2).Contains("Main"); if (!flag) { _gdInstance = null; _gdState = null; } ((BaseUnityPlugin)this).Logger.LogInfo((object)(flag ? ("Using GameDirector.currentState == Main (states: " + string.Join(", ", Enum.GetNames(type2)) + ")") : "GameDirector state not found, relying on Generated flag and delay only")); } if (_gdInstance == null || _gdState == null) { return true; } object obj = ((_gdInstance is FieldInfo fieldInfo) ? fieldInfo.GetValue(null) : ((PropertyInfo)_gdInstance).GetValue(null)); if (obj == null) { return false; } string text = ((_gdState is FieldInfo fieldInfo2) ? fieldInfo2.GetValue(obj) : ((PropertyInfo)_gdState).GetValue(obj))?.ToString() ?? ""; if (text != _lastDirectorState) { _lastDirectorState = text; ((BaseUnityPlugin)this).Logger.LogInfo((object)("GameDirector state: " + text)); } return text == "Main"; } catch (Exception ex) { ((BaseUnityPlugin)this).Logger.LogWarning((object)("IsDirectorMain error: " + ex.Message)); return true; } } private bool IsLevelGenerated() { try { if (!_lgSearched) { _lgSearched = true; Type type = typeof(ValuableObject).Assembly.GetType("LevelGenerator"); BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; _lgInstance = (MemberInfo)(((object)type?.GetField("Instance", bindingAttr)) ?? ((object)type?.GetProperty("Instance", bindingAttr))); BindingFlags bindingAttr2 = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; _lgGenerated = (MemberInfo)(((object)type?.GetField("Generated", bindingAttr2)) ?? ((object)type?.GetProperty("Generated", bindingAttr2))); ((BaseUnityPlugin)this).Logger.LogInfo((object)((_lgInstance != null && _lgGenerated != null) ? "Using LevelGenerator.Instance.Generated" : "LevelGenerator.Generated not found, panel will not wait for level load")); } if (_lgInstance == null || _lgGenerated == null) { return true; } object obj = ((_lgInstance is FieldInfo fieldInfo) ? fieldInfo.GetValue(null) : ((PropertyInfo)_lgInstance).GetValue(null)); if (obj == null) { return false; } object obj2 = ((_lgGenerated is FieldInfo fieldInfo2) ? fieldInfo2.GetValue(obj) : ((PropertyInfo)_lgGenerated).GetValue(obj)); bool flag = default(bool); int num; if (obj2 is bool) { flag = (bool)obj2; num = 1; } else { num = 0; } return (byte)((uint)num & (flag ? 1u : 0u)) != 0; } catch (Exception ex) { ((BaseUnityPlugin)this).Logger.LogWarning((object)("IsLevelGenerated error: " + ex.Message)); return true; } } private bool IsInLevel() { try { if (!_runIsLevelSearched) { _runIsLevelSearched = true; _runIsLevel = typeof(ValuableObject).Assembly.GetType("SemiFunc")?.GetMethod("RunIsLevel", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); ((BaseUnityPlugin)this).Logger.LogInfo((object)((_runIsLevel != null) ? "Using SemiFunc.RunIsLevel" : "SemiFunc.RunIsLevel not found, fallback to valuables check")); } if (_runIsLevel != null) { return (bool)_runIsLevel.Invoke(null, null); } return Object.FindObjectsByType<ValuableObject>((FindObjectsSortMode)0).Length != 0; } catch (Exception ex) { ((BaseUnityPlugin)this).Logger.LogWarning((object)("IsInLevel error: " + ex.Message)); return false; } } private FieldInfo FindValueField() { if (_valueFieldSearched) { return _valueField; } _valueFieldSearched = true; Type typeFromHandle = typeof(ValuableObject); BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; string[] array = new string[4] { "dollarValueCurrent", "dollarValueOverride", "dollarValueOriginal", "dollarValue" }; foreach (string text in array) { FieldInfo field = typeFromHandle.GetField(text, bindingAttr); if (field != null && (field.FieldType == typeof(float) || field.FieldType == typeof(int))) { _valueField = field; ((BaseUnityPlugin)this).Logger.LogInfo((object)("Value field: " + text)); return field; } } FieldInfo fieldInfo = (_valueField = typeFromHandle.GetFields(bindingAttr).FirstOrDefault((FieldInfo f) => (f.FieldType == typeof(float) || f.FieldType == typeof(int)) && (f.Name.ToLower().Contains("dollar") || f.Name.ToLower().Contains("value")))); if (fieldInfo != null) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("Value field (guess): " + fieldInfo.Name)); } return fieldInfo; } private void DumpFields() { if (!_dumpedFields) { _dumpedFields = true; BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; ((BaseUnityPlugin)this).Logger.LogWarning((object)("ValuableObject fields: " + string.Join(", ", from f in typeof(ValuableObject).GetFields(bindingAttr) select f.FieldType.Name + " " + f.Name))); } } private void Refresh() { try { ValuableObject[] array = Object.FindObjectsByType<ValuableObject>((FindObjectsSortMode)0); FieldInfo fieldInfo = FindValueField(); if (fieldInfo == null) { DumpFields(); _valueLine = $"Valuables: {array.Length} (value field not found, see log)"; } else { float num = 0f; ValuableObject[] array2 = array; foreach (ValuableObject val in array2) { if (!((Object)(object)val == (Object)null) && ((Component)val).gameObject.activeInHierarchy) { num += Convert.ToSingle(fieldInfo.GetValue(val)); } } _valueLine = $"Level value: ${Format(num)} ({array.Length} items)"; } } catch (Exception ex) { _valueLine = "Level value: ?"; ((BaseUnityPlugin)this).Logger.LogWarning((object)("Valuables error: " + ex.Message)); } try { List<EnemyParent> list = (from e in Object.FindObjectsByType<EnemyParent>((FindObjectsSortMode)0) where (Object)(object)e != (Object)null && ((Component)e).gameObject.activeInHierarchy && IsEnemyAlive(e) select e).ToList(); _enemiesLine = $"Enemies: {list.Count}"; Dictionary<string, int> dictionary = new Dictionary<string, int>(); Dictionary<string, int> tiers = new Dictionary<string, int>(); foreach (EnemyParent item in list) { string text = CleanName(string.IsNullOrEmpty(item.enemyName) ? ((Object)item).name : item.enemyName); dictionary[text] = ((!dictionary.TryGetValue(text, out var value)) ? 1 : (value + 1)); if (!tiers.ContainsKey(text)) { int tier = GetTier(item); tiers[text] = tier; if (_loggedTier.Add(text)) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("Enemy \"" + text + "\" tier: " + ((tier == int.MaxValue) ? "unknown" : tier.ToString()))); } } } _groups = (from k in dictionary orderby tiers[k.Key], k.Value descending, k.Key select k).ToList(); } catch (Exception ex2) { _enemiesLine = "Enemies: ?"; _groups = new List<KeyValuePair<string, int>>(); ((BaseUnityPlugin)this).Logger.LogWarning((object)("Enemies error: " + ex2.Message)); } } private int GetTier(EnemyParent e) { try { if (!_difficultyFieldSearched) { _difficultyFieldSearched = true; _difficultyField = typeof(EnemyParent).GetField("difficulty", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? typeof(EnemyParent).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault((FieldInfo f) => f.Name.ToLower().Contains("difficulty")); if (_difficultyField == null) { ((BaseUnityPlugin)this).Logger.LogWarning((object)"EnemyParent difficulty field not found, enemies will not be sorted by rank"); } } if (_difficultyField == null) { return int.MaxValue; } object value = _difficultyField.GetValue(e); if (value == null) { return int.MaxValue; } string text = new string(value.ToString().Where(char.IsDigit).ToArray()); if (text.Length > 0) { return int.Parse(text); } if (value is Enum) { return Convert.ToInt32(value) + 1; } return Convert.ToInt32(value); } catch { return int.MaxValue; } } private static bool? ReadBool(object obj, params string[] names) { Type type = obj.GetType(); foreach (string name in names) { FieldInfo field = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (field != null && field.FieldType == typeof(bool)) { return (bool)field.GetValue(obj); } PropertyInfo property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (property != null && property.PropertyType == typeof(bool) && property.CanRead) { return (bool)property.GetValue(obj); } } return null; } private bool IsEnemyAlive(EnemyParent parent) { //IL_004f: Unknown result type (might be due to invalid IL or missing references) bool? flag = ReadBool(parent, "Spawned", "spawned", "IsSpawned"); if (flag.HasValue) { if (!flag.Value) { return false; } } else { bool flag2 = false; foreach (Transform item in ((Component)parent).transform) { if (((Component)item).gameObject.activeInHierarchy) { flag2 = true; break; } } if (!flag2) { return false; } } if (!_healthTypeSearched) { _healthTypeSearched = true; _healthType = typeof(EnemyParent).Assembly.GetType("EnemyHealth"); } if (_healthType != null) { Component componentInChildren = ((Component)parent).GetComponentInChildren(_healthType, true); if ((Object)(object)componentInChildren != (Object)null && ReadBool(componentInChildren, "dead", "isDead", "Dead", "IsDead") == true) { return false; } } return true; } private static string CleanName(string n) { return n.Replace("(Clone)", "").Replace("Enemy - ", "").Trim(); } private static string Format(float v) { return ((long)v).ToString("N0").Replace(',', ' '); } private void DrawText(Rect r, string text) { //IL_0028: Unknown result type (might be due to invalid IL or missing references) //IL_0039: Unknown result type (might be due to invalid IL or missing references) GUI.Label(new Rect(((Rect)(ref r)).x + 1f, ((Rect)(ref r)).y + 1f, ((Rect)(ref r)).width, ((Rect)(ref r)).height), text, _shadow); GUI.Label(r, text, _style); } private void OnGUI() { //IL_0034: Unknown result type (might be due to invalid IL or missing references) //IL_0039: 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_0051: Unknown result type (might be due to invalid IL or missing references) //IL_005d: Unknown result type (might be due to invalid IL or missing references) //IL_0064: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Expected O, but got Unknown //IL_007b: 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_0096: Expected O, but got Unknown //IL_00a1: Unknown result type (might be due to invalid IL or missing references) //IL_0115: Unknown result type (might be due to invalid IL or missing references) //IL_0229: Unknown result type (might be due to invalid IL or missing references) //IL_0233: Expected O, but got Unknown //IL_022e: Unknown result type (might be due to invalid IL or missing references) //IL_0440: Unknown result type (might be due to invalid IL or missing references) //IL_03ec: Unknown result type (might be due to invalid IL or missing references) //IL_0420: Unknown result type (might be due to invalid IL or missing references) if (!_visible || !_inLevel || string.IsNullOrEmpty(_valueLine)) { return; } if (_style == null) { _style = new GUIStyle(GUI.skin.label) { fontSize = _fontSize.Value, richText = false, font = _font, alignment = (TextAnchor)4, wordWrap = false }; _style.normal.textColor = Color.white; _shadow = new GUIStyle(_style); _shadow.normal.textColor = Color.black; } float num = Screen.width; float num2 = _offsetY.Value; float num3 = _style.lineHeight + 4f; float num4 = _iconSize.Value; string[] array = new string[2] { _valueLine, _enemiesLine }; foreach (string text in array) { if (!string.IsNullOrEmpty(text)) { DrawText(new Rect(0f, num2, num, num3), text); num2 += num3; } } List<Entry> list = new List<Entry>(); foreach (KeyValuePair<string, int> group in _groups) { _icons.TryGetValue(Normalize(group.Key), out var value); if ((Object)(object)value == (Object)null && _loggedNoIcon.Add(group.Key)) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("No icon for enemy: \"" + group.Key + "\" (add icons/" + group.Key.Replace(' ', '_') + ".png)")); } string text2 = (((Object)(object)value != (Object)null) ? ("x" + group.Value) : (group.Key + " x" + group.Value)); float x = _style.CalcSize(new GUIContent(text2)).x; list.Add(new Entry { Icon = value, Text = text2, Width = (((Object)(object)value != (Object)null) ? (num4 + 2f) : 0f) + x }); } float num5 = num * 0.9f; List<Entry> list2 = new List<Entry>(); float num6 = 0f; List<List<Entry>> list3 = new List<List<Entry>>(); foreach (Entry item in list) { float num7 = item.Width + ((list2.Count > 0) ? 16f : 0f); if (list2.Count > 0 && num6 + num7 > num5) { list3.Add(list2); list2 = new List<Entry>(); num6 = 0f; num7 = item.Width; } list2.Add(item); num6 += num7; } if (list2.Count > 0) { list3.Add(list2); } foreach (List<Entry> item2 in list3) { float num8 = item2.Sum((Entry e) => e.Width) + 16f * (float)(item2.Count - 1); float num9 = (num - num8) / 2f; float num10 = Mathf.Max(num4, num3); foreach (Entry item3 in item2) { if ((Object)(object)item3.Icon != (Object)null) { GUI.DrawTexture(new Rect(num9, num2, num4, num4), (Texture)(object)item3.Icon, (ScaleMode)2, true); float num11 = item3.Width - num4 - 2f; DrawText(new Rect(num9 + num4 + 2f, num2, num11, num10), item3.Text); } else { DrawText(new Rect(num9, num2, item3.Width, num10), item3.Text); } num9 += item3.Width + 16f; } num2 += num10 + 2f; } } } }