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 StabilityView v0.1.1
plugins/StabilityView.dll
Decompiled 3 weeks agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using System.Text; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using Microsoft.CodeAnalysis; using UnityEngine; using ValheimMods.Shared; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: IgnoresAccessChecksTo("assembly_valheim")] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("StabilityView")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyDescription("Shows structure support values, where support comes from, and how changes affect it.")] [assembly: AssemblyFileVersion("0.1.1.0")] [assembly: AssemblyInformationalVersion("0.1.1")] [assembly: AssemblyProduct("StabilityView")] [assembly: AssemblyTitle("StabilityView")] [assembly: InternalsVisibleTo("ValheimMods.Tests")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("0.1.1.0")] [module: UnverifiableCode] [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 StabilityView { internal static class Labels { public static readonly Color IncreaseColor = new Color(0.35f, 1f, 0.4f); public static readonly Color DecreaseColor = new Color(1f, 0.3f, 0.25f); public static readonly Color NewColor = new Color(0.55f, 0.8f, 1f); public static Color ChangeColor(float delta, bool isNew) { //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) if (!isNew) { if (!(delta > 0f)) { return DecreaseColor; } return IncreaseColor; } return NewColor; } public static (string Text, string Plain) Piece(string materialType, float support, MaterialInfo material, float? delta, bool isNew) { //IL_00fd: 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_0104: 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) StringBuilder stringBuilder = new StringBuilder(); if (Settings.ShowMaterial.Value) { stringBuilder.Append(materialType).Append(' '); } stringBuilder.Append(support.ToString("0.#", CultureInfo.CurrentCulture)); if (Settings.ShowMaxSupport.Value) { stringBuilder.Append('/').Append(material.Max.ToString("0", CultureInfo.CurrentCulture)); } if (Settings.ShowBreakThreshold.Value) { stringBuilder.Append(" (min ").Append(material.Min.ToString("0", CultureInfo.CurrentCulture)).Append(')'); } string text = (isNew ? "new" : (delta.HasValue ? (((delta.Value > 0f) ? "+" : "") + delta.Value.ToString("0.#", CultureInfo.CurrentCulture)) : null)); string text2 = stringBuilder.ToString(); Color color = Stability.ColorFor(support, material); return (Text: Colored(text2, color) + ((text == null) ? "" : ("\n" + Colored(text, ChangeColor(delta.GetValueOrDefault(), isNew)))), Plain: (text == null) ? text2 : (text2 + "\n" + text)); } public static string Status(int shown, int hiddenByLimit, int hiddenByRadius, bool snapshotActive, bool manualSnapshot) { //IL_00b5: Unknown result type (might be due to invalid IL or missing references) StringBuilder stringBuilder = new StringBuilder("StabilityView: ").Append(shown).Append(" shown"); if (hiddenByLimit > 0) { stringBuilder.Append(" · ").Append(hiddenByLimit).Append(" hidden by MaxPieces (") .Append(Settings.MaxPieces.Value) .Append(')'); } if (hiddenByRadius > 0) { stringBuilder.Append(" · ").Append(hiddenByRadius).Append(" beyond ") .Append(Settings.Radius.Value.ToString("0", CultureInfo.CurrentCulture)) .Append(" m"); } if (snapshotActive) { stringBuilder.Append(manualSnapshot ? $" · changes since manual snapshot ({Settings.ClearSnapshot.Value} clears)" : " · showing changes"); } return stringBuilder.ToString(); } private static string Colored(string text, Color color) { //IL_0010: Unknown result type (might be due to invalid IL or missing references) return "<color=#" + ColorUtility.ToHtmlStringRGB(color) + ">" + text + "</color>"; } } internal sealed class Overlay : MonoBehaviour { private sealed class Entry { public WearNTear Piece; public Color Color; public string Text; public string Plain; } private const float RefreshInterval = 0.25f; private const float SourceMaxAge = 1f; private const int SourcesPerFrame = 25; private static readonly int s_emissionColorId = Shader.PropertyToID("_EmissionColor"); private bool m_enabled; private readonly List<Entry> m_entries = new List<Entry>(); private readonly Dictionary<WearNTear, SourceInfo> m_sources = new Dictionary<WearNTear, SourceInfo>(); private HashSet<GameObject> m_highlighted = new HashSet<GameObject>(); private HashSet<GameObject> m_nextHighlighted = new HashSet<GameObject>(); private float m_nextRefresh; private int m_sourceCursor; private int m_hiddenByLimit; private int m_hiddenByRadius; private GUIStyle m_style; private GUIStyle m_shadowStyle; private Material m_lineMaterial; private bool m_lineMaterialFailed; private bool m_analysisErrorLogged; private FrameGuard m_guard; public static Overlay Instance { get; private set; } public bool IsShowing { get; private set; } private void Awake() { Instance = this; m_guard = new FrameGuard((Behaviour)(object)this, "the stability overlay"); } private void OnDisable() { Hide(); } private void OnDestroy() { Hide(); } private void Update() { try { Tick(); } catch (Exception e) { m_guard.Report(e); } } private void Tick() { HandleKeys(); if (!m_enabled || !CanShow()) { if (IsShowing) { Hide(); } return; } IsShowing = true; if (Time.time >= m_nextRefresh) { m_nextRefresh = Time.time + 0.25f; Refresh(); } if (Settings.ShowSourceLines.Value) { AnalyzeSources(); } } private static bool CanShow() { Player localPlayer = Player.m_localPlayer; if (!Object.op_Implicit((Object)(object)localPlayer) || !Object.op_Implicit((Object)(object)GameCamera.instance)) { return false; } if (Settings.OnlyInBuildMode.Value) { return ((Character)localPlayer).InPlaceMode(); } return true; } private void Hide() { IsShowing = false; m_entries.Clear(); m_sources.Clear(); m_nextHighlighted.Clear(); ApplyHighlightChanges(); } private void HandleKeys() { if (!IsTyping()) { if (Hotkeys.Pressed(Settings.ToggleOverlay)) { m_enabled = !m_enabled; Message((!m_enabled) ? "Stability overlay off" : (Settings.OnlyInBuildMode.Value ? "Stability overlay on (shown while building)" : "Stability overlay on")); } if (Hotkeys.Pressed(Settings.ToggleSourceLines)) { Settings.ShowSourceLines.Value = !Settings.ShowSourceLines.Value; Message(Settings.ShowSourceLines.Value ? "Support source lines on" : "Support source lines off"); } if (Hotkeys.Pressed(Settings.ClearSnapshot)) { Snapshot.Clear(); Message("Stability snapshot cleared"); } if (Hotkeys.Pressed(Settings.TakeSnapshot)) { Snapshot.Capture(manual: true); Message(Settings.AutoSnapshot.Value ? "Stability snapshot taken (automatic snapshots paused until cleared)" : "Stability snapshot taken"); } } } private static bool IsTyping() { if (!Hotkeys.IsTyping()) { return InventoryGui.IsVisible(); } return true; } private static void Message(string text) { if (Object.op_Implicit((Object)(object)Player.m_localPlayer)) { ((Character)Player.m_localPlayer).Message((MessageType)1, text, 0, (Sprite)null, false); } } private void Refresh() { //IL_0020: Unknown result type (might be due to invalid IL or missing references) //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Unknown result type (might be due to invalid IL or missing references) //IL_0076: Unknown result type (might be due to invalid IL or missing references) //IL_0079: Unknown result type (might be due to invalid IL or missing references) //IL_0082: Unknown result type (might be due to invalid IL or missing references) //IL_0084: 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_008a: Unknown result type (might be due to invalid IL or missing references) //IL_022e: Unknown result type (might be due to invalid IL or missing references) //IL_0233: Unknown result type (might be due to invalid IL or missing references) //IL_0264: Unknown result type (might be due to invalid IL or missing references) //IL_0269: Unknown result type (might be due to invalid IL or missing references) //IL_027c: Unknown result type (might be due to invalid IL or missing references) //IL_0283: Unknown result type (might be due to invalid IL or missing references) Camera camera = GameCamera.instance.m_camera; if (!Object.op_Implicit((Object)(object)camera)) { return; } Vector3 position = ((Component)camera).transform.position; float num = Settings.Radius.Value * Settings.Radius.Value; int num2 = 0; List<KeyValuePair<float, WearNTear>> list = new List<KeyValuePair<float, WearNTear>>(); foreach (WearNTear allInstance in WearNTear.GetAllInstances()) { if (!Object.op_Implicit((Object)(object)allInstance) || !Stability.UsesSupport(allInstance)) { continue; } Vector3 val = Stability.CenterOfMass(allInstance); if (IsOnScreen(camera, val)) { Vector3 val2 = val - position; float sqrMagnitude = ((Vector3)(ref val2)).sqrMagnitude; if (sqrMagnitude <= num) { list.Add(new KeyValuePair<float, WearNTear>(sqrMagnitude, allInstance)); } else { num2++; } } } list.Sort((KeyValuePair<float, WearNTear> a, KeyValuePair<float, WearNTear> b) => a.Key.CompareTo(b.Key)); int value = Settings.MaxPieces.Value; m_hiddenByLimit = Math.Max(0, list.Count - value); m_hiddenByRadius = num2; bool active = Snapshot.Active; m_entries.Clear(); m_nextHighlighted.Clear(); foreach (KeyValuePair<float, WearNTear> item in list.Take(value)) { WearNTear value2 = item.Value; float num3 = Stability.Support(value2); MaterialInfo material = Stability.Material(value2); float num4 = 0f; bool flag = false; bool flag2 = false; if (active) { if (Snapshot.TryGet(value2, out var support)) { num4 = num3 - support; flag = Mathf.Abs(num4) >= Settings.ChangeThreshold.Value; } else { flag2 = true; } } if (!Settings.HideFullySupported.Value || !Stability.IsAtMax(num3, material) || flag || flag2) { var (text, plain) = Labels.Piece(((object)Unsafe.As<MaterialType, MaterialType>(ref value2.m_materialType)/*cast due to .constrained prefix*/).ToString(), num3, material, flag ? new float?(num4) : ((float?)null), flag2); m_entries.Add(new Entry { Piece = value2, Color = Stability.ColorFor(num3, material), Text = text, Plain = plain }); if (Settings.HighlightChanges.Value && (flag || flag2)) { Color val3 = Labels.ChangeColor(num4, flag2); MaterialMan.instance.SetValue<Color>(((Component)value2).gameObject, s_emissionColorId, val3 * 0.4f, false); m_nextHighlighted.Add(((Component)value2).gameObject); } } } ApplyHighlightChanges(); HashSet<WearNTear> visible = new HashSet<WearNTear>(m_entries.Select((Entry e) => e.Piece)); foreach (WearNTear item2 in m_sources.Keys.Where((WearNTear k) => !Object.op_Implicit((Object)(object)k) || !visible.Contains(k)).ToList()) { m_sources.Remove(item2); } } private static bool IsOnScreen(Camera camera, Vector3 position) { //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_0008: 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_0022: Unknown result type (might be due to invalid IL or missing references) //IL_002f: 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) Vector3 val = camera.WorldToViewportPoint(position); if (val.z > 0f && val.x >= -0.05f && val.x <= 1.05f && val.y >= -0.05f) { return val.y <= 1.05f; } return false; } private void ApplyHighlightChanges() { foreach (GameObject item in m_highlighted) { if (Object.op_Implicit((Object)(object)item) && !m_nextHighlighted.Contains(item)) { MaterialMan.instance.ResetValue(item, s_emissionColorId); } } HashSet<GameObject> nextHighlighted = m_nextHighlighted; HashSet<GameObject> highlighted = m_highlighted; m_highlighted = nextHighlighted; m_nextHighlighted = highlighted; m_nextHighlighted.Clear(); } private void AnalyzeSources() { if (m_entries.Count == 0) { return; } int num = Math.Min(25, m_entries.Count); for (int i = 0; i < num; i++) { m_sourceCursor = (m_sourceCursor + 1) % m_entries.Count; WearNTear piece = m_entries[m_sourceCursor].Piece; if (!Object.op_Implicit((Object)(object)piece) || (m_sources.TryGetValue(piece, out var value) && Time.time - value.AnalyzedAt < 1f)) { continue; } try { m_sources[piece] = SupportAnalyzer.Analyze(piece); } catch (Exception arg) { if (!m_analysisErrorLogged) { ModInfo.Log.LogWarning((object)$"Support source analysis failed: {arg}"); m_analysisErrorLogged = true; } m_sources[piece] = new SourceInfo { Kind = SourceKind.None, AnalyzedAt = Time.time }; } } } private void OnGUI() { try { Draw(); } catch (Exception e) { m_guard.Report(e); } } private void Draw() { //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_0013: Invalid comparison between Unknown and I4 //IL_0081: Unknown result type (might be due to invalid IL or missing references) //IL_0086: Unknown result type (might be due to invalid IL or missing references) //IL_008b: 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_009e: Unknown result type (might be due to invalid IL or missing references) //IL_00b0: Unknown result type (might be due to invalid IL or missing references) //IL_00f4: Unknown result type (might be due to invalid IL or missing references) //IL_010a: Unknown result type (might be due to invalid IL or missing references) if (!IsShowing || (int)Event.current.type != 7) { return; } Camera val = (Object.op_Implicit((Object)(object)GameCamera.instance) ? GameCamera.instance.m_camera : null); if (!Object.op_Implicit((Object)(object)val)) { return; } EnsureStyles(); if (Settings.ShowStatus.Value) { DrawStatus(); } Rect val3 = default(Rect); foreach (Entry entry in m_entries) { if (Object.op_Implicit((Object)(object)entry.Piece)) { Vector3 val2 = val.WorldToScreenPoint(Stability.CenterOfMass(entry.Piece)); if (!(val2.z <= 0f)) { ((Rect)(ref val3))..ctor(val2.x - 100f, (float)Screen.height - val2.y - 20f, 200f, 40f); GUI.Label(new Rect(((Rect)(ref val3)).x + 1f, ((Rect)(ref val3)).y + 1f, ((Rect)(ref val3)).width, ((Rect)(ref val3)).height), entry.Plain, m_shadowStyle); GUI.Label(val3, entry.Text, m_style); } } } } private void DrawStatus() { //IL_006b: 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) string text = Labels.Status(m_entries.Count, m_hiddenByLimit, m_hiddenByRadius, Snapshot.Active, Snapshot.IsManual); Rect val = default(Rect); ((Rect)(ref val))..ctor(0f, 44f, (float)Screen.width, 24f); GUI.Label(new Rect(((Rect)(ref val)).x + 1f, ((Rect)(ref val)).y + 1f, ((Rect)(ref val)).width, ((Rect)(ref val)).height), text, m_shadowStyle); GUI.Label(val, text, m_style); } private void EnsureStyles() { //IL_002d: 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_0039: 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) //IL_0047: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Expected O, but got Unknown //IL_005e: Unknown result type (might be due to invalid IL or missing references) //IL_006f: Unknown result type (might be due to invalid IL or missing references) //IL_0074: Unknown result type (might be due to invalid IL or missing references) //IL_0080: Expected O, but got Unknown //IL_009f: Unknown result type (might be due to invalid IL or missing references) int value = Settings.FontSize.Value; if (m_style == null || m_style.fontSize != value) { m_style = new GUIStyle(GUI.skin.label) { alignment = (TextAnchor)4, richText = true, fontSize = value, fontStyle = (FontStyle)1 }; m_style.normal.textColor = Color.white; m_shadowStyle = new GUIStyle(m_style) { richText = false }; m_shadowStyle.normal.textColor = new Color(0f, 0f, 0f, 0.85f); } } private void OnRenderObject() { //IL_00d3: Unknown result type (might be due to invalid IL or missing references) //IL_00d8: Unknown result type (might be due to invalid IL or missing references) //IL_00e7: Unknown result type (might be due to invalid IL or missing references) //IL_00ef: Unknown result type (might be due to invalid IL or missing references) //IL_00f5: Unknown result type (might be due to invalid IL or missing references) //IL_010c: Unknown result type (might be due to invalid IL or missing references) //IL_0114: Unknown result type (might be due to invalid IL or missing references) //IL_011a: Unknown result type (might be due to invalid IL or missing references) if (!IsShowing || !Settings.ShowSourceLines.Value || m_sources.Count == 0) { return; } Camera val = (Object.op_Implicit((Object)(object)GameCamera.instance) ? GameCamera.instance.m_camera : null); if (!Object.op_Implicit((Object)(object)val) || (Object)(object)Camera.current != (Object)(object)val || !EnsureLineMaterial()) { return; } m_lineMaterial.SetInt("_ZTest", Settings.LinesThroughWalls.Value ? 8 : 4); m_lineMaterial.SetPass(0); GL.PushMatrix(); GL.Begin(1); foreach (Entry entry in m_entries) { if (Object.op_Implicit((Object)(object)entry.Piece) && m_sources.TryGetValue(entry.Piece, out var value)) { Vector3 val2 = Stability.CenterOfMass(entry.Piece); if (Object.op_Implicit((Object)(object)value.A)) { Line(val2, Stability.CenterOfMass(value.A), entry.Color); } if (Object.op_Implicit((Object)(object)value.B)) { Line(val2, Stability.CenterOfMass(value.B), entry.Color); } } } GL.End(); GL.PopMatrix(); } private static void Line(Vector3 from, Vector3 to, Color color) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0018: 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) //IL_002d: Unknown result type (might be due to invalid IL or missing references) GL.Color(color); GL.Vertex(from); GL.Color(new Color(color.r, color.g, color.b, 0.25f)); GL.Vertex(to); } private bool EnsureLineMaterial() { //IL_0046: Unknown result type (might be due to invalid IL or missing references) //IL_004b: Unknown result type (might be due to invalid IL or missing references) //IL_0058: Expected O, but got Unknown if (Object.op_Implicit((Object)(object)m_lineMaterial)) { return true; } if (m_lineMaterialFailed) { return false; } Shader val = Shader.Find("Hidden/Internal-Colored"); if (!Object.op_Implicit((Object)(object)val)) { ModInfo.Log.LogWarning((object)"Line shader not found; support source lines are disabled"); m_lineMaterialFailed = true; return false; } m_lineMaterial = new Material(val) { hideFlags = (HideFlags)61 }; m_lineMaterial.SetInt("_SrcBlend", 5); m_lineMaterial.SetInt("_DstBlend", 10); m_lineMaterial.SetInt("_Cull", 0); m_lineMaterial.SetInt("_ZWrite", 0); return true; } } [HarmonyPatch] internal static class AutoSnapshotPatches { [HarmonyPrefix] [HarmonyPatch(typeof(Player), "PlacePiece")] private static void BeforePlace(Player __instance) { Capture(__instance); } [HarmonyPrefix] [HarmonyPatch(typeof(Player), "RemovePiece")] private static void BeforeRemove(Player __instance) { Capture(__instance); } private static void Capture(Player player) { if ((Object)(object)player == (Object)(object)Player.m_localPlayer && Settings.AutoSnapshot.Value && !Snapshot.IsManual && Object.op_Implicit((Object)(object)Overlay.Instance) && Overlay.Instance.IsShowing) { Snapshot.Capture(manual: false); } } } [BepInPlugin("s992.StabilityView", "StabilityView", "0.1.1")] public class Plugin : BaseUnityPlugin { private void Awake() { //IL_0010: Unknown result type (might be due to invalid IL or missing references) //IL_0020: Expected O, but got Unknown Settings.Bind(((BaseUnityPlugin)this).Config); if (SafePatching.PatchAll(new Harmony("s992.StabilityView"), ((BaseUnityPlugin)this).Logger)) { ((Component)this).gameObject.AddComponent<Overlay>(); ((BaseUnityPlugin)this).Logger.LogInfo((object)(ModInfo.Name + " " + ModInfo.Version + " loaded")); } } } internal static class Settings { public static ConfigEntry<KeyboardShortcut> ToggleOverlay; public static ConfigEntry<KeyboardShortcut> ToggleSourceLines; public static ConfigEntry<KeyboardShortcut> TakeSnapshot; public static ConfigEntry<KeyboardShortcut> ClearSnapshot; public static ConfigEntry<bool> OnlyInBuildMode; public static ConfigEntry<float> Radius; public static ConfigEntry<int> MaxPieces; public static ConfigEntry<bool> ShowStatus; public static ConfigEntry<int> FontSize; public static ConfigEntry<bool> ShowMaterial; public static ConfigEntry<bool> ShowMaxSupport; public static ConfigEntry<bool> ShowBreakThreshold; public static ConfigEntry<bool> HideFullySupported; public static ConfigEntry<bool> ShowSourceLines; public static ConfigEntry<bool> LinesThroughWalls; public static ConfigEntry<bool> AutoSnapshot; public static ConfigEntry<bool> HighlightChanges; public static ConfigEntry<float> ChangeThreshold; public static ConfigEntry<float> ChangeDuration; public static void Bind(ConfigFile config) { //IL_0015: 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_0067: 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) //IL_00f1: Unknown result type (might be due to invalid IL or missing references) //IL_00fb: Expected O, but got Unknown //IL_0125: Unknown result type (might be due to invalid IL or missing references) //IL_012f: Expected O, but got Unknown //IL_016e: Unknown result type (might be due to invalid IL or missing references) //IL_0178: Expected O, but got Unknown //IL_027e: Unknown result type (might be due to invalid IL or missing references) //IL_0288: Expected O, but got Unknown //IL_02b6: Unknown result type (might be due to invalid IL or missing references) //IL_02c0: Expected O, but got Unknown ToggleOverlay = config.Bind<KeyboardShortcut>("Keys", "ToggleOverlay", new KeyboardShortcut((KeyCode)288, Array.Empty<KeyCode>()), "Show or hide the stability overlay."); ToggleSourceLines = config.Bind<KeyboardShortcut>("Keys", "ToggleSourceLines", new KeyboardShortcut((KeyCode)290, Array.Empty<KeyCode>()), "Show or hide the lines to where each piece gets its support."); TakeSnapshot = config.Bind<KeyboardShortcut>("Keys", "TakeSnapshot", new KeyboardShortcut((KeyCode)289, Array.Empty<KeyCode>()), "Record current support values; changes are then shown relative to them, and automatic snapshots pause until it's cleared."); ClearSnapshot = config.Bind<KeyboardShortcut>("Keys", "ClearSnapshot", new KeyboardShortcut((KeyCode)289, (KeyCode[])(object)new KeyCode[1] { (KeyCode)304 }), "Forget the snapshot and stop showing changes (resumes automatic snapshots)."); OnlyInBuildMode = config.Bind<bool>("Display", "OnlyInBuildMode", true, "Only show the overlay while a building tool (hammer, hoe, ...) is equipped."); Radius = config.Bind<float>("Display", "Radius", 30f, new ConfigDescription("Show on-screen pieces within this many metres of the camera.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(2f, 150f), Array.Empty<object>())); MaxPieces = config.Bind<int>("Display", "MaxPieces", 200, new ConfigDescription("Show at most this many on-screen pieces (closest to the camera first).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 1000), Array.Empty<object>())); ShowStatus = config.Bind<bool>("Display", "ShowStatus", true, "Show a status line with how many pieces are labelled and how many were left out."); FontSize = config.Bind<int>("Display", "FontSize", 13, new ConfigDescription("Label font size.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(8, 32), Array.Empty<object>())); ShowMaterial = config.Bind<bool>("Display", "ShowMaterial", false, "Prefix labels with the piece's material (Wood, Stone, ...)."); ShowMaxSupport = config.Bind<bool>("Display", "ShowMaxSupport", true, "Show support as current/max instead of just current."); ShowBreakThreshold = config.Bind<bool>("Display", "ShowBreakThreshold", false, "Show the support value below which the piece collapses."); HideFullySupported = config.Bind<bool>("Display", "HideFullySupported", false, "Skip labels for pieces at their material's maximum (grounded pieces)."); ShowSourceLines = config.Bind<bool>("Support sources", "ShowSourceLines", true, "Draw a line from each piece to the piece(s) it gets its support from."); LinesThroughWalls = config.Bind<bool>("Support sources", "LinesThroughWalls", true, "Draw source lines on top of everything instead of hiding them behind geometry."); AutoSnapshot = config.Bind<bool>("Changes", "AutoSnapshot", true, "Take a snapshot automatically just before you place or remove a piece, so each change shows its effect."); HighlightChanges = config.Bind<bool>("Changes", "HighlightChanges", true, "Tint pieces whose support changed since the snapshot (green = up, red = down)."); ChangeThreshold = config.Bind<float>("Changes", "ChangeThreshold", 0.5f, new ConfigDescription("Ignore changes smaller than this.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>())); ChangeDuration = config.Bind<float>("Changes", "ChangeDuration", 0f, new ConfigDescription("Stop showing changes this many seconds after the snapshot. 0 = until the next snapshot.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 600f), Array.Empty<object>())); } } internal static class Snapshot { private static Dictionary<ZDOID, float> s_values; private static float s_takenAt; public static bool Active { get { if (s_values != null) { if (!(Settings.ChangeDuration.Value <= 0f)) { return Time.time - s_takenAt <= Settings.ChangeDuration.Value; } return true; } return false; } } public static bool IsManual { get; private set; } public static void Capture(bool manual) { //IL_0037: Unknown result type (might be due to invalid IL or missing references) s_values = new Dictionary<ZDOID, float>(); foreach (WearNTear allInstance in WearNTear.GetAllInstances()) { if (Stability.UsesSupport(allInstance)) { s_values[allInstance.m_nview.GetZDO().m_uid] = Stability.Support(allInstance); } } s_takenAt = Time.time; IsManual = manual; } public static void Clear() { s_values = null; IsManual = false; } public static bool TryGet(WearNTear wnt, out float support) { //IL_001e: Unknown result type (might be due to invalid IL or missing references) support = 0f; if (s_values != null) { return s_values.TryGetValue(wnt.m_nview.GetZDO().m_uid, out support); } return false; } } internal struct MaterialInfo { public float Max; public float Min; public float HorizontalLoss; public float VerticalLoss; } internal static class Stability { public static readonly Color MaxColor = new Color(0.6f, 0.8f, 1f); public static bool UsesSupport(WearNTear wnt) { if (wnt.m_noSupportWear && (Object)(object)wnt.m_nview != (Object)null) { return wnt.m_nview.IsValid(); } return false; } public static float Support(WearNTear wnt) { return wnt.GetSupport(); } public static MaterialInfo Material(WearNTear wnt) { float max = default(float); float min = default(float); float horizontalLoss = default(float); float verticalLoss = default(float); wnt.GetMaterialProperties(ref max, ref min, ref horizontalLoss, ref verticalLoss); return new MaterialInfo { Max = max, Min = min, HorizontalLoss = horizontalLoss, VerticalLoss = verticalLoss }; } public static Vector3 CenterOfMass(WearNTear wnt) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) return wnt.GetCOM(); } public static float Ratio(float support, MaterialInfo material) { if (!(material.Max <= material.Min)) { return Mathf.Clamp01((support - material.Min) / (material.Max - material.Min)); } return 1f; } public static bool IsAtMax(float support, MaterialInfo material) { return support >= material.Max; } public static Color ColorFor(float support, MaterialInfo material) { //IL_0009: 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_007d: Unknown result type (might be due to invalid IL or missing references) //IL_0089: Unknown result type (might be due to invalid IL or missing references) //IL_002e: 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) //IL_0054: Unknown result type (might be due to invalid IL or missing references) if (IsAtMax(support, material)) { return MaxColor; } float num = Ratio(support, material); if (!(num < 0.5f)) { return Color.Lerp(new Color(1f, 0.9f, 0.2f), new Color(0.3f, 1f, 0.35f), (num - 0.5f) * 2f); } return Color.Lerp(new Color(1f, 0.25f, 0.2f), new Color(1f, 0.9f, 0.2f), num * 2f); } } internal enum SourceKind { Terrain, StaticObject, Piece, Braced, None } internal sealed class SourceInfo { public SourceKind Kind; public WearNTear A; public WearNTear B; public float Value; public float AnalyzedAt; } internal static class SupportAnalyzer { private struct SupportPoint { public Vector3 Point; public float Value; public WearNTear Piece; } private static readonly Collider[] s_overlaps = (Collider[])(object)new Collider[128]; private static readonly List<SupportPoint> s_points = new List<SupportPoint>(); public static SourceInfo Analyze(WearNTear wnt) { //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_005e: 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_0065: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Unknown result type (might be due to invalid IL or missing references) //IL_006c: 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) //IL_0078: 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_02d3: Unknown result type (might be due to invalid IL or missing references) //IL_02d8: 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_0134: Unknown result type (might be due to invalid IL or missing references) //IL_0137: Unknown result type (might be due to invalid IL or missing references) //IL_0149: Unknown result type (might be due to invalid IL or missing references) //IL_0151: Unknown result type (might be due to invalid IL or missing references) //IL_0334: Unknown result type (might be due to invalid IL or missing references) //IL_0339: Unknown result type (might be due to invalid IL or missing references) //IL_033a: Unknown result type (might be due to invalid IL or missing references) //IL_033f: Unknown result type (might be due to invalid IL or missing references) //IL_034d: Unknown result type (might be due to invalid IL or missing references) //IL_034f: Unknown result type (might be due to invalid IL or missing references) //IL_0198: Unknown result type (might be due to invalid IL or missing references) //IL_019d: Unknown result type (might be due to invalid IL or missing references) //IL_01a2: 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_01ab: 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_01be: Unknown result type (might be due to invalid IL or missing references) //IL_01bf: Unknown result type (might be due to invalid IL or missing references) //IL_01c4: Unknown result type (might be due to invalid IL or missing references) //IL_01c8: Unknown result type (might be due to invalid IL or missing references) //IL_01cd: 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_0236: Unknown result type (might be due to invalid IL or missing references) //IL_0238: 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) SourceInfo sourceInfo = new SourceInfo { Kind = SourceKind.None, AnalyzedAt = Time.time }; if (wnt.m_colliders == null) { wnt.SetupColliders(); } MaterialInfo materialInfo = Stability.Material(wnt); Vector3 cOM = wnt.GetCOM(); bool flag = false; float best = 0f; WearNTear bestPiece = null; s_points.Clear(); foreach (BoundData bound in wnt.m_bounds) { int num = Physics.OverlapBoxNonAlloc(bound.m_pos, bound.m_size, s_overlaps, bound.m_rot, WearNTear.s_rayMask); for (int i = 0; i < num; i++) { Collider val = s_overlaps[i]; if ((Object)(object)val.attachedRigidbody != (Object)null || val.isTrigger || wnt.m_colliders.Contains(val)) { continue; } if (((Component)val).gameObject.layer == WearNTear.s_terrainLayer) { flag = true; continue; } WearNTear componentInParent = ((Component)val).GetComponentInParent<WearNTear>(); if ((Object)(object)componentInParent == (Object)null) { sourceInfo.Kind = SourceKind.StaticObject; sourceInfo.Value = materialInfo.Max; return sourceInfo; } if (!componentInParent.m_supports) { continue; } float num2 = Vector3.Distance(cOM, componentInParent.GetCOM()) + 0.1f; float num3 = Vector3.Distance(cOM, ((Component)componentInParent).transform.position) + 0.1f; if (num3 < num2 && !wnt.m_forceCorrectCOMCalculation) { num2 = num3; } float support = componentInParent.GetSupport(); Consider(support - materialInfo.HorizontalLoss * num2 * support, componentInParent, ref best, ref bestPiece); Vector3 val2 = WearNTear.FindSupportPoint(cOM, componentInParent, val); if (val2.y < cOM.y + 0.05f) { Vector3 val3 = val2 - cOM; Vector3 normalized = ((Vector3)(ref val3)).normalized; if (normalized.y < 0f) { float num4 = Mathf.Acos(1f - Mathf.Abs(normalized.y)) / (MathF.PI / 2f); float num5 = Mathf.Lerp(materialInfo.HorizontalLoss, materialInfo.VerticalLoss, num4); Consider(support - num5 * num2 * support, componentInParent, ref best, ref bestPiece); } s_points.Add(new SupportPoint { Point = val2, Value = support - materialInfo.VerticalLoss * num2 * support, Piece = componentInParent }); } } } if (flag) { sourceInfo.Kind = SourceKind.Terrain; sourceInfo.Value = materialInfo.Max; return sourceInfo; } if ((Object)(object)bestPiece != (Object)null) { sourceInfo.Kind = SourceKind.Piece; sourceInfo.A = bestPiece; } for (int j = 0; j < s_points.Count - 1; j++) { Vector3 val4 = s_points[j].Point - cOM; val4.y = 0f; for (int k = j + 1; k < s_points.Count; k++) { float num6 = (s_points[j].Value + s_points[k].Value) * 0.5f; if (!(num6 <= best)) { Vector3 val5 = s_points[k].Point - cOM; val5.y = 0f; if (Vector3.Angle(val4, val5) >= 100f) { best = num6; sourceInfo.Kind = SourceKind.Braced; sourceInfo.A = s_points[j].Piece; sourceInfo.B = s_points[k].Piece; } } } } sourceInfo.Value = Mathf.Min(best, materialInfo.Max); return sourceInfo; } private static void Consider(float value, WearNTear piece, ref float best, ref WearNTear bestPiece) { if (value > best) { best = value; bestPiece = piece; } } } public static class MyPluginInfo { public const string PLUGIN_GUID = "s992.StabilityView"; public const string PLUGIN_NAME = "StabilityView"; public const string PLUGIN_VERSION = "0.1.1"; } } namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] internal sealed class IgnoresAccessChecksToAttribute : Attribute { public IgnoresAccessChecksToAttribute(string assemblyName) { } } } namespace ValheimMods.Shared { internal sealed class FrameGuard { private const int MaxErrors = 10; private readonly Behaviour m_owner; private readonly string m_feature; private int m_errors; public FrameGuard(Behaviour owner, string feature) { m_owner = owner; m_feature = feature; } public void Report(Exception e) { m_errors++; if (m_errors == 1) { ModInfo.Log.LogError((object)$"Error in {m_feature}: {e}"); } if (m_errors >= 10) { m_owner.enabled = false; string text = ModInfo.Name + ": " + m_feature + " kept failing and is off until the game restarts. Details are in BepInEx/LogOutput.log."; ModInfo.Log.LogError((object)text); PlayerNotice.Show(text); } } } internal static class Hotkeys { private static readonly KeyCode[] s_modifierKeys; public static bool Pressed(ConfigEntry<KeyboardShortcut> entry) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) KeyboardShortcut value = entry.Value; if ((int)((KeyboardShortcut)(ref value)).MainKey == 0 || !ZInput.GetKeyDown(((KeyboardShortcut)(ref value)).MainKey, false)) { return false; } List<KeyCode> modifiers = ((KeyboardShortcut)(ref value)).Modifiers.ToList(); if (modifiers.Any((KeyCode key) => !ZInput.GetKey(key, false))) { return false; } return !s_modifierKeys.Any((KeyCode key) => !modifiers.Contains(key) && ZInput.GetKey(key, false)); } public static bool Held(ConfigEntry<KeyboardShortcut> entry) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0009: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Unknown result type (might be due to invalid IL or missing references) KeyboardShortcut value = entry.Value; if ((int)((KeyboardShortcut)(ref value)).MainKey != 0 && ZInput.GetKey(((KeyboardShortcut)(ref value)).MainKey, false)) { return ((KeyboardShortcut)(ref value)).Modifiers.All((KeyCode key) => ZInput.GetKey(key, false)); } return false; } public static bool IsTyping() { if (!Console.IsVisible() && !TextInput.IsVisible() && !Menu.IsVisible() && !Minimap.InTextInput()) { if (Object.op_Implicit((Object)(object)Chat.instance)) { return Chat.instance.HasFocus(); } return false; } return true; } static Hotkeys() { KeyCode[] array = new KeyCode[6]; RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/); s_modifierKeys = (KeyCode[])(object)array; } } internal static class ModInfo { public static readonly string Name = typeof(ModInfo).Assembly.GetName().Name; public static readonly string Version = typeof(ModInfo).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? typeof(ModInfo).Assembly.GetName().Version.ToString(3); private static ManualLogSource s_log; public static ManualLogSource Log => s_log ?? (s_log = Logger.CreateLogSource(Name)); } internal static class PatchGuard { private static readonly HashSet<string> s_reported = new HashSet<string>(); public static Exception Contain(Exception exception, string feature) { if (exception == null || !FromThisMod(exception)) { return exception; } if (s_reported.Add(feature)) { ModInfo.Log.LogError((object)$"Error in {feature} (further errors there aren't logged): {exception}"); PlayerNotice.Show(ModInfo.Name + ": " + feature + " hit an error and may not work until the game restarts. Details are in BepInEx/LogOutput.log."); } return null; } private static bool FromThisMod(Exception exception) { Assembly assembly = typeof(PatchGuard).Assembly; for (Exception ex = exception; ex != null; ex = ex.InnerException) { StackFrame[] array = new StackTrace(ex, fNeedFileInfo: false).GetFrames() ?? Array.Empty<StackFrame>(); for (int i = 0; i < array.Length; i++) { if (array[i].GetMethod()?.DeclaringType?.Assembly == assembly) { return true; } } } return false; } } internal sealed class PlayerNotice : MonoBehaviour { private static PlayerNotice s_instance; private readonly List<string> m_pending = new List<string>(); public static void Show(string message) { //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) //IL_0026: Expected O, but got Unknown if (!Object.op_Implicit((Object)(object)s_instance)) { GameObject val = new GameObject(ModInfo.Name + ".PlayerNotice"); Object.DontDestroyOnLoad((Object)val); s_instance = val.AddComponent<PlayerNotice>(); } s_instance.m_pending.Add(message); } private void Update() { if (m_pending.Count == 0 || !Object.op_Implicit((Object)(object)Player.m_localPlayer) || !Object.op_Implicit((Object)(object)MessageHud.instance)) { return; } try { foreach (string item in m_pending) { Send(item); } } catch (Exception ex) { ModInfo.Log.LogWarning((object)("Couldn't show a message in game: " + ex.Message)); } m_pending.Clear(); } private static void Send(string message) { ((Character)Player.m_localPlayer).Message((MessageType)1, message, 0, (Sprite)null, false); if (Object.op_Implicit((Object)(object)Console.instance)) { Console.instance.Print(message); } } } internal static class SafePatching { public static bool PatchAll(Harmony harmony, ManualLogSource log) { List<string> list = new List<string>(); Type[] typesFromAssembly = AccessTools.GetTypesFromAssembly(typeof(SafePatching).Assembly); foreach (Type type in typesFromAssembly) { try { harmony.CreateClassProcessor(type).Patch(); } catch (Exception arg) { list.Add(type.Name); log.LogError((object)$"Patch {type.Name} failed: {arg}"); } } if (list.Count == 0) { return true; } harmony.UnpatchSelf(); string text = ModInfo.Name + " is turned off: this game version changed something it relies on (" + string.Join(", ", list) + "). Check for an update to " + ModInfo.Name + "."; log.LogError((object)text); PlayerNotice.Show(text); return false; } public static void ExpectReplaced(int replaced, int expected, string what) { if (replaced != expected) { throw new InvalidOperationException($"{what}: expected {expected} change(s), made {replaced}"); } } } }