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 MeasureTwice v1.1.0
MeasureTwice.dll
Decompiled a month agousing System; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using Configs; using HarmonyLib; using Jotunn.Configs; using Jotunn.Entities; using Jotunn.Extensions; using Jotunn.Managers; using Jotunn.Utils; using Logging; using MeasureTwice.Patches; using Microsoft.CodeAnalysis; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: AssemblyTitle("MeasureTwice")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("MeasureTwice")] [assembly: AssemblyCopyright("Copyright © 2024")] [assembly: AssemblyTrademark("")] [assembly: ComVisible(false)] [assembly: Guid("08a6f7d7-cf93-4931-aecd-abf2ce6ed34c")] [assembly: AssemblyFileVersion("1.1.0")] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("1.1.0.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 MeasureTwice { [BepInPlugin("Searica.Valheim.MeasureTwice", "MeasureTwice", "1.1.0")] [BepInDependency("com.jotunn.jotunn", "2.30.2")] [NetworkCompatibility(/*Could not decode attribute arguments.*/)] [SynchronizationMode(/*Could not decode attribute arguments.*/)] internal sealed class MeasureTwice : BaseUnityPlugin { public const string PluginName = "MeasureTwice"; internal const string Author = "Searica"; public const string PluginGUID = "Searica.Valheim.MeasureTwice"; public const string PluginVersion = "1.1.0"; internal static MeasureTwice Instance; internal static ConfigFile ConfigFile; internal static ConfigFileWatcher ConfigFileWatcher; internal CustomPiece HorzRulerPiece; internal const string HorzRulerName = "horz_ruler"; internal const string HorzRulerPieceName = "Ruler (Horizontal)"; internal CustomPiece VertRulerPiece; internal const string VertRulerName = "vert_ruler"; internal const string VertRulerPieceName = "Ruler (Vertical)"; internal const string GlobalSection = "Global"; public ConfigEntry<int> TimedDestruction; public ConfigEntry<float> ScrollSpeed; public ConfigEntry<KeyCode> LengthModifierKey; public void Awake() { //IL_0078: Unknown result type (might be due to invalid IL or missing references) //IL_0082: Expected O, but got Unknown Instance = this; ConfigFile = ((BaseUnityPlugin)this).Config; Log.Init(((BaseUnityPlugin)this).Logger); ((BaseUnityPlugin)this).Config.DisableSaveOnConfigSet(); SetUpConfigEntries(); ((BaseUnityPlugin)this).Config.Save(); ((BaseUnityPlugin)this).Config.SaveOnConfigSet = true; Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "Searica.Valheim.MeasureTwice"); Game.isModded = true; PrefabManager.OnVanillaPrefabsAvailable += AddCustomPieces; ConfigFileWatcher = new ConfigFileWatcher(((BaseUnityPlugin)this).Config, 1000L); } internal void SetUpConfigEntries() { Log.Verbosity = ConfigFileExtensions.BindConfigInOrder<Log.InfoLevel>(((BaseUnityPlugin)this).Config, "Global", "Verbosity", Log.InfoLevel.Low, "Logging level.", true, true, true, (AcceptableValueBase)null, (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); LengthModifierKey = ConfigFileExtensions.BindConfigInOrder<KeyCode>(((BaseUnityPlugin)this).Config, "Global", "Length Modifier Key", (KeyCode)307, "Hold this key to scroll and adjust length of the spacer block", false, true, true, (AcceptableValueBase)null, (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); ScrollSpeed = ConfigFileExtensions.BindConfigInOrder<float>(((BaseUnityPlugin)this).Config, "Global", "Scroll Speed", 0.05f, "Chnage in length for each tick of a the scroll wheel.", false, true, true, (AcceptableValueBase)(object)new AcceptableValueRange<float>(-1f, 1f), (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); TimedDestruction = ConfigFileExtensions.BindConfigInOrder<int>(((BaseUnityPlugin)this).Config, "Global", "Timed Destroy", 30, "Number of seconds before spacer block self destructs.", false, true, true, (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 120), (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null); } public void OnDestroy() { ((BaseUnityPlugin)this).Config.Save(); } private void AddCustomPieces() { //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0031: 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_0057: 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_0080: Expected O, but got Unknown //IL_00aa: Unknown result type (might be due to invalid IL or missing references) //IL_00b0: Expected O, but got Unknown //IL_00d4: Unknown result type (might be due to invalid IL or missing references) //IL_00de: Expected O, but got Unknown //IL_0110: 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_0136: Unknown result type (might be due to invalid IL or missing references) //IL_0146: Unknown result type (might be due to invalid IL or missing references) //IL_0168: Unknown result type (might be due to invalid IL or missing references) //IL_016f: Expected O, but got Unknown //IL_0199: Unknown result type (might be due to invalid IL or missing references) //IL_019f: Expected O, but got Unknown //IL_01c3: Unknown result type (might be due to invalid IL or missing references) //IL_01cd: Expected O, but got Unknown GameObject val = PrefabManager.Instance.CreateClonedPrefab("horz_ruler", "wood_beam_1"); val.transform.localScale = new Vector3(val.transform.localScale.x, val.transform.localScale.y * 0.5f, val.transform.localScale.z * 0.5f); val.AddComponent<CustomRuler>().m_horiztonal = true; val.GetComponent<ZNetView>().m_persistent = false; PieceConfig val2 = new PieceConfig(); val2.Name = "Ruler (Horizontal)"; val2.Category = "Ruler"; val2.Requirements = (RequirementConfig[])(object)new RequirementConfig[1] { new RequirementConfig("Wood", 0, 0, false) }; val2.CraftingStation = CraftingStations.None; val2.PieceTable = PieceTables.Hammer; PieceConfig val3 = val2; HorzRulerPiece = new CustomPiece(val, true, val3); PieceManager.Instance.AddPiece(HorzRulerPiece); GameObject val4 = PrefabManager.Instance.CreateClonedPrefab("vert_ruler", "wood_pole"); val4.transform.localScale = new Vector3(val4.transform.localScale.x * 0.5f, val4.transform.localScale.y, val4.transform.localScale.z * 0.5f); val4.AddComponent<CustomRuler>().m_horiztonal = false; val4.GetComponent<ZNetView>().m_persistent = false; val2 = new PieceConfig(); val2.Name = "Ruler (Vertical)"; val2.Category = "Ruler"; val2.Requirements = (RequirementConfig[])(object)new RequirementConfig[1] { new RequirementConfig("Wood", 0, 0, false) }; val2.CraftingStation = CraftingStations.None; val2.PieceTable = PieceTables.Hammer; PieceConfig val5 = val2; VertRulerPiece = new CustomPiece(val4, true, val5); PieceManager.Instance.AddPiece(VertRulerPiece); PrefabManager.OnVanillaPrefabsAvailable -= AddCustomPieces; } } } namespace MeasureTwice.Patches { [HarmonyPatch] internal class CustomRuler : MonoBehaviour { public float m_timeout = 1f; private ZNetView m_nview; private static bool m_triggerOnPlaced; public bool m_horiztonal; public void Awake() { m_nview = ((Component)this).GetComponent<ZNetView>(); m_timeout = MeasureTwice.Instance.TimedDestruction.Value; if (m_triggerOnPlaced) { ApplyScale(); ((MonoBehaviour)this).InvokeRepeating("DestroyNow", m_timeout, 1f); } } public float GetLastOriginalLength() { //IL_001f: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) if (m_horiztonal) { return ((Component)this).transform.localScale.x; } return ((Component)this).transform.localScale.y; } public static void SetTriggerOnPlaced(bool trigger) { m_triggerOnPlaced = trigger; } [HarmonyPrefix] [HarmonyPatch(typeof(Player), "PlacePiece")] private static void PlacePiecePrefix() { SetTriggerOnPlaced(trigger: true); } [HarmonyPostfix] [HarmonyPatch(typeof(Player), "PlacePiece")] private static void PlacePiecePostfix() { SetTriggerOnPlaced(trigger: false); } public void ApplyScale() { //IL_000c: Unknown result type (might be due to invalid IL or missing references) ((Component)this).transform.localScale = ScaleManager.GetScaleOnPlaced(m_horiztonal); } public void DestroyNow() { if (Object.op_Implicit((Object)(object)m_nview)) { if (m_nview.IsValid()) { if (!m_nview.HasOwner()) { m_nview.ClaimOwnership(); } if (m_nview.IsOwner()) { ZNetScene.instance.Destroy(((Component)this).gameObject); } } } else { Object.Destroy((Object)(object)((Component)this).gameObject); } } } [HarmonyPatch] internal static class ScaleManager { private class RulerInfo { public float LastOriginalLength; public float LastTotalDelta; public Vector3 LastGhostScale = Vector3.one; public void Reset() { //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) LastOriginalLength = 0f; LastTotalDelta = 0f; LastGhostScale = Vector3.one; } } public const float MinLength = 0.1f; public const float MaxLength = 10f; private const float Tolerance = 0.01f; private static RulerInfo HorzRulerInfo = new RulerInfo(); private static RulerInfo VertRulerInfo = new RulerInfo(); private static RulerInfo ActiveRulerInfo = new RulerInfo(); private static bool SpacerBlockIsInUse = false; private static bool DisabledScroll = false; public static Vector3 GetScaleOnPlaced(bool horiztonal) { //IL_0013: 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) if (horiztonal) { return HorzRulerInfo.LastGhostScale; } return VertRulerInfo.LastGhostScale; } [HarmonyPrefix] [HarmonyPatch(typeof(Player), "UpdatePlacement")] private static void UpdatePlacementPrefix(Player __instance) { //IL_00b1: Unknown result type (might be due to invalid IL or missing references) //IL_00b6: Unknown result type (might be due to invalid IL or missing references) //IL_008a: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_00df: Unknown result type (might be due to invalid IL or missing references) if (!Object.op_Implicit((Object)(object)__instance) || (Object)(object)__instance != (Object)(object)Player.m_localPlayer) { return; } if (!((Character)__instance).InPlaceMode() || Hud.IsPieceSelectionVisible()) { if (SpacerBlockIsInUse) { SpacerBlockIsInUse = false; VertRulerInfo.Reset(); HorzRulerInfo.Reset(); } } else { if (!IsValidSelectedPiece(__instance, out var spacerBlock)) { return; } if (!SpacerBlockIsInUse && Object.op_Implicit((Object)(object)spacerBlock)) { SpacerBlockIsInUse = true; if (spacerBlock.m_horiztonal) { HorzRulerInfo.LastOriginalLength = spacerBlock.GetLastOriginalLength(); HorzRulerInfo.LastGhostScale = ((Component)spacerBlock).transform.localScale; } else { VertRulerInfo.LastOriginalLength = spacerBlock.GetLastOriginalLength(); VertRulerInfo.LastGhostScale = ((Component)spacerBlock).transform.localScale; } } if (ShouldModifyLength()) { DisabledScroll = true; ZInput instance = ZInput.instance; if (instance != null) { instance.m_mouseScrollDeltaAction.Disable(); } SetLength(__instance, spacerBlock, Input.mouseScrollDelta.y * MeasureTwice.Instance.ScrollSpeed.Value); } RefreshGhostScale(__instance, spacerBlock); } } [HarmonyPostfix] [HarmonyPriority(0)] [HarmonyPatch(typeof(Player), "UpdatePlacement")] private static void UpdatePlacementPostfix(Player __instance, ref int __state) { if (DisabledScroll) { DisabledScroll = false; ZInput instance = ZInput.instance; if (instance != null) { instance.m_mouseScrollDeltaAction.Enable(); } } } internal static bool IsValidSelectedPiece(Player player, out CustomRuler spacerBlock) { Piece selectedPiece = player.GetSelectedPiece(); if (!Object.op_Implicit((Object)(object)selectedPiece) || !((Component)selectedPiece).TryGetComponent<CustomRuler>(ref spacerBlock)) { spacerBlock = null; return false; } return Object.op_Implicit((Object)(object)spacerBlock); } internal static bool ShouldModifyLength() { //IL_000a: 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) if (Input.GetKey(MeasureTwice.Instance.LengthModifierKey.Value)) { return Input.mouseScrollDelta.y != 0f; } return false; } public static float ModifyLength(float length, float delta) { return Mathf.Clamp(length + delta, 0.1f, 10f); } private static void SetLength(Player player, CustomRuler spacerBlock, float delta) { if (spacerBlock.m_horiztonal) { HorzRulerInfo.LastTotalDelta = Mathf.Clamp(HorzRulerInfo.LastTotalDelta + delta, 0.1f - HorzRulerInfo.LastOriginalLength, 10f - HorzRulerInfo.LastOriginalLength); HorzRulerInfo.LastGhostScale.x = ModifyLength(HorzRulerInfo.LastOriginalLength, HorzRulerInfo.LastTotalDelta); ((Character)player).Message((MessageType)2, $"Ruler Length: {HorzRulerInfo.LastGhostScale.x:#,0.000}", 0, (Sprite)null, false); } else { VertRulerInfo.LastTotalDelta = Mathf.Clamp(VertRulerInfo.LastTotalDelta + delta, 0.1f - VertRulerInfo.LastOriginalLength, 10f - VertRulerInfo.LastOriginalLength); VertRulerInfo.LastGhostScale.y = ModifyLength(VertRulerInfo.LastOriginalLength, VertRulerInfo.LastTotalDelta); ((Character)player).Message((MessageType)2, $"Ruler Length: {VertRulerInfo.LastGhostScale.y:#,0.000}", 0, (Sprite)null, false); } } private static void RefreshGhostScale(Player player, CustomRuler spacerBlock) { //IL_0063: Unknown result type (might be due to invalid IL or missing references) //IL_006d: Unknown result type (might be due to invalid IL or missing references) //IL_008e: Unknown result type (might be due to invalid IL or missing references) if (!SpacerBlockIsInUse || !Object.op_Implicit((Object)(object)player.m_placementGhost)) { return; } if (spacerBlock.m_horiztonal && HorzRulerInfo.LastOriginalLength > 0f) { ActiveRulerInfo = HorzRulerInfo; } else { if (!(VertRulerInfo.LastOriginalLength > 0f)) { return; } ActiveRulerInfo = VertRulerInfo; } if (Vector3.Distance(player.m_placementGhost.transform.localScale, ActiveRulerInfo.LastGhostScale) > 0.01f) { player.m_placementGhost.transform.localScale = ActiveRulerInfo.LastGhostScale; } } } } namespace Logging { internal static class Log { internal enum InfoLevel { Low, Medium, High } private static ManualLogSource logSource; internal static ConfigEntry<InfoLevel> Verbosity { get; set; } internal static InfoLevel VerbosityLevel => Verbosity.Value; internal static bool IsVerbosityLow => Verbosity.Value >= InfoLevel.Low; internal static bool IsVerbosityMedium => Verbosity.Value >= InfoLevel.Medium; internal static bool IsVerbosityHigh => Verbosity.Value >= InfoLevel.High; internal static void Init(ManualLogSource logSource) { Log.logSource = logSource; } internal static void LogDebug(object data) { logSource.LogDebug(data); } internal static void LogError(object data) { logSource.LogError(data); } internal static void LogFatal(object data) { logSource.LogFatal(data); } internal static void LogMessage(object data) { logSource.LogMessage(data); } internal static void LogWarning(object data) { logSource.LogWarning(data); } internal static void LogInfo(object data, InfoLevel level = InfoLevel.Low) { if (Verbosity == null || VerbosityLevel >= level) { logSource.LogInfo(data); } } internal static void LogGameObject(GameObject prefab, bool includeChildren = false) { //IL_006b: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Expected O, but got Unknown LogInfo("***** " + ((Object)prefab).name + " *****"); Component[] components = prefab.GetComponents<Component>(); for (int i = 0; i < components.Length; i++) { LogComponent(components[i]); } if (!includeChildren) { return; } LogInfo("***** " + ((Object)prefab).name + " (children) *****"); foreach (Transform item in prefab.transform) { Transform val = item; if (Object.op_Implicit((Object)(object)val)) { LogInfo(" - " + ((Object)val).name); components = ((Component)val).GetComponents<Component>(); for (int i = 0; i < components.Length; i++) { LogComponent(components[i]); } } } } internal static void LogComponent(Component compo) { if (!Object.op_Implicit((Object)(object)compo)) { return; } try { LogInfo("--- " + ((object)compo).GetType().Name + ": " + ((Object)compo).name + " ---"); } catch (Exception ex) { LogError(ex.ToString()); LogWarning("Could not get type name for component!"); return; } try { foreach (PropertyInfo declaredProperty in AccessTools.GetDeclaredProperties(((object)compo).GetType())) { try { LogInfo($" - {declaredProperty.Name} = {declaredProperty.GetValue(compo)}"); } catch (Exception ex2) { LogError(ex2.ToString()); LogWarning("Could not get property: " + declaredProperty.Name + " for component!"); } } } catch (Exception ex3) { LogError(ex3.ToString()); LogWarning("Could not get properties for component!"); } try { foreach (FieldInfo declaredField in AccessTools.GetDeclaredFields(((object)compo).GetType())) { try { LogInfo($" - {declaredField.Name} = {declaredField.GetValue(compo)}"); } catch (Exception ex4) { LogError(ex4.ToString()); LogWarning("Could not get field: " + declaredField.Name + " for component!"); } } } catch (Exception ex5) { LogError(ex5.ToString()); LogWarning("Could not get fields for component!"); } } } } namespace Configs { public static class ConfigFileExtensions { public static bool DisableSaveOnConfigSet(this ConfigFile configFile) { bool saveOnConfigSet = configFile.SaveOnConfigSet; configFile.SaveOnConfigSet = false; return saveOnConfigSet; } } internal static class SafeInvokeEvent { internal static void SafeInvoke(this Action events) { if (events == null) { return; } Delegate[] invocationList = events.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { Action action = (Action)invocationList[i]; try { action(); } catch (Exception arg) { Log.LogWarning("Exception thrown at event " + new StackFrame(1).GetMethod().Name + $" in {action.Method.DeclaringType.Name}.{action.Method.Name}:\n{arg}"); } } } } }