Decompiled source of AskaPrecisionBuilding v1.0.2

plugins/AskaPrecisionBuilding.dll

Decompiled 13 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Microsoft.CodeAnalysis;
using SSSGame;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("AskaPrecisionBuilding")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+68fe07186b5362ed8ed9228338a768838dbb4a79")]
[assembly: AssemblyProduct("AskaPrecisionBuilding")]
[assembly: AssemblyTitle("AskaPrecisionBuilding")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.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 AskaPrecisionBuilding
{
	[BepInPlugin("AskaPrecisionBuilding", "Aska Precision Building", "1.0.2")]
	public class Plugin : BasePlugin
	{
		internal static ManualLogSource Logger;

		public override void Load()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			//IL_0030: 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_0052: Expected O, but got Unknown
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BasePlugin)this).Log;
			RotationConfig.Bind(((BasePlugin)this).Config);
			SnapState.InitFromConfig();
			ClassInjector.RegisterTypeInIl2Cpp<RotationController>();
			GameObject val = new GameObject("AskaPrecisionBuilding");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			RotationController.Instance = val.AddComponent<RotationController>();
			ManualLogSource logger = Logger;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(36, 3, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("Aska Precision Building");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" v");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("1.0.2");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" loaded. ");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Cycle snap angle with [");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<KeyCode>(RotationConfig.CycleSnapKey.Value);
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("].");
			}
			logger.LogInfo(val2);
		}
	}
	internal static class RotationConfig
	{
		internal static readonly int[] SnapAngles = new int[3] { 15, 45, 90 };

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<KeyCode> RotateLeftKey;

		internal static ConfigEntry<KeyCode> RotateRightKey;

		internal static ConfigEntry<KeyCode> CycleSnapKey;

		internal static ConfigEntry<int> StartSnapAngle;

		internal static ConfigEntry<bool> ShowHud;

		internal static ConfigEntry<bool> DebugLogging;

		internal static void Bind(ConfigFile cfg)
		{
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Expected O, but got Unknown
			Enabled = cfg.Bind<bool>("General", "Enabled", true, "Master switch for the snap-rotate keys. Aska's normal rotate keys (Q/E) are never touched.");
			RotateLeftKey = cfg.Bind<KeyCode>("Keybinds", "SnapRotateLeft", (KeyCode)276, "Rotate the structure one snap step counter-clockwise (single press).");
			RotateRightKey = cfg.Bind<KeyCode>("Keybinds", "SnapRotateRight", (KeyCode)275, "Rotate the structure one snap step clockwise (single press).");
			CycleSnapKey = cfg.Bind<KeyCode>("Keybinds", "CycleSnapAngle", (KeyCode)114, "Cycle the snap step size: 15 -> 45 -> 90 -> 15 ...");
			StartSnapAngle = cfg.Bind<int>("General", "StartSnapAngle", 15, new ConfigDescription("Snap step (degrees) selected when the game starts.", (AcceptableValueBase)(object)new AcceptableValueList<int>(SnapAngles), Array.Empty<object>()));
			ShowHud = cfg.Bind<bool>("HUD", "ShowIndicator", true, "Show a small on-screen indicator of the current snap step.");
			DebugLogging = cfg.Bind<bool>("Debug", "VerboseLogging", false, "Log rotation info to the BepInEx console. Useful while tuning; leave off for normal play.");
		}
	}
	public class RotationController : MonoBehaviour
	{
		internal static RotationController Instance;

		private float _flashTimer;

		private PlayerBuilder _builder;

		private Preview _preview;

		public RotationController(IntPtr ptr)
			: base(ptr)
		{
		}

		private void Update()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: 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_003e: Expected O, but got Unknown
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			if (!RotationConfig.Enabled.Value)
			{
				return;
			}
			if (Input.GetKeyDown(RotationConfig.CycleSnapKey.Value))
			{
				SnapState.Cycle();
				_flashTimer = 2f;
				ManualLogSource logger = Plugin.Logger;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(33, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[PrecisionBuilding] Snap step -> ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(SnapState.Label);
				}
				logger.LogInfo(val);
			}
			if (Input.GetKeyDown(RotationConfig.RotateLeftKey.Value))
			{
				StepSnap(-1);
			}
			if (Input.GetKeyDown(RotationConfig.RotateRightKey.Value))
			{
				StepSnap(1);
			}
			if (_flashTimer > 0f)
			{
				_flashTimer -= Time.deltaTime;
			}
		}

		private void StepSnap(int dir)
		{
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			try
			{
				PlacementTool val = ActiveTool();
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				float num = ReadAngle(val);
				if (float.IsNaN(num))
				{
					return;
				}
				float num2 = SnapState.CurrentAngle;
				float num3 = Mathf.Repeat(Mathf.Round(num / num2) * num2 + (float)dir * num2, 360f);
				WriteAngle(val, num3);
				RecomputePreview(val);
				ApplyPreviewYaw(num3);
				if (RotationConfig.DebugLogging.Value)
				{
					ManualLogSource logger = Plugin.Logger;
					bool flag = default(bool);
					BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(32, 4, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[PrecisionBuilding] Snap ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>((dir > 0) ? "+" : "-");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<float>(num2);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("°: ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<float>(num, "0.#");
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" -> ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<float>(num3, "0.#");
					}
					logger.LogInfo(val2);
				}
			}
			catch (Exception ex)
			{
				Plugin.Logger.LogWarning((object)("[PrecisionBuilding] StepSnap error: " + ex.Message));
			}
		}

		private PlacementTool ActiveTool()
		{
			if ((Object)(object)_builder == (Object)null)
			{
				_builder = Object.FindObjectOfType<PlayerBuilder>();
			}
			if (!((Object)(object)_builder != (Object)null))
			{
				return null;
			}
			return _builder._placementTool;
		}

		private static float ReadAngle(PlacementTool tool)
		{
			AnchorPointPlacementTool val = ((Il2CppObjectBase)tool).TryCast<AnchorPointPlacementTool>();
			if ((Object)(object)val != (Object)null)
			{
				return val._angle;
			}
			GridPlacementTool val2 = ((Il2CppObjectBase)tool).TryCast<GridPlacementTool>();
			if ((Object)(object)val2 != (Object)null)
			{
				return val2._angle;
			}
			return float.NaN;
		}

		private static void WriteAngle(PlacementTool tool, float value)
		{
			AnchorPointPlacementTool val = ((Il2CppObjectBase)tool).TryCast<AnchorPointPlacementTool>();
			if ((Object)(object)val != (Object)null)
			{
				val._angle = value;
				return;
			}
			GridPlacementTool val2 = ((Il2CppObjectBase)tool).TryCast<GridPlacementTool>();
			if ((Object)(object)val2 != (Object)null)
			{
				val2._angle = value;
			}
		}

		private static void RecomputePreview(PlacementTool tool)
		{
			AnchorPointPlacementTool val = ((Il2CppObjectBase)tool).TryCast<AnchorPointPlacementTool>();
			if ((Object)(object)val != (Object)null)
			{
				val._ComputeSnappedPosition();
				return;
			}
			GridPlacementTool val2 = ((Il2CppObjectBase)tool).TryCast<GridPlacementTool>();
			if ((Object)(object)val2 != (Object)null)
			{
				val2._TrySnap(true);
			}
		}

		private void ApplyPreviewYaw(float yaw)
		{
			//IL_0034: 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_003a: 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_0047: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_preview == (Object)null)
			{
				_preview = Object.FindObjectOfType<Preview>();
			}
			if (!((Object)(object)_preview == (Object)null))
			{
				Transform transform = ((Component)_preview).transform;
				Vector3 eulerAngles = transform.eulerAngles;
				transform.eulerAngles = new Vector3(eulerAngles.x, yaw, eulerAngles.z);
			}
		}

		private void OnGUI()
		{
			//IL_003b: 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_004f: 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_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Expected O, but got Unknown
			if (RotationConfig.Enabled.Value && (RotationConfig.ShowHud.Value || !(_flashTimer <= 0f)))
			{
				Rect val = new Rect(12f, 12f, 190f, 26f);
				GUIStyle val2 = new GUIStyle(GUI.skin.box)
				{
					alignment = (TextAnchor)4,
					fontSize = 13,
					fontStyle = (FontStyle)1
				};
				GUI.Box(val, "Snap Step: " + SnapState.Label, val2);
			}
		}
	}
	internal static class SnapState
	{
		private static int _index;

		internal static int CurrentAngle => RotationConfig.SnapAngles[_index];

		internal static bool Active => RotationConfig.Enabled.Value;

		internal static string Label => CurrentAngle + "°";

		internal static void InitFromConfig()
		{
			SetAngle(RotationConfig.StartSnapAngle.Value);
		}

		internal static void Cycle()
		{
			_index = (_index + 1) % RotationConfig.SnapAngles.Length;
		}

		internal static void SetAngle(int degrees)
		{
			for (int i = 0; i < RotationConfig.SnapAngles.Length; i++)
			{
				if (RotationConfig.SnapAngles[i] == degrees)
				{
					_index = i;
					return;
				}
			}
			_index = 0;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "AskaPrecisionBuilding";

		public const string PLUGIN_NAME = "Aska Precision Building";

		public const string PLUGIN_VERSION = "1.0.2";
	}
}