using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace AutoSplitStack;
[BepInPlugin("aedenthorn.AutoSplitStack", "AutoSplitStack", "0.3.0")]
public class BepInExPlugin : BaseUnityPlugin
{
[HarmonyPatch(typeof(InventoryGui), "OnRightClickItem")]
private static class OnRightClickItem_Patch
{
private static bool Prefix(InventoryGui __instance, InventoryGrid grid, ItemData item, int ___m_dragAmount)
{
if (ZInput.GetButton("JoyLTrigger") || CheckModKey(modKey.Value))
{
if (item != null && item.m_stack > 1 && Object.op_Implicit((Object)(object)Player.m_localPlayer))
{
int num = ((___m_dragAmount > 0) ? ((item.m_stack - ___m_dragAmount) / 2 + ___m_dragAmount) : (item.m_stack / 2));
((object)__instance).GetType().GetMethod("SetupDragItem", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(__instance, new object[3]
{
item,
grid.GetInventory(),
num
});
autoSplitting = true;
}
return false;
}
return true;
}
}
[HarmonyPatch(typeof(InventoryGui), "SetupDragItem")]
private static class InventoryGui_SetupDragItem_Patch
{
private static bool Prefix(ItemData item, Inventory inventory, int amount)
{
if (autoSplitting)
{
return false;
}
return true;
}
}
[HarmonyPatch(typeof(InventoryGui), "Update")]
private static class InventoryGui_Update_Patch
{
private static void Postfix()
{
if (!Input.GetMouseButton(1))
{
autoSplitting = false;
}
}
}
[HarmonyPatch(typeof(Terminal), "InputText")]
private static class InputText_Patch
{
private static bool Prefix(Terminal __instance)
{
if (!modEnabled.Value)
{
return true;
}
string text = ((TMP_InputField)__instance.m_input).text;
if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
{
((BaseUnityPlugin)context).Config.Reload();
((BaseUnityPlugin)context).Config.Save();
__instance.AddString(text);
__instance.AddString(((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded");
return false;
}
return true;
}
}
private static readonly bool isDebug = true;
private static BepInExPlugin context;
public static ConfigEntry<bool> modEnabled;
public static ConfigEntry<int> nexusID;
public static ConfigEntry<string> modKey;
public static bool autoSplitting = false;
public static void Dbgl(string str = "", bool pref = true)
{
if (isDebug)
{
Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
}
}
private void Awake()
{
context = this;
modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 76, "Nexus mod ID for updates");
modKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ModKey", "left shift", "Modifier key to split stack");
if (modEnabled.Value)
{
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
}
}
private static bool CheckModKey(string value)
{
try
{
return Input.GetKey(value.ToLower());
}
catch
{
return false;
}
}
}