Decompiled source of YetAnotherSellMod v1.0.0

baer1.YetAnotherSellMod.dll

Decompiled a week 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.Configuration;
using BepInEx.Logging;
using ChatCommandAPI;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
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("baer1.YetAnotherSellMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+f64540d044d36011530a2a5afc83faccf5f4ee91")]
[assembly: AssemblyProduct("LethalSellMod")]
[assembly: AssemblyTitle("baer1.YetAnotherSellMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 LethalSellMod
{
	[BepInPlugin("baer1.YetAnotherSellMod", "LethalSellMod", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class LethalSellMod : BaseUnityPlugin
	{
		private ConfigEntry<string> itemBlacklist = null;

		private ConfigEntry<string> itemPriority = null;

		internal Dictionary<string, int> ItemPriority = null;

		public static LethalSellMod Instance { get; private set; }

		internal static ManualLogSource Logger { get; private set; }

		internal string[] ItemBlacklist => csv(itemBlacklist.Value);

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			itemBlacklist = ((BaseUnityPlugin)this).Config.Bind<string>("Items", "ItemBlacklist", csv(new string[1] { "ShotgunItem" }), "Items to never sell by internal name (comma-separated)");
			itemPriority = ((BaseUnityPlugin)this).Config.Bind<string>("Items", "ItemPriority", csv(Array.Empty<string>()), "Items to sell before others (by internal name, comma-separated)");
			ItemPriority = csv(itemPriority.Value).Select((string e, int i) => new KeyValuePair<string, int>(e, i)).ToDictionary((KeyValuePair<string, int> kvp) => kvp.Key, (KeyValuePair<string, int> kvp) => kvp.Value);
			new SellCommand();
			Logger.LogInfo((object)"baer1.YetAnotherSellMod v1.0.0 has loaded!");
		}

		private static string csv(string[] e)
		{
			return GeneralExtensions.Join<string>((IEnumerable<string>)e, (Func<string, string>)null, ",");
		}

		private static string[] csv(string e)
		{
			return e.Split(',');
		}
	}
	public class SellCommand : Command
	{
		private const string CLONE = "(Clone)";

		public override string Name => "Sell";

		public override string Description => "Sells items on 71-Gordion";

		public override string[] Syntax => new string[2] { "[value]", "{ quota | all }" };

		public override bool Invoke(string[] args, Dictionary<string, string> kwargs, out string error)
		{
			error = "Invalid arguments";
			int itemCount;
			int totalValue;
			switch (args.Length)
			{
			case 0:
				if (!OpenDoor(out error, out itemCount, out totalValue))
				{
					return false;
				}
				ChatCommandAPI.Print("Selling " + a(itemCount) + " with a total value of " + b(totalValue));
				return true;
			case 1:
			{
				string text = args[0].ToLower();
				string text2 = text;
				int? value;
				if (!(text2 == "quota"))
				{
					if (text2 == "all")
					{
						value = null;
					}
					else
					{
						if (!int.TryParse(args[0], out var result))
						{
							return false;
						}
						value = result;
					}
				}
				else
				{
					value = TimeOfDay.Instance.profitQuota - TimeOfDay.Instance.quotaFulfilled;
				}
				error = "Selling desk not found";
				DepositItemsDesk val = Object.FindObjectOfType<DepositItemsDesk>();
				if ((Object)(object)GameNetworkManager.Instance == (Object)null || (Object)(object)val == (Object)null)
				{
					return false;
				}
				error = "No items found";
				List<GrabbableObject> list = ItemsForValue(value, val);
				if (list == null)
				{
					return false;
				}
				error = "Error selling items";
				if (!SellItems(list, val, out itemCount))
				{
					return false;
				}
				if (!val.doorOpen && !value.HasValue)
				{
					if (!OpenDoor(out string _, out itemCount, out totalValue))
					{
						return false;
					}
					ChatCommandAPI.Print("Selling " + a(itemCount) + " with a total value of " + b(totalValue));
				}
				else
				{
					ChatCommandAPI.Print("Put " + a(itemCount) + " worth " + b(list) + " on the desk");
				}
				return true;
			}
			default:
				return false;
			}
		}

		private static string a(int itemCount)
		{
			return itemCount + ((itemCount == 1) ? " item" : " items");
		}

		private static string b(List<GrabbableObject> items)
		{
			return items.Sum((GrabbableObject i) => i.scrapValue) + (Mathf.Approximately(StartOfRound.Instance.companyBuyingRate, 1f) ? "" : $" ({(int)((float)items.Sum((GrabbableObject i) => i.scrapValue) * StartOfRound.Instance.companyBuyingRate)})");
		}

		private static string b(int totalValue)
		{
			return totalValue + (Mathf.Approximately(StartOfRound.Instance.companyBuyingRate, 1f) ? "" : $" ({(int)((float)totalValue * StartOfRound.Instance.companyBuyingRate)})");
		}

		private static List<GrabbableObject>? ItemsForValue(int? value, DepositItemsDesk desk)
		{
			GrabbableObject[] array = Object.FindObjectsOfType<GrabbableObject>();
			if (array == null || array.Length == 0)
			{
				return null;
			}
			array = FilterAndSortItems(array, desk);
			if (array.Length == 0)
			{
				return null;
			}
			if (array.Length == 1 || !value.HasValue)
			{
				return array.ToList();
			}
			value = (int)Math.Ceiling(((float?)value / StartOfRound.Instance.companyBuyingRate).Value);
			int num = array.Length;
			int num2 = int.MinValue;
			List<GrabbableObject> result = new List<GrabbableObject>();
			for (int i = 0; i < 1 << num; i++)
			{
				List<GrabbableObject> list = new List<GrabbableObject>();
				int num3 = 0;
				for (int j = 0; j < num; j++)
				{
					if ((i & (1 << j)) != 0)
					{
						list.Add(array[j]);
						num3 += array[j].scrapValue;
					}
				}
				int num4 = value.Value - num3;
				if (num4 > num2 && num4 <= 0)
				{
					LethalSellMod.Logger.LogDebug((object)string.Format("Found {0}match: {1} items for {2} value ({3} requested, diff:{4}, bestDiff:{5})", (num2 == int.MinValue) ? "" : ((num4 == 0) ? "best " : "better "), list.Count, num3, value.Value, num4, num2));
					if (num4 == 0)
					{
						return list;
					}
					num2 = num4;
					result = list;
				}
			}
			return result;
		}

		private static bool SellItems(List<GrabbableObject> items, DepositItemsDesk desk, out int itemCount)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: 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)
			GameNetworkManager.Instance.localPlayerController.DropAllHeldItemsAndSync();
			itemCount = 0;
			RaycastHit val2 = default(RaycastHit);
			foreach (GrabbableObject item in items)
			{
				if (!((Object)(object)item == (Object)null))
				{
					itemCount++;
					Vector3 val = RoundManager.RandomPointInBounds(((Collider)desk.triggerCollider).bounds);
					Bounds bounds = ((Collider)desk.triggerCollider).bounds;
					val.y = ((Bounds)(ref bounds)).min.y;
					if (Physics.Raycast(new Ray(val + Vector3.up * 3f, Vector3.down), ref val2, 8f, 1048640, (QueryTriggerInteraction)2))
					{
						val = ((RaycastHit)(ref val2)).point;
					}
					val.y += item.itemProperties.verticalOffset;
					val = ((Component)desk.deskObjectsContainer).transform.InverseTransformPoint(val);
					desk.AddObjectToDeskServerRpc(NetworkObjectReference.op_Implicit(((NetworkBehaviour)item).NetworkObject));
					GameNetworkManager.Instance.localPlayerController.PlaceGrabbableObject(((Component)desk.deskObjectsContainer).transform, val, false, item);
					GameNetworkManager.Instance.localPlayerController.PlaceObjectServerRpc(NetworkObjectReference.op_Implicit(((NetworkBehaviour)item).NetworkObject), NetworkObjectReference.op_Implicit(desk.deskObjectsContainer), val, false);
				}
			}
			return true;
		}

		public static string RemoveClone(string name)
		{
			string result;
			if (!name.EndsWith("(Clone)"))
			{
				result = name;
			}
			else
			{
				int length = "(Clone)".Length;
				result = name.Substring(0, name.Length - length);
			}
			return result;
		}

		private static GrabbableObject[] FilterAndSortItems(GrabbableObject[] items, DepositItemsDesk desk)
		{
			DepositItemsDesk desk2 = desk;
			return (from i in items.Where(delegate(GrabbableObject i)
				{
					int result;
					if (i != null && i.scrapValue > 0 && !i.isHeld && !i.isPocketed)
					{
						Item itemProperties = i.itemProperties;
						if (itemProperties != null && itemProperties.isScrap)
						{
							result = ((!desk2.itemsOnCounter.Contains(i)) ? 1 : 0);
							goto IL_0045;
						}
					}
					result = 0;
					goto IL_0045;
					IL_0045:
					return (byte)result != 0;
				})
				where !LethalSellMod.Instance.ItemBlacklist.Contains(RemoveClone(((Object)i).name))
				orderby LethalSellMod.Instance.ItemPriority.GetValueOrDefault(RemoveClone(((Object)i).name), int.MaxValue)
				select i).ToArray();
		}

		private static bool OpenDoor(out string error, out int itemCount, out int totalValue)
		{
			itemCount = 0;
			totalValue = 0;
			error = "Selling desk not found";
			DepositItemsDesk val = Object.FindObjectOfType<DepositItemsDesk>();
			if ((Object)(object)GameNetworkManager.Instance == (Object)null || (Object)(object)val == (Object)null)
			{
				return false;
			}
			itemCount = val.itemsOnCounter.Count;
			totalValue = val.itemsOnCounter.Sum((GrabbableObject i) => i.scrapValue);
			error = "No items on desk";
			if (itemCount == 0)
			{
				return false;
			}
			error = "Door already open";
			if (val.doorOpen)
			{
				return false;
			}
			val.SetTimesHeardNoiseServerRpc(5f);
			return true;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "baer1.YetAnotherSellMod";

		public const string PLUGIN_NAME = "LethalSellMod";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}