Decompiled source of ProductAlign v1.1.0

Product Align.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
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("Product Align")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Product Align")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e27f2535-9332-4363-beb9-c94d043b8cc4")]
[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.ProductAlign", "ProductAlign", "1.1.0")]
public class ProductAlign : BaseUnityPlugin
{
	public static ProductAlign 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 item 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.ProductAlign").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"模组已应用,欢迎使用商品放置对齐");
	}

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

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

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

	public static void LogError(string msg)
	{
		ProductAlign instance = Instance;
		if (instance != null)
		{
			((BaseUnityPlugin)instance).Logger.LogError((object)msg);
		}
	}
}
public static class PositionAlignUtils
{
	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)
		{
			ProductAlign.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), "HandleItemPlacement")]
public static class Patch_HandleItemPlacement
{
	private static float lastAlignedX = float.NaN;

	private static float lastAlignedZ = float.NaN;

	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_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: 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_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: 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_0097: 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_00b4: Unknown result type (might be due to invalid IL or missing references)
		FieldInfo fieldInfo = AccessTools.Field(typeof(PlayerInventory), "itemBlueprint");
		if (fieldInfo == null)
		{
			ProductAlign.LogError("无法找到 PlayerInventory.itemBlueprint 字段");
			return;
		}
		GameObject val = (GameObject)fieldInfo.GetValue(__instance);
		if (!((Object)(object)val == (Object)null))
		{
			float gridSize = ProductAlign.Instance.GetGridSize();
			Transform transform = val.transform;
			Vector3 val2 = PositionAlignUtils.AlignPositionToGrid(transform.position, gridSize);
			bool num = !Mathf.Approximately(val2.x, lastAlignedX);
			bool flag = !Mathf.Approximately(val2.z, lastAlignedZ);
			transform.position = val2;
			if (num || flag)
			{
				lastAlignedX = val2.x;
				lastAlignedZ = val2.z;
				ProductAlign.LogInfo($"商品蓝图位置修正为:{val2}");
			}
		}
	}
}
[HarmonyPatch]
public static class Patch_ServerPlaceItem
{
	[CompilerGenerated]
	private sealed class <TargetMethods>d__0 : IEnumerable<MethodBase>, IEnumerable, IEnumerator<MethodBase>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private MethodBase <>2__current;

		private int <>l__initialThreadId;

		private Type <type>5__2;

		MethodBase IEnumerator<MethodBase>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <TargetMethods>d__0(int <>1__state)
		{
			this.<>1__state = <>1__state;
			<>l__initialThreadId = Environment.CurrentManagedThreadId;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<type>5__2 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<type>5__2 = typeof(PlayerInventory);
				<>2__current = AccessTools.Method(<type>5__2, "PlaceItemOnContainerServerRpc", (Type[])null, (Type[])null);
				<>1__state = 1;
				return true;
			case 1:
				<>1__state = -1;
				<>2__current = AccessTools.Method(<type>5__2, "PlaceItemOnStandServerRpc", (Type[])null, (Type[])null);
				<>1__state = 2;
				return true;
			case 2:
				<>1__state = -1;
				return false;
			}
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}

		[DebuggerHidden]
		IEnumerator<MethodBase> IEnumerable<MethodBase>.GetEnumerator()
		{
			if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
			{
				<>1__state = 0;
				return this;
			}
			return new <TargetMethods>d__0(0);
		}

		[DebuggerHidden]
		IEnumerator IEnumerable.GetEnumerator()
		{
			return ((IEnumerable<MethodBase>)this).GetEnumerator();
		}
	}

	[IteratorStateMachine(typeof(<TargetMethods>d__0))]
	private static IEnumerable<MethodBase> TargetMethods()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <TargetMethods>d__0(-2);
	}

	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 = ProductAlign.Instance.GetGridSize();
		position = PositionAlignUtils.AlignPositionToGrid(position, gridSize);
		ProductAlign.LogInfo($"服务器端商品放置位置修正为:{position}");
	}
}