Decompiled source of Storage Sorting v1.0.0

WWAG_StorageSort.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Inventory;
using TMPro;
using UI;
using UnityEngine;
using UnityEngine.Events;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("Sort")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Sort")]
[assembly: AssemblyTitle("Sort")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Sort;

public class ConstInfo
{
	public const string PLUGIN_GUID = "com.xatini.plugins.storagesort";

	public const string PLUGIN_NAME = "Sort";

	public const string PLUGIN_VERSION = "0.0.1";
}
[BepInPlugin("com.xatini.plugins.storagesort", "Sort", "0.0.1")]
public class Plugin : BaseUnityPlugin
{
	private SortHandler _sortHandler;

	private void Awake()
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin com.xatini.plugins.storagesort is loaded!");
		UIDialogueStack.OnFocusChanged += new FocusChangedHandler(OnFocusChanged);
	}

	private void OnFocusChanged(IUIDialogue from, IUIDialogue to)
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Expected O, but got Unknown
		UIWindow val = (UIWindow)(object)((to is UIWindow) ? to : null);
		if (val != null)
		{
			StorageWindowUI component = ((Component)val).GetComponent<StorageWindowUI>();
			if (!((Object)(object)component == (Object)null))
			{
				CreateButton(component);
				UIDialogueStack.OnFocusChanged -= new FocusChangedHandler(OnFocusChanged);
			}
		}
	}

	private void CreateButton(StorageWindowUI window)
	{
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: 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_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Expected O, but got Unknown
		_sortHandler = new SortHandler();
		GameObject gameObject = ((Component)window.takeAllButton).gameObject;
		RectTransform component = gameObject.GetComponent<RectTransform>();
		GameObject val = Object.Instantiate<GameObject>(gameObject);
		((Object)val).name = "sort-button";
		val.transform.SetParent(gameObject.transform.parent, false);
		RectTransform component2 = val.GetComponent<RectTransform>();
		component2.sizeDelta = new Vector2(220f, component.sizeDelta.y);
		((Transform)component2).position = ((Transform)component).position - new Vector3(-350f, -650f);
		TextMeshProUGUI componentInChildren = val.GetComponentInChildren<TextMeshProUGUI>();
		((TMP_Text)componentInChildren).text = "Sort";
		TextMeshLocalizer component3 = ((Component)componentInChildren).gameObject.GetComponent<TextMeshLocalizer>();
		if ((Object)(object)component3 != (Object)null)
		{
			Object.Destroy((Object)(object)component3);
		}
		SelectableButton component4 = val.GetComponent<SelectableButton>();
		component4.SetClickAction((UnityAction)delegate
		{
			SortStorage(window);
		});
	}

	private void SortStorage(StorageWindowUI window)
	{
		if (_sortHandler == null)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Sort Handler is Null");
		}
		else
		{
			_sortHandler.SortItems(((BaseUnityPlugin)this).Logger, window);
		}
	}
}
public class SortHandler
{
	public void SortItems(ManualLogSource logger, StorageWindowUI window)
	{
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		//IL_013d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0174: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)window == (Object)null)
		{
			logger.LogInfo((object)"Cant find window");
			return;
		}
		logger.LogInfo((object)"Sort Started");
		List<InventoryCellUI> cells = ((FlexibleCellContainer<InventoryCellUI>)(object)window.cells).GetCells();
		InventoryData inventoryData = GameUniverse.persistentWorld.world.GetMainThreadData<InventoryData>();
		List<InventoryCellUI> source = (from cell in cells
			orderby string.IsNullOrEmpty(GetItemName(inventoryData, window.inventorySystem, cell.currentItem)), GetItemName(inventoryData, window.inventorySystem, cell.currentItem)
			select cell).ToList();
		List<ItemEntity> items = source.Select((InventoryCellUI cell) => cell.currentItem).ToList();
		int i;
		for (i = 0; i < cells.Count; i++)
		{
			try
			{
				InventoryCellUI val = cells[i];
				InventoryCellUI val2 = ((IEnumerable<InventoryCellUI>)cells).FirstOrDefault((Func<InventoryCellUI, bool>)((InventoryCellUI data) => data.currentItem == items[i]));
				if (!((Object)(object)val2 == (Object)null))
				{
					if (((InventoryData)(ref inventoryData)).IsItemValid(val.currentItem))
					{
						SwapCells(inventoryData, val, val2);
					}
					else if (((InventoryData)(ref inventoryData)).IsItemValid(val2.currentItem))
					{
						SwapCells(inventoryData, val2, val);
					}
					val.OnUpdate();
					val2.OnUpdate();
				}
			}
			catch (Exception ex)
			{
				logger.LogInfo((object)(ex.Message + "..... " + ex.StackTrace));
			}
		}
		foreach (InventoryCellUI item in cells)
		{
			item.OnUpdate();
		}
		logger.LogInfo((object)"Sort Completed");
	}

	private void SwapCells(InventoryData inventoryData, InventoryCellUI currentCell, InventoryCellUI nextCell)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		int num = default(int);
		int num2 = default(int);
		((InventoryData)(ref inventoryData)).AddOrSwapCells(currentCell.GetCellId(), nextCell.GetCellId(), true, ref num, ref num2);
	}

	private string GetItemName(InventoryData inventoryData, InventorySystem inventorySystem, ItemEntity item)
	{
		//IL_0003: 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)
		if (!((InventoryData)(ref inventoryData)).IsItemValid(item))
		{
			return "";
		}
		return inventorySystem.GetItemName(item);
	}
}
public static class MyPluginInfo
{
	public const string PLUGIN_GUID = "Sort";

	public const string PLUGIN_NAME = "Sort";

	public const string PLUGIN_VERSION = "1.0.0";
}