Decompiled source of GroundPlacementAlign v1.0.0

Ground Placement Align.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Ground Placement Align")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Ground Placement Align")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("591f038c-9fef-4617-af55-3f0bee394ba9")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("LuoXCarrier.GroundPlacementAlign", "GroundPlacementAlign", "1.0.0")]
public class GroundPlacementAlign : BaseUnityPlugin
{
	public static GroundPlacementAlign Instance;

	public ConfigEntry<float> gridSize;

	private void Awake()
	{
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Expected O, but got Unknown
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		Instance = this;
		gridSize = ((BaseUnityPlugin)this).Config.Bind<float>("General", "GridSize", 0.05f, new ConfigDescription("地面物品放置对齐的网格大小,必须大于0。|| Grid size for ground block placement alignment. Must be > 0.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.01f, 100f), Array.Empty<object>()));
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"加载成功,地面物品放置间距为 {gridSize.Value}");
		gridSize.SettingChanged += delegate
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"配置项 GridSize 已修改,新值 = {gridSize.Value}");
		};
		new Harmony("LuoXCarrier.GroundPlacementAlign").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"模组已应用,欢迎使用物品放置对齐");
	}

	public float GetGridSize()
	{
		return gridSize.Value;
	}

	public static void LogInfo(string msg)
	{
		GroundPlacementAlign instance = Instance;
		if (instance != null)
		{
			((BaseUnityPlugin)instance).Logger.LogInfo((object)msg);
		}
	}

	public static void LogWarning(string msg)
	{
		GroundPlacementAlign instance = Instance;
		if (instance != null)
		{
			((BaseUnityPlugin)instance).Logger.LogWarning((object)msg);
		}
	}

	public static void LogError(string msg)
	{
		GroundPlacementAlign instance = Instance;
		if (instance != null)
		{
			((BaseUnityPlugin)instance).Logger.LogError((object)msg);
		}
	}
}
public static class GroundPlacementAlignUtils
{
	public static Vector3 AlignPositionToGrid(Vector3 pos, float gridSize)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: 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_004a: Unknown result type (might be due to invalid IL or missing references)
		if (gridSize <= 0f)
		{
			GroundPlacementAlign.LogWarning("GridSize 无效,使用默认 0.05");
			gridSize = 0.05f;
		}
		float num = Mathf.Round(pos.x / gridSize) * gridSize;
		float num2 = Mathf.Round(pos.y / gridSize) * gridSize;
		float num3 = Mathf.Round(pos.z / gridSize) * gridSize;
		return new Vector3(num, num2, num3);
	}
}
[HarmonyPatch(typeof(PlayerInventory), "HandleNewBlockPlacement")]
public static class Patch_HandleNewBlockPlacement
{
	private static void Postfix(PlayerInventory __instance)
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Expected O, but got Unknown
		//IL_0052: 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)
		//IL_005e: 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)
		FieldInfo fieldInfo = AccessTools.Field(typeof(PlayerInventory), "blockBlueprint");
		if (fieldInfo == null)
		{
			GroundPlacementAlign.LogError("无法找到 PlayerInventory.blockBlueprint 字段");
			return;
		}
		GameObject val = (GameObject)fieldInfo.GetValue(__instance);
		if (!((Object)(object)val == (Object)null))
		{
			float gridSize = GroundPlacementAlign.Instance.GetGridSize();
			Transform transform = val.transform;
			Vector3 val3 = (transform.position = GroundPlacementAlignUtils.AlignPositionToGrid(transform.position, gridSize));
			GroundPlacementAlign.LogInfo($"地面蓝图位置修正为:{val3}");
		}
	}
}
[HarmonyPatch(typeof(PlayerInventory), "PlaceBlockServerRpc")]
public static class Patch_PlaceBlockServerRpc
{
	private static void Prefix(ref Vector3 position)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		float gridSize = GroundPlacementAlign.Instance.GetGridSize();
		position = GroundPlacementAlignUtils.AlignPositionToGrid(position, gridSize);
		GroundPlacementAlign.LogInfo($"服务器端地面物品放置位置修正为:{position}");
	}
}