Decompiled source of SortDealerCustomers Il2cpp v0.0.1

SortDealerCustomers.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using HarmonyLib;
using Il2CppScheduleOne.Economy;
using Il2CppScheduleOne.UI.Phone;
using Il2CppSystem.Collections.Generic;
using MelonLoader;
using SortDealerCustomersNs;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(SortDealerCustomersClass), "SortDealerCustomers", "0.0.1", "xVilho", null)]
[assembly: MelonColor(255, 200, 150, 255)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
namespace SortDealerCustomersNs;

public class SortDealerCustomersClass : MelonMod
{
	[HarmonyPatch(typeof(CustomerSelector))]
	internal static class DealerScreenSort
	{
		[HarmonyPatch("Open")]
		[HarmonyPostfix]
		private static void SortCustomerEntriesAfterOpen(CustomerSelector __instance)
		{
			SortEntries(__instance);
			MelonCoroutines.Start(DeferredSort(__instance));
		}

		[HarmonyPatch("CreateEntry")]
		[HarmonyPostfix]
		private static void SortCustomerEntriesAfterCreate(CustomerSelector __instance)
		{
			TryInsertNewestEntryInOrder(__instance);
		}

		[HarmonyPatch("CustomerSelected")]
		[HarmonyPostfix]
		private static void SortCustomerEntriesAfterSelect(CustomerSelector __instance, Customer customer)
		{
			SortEntries(__instance);
			MelonCoroutines.Start(DeferredSort(__instance));
		}

		[HarmonyPatch("Close")]
		[HarmonyPostfix]
		private static void SortCustomerEntriesAfterClose(CustomerSelector __instance)
		{
		}

		private static IEnumerator DeferredSort(CustomerSelector inst)
		{
			yield return null;
			if ((Object)(object)inst != (Object)null)
			{
				SortEntries(inst);
			}
		}

		private static void CleanupInactiveEntries(CustomerSelector __instance)
		{
		}

		private static void TryInsertNewestEntryInOrder(CustomerSelector __instance)
		{
			try
			{
				Transform entriesContainer = (Transform)(object)__instance.EntriesContainer;
				if ((Object)(object)entriesContainer == (Object)null)
				{
					return;
				}
				int childCount = entriesContainer.childCount;
				if (childCount <= 1)
				{
					return;
				}
				Transform child = entriesContainer.GetChild(childCount - 1);
				if ((Object)(object)child == (Object)null)
				{
					return;
				}
				GameObject gameObject = ((Component)child).gameObject;
				if (gameObject == null || !gameObject.activeInHierarchy)
				{
					return;
				}
				string customerKey = GetCustomerKey(__instance, (RectTransform)(object)((child is RectTransform) ? child : null));
				string labelText = GetLabelText(child);
				int siblingIndex = childCount - 1;
				for (int i = 0; i < childCount - 1; i++)
				{
					Transform child2 = entriesContainer.GetChild(i);
					if ((Object)(object)child2 == (Object)null)
					{
						continue;
					}
					GameObject gameObject2 = ((Component)child2).gameObject;
					if (gameObject2 != null && gameObject2.activeInHierarchy)
					{
						string customerKey2 = GetCustomerKey(__instance, (RectTransform)(object)((child2 is RectTransform) ? child2 : null));
						string labelText2 = GetLabelText(child2);
						int num = 0;
						if (!string.IsNullOrEmpty(customerKey) || !string.IsNullOrEmpty(customerKey2))
						{
							num = string.Compare(customerKey, customerKey2, StringComparison.OrdinalIgnoreCase);
						}
						if (num == 0)
						{
							num = string.Compare(labelText, labelText2, StringComparison.OrdinalIgnoreCase);
						}
						if (num == 0)
						{
							num = string.Compare(((Object)child).name, ((Object)child2).name, StringComparison.OrdinalIgnoreCase);
						}
						if (num < 0)
						{
							siblingIndex = i;
							break;
						}
					}
				}
				child.SetSiblingIndex(siblingIndex);
				try
				{
					RectTransform val = (RectTransform)(object)((entriesContainer is RectTransform) ? entriesContainer : null);
					if (val != null)
					{
						LayoutRebuilder.ForceRebuildLayoutImmediate(val);
					}
				}
				catch
				{
				}
			}
			catch
			{
			}
		}

		private static void SortEntries(CustomerSelector __instance)
		{
			try
			{
				Transform entriesContainer = (Transform)(object)__instance.EntriesContainer;
				if ((Object)(object)entriesContainer == (Object)null)
				{
					return;
				}
				int childCount = entriesContainer.childCount;
				if (childCount <= 1)
				{
					return;
				}
				List<Transform> list = new List<Transform>(childCount);
				for (int i = 0; i < childCount; i++)
				{
					Transform child = entriesContainer.GetChild(i);
					if (!((Object)(object)child == (Object)null))
					{
						GameObject gameObject = ((Component)child).gameObject;
						if (!((Object)(object)gameObject == (Object)null) && gameObject.activeInHierarchy)
						{
							list.Add(child);
						}
					}
				}
				list.Sort(delegate(Transform a, Transform b)
				{
					RectTransform entry = (RectTransform)(object)((a is RectTransform) ? a : null);
					RectTransform entry2 = (RectTransform)(object)((b is RectTransform) ? b : null);
					string customerKey = GetCustomerKey(__instance, entry);
					string customerKey2 = GetCustomerKey(__instance, entry2);
					if (!string.IsNullOrEmpty(customerKey) || !string.IsNullOrEmpty(customerKey2))
					{
						int num = string.Compare(customerKey, customerKey2, StringComparison.OrdinalIgnoreCase);
						if (num != 0)
						{
							return num;
						}
					}
					string labelText = GetLabelText(a);
					string labelText2 = GetLabelText(b);
					int num2 = string.Compare(labelText, labelText2, StringComparison.OrdinalIgnoreCase);
					return (num2 != 0) ? num2 : string.Compare(((Object)a).name, ((Object)b).name, StringComparison.OrdinalIgnoreCase);
				});
				for (int j = 0; j < list.Count; j++)
				{
					list[j].SetSiblingIndex(j);
				}
				try
				{
					Canvas.ForceUpdateCanvases();
					RectTransform val = (RectTransform)(object)((entriesContainer is RectTransform) ? entriesContainer : null);
					if (val != null)
					{
						LayoutRebuilder.ForceRebuildLayoutImmediate(val);
						Transform parent = ((Transform)val).parent;
						RectTransform val2 = (RectTransform)(object)((parent is RectTransform) ? parent : null);
						if ((Object)(object)val2 != (Object)null)
						{
							LayoutRebuilder.ForceRebuildLayoutImmediate(val2);
						}
					}
					Canvas.ForceUpdateCanvases();
				}
				catch
				{
				}
			}
			catch (Exception ex)
			{
				LogError("Sorting failed: " + ex);
			}
		}

		private static string GetLabelText(Transform t)
		{
			try
			{
				Transform val = null;
				foreach (Transform componentsInChild in ((Component)t).GetComponentsInChildren<Transform>(true))
				{
					if ((Object)(object)componentsInChild != (Object)null && string.Equals(((Object)componentsInChild).name, "Name", StringComparison.OrdinalIgnoreCase))
					{
						val = componentsInChild;
						break;
					}
				}
				if ((Object)(object)val != (Object)null)
				{
					string anyTextProperty = GetAnyTextProperty(val);
					if (!string.IsNullOrWhiteSpace(anyTextProperty))
					{
						return anyTextProperty;
					}
				}
				string anyTextProperty2 = GetAnyTextProperty(t);
				if (!string.IsNullOrWhiteSpace(anyTextProperty2))
				{
					return anyTextProperty2;
				}
			}
			catch
			{
			}
			return ((t != null) ? ((Object)t).name : null) ?? string.Empty;
		}

		private static string GetAnyTextProperty(Transform root)
		{
			try
			{
				Text componentInChildren = ((Component)root).GetComponentInChildren<Text>(true);
				if ((Object)(object)componentInChildren != (Object)null && !string.IsNullOrWhiteSpace(componentInChildren.text))
				{
					return componentInChildren.text;
				}
			}
			catch
			{
			}
			foreach (Component componentsInChild in ((Component)root).GetComponentsInChildren<Component>(true))
			{
				if ((Object)(object)componentsInChild == (Object)null)
				{
					continue;
				}
				Type type = ((object)componentsInChild).GetType();
				if (type == null)
				{
					continue;
				}
				PropertyInfo property = type.GetProperty("text", BindingFlags.Instance | BindingFlags.Public);
				if (!(property == null) && property.CanRead)
				{
					object value = property.GetValue(componentsInChild);
					string text = (value as string) ?? value?.ToString();
					if (!string.IsNullOrWhiteSpace(text))
					{
						return text;
					}
				}
			}
			return string.Empty;
		}

		private static string GetCustomerKey(CustomerSelector selector, RectTransform entry)
		{
			if ((Object)(object)selector == (Object)null || (Object)(object)entry == (Object)null)
			{
				return string.Empty;
			}
			try
			{
				Dictionary<RectTransform, Customer> entryToCustomer = selector.entryToCustomer;
				if (entryToCustomer == null)
				{
					return string.Empty;
				}
				Type type = ((object)entryToCustomer).GetType();
				MethodInfo method = type.GetMethod("ContainsKey", BindingFlags.Instance | BindingFlags.Public);
				MethodInfo method2 = type.GetMethod("get_Item", BindingFlags.Instance | BindingFlags.Public);
				object obj = null;
				bool flag = false;
				if (method != null && method2 != null)
				{
					flag = (bool)(method.Invoke(entryToCustomer, new object[1] { entry }) ?? ((object)false));
					if (flag)
					{
						obj = method2.Invoke(entryToCustomer, new object[1] { entry });
					}
				}
				else
				{
					MethodInfo method3 = type.GetMethod("TryGetValue", BindingFlags.Instance | BindingFlags.Public);
					if (method3 != null)
					{
						object[] array = new object[2] { entry, null };
						flag = (bool)(method3.Invoke(entryToCustomer, array) ?? ((object)false));
						if (flag)
						{
							obj = array[1];
						}
					}
				}
				if (!flag)
				{
					return string.Empty;
				}
				if (obj == null)
				{
					return string.Empty;
				}
				string[] array2 = new string[5] { "DisplayName", "FullName", "Name", "CustomerName", "FirstName" };
				foreach (string name in array2)
				{
					PropertyInfo property = obj.GetType().GetProperty(name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
					if (property != null && property.CanRead)
					{
						object value = property.GetValue(obj);
						string text = (value as string) ?? value?.ToString();
						if (!string.IsNullOrWhiteSpace(text))
						{
							return text;
						}
					}
				}
				object obj2 = obj.GetType().GetProperty("FirstName", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public)?.GetValue(obj);
				object obj3 = obj.GetType().GetProperty("LastName", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public)?.GetValue(obj);
				string text2 = (obj2 as string) ?? obj2?.ToString();
				string text3 = (obj3 as string) ?? obj3?.ToString();
				if (!string.IsNullOrWhiteSpace(text2) || !string.IsNullOrWhiteSpace(text3))
				{
					return (text2 + " " + text3).Trim();
				}
				return obj.ToString() ?? string.Empty;
			}
			catch
			{
				return string.Empty;
			}
		}
	}

	internal static Instance Logger;

	public static void Log(string msg)
	{
		Logger.Msg(msg ?? "");
	}

	public static void LogWarning(string msg)
	{
		Logger.Warning(msg ?? "");
	}

	public static void LogError(string msg)
	{
		Logger.Error(msg ?? "");
	}

	public override void OnInitializeMelon()
	{
		Logger = ((MelonBase)this).LoggerInstance;
		Log("initialized");
	}
}