private static bool Prefix(MechLabInventoryWidget __instance) { Comparison <InventoryItemElement_NotListView> currComp = Traverse.Create(__instance).Field("currentSort").GetValue <Comparison <InventoryItemElement_NotListView> >(); bool currDir = Traverse.Create(__instance).Field("invertSort").GetValue <bool>(); try { Traverse.Create(__instance).Field("currentSort").SetValue(new Comparison <InventoryItemElement_NotListView>(Compare_NotListView)); Traverse.Create(__instance).Field("invertSort").SetValue(true); return(true); } catch (Exception e) { BetterSorting.Log($"Failed to sort MechLabInventoryWidget, resetting to default.{Environment.NewLine}Details:{e.ToString()}"); Traverse.Create(__instance).Field("currentSort").SetValue(currComp); Traverse.Create(__instance).Field("invertSort").SetValue(currDir); return(true); } }
private static bool Prefix(MechLabInventoryWidget_ListView __instance) { IComparer <ListElementController_BASE> currComp = Traverse.Create(__instance).Field("currentListItemSorter").GetValue <IComparer <ListElementController_BASE> >(); bool currDir = Traverse.Create(__instance).Field("invertSort").GetValue <bool>(); try { Traverse.Create(__instance).Field("currentListItemSorter").SetValue(new Comparer_ListView()); Traverse.Create(__instance).Field("invertSort").SetValue(true); return(true); } catch (Exception e) { BetterSorting.Log($"Failed to sort MechLabInventoryWidget, resetting to default.{Environment.NewLine}Details:{e.ToString()}"); Traverse.Create(__instance).Field("currentSort").SetValue(currComp); Traverse.Create(__instance).Field("invertSort").SetValue(currDir); return(true); } }
public static int Compare(ListElementControllerCompare a, ListElementControllerCompare b) { // in general we can rely on the custom comparer for sorting, with a few exceptions handled below int num = BetterSorting.GetSortVal(a).CompareTo(BetterSorting.GetSortVal(b)); // upgrades don't have a sub-type to identify them, so a name comparison is necessary to sort them together if (num == 0 && a?.componentDef != null && b?.componentDef != null && a.componentDef.ComponentType == ComponentType.Upgrade) { Log($"comparing equipment by name..."); // normalize name and compare (ASC) num = b.componentDef.Description.UIName.Trim(trimChars).CompareTo(a.componentDef.Description.UIName.Trim(trimChars)); if (num == 0) { // rarity num = a.componentDef.Description.Rarity.CompareTo(b.componentDef.Description.Rarity); } } return(num); }
public static int Compare_NotListView(InventoryItemElement_NotListView a, InventoryItemElement_NotListView b) { return(BetterSorting.Compare(ListElementControllerCompare.Wrap(b.controller), ListElementControllerCompare.Wrap(a.controller))); }
public int Compare(ListElementController_BASE a, ListElementController_BASE b) { return(BetterSorting.Compare(ListElementControllerCompare.Wrap(b), ListElementControllerCompare.Wrap(a))); }
// replace the unused and broken sortBy_Type with a more comprehensive comparison public static bool Prefix(MechLabInventoryWidget __instance, InventoryItemElement a, InventoryItemElement b, ref int __result) { try { int num = 0; char[] trimChars = new char[] { '+', ' ' }; // check for shop first ShopDefItem s1 = a?.controller?.shopDefItem; ShopDefItem s2 = b?.controller?.shopDefItem; if (s1 != null || s2 != null) { //Log("comparing shop items..."); if (s1 != null && s2 != null) { __result = BetterSorting.GetShopDefSortVal(s2).CompareTo(BetterSorting.GetShopDefSortVal(s1)); } else if (s1 != null) { __result = -1; } else if (s2 != null) { __result = 1; } return(false); } WeaponDef weaponDef = a?.controller?.weaponDef; WeaponDef weaponDef2 = b?.controller?.weaponDef; if (weaponDef != null && weaponDef2 != null) { //Log("comparing two weapons"); // category (missile, ballistic...) num = weaponDef.Category.CompareTo(weaponDef2.Category); // sub-type (SRM,LRM...) if (num == 0) { num = weaponDef.WeaponSubType.CompareTo(weaponDef2.WeaponSubType); } // tonnage if (num == 0) { num = weaponDef.Tonnage.CompareTo(weaponDef2.Tonnage); } // rarity (descending) if (num == 0) { num = weaponDef2.Description.Rarity.CompareTo(weaponDef.Description.Rarity); } // manufacturer if (num == 0) { num = weaponDef.Description.Manufacturer.CompareTo(weaponDef2.Description.Manufacturer); } } else if (weaponDef != null) { num = -1; } else if (weaponDef2 != null) { num = 1; } else if (a?.controller?.ammoBoxDef != null && b?.controller?.ammoBoxDef != null) // ammo { //Log("comparing ammo"); // ammo type num = a.controller.ammoBoxDef.Ammo.Category.CompareTo(b.controller.ammoBoxDef.Ammo.Category); } else if (a?.controller?.componentDef != null && b?.controller?.componentDef != null) // equipment { //Log("comparing equipment"); // remove + from ui name and compare num = a.controller.componentDef.Description.UIName.Trim(trimChars).CompareTo(b.controller.componentDef.Description.UIName.Trim(trimChars)); // rarity (descending) if (num == 0) { num = b.controller.componentDef.Description.Rarity.CompareTo(a.controller.componentDef.Description.Rarity); } } else { //Log("comparing componentDef"); MechComponentRef m1 = a?.ComponentRef; MechComponentRef m2 = b?.ComponentRef; if (m1 != null && m2 != null) { // general type num = a.ComponentRef.ComponentDefType.CompareTo(b.ComponentRef.ComponentDefType); } else if (m1 != null) { num = -1; } else if (m2 != null) { num = 1; } } __result = num; return(false); } catch (Exception e) { BetterSorting.Log(e.ToString()); // fall back to original comparison (by name) __result = Traverse.Create(__instance).Method("SortBy_Name", new Type[] { typeof(InventoryItemElement), typeof(InventoryItemElement) }, new InventoryItemElement[] { b, a }).GetValue <int>(); return(false); } }
private static void Postfix(ShopDefItem __instance, ref int __result) { __result = BetterSorting.GetShopDefSortVal(__instance); }