Decompiled source of ScrapBinMod v0.0.1

ScrapBinMod.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Extras;
using LethalLib.Modules;
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("ScrapBinMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+2b951f46d2b78af130958d1101674930880c0867")]
[assembly: AssemblyProduct("ScrapBinMod")]
[assembly: AssemblyTitle("ScrapBinMod")]
[assembly: AssemblyVersion("1.0.0.0")]
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;
		}
	}
}
namespace ScrapBin
{
	public static class ScrapBinConfig
	{
		public static void SetupConfig(ConfigFile config)
		{
		}
	}
	public class MyPluginInfo
	{
		public const string MOD_GUID = "Wrex.ScrapBinMod";

		public const string MOD_NAME = "Scrap Bin Mod";

		public const string MOD_VERSION = "0.0.1";
	}
	[HarmonyPatch(typeof(Terminal))]
	public class TerminalPatches
	{
		[HarmonyPatch("ParsePlayerSentence")]
		[HarmonyPrefix]
		[HarmonyPriority(800)]
		private static bool ParsePlayerSentencePatch(ref Terminal __instance, ref TerminalNode __result)
		{
			string[] array = __instance.screenText.text.Substring(__instance.screenText.text.Length - __instance.textAdded).Split(' ', StringSplitOptions.RemoveEmptyEntries);
			if (array.Length != 0 && string.Equals(array[0], "smelt"))
			{
				TerminalNode val = ScriptableObject.CreateInstance<TerminalNode>();
				val.clearPreviousText = true;
				val.maxCharactersToType = 50;
				if (array.Length == 1)
				{
					val.displayText = "Type Smelt <Amount> to smelt scrap from the Scrap Bin (by Wrex Industries) into a solid Gold bar worth <Amount>.\n\nCurrent value of scrap in bin is " + ScrapBinScript.instance.GetTotalPrice() + "\n\nMinimum Smelting value currently is " + ScrapBinScript.instance.GetMinSmeltPrice() + "\n\n";
				}
				else
				{
					val.displayText = ScrapBinScript.instance.SmeltGoldCommand(int.Parse(array[1]));
				}
				__result = val;
				return false;
			}
			return true;
		}
	}
	[BepInPlugin("Wrex.ScrapBinMod", "Scrap Bin Mod", "0.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private readonly Harmony _harmony = new Harmony("Wrex.ScrapBinMod");

		private const string bundleName = "ScrapBinModBundle";

		private const string scrapBinUnlockableName = "Assets/DansMods/ScrapBinMod/WrexScrapBinUnlockable.asset";

		private static Plugin instance;

		private static AssetBundle bundle;

		internal static ManualLogSource Logger { get; private set; }

		private void Awake()
		{
			instance = this;
			try
			{
				ConfigureLogging();
				ScrapBinConfig.SetupConfig(((BaseUnityPlugin)this).Config);
				_harmony.PatchAll(typeof(TerminalPatches));
				LoadAssetBundle();
			}
			catch (Exception ex)
			{
				Logger.LogError((object)ex.Message);
				Logger.LogError((object)ex.StackTrace);
			}
		}

		private void ConfigureLogging()
		{
			Logger = Logger.CreateLogSource("Wrex.ScrapBinMod");
			Logger.LogInfo((object)"Loading Scrap Bin Mod");
		}

		private void LoadAssetBundle()
		{
			Assembly assembly = typeof(Plugin).Assembly;
			string text = Path.Combine(Path.GetDirectoryName(assembly.Location), "ScrapBinModBundle");
			bundle = AssetBundle.LoadFromFile(text);
			if ((Object)(object)bundle == (Object)null)
			{
				Logger.LogError((object)"Failed to load asset bundle");
				return;
			}
			UnlockableItemDef val = bundle.LoadAsset<UnlockableItemDef>("Assets/DansMods/ScrapBinMod/WrexScrapBinUnlockable.asset");
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogError((object)"Failed to load custom assets");
				return;
			}
			NetworkPrefabs.RegisterNetworkPrefab(val.unlockable.prefabObject);
			Unlockables.RegisterUnlockable(val, 5, (StoreType)1);
			Logger.LogInfo((object)"Custom assets loaded");
		}
	}
	public class ScrapBinScript : NetworkBehaviour
	{
		internal static ScrapBinScript instance;

		public Transform _itemDropNode;

		private Transform _hangarShipContainer;

		private Stack<GrabbableObject> _scrapPile;

		private PhysicsProp goldBar = null;

		private void Awake()
		{
			instance = this;
			_hangarShipContainer = GameObject.Find("Environment/HangarShip").transform;
			((Component)this).transform.SetParent(_hangarShipContainer);
			_scrapPile = new Stack<GrabbableObject>();
			PhysicsProp[] source = Resources.FindObjectsOfTypeAll<PhysicsProp>();
			goldBar = source.First((PhysicsProp prop) => ((Object)prop).name.Equals("GoldBar"));
			if ((Object)(object)goldBar != (Object)null)
			{
				Plugin.Logger.LogMessage((object)"Gold Bar Asset Found");
			}
		}

		private void OnDestroy()
		{
		}

		public void DepositScrap(PlayerControllerB playerWhoTriggered)
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: 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_00ec: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_itemDropNode == (Object)null)
			{
				Debug.Log((object)"Item Drop Node is Null");
				return;
			}
			if (!playerWhoTriggered.isHoldingObject || playerWhoTriggered.isGrabbingObjectAnimation || !((Object)playerWhoTriggered.currentlyHeldObjectServer != (Object)null))
			{
				Debug.Log((object)"No Scrap Held");
				return;
			}
			if (!playerWhoTriggered.isInHangarShipRoom)
			{
				Debug.Log((object)"Player not in ship");
				return;
			}
			GrabbableObject currentlyHeldObjectServer = playerWhoTriggered.currentlyHeldObjectServer;
			if (currentlyHeldObjectServer.itemProperties.isScrap && currentlyHeldObjectServer.scrapValue > 0)
			{
				Debug.Log((object)("Placing " + ((Object)currentlyHeldObjectServer).name + " in Scrap Bin"));
				_scrapPile.Push(currentlyHeldObjectServer);
				currentlyHeldObjectServer.OnBroughtToShip();
				playerWhoTriggered.DiscardHeldObject(false, (NetworkObject)null, default(Vector3), false);
				((Component)currentlyHeldObjectServer).transform.SetParent(_hangarShipContainer, true);
				currentlyHeldObjectServer.EnablePhysics(false);
				currentlyHeldObjectServer.EnableItemMeshes(false);
			}
			else
			{
				Debug.Log((object)(((Object)currentlyHeldObjectServer).name + " isn't scrap or has no value"));
			}
		}

		public void WithdrawScrap(PlayerControllerB playerWhoTriggered)
		{
			//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_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//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)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_itemDropNode == (Object)null)
			{
				Debug.Log((object)"Item Drop Node is Null");
				return;
			}
			if (_scrapPile.Count == 0)
			{
				Debug.Log((object)"No Scrap In Bin");
				return;
			}
			Vector3 position = _itemDropNode.position;
			Debug.Log((object)("Scrap Withdrawn at " + ((object)(Vector3)(ref position)).ToString()));
			GrabbableObject val = _scrapPile.Pop();
			val.fallTime = 0f;
			val.startFallingPosition = ((Component)val).transform.position + Vector3.up * 0.15f;
			val.EnableItemMeshes(true);
			val.EnablePhysics(true);
		}

		private void WithdrawAllScrap(PlayerControllerB? playerWhoTriggered)
		{
			if ((Object)(object)_itemDropNode == (Object)null)
			{
				Debug.Log((object)"Item Drop Node is Null");
				return;
			}
			Debug.Log((object)"Withdrawing All Scrap");
			while (_scrapPile != null && _scrapPile.Count > 0)
			{
				WithdrawScrap(null);
			}
		}

		public string SmeltGoldCommand(int priceTarget)
		{
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			if (GetTotalPrice() < priceTarget)
			{
				return "Cannot afford " + priceTarget + ", there is only " + GetTotalPrice() + " in the Scrap Bin (by Wrex Industries)\n\n";
			}
			if (priceTarget < _scrapPile.Peek().scrapValue)
			{
				return "Price must be as much as or more than the first scrap in bin (" + _scrapPile.Peek().scrapValue + ")\n\n";
			}
			Debug.Log((object)("Withdrawing Gold worth " + priceTarget));
			int num = 0;
			Stack<GrabbableObject> stack = new Stack<GrabbableObject>();
			do
			{
				GrabbableObject val = _scrapPile.Peek();
				num += val.scrapValue;
				stack.Push(val);
				WithdrawScrap(null);
			}
			while (_scrapPile.Count > 0 && num < priceTarget);
			Plugin.Logger.LogMessage((object)"Item Drop Node:");
			ManualLogSource logger = Plugin.Logger;
			Vector3 val2 = _itemDropNode.position;
			logger.LogMessage((object)("Position: " + ((object)(Vector3)(ref val2)).ToString()));
			ManualLogSource logger2 = Plugin.Logger;
			val2 = _itemDropNode.localPosition;
			logger2.LogMessage((object)("Local Position: " + ((object)(Vector3)(ref val2)).ToString()));
			if ((Object)(object)goldBar != (Object)null)
			{
				PhysicsProp val3 = Object.Instantiate<PhysicsProp>(goldBar, _itemDropNode.position, Quaternion.Euler(-90f, 0f, 0f), _hangarShipContainer);
				((Component)val3).GetComponent<NetworkObject>().Spawn(false);
				((GrabbableObject)val3).SetScrapValue(num);
			}
			Debug.Log((object)(stack.Count + " pieces of scrap smelted for " + num));
			return stack.Count + " pieces of scrap smelted for " + num + "\n\n";
		}

		public int GetTotalPrice()
		{
			return _scrapPile.Sum((GrabbableObject scrap) => scrap.scrapValue);
		}

		public int GetMinSmeltPrice()
		{
			return (_scrapPile.Count != 0) ? _scrapPile.Peek().scrapValue : 0;
		}
	}
}