Decompiled source of TooLittleSpawns v1.0.2

TooLittleSpawns.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HG;
using HG.Reflection;
using Microsoft.CodeAnalysis;
using On.RoR2;
using RiskOfOptions;
using RiskOfOptions.OptionConfigs;
using RiskOfOptions.Options;
using RoR2;
using RoR2.Navigation;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: OptIn]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TooLittleSpawns")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+330bbb1756ba775a43f02a6cca8cf18d7f5ad93c")]
[assembly: AssemblyProduct("TooLittleSpawns")]
[assembly: AssemblyTitle("TooLittleSpawns")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: UnverifiableCode]
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 TooLittleSpawns
{
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			if (TooLittleSpawns.debug.Value)
			{
				_logSource.LogDebug(data);
			}
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
	internal static class ROOSupport
	{
		internal static void SliderConfig(float min, float max, ConfigEntry<float> config)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: 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_001f: Expected O, but got Unknown
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			StepSliderConfig val = new StepSliderConfig
			{
				max = max,
				min = min,
				FormatString = "{0:0}"
			};
			ModSettingsManager.AddOption((BaseOption)new StepSliderOption(config, val));
		}

		internal static void SliderConfig(int min, int max, ConfigEntry<int> config)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: 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_001f: Expected O, but got Unknown
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			IntSliderConfig val = new IntSliderConfig
			{
				max = max,
				min = min,
				formatString = "{0:0}"
			};
			ModSettingsManager.AddOption((BaseOption)new IntSliderOption(config, val));
		}

		internal static void CheckboxConfig(ConfigEntry<bool> config, bool restartRequired = false)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			CheckBoxConfig val = new CheckBoxConfig();
			((BaseOptionConfig)val).restartRequired = restartRequired;
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(config, val));
		}

		internal static void StringConfig(ConfigEntry<string> config)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			InputFieldConfig val = new InputFieldConfig();
			ModSettingsManager.AddOption((BaseOption)new StringInputFieldOption(config, val));
		}

		internal static void KeyboardConfig(ConfigEntry<KeyboardShortcut> config)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			KeyBindConfig val = new KeyBindConfig();
			ModSettingsManager.AddOption((BaseOption)new KeyBindOption(config, val));
		}
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("kina.TooLittleSpawns", "TooLittleSpawns", "1.0.2")]
	public class TooLittleSpawns : BaseUnityPlugin
	{
		private const string PluginGUID = "kina.TooLittleSpawns";

		private const string PluginAuthor = "kina";

		private const string PluginName = "TooLittleSpawns";

		private const string PluginVersion = "1.0.2";

		private static ConfigEntry<int> playerBehavior;

		public static ConfigEntry<bool> debug;

		private GameObject teleporterObject;

		private SpawnPoint initialSpawn;

		private List<SpawnPoint> newSpawns = new List<SpawnPoint>();

		private static bool UHRInstalled => Chainloader.PluginInfos.ContainsKey("iDeathHD.UnityHotReload");

		private static bool ROOInstalled => Chainloader.PluginInfos.ContainsKey("com.rune580.riskofoptions");

		public void Awake()
		{
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Expected O, but got Unknown
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Expected O, but got Unknown
			Log.Init(((BaseUnityPlugin)this).Logger);
			playerBehavior = ((BaseUnityPlugin)this).Config.Bind<int>("TooLittleSpawns", "Max players needed to use new behavior", 4, "Max players limit to determine whether or not to use new behavior. Note that this is the limit, not the amount of players.");
			if (ROOInstalled)
			{
				ROOSupport.SliderConfig(1, 16, playerBehavior);
			}
			debug = ((BaseUnityPlugin)this).Config.Bind<bool>("TooLittleSpawns", "Log debug", false, "Whether to use debug logging or not. Logs whether a spawn is using a spawnpoint, creating it from the nodegraph and other stuff if you're paranoid about it working or something");
			if (ROOInstalled)
			{
				ROOSupport.CheckboxConfig(debug);
			}
			SceneDirector.PlaceTeleporter += new hook_PlaceTeleporter(SceneDirectorOnPlaceTeleporter);
			SpawnPoint.ConsumeSpawnPoint += new hook_ConsumeSpawnPoint(SpawnPointOnConsumeSpawnPoint);
		}

		private void SceneDirectorOnPlaceTeleporter(orig_PlaceTeleporter orig, SceneDirector self)
		{
			orig.Invoke(self);
			teleporterObject = self.teleporterInstance;
		}

		private SpawnPoint SpawnPointOnConsumeSpawnPoint(orig_ConsumeSpawnPoint orig)
		{
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_0442: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_0251: Unknown result type (might be due to invalid IL or missing references)
			//IL_0256: Unknown result type (might be due to invalid IL or missing references)
			//IL_025a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0360: Unknown result type (might be due to invalid IL or missing references)
			//IL_0378: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_039a: Unknown result type (might be due to invalid IL or missing references)
			//IL_039f: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0294: Unknown result type (might be due to invalid IL or missing references)
			//IL_0299: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_041c: Unknown result type (might be due to invalid IL or missing references)
			//IL_020a: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0310: Unknown result type (might be due to invalid IL or missing references)
			if (RoR2Application.maxPlayers <= playerBehavior.Value)
			{
				return orig.Invoke();
			}
			try
			{
				if (!Object.op_Implicit((Object)(object)initialSpawn))
				{
					Log.Debug("creating initial spawn .,,.");
					if (SpawnPoint.readOnlyInstancesList.Count == 0)
					{
						Log.Debug("no spawn points found at all .,.,. using default behavior !!");
						return orig.Invoke();
					}
					List<SpawnPoint> list = new List<SpawnPoint>(SpawnPoint.readOnlyInstancesList);
					if (Object.op_Implicit((Object)(object)teleporterObject))
					{
						list.Sort(delegate(SpawnPoint a, SpawnPoint b)
						{
							//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_0026: 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_003b: Unknown result type (might be due to invalid IL or missing references)
							Vector3 val11 = teleporterObject.transform.position;
							float sqrMagnitude = ((Vector3)(ref val11)).sqrMagnitude;
							val11 = teleporterObject.transform.position - ((Component)b).transform.position;
							return sqrMagnitude.CompareTo(((Vector3)(ref val11)).sqrMagnitude);
						});
					}
					initialSpawn = list[list.Count - 1];
					initialSpawn.consumed = true;
					newSpawns.Add(initialSpawn);
					return initialSpawn;
				}
				SpawnPoint val = null;
				float num = float.MaxValue;
				foreach (SpawnPoint readOnlyInstances in SpawnPoint.readOnlyInstancesList)
				{
					if (!readOnlyInstances.consumed)
					{
						float num2 = Vector3.Distance(((Component)readOnlyInstances).gameObject.transform.position, ((Component)initialSpawn).gameObject.transform.position);
						if (num2 < num && num2 < 40f)
						{
							val = readOnlyInstances;
							num = num2;
						}
					}
				}
				if (!Object.op_Implicit((Object)(object)val))
				{
					NodeGraph groundNodes = SceneInfo.instance.groundNodes;
					NodeFlags val2 = (NodeFlags)0;
					NodeFlags val3 = (NodeFlags)0;
					val3 = (NodeFlags)(val3 | 4);
					List<NodeIndex> list2 = groundNodes.GetActiveNodesForHullMaskWithFlagConditions((HullMask)2, val2, val3);
					for (int num3 = 0; num3 < list2.Count; num3++)
					{
						if (!SceneDirector.IsNodeSuitableForPod(groundNodes, list2[num3]))
						{
							list2.RemoveAt(num3);
						}
					}
					if (PlayerSpawnInhibitor.readOnlyInstancesList.Count > 0)
					{
						List<NodeIndex> list3 = new List<NodeIndex>();
						for (int num4 = 0; num4 < list2.Count; num4++)
						{
							bool flag = false;
							foreach (PlayerSpawnInhibitor readOnlyInstances2 in PlayerSpawnInhibitor.readOnlyInstancesList)
							{
								if (readOnlyInstances2.IsInhibiting(groundNodes, list2[num4]))
								{
									flag = true;
									break;
								}
							}
							if (!flag)
							{
								list3.Add(list2[num4]);
							}
						}
						if (list3.Count > 0)
						{
							list2 = list3;
						}
					}
					num = float.MaxValue;
					NodeIndex? val4 = null;
					Vector3 val5 = default(Vector3);
					foreach (NodeIndex item in list2)
					{
						groundNodes.GetNodePosition(item, ref val5);
						bool flag2 = false;
						foreach (SpawnPoint newSpawn in newSpawns)
						{
							if (Object.op_Implicit((Object)(object)newSpawn) && ((Component)newSpawn).gameObject.transform.position == val5)
							{
								flag2 = true;
							}
							if (Object.op_Implicit((Object)(object)newSpawn) && Vector3.Distance(((Component)newSpawn).gameObject.transform.position, val5) < 5f)
							{
								flag2 = true;
							}
						}
						if (!flag2)
						{
							float num5 = Vector3.Distance(val5, ((Component)initialSpawn).gameObject.transform.position);
							if (num5 < num)
							{
								val4 = item;
								num = num5;
							}
						}
					}
					if (!val4.HasValue)
					{
						Log.Error("new spawn node is null ! gorp ,.., falling back on first spawn entry ,..,");
						return SpawnPoint.readOnlyInstancesList[0];
					}
					Vector3 val6 = default(Vector3);
					groundNodes.GetNodePosition(val4.Value, ref val6);
					List<LinkIndex> list4 = CollectionPool<LinkIndex, List<LinkIndex>>.RentCollection();
					groundNodes.GetActiveNodeLinks(val4.Value, list4);
					Quaternion val9;
					if (list4.Count > 0)
					{
						LinkIndex val7 = Run.instance.spawnRng.NextElementUniform<LinkIndex>(list4);
						Vector3 val8 = default(Vector3);
						groundNodes.GetNodePosition(groundNodes.GetLinkEndNode(val7), ref val8);
						val9 = Util.QuaternionSafeLookRotation(val8 - val6);
					}
					else
					{
						val9 = Quaternion.Euler(0f, Run.instance.spawnRng.nextNormalizedFloat * 360f, 0f);
					}
					GameObject val10 = Object.Instantiate<GameObject>(SpawnPoint.prefab, val6, val9);
					val = val10.GetComponent<SpawnPoint>();
					newSpawns.Add(val);
					Log.Debug($"finalized spawn from nodegraph {val10.transform.position} !");
				}
				else
				{
					Log.Debug($"got spawn from spawnlist {((Component)val).gameObject.transform.position}");
				}
				val.consumed = true;
				return val;
			}
			catch (Exception data)
			{
				Log.Warning("error while trying to generate a new spawn point !!! falling back on default behavior ,..,,.");
				Log.Warning(data);
				return orig.Invoke();
			}
		}

		private void Update()
		{
		}
	}
}