Decompiled source of NoSunstoneValley v1.0.0

BepInEx/plugins/NoSunstoneValley/NoSunstoneValley.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.1.0.0")]
namespace NoSunstoneValley;

[BepInPlugin("KastoneX.NoSunstoneValley", "No Sunstone Valley", "0.1.0")]
public class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(RunManager), "SetRunLevel")]
	private static class Selection
	{
		private static bool Blocked(Level level)
		{
			return (Object)(object)level != (Object)null && (string.Equals(((Object)level).name, "Level - Desert", StringComparison.OrdinalIgnoreCase) || string.Equals(level.NarrativeName, "Sunstone Valley Resort", StringComparison.OrdinalIgnoreCase));
		}

		[HarmonyPrefix]
		private static void Prefix(ref List<Level> ___levels, out List<Level> __state)
		{
			__state = null;
			if (___levels == null)
			{
				return;
			}
			List<Level> list = new List<Level>(___levels.Count);
			int num = 0;
			foreach (Level ___level in ___levels)
			{
				if (Blocked(___level))
				{
					num++;
				}
				else
				{
					list.Add(___level);
				}
			}
			if (num == 0)
			{
				return;
			}
			bool flag = false;
			foreach (Level item in list)
			{
				if ((Object)(object)item != (Object)null)
				{
					flag = true;
				}
			}
			if (!flag)
			{
				Log.LogWarning((object)"No alternative levels available; original selection retained.");
				return;
			}
			__state = ___levels;
			___levels = list;
			Log.LogInfo((object)"Excluded Sunstone Valley Resort from this selection; original level catalogue will be restored afterwards.");
		}

		[HarmonyPostfix]
		private static void Postfix(ref List<Level> ___levels, List<Level> __state)
		{
			if (__state != null)
			{
				___levels = __state;
			}
		}

		[HarmonyFinalizer]
		private static Exception Finalizer(Exception __exception, ref List<Level> ___levels, List<Level> __state)
		{
			if (__state != null)
			{
				___levels = __state;
			}
			return __exception;
		}
	}

	private static ManualLogSource Log;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		harmony = new Harmony("KastoneX.NoSunstoneValley");
		harmony.PatchAll(typeof(Selection));
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Sunstone Valley Resort excluded from random run-level selection (host). Current scene and saves are not changed.");
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}