Decompiled source of StationSlotSwap v1.0.0

plugins/StationSlotSwap.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("StationSlotSwap")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+bbbd4e35c420d108baa1aa9fdfd4a47a77e862b5")]
[assembly: AssemblyProduct("StationSlotSwap")]
[assembly: AssemblyTitle("StationSlotSwap")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 StationSlotSwap
{
	[BepInPlugin("com.github.jiesiluo.stationslotswap", "Station Slot Swap", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "com.github.jiesiluo.stationslotswap";

		public const string Name = "Station Slot Swap";

		public const string Version = "1.0.0";

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("com.github.jiesiluo.stationslotswap").PatchAll(typeof(UiPatches));
		}
	}
	internal static class UiPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIStationStorage), "_OnOpen")]
		private static void StationStorageOpen(UIStationStorage __instance)
		{
			SlotDrag.Attach(__instance);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIControlPanelStationStorage), "_OnOpen")]
		private static void PanelStorageOpen(UIControlPanelStationStorage __instance)
		{
			SlotDrag.Attach(__instance);
		}
	}
	public class SlotDrag : MonoBehaviour, IBeginDragHandler, IEventSystemHandler, IDragHandler, IEndDragHandler
	{
		private UIStationStorage stationUI;

		private UIControlPanelStationStorage panelUI;

		private static SlotDrag dragging;

		private static GameObject ghost;

		public static void Attach(UIStationStorage ui)
		{
			if (!((Object)(object)ui == (Object)null) && !((Object)(object)ui.itemButton == (Object)null))
			{
				SlotDrag slotDrag = ((Component)ui.itemButton).gameObject.GetComponent<SlotDrag>();
				if ((Object)(object)slotDrag == (Object)null)
				{
					slotDrag = ((Component)ui.itemButton).gameObject.AddComponent<SlotDrag>();
				}
				slotDrag.stationUI = ui;
			}
		}

		public static void Attach(UIControlPanelStationStorage ui)
		{
			if (!((Object)(object)ui == (Object)null) && !((Object)(object)ui.itemButton == (Object)null))
			{
				SlotDrag slotDrag = ((Component)ui.itemButton).gameObject.GetComponent<SlotDrag>();
				if ((Object)(object)slotDrag == (Object)null)
				{
					slotDrag = ((Component)ui.itemButton).gameObject.AddComponent<SlotDrag>();
				}
				slotDrag.panelUI = ui;
			}
		}

		public static bool Resolve(GameObject go, out PlanetFactory factory, out StationComponent station, out int index, out MonoBehaviour ui)
		{
			factory = null;
			station = null;
			index = -1;
			ui = null;
			if ((Object)(object)go == (Object)null)
			{
				return false;
			}
			UIStationStorage componentInParent = go.GetComponentInParent<UIStationStorage>();
			if ((Object)(object)componentInParent != (Object)null)
			{
				ui = (MonoBehaviour)(object)componentInParent;
				factory = (((Object)(object)componentInParent.stationWindow != (Object)null) ? componentInParent.stationWindow.factory : null);
				station = componentInParent.station;
				index = componentInParent.index;
			}
			else
			{
				UIControlPanelStationStorage componentInParent2 = go.GetComponentInParent<UIControlPanelStationStorage>();
				if ((Object)(object)componentInParent2 == (Object)null)
				{
					return false;
				}
				ui = (MonoBehaviour)(object)componentInParent2;
				factory = componentInParent2.factory;
				station = componentInParent2.station;
				index = componentInParent2.index;
			}
			if (factory != null && SlotSwap.CanSwap(station) && index >= 0)
			{
				return index < station.storage.Length;
			}
			return false;
		}

		public void OnBeginDrag(PointerEventData e)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Expected O, but got Unknown
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Unknown result type (might be due to invalid IL or missing references)
			//IL_022d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_024b: Unknown result type (might be due to invalid IL or missing references)
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0263: Unknown result type (might be due to invalid IL or missing references)
			EndGhost();
			if ((int)e.button != 0)
			{
				return;
			}
			Player mainPlayer = GameMain.mainPlayer;
			if (mainPlayer == null || mainPlayer.inhandItemId != 0 || !Resolve(((Component)this).gameObject, out var _, out var station, out var index, out var _))
			{
				return;
			}
			int itemId = station.storage[index].itemId;
			if (itemId <= 0)
			{
				return;
			}
			dragging = this;
			Image val = (((Object)(object)stationUI != (Object)null) ? stationUI.itemImage : (((Object)(object)panelUI != (Object)null) ? panelUI.itemImage : null));
			object obj;
			if (!((Object)(object)val != (Object)null) || !((Object)(object)((Graphic)val).canvas != (Object)null))
			{
				Canvas componentInParent = ((Component)this).GetComponentInParent<Canvas>();
				obj = ((componentInParent != null) ? componentInParent.rootCanvas : null);
			}
			else
			{
				obj = ((Graphic)val).canvas.rootCanvas;
			}
			Canvas val2 = (Canvas)obj;
			if (!((Object)(object)val2 == (Object)null))
			{
				ghost = new GameObject("StationSlotSwap-drag", new Type[3]
				{
					typeof(RectTransform),
					typeof(CanvasGroup),
					typeof(Image)
				});
				ghost.layer = (((Object)(object)val != (Object)null) ? ((Component)val).gameObject.layer : ((Component)val2).gameObject.layer);
				ghost.transform.SetParent(((Component)val2).transform, false);
				ghost.transform.SetAsLastSibling();
				ghost.GetComponent<CanvasGroup>().blocksRaycasts = false;
				Canvas val3 = ghost.AddComponent<Canvas>();
				val3.overrideSorting = true;
				if ((Object)(object)val != (Object)null && (Object)(object)((Graphic)val).canvas != (Object)null)
				{
					val3.sortingLayerID = ((Graphic)val).canvas.sortingLayerID;
				}
				val3.sortingOrder = 32000;
				Image component = ghost.GetComponent<Image>();
				ItemProto obj2 = ((ProtoSet<ItemProto>)(object)LDB.items).Select(itemId);
				component.sprite = ((obj2 != null) ? obj2.iconSprite : null);
				((Graphic)component).raycastTarget = false;
				((Graphic)component).color = new Color(1f, 1f, 1f, 0.85f);
				RectTransform val4 = (RectTransform)ghost.transform;
				? val5;
				if (!((Object)(object)val != (Object)null))
				{
					val5 = new Vector2(40f, 40f);
				}
				else
				{
					Rect rect = ((Graphic)val).rectTransform.rect;
					val5 = ((Rect)(ref rect)).size;
				}
				Vector2 val6 = (Vector2)val5;
				val4.sizeDelta = (Vector2)((val6.x > 1f) ? val6 : new Vector2(40f, 40f));
				MoveGhost(e);
			}
		}

		public void OnDrag(PointerEventData e)
		{
			if ((Object)(object)dragging == (Object)(object)this)
			{
				MoveGhost(e);
			}
		}

		public void OnEndDrag(PointerEventData e)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: 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)
			if ((Object)(object)dragging != (Object)(object)this)
			{
				return;
			}
			EndGhost();
			if (Resolve(((Component)this).gameObject, out var factory, out var station, out var index, out var ui))
			{
				RaycastResult pointerCurrentRaycast = e.pointerCurrentRaycast;
				if (Resolve(((RaycastResult)(ref pointerCurrentRaycast)).gameObject, out var _, out var station2, out var index2, out var ui2) && station2 == station && index2 != index && SlotSwap.Swap(factory, station, index, index2))
				{
					Refresh(ui);
					Refresh(ui2);
					VFAudio.Create("ui-click-0", (Transform)null, Vector3.zero, true, 1, -1, -1L);
				}
			}
		}

		private static void Refresh(MonoBehaviour ui)
		{
			UIStationStorage val = (UIStationStorage)(object)((ui is UIStationStorage) ? ui : null);
			if (val != null)
			{
				val.ClosePopMenu();
				return;
			}
			UIControlPanelStationStorage val2 = (UIControlPanelStationStorage)(object)((ui is UIControlPanelStationStorage) ? ui : null);
			if (val2 != null)
			{
				val2.ClosePopMenu();
				val2.OnDataChange();
			}
		}

		private static void MoveGhost(PointerEventData e)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)ghost == (Object)null))
			{
				RectTransform val = (RectTransform)ghost.transform.parent;
				Canvas componentInParent = ((Component)val).GetComponentInParent<Canvas>();
				Canvas val2 = ((componentInParent != null) ? componentInParent.rootCanvas : null);
				Camera val3 = (((Object)(object)val2 != (Object)null && (int)val2.renderMode != 0) ? val2.worldCamera : null);
				Vector2 val4 = default(Vector2);
				if (RectTransformUtility.ScreenPointToLocalPointInRectangle(val, e.position, val3, ref val4))
				{
					ghost.transform.localPosition = Vector2.op_Implicit(val4);
				}
			}
		}

		private static void EndGhost()
		{
			dragging = null;
			if ((Object)(object)ghost != (Object)null)
			{
				Object.Destroy((Object)(object)ghost);
			}
			ghost = null;
		}

		private void OnDisable()
		{
			if ((Object)(object)dragging == (Object)(object)this)
			{
				EndGhost();
			}
		}
	}
	public static class SlotSwap
	{
		public static bool CanSwap(StationComponent st)
		{
			if (st != null && st.id > 0 && !st.isCollector && !st.isVeinCollector)
			{
				return st.storage != null;
			}
			return false;
		}

		public static bool Swap(PlanetFactory factory, StationComponent st, int a, int b)
		{
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: 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_0095: 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_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			if (factory == null || factory.transport == null || !CanSwap(st))
			{
				return false;
			}
			StationStore[] storage = st.storage;
			if (a == b || a < 0 || b < 0 || a >= storage.Length || b >= storage.Length)
			{
				return false;
			}
			if (factory.transport.stationPool == null || st.id >= factory.transport.stationPool.Length || factory.transport.stationPool[st.id] != st)
			{
				return false;
			}
			lock (storage)
			{
				StationStore val = storage[a];
				storage[a] = storage[b];
				storage[b] = val;
				StationPriorityLock[] priorityLocks = st.priorityLocks;
				if (priorityLocks != null && a < priorityLocks.Length && b < priorityLocks.Length)
				{
					StationPriorityLock val2 = priorityLocks[a];
					priorityLocks[a] = priorityLocks[b];
					priorityLocks[b] = val2;
				}
				if (st.slots != null)
				{
					for (int i = 0; i < st.slots.Length; i++)
					{
						int num = st.slots[i].storageIdx - 1;
						if (num == a || num == b)
						{
							st.slots[i].storageIdx = Map(num, a, b) + 1;
						}
					}
				}
				if (st.workDroneOrders != null)
				{
					for (int j = 0; j < st.workDroneOrders.Length; j++)
					{
						if (st.workDroneOrders[j].itemId > 0)
						{
							st.workDroneOrders[j].thisIndex = Map(st.workDroneOrders[j].thisIndex, a, b);
						}
					}
				}
				if (st.workShipOrders != null)
				{
					for (int k = 0; k < st.workShipOrders.Length; k++)
					{
						if (st.workShipOrders[k].itemId > 0)
						{
							st.workShipOrders[k].thisIndex = Map(st.workShipOrders[k].thisIndex, a, b);
						}
					}
				}
			}
			StationComponent[] stationPool = factory.transport.stationPool;
			for (int l = 1; l < factory.transport.stationCursor && l < stationPool.Length; l++)
			{
				StationComponent val3 = stationPool[l];
				if (val3 == null || val3.id != l || val3 == st || val3.workDroneOrders == null)
				{
					continue;
				}
				for (int m = 0; m < val3.workDroneOrders.Length; m++)
				{
					if (val3.workDroneOrders[m].otherStationId == st.id)
					{
						val3.workDroneOrders[m].otherIndex = Map(val3.workDroneOrders[m].otherIndex, a, b);
					}
				}
			}
			GalacticTransport val4 = GameMain.data?.galacticTransport;
			if (st.gid > 0 && val4?.stationPool != null)
			{
				StationComponent[] stationPool2 = val4.stationPool;
				for (int n = 1; n < val4.stationCursor && n < stationPool2.Length; n++)
				{
					StationComponent val5 = stationPool2[n];
					if (val5 == null || val5.gid != n || val5 == st || val5.workShipOrders == null)
					{
						continue;
					}
					for (int num2 = 0; num2 < val5.workShipOrders.Length; num2++)
					{
						if (val5.workShipOrders[num2].otherStationGId == st.gid)
						{
							val5.workShipOrders[num2].otherIndex = Map(val5.workShipOrders[num2].otherIndex, a, b);
						}
					}
				}
			}
			st.UpdateNeeds();
			factory.transport.RefreshStationTraffic(st.id);
			if (st.gid > 0 && val4 != null)
			{
				val4.RefreshTraffic(st.gid);
			}
			return true;
		}

		private static int Map(int i, int a, int b)
		{
			if (i != a)
			{
				if (i != b)
				{
					return i;
				}
				return a;
			}
			return b;
		}
	}
}