using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CasperEquinoxGUI;
using CasperEquinoxGUI.BaseClasses;
using CasperEquinoxGUI.Controls;
using CasperEquinoxGUI.Layouts;
using CasperEquinoxGUI.Utilities;
using EquinoxsModUtils;
using HarmonyLib;
using SmartInserters.Patches;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("SmartInserters")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SmartInserters")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("6f87a872-92b3-4a7f-b42d-5a160a437260")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SmartInserters
{
public static class NewLimitGUI
{
public static Window limitWindow;
public static Grid grid;
public static Textbox limitBox;
public static Button setButton;
private static uint currentInserterId;
public static void CreateLimitGUI()
{
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Expected O, but got Unknown
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
//IL_001c: 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)
//IL_002c: Expected O, but got Unknown
//IL_0032: Expected O, but got Unknown
//IL_0037: Unknown result type (might be due to invalid IL or missing references)
//IL_003c: Unknown result type (might be due to invalid IL or missing references)
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_0059: Expected O, but got Unknown
//IL_0090: Unknown result type (might be due to invalid IL or missing references)
//IL_0095: Unknown result type (might be due to invalid IL or missing references)
//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
//IL_00d2: 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_00e2: Unknown result type (might be due to invalid IL or missing references)
//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
//IL_0103: Expected O, but got Unknown
//IL_0104: Unknown result type (might be due to invalid IL or missing references)
//IL_0115: Expected O, but got Unknown
grid = new Grid(1u, 2u, "equal", "equal");
limitBox = new Textbox
{
Margin = new Thickness(0, 0, 0, 10)
};
setButton = new Button("Set")
{
RowIndex = 1u,
HorizontalContentAlignment = (HorizontalAlignment)1,
VerticalContentAlignment = (VerticalAlignment)1
};
((ClickableControl)setButton).LeftClicked += OnSetClicked;
((Layout)grid).AddControl((Control)(object)limitBox);
((Layout)grid).AddControl((Control)(object)setButton);
limitWindow = new Window
{
Width = 200,
Height = 90,
ShowBackground = false,
ShowTitle = false,
ShowCloseButton = false,
ShowShader = false,
Visible = false,
HorizontalAlignment = (HorizontalAlignment)0,
VerticalAlignment = (VerticalAlignment)0,
Margin = new Thickness(SmartInsertersPlugin.guiXOffset.Value, SmartInsertersPlugin.guiYOffset.Value, 0, 0),
RootLayout = (Layout)(object)grid
};
CaspuinoxGUI.RegisterWindow(ref limitWindow);
}
public static void Show()
{
//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)
currentInserterId = GetAimedAtInserter().commonInfo.instanceId;
limitWindow.Show();
if (SmartInsertersPlugin.inserterLimits.ContainsKey(currentInserterId))
{
limitBox.Input = SmartInsertersPlugin.inserterLimits[currentInserterId];
}
else
{
limitBox.Input = "";
}
}
public static void Hide()
{
Window obj = limitWindow;
if (obj != null)
{
obj.Hide();
}
}
public static void OnSetClicked(object sender, EventArgs e)
{
//IL_0098: Unknown result type (might be due to invalid IL or missing references)
//IL_009d: Unknown result type (might be due to invalid IL or missing references)
if (string.IsNullOrEmpty(limitBox.Input))
{
((FMODAudioSource)(ref Player.instance.audio.buildError)).PlayRandomClip(true);
Debug.Log((object)"limit is null or emtpy");
return;
}
string input = limitBox.Input;
input = input.ToLower();
if (!input.EndsWith("s") && !int.TryParse(input, out var _))
{
((FMODAudioSource)(ref Player.instance.audio.buildError)).PlayRandomClip(true);
Debug.Log((object)("limit: " + input));
}
else
{
uint instanceId = GetAimedAtInserter().commonInfo.instanceId;
SmartInsertersPlugin.inserterLimits[instanceId] = input;
((FMODAudioSource)(ref Player.instance.audio.buildClick)).PlayRandomClip(true);
}
}
public static InserterInstance GetAimedAtInserter()
{
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_002d: 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)
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
GenericMachineInstanceRef val = (GenericMachineInstanceRef)EMU.GetPrivateField<PlayerInteraction>("targetMachineRef", Player.instance.interaction);
return MachineManager.instance.Get<InserterInstance, InserterDefinition>(((GenericMachineInstanceRef)(ref val)).index, (MachineTypeEnum)7);
}
}
[BepInPlugin("com.equinox.SmartInserters", "SmartInserters", "2.0.0")]
public class SmartInsertersPlugin : BaseUnityPlugin
{
private const string MyGUID = "com.equinox.SmartInserters";
private const string PluginName = "SmartInserters";
private const string VersionString = "2.0.0";
private static readonly Harmony Harmony = new Harmony("com.equinox.SmartInserters");
public static ManualLogSource Log = new ManualLogSource("SmartInserters");
public static Dictionary<uint, string> inserterLimits = new Dictionary<uint, string>();
public static ConfigEntry<int> guiXOffset;
public static ConfigEntry<int> guiYOffset;
private static string dataFolder => Application.persistentDataPath + "/SmartInserters";
private void Awake()
{
((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: SmartInserters, VersionString: 2.0.0 is loading...");
Harmony.PatchAll();
CaspuinoxGUI.ReadyForGUI += OnReadyForGUI;
CreateConfigEntries();
ApplyPatches();
((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: SmartInserters, VersionString: 2.0.0 is loaded.");
Log = ((BaseUnityPlugin)this).Logger;
}
private void OnReadyForGUI()
{
NewLimitGUI.CreateLimitGUI();
}
public static void SaveData(string worldName)
{
Directory.CreateDirectory(dataFolder);
Directory.CreateDirectory(dataFolder + "/" + worldName);
string path = dataFolder + "/" + worldName + "/InserterLimits.txt";
List<string> list = new List<string>();
foreach (KeyValuePair<uint, string> inserterLimit in inserterLimits)
{
list.Add($"{inserterLimit.Key}|{inserterLimit.Value}");
}
File.WriteAllLines(path, list);
}
public static void LoadData(string worldName)
{
string path = dataFolder + "/" + worldName + "/InserterLimits.txt";
if (File.Exists(path))
{
string[] array = File.ReadAllLines(path);
string[] array2 = array;
foreach (string text in array2)
{
string[] array3 = text.Split(new char[1] { '|' });
uint key = uint.Parse(array3[0]);
inserterLimits[key] = array3[1];
}
}
}
private void ApplyPatches()
{
Harmony.CreateAndPatchAll(typeof(FilterInserterUIPatch), (string)null);
Harmony.CreateAndPatchAll(typeof(InserterInstancePatch), (string)null);
Harmony.CreateAndPatchAll(typeof(SaveStatePatch), (string)null);
}
private void CreateConfigEntries()
{
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
//IL_0027: Expected O, but got Unknown
//IL_0048: Unknown result type (might be due to invalid IL or missing references)
//IL_0052: Expected O, but got Unknown
guiXOffset = ((BaseUnityPlugin)this).Config.Bind<int>("General", "GUI X Offset", 0, new ConfigDescription("Controls the horizontal position of the limit GUI", (AcceptableValueBase)null, Array.Empty<object>()));
guiYOffset = ((BaseUnityPlugin)this).Config.Bind<int>("General", "GUI Y Offset", 0, new ConfigDescription("Controls the vertical position of the limit GUI", (AcceptableValueBase)null, Array.Empty<object>()));
}
}
}
namespace SmartInserters.Patches
{
internal class FilterInserterUIPatch
{
[HarmonyPatch(typeof(FilterInserterUI), "OnOpen")]
[HarmonyPostfix]
private static void ShowLimitGUI()
{
NewLimitGUI.Show();
}
[HarmonyPatch(typeof(FilterInserterUI), "OnClose")]
[HarmonyPostfix]
private static void HideLimitGUI()
{
NewLimitGUI.Hide();
}
}
internal class InserterInstancePatch
{
[HarmonyPatch(typeof(InserterInstance), "Give")]
[HarmonyPrefix]
private static bool ShouldGiveItems(InserterInstance __instance)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
//IL_0029: 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_00a3: Unknown result type (might be due to invalid IL or missing references)
//IL_00be: Unknown result type (might be due to invalid IL or missing references)
//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
uint instanceId = __instance.commonInfo.instanceId;
if (!SmartInsertersPlugin.inserterLimits.ContainsKey(instanceId))
{
return true;
}
if (__instance.filterType == -1)
{
return true;
}
ResourceInfo resInfoFromId = SaveState.GetResInfoFromId(__instance.filterType);
int maxStackCount = resInfoFromId.maxStackCount;
string text = SmartInsertersPlugin.inserterLimits[instanceId];
int num = 0;
if (!text.EndsWith("s"))
{
num = int.Parse(text);
}
else
{
text = text.Replace("s", "");
num = int.Parse(text) * maxStackCount;
}
int num2 = 0;
Inventory[] inventories = __instance.giveResourceContainer.GetCommonInfo().inventories;
for (int i = 0; i < inventories.Length; i++)
{
Inventory val = inventories[i];
num2 += ((Inventory)(ref val)).GetResourceCount(__instance.filterType, false);
}
if (num2 + __instance.currentHeldStackCount <= num)
{
return true;
}
return false;
}
}
internal class SaveStatePatch
{
[HarmonyPatch(typeof(SaveState), "SaveToFile")]
[HarmonyPostfix]
private static void SaveData()
{
SmartInsertersPlugin.SaveData(SaveState.instance.metadata.worldName);
}
[HarmonyPatch(typeof(SaveState), "LoadFileData", new Type[]
{
typeof(SaveMetadata),
typeof(string)
})]
[HarmonyPostfix]
private static void LoadData(SaveState __instance, SaveMetadata saveMetadata, string replayLocation)
{
SmartInsertersPlugin.LoadData(saveMetadata.worldName);
}
}
}