Decompiled source of PlannedEvolution v2.3.3

PlannedEvolution.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 BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using On.RoR2.Artifacts;
using RoR2;
using RoR2.Artifacts;
using UnityEngine;

[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("PlannedEvolution")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+1bfebf389102374b1362d07be314e57b8ccdd344")]
[assembly: AssemblyProduct("PlannedEvolution")]
[assembly: AssemblyTitle("PlannedEvolution")]
[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 PlannedEvolution
{
	[BepInPlugin("com.FortressForce.PlannedEvolution", "Planned Evolution", "2.3.3")]
	public class PlannedEvolutionPlugin : BaseUnityPlugin
	{
		private class EvolutionStep
		{
			public string ItemIdentifier = string.Empty;

			public int Count;
		}

		private ConfigEntry<bool>? ModEnabled;

		private ConfigEntry<string>[] StageConfigs = new ConfigEntry<string>[20];

		private static ConfigEntry<string>? BlacklistWhite;

		private static ConfigEntry<string>? BlacklistGreen;

		private static ConfigEntry<string>? BlacklistRed;

		private List<ItemIndex> BannedItemIndices = new List<ItemIndex>();

		private bool isCacheBuilt;

		private static Inventory? MonsterInventory
		{
			get
			{
				object? obj = typeof(MonsterTeamGainsItemsArtifactManager).GetField("monsterTeamInventory", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null);
				return (Inventory?)((obj is Inventory) ? obj : null);
			}
		}

		public void Awake()
		{
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Expected O, but got Unknown
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Set to false to disable the mod.");
			for (int i = 0; i < 20; i++)
			{
				int num = i + 1;
				string text = ((num <= 5) ? "AnyWhite, 1" : ((num <= 10) ? "AnyGreen, 1" : "AnyRed, 1"));
				StageConfigs[i] = ((BaseUnityPlugin)this).Config.Bind<string>("Evolution Schedule", $"Stage {num}", text, "ItemName, Count; ItemName, Count");
			}
			BlacklistWhite = ((BaseUnityPlugin)this).Config.Bind<string>("Blacklists", "Banned Whites", "RollOfPennies", "Banned White items.");
			BlacklistGreen = ((BaseUnityPlugin)this).Config.Bind<string>("Blacklists", "Banned Greens", "SquidPolyp", "Banned Green items.");
			BlacklistRed = ((BaseUnityPlugin)this).Config.Bind<string>("Blacklists", "Banned Reds", "ShockNearby", "Banned Red items.");
			ConfigEntry<bool>? modEnabled = ModEnabled;
			if (modEnabled == null || modEnabled.Value)
			{
				MonsterTeamGainsItemsArtifactManager.GrantMonsterTeamItem += new hook_GrantMonsterTeamItem(OverrideEvolution);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"PlannedEvolution 2.3.1: Private field access fixed via Reflection.");
			}
		}

		private void OverrideEvolution(orig_GrantMonsterTeamItem orig)
		{
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Expected O, but got Unknown
			orig.Invoke();
			try
			{
				if (!isCacheBuilt)
				{
					BuildBlacklistCache();
				}
				Inventory monsterInventory = MonsterInventory;
				if (!((Object)(object)monsterInventory != (Object)null))
				{
					return;
				}
				monsterInventory.CleanInventory();
				int num = Run.instance.stageClearCount + 1;
				List<string> list = new List<string>();
				for (int i = 0; i < num && i < StageConfigs.Length; i++)
				{
					string text = StageConfigs[i]?.Value;
					if (string.IsNullOrEmpty(text))
					{
						continue;
					}
					foreach (EvolutionStep item in ParseStageConfig(text))
					{
						string text2 = ApplyEvolutionStep(item, monsterInventory);
						if (i + 1 == num && !string.IsNullOrEmpty(text2))
						{
							list.Add($"{item.Count}x {text2}");
						}
					}
				}
				if (RunArtifactManager.instance.IsArtifactEnabled(ArtifactCatalog.FindArtifactDef("Evolution")) && list.Count > 0)
				{
					Chat.SendBroadcastChat((ChatMessageBase)new SimpleChatMessage
					{
						baseToken = $"[Planned Evolution] Stage {num}: " + string.Join(", ", list)
					});
				}
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Error in OverrideEvolution: {arg}");
			}
		}

		private List<EvolutionStep> ParseStageConfig(string rawText)
		{
			List<EvolutionStep> list = new List<EvolutionStep>();
			string[] array = rawText.Split(';');
			for (int i = 0; i < array.Length; i++)
			{
				string[] array2 = array[i].Trim().Split(',');
				if (array2.Length == 2 && int.TryParse(array2[1].Trim(), out var result))
				{
					list.Add(new EvolutionStep
					{
						ItemIdentifier = array2[0].Trim(),
						Count = result
					});
				}
			}
			return list;
		}

		private string? ApplyEvolutionStep(EvolutionStep step, Inventory inventory)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: 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_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Invalid comparison between Unknown and I4
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			ItemIndex val = (step.ItemIdentifier.StartsWith("Any") ? PickRandomItem(step.ItemIdentifier) : FindItemIndexBetter(step.ItemIdentifier));
			if ((int)val != -1)
			{
				ItemDef itemDef = ItemCatalog.GetItemDef(val);
				if (step.Count > 0)
				{
					inventory.GiveItemPermanent(val, step.Count);
				}
				else if (step.Count < 0)
				{
					inventory.RemoveItemPermanent(val, Math.Abs(step.Count));
				}
				return Language.GetString(itemDef.nameToken);
			}
			return null;
		}

		private void BuildBlacklistCache()
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Invalid comparison between Unknown and I4
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			if (isCacheBuilt || !((ResourceAvailability)(ref ItemCatalog.availability)).available)
			{
				return;
			}
			BannedItemIndices.Clear();
			string[] array = (BlacklistWhite?.Value + "," + BlacklistGreen?.Value + "," + BlacklistRed?.Value).Split(',');
			foreach (string text in array)
			{
				ItemIndex val = FindItemIndexBetter(text.Trim());
				if ((int)val != -1)
				{
					BannedItemIndices.Add(val);
				}
			}
			isCacheBuilt = true;
		}

		private ItemIndex FindItemIndexBetter(string identifier)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: 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_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			ItemIndex val = ItemCatalog.FindItemIndex(identifier);
			if ((int)val != -1)
			{
				return val;
			}
			GenericStaticEnumerable<ItemIndex, AllItemsEnumerator> allItems = ItemCatalog.allItems;
			AllItemsEnumerator enumerator = 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) && (((Object)itemDef).name.Equals(identifier, StringComparison.OrdinalIgnoreCase) || Language.GetString(itemDef.nameToken).Equals(identifier, StringComparison.OrdinalIgnoreCase)))
					{
						return current;
					}
				}
			}
			finally
			{
				((IDisposable)(AllItemsEnumerator)(ref enumerator)).Dispose();
			}
			return (ItemIndex)(-1);
		}

		private ItemIndex PickRandomItem(string keyword)
		{
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			List<PickupIndex> list = keyword switch
			{
				"AnyWhite" => Run.instance.availableTier1DropList, 
				"AnyGreen" => Run.instance.availableTier2DropList, 
				"AnyRed" => Run.instance.availableTier3DropList, 
				"AnyVoid" => Run.instance.availableVoidTier1DropList, 
				_ => null, 
			};
			if (list == null || list.Count == 0)
			{
				return (ItemIndex)(-1);
			}
			List<PickupIndex> list2 = list.Where(delegate(PickupIndex p)
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				//IL_000d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				//IL_0013: Unknown result type (might be due to invalid IL or missing references)
				//IL_0015: Invalid comparison between Unknown and I4
				//IL_001d: Unknown result type (might be due to invalid IL or missing references)
				PickupDef pickupDef = PickupCatalog.GetPickupDef(p);
				ItemIndex val = (ItemIndex)((pickupDef == null) ? (-1) : ((int)pickupDef.itemIndex));
				return (int)val != -1 && !BannedItemIndices.Contains(val);
			}).ToList();
			if (list2.Count == 0)
			{
				return (ItemIndex)(-1);
			}
			return (ItemIndex)(((??)PickupCatalog.GetPickupDef(Run.instance.treasureRng.NextElementUniform<PickupIndex>(list2))?.itemIndex) ?? (-1));
		}
	}
}