Decompiled source of ChatToSell v1.0.0

ChatToSell.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
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: AssemblyTitle("ChatToSell")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ChatToSell")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d35b9883-5723-4d71-a822-a0f5fee5d9bb")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ChatToSell;

internal class AutoSell
{
	public static void StartAutoSell(int value)
	{
		//IL_0416: Unknown result type (might be due to invalid IL or missing references)
		//IL_0430: Unknown result type (might be due to invalid IL or missing references)
		//IL_0435: Unknown result type (might be due to invalid IL or missing references)
		//IL_043d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0442: Unknown result type (might be due to invalid IL or missing references)
		//IL_044c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0456: Unknown result type (might be due to invalid IL or missing references)
		//IL_0460: Unknown result type (might be due to invalid IL or missing references)
		if (RoundManager.Instance.currentLevel.sceneName != "CompanyBuilding")
		{
			ChatToSell.DisplayTip("ChatToSell", "Error : You must be at the Company Building to do this");
			return;
		}
		DepositItemsDesk val = Object.FindObjectOfType<DepositItemsDesk>();
		if (val == null)
		{
			ChatToSell.DisplayTip("ChatToSell", "Error : Please land the ship first");
			return;
		}
		if (value <= 0 && value != -1)
		{
			ChatToSell.DisplayTip("ChatToSell", "Error : " + value + " is not valid number");
			return;
		}
		GameObject val2 = GameObject.Find("/Environment/HangarShip");
		List<GrabbableObject> list = (from obj in val2.GetComponentsInChildren<GrabbableObject>()
			where obj.itemProperties.isScrap && !(obj is RagdollGrabbableObject)
			select obj).ToList();
		List<GrabbableObject> source = (from obj in Object.FindObjectsOfType<GrabbableObject>()
			where obj.itemProperties.isScrap && !(obj is RagdollGrabbableObject)
			select obj).ToList();
		HashSet<GrabbableObject> scrapsInShipSet = new HashSet<GrabbableObject>(list);
		List<GrabbableObject> source2 = source.Where((GrabbableObject x) => !scrapsInShipSet.Contains(x)).ToList();
		HashSet<string> hashSet = (from item in ChatToSell.ItemsNotToSell.Value.Split(new char[1] { ',' })
			select item.Trim().ToLower() into item
			where !string.IsNullOrEmpty(item)
			select item).ToHashSet();
		HashSet<string> itemsToSellFirst = (from item in ChatToSell.ItemsToSellFirst.Value.Split(new char[1] { ',' })
			select item.Trim().ToLower() into item
			where !string.IsNullOrEmpty(item)
			select item).ToHashSet();
		List<GrabbableObject> list2 = new List<GrabbableObject>();
		int num = 0;
		int num2 = 0;
		bool flag = value == -1;
		bool value2 = ChatToSell.PrioritizeItemsOnCruiser.Value;
		IOrderedEnumerable<GrabbableObject> first = from x in source2
			where itemsToSellFirst.Contains(x.itemProperties.itemName.ToLower())
			select x into obj
			orderby obj.scrapValue
			select obj;
		IOrderedEnumerable<GrabbableObject> second = from x in source2
			where !itemsToSellFirst.Contains(x.itemProperties.itemName.ToLower())
			select x into obj
			orderby obj.scrapValue
			select obj;
		IOrderedEnumerable<GrabbableObject> second2 = from x in list
			where itemsToSellFirst.Contains(x.itemProperties.itemName.ToLower())
			select x into obj
			orderby obj.scrapValue
			select obj;
		IOrderedEnumerable<GrabbableObject> second3 = from x in list
			where !itemsToSellFirst.Contains(x.itemProperties.itemName.ToLower())
			select x into obj
			orderby obj.scrapValue
			select obj;
		list2 = (value2 ? first.Concat(second).Concat(second2).Concat(second3)
			.ToList() : first.Concat(second2).Concat(second).Concat(second3)
			.ToList());
		PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
		MethodInfo methodInfo = AccessTools.Method(typeof(PlayerControllerB), "PlaceObjectServerRpc", (Type[])null, (Type[])null);
		foreach (GrabbableObject item in list2)
		{
			if (!hashSet.Contains(item.itemProperties.itemName.ToLower()) && !val.itemsOnCounter.Contains(item) && !IsHeldBySomeone(item) && (item.scrapValue + num <= value || flag))
			{
				num += item.scrapValue;
				num2++;
				PlaceItemOnCounter(item);
				localPlayerController.PlaceGrabbableObject(((Component)val.deskObjectsContainer).transform, Vector3.zero, false, item);
				NetworkObjectReference val3 = NetworkObjectReference.op_Implicit(((Component)item).gameObject.GetComponent<NetworkObject>());
				NetworkObjectReference val4 = NetworkObjectReference.op_Implicit(val.deskObjectsContainer);
				object[] parameters = new object[4]
				{
					val3,
					val4,
					Vector3.zero,
					false
				};
				methodInfo.Invoke(localPlayerController, parameters);
			}
		}
		ChatToSell.DisplayTip("ChatToSell", "Total sold : " + num + "\nItems sold : " + num2);
	}

	public static void PlaceItemOnCounter(GrabbableObject objectToSell)
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)GameNetworkManager.Instance != (Object)null)
		{
			DepositItemsDesk val = Object.FindObjectOfType<DepositItemsDesk>();
			if ((Object)(object)val == (Object)null)
			{
				ChatToSell.mls.LogInfo((object)"Error : Couldn't find the DepositItemsDesk");
				return;
			}
			NetworkObjectReference val2 = NetworkObjectReference.op_Implicit(((Component)objectToSell).gameObject.GetComponent<NetworkObject>());
			val.AddObjectToDeskServerRpc(val2);
		}
	}

	private static bool IsHeldBySomeone(GrabbableObject obj)
	{
		PlayerControllerB[] array = Object.FindObjectsOfType<PlayerControllerB>();
		PlayerControllerB[] array2 = array;
		foreach (PlayerControllerB val in array2)
		{
			if ((Object)(object)val != (Object)null && val.ItemSlots.Contains(obj))
			{
				return true;
			}
		}
		return false;
	}
}
[HarmonyPatch(typeof(HUDManager))]
internal class HUDManagerPatch
{
	[HarmonyPatch("AddTextToChatOnServer")]
	[HarmonyPrefix]
	public static bool AddTextToChatOnServer_Prefix(HUDManager __instance, ref string chatMessage)
	{
		Match match = Regex.Match(chatMessage, "^/sell\\s+(-?\\d+)$");
		if (match != null && match.Success)
		{
			AutoSell.StartAutoSell(int.Parse(match.Groups[1].Value));
			return false;
		}
		return true;
	}
}
[BepInPlugin("Xelan.ChatToSell", "ChatToSell", "1.0.0")]
public class ChatToSell : BaseUnityPlugin
{
	private const string modGUID = "Xelan.ChatToSell";

	private const string modName = "ChatToSell";

	private const string modVersion = "1.0.0";

	private readonly Harmony harmony = new Harmony("Xelan.ChatToSell");

	private static ChatToSell Instance;

	public static ManualLogSource mls;

	public static ConfigEntry<string> ItemsNotToSell;

	public static ConfigEntry<string> ItemsToSellFirst;

	public static ConfigEntry<bool> PrioritizeItemsOnCruiser;

	private void Awake()
	{
		if ((Object)(object)Instance == (Object)null)
		{
			Instance = this;
		}
		mls = Logger.CreateLogSource("Xelan.ChatToSell");
		mls.LogInfo((object)"╔═══════════════════════════════════════════════╗");
		mls.LogInfo((object)"║              ChatToSell is UP !              ║");
		mls.LogInfo((object)"╚═══════════════════════════════════════════════╝");
		LoadConfig();
		harmony.PatchAll();
	}

	private void LoadConfig()
	{
		ItemsNotToSell = ((BaseUnityPlugin)this).Config.Bind<string>("ChatToSell", "ItemsNotToSell", "Zed Dog, Kitchen knife, Shotgun, Gift", "Items you don't want to sell with /sell <value> \nUsage : 'item, item, item, ...'");
		ItemsToSellFirst = ((BaseUnityPlugin)this).Config.Bind<string>("ChatToSell", "ItemsToSellFirst", "Painting, Fancy lamp, Bottles, Cash register, Garbage lid, V-type engine, Large axle, Apparatus, Toilet paper, Soccer ball, Whoopie cushion, Hive, Clock, Easter egg, Chemical jug, Control pad", "Items you want to sell in priority with /sell <value> \nUsage : 'item, item, item, ...'");
		PrioritizeItemsOnCruiser = ((BaseUnityPlugin)this).Config.Bind<bool>("ChatToSell", "PrioritizeItemsOnCruiser", false, "Bypass 'ItemsToSellFirst' and sell the items on the Cruiser first");
	}

	public static void DisplayTip(string title, string msg, bool isWarning = false)
	{
		HUDManager.Instance.DisplayTip(title, msg, isWarning, false, "LC_Tip1");
	}
}