Decompiled source of Productalignment v1.0.2

Productalignment.dll

Decompiled 3 days 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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ClassLibrary1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ClassLibrary1")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("2771de1f-a90e-4fe9-9c9c-9350f0206647")]
[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.Productalignment", "Productalignment", "1.0.0")]
public class Productalignment : BaseUnityPlugin
{
	public static Productalignment Instance;

	public ConfigEntry<float> gridSize;

	private void Awake()
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Expected O, but got Unknown
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Expected O, but got Unknown
		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}");
		};
		Harmony val = new Harmony("LuoXCarrier.Productalignment");
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Harmony补丁已应用");
	}

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

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

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

	public static void LogError(string msg)
	{
		Productalignment instance = Instance;
		if (instance != null)
		{
			((BaseUnityPlugin)instance).Logger.LogError((object)msg);
		}
	}
}
public static class Utils
{
	public static Vector3 AlignPositionToGrid(Vector3 pos, float gridSize)
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: 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_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_0060: Unknown result type (might be due to invalid IL or missing references)
		if (gridSize <= 0f)
		{
			Productalignment.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_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Expected O, but got Unknown
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: 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_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: 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_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		FieldInfo fieldInfo = AccessTools.Field(typeof(PlayerInventory), "itemBlueprint");
		if (fieldInfo == null)
		{
			Productalignment.LogError("无法找到PlayerInventory.itemBlueprint字段");
			return;
		}
		GameObject val = (GameObject)fieldInfo.GetValue(__instance);
		if (!((Object)(object)val == (Object)null))
		{
			float gridSize = Productalignment.Instance.GetGridSize();
			Transform transform = val.transform;
			Vector3 val2 = Utils.AlignPositionToGrid(transform.position, gridSize);
			bool flag = !Mathf.Approximately(val2.x, lastAlignedX);
			bool flag2 = !Mathf.Approximately(val2.z, lastAlignedZ);
			transform.position = val2;
			if (flag || flag2)
			{
				lastAlignedX = val2.x;
				lastAlignedZ = val2.z;
				Productalignment.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__1;

		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__1 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<type>5__1 = typeof(PlayerInventory);
				<>2__current = AccessTools.Method(<type>5__1, "PlaceItemOnContainerServerRpc", (Type[])null, (Type[])null);
				<>1__state = 1;
				return true;
			case 1:
				<>1__state = -1;
				<>2__current = AccessTools.Method(<type>5__1, "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_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		float gridSize = Productalignment.Instance.GetGridSize();
		position = Utils.AlignPositionToGrid(position, gridSize);
		Productalignment.LogInfo($"服务器端物品放置位置修正为:{position}");
	}
}