Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions WTT-ClientCommonLib/Helpers/SortHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using EFT.InventoryLogic;
using System;
using System.Collections.Generic;
using ItemSortOrderManager = GClass3381;

namespace WTTClientCommonLib.Helpers
{
public static class SortHelper
{
public static void AddToSortOrder(this Type itemType, Type itemTypeToInsertAfter, int offset = 1)
{
InsertAfter(itemType, itemTypeToInsertAfter, offset);
}

public static int GetSortOrder(this Type itemType)
{
return ItemSortOrderManager.List_0.IndexOf(itemType);
}

public static void Insert(int sortIndex, Type itemType)
{
ItemSortOrderManager.List_0.Insert(sortIndex, itemType);
}

public static void SetIndex(int newIndex, Type itemType)
{
List<Type> list = ItemSortOrderManager.List_0;
int oldIndex = list.IndexOf(itemType);

if (oldIndex < 0)
{
LogHelper.LogWarn($"failed to set sort index for item {itemType} because it does not exist in sort order list");
return;
}

newIndex = Math.Clamp(newIndex, 0, list.Count - 1);

list.RemoveAt(oldIndex);

if (newIndex > oldIndex)
{
newIndex--;
}

list.Insert(newIndex, itemType);
}

public static void InsertAfter(Type itemType, Type itemToInsertAfter, int offset = 1)
{
int itemIndex = ItemSortOrderManager.IndexOf(itemToInsertAfter);

if (itemIndex < 0)
{
LogHelper.LogWarn($"failed to add item type {itemType} to sort order because item type {itemToInsertAfter} doesn't exist in sort order list");
return;
}

ItemSortOrderManager.List_0.Insert(itemIndex + offset, itemType);
}
}
}
42 changes: 42 additions & 0 deletions WTT-ClientCommonLib/Patches/FixCustomItemSortingOrderPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using EFT.InventoryLogic;
using HarmonyLib;
using SPT.Reflection.Patching;
using System;
using System.Reflection;

namespace WTTClientCommonLib.Patches
{
public class FixCustomItemSortingOrderPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.Method(typeof(GClass3380), nameof(GClass3380.GetIndexOfItemType));
}

[PatchPrefix]
public static bool PatchPrefix(GClass3380 __instance, ref int __result, Item i)
{
Type type = i.GetType();

int index = GClass3381.IndexOf(type);
if (index >= 0)
{
__result = index;
return false;
}

for (Type type2 = type; type2 != null; type2 = type2.BaseType)
{
index = GClass3381.IndexOf(type2);
if (index >= 0)
{
__result = index;
return false;
}
}

__result = int.MaxValue;
return false;
}
}
}
3 changes: 2 additions & 1 deletion WTT-ClientCommonLib/WTTClientCommonLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ private void Awake()
new ClothingBundleRendererPatch().Enable();
new HideoutCustomizationIconPatch().Enable();
new HideoutCustomizationTexturesPatch().Enable();

new FixCustomItemSortingOrderPatch().Enable();

var resourceLoader = new ResourceLoader(Logger, AssetLoader);
resourceLoader.LoadAllResourcesFromServer();
}
Expand Down