Decompiled source of SnappySigns v1.0.0

SnappySigns.dll

Decompiled 5 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("SnappySigns")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+13151aa08c8fe9ebfeb773f174105f0290ac3c54")]
[assembly: AssemblyProduct("Snappy Signs")]
[assembly: AssemblyTitle("SnappySigns")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 SnappySigns
{
	[BepInPlugin("com.syloreon.snappysigns", "Snappy Signs", "1.0.0")]
	public class SnappySignsPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.syloreon.snappysigns";

		public const string PluginName = "Snappy Signs";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> ModEnabled;

		internal static ConfigEntry<bool> Corners;

		internal static ConfigEntry<bool> EdgeMidpoints;

		internal static ConfigEntry<bool> Center;

		private void Awake()
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master toggle. When off, signs are left untouched (takes effect on next world/game load).");
			Corners = ((BaseUnityPlugin)this).Config.Bind<bool>("SnapPoints", "Corners", true, "Add snap points at the four corners of each sign's board.");
			EdgeMidpoints = ((BaseUnityPlugin)this).Config.Bind<bool>("SnapPoints", "EdgeMidpoints", true, "Add snap points at the midpoint of each of the board's four edges.");
			Center = ((BaseUnityPlugin)this).Config.Bind<bool>("SnapPoints", "Center", true, "Add a snap point at the center of each sign's board.");
			new Harmony("com.syloreon.snappysigns").PatchAll();
			Log.LogInfo((object)"Snappy Signs v1.0.0 loaded.");
		}
	}
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	internal static class ZNetScene_Awake_Patch
	{
		private static void Postfix(ZNetScene __instance)
		{
			if (!SnappySignsPlugin.ModEnabled.Value)
			{
				return;
			}
			int num = 0;
			foreach (GameObject prefab in __instance.m_prefabs)
			{
				if (!((Object)(object)prefab == (Object)null) && !((Object)(object)prefab.GetComponent<Sign>() == (Object)null) && !((Object)(object)prefab.GetComponent<Piece>() == (Object)null) && SignSnapper.AddSnapPoints(prefab))
				{
					num++;
				}
			}
			SnappySignsPlugin.Log.LogInfo((object)$"Added snap points to {num} sign prefab(s).");
		}
	}
	internal static class SignSnapper
	{
		private const string SnapTag = "snappoint";

		internal static bool AddSnapPoints(GameObject prefab)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: 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_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: 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)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: 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_0100: 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_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: 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_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: 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_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: 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_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_019b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: 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)
			if (HasSnapPoints(prefab.transform))
			{
				return false;
			}
			if (!TryGetLocalBounds(prefab, out var bounds))
			{
				SnappySignsPlugin.Log.LogWarning((object)("Could not determine bounds for sign '" + ((Object)prefab).name + "'; skipping."));
				return false;
			}
			Vector3 center = ((Bounds)(ref bounds)).center;
			Vector3 extents = ((Bounds)(ref bounds)).extents;
			int num = 0;
			if (extents.y < ((Vector3)(ref extents))[num])
			{
				num = 1;
			}
			if (extents.z < ((Vector3)(ref extents))[num])
			{
				num = 2;
			}
			int num2 = (num + 1) % 3;
			int num3 = (num + 2) % 3;
			Vector3 val = Axis(num2) * ((Vector3)(ref extents))[num2];
			Vector3 val2 = Axis(num3) * ((Vector3)(ref extents))[num3];
			int num4 = 0;
			if (SnappySignsPlugin.Center.Value)
			{
				num4 += Add(prefab.transform, center);
			}
			if (SnappySignsPlugin.Corners.Value)
			{
				num4 += Add(prefab.transform, center + val + val2);
				num4 += Add(prefab.transform, center + val - val2);
				num4 += Add(prefab.transform, center - val + val2);
				num4 += Add(prefab.transform, center - val - val2);
			}
			if (SnappySignsPlugin.EdgeMidpoints.Value)
			{
				num4 += Add(prefab.transform, center + val);
				num4 += Add(prefab.transform, center - val);
				num4 += Add(prefab.transform, center + val2);
				num4 += Add(prefab.transform, center - val2);
			}
			if (num4 == 0)
			{
				return false;
			}
			SnappySignsPlugin.Log.LogInfo((object)$"  '{((Object)prefab).name}': added {num4} snap point(s).");
			return true;
		}

		private static int Add(Transform parent, Vector3 localPos)
		{
			//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_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: 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_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("_snappoint")
			{
				tag = "snappoint",
				layer = ((Component)parent).gameObject.layer
			};
			val.transform.SetParent(parent, false);
			val.transform.localPosition = localPos;
			val.transform.localRotation = Quaternion.identity;
			return 1;
		}

		private static bool HasSnapPoints(Transform root)
		{
			for (int i = 0; i < root.childCount; i++)
			{
				if (((Component)root.GetChild(i)).CompareTag("snappoint"))
				{
					return true;
				}
			}
			return false;
		}

		private static bool TryGetLocalBounds(GameObject prefab, out Bounds bounds)
		{
			//IL_0001: 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)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: 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_0057: 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_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: 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_0096: 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_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: 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_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			bounds = default(Bounds);
			bool flag = false;
			MeshFilter[] componentsInChildren = prefab.GetComponentsInChildren<MeshFilter>(true);
			Transform transform = prefab.transform;
			MeshFilter[] array = componentsInChildren;
			foreach (MeshFilter val in array)
			{
				Mesh sharedMesh = val.sharedMesh;
				if ((Object)(object)sharedMesh == (Object)null)
				{
					continue;
				}
				Bounds bounds2 = sharedMesh.bounds;
				Vector3 center = ((Bounds)(ref bounds2)).center;
				bounds2 = sharedMesh.bounds;
				Vector3 extents = ((Bounds)(ref bounds2)).extents;
				for (int j = -1; j <= 1; j += 2)
				{
					for (int k = -1; k <= 1; k += 2)
					{
						for (int l = -1; l <= 1; l += 2)
						{
							Vector3 val2 = center + new Vector3(extents.x * (float)j, extents.y * (float)k, extents.z * (float)l);
							Vector3 val3 = ((Component)val).transform.TransformPoint(val2);
							Vector3 val4 = transform.InverseTransformPoint(val3);
							if (!flag)
							{
								bounds = new Bounds(val4, Vector3.zero);
								flag = true;
							}
							else
							{
								((Bounds)(ref bounds)).Encapsulate(val4);
							}
						}
					}
				}
			}
			return flag;
		}

		private static Vector3 Axis(int i)
		{
			//IL_0009: 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_0015: Unknown result type (might be due to invalid IL or missing references)
			return (Vector3)(i switch
			{
				0 => Vector3.right, 
				1 => Vector3.up, 
				_ => Vector3.forward, 
			});
		}
	}
}