Decompiled source of artifactofpreparation v1.1.0

ArtifactofPreparation.dll

Decompiled 2 weeks 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HG;
using Microsoft.CodeAnalysis;
using On.RoR2;
using R2API;
using RoR2;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ArtifactofPreparation")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ArtifactofPreparation")]
[assembly: AssemblyTitle("ArtifactofPreparation")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 ArtifactOfPreparation
{
	[BepInPlugin("com.scott_jc.artifactofpreparation", "Artifact of Preparation", "1.0.9")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class ArtifactOfPreparationPlugin : BaseUnityPlugin
	{
		private enum PoolType
		{
			White,
			Green,
			Red,
			Yellow,
			Lunar,
			Void
		}

		private enum ConfigBools
		{
			configRefresh,
			configexcludescrap,
			configexcludedebugequip,
			configexcludeuselessyellow,
			configexcludepearl,
			configexcludesentrykeys,
			configprepgiveequip,
			configprepgiveequiplunar,
			configNoDuplicates,
			configANoDuplicates
		}

		private enum ConfigInts
		{
			configprepgivered,
			configprepgivegreen,
			configprepgivewhite,
			configprepgivepurple,
			configprepgiveblue,
			configprepgiveyellow
		}

		public static ArtifactDef PreparationArtifact;

		private static readonly Dictionary<PoolType, List<ItemIndex>> itemPools = new Dictionary<PoolType, List<ItemIndex>>();

		private static readonly Dictionary<ConfigBools, ConfigEntry<bool>> configsBools = new Dictionary<ConfigBools, ConfigEntry<bool>>();

		private static readonly Dictionary<ConfigInts, ConfigEntry<int>> configsInts = new Dictionary<ConfigInts, ConfigEntry<int>>();

		private static List<EquipmentIndex> equipmentPool;

		private static ConfigEntry<string> configExcludedItems;

		private static ConfigEntry<string> configGuaranteedItems;

		private static string[] excludedItems;

		private static string[] guaranteedItems;

		private static bool poolsBuilt = false;

		private static bool ranonce = false;

		public void Awake()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			RegisterArtifact();
			LoadConfig();
			Run.Start += new hook_Start(Run_Start);
		}

		private void RegisterArtifact()
		{
			PreparationArtifact = ScriptableObject.CreateInstance<ArtifactDef>();
			PreparationArtifact.cachedName = "ARTIFACT_PREPARATION";
			PreparationArtifact.nameToken = "ARTIFACT_PREPARATION_NAME";
			PreparationArtifact.descriptionToken = "ARTIFACT_PREPARATION_DESC";
			Sprite smallIconSelectedSprite = LoadSprite(Path.Combine(Paths.BepInExRootPath, "plugins", "scottjc-artifactofpreparation", "icon.png"));
			Sprite smallIconDeselectedSprite = LoadSprite(Path.Combine(Paths.BepInExRootPath, "plugins", "scottjc-artifactofpreparation", "starterdeIcon.png"));
			PreparationArtifact.smallIconSelectedSprite = smallIconSelectedSprite;
			PreparationArtifact.smallIconDeselectedSprite = smallIconDeselectedSprite;
			LanguageAPI.Add("ARTIFACT_PREPARATION_NAME", "Artifact of Preparation");
			LanguageAPI.Add("ARTIFACT_PREPARATION_DESC", "At the start of the run, each player receives a random set of items.");
			ContentAddition.AddArtifactDef(PreparationArtifact);
		}

		private Sprite LoadSprite(string path)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			if (File.Exists(path))
			{
				byte[] array = File.ReadAllBytes(path);
				Texture2D val = new Texture2D(2, 2);
				ImageConversion.LoadImage(val, array);
				return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
			}
			return null;
		}

		private bool ArtifactEnabled()
		{
			if ((Object)(object)PreparationArtifact != (Object)null && (Object)(object)RunArtifactManager.instance != (Object)null)
			{
				return RunArtifactManager.instance.IsArtifactEnabled(PreparationArtifact);
			}
			return false;
		}

		private void Run_Start(orig_Start orig, Run self)
		{
			orig.Invoke(self);
			if (!NetworkServer.active || !ArtifactEnabled())
			{
				return;
			}
			if (!poolsBuilt)
			{
				itemPools[PoolType.White] = BuildItemList((ItemTier)0);
				itemPools[PoolType.Green] = BuildItemList((ItemTier)1);
				itemPools[PoolType.Red] = BuildItemList((ItemTier)2);
				itemPools[PoolType.Yellow] = BuildItemList((ItemTier)4);
				itemPools[PoolType.Lunar] = BuildItemList((ItemTier)3);
				itemPools[PoolType.Void] = BuildVoidItemList();
				equipmentPool = BuildEquipmentList();
				poolsBuilt = true;
			}
			foreach (CharacterMaster readOnlyInstances in CharacterMaster.readOnlyInstancesList)
			{
				if ((Object)(object)((readOnlyInstances != null) ? readOnlyInstances.inventory : null) != (Object)null)
				{
					GiveRandomItems(readOnlyInstances.inventory, itemPools[PoolType.White], configsInts[ConfigInts.configprepgivewhite].Value);
					GiveRandomItems(readOnlyInstances.inventory, itemPools[PoolType.Green], configsInts[ConfigInts.configprepgivegreen].Value);
					GiveRandomItems(readOnlyInstances.inventory, itemPools[PoolType.Red], configsInts[ConfigInts.configprepgivered].Value);
					GiveRandomItems(readOnlyInstances.inventory, itemPools[PoolType.Yellow], configsInts[ConfigInts.configprepgiveyellow].Value);
					GiveRandomItems(readOnlyInstances.inventory, itemPools[PoolType.Lunar], configsInts[ConfigInts.configprepgiveblue].Value);
					GiveRandomItems(readOnlyInstances.inventory, itemPools[PoolType.Void], configsInts[ConfigInts.configprepgivepurple].Value);
					if (configsBools[ConfigBools.configprepgiveequip].Value)
					{
						GiveRandomEquipment(readOnlyInstances.inventory, equipmentPool);
					}
					GiveGuaranteedItems(readOnlyInstances.inventory);
					OrganizeInventoryByRarity(readOnlyInstances.inventory);
				}
			}
		}

		private void LoadConfig()
		{
			configsBools[ConfigBools.configexcludescrap] = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "excludescrap", true, "Whether or not I Exclude scrap or not");
			configsBools[ConfigBools.configexcludedebugequip] = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "excludedebugequip", true, "Whether or not I Exclude debug equips or not");
			configsBools[ConfigBools.configexcludeuselessyellow] = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "excludeuselessyellow", true, "Exclude boss items that do nothing until the end of a loop.");
			configsBools[ConfigBools.configexcludepearl] = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "excludepearl", true, "Excludes pearls");
			configsBools[ConfigBools.configexcludesentrykeys] = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "excludesentrykeys", true, "Exclude sentry keys (they give movement speed so you might like em)");
			configsBools[ConfigBools.configprepgiveequip] = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "prepgiveequip", true, "Whether to give an equipment or not");
			configsBools[ConfigBools.configprepgiveequiplunar] = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "prepgiveequiplunar", false, "Whether or not to include lunar equipments in the list.");
			configsBools[ConfigBools.configNoDuplicates] = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "noDuplicates", false, "Prevent giving duplicate items (won't effect guaranteedItems, and will allow duplicates if pool runs out)");
			configsBools[ConfigBools.configANoDuplicates] = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "AnoDuplicates", false, "Absolutely no dupes, if this is set to true, you can't get more than 1 of an item (guaranteedItems exempt), this supercedes the above option");
			configsInts[ConfigInts.configprepgivered] = ((BaseUnityPlugin)this).Config.Bind<int>("Main", "prepgivered", 1, "How many legendary (Red) items to give");
			configsInts[ConfigInts.configprepgivegreen] = ((BaseUnityPlugin)this).Config.Bind<int>("Main", "prepgivegreen", 3, "How many Uncommon (Green) Items to give");
			configsInts[ConfigInts.configprepgivewhite] = ((BaseUnityPlugin)this).Config.Bind<int>("Main", "prepgivewhite", 5, "How many Common (White) items to give");
			configsInts[ConfigInts.configprepgivepurple] = ((BaseUnityPlugin)this).Config.Bind<int>("Main", "prepgivepurple", 0, "How many Void (Purple) Items to give");
			configsInts[ConfigInts.configprepgiveblue] = ((BaseUnityPlugin)this).Config.Bind<int>("Main", "prepgiveblue", 0, "How many Lunar (Blue) Items to give");
			configsInts[ConfigInts.configprepgiveyellow] = ((BaseUnityPlugin)this).Config.Bind<int>("Main", "prepgiveyellow", 0, "How many Boss (Yellow) items to give");
			configExcludedItems = ((BaseUnityPlugin)this).Config.Bind<string>("Main", "ExcludedItems", "", "Items or Equipment to exclude (Seperated by Comma: Item1, Item2, etc), works for any tier of item. You must use the internal names of items i.e. PlasmaShrimp is MissileVoid, consult the RoR2 Wiki for more info.");
			excludedItems = GetItemsList(configExcludedItems.Value).ToArray();
			configGuaranteedItems = ((BaseUnityPlugin)this).Config.Bind<string>("Main", "guaranteedItems", "", "Item names to always start with, similar to Artifact of Rebirth's effect, separated by commas (again, you must use internal names, consult the RoR2 wiki) If you give an equipment here, it will override the random equipment setting, you can only give 1 equipment.");
			guaranteedItems = GetItemsList(configGuaranteedItems.Value).ToArray();
			ranonce = true;
		}

		private static List<string> GetItemsList(string InputStr)
		{
			List<string> list = new List<string>();
			string text = "";
			if (string.IsNullOrWhiteSpace(InputStr))
			{
				return list;
			}
			string[] array = InputStr.Split(',');
			for (int i = 0; i < array.Length; i++)
			{
				text = array[i];
				if (text.StartsWith("ItemIndex.", StringComparison.OrdinalIgnoreCase))
				{
					text = text.Substring(10);
				}
				if (!string.IsNullOrEmpty(text.Trim()))
				{
					list.Add(text.Trim());
				}
			}
			return list;
		}

		public unsafe static List<ItemIndex> BuildItemList(ItemTier tier)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			List<ItemIndex> list = new List<ItemIndex>();
			AllItemsEnumerator enumerator = ItemCatalog.allItems.GetEnumerator();
			try
			{
				while (((AllItemsEnumerator)(ref enumerator)).MoveNext())
				{
					ItemIndex current = ((AllItemsEnumerator)(ref enumerator)).Current;
					ItemDef itemDef = ItemCatalog.GetItemDef(current);
					if (Object.op_Implicit((Object)(object)itemDef) && itemDef.tier == tier && (!configsBools[ConfigBools.configexcludeuselessyellow].Value || (!(((Object)itemDef).name == "MasterBattery") && !(((Object)itemDef).name == "MasterCore"))) && (!configsBools[ConfigBools.configexcludepearl].Value || (!(((Object)itemDef).name == "ShinyPearl") && !(((Object)itemDef).name == "Pearl"))) && (!configsBools[ConfigBools.configexcludescrap].Value || (!(((Object)itemDef).name == "ScrapWhite") && !(((Object)itemDef).name == "ScrapGreen") && !(((Object)itemDef).name == "ScrapRed"))) && (!configsBools[ConfigBools.configexcludesentrykeys].Value || !(((Object)itemDef).name == "PowerPyramid")) && !excludedItems.Contains(((Object)itemDef).name))
					{
						list.Add(current);
					}
				}
				return list;
			}
			finally
			{
				((IDisposable)(*(AllItemsEnumerator*)(&enumerator))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		public unsafe static List<ItemIndex> BuildVoidItemList()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Invalid comparison between Unknown and I4
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			List<ItemIndex> list = new List<ItemIndex>();
			AllItemsEnumerator enumerator = ItemCatalog.allItems.GetEnumerator();
			try
			{
				while (((AllItemsEnumerator)(ref enumerator)).MoveNext())
				{
					ItemIndex current = ((AllItemsEnumerator)(ref enumerator)).Current;
					ItemDef itemDef = ItemCatalog.GetItemDef(current);
					if (!((Object)(object)itemDef == (Object)null))
					{
						ItemTier tier = itemDef.tier;
						if (tier - 6 <= 3)
						{
							list.Add(current);
						}
					}
				}
				return list;
			}
			finally
			{
				((IDisposable)(*(AllItemsEnumerator*)(&enumerator))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		public unsafe static List<EquipmentIndex> BuildEquipmentList()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			List<EquipmentIndex> list = new List<EquipmentIndex>();
			AllEquipmentEnumerator enumerator = EquipmentCatalog.allEquipment.GetEnumerator();
			try
			{
				while (((AllEquipmentEnumerator)(ref enumerator)).MoveNext())
				{
					EquipmentIndex current = ((AllEquipmentEnumerator)(ref enumerator)).Current;
					EquipmentDef equipmentDef = EquipmentCatalog.GetEquipmentDef(current);
					if (!((Object)(object)equipmentDef == (Object)null) && (!configsBools[ConfigBools.configprepgiveequip].Value || equipmentDef.canDrop) && (configsBools[ConfigBools.configprepgiveequiplunar].Value || !equipmentDef.isLunar) && !excludedItems.Contains(((Object)equipmentDef).name))
					{
						list.Add(current);
					}
				}
				return list;
			}
			finally
			{
				((IDisposable)(*(AllEquipmentEnumerator*)(&enumerator))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		private static void GiveRandomItems(Inventory inventory, List<ItemIndex> pool, int count)
		{
			//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_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			if (pool.Count <= 0)
			{
				return;
			}
			bool flag = false;
			List<ItemIndex> list = new List<ItemIndex>(pool);
			for (int i = 0; i < count; i++)
			{
				if (list.Count == 0)
				{
					if (configsBools[ConfigBools.configANoDuplicates].Value)
					{
						break;
					}
					list = new List<ItemIndex>(pool);
					flag = true;
				}
				ItemIndex val = list[Random.Range(0, list.Count)];
				inventory.GiveItemPermanent(val, 1);
				if (!flag && (configsBools[ConfigBools.configANoDuplicates].Value || configsBools[ConfigBools.configNoDuplicates].Value))
				{
					list.Remove(val);
				}
			}
		}

		private static void GiveRandomEquipment(Inventory inventory, List<EquipmentIndex> pool)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			if (pool.Count != 0)
			{
				EquipmentIndex val = pool[Random.Range(0, pool.Count)];
				inventory.SetEquipmentIndex(val, false);
			}
		}

		private static void GiveGuaranteedItems(Inventory inventory)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Invalid comparison between Unknown and I4
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			string[] array = guaranteedItems;
			foreach (string text in array)
			{
				ItemIndex val = ItemCatalog.FindItemIndex(text);
				if ((int)val != -1)
				{
					inventory.GiveItemPermanent(val, 1);
					continue;
				}
				EquipmentIndex val2 = EquipmentCatalog.FindEquipmentIndex(text);
				if ((int)val2 != -1)
				{
					inventory.SetEquipmentIndex(val2, false);
				}
			}
		}

		private static void OrganizeInventoryByRarity(Inventory inventory)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: 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_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)inventory == (Object)null)
			{
				return;
			}
			Dictionary<ItemIndex, int> dictionary = new Dictionary<ItemIndex, int>();
			Enumerator<ItemDef> enumerator = ItemCatalog.allItemDefs.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					ItemDef current = enumerator.Current;
					int itemCountPermanent = inventory.GetItemCountPermanent(current.itemIndex);
					if (itemCountPermanent > 0)
					{
						dictionary[current.itemIndex] = itemCountPermanent;
					}
				}
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
			foreach (KeyValuePair<ItemIndex, int> item in dictionary)
			{
				inventory.RemoveItemPermanent(item.Key, item.Value);
			}
			ItemTier[] array = new ItemTier[8];
			RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
			ItemTier[] array2 = (ItemTier[])(object)array;
			foreach (ItemTier val in array2)
			{
				foreach (KeyValuePair<ItemIndex, int> item2 in dictionary)
				{
					ItemDef itemDef = ItemCatalog.GetItemDef(item2.Key);
					if ((Object)(object)itemDef != (Object)null && itemDef.tier == val)
					{
						inventory.GiveItemPermanent(item2.Key, item2.Value);
					}
				}
			}
		}
	}
}