Decompiled source of ForgottenDelivery v1.0.1

ForgottenDelivery.dll

Decompiled 2 days ago
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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Photon.Pun;
using REPOLib.Modules;
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("ForgottenDelivery")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ForgottenDelivery")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("fe9e13a2-1750-439b-80c1-bf36503d03a9")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ForgottenDelivery
{
	internal class ConfigManager
	{
		public static ConfigEntry<int> maxSpawnsPerLocation;

		public static ConfigEntry<int> spawnChance;

		public static ConfigEntry<int> chanceForBigPackage;

		public static ConfigEntry<string> packageDrops;

		public static ConfigEntry<string> bigPackageDrops;

		public static ConfigEntry<string> packageBlacklist;

		public static ConfigEntry<string> bigPackageBlacklist;

		private static readonly string[] itemTypes = new string[12]
		{
			"drone", "orb", "cart", "upgrade", "crystal", "grenade", "melee", "healthpack", "gun", "tracker",
			"mine", "pocketcart"
		};

		public static void Init()
		{
			maxSpawnsPerLocation = ((BaseUnityPlugin)ForgottenDeliveryMod.instance).Config.Bind<int>("Spawn Settings", "maxSpawnsPerLocation", 1, "The maximum number of packages to spawn per location.");
			spawnChance = ((BaseUnityPlugin)ForgottenDeliveryMod.instance).Config.Bind<int>("Spawn Settings", "spawnChance", 50, "How rare it is for a package to spawn. Can be set to a minimum of 0 (never) and a maximum of 100 (guaranteed).");
			chanceForBigPackage = ((BaseUnityPlugin)ForgottenDeliveryMod.instance).Config.Bind<int>("Spawn Settings", "chanceForBigPackage", 25, "How rare it is for a spawned package to be big. Can be set to a minimum of 0 (never) and a maximum of 100 (guaranteed).");
			packageDrops = ((BaseUnityPlugin)ForgottenDeliveryMod.instance).Config.Bind<string>("Drop Settings", "packageDrops", "drone;orb;upgrade;crystal;grenade;healthpack;mine", "The item types that can be dropped by a regular package. This setting is case insenstive and you can chain item types with semicolons. All types are listed in the README.");
			bigPackageDrops = ((BaseUnityPlugin)ForgottenDeliveryMod.instance).Config.Bind<string>("Drop Settings", "bigPackageDrops", "cart;melee;gun;tracker;pocketcart", "The item types that can be dropped by a big package. This setting is case insenstive and you can chain item types with semicolons. All types are listed in the README.");
			packageBlacklist = ((BaseUnityPlugin)ForgottenDeliveryMod.instance).Config.Bind<string>("Drop Settings", "packageBlacklist", "", "The items that regular packages should be banned from dropping. This setting is case insenstive and you can chain item names with semicolons. Item names should be exactly as they appear in game.");
			bigPackageBlacklist = ((BaseUnityPlugin)ForgottenDeliveryMod.instance).Config.Bind<string>("Drop Settings", "bigPackageBlacklist", "", "The items that big packages should be banned from dropping. This setting is case insenstive and you can chain item names with semicolons. Item names should be exactly as they appear in game.");
		}

		public static bool ValidatePackageDrops(bool big)
		{
			string[] array = (big ? bigPackageDrops.Value.ToLower().Split(new char[1] { ';' }) : packageDrops.Value.ToLower().Split(new char[1] { ';' }));
			for (int i = 0; i < array.Length; i++)
			{
				if (!itemTypes.Contains(array[i]))
				{
					return false;
				}
			}
			return true;
		}

		public static itemType[] GetPackageDrops(bool big)
		{
			string text = bigPackageDrops.Value;
			if (!ValidatePackageDrops(big: true))
			{
				text = (string)((ConfigEntryBase)bigPackageDrops).DefaultValue;
			}
			string text2 = packageDrops.Value;
			if (!ValidatePackageDrops(big: false))
			{
				text2 = (string)((ConfigEntryBase)packageDrops).DefaultValue;
			}
			string[] array = (big ? text.ToLower().Split(new char[1] { ';' }) : text2.ToLower().Split(new char[1] { ';' }));
			itemType[] array2 = (itemType[])(object)new itemType[array.Length];
			for (int i = 0; i < array.Length; i++)
			{
				switch (array[i])
				{
				case "drone":
					array2[i] = (itemType)0;
					break;
				case "orb":
					array2[i] = (itemType)1;
					break;
				case "cart":
					array2[i] = (itemType)2;
					break;
				case "upgrade":
					array2[i] = (itemType)3;
					break;
				case "crystal":
					array2[i] = (itemType)5;
					break;
				case "grenade":
					array2[i] = (itemType)6;
					break;
				case "melee":
					array2[i] = (itemType)7;
					break;
				case "healthpack":
					array2[i] = (itemType)8;
					break;
				case "gun":
					array2[i] = (itemType)9;
					break;
				case "tracker":
					array2[i] = (itemType)10;
					break;
				case "mine":
					array2[i] = (itemType)11;
					break;
				default:
					array2[i] = (itemType)12;
					break;
				}
			}
			return array2;
		}

		public static string[] GetBlacklist(bool big)
		{
			return big ? bigPackageBlacklist.Value.ToLower().Split(new char[1] { ';' }) : packageBlacklist.Value.ToLower().Split(new char[1] { ';' });
		}
	}
	[BepInPlugin("eXish.ForgottenDelivery", "ForgottenDelivery", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class ForgottenDeliveryMod : BaseUnityPlugin
	{
		private const string mGUID = "eXish.ForgottenDelivery";

		private const string mName = "ForgottenDelivery";

		private const string mVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("eXish.ForgottenDelivery");

		internal static ForgottenDeliveryMod instance;

		internal static ManualLogSource log;

		internal static GameObject packagePrefab;

		internal static GameObject packageBigPrefab;

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			log = ((BaseUnityPlugin)this).Logger;
			ConfigManager.Init();
			if (ConfigManager.maxSpawnsPerLocation.Value < 0)
			{
				log.LogWarning((object)$"The value \"{ConfigManager.maxSpawnsPerLocation.Value}\" is not valid for setting \"maxSpawnsPerLocation\"! The default will be used instead.");
			}
			if (ConfigManager.spawnChance.Value < 0 || ConfigManager.spawnChance.Value > 100)
			{
				log.LogWarning((object)$"The value \"{ConfigManager.spawnChance.Value}\" is not valid for setting \"spawnChance\"! The default will be used instead.");
			}
			if (ConfigManager.chanceForBigPackage.Value < 0 || ConfigManager.chanceForBigPackage.Value > 100)
			{
				log.LogWarning((object)$"The value \"{ConfigManager.chanceForBigPackage.Value}\" is not valid for setting \"chanceForBigPackage\"! The default will be used instead.");
			}
			if (!ConfigManager.ValidatePackageDrops(big: false))
			{
				log.LogWarning((object)("The value \"" + ConfigManager.packageDrops.Value + "\" is not valid for setting \"packageDrops\"! The default will be used instead."));
			}
			if (!ConfigManager.ValidatePackageDrops(big: true))
			{
				log.LogWarning((object)("The value \"" + ConfigManager.bigPackageDrops.Value + "\" is not valid for setting \"bigPackageDrops\"! The default will be used instead."));
			}
			string text = ((BaseUnityPlugin)this).Info.Location.TrimEnd("ForgottenDelivery.dll".ToCharArray());
			AssetBundle val = AssetBundle.LoadFromFile(text + "forgottendelivery");
			if ((Object)(object)val != (Object)null)
			{
				packagePrefab = val.LoadAsset<GameObject>("Assets/ForgottenDelivery/eXDeliveryBox.prefab");
				packageBigPrefab = val.LoadAsset<GameObject>("Assets/ForgottenDelivery/eXDeliveryBoxBig.prefab");
				NetworkPrefabs.RegisterNetworkPrefab(packagePrefab);
				NetworkPrefabs.RegisterNetworkPrefab(packageBigPrefab);
			}
			else
			{
				log.LogError((object)"Unable to locate the asset file! Delivery packages will not spawn.");
			}
			harmony.PatchAll();
			log.LogInfo((object)"ForgottenDelivery-1.0.0 loaded!");
		}
	}
}
namespace ForgottenDelivery.Patches
{
	[HarmonyPatch(typeof(ValuableDirector))]
	internal class ValuableDirectorPatch
	{
		[HarmonyPatch("VolumesAndSwitchSetup")]
		[HarmonyPostfix]
		private static void VolumesAndSwitchSetupPatch()
		{
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_027d: Unknown result type (might be due to invalid IL or missing references)
			//IL_028f: Unknown result type (might be due to invalid IL or missing references)
			if (!SemiFunc.RunIsLevel())
			{
				return;
			}
			List<ValuableVolume> list = (from x in Object.FindObjectsOfType<ValuableVolume>(false)
				where (int)x.VolumeType == 2
				select x).ToList();
			List<ValuableVolume> list2 = (from x in Object.FindObjectsOfType<ValuableVolume>(false)
				where (int)x.VolumeType == 4
				select x).ToList();
			ForgottenDeliveryMod.log.LogInfo((object)$"Found {list.Count} suitable volumes for spawning regular packages.");
			ForgottenDeliveryMod.log.LogInfo((object)$"Found {list2.Count} suitable volumes for spawning big packages.");
			int num = ConfigManager.spawnChance.Value;
			if (num < 0 || num > 100)
			{
				num = (int)((ConfigEntryBase)ConfigManager.spawnChance).DefaultValue;
			}
			int num2 = ConfigManager.chanceForBigPackage.Value;
			if (num2 < 0 || num2 > 100)
			{
				num2 = (int)((ConfigEntryBase)ConfigManager.chanceForBigPackage).DefaultValue;
			}
			int num3 = ConfigManager.maxSpawnsPerLocation.Value;
			if (num3 < 0)
			{
				num3 = (int)((ConfigEntryBase)ConfigManager.maxSpawnsPerLocation).DefaultValue;
			}
			int num4 = 0;
			int num5 = 0;
			for (int i = 0; i < num3; i++)
			{
				if (num <= Random.Range(0, 100))
				{
					continue;
				}
				if (num2 > Random.Range(0, 100) && list2.Count > 0)
				{
					int index = Random.Range(0, list2.Count);
					ValuablePropSwitch componentInParent = ((Component)list2[index]).GetComponentInParent<ValuablePropSwitch>();
					if ((Object)(object)componentInParent != (Object)null)
					{
						componentInParent.PropParent.SetActive(false);
						componentInParent.ValuableParent.SetActive(true);
					}
					NetworkPrefabs.SpawnNetworkPrefab("eXDeliveryBoxBig", ((Component)list2[index]).transform.position, ((Component)list2[index]).transform.rotation, (byte)0, (object[])null);
					list2.Remove(list2[index]);
					num5++;
				}
				else if (list.Count > 0)
				{
					int index2 = Random.Range(0, list.Count);
					ValuablePropSwitch componentInParent2 = ((Component)list[index2]).GetComponentInParent<ValuablePropSwitch>();
					if ((Object)(object)componentInParent2 != (Object)null)
					{
						componentInParent2.PropParent.SetActive(false);
						componentInParent2.ValuableParent.SetActive(true);
					}
					NetworkPrefabs.SpawnNetworkPrefab("eXDeliveryBox", ((Component)list[index2]).transform.position, ((Component)list[index2]).transform.rotation, (byte)0, (object[])null);
					list.Remove(list[index2]);
					num4++;
				}
			}
			ForgottenDeliveryMod.log.LogInfo((object)$"A total of {num4} regular packages were spawned.");
			ForgottenDeliveryMod.log.LogInfo((object)$"A total of {num5} big packages were spawned.");
		}
	}
	[HarmonyPatch(typeof(PhysGrabObjectImpactDetector))]
	internal class ImpactDetectorPatch
	{
		[HarmonyPatch("DestroyObject")]
		[HarmonyPostfix]
		private static void DestroyObjectPatch(PhysGrabObjectImpactDetector __instance)
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			if (!(((Object)((Component)__instance).gameObject).name == "eXDeliveryBoxBig(Clone)") && !(((Object)((Component)__instance).gameObject).name == "eXDeliveryBox(Clone)"))
			{
				return;
			}
			List<Item> list = new List<Item>();
			bool flag = ((((Object)((Component)__instance).gameObject).name == "eXDeliveryBoxBig(Clone)") ? true : false);
			itemType[] packageDrops = ConfigManager.GetPackageDrops(flag);
			string[] blacklist = ConfigManager.GetBlacklist(flag);
			foreach (Item value in StatsManager.instance.itemDictionary.Values)
			{
				if (packageDrops.Contains(value.itemType) && !blacklist.Contains(value.itemName.ToLower()))
				{
					list.Add(value);
				}
			}
			if (list.Count > 0)
			{
				int index = Random.Range(0, list.Count);
				if (flag)
				{
					ForgottenDeliveryMod.log.LogInfo((object)("A big package was destroyed, spawning a " + ((Object)list[index]).name + "."));
				}
				else
				{
					ForgottenDeliveryMod.log.LogInfo((object)("A regular package was destroyed, spawning a " + ((Object)list[index]).name + "."));
				}
				if (SemiFunc.IsMultiplayer())
				{
					PhotonNetwork.InstantiateRoomObject("Items/" + ((Object)list[index].prefab).name, ((Component)__instance).transform.position, ((Component)__instance).transform.rotation, (byte)0, (object[])null);
				}
				else
				{
					Object.Instantiate<GameObject>(list[index].prefab, ((Component)__instance).transform.position, ((Component)__instance).transform.rotation);
				}
			}
			else if (flag)
			{
				ForgottenDeliveryMod.log.LogInfo((object)"A big package was destroyed, but the current drop settings do not allow for an item to be dropped by this package.");
			}
			else
			{
				ForgottenDeliveryMod.log.LogInfo((object)"A regular package was destroyed, but the current drop settings do not allow for an item to be dropped by this package.");
			}
		}
	}
}