Decompiled source of WeightedCategoryPatch v1.0.4

BeepInEx/plugins/WeightedCategoryPatch.dll

Decompiled 2 days 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using BrutalCompanyMinus;
using BrutalCompanyMinus.Minus;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("BrutalCompanyMinus")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("WeightedCategoryPatch")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WeightedCategoryPatch")]
[assembly: AssemblyTitle("WeightedCategoryPatch")]
[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.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]
	[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;
		}
	}
	[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 WeightedCategoryPatch
{
	[HarmonyPatch]
	internal static class EventManagerPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch(typeof(EventManager), "ModifyLevel")]
		private static void ModifyLevel_Prefix()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			if (Configuration.useCustomWeights.Value)
			{
				int num = Configuration.eventTypeScales.Length;
				float[] array = new float[num];
				for (int i = 0; i < num; i++)
				{
					array[i] = Scale.Compute(Configuration.eventTypeScales[i], (EventType)3);
				}
				float num2 = 0f;
				for (int j = 0; j < num; j++)
				{
					num2 += array[j];
				}
				num2 = fix(num2);
				float[] array2 = new float[num];
				for (int k = 0; k < num; k++)
				{
					array2[k] = array[k] / num2;
				}
				EventManager.eventTypeRarities = array2;
			}
			static float fix(float value)
			{
				if (!(value < 1f))
				{
					return value;
				}
				return 1f;
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(EventManager), "ChooseEvents")]
		private static bool ChooseEvents_Prefix(out List<MEvent> additionalEvents, ref List<MEvent> __result)
		{
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			if (!Configuration.useCustomWeights.Value)
			{
				additionalEvents = new List<MEvent>();
				return true;
			}
			EventManager.currentEvents.Clear();
			EventManager.sideEvents.Clear();
			List<MEvent> list = new List<MEvent>();
			List<MEvent> list2 = new List<MEvent>();
			foreach (MEvent @event in EventManager.events)
			{
				list2.Add(@event);
			}
			Random random = new Random(StartOfRound.Instance.randomMapSeed + 32345 + Environment.TickCount);
			int num = (int)Scale.Compute(Configuration.eventsToSpawn, (EventType)3) + RoundManager.Instance.GetRandomWeightedIndex(Helper.IntArray(Configuration.weightsForExtraEvents), random);
			if (Configuration.scaleHeat.Value && EventManager.currentHeatDifficulty() == Configuration.heatMaxCap.Value && Configuration.heatForceEventAtMax.Value)
			{
				string[] array = (from s in Configuration.heatEventsToForce.Value.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
					select s.Trim()).ToArray();
				EventManager.forcedEvents.AddRange(EventManager.GetEventsByName(array));
			}
			foreach (MEvent forcedEvent in EventManager.forcedEvents)
			{
				list2.RemoveAll((MEvent x) => x.Name() == forcedEvent.Name());
				foreach (string item in forcedEvent.EventsToRemove)
				{
					_ = item;
					list2.RemoveAll((MEvent x) => x.Name() == forcedEvent.Name());
				}
			}
			int num2;
			for (num2 = 0; num2 < num; num2++)
			{
				MEvent newEvent = PickEventByCategory(list2, random);
				if (!newEvent.AddEventIfOnly())
				{
					num2--;
					list2.RemoveAll((MEvent x) => x.Name() == newEvent.Name());
				}
				else
				{
					bool flag = (newEvent.MoonMode ? EventManager.IsEventOnMoonWhitelist(newEvent) : (!EventManager.IsIgnoredEventByMoonBlacklist(newEvent)));
					if (flag && (newEvent.isSpecialEvent || newEvent.isBetaEvent))
					{
						bool num3 = newEvent.isSpecialEvent && !Configuration.enableSpecialEvents.Value;
						bool flag2 = newEvent.isBetaEvent && !Configuration.enableBetaEvents.Value;
						if (num3 || flag2)
						{
							flag = false;
						}
					}
					if (!flag)
					{
						num2--;
						list2.RemoveAll((MEvent x) => x.Name() == newEvent.Name());
					}
					else
					{
						list.Add(newEvent);
						list2.RemoveAll((MEvent x) => x.Name() == newEvent.Name());
						int num4 = 0;
						foreach (string eventToRemove in newEvent.EventsToRemove)
						{
							list2.RemoveAll((MEvent x) => x.Name() == eventToRemove);
							num4 += list.RemoveAll((MEvent x) => x.Name() == eventToRemove);
						}
						foreach (string eventToSpawnWith in newEvent.EventsToSpawnWith)
						{
							list2.RemoveAll((MEvent x) => x.Name() == eventToSpawnWith);
							num4 += list.RemoveAll((MEvent x) => x.Name() == eventToSpawnWith);
						}
						num2 -= num4;
					}
				}
			}
			List<MEvent> list3 = new List<MEvent>();
			for (int num5 = 0; num5 < list.Count; num5++)
			{
				foreach (string eventToSpawnWith2 in list[num5].EventsToSpawnWith)
				{
					if (list3.FindIndex((MEvent x) => x.Name() == eventToSpawnWith2) == -1)
					{
						list3.Add(MEvent.GetEvent(eventToSpawnWith2));
					}
					if (EventManager.sideEvents.FindIndex((MEvent x) => x.Name() == eventToSpawnWith2) == -1)
					{
						EventManager.sideEvents.Add(MEvent.GetEvent(eventToSpawnWith2));
					}
				}
			}
			foreach (MEvent e in EventManager.disabledEvents)
			{
				int num6 = list3.FindIndex((MEvent x) => x.Name() == e.Name());
				if (num6 != -1)
				{
					list3.RemoveAt(num6);
				}
			}
			additionalEvents = list3;
			EventManager.currentEvents = list;
			__result = list;
			return false;
		}

		private static MEvent PickEventByCategory(List<MEvent> pool, Random rng)
		{
			//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_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)
			EventType val = PickCategoryByRarities(rng);
			List<MEvent> list = new List<MEvent>();
			foreach (MEvent item in pool)
			{
				if (item.Type == val)
				{
					list.Add(item);
				}
			}
			if (list.Count == 0)
			{
				list.AddRange(pool);
			}
			return EventManager.RandomWeightedEvent(list, rng);
		}

		private static EventType PickCategoryByRarities(Random rng)
		{
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			float[] eventTypeRarities = EventManager.eventTypeRarities;
			float num = 0f;
			float[] array = eventTypeRarities;
			foreach (float num2 in array)
			{
				num += num2;
			}
			if (!(num <= 0f))
			{
				float num3 = (float)rng.NextDouble() * num;
				float num4 = 0f;
				Array values = Enum.GetValues(typeof(EventType));
				for (int j = 0; j < eventTypeRarities.Length && j < values.Length; j++)
				{
					num4 += eventTypeRarities[j];
					if (num3 < num4)
					{
						return (EventType)values.GetValue(j);
					}
				}
				return (EventType)3;
			}
			return (EventType)3;
		}
	}
	[BepInPlugin("PikaWarrior.WeightedCategoryPatch", "WeightedCategoryPatch", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private readonly Harmony _harmony = new Harmony("PikaWarrior.WeightedCategoryPatch");

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony.PatchAll(typeof(EventManagerPatch));
			Log.LogInfo((object)"WeightedCategoryPatch v1.0.0 loaded.");
		}
	}
	internal static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "PikaWarrior.WeightedCategoryPatch";

		public const string PLUGIN_NAME = "WeightedCategoryPatch";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}