Decompiled source of SnapPointsByComfy v1.0.0

SnapPointsByComfy.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
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 ComfyLib;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SnapPointsByComfy")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SnapPointsByComfy")]
[assembly: AssemblyCopyright("Copyright © 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("F2EA69C9-259B-4656-8530-C13E6202C834")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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 SnapPointsByComfy
{
	public static class PluginConfig
	{
		public static ConfigEntry<bool> IsModEnabled { get; private set; }

		public static ConfigEntry<bool> IsCenterSnapPointEnabled { get; private set; }

		public static ConfigEntry<bool> ResetOnPieceChange { get; private set; }

		public static ConfigEntry<SnapManager.SnapMode> SnapModeSelected { get; private set; }

		public static ConfigEntry<KeyboardShortcut> EnableModToggleKey { get; private set; }

		public static ConfigEntry<KeyboardShortcut> IterateSourceSnapPointKey { get; private set; }

		public static ConfigEntry<KeyboardShortcut> IterateTargetSnapPointKey { get; private set; }

		public static ConfigEntry<KeyboardShortcut> SnapModeToggleKey { get; private set; }

		public static void BindConfig(ConfigFile config)
		{
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: 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)
			IsModEnabled = config.BindInOrder("_Global", "isModEnabled", defaultValue: true, "Globally enable or disable this mod.");
			IsCenterSnapPointEnabled = config.BindInOrder("Options", "isCenterSnapPointEnabled", defaultValue: true, "Enables an additional snap point at the center of each piece.");
			ResetOnPieceChange = config.BindInOrder("Options", "resetOnPieceChange", defaultValue: false, "Reset the current snap point when a new piece is selected.");
			SnapModeSelected = config.BindInOrder("Mode", "sourcePieceModeSelected", SnapManager.SnapMode.Cycle, "Mode for handling snap points for the piece currently being placed (Cycle or Closest).");
			EnableModToggleKey = config.BindInOrder<KeyboardShortcut>("KeyBinds", "enableModToggleKey", new KeyboardShortcut((KeyCode)305, Array.Empty<KeyCode>()), "Key bind to enable/disable the mod.");
			IterateSourceSnapPointKey = config.BindInOrder<KeyboardShortcut>("KeyBinds", "iterateSourceSnapPointsKey", new KeyboardShortcut((KeyCode)306, Array.Empty<KeyCode>()), "Key bind to iterate through the snap points of the piece currently being placed.");
			IterateTargetSnapPointKey = config.BindInOrder<KeyboardShortcut>("KeyBinds", "iterateTargetSnapPointKey", new KeyboardShortcut((KeyCode)308, Array.Empty<KeyCode>()), "Key bind to iterate through the snap points of the piece currently being targeted.");
			SnapModeToggleKey = config.BindInOrder<KeyboardShortcut>("KeyBinds", "enableClosestSourceSnapPointKey", new KeyboardShortcut((KeyCode)301, Array.Empty<KeyCode>()), "Key bind to toggle between closest snap point enabled or disabled for the piece being placed.");
			IsCenterSnapPointEnabled.OnSettingChanged<bool>(SnapManager.RebuildSnapPoints);
		}
	}
	public static class SnapManager
	{
		public enum SnapMode
		{
			Cycle,
			Closest
		}

		public static Piece LastRayPiece;

		public static Transform Source;

		public static Transform Target;

		public static List<Transform> _sourceSnapPoints;

		public static List<Transform> _targetSnapPoints;

		private static int _targetSnapPointIndex;

		private static int _sourceSnapPointIndex;

		public static void ClampSourceSnapPoint()
		{
			_sourceSnapPointIndex = Math.Min(_sourceSnapPointIndex, Math.Max(_sourceSnapPoints.Count - 1, 0));
		}

		public static void ClampTargetSnapPoint()
		{
			_targetSnapPointIndex = Math.Min(_targetSnapPointIndex, Math.Max(_targetSnapPoints.Count - 1, 0));
		}

		public static List<Transform> GetSnapPoints(Transform piece)
		{
			List<Transform> list = new List<Transform>();
			if ((Object)(object)piece == (Object)null)
			{
				return list;
			}
			for (int i = 0; i < piece.childCount; i++)
			{
				Transform child = piece.GetChild(i);
				if (((Component)child).CompareTag("snappoint"))
				{
					list.Add(child);
				}
			}
			if (PluginConfig.IsCenterSnapPointEnabled.Value)
			{
				list.Add(((Component)piece).transform);
			}
			return list;
		}

		public static Vector3 GetOffset()
		{
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: 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_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			if (PluginConfig.SnapModeSelected.Value == SnapMode.Closest)
			{
				if (!Object.op_Implicit((Object)(object)Player.m_localPlayer.m_placementMarkerInstance))
				{
					return Vector3.zero;
				}
				return _targetSnapPoints.OrderBy((Transform snapPoint) => Vector3.Distance(Player.m_localPlayer.m_placementMarkerInstance.transform.position, snapPoint.position)).First().position - _sourceSnapPoints[_sourceSnapPointIndex].position;
			}
			return _targetSnapPoints[_targetSnapPointIndex].position - _sourceSnapPoints[_sourceSnapPointIndex].position;
		}

		public static bool IsValidSnapPoints()
		{
			if (_targetSnapPoints.Any())
			{
				return _sourceSnapPoints.Any();
			}
			return false;
		}

		public static void NextSourceSnapPoint()
		{
			_sourceSnapPointIndex++;
			if (_sourceSnapPointIndex >= GetSnapPoints(Source).Count)
			{
				_sourceSnapPointIndex = 0;
			}
		}

		public static void NextTargetSnapPoint()
		{
			_targetSnapPointIndex++;
			if (_targetSnapPointIndex >= GetSnapPoints(Target).Count)
			{
				_targetSnapPointIndex = 0;
			}
		}

		public static void RebuildSnapPoints()
		{
			_sourceSnapPoints = GetSnapPoints(Source);
			_sourceSnapPointIndex = 0;
			_targetSnapPoints = GetSnapPoints(Target);
			_targetSnapPointIndex = 0;
		}

		public static void ResetSourceSnapPoint()
		{
			_sourceSnapPointIndex = 0;
		}

		public static void ResetTargetSnapPoint()
		{
			_targetSnapPointIndex = 0;
		}

		public static void SetSource(Transform parent)
		{
			Source = parent;
			_sourceSnapPoints = GetSnapPoints(Source);
		}

		public static void SetTarget(Transform parent)
		{
			Target = parent;
			_targetSnapPoints = GetSnapPoints(Target);
		}

		public static void ToggleSnapMode()
		{
			if (PluginConfig.SnapModeSelected.Value == SnapMode.Cycle)
			{
				PluginConfig.SnapModeSelected.Value = SnapMode.Closest;
			}
			else
			{
				PluginConfig.SnapModeSelected.Value = SnapMode.Cycle;
			}
		}
	}
	[HarmonyPatch(typeof(Player))]
	internal static class PlayerPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("PieceRayTest")]
		public static void PieceRayTestPostfix(Player __instance, ref bool __result, ref Vector3 point, ref Vector3 normal, ref Piece piece, ref Heightmap heightmap, ref Collider waterSurface, bool water)
		{
			SnapManager.LastRayPiece = piece;
		}

		[HarmonyPostfix]
		[HarmonyPatch("UpdatePlacementGhost")]
		private static void UpdatePlacementGhostPostfix(GameObject ___m_placementGhost, bool flashGuardStone)
		{
			//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_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: 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_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: 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_013c: Unknown result type (might be due to invalid IL or missing references)
			KeyboardShortcut value = PluginConfig.EnableModToggleKey.Value;
			if (ZInput.GetKeyDown(((KeyboardShortcut)(ref value)).MainKey, true))
			{
				SnapPointsByComfy.ToggleModEnabled();
			}
			if (!PluginConfig.IsModEnabled.Value || (Object)(object)___m_placementGhost == (Object)null)
			{
				return;
			}
			value = PluginConfig.SnapModeToggleKey.Value;
			if (ZInput.GetKeyDown(((KeyboardShortcut)(ref value)).MainKey, true))
			{
				SnapManager.ToggleSnapMode();
				SnapPointsByComfy.SendModeChangeMessage();
			}
			Piece component = ___m_placementGhost.GetComponent<Piece>();
			if ((Object)(object)component == (Object)null || !Object.op_Implicit((Object)(object)SnapManager.LastRayPiece))
			{
				return;
			}
			if ((Object)(object)SnapManager.Target != (Object)(object)((Component)SnapManager.LastRayPiece).transform)
			{
				SnapManager.SetTarget(((Component)SnapManager.LastRayPiece).transform);
				if (PluginConfig.ResetOnPieceChange.Value)
				{
					SnapManager.ResetTargetSnapPoint();
				}
				SnapManager.ClampTargetSnapPoint();
			}
			if ((Object)(object)SnapManager.Source != (Object)(object)((Component)component).transform)
			{
				SnapManager.SetSource(((Component)component).transform);
				if (PluginConfig.ResetOnPieceChange.Value)
				{
					SnapManager.ResetSourceSnapPoint();
				}
				SnapManager.ClampSourceSnapPoint();
			}
			if (SnapManager.IsValidSnapPoints())
			{
				value = PluginConfig.IterateSourceSnapPointKey.Value;
				if (ZInput.GetKeyDown(((KeyboardShortcut)(ref value)).MainKey, true))
				{
					SnapManager.NextSourceSnapPoint();
				}
				value = PluginConfig.IterateTargetSnapPointKey.Value;
				if (ZInput.GetKeyDown(((KeyboardShortcut)(ref value)).MainKey, true))
				{
					SnapManager.NextTargetSnapPoint();
				}
				Transform transform = ___m_placementGhost.transform;
				transform.position += SnapManager.GetOffset();
			}
		}
	}
	[BepInPlugin("bruce.comfy.valheim.snappointsbycomfy", "SnapPointsByComfy", "1.0.0")]
	public sealed class SnapPointsByComfy : BaseUnityPlugin
	{
		public const string PluginGuid = "bruce.comfy.valheim.snappointsbycomfy";

		public const string PluginName = "SnapPointsByComfy";

		public const string PluginVersion = "1.0.0";

		private static ManualLogSource _logger;

		private void Awake()
		{
			_logger = ((BaseUnityPlugin)this).Logger;
			PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "bruce.comfy.valheim.snappointsbycomfy");
		}

		public static void SendModeChangeMessage()
		{
			((Character)Player.m_localPlayer).Message((MessageType)1, $"Snap Points Made Easy: Switched to {PluginConfig.SnapModeSelected.Value}", 0, (Sprite)null, false);
		}

		public static void ToggleModEnabled()
		{
			PluginConfig.IsModEnabled.Value = !PluginConfig.IsModEnabled.Value;
		}

		public static void LogInfo(object obj)
		{
			_logger.LogInfo((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {obj}");
		}

		public static void LogError(object obj)
		{
			_logger.LogError((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {obj}");
		}
	}
}
namespace ComfyLib
{
	public static class ConfigFileExtensions
	{
		internal sealed class ConfigurationManagerAttributes
		{
			public Action<ConfigEntryBase> CustomDrawer;

			public bool? Browsable;

			public bool? HideDefaultButton;

			public bool? HideSettingName;

			public bool? IsAdvanced;

			public int? Order;

			public bool? ReadOnly;
		}

		private static readonly Dictionary<string, int> _sectionToSettingOrder = new Dictionary<string, int>();

		private static int GetSettingOrder(string section)
		{
			if (!_sectionToSettingOrder.TryGetValue(section, out var value))
			{
				value = 0;
			}
			_sectionToSettingOrder[section] = value - 1;
			return value;
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, AcceptableValueBase acceptableValues, bool browsable = true, bool hideDefaultButton = false, bool hideSettingName = false, bool isAdvanced = false, bool readOnly = false)
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, acceptableValues, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Browsable = browsable,
					CustomDrawer = null,
					HideDefaultButton = hideDefaultButton,
					HideSettingName = hideSettingName,
					IsAdvanced = isAdvanced,
					Order = GetSettingOrder(section),
					ReadOnly = readOnly
				}
			}));
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, Action<ConfigEntryBase> customDrawer = null, bool browsable = true, bool hideDefaultButton = false, bool hideSettingName = false, bool isAdvanced = false, bool readOnly = false)
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)null, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Browsable = browsable,
					CustomDrawer = customDrawer,
					HideDefaultButton = hideDefaultButton,
					HideSettingName = hideSettingName,
					IsAdvanced = isAdvanced,
					Order = GetSettingOrder(section),
					ReadOnly = readOnly
				}
			}));
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action settingChangedHandler)
		{
			configEntry.SettingChanged += delegate
			{
				settingChangedHandler();
			};
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<T> settingChangedHandler)
		{
			configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				settingChangedHandler((T)((SettingChangedEventArgs)eventArgs).ChangedSetting.BoxedValue);
			};
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<ConfigEntry<T>> settingChangedHandler)
		{
			configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				settingChangedHandler((ConfigEntry<T>)(object)((SettingChangedEventArgs)eventArgs).ChangedSetting);
			};
		}
	}
}