Decompiled source of ShipInventoryQuotaAddon v1.1.0

ShipInventoryQuotaAddon.dll

Decompiled 17 hours ago
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;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ShipInventoryUpdated.Objects;
using ShipInventoryUpdated.Scripts;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ShipInventoryQuotaAddon")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Auto-retrieves items to meet quota")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0")]
[assembly: AssemblyProduct("ShipInventoryQuotaAddon")]
[assembly: AssemblyTitle("ShipInventoryQuotaAddon")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ShipInventoryQuotaAddon
{
	public static class PluginInfo
	{
		public const string GUID = "Detologist.ShipInventoryQuotaAddon";

		public const string NAME = "Ship Inventory Quota Addon";

		public const string VERSION = "1.3.0";
	}
	[BepInPlugin("Detologist.ShipInventoryQuotaAddon", "Ship Inventory Quota Addon", "1.3.0")]
	public class QuotaPlugin : BaseUnityPlugin
	{
		public static ManualLogSource Log;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("Detologist.ShipInventoryQuotaAddon");
			val.PatchAll();
			Log.LogInfo((object)"Ship Inventory Quota Addon v1.3.0 loaded successfully.");
		}
	}
	[HarmonyPatch(typeof(Terminal))]
	public class TerminalPatch
	{
		[HarmonyPatch("ParsePlayerSentence")]
		[HarmonyPrefix]
		private static bool Prefix(Terminal __instance, ref TerminalNode __result)
		{
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			string text = __instance.screenText.text.Substring(__instance.screenText.text.Length - __instance.textAdded).ToLower().Trim();
			if (text == "rquota")
			{
				ItemData[] items = Inventory.Items;
				if (items == null || items.Length == 0)
				{
					__result = CreateNode("INVENTORY EMPTY");
					return false;
				}
				int num = TimeOfDay.Instance.profitQuota - TimeOfDay.Instance.quotaFulfilled;
				if (num <= 0)
				{
					__result = CreateNode("QUOTA ALREADY MET");
					return false;
				}
				var list = (from item in items
					select new
					{
						Data = item,
						Value = GetAnyValue(item)
					} into x
					where x.Value > 0
					select x).ToList();
				List<int> collection = FindBestCombination(list.Select(x => x.Value).ToList(), num);
				List<ItemData> list2 = new List<ItemData>();
				List<int> list3 = new List<int>(collection);
				int num2 = 0;
				foreach (var item in list)
				{
					if (list3.Contains(item.Value))
					{
						list2.Add(item.Data);
						num2 += item.Value;
						list3.Remove(item.Value);
					}
				}
				if (list2.Count > 0)
				{
					Inventory.Remove(list2.ToArray());
					__result = CreateNode($"QUOTA RETRIEVAL\n----------------\nQuota Required: {num}\nAmount Taken: {num2}\nExtra Profit: {num2 - ((num > 0) ? num : 0)}\nItems Retrieved: {list2.Count}");
				}
				else
				{
					__result = CreateNode("NO VALID COMBINATION FOUND");
				}
				return false;
			}
			return true;
		}

		private static List<int> FindBestCombination(List<int> values, int target)
		{
			if (values.Sum() <= target)
			{
				return values;
			}
			int count = values.Count;
			int num = target + ((values.Count > 0) ? values.Max() : 0);
			bool[] array = new bool[num + 1];
			int[] array2 = new int[num + 1];
			int[] array3 = new int[num + 1];
			array[0] = true;
			for (int i = 0; i < count; i++)
			{
				int num2 = values[i];
				for (int num3 = num; num3 >= num2; num3--)
				{
					if (!array[num3] && array[num3 - num2])
					{
						array[num3] = true;
						array2[num3] = num3 - num2;
						array3[num3] = num2;
					}
				}
			}
			for (int j = target; j <= num; j++)
			{
				if (array[j])
				{
					List<int> list = new List<int>();
					for (int num4 = j; num4 > 0; num4 = array2[num4])
					{
						list.Add(array3[num4]);
					}
					return list;
				}
			}
			return values;
		}

		private static int GetAnyValue(ItemData item)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			FieldInfo[] fields = typeof(ItemData).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			FieldInfo[] array = fields;
			foreach (FieldInfo fieldInfo in array)
			{
				if (fieldInfo.FieldType == typeof(int))
				{
					int num = (int)fieldInfo.GetValue(item);
					if (num > 1 && num < 5000)
					{
						return num;
					}
				}
			}
			return 0;
		}

		private static TerminalNode CreateNode(string message)
		{
			TerminalNode val = ScriptableObject.CreateInstance<TerminalNode>();
			val.displayText = message + "\n\n";
			val.clearPreviousText = true;
			return val;
		}
	}
}