using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GGECS;
using HarmonyLib;
using Inventory;
using Inventory.Unity;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Tower Storage Plus")]
[assembly: AssemblyProduct("Tower Storage Plus")]
[assembly: AssemblyDescription("Tower-wide crafting storage and larger item stacks for Wizard with a Gun")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyVersion("1.0.2.0")]
namespace BoogaJoker.TowerStoragePlus;
[BepInPlugin("com.boogajoker.towerstorageplus", "Tower Storage Plus", "1.0.2")]
public sealed class Plugin : BaseUnityPlugin
{
public const string PluginGuid = "com.boogajoker.towerstorageplus";
public const string PluginName = "Tower Storage Plus";
public const string PluginVersion = "1.0.2";
private Harmony _patches;
internal static ManualLogSource ModLog { get; private set; }
internal static int TowerSearchWidth { get; private set; }
private void Awake()
{
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_001b: Expected O, but got Unknown
ModLog = ((BaseUnityPlugin)this).Logger;
_patches = new Harmony("com.boogajoker.towerstorageplus");
_patches.PatchAll();
((MonoBehaviour)this).StartCoroutine(KeepTowerStorageActive());
((BaseUnityPlugin)this).Logger.LogInfo((object)"Tower Storage Plus 1.0.2 loaded. Waiting for the Tower map.");
}
private IEnumerator KeepTowerStorageActive()
{
int reportedWidth = -1;
while (true)
{
if (SimUtil.worldRunning && (Object)(object)GameUniverse.towerWorld != (Object)null && GameUniverse.towerWorld.world != null && GameUniverse.towerWorld.world.isValid)
{
ECSWorld world = GameUniverse.towerWorld.world;
GroundData mainThreadData = world.GetMainThreadData<GroundData>();
if (mainThreadData.tileToEntityMap.IsCreated && mainThreadData.tileToEntityMap.Count() > 0)
{
NativeArray<int2> keyArray = mainThreadData.tileToEntityMap.GetKeyArray((Allocator)2);
int2 val = keyArray[0];
int2 val2 = keyArray[0];
for (int i = 1; i < keyArray.Length; i++)
{
val = math.min(val, keyArray[i]);
val2 = math.max(val2, keyArray[i]);
}
keyArray.Dispose();
int2 val3 = val2 - val + 1;
int num = math.max(val3.x, val3.y);
LockAndAssignInteractableOnInteractSystem system = world.GetSystem<LockAndAssignInteractableOnInteractSystem>();
if (system != null)
{
GameDataCache.GetGameData<AdditionalStorageGameData>(((Component)GameUniverse.towerWorld).gameObject).connectionAreaWidth = num;
TowerSearchWidth = num;
system.GetSystemData();
StorageSystemPatches.SetSearchWidth(system, num);
if (reportedWidth != num)
{
reportedWidth = num;
ModLog.LogInfo((object)($"Tower storage is active across the full {val3.x} x {val3.y} tile map " + $"(search width {num})."));
}
}
}
}
yield return (object)new WaitForSecondsRealtime(1f);
}
}
private void OnDestroy()
{
((MonoBehaviour)this).StopAllCoroutines();
Harmony patches = _patches;
if (patches != null)
{
patches.UnpatchSelf();
}
}
}
[HarmonyPatch(typeof(StaticItem), "GetItemCreateDesc")]
internal static class NewItemStackPatch
{
private static void Postfix(ref UnsafeCreateEntityDesc __result)
{
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
//IL_0025: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
//IL_0038: Unknown result type (might be due to invalid IL or missing references)
if (((UnsafeCreateEntityDesc)(ref __result)).HasComponent<ItemComp>() && ((UnsafeCreateEntityDesc)(ref __result)).HasComponent<ItemCountComp>() && InventoryUtil.IsStackManageableType(((UnsafeCreateEntityDesc)(ref __result)).GetComponentData<ItemComp>().itemType))
{
ItemCountComp componentData = ((UnsafeCreateEntityDesc)(ref __result)).GetComponentData<ItemCountComp>();
componentData.maxCount = 999;
((UnsafeCreateEntityDesc)(ref __result)).SetComponentData<ItemCountComp>(componentData);
}
}
}
internal static class StackPatches
{
internal const int MaximumStack = 999;
internal static bool CanStack(InventoryData inventory, ItemEntity item)
{
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
if (((InventoryData)(ref inventory)).IsItemValid(item))
{
return InventoryUtil.IsStackManageableType(((InventoryData)(ref inventory)).GetItemType(item));
}
return false;
}
}
[HarmonyPatch(typeof(InventoryData), "GetItemCountData")]
internal static class ExistingItemCountDataPatch
{
private static void Postfix(InventoryData __instance, ItemEntity item, ref int maxCount)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
if (StackPatches.CanStack(__instance, item))
{
maxCount = 999;
}
}
}
[HarmonyPatch(typeof(InventoryData), "GetItemMaxCount", new Type[] { typeof(ItemEntity) })]
internal static class ExistingItemMaxPatch
{
private static void Postfix(InventoryData __instance, ItemEntity item, ref int __result)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
if (StackPatches.CanStack(__instance, item))
{
__result = 999;
}
}
}
[HarmonyPatch(typeof(InventoryData), "IsItemFull")]
internal static class ExistingItemFullPatch
{
private static bool Prefix(InventoryData __instance, ItemEntity item, ref bool __result)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_000e: Unknown result type (might be due to invalid IL or missing references)
if (!StackPatches.CanStack(__instance, item))
{
return true;
}
__result = ((InventoryData)(ref __instance)).GetItemCount(item) >= 999;
return false;
}
}
[HarmonyPatch(typeof(InventoryData), "SetItemCount")]
internal static class ExistingItemSetCountPatch
{
private static void Prefix(ref InventoryData __instance, ItemEntity item)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
//IL_0031: Unknown result type (might be due to invalid IL or missing references)
//IL_0032: Unknown result type (might be due to invalid IL or missing references)
if (StackPatches.CanStack(__instance, item))
{
ItemCountComp itemComponent = ((InventoryData)(ref __instance)).GetItemComponent<ItemCountComp>(item);
if (itemComponent.maxCount != 999)
{
itemComponent.maxCount = 999;
((InventoryData)(ref __instance)).SetItemComponent<ItemCountComp>(item, itemComponent);
}
}
}
}
[HarmonyPatch(typeof(LockAndAssignInteractableOnInteractSystem), "GetSystemData")]
internal static class StorageSystemDataPatch
{
private static void Postfix(LockAndAssignInteractableOnInteractSystem __instance)
{
if (Plugin.TowerSearchWidth > 0)
{
StorageSystemPatches.SetSearchWidth(__instance, Plugin.TowerSearchWidth);
}
}
}
[HarmonyPatch(typeof(LockAndAssignInteractableOnInteractSystem), "OnHandleCommand")]
internal static class StorageInteractionPatch
{
private static void Prefix(LockAndAssignInteractableOnInteractSystem __instance)
{
if (Plugin.TowerSearchWidth > 0)
{
StorageSystemPatches.SetSearchWidth(__instance, Plugin.TowerSearchWidth);
}
}
}
internal static class StorageSystemPatches
{
private static readonly FieldRef<LockAndAssignInteractableOnInteractSystem, int2> SearchWidth = AccessTools.FieldRefAccess<LockAndAssignInteractableOnInteractSystem, int2>("_additionalStorageWidth");
internal static void SetSearchWidth(LockAndAssignInteractableOnInteractSystem system, int width)
{
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
SearchWidth.Invoke(system) = new int2(width, width);
}
}