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 BuildGrid v1.1.0
BuildGrid.dll
Decompiled a month 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 Microsoft.CodeAnalysis; using UnityEngine; using UnityEngine.Rendering; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")] [assembly: AssemblyCompany("BuildGrid")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.1.0.0")] [assembly: AssemblyInformationalVersion("1.1.0+b2c656445233306318f6db9818d101024e6e9805")] [assembly: AssemblyProduct("BuildGrid")] [assembly: AssemblyTitle("BuildGrid")] [assembly: AssemblyVersion("1.1.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 BuildGrid { internal sealed class GridRenderer : MonoBehaviour { private const float SampleStep = 1f; private readonly List<LineRenderer> _pool = new List<LineRenderer>(); private Material _material; private GridMode _mode; private bool _built; private bool _hadPlayer; private bool _shown; private Vector3 _builtCenter; private float _builtYaw; private string _builtSettings = string.Empty; private bool _hasAnchor; private Vector3 _anchorPosition; private float _anchorYaw; private void Start() { _mode = Plugin.StartMode.Value; _material = MakeMaterial(); } private void Update() { //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_00a8: Unknown result type (might be due to invalid IL or missing references) //IL_009b: Unknown result type (might be due to invalid IL or missing references) //IL_00ad: Unknown result type (might be due to invalid IL or missing references) //IL_0105: Unknown result type (might be due to invalid IL or missing references) //IL_00f9: Unknown result type (might be due to invalid IL or missing references) KeyboardShortcut value = Plugin.ToggleKey.Value; if (((KeyboardShortcut)(ref value)).IsDown() && !Plugin.IsTyping()) { Cycle(); } Player localPlayer = Player.m_localPlayer; bool flag = (Object)(object)localPlayer != (Object)null; if (flag && !_hadPlayer) { _mode = Plugin.StartMode.Value; _built = false; _hasAnchor = false; } _hadPlayer = flag; if (!flag || _mode == GridMode.Off) { HideAll(); return; } if (_mode == GridMode.Local) { RefreshAnchor(localPlayer); if (!_hasAnchor) { HideAll(); return; } } Vector3 center = ((_mode == GridMode.Global) ? ((Component)localPlayer).transform.position : _anchorPosition); float num = ((_mode == GridMode.Global) ? 0f : _anchorYaw); string text = SettingsKey(); if (!_built || text != _builtSettings || Mathf.Abs(num - _builtYaw) > 0.01f || NeedsSlide(center)) { Rebuild(center, num); _builtSettings = text; } } private void OnDestroy() { foreach (LineRenderer item in _pool) { if ((Object)(object)item != (Object)null) { Object.Destroy((Object)(object)((Component)item).gameObject); } } _pool.Clear(); } private void Cycle() { _mode = ((_mode == GridMode.Off) ? GridMode.Global : ((_mode == GridMode.Global) ? GridMode.Local : GridMode.Off)); _built = false; Plugin.Say((_mode == GridMode.Off) ? "Grid: off" : ((_mode == GridMode.Global) ? "Grid: global" : "Grid: local")); } private void RefreshAnchor(Player player) { //IL_0032: Unknown result type (might be due to invalid IL or missing references) //IL_0037: Unknown result type (might be due to invalid IL or missing references) //IL_003e: 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_008b: 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_0053: Unknown result type (might be due to invalid IL or missing references) //IL_0058: 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) GameObject hoverObject = ((Humanoid)player).GetHoverObject(); Piece val = (((Object)(object)hoverObject != (Object)null) ? hoverObject.GetComponentInParent<Piece>() : null); if ((Object)(object)val == (Object)null || !val.IsPlacedByPlayer()) { return; } Vector3 position = ((Component)val).transform.position; float y = ((Component)val).transform.eulerAngles.y; if (_hasAnchor) { Vector3 val2 = position - _anchorPosition; if (((Vector3)(ref val2)).sqrMagnitude < 0.0001f && Mathf.Abs(y - _anchorYaw) < 0.01f) { return; } } _hasAnchor = true; _anchorPosition = position; _anchorYaw = y; _built = false; } private bool NeedsSlide(Vector3 center) { //IL_0036: Unknown result type (might be due to invalid IL or missing references) //IL_0038: Unknown result type (might be due to invalid IL or missing references) //IL_003d: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_0009: 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) //IL_0010: 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) if (_mode != GridMode.Global) { Vector3 val = center - _builtCenter; return ((Vector3)(ref val)).sqrMagnitude > 0.0001f; } float num = Plugin.GlobalSpacing.Value * 0.5f; Vector3 val2 = center - _builtCenter; val2.y = 0f; return ((Vector3)(ref val2)).sqrMagnitude > num * num; } private void Rebuild(Vector3 center, float yaw) { //IL_005c: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Unknown result type (might be due to invalid IL or missing references) //IL_0061: 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_00a1: Unknown result type (might be due to invalid IL or missing references) //IL_009a: Unknown result type (might be due to invalid IL or missing references) //IL_00a6: Unknown result type (might be due to invalid IL or missing references) //IL_00ff: Unknown result type (might be due to invalid IL or missing references) //IL_010f: Unknown result type (might be due to invalid IL or missing references) //IL_00b1: Unknown result type (might be due to invalid IL or missing references) //IL_00c4: Unknown result type (might be due to invalid IL or missing references) //IL_0125: Unknown result type (might be due to invalid IL or missing references) //IL_014d: Unknown result type (might be due to invalid IL or missing references) //IL_0182: Unknown result type (might be due to invalid IL or missing references) //IL_017a: Unknown result type (might be due to invalid IL or missing references) //IL_020f: 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_0191: Unknown result type (might be due to invalid IL or missing references) //IL_02f4: Unknown result type (might be due to invalid IL or missing references) //IL_02f5: 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_0244: Unknown result type (might be due to invalid IL or missing references) //IL_023c: Unknown result type (might be due to invalid IL or missing references) //IL_01b3: Unknown result type (might be due to invalid IL or missing references) //IL_0251: Unknown result type (might be due to invalid IL or missing references) //IL_0253: Unknown result type (might be due to invalid IL or missing references) //IL_01d4: Unknown result type (might be due to invalid IL or missing references) //IL_0266: Unknown result type (might be due to invalid IL or missing references) //IL_01dc: Unknown result type (might be due to invalid IL or missing references) //IL_0275: Unknown result type (might be due to invalid IL or missing references) //IL_028f: Unknown result type (might be due to invalid IL or missing references) //IL_029e: Unknown result type (might be due to invalid IL or missing references) float value = Plugin.Radius.Value; float num = ((_mode == GridMode.Global) ? Plugin.GlobalSpacing.Value : Plugin.LocalSpacing.Value); bool drape = _mode == GridMode.Global && Plugin.GlobalFollowsTerrain.Value; Color val = ((_mode == GridMode.Global) ? Plugin.GlobalColor.Value : Plugin.LocalColor.Value); int value2 = Plugin.MajorEvery.Value; float value3 = Plugin.HeightOffset.Value; Quaternion rotation = Quaternion.Euler(0f, yaw, 0f); Vector2 val2 = ((_mode == GridMode.Global) ? Vector2.zero : Plugin.LocalOffset.Value); float num2; float num3; int num4; int num5; if (_mode == GridMode.Global) { num2 = Mathf.Floor((center.x - value) / num) * num; num3 = Mathf.Floor((center.z - value) / num) * num; num4 = Mathf.CeilToInt(2f * value / num) + 1; num5 = num4; } else { int num6 = Mathf.CeilToInt(value / num); num2 = (float)(-num6) * num + val2.x; num3 = (float)(-num6) * num + val2.y; num4 = 2 * num6 + 1; num5 = num4; } float flatHeight = center.y + value3; int num7 = 0; for (int i = 0; i < num4; i++) { float num8 = num2 + (float)i * num; bool flag = value2 > 0 && Index(num8, num, val2.x) % value2 == 0; LineRenderer line = Take(num7++); Style(line, flag ? Plugin.MajorColor.Value : val); Lay(line, rotation, center, drape, flatHeight, value3, new Vector3(num8, 0f, (_mode == GridMode.Global) ? (center.z - value) : num3), new Vector3(num8, 0f, (_mode == GridMode.Global) ? (center.z + value) : (num3 + (float)(num5 - 1) * num))); } for (int j = 0; j < num5; j++) { float num9 = num3 + (float)j * num; bool flag2 = value2 > 0 && Index(num9, num, val2.y) % value2 == 0; LineRenderer line2 = Take(num7++); Style(line2, flag2 ? Plugin.MajorColor.Value : val); Lay(line2, rotation, center, drape, flatHeight, value3, new Vector3((_mode == GridMode.Global) ? (center.x - value) : num2, 0f, num9), new Vector3((_mode == GridMode.Global) ? (center.x + value) : (num2 + (float)(num4 - 1) * num), 0f, num9)); } for (int k = num7; k < _pool.Count; k++) { ((Renderer)_pool[k]).enabled = false; } _built = true; _shown = true; _builtCenter = center; _builtYaw = yaw; } private static int Index(float coordinate, float spacing, float offset) { return Mathf.Abs(Mathf.RoundToInt((coordinate - offset) / spacing)); } private void Lay(LineRenderer line, Quaternion rotation, Vector3 center, bool drape, float flatHeight, float lift, Vector3 from, Vector3 to) { //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0014: Unknown result type (might be due to invalid IL or missing references) //IL_004c: Unknown result type (might be due to invalid IL or missing references) //IL_004e: 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_0056: Unknown result type (might be due to invalid IL or missing references) //IL_005b: Unknown result type (might be due to invalid IL or missing references) //IL_005c: 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_005f: 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_0069: 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_0098: Unknown result type (might be due to invalid IL or missing references) bool flag = _mode == GridMode.Local; int num = (line.positionCount = (drape ? Mathf.Max(2, Mathf.CeilToInt(Vector3.Distance(from, to) / 1f) + 1) : 2)); for (int i = 0; i < num; i++) { float num3 = ((num == 1) ? 0f : ((float)i / (float)(num - 1))); Vector3 val = Vector3.Lerp(from, to, num3); if (flag) { val = center + rotation * val; val.y = flatHeight; } else if (drape) { val.y = GroundHeight(val) + lift; } else { val.y = flatHeight; } line.SetPosition(i, val); } ((Renderer)line).enabled = true; } private static float GroundHeight(Vector3 p) { //IL_001e: 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) float result = default(float); if ((Object)(object)ZoneSystem.instance != (Object)null && ZoneSystem.instance.GetGroundHeight(p, ref result)) { return result; } return p.y; } private LineRenderer Take(int index) { while (_pool.Count <= index) { _pool.Add(MakeLine(_pool.Count)); } return _pool[index]; } private void Style(LineRenderer line, Color colour) { //IL_0001: 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) line.startColor = colour; line.endColor = colour; line.widthMultiplier = Plugin.LineWidth.Value; } private LineRenderer MakeLine(int index) { //IL_0011: 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) GameObject val = new GameObject("BuildGrid_Line_" + index); val.transform.SetParent(((Component)this).transform, false); LineRenderer obj = val.AddComponent<LineRenderer>(); obj.useWorldSpace = true; obj.loop = false; obj.numCapVertices = 2; obj.numCornerVertices = 2; obj.textureMode = (LineTextureMode)0; obj.alignment = (LineAlignment)0; ((Renderer)obj).shadowCastingMode = (ShadowCastingMode)0; ((Renderer)obj).receiveShadows = false; ((Renderer)obj).material = _material; obj.positionCount = 0; ((Renderer)obj).enabled = false; return obj; } private static Material MakeMaterial() { //IL_004b: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Unknown result type (might be due to invalid IL or missing references) //IL_005c: Expected O, but got Unknown Shader val = Shader.Find("Sprites/Default") ?? Shader.Find("Unlit/Color") ?? Shader.Find("Particles/Standard Unlit"); if ((Object)(object)val == (Object)null) { Plugin.Log.LogWarning((object)"No unlit shader found; the grid will use whatever Unity gives a bare material."); val = Shader.Find("Standard"); } return new Material(val) { renderQueue = 3100 }; } private string SettingsKey() { //IL_005b: Unknown result type (might be due to invalid IL or missing references) //IL_00a3: 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_00c9: Unknown result type (might be due to invalid IL or missing references) return string.Join("|", Plugin.Radius.Value, Plugin.GlobalSpacing.Value, Plugin.LocalSpacing.Value, Plugin.GlobalFollowsTerrain.Value, Plugin.LocalOffset.Value, Plugin.LineWidth.Value, Plugin.HeightOffset.Value, Plugin.MajorEvery.Value, Plugin.GlobalColor.Value, Plugin.LocalColor.Value, Plugin.MajorColor.Value, _mode); } private void HideAll() { if (!_shown) { return; } foreach (LineRenderer item in _pool) { ((Renderer)item).enabled = false; } _shown = false; _built = false; } } internal enum GridMode { Off, Global, Local } [BepInPlugin("matheba.buildgrid", "BuildGrid", "1.1.0")] [BepInProcess("valheim.exe")] public class Plugin : BaseUnityPlugin { internal static ManualLogSource Log; internal static ConfigEntry<KeyboardShortcut> ToggleKey; internal static ConfigEntry<GridMode> StartMode; internal static ConfigEntry<bool> ShowToggleMessage; internal static ConfigEntry<float> Radius; internal static ConfigEntry<float> GlobalSpacing; internal static ConfigEntry<bool> GlobalFollowsTerrain; internal static ConfigEntry<float> LocalSpacing; internal static ConfigEntry<Vector2> LocalOffset; internal static ConfigEntry<float> LineWidth; internal static ConfigEntry<float> HeightOffset; internal static ConfigEntry<int> MajorEvery; internal static ConfigEntry<Color> GlobalColor; internal static ConfigEntry<Color> LocalColor; internal static ConfigEntry<Color> MajorColor; private GameObject _renderer; private void Awake() { //IL_0022: 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_00ae: Expected O, but got Unknown //IL_00e1: Unknown result type (might be due to invalid IL or missing references) //IL_00eb: Expected O, but got Unknown //IL_013e: Unknown result type (might be due to invalid IL or missing references) //IL_0148: Expected O, but got Unknown //IL_0167: Unknown result type (might be due to invalid IL or missing references) //IL_01a9: Unknown result type (might be due to invalid IL or missing references) //IL_01b3: Expected O, but got Unknown //IL_01e6: Unknown result type (might be due to invalid IL or missing references) //IL_01f0: Expected O, but got Unknown //IL_0218: Unknown result type (might be due to invalid IL or missing references) //IL_0222: Expected O, but got Unknown //IL_024b: 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) //IL_02bb: Unknown result type (might be due to invalid IL or missing references) //IL_02d5: Unknown result type (might be due to invalid IL or missing references) //IL_02df: Expected O, but got Unknown //IL_030f: Unknown result type (might be due to invalid IL or missing references) Log = ((BaseUnityPlugin)this).Logger; ToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("1 - General", "Cycle hotkey", new KeyboardShortcut((KeyCode)121, Array.Empty<KeyCode>()), "Cycles off, global, local. Ignored while you are typing in chat or the console."); StartMode = ((BaseUnityPlugin)this).Config.Bind<GridMode>("1 - General", "Mode on load", GridMode.Off, "Which grid is showing every time you enter a world, whatever it was when you left the last one. The key cycles from there."); ShowToggleMessage = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Show on-screen message", true, "Shows a centre-screen message naming the mode each time you cycle."); Radius = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Grid", "Radius", 16f, new ConfigDescription("How far the grid extends from you, in metres. Bigger costs a little more each frame.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(4f, 48f), Array.Empty<object>())); GlobalSpacing = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Grid", "Global spacing", 1f, new ConfigDescription("Distance between global grid lines, in metres. 1 is half a floor tile. Lines sit on world multiples of this, so they never drift as you walk.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 8f), Array.Empty<object>())); GlobalFollowsTerrain = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Grid", "Global grid follows terrain", true, "On drapes the global grid over the ground, so a bump shows as a bump: that is the one for terraforming. Off draws it flat at your feet."); LocalSpacing = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Grid", "Local spacing", 1f, new ConfigDescription("Distance between local grid lines, in metres.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 8f), Array.Empty<object>())); LocalOffset = ((BaseUnityPlugin)this).Config.Bind<Vector2>("2 - Grid", "Local offset", new Vector2(1f, 1f), "Shifts the local grid within its cell, in metres. A floor tile's pivot is its centre, so half a tile here puts the lines on the tile's edges rather than through its middle."); LineWidth = ((BaseUnityPlugin)this).Config.Bind<float>("3 - Look", "Line width", 0.02f, new ConfigDescription("Width of each line, in metres.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.01f, 0.3f), Array.Empty<object>())); HeightOffset = ((BaseUnityPlugin)this).Config.Bind<float>("3 - Look", "Height above ground", 0.1f, new ConfigDescription("How far above the surface the lines float, in metres. Too low and the ground swallows them on slopes.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 0.5f), Array.Empty<object>())); MajorEvery = ((BaseUnityPlugin)this).Config.Bind<int>("3 - Look", "Major line every", 2, new ConfigDescription("Every Nth line is drawn in the major colour, to make counting easier. 0 turns that off.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 20), Array.Empty<object>())); GlobalColor = ((BaseUnityPlugin)this).Config.Bind<Color>("3 - Look", "Global colour", new Color(0.2f, 0.85f, 1f, 0.55f), "Colour of the global grid."); LocalColor = ((BaseUnityPlugin)this).Config.Bind<Color>("3 - Look", "Local colour", new Color(1f, 0.65f, 0.2f, 0.65f), "Colour of the local grid."); MajorColor = ((BaseUnityPlugin)this).Config.Bind<Color>("3 - Look", "Major line colour", new Color(1f, 1f, 1f, 0.85f), "Colour of every Nth line, in either grid."); _renderer = new GameObject("BuildGrid"); Object.DontDestroyOnLoad((Object)(object)_renderer); _renderer.AddComponent<GridRenderer>(); Log.LogInfo((object)(string.Format("{0} {1} loaded. Hotkey: {2}, ", "BuildGrid", "1.1.0", ToggleKey.Value) + $"start mode: {StartMode.Value}")); } private void OnDestroy() { if ((Object)(object)_renderer != (Object)null) { Object.Destroy((Object)(object)_renderer); } } internal static bool IsTyping() { if (Console.IsVisible() || TextInput.IsVisible()) { return true; } if ((Object)(object)Chat.instance != (Object)null) { return Chat.instance.HasFocus(); } return false; } internal static void Say(string message) { if (ShowToggleMessage.Value && (Object)(object)Player.m_localPlayer != (Object)null) { ((Character)Player.m_localPlayer).Message((MessageType)2, message, 0, (Sprite)null, false); } } } internal static class PluginInfo { internal const string Guid = "matheba.buildgrid"; internal const string Name = "BuildGrid"; internal const string Version = "1.1.0"; } }