Decompiled source of NoMineshaft v1.1.1

plugins/com.eigeen.lethal.NoMineshaft.dll

Decompiled 2 months 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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("com.eigeen.lethal.NoMineshaft")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("NoMineshaft")]
[assembly: AssemblyTitle("com.eigeen.lethal.NoMineshaft")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 Plugin
{
	public enum InteriorEnum
	{
		Factory = 0,
		Manor = 1,
		Mineshaft = 4
	}
	[BepInPlugin("com.eigeen.lethal.NoMineshaft", "NoMineshaft", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin Instance { get; private set; }

		public static ManualLogSource Logger { get; private set; }

		internal static Harmony? Harmony { get; set; }

		public ConfigEntry<bool> ConfigEnable { get; private set; }

		public ConfigEntry<bool> ConfigLegacyMode { get; private set; }

		public ConfigEntry<bool> ConfigWhitelistMode { get; private set; }

		public ConfigEntry<string> ConfigMapWhitelist { get; private set; }

		public HashSet<string> MapWhitelist { get; private set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			ConfigEnable = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable", true, "Enable plugin features.");
			ConfigLegacyMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "LegacyMode", false, "Use legacy mode. If you don't know what is it, keep default value.");
			ConfigWhitelistMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "WhitelistMode", false, "Enable whitelist mode. Maps included in MapWhitelist will remove Mineshaft.\r\nIf false, all of the maps will remove mineshaft.");
			ConfigMapWhitelist = ((BaseUnityPlugin)this).Config.Bind<string>("General", "MapWhitelist", "Adamance, Artifice, Assurance, Dine, Embrion, Experimentation, Liquidation, March, Offense, Rend, Titan, Vow", "Maps included in MapWhitelist will take effect.");
			Logger.LogInfo((object)"com.eigeen.lethal.NoMineshaft v1.1.0 has loaded!");
			if (ConfigEnable.Value)
			{
				if (ConfigLegacyMode.Value)
				{
					Logger.LogWarning((object)"Using legacy mode, beware of unsync issues.");
				}
				ParseWhitelist();
				Patch();
			}
		}

		internal static void Patch()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("com.eigeen.lethal.NoMineshaft");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void ParseWhitelist()
		{
			MapWhitelist = new HashSet<string>(Array.ConvertAll(ConfigMapWhitelist.Value.Split(','), (string s) => s.Trim()));
		}

		public static bool IsMapAllow(string levelName)
		{
			if (!Instance.ConfigWhitelistMode.Value)
			{
				return true;
			}
			bool flag = Instance.MapWhitelist.Contains(levelName.Replace("Level", ""));
			if (!flag)
			{
				Logger.LogInfo((object)(levelName + " excluded in the whitelist, ignore."));
			}
			return flag;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.eigeen.lethal.NoMineshaft";

		public const string PLUGIN_NAME = "NoMineshaft";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace Plugin.Patches
{
	[HarmonyPatch(typeof(StartOfRound), "ChooseNewRandomMapSeed")]
	public class ChooseNewRandomMapSeedPatch
	{
		private static void Postfix(StartOfRound __instance)
		{
			if (Plugin.Instance.ConfigLegacyMode.Value || !Plugin.IsMapAllow(((Object)__instance.currentLevel).name))
			{
				return;
			}
			int randomMapSeed = __instance.randomMapSeed;
			RoundManager instance = RoundManager.Instance;
			InteriorEnum? interiorEnum = CalcInteriorType(randomMapSeed, instance);
			if (!interiorEnum.HasValue)
			{
				return;
			}
			interiorEnum = interiorEnum.Value;
			if (interiorEnum.GetValueOrDefault() != InteriorEnum.Mineshaft)
			{
				Plugin.Logger.LogInfo((object)$"Detected the current map seed ({randomMapSeed}) will generate {interiorEnum} interior, do nothing.");
				return;
			}
			Plugin.Logger.LogInfo((object)$"Detected the current map seed ({randomMapSeed}) will generate mineshaft interior, try to regenerate.");
			instance.hasInitializedLevelRandomSeed = false;
			instance.InitializeRandomNumberGenerators();
			for (int i = 0; i < 1000; i++)
			{
				randomMapSeed = NewMapSeed();
				interiorEnum = CalcInteriorType(randomMapSeed, instance);
				Plugin.Logger.LogInfo((object)$"Try No.{i} (Seed {randomMapSeed}) Interior: {interiorEnum}");
				if (!interiorEnum.HasValue)
				{
					Plugin.Logger.LogWarning((object)"Detected unknown interior type. Ignore.");
					return;
				}
				if (new InteriorEnum?(interiorEnum.Value).GetValueOrDefault() != InteriorEnum.Mineshaft)
				{
					__instance.randomMapSeed = randomMapSeed;
					Plugin.Logger.LogInfo((object)$"Generated new map seed: {randomMapSeed} after {i + 1} retries.");
					return;
				}
			}
			Plugin.Logger.LogWarning((object)"Regenerate failed after 1000 retries.");
		}

		private static InteriorEnum? CalcInteriorType(int mapSeed, RoundManager roundManager)
		{
			Random random = new Random(mapSeed);
			if (roundManager.currentLevel.dungeonFlowTypes == null || roundManager.currentLevel.dungeonFlowTypes.Length == 0)
			{
				return null;
			}
			List<int> list = roundManager.currentLevel.dungeonFlowTypes.Select((IntWithRarity flowType) => flowType.rarity).ToList();
			Plugin.Logger.LogDebug((object)("List: " + string.Join(", ", list)));
			int randomWeightedIndex = roundManager.GetRandomWeightedIndex(list.ToArray(), random);
			Plugin.Logger.LogDebug((object)$"randomWeightedIndex: {randomWeightedIndex}");
			int id = roundManager.currentLevel.dungeonFlowTypes[randomWeightedIndex].id;
			Plugin.Logger.LogDebug((object)$"mapId: {id}");
			if (Enum.IsDefined(typeof(InteriorEnum), id))
			{
				return (InteriorEnum)id;
			}
			return null;
		}

		private static int NewMapSeed()
		{
			return Random.Range(1, 100000000);
		}
	}
	[HarmonyPatch(typeof(RoundManager), "GenerateNewFloor")]
	public class GenerateNewFloorPatch
	{
		private static bool Prefix(RoundManager __instance)
		{
			if (!Plugin.Instance.ConfigLegacyMode.Value)
			{
				return true;
			}
			if (!Plugin.IsMapAllow(((Object)__instance.currentLevel).name))
			{
				return true;
			}
			__instance.currentLevel.dungeonFlowTypes = __instance.currentLevel.dungeonFlowTypes.Where((IntWithRarity dungeonFlowType) => dungeonFlowType.id != 4).ToArray();
			Plugin.Logger.LogInfo((object)("Removed mineshaft generation of " + ((Object)__instance.currentLevel).name + "."));
			return true;
		}
	}
}