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.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SortEverythingIntoChests")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SortEverythingIntoChests")]
[assembly: AssemblyTitle("SortEverythingIntoChests")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SortEverythingIntoChests;
[HarmonyPatch(typeof(Container))]
internal class ContainerLifecycle
{
public static readonly List<Container> _containers = new List<Container>();
[HarmonyPatch(typeof(Container), "Awake")]
[HarmonyPostfix]
private static void Awake(Container __instance, ZNetView ___m_nview)
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)___m_nview == (Object)null)
{
return;
}
ZDO zDO = ___m_nview.GetZDO();
if (zDO != null)
{
_ = zDO.m_uid;
if (0 == 0 && TryGetValidOwningPiece(___m_nview, out var _) && ((object)__instance).ToString().StartsWith("piece_chest") && !_containers.Contains(__instance))
{
_containers.Add(__instance);
}
}
}
private static bool TryGetValidOwningPiece(ZNetView zNetView, out Piece piece)
{
piece = ((Component)zNetView).GetComponent<Piece>();
return (Object)(object)piece != (Object)null;
}
[HarmonyPatch(typeof(Container), "OnDestroyed")]
[HarmonyPostfix]
private static void OnDestroyed(Container __instance)
{
_containers.Remove(__instance);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Container), "GetHoverText")]
public static string ContainerGetHoverText_Patch(string __result)
{
return __result + "\n[<color=yellow><b>" + Mod.keyDepositString.Value + "</b></color>] Sort All To Chests";
}
}
[BepInPlugin("com.dryzler.valheim.sorteverythingintochests", "Dryzler's Sort Everything Into Chests", "1.0.7")]
[BepInProcess("valheim.exe")]
public class Mod : BaseUnityPlugin
{
private class Lib
{
public static void initMod(Mod mod)
{
instance = mod;
mod.dateTimeAwaked = DateTime.UtcNow;
mod.harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), mod.PLUGIN_ID);
try
{
X509Certificate2 serverCert = new X509Certificate2(Encoding.ASCII.GetBytes(mod.CERT));
byte[] bytes = Encoding.ASCII.GetBytes(apiKey.Value.Substring(0, 6));
string signature = apiKey.Value.Substring(6);
byte[] signature2 = (from i in Enumerable.Range(0, signature.Length / 2)
select Convert.ToByte(signature.Substring(i * 2, 2), 16)).ToArray();
mod.active = VerifySignature(bytes, signature2, serverCert);
}
catch (Exception ex)
{
ex.ToString();
}
}
public static bool VerifySignature(byte[] data, byte[] signature, X509Certificate2 serverCert)
{
using (SHA256.Create())
{
using RSA rSA = serverCert.GetRSAPublicKey();
return rSA.VerifyData(data, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
public static bool isDemoFinished()
{
if ((DateTime.UtcNow - instance.dateTimeAwaked).TotalMinutes >= (double)instance.MINUTES)
{
return true;
}
return false;
}
public static bool modIsActivated()
{
return instance.active;
}
}
[HarmonyPatch(typeof(Player), "Update")]
private static class Player_Update_Patch
{
private static bool Prefix(Player __instance)
{
try
{
if (Input.GetKeyDown(keyDepositString.Value.ToLower()))
{
doIt();
return false;
}
}
catch
{
}
return true;
}
}
private Harmony harmony;
private bool active;
private DateTime dateTimeAwaked;
private static Mod instance;
public static KeyCode configDepositKey;
public static ConfigEntry<string> apiKey { get; private set; }
private string PLUGIN_ID => "com.dryzler.valheim.sorteverythingintochests";
public string CERT => "-----BEGIN CERTIFICATE-----\r\nMIIB4TCCAYugAwIBAgIUXQJpDzY3TvKHl/GNfcpykCAq5SYwDQYJKoZIhvcNAQEL\r\nBQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM\r\nGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNDA2MjAwNjIyNTJaFw00NDA2\r\nMTUwNjIyNTJaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw\r\nHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwXDANBgkqhkiG9w0BAQEF\r\nAANLADBIAkEA1uX7eMY71hwX3AWAcSi8SGiIRnlKEjzqaNEXCo4H5dLPWttHvmP2\r\nXdzFQf/UX78njP+jejL5dPbEqQ0HfsdPAQIDAQABo1MwUTAdBgNVHQ4EFgQUThhx\r\nogoLkFxvkSlne7ePVD/EqY8wHwYDVR0jBBgwFoAUThhxogoLkFxvkSlne7ePVD/E\r\nqY8wDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAANBAEXK6FO/kCdaBsfy\r\nR91zWAtQLtT1dLt+CW1UfWBgEzWMS1DJME7lMc+XoUr3J61ThUKFbw7LhNXtKx1E\r\nxMZgMRM=\r\n-----END CERTIFICATE-----\r\n";
private int MINUTES => 10;
public static ConfigEntry<int> containerLookupRange { get; private set; }
public static ConfigEntry<int> excludedSlots { get; private set; }
public static ConfigEntry<string> keyDepositString { get; private set; }
private void Awake()
{
//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
apiKey = ((BaseUnityPlugin)this).Config.Bind<string>("Dryzlers Sort Everything Into Chests", "Dryzlers-Mods-Key", "Get your mod key at https://dryzlers-mods.com", "The mod works for " + MINUTES + " minutes. After that you need a key which you enter here.");
containerLookupRange = ((BaseUnityPlugin)this).Config.Bind<int>("Dryzlers Sort Everything Into Chests", "Container Range", 100, "The maximum range to send items");
excludedSlots = ((BaseUnityPlugin)this).Config.Bind<int>("Dryzlers Sort Everything Into Chests", "Excluded Slots", 8, "The number of slots in the player's inventory that shall not be touched (from the top left corner)");
keyDepositString = ((BaseUnityPlugin)this).Config.Bind<string>("Dryzlers Sort Everything Into Chests", "Keybinding", "G", "The key to press to sort everything into chests");
configDepositKey = (KeyCode)Enum.Parse(typeof(KeyCode), keyDepositString.Value);
Lib.initMod(this);
}
public static void doIt()
{
//IL_0055: Unknown result type (might be due to invalid IL or missing references)
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
//IL_00da: Unknown result type (might be due to invalid IL or missing references)
//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
//IL_01d4: 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)
//IL_0179: Unknown result type (might be due to invalid IL or missing references)
//IL_0182: Unknown result type (might be due to invalid IL or missing references)
//IL_0189: Unknown result type (might be due to invalid IL or missing references)
//IL_0242: Unknown result type (might be due to invalid IL or missing references)
//IL_0247: Unknown result type (might be due to invalid IL or missing references)
//IL_0296: Unknown result type (might be due to invalid IL or missing references)
//IL_029d: Unknown result type (might be due to invalid IL or missing references)
//IL_0256: Unknown result type (might be due to invalid IL or missing references)
//IL_025d: Unknown result type (might be due to invalid IL or missing references)
if (Lib.isDemoFinished() && !Lib.modIsActivated())
{
MessageHud.instance.ShowMessage((MessageType)2, "The test period for the Mod 'Dryzlers Sort Everything Into Chests' is over." + Environment.NewLine + "Visit https://dryzlers-mods.com for more information.", 0, (Sprite)null);
return;
}
Inventory inventory = ((Humanoid)Player.m_localPlayer).GetInventory();
int value = excludedSlots.Value;
for (int i = inventory.GetWidth() + value; i < inventory.GetWidth() * inventory.GetHeight(); i++)
{
Vector2Int val = ConvertToGrid(i);
ItemData itemAt = inventory.GetItemAt(((Vector2Int)(ref val)).x, ((Vector2Int)(ref val)).y);
if (itemAt == null || itemAt.m_equipped)
{
continue;
}
int num = itemAt.m_stack;
int num2 = 0;
foreach (Container container in ContainerLifecycle._containers)
{
if ((Object)(object)container == (Object)null)
{
continue;
}
if (num <= 0)
{
break;
}
bool flag = false;
if (Vector3.Distance(((Component)container).transform.position, ((Component)Player.m_localPlayer).transform.position) > (float)containerLookupRange.Value)
{
continue;
}
Inventory inventory2 = container.GetInventory();
foreach (ItemData allItem in inventory2.GetAllItems())
{
if (!(itemAt.m_shared.m_name == allItem.m_shared.m_name))
{
continue;
}
flag = true;
int num3 = allItem.m_shared.m_maxStackSize - allItem.m_stack;
num2 = allItem.m_shared.m_maxStackSize;
if (num3 > 0)
{
if (num <= num3)
{
Vector2i itemLocationInBox = GetItemLocationInBox(inventory2, allItem);
inventory2.MoveItemToThis(inventory, itemAt, num, itemLocationInBox.x, itemLocationInBox.y);
num -= num;
Traverse.Create((object)inventory2).Method("Changed", Array.Empty<object>()).GetValue();
break;
}
Vector2i itemLocationInBox2 = GetItemLocationInBox(inventory2, allItem);
inventory2.MoveItemToThis(inventory, itemAt, num3, itemLocationInBox2.x, itemLocationInBox2.y);
num -= num3;
Traverse.Create((object)inventory2).Method("Changed", Array.Empty<object>()).GetValue();
}
}
if (!flag || num2 <= 0)
{
continue;
}
while (num > 0 && inventory2.HaveEmptySlot())
{
Vector2i freeLocationInBox = GetFreeLocationInBox(inventory2);
if (num >= num2)
{
inventory2.MoveItemToThis(inventory, itemAt, num2, freeLocationInBox.x, freeLocationInBox.y);
num -= num2;
Traverse.Create((object)inventory2).Method("Changed", Array.Empty<object>()).GetValue();
}
else
{
inventory2.MoveItemToThis(inventory, itemAt, num, freeLocationInBox.x, freeLocationInBox.y);
num = 0;
Traverse.Create((object)inventory2).Method("Changed", Array.Empty<object>()).GetValue();
}
}
}
}
}
private static bool BoxHasItem(Inventory boxInventory, ItemData item)
{
for (int i = 0; i < boxInventory.GetWidth(); i++)
{
for (int j = 0; j < boxInventory.GetHeight(); j++)
{
ItemData itemAt = boxInventory.GetItemAt(i, j);
if (itemAt != null && itemAt.m_stack > 0 && itemAt.m_shared.m_name == item.m_shared.m_name)
{
return true;
}
}
}
return false;
}
private static Vector2i GetFreeLocationInBox(Inventory boxInventory)
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_0027: Unknown result type (might be due to invalid IL or missing references)
for (int i = 0; i < boxInventory.GetWidth(); i++)
{
for (int j = 0; j < boxInventory.GetHeight(); j++)
{
ItemData itemAt = boxInventory.GetItemAt(i, j);
if (itemAt == null)
{
return new Vector2i(i, j);
}
if (itemAt.m_stack <= 0)
{
return new Vector2i(i, j);
}
}
}
throw new Exception("no free slot found (should not happen");
}
private static Vector2i GetItemLocationInBox(Inventory boxInventory, ItemData item)
{
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
int num = boxInventory.GetAllItems().IndexOf(item);
return new Vector2i(boxInventory.GetItem(num).m_gridPos.x, boxInventory.GetItem(num).m_gridPos.y);
}
private static Vector2Int ConvertToGrid(int index)
{
//IL_005b: Unknown result type (might be due to invalid IL or missing references)
Vector2Int result = default(Vector2Int);
((Vector2Int)(ref result))..ctor(0, 0);
float num = 1f;
float num2 = 0f;
if (index != 0)
{
num = (float)Math.Floor((float)index / 8f);
}
float num3 = (float)Math.Floor((float)index / 8f);
num2 = ((float)index / 8f - num3) * 8f;
((Vector2Int)(ref result)).y = (int)num;
((Vector2Int)(ref result)).x = (int)num2;
return result;
}
}