using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.EventSystems;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Replenishment")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Replenishment")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("2ee612e5-a3f8-40ed-b0a1-eecf723b4e6e")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace ReplenishmentMod;
[BepInPlugin("com.hetima.dsp.Replenishment", "Replenishment", "1.1.0")]
public class Replenishment : BaseUnityPlugin
{
public static class Configs
{
public static bool configEnableOutgoingStorage = false;
public static bool configEnableSearchingAllPlanets = false;
public static bool configEnableSearchingInterstellarStations = false;
public static bool configEnableRightClickOnReplicator = true;
}
private static class Patch
{
internal static bool _initialized;
[HarmonyPostfix]
[HarmonyPatch(typeof(UIBuildMenu), "StaticLoad")]
public static void UIBuildMenu_StaticLoad_Postfix()
{
if (!_initialized)
{
ref ItemProto[,] reference = ref AccessTools.StaticFieldRefAccess<ItemProto[,]>(typeof(UIBuildMenu), "protos");
reference[6, 10] = ((ProtoSet<ItemProto>)(object)LDB.items).Select(5002);
reference[6, 9] = ((ProtoSet<ItemProto>)(object)LDB.items).Select(5001);
reference[6, 8] = ((ProtoSet<ItemProto>)(object)LDB.items).Select(5003);
reference[4, 10] = ((ProtoSet<ItemProto>)(object)LDB.items).Select(1210);
reference[4, 9] = ((ProtoSet<ItemProto>)(object)LDB.items).Select(1803);
reference[4, 8] = ((ProtoSet<ItemProto>)(object)LDB.items).Select(1802);
reference[4, 7] = ((ProtoSet<ItemProto>)(object)LDB.items).Select(1801);
reference[1, 10] = ((ProtoSet<ItemProto>)(object)LDB.items).Select(2207);
_initialized = true;
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UIBuildMenu), "_OnRegEvent")]
public static void UIBuildMenu_OnRegEvent_Postfix(UIBuildMenu __instance)
{
for (int i = 0; i < __instance.childButtons.Length; i++)
{
if ((Object)(object)__instance.childButtons[i] != (Object)null)
{
__instance.childButtons[i].onRightClick += OnToolBtnRightClick;
__instance.childButtons[i].data = i;
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UIBuildMenu), "_OnUnregEvent")]
public static void UIBuildMenu_OnUnregEvent_Postfix(UIBuildMenu __instance)
{
for (int i = 0; i < __instance.childButtons.Length; i++)
{
if ((Object)(object)__instance.childButtons[i] != (Object)null)
{
__instance.childButtons[i].onRightClick -= OnToolBtnRightClick;
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UIReplicatorWindow), "OnRecipeMouseDown")]
public static void UIReplicatorWindow_OnRecipeMouseDown_Postfix(UIReplicatorWindow __instance, BaseEventData evtData)
{
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_001a: Invalid comparison between Unknown and I4
if ((Object)(object)__instance != (Object)null)
{
PointerEventData val = (PointerEventData)(object)((evtData is PointerEventData) ? evtData : null);
if (val != null && (int)val.button == 1)
{
OnReplicatorRightClick();
}
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(UIReplicatorWindow), "_OnRegEvent")]
public static void UIReplicatorWindow_OnRegEvent_Prefix(UIReplicatorWindow __instance)
{
List<UIButton> list = AccessTools.FieldRefAccess<UIReplicatorWindow, List<UIButton>>(__instance, "treeUpList");
List<UIButton> list2 = AccessTools.FieldRefAccess<UIReplicatorWindow, List<UIButton>>(__instance, "treeDownList");
foreach (UIButton item in list)
{
item.onRightClick += OnReplicatorRightClick;
}
foreach (UIButton item2 in list2)
{
item2.onRightClick += OnReplicatorRightClick;
}
if (list.Count < 8)
{
for (int i = list.Count; i < 8; i++)
{
UIButton val = Object.Instantiate<UIButton>(__instance.treeUpPrefab, ((Component)__instance.treeUpPrefab).transform.parent);
val.onRightClick += OnReplicatorRightClick;
list.Add(val);
}
}
if (list2.Count < 8)
{
for (int j = list2.Count; j < 8; j++)
{
UIButton val2 = Object.Instantiate<UIButton>(__instance.treeDownPrefab, ((Component)__instance.treeDownPrefab).transform.parent);
val2.onRightClick += OnReplicatorRightClick;
list2.Add(val2);
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UIReplicatorWindow), "_OnUnregEvent")]
public static void UIReplicatorWindow_OnUnregEvent_Postfix(UIReplicatorWindow __instance)
{
List<UIButton> obj = AccessTools.FieldRefAccess<UIReplicatorWindow, List<UIButton>>(__instance, "treeUpList");
List<UIButton> list = AccessTools.FieldRefAccess<UIReplicatorWindow, List<UIButton>>(__instance, "treeDownList");
foreach (UIButton item in obj)
{
item.onRightClick -= OnReplicatorRightClick;
}
foreach (UIButton item2 in list)
{
item2.onRightClick -= OnReplicatorRightClick;
}
}
}
public const string __NAME__ = "Replenishment";
public const string __GUID__ = "com.hetima.dsp.Replenishment";
internal static ManualLogSource Logger;
private void Awake()
{
//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
Logger = ((BaseUnityPlugin)this).Logger;
Configs.configEnableOutgoingStorage = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableOutgoingStorage", false, "Whether or not to enable picking items from storages with an outgoing sorter attached.").Value;
Configs.configEnableSearchingAllPlanets = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableSearchingAllPlanets", false, "Whether or not to enable picking items from storages on any planets.").Value;
Configs.configEnableSearchingInterstellarStations = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableSearchingInterstellarStations", false, "Whether or not to enable picking items from interstellar stations.").Value;
Configs.configEnableRightClickOnReplicator = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableRightClickOnReplicator", true, "Enable right-click to replenish on Replicator window.").Value;
new Harmony("com.hetima.dsp.Replenishment").PatchAll(typeof(Patch));
}
public static PlanetFactory BirthPlanetFactory()
{
GameData gameData = UIRoot.instance.uiGame.gameData;
PlanetFactory[] factories = gameData.factories;
int birthPlanetId = gameData.galaxy.birthPlanetId;
for (int i = 0; i < gameData.factoryCount; i++)
{
if (factories[i].planetId == birthPlanetId)
{
return factories[i];
}
}
return null;
}
private static int GetFromFactoryStorages(PlanetFactory factory, int itemId, out int inc)
{
inc = 0;
if (factory == null)
{
return 0;
}
int num = StorageComponent.itemStackCount[itemId];
int num2 = 0;
FactoryStorage factoryStorage = factory.factoryStorage;
int num3 = default(int);
int num4 = default(int);
int num5 = default(int);
for (int i = 1; i < factoryStorage.storageCursor; i++)
{
StorageComponent val = factoryStorage.storagePool[i];
if (val == null || val.entityId <= 0 || val.GetItemCount(itemId) <= 0)
{
continue;
}
if (!Configs.configEnableOutgoingStorage)
{
bool flag = false;
for (int j = 0; j <= 11; j++)
{
factory.ReadObjectConn(val.entityId, j, ref flag, ref num3, ref num4);
if (flag)
{
break;
}
}
if (flag)
{
continue;
}
}
num2 += val.TakeItem(itemId, num, ref num5);
inc += num5;
num -= num2;
if (num <= 0)
{
return num2;
}
}
return num2;
}
private static int GetFromFactoryStations(PlanetFactory factory, int itemId, int count, out int inc)
{
inc = 0;
if (factory == null || factory.factorySystem == null || factory.transport == null)
{
return 0;
}
int num = count;
int num2 = 0;
StationComponent[] stationPool = factory.transport.stationPool;
int num5 = default(int);
foreach (StationComponent val in stationPool)
{
if (val != null && val.entityId > 0 && !val.isVeinCollector && !val.isCollector && val.isStellar)
{
int num3 = num;
int num4 = itemId;
val.TakeItem(ref num4, ref num3, ref num5);
inc += num5;
num -= num3;
num2 += num3;
if (num <= 0)
{
return num2;
}
}
}
return num2;
}
private static bool CheckInventoryCapacity()
{
Player mainPlayer = UIRoot.instance.uiGame.gameData.mainPlayer;
if (mainPlayer != null)
{
for (int num = mainPlayer.package.size - 1; num >= 0; num--)
{
if (mainPlayer.package.grids[num].itemId == 0)
{
return true;
}
}
}
return false;
}
public static bool DeliverFromAllPlanets(int itemId, out string err)
{
PlanetFactory[] factories = UIRoot.instance.uiGame.gameData.factories;
if (!CheckInventoryCapacity())
{
err = "Inventory is full";
return false;
}
err = "Item not found";
int num = 0;
int num2 = StorageComponent.itemStackCount[itemId];
Player mainPlayer = UIRoot.instance.uiGame.gameData.mainPlayer;
int num3 = 0;
PlanetFactory[] array = factories;
foreach (PlanetFactory factory in array)
{
num += GetFromFactoryStorages(factory, itemId, out var inc);
num3 += inc;
num2 -= num;
if (num2 <= 0)
{
break;
}
if (Configs.configEnableSearchingInterstellarStations)
{
num += GetFromFactoryStations(factory, itemId, num2, out var inc2);
num3 += inc2;
num2 -= num;
if (num2 <= 0)
{
break;
}
}
}
if (num > 0)
{
int num4 = mainPlayer.TryAddItemToPackage(itemId, num, num3, false, 0, false);
UIItemup.Up(itemId, num4);
return true;
}
return false;
}
public static bool DeliverFromBirthPlanet(int itemId, out string err)
{
if (!CheckInventoryCapacity())
{
err = "Inventory is full";
return false;
}
int num = StorageComponent.itemStackCount[itemId];
Player mainPlayer = UIRoot.instance.uiGame.gameData.mainPlayer;
PlanetFactory factory = BirthPlanetFactory();
err = "Item not found";
int inc;
int num2 = GetFromFactoryStorages(factory, itemId, out inc);
int inc2 = 0;
if (Configs.configEnableSearchingInterstellarStations && num2 < num)
{
num -= num2;
num2 += GetFromFactoryStations(factory, itemId, num, out inc2);
}
if (num2 > 0)
{
int num3 = mainPlayer.TryAddItemToPackage(itemId, num2, inc + inc2, false, 0, false);
UIItemup.Up(itemId, num3);
return true;
}
return false;
}
public static bool DeliverFromVoid(int itemId, out string err)
{
if (!CheckInventoryCapacity())
{
err = "Inventory is full";
return false;
}
err = "";
Player mainPlayer = UIRoot.instance.uiGame.gameData.mainPlayer;
int num = StorageComponent.itemStackCount[itemId];
int num2 = 0;
int num3 = mainPlayer.TryAddItemToPackage(itemId, num, num2, false, 0, false);
UIItemup.Up(itemId, num3);
return true;
}
public static bool DeliverFrom(int itemId, out string err)
{
if (GameMain.sandboxToolsEnabled)
{
return DeliverFromVoid(itemId, out err);
}
if (Configs.configEnableSearchingAllPlanets)
{
return DeliverFromAllPlanets(itemId, out err);
}
return DeliverFromBirthPlanet(itemId, out err);
}
private static void OnToolBtnRightClick(int obj)
{
//IL_006c: Unknown result type (might be due to invalid IL or missing references)
//IL_0055: Unknown result type (might be due to invalid IL or missing references)
UIBuildMenu buildMenu = UIRoot.instance.uiGame.buildMenu;
if (buildMenu.childButtons.Length <= obj)
{
return;
}
UIButton val = buildMenu.childButtons[obj];
if (!((Object)(object)val != (Object)null))
{
return;
}
int itemId = val.tips.itemId;
if (itemId < 12000 && itemId > 0)
{
if (DeliverFrom(itemId, out var err))
{
VFAudio.Create("transfer-item", (Transform)null, Vector3.zero, true, 0, -1, -1L);
return;
}
VFAudio.Create("ui-error", (Transform)null, Vector3.zero, true, 5, -1, -1L);
UIRealtimeTip.Popup(err, false, 0);
}
}
public static int ItemIdHintUnderMouse()
{
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Expected O, but got Unknown
//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_003b: Unknown result type (might be due to invalid IL or missing references)
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
//IL_0055: Unknown result type (might be due to invalid IL or missing references)
List<RaycastResult> list = new List<RaycastResult>();
PointerEventData val = new PointerEventData(EventSystem.current);
val.position = Vector2.op_Implicit(Input.mousePosition);
EventSystem.current.RaycastAll(val, list);
foreach (RaycastResult item in list)
{
RaycastResult current = item;
UIButton componentInParent = ((RaycastResult)(ref current)).gameObject.GetComponentInParent<UIButton>();
if (componentInParent != null)
{
_ = componentInParent.tips;
if (componentInParent.tips.itemId > 0)
{
return componentInParent.tips.itemId;
}
}
UIReplicatorWindow componentInParent2 = ((RaycastResult)(ref current)).gameObject.GetComponentInParent<UIReplicatorWindow>();
if ((Object)(object)componentInParent2 != (Object)null)
{
int num = AccessTools.FieldRefAccess<UIReplicatorWindow, int>(componentInParent2, "mouseRecipeIndex");
RecipeProto[] array = AccessTools.FieldRefAccess<UIReplicatorWindow, RecipeProto[]>(componentInParent2, "recipeProtoArray");
if (num < 0)
{
return 0;
}
RecipeProto val2 = array[num];
if (val2 != null)
{
return val2.Results[0];
}
return 0;
}
UIStorageGrid componentInParent3 = ((RaycastResult)(ref current)).gameObject.GetComponentInParent<UIStorageGrid>();
if ((Object)(object)componentInParent3 != (Object)null)
{
StorageComponent val3 = AccessTools.FieldRefAccess<UIStorageGrid, StorageComponent>(componentInParent3, "storage");
int num2 = AccessTools.FieldRefAccess<UIStorageGrid, int>(componentInParent3, "mouseOnX");
int num3 = AccessTools.FieldRefAccess<UIStorageGrid, int>(componentInParent3, "mouseOnY");
if (num2 >= 0 && num3 >= 0 && val3 != null)
{
int num4 = num2 + num3 * componentInParent3.colCount;
return val3.grids[num4].itemId;
}
return 0;
}
UIProductEntry componentInParent4 = ((RaycastResult)(ref current)).gameObject.GetComponentInParent<UIProductEntry>();
if ((Object)(object)componentInParent4 != (Object)null)
{
if (componentInParent4.productionStatWindow.isProductionTab)
{
return componentInParent4.entryData?.itemId ?? 0;
}
return 0;
}
}
return 0;
}
private static void OnReplicatorRightClick(int obj = 0)
{
//IL_0039: Unknown result type (might be due to invalid IL or missing references)
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
if (!Configs.configEnableRightClickOnReplicator)
{
return;
}
int num = ItemIdHintUnderMouse();
if (num > 0)
{
if (DeliverFrom(num, out var err))
{
VFAudio.Create("transfer-item", (Transform)null, Vector3.zero, true, 0, -1, -1L);
return;
}
VFAudio.Create("ui-error", (Transform)null, Vector3.zero, true, 5, -1, -1L);
UIRealtimeTip.Popup(err, false, 0);
}
}
}