Decompiled source of ValeriusNoobShield v1.0.2

BepInEx\plugins\ValeriusNoobShield.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("ValeriusNoobShield")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ValeriusNoobShield")]
[assembly: AssemblyTitle("ValeriusNoobShield")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
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;
		}
	}
}
[BepInPlugin("com.valheim.valeriusnoobshield", "Valerius Noob Shield", "1.0.2")]
public class Plugin : BaseUnityPlugin
{
	public const string PluginGuid = "com.valheim.valeriusnoobshield";

	public const string PluginName = "Valerius Noob Shield";

	public const string PluginVersion = "1.0.2";

	public const int MaxBiomeOverflow = 2;

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		new Harmony("com.valheim.valeriusnoobshield").PatchAll();
		Log.LogInfo((object)("Loaded. Max downward biome overflow: " + $"{2}"));
	}
}
public static class BiomeUtils
{
	private static readonly (Biome biome, int tier)[] Biomes = new(Biome, int)[8]
	{
		((Biome)1, 0),
		((Biome)8, 1),
		((Biome)2, 2),
		((Biome)4, 3),
		((Biome)16, 4),
		((Biome)512, 5),
		((Biome)32, 6),
		((Biome)64, 7)
	};

	public static int GetTier(Biome biome)
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: 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)
		(Biome, int)[] biomes = Biomes;
		for (int i = 0; i < biomes.Length; i++)
		{
			(Biome, int) tuple = biomes[i];
			if ((biome & tuple.Item1) != 0)
			{
				return tuple.Item2;
			}
		}
		return -1;
	}

	public static string GetName(int tier)
	{
		return tier switch
		{
			0 => "Meadows", 
			1 => "Black Forest", 
			2 => "Swamp", 
			3 => "Mountain", 
			4 => "Plains", 
			5 => "Mistlands", 
			6 => "Ashlands", 
			7 => "Deep North", 
			_ => "Unknown", 
		};
	}

	public static string FormatDifference(int difference)
	{
		if (difference == 0)
		{
			return "native";
		}
		int num = Mathf.Abs(difference);
		string text = ((difference > 0) ? "down" : "up");
		if (num != 1)
		{
			return $"{num} tiers {text}";
		}
		return "1 tier " + text;
	}
}
[HarmonyPatch(typeof(SpawnSystem), "UpdateSpawning")]
public static class SpawnSystemNativeBiomePatch
{
	private static readonly Dictionary<string, int> NativeBiomeTiers = new Dictionary<string, int>();

	private static bool registryBuilt;

	private static void Prefix(SpawnSystem __instance)
	{
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		if (registryBuilt || __instance.m_spawnLists == null || __instance.m_spawnLists.Count == 0)
		{
			return;
		}
		foreach (SpawnSystemList spawnList in __instance.m_spawnLists)
		{
			if (spawnList?.m_spawners == null)
			{
				continue;
			}
			foreach (SpawnData spawner in spawnList.m_spawners)
			{
				if ((Object)(object)spawner?.m_prefab == (Object)null || !string.IsNullOrEmpty(spawner.m_requiredGlobalKey))
				{
					continue;
				}
				int tier = BiomeUtils.GetTier(spawner.m_biome);
				if (tier >= 0)
				{
					string name = ((Object)spawner.m_prefab).name;
					if (!NativeBiomeTiers.TryGetValue(name, out var value) || tier < value)
					{
						NativeBiomeTiers[name] = tier;
					}
				}
			}
		}
		if (NativeBiomeTiers.Count != 0)
		{
			registryBuilt = true;
			Plugin.Log.LogInfo((object)("Native biome registry built: " + $"{NativeBiomeTiers.Count} creatures"));
		}
	}

	public static bool TryGetNativeTier(string prefabName, out int nativeTier)
	{
		return NativeBiomeTiers.TryGetValue(prefabName, out nativeTier);
	}
}
[HarmonyPatch(typeof(SpawnSystem), "IsSpawnPointGood")]
public static class SpawnSystemBiomeProgressionPatch
{
	private static void Postfix(SpawnData spawn, ref Vector3 spawnPoint, ref bool __result)
	{
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		if (!__result || (Object)(object)spawn?.m_prefab == (Object)null || ((Object)spawn.m_prefab).name == "projectile_FimbulvinterMeteor" || string.IsNullOrEmpty(spawn.m_requiredGlobalKey) || !SpawnSystemNativeBiomePatch.TryGetNativeTier(((Object)spawn.m_prefab).name, out var nativeTier) || (Object)(object)ZoneSystem.instance == (Object)null)
		{
			return;
		}
		Vector3 val = default(Vector3);
		Biome biome = default(Biome);
		BiomeArea val2 = default(BiomeArea);
		Heightmap val3 = default(Heightmap);
		ZoneSystem.instance.GetGroundData(ref spawnPoint, ref val, ref biome, ref val2, ref val3);
		int tier = BiomeUtils.GetTier(biome);
		if (tier >= 0)
		{
			int num = nativeTier - tier;
			if (num > 2)
			{
				__result = false;
				Plugin.Log.LogInfo((object)("BLOCKED " + ((Object)spawn.m_prefab).name + " | " + BiomeUtils.GetName(nativeTier) + " -> " + BiomeUtils.GetName(tier) + " | " + BiomeUtils.FormatDifference(num)));
			}
		}
	}
}
[HarmonyPatch(typeof(SpawnSystem), "Spawn")]
public static class SpawnSystemAllowedLogPatch
{
	private static void Postfix(SpawnData critter, Vector3 spawnPoint)
	{
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)critter?.m_prefab == (Object)null) && !(((Object)critter.m_prefab).name == "projectile_FimbulvinterMeteor") && SpawnSystemNativeBiomePatch.TryGetNativeTier(((Object)critter.m_prefab).name, out var nativeTier) && !((Object)(object)ZoneSystem.instance == (Object)null))
		{
			Vector3 val = default(Vector3);
			Biome biome = default(Biome);
			BiomeArea val2 = default(BiomeArea);
			Heightmap val3 = default(Heightmap);
			ZoneSystem.instance.GetGroundData(ref spawnPoint, ref val, ref biome, ref val2, ref val3);
			int tier = BiomeUtils.GetTier(biome);
			if (tier >= 0)
			{
				int difference = nativeTier - tier;
				Plugin.Log.LogInfo((object)("ALLOWED " + ((Object)critter.m_prefab).name + " | " + BiomeUtils.GetName(nativeTier) + " -> " + BiomeUtils.GetName(tier) + " | " + BiomeUtils.FormatDifference(difference)));
			}
		}
	}
}
[HarmonyPatch(typeof(RandEventSystem))]
public static class RandEventSystemProgressionPatch
{
	[HarmonyPatch("GetValidEventPoints")]
	[HarmonyPostfix]
	private static void Postfix(RandomEvent ev, ref List<Vector3> __result)
	{
		if (__result == null || __result.Count == 0 || ev?.m_spawn == null || ev.m_spawn.Count == 0 || (Object)(object)ZoneSystem.instance == (Object)null)
		{
			return;
		}
		__result.RemoveAll(delegate(Vector3 position)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = default(Vector3);
			Biome biome = default(Biome);
			BiomeArea val2 = default(BiomeArea);
			Heightmap val3 = default(Heightmap);
			ZoneSystem.instance.GetGroundData(ref position, ref val, ref biome, ref val2, ref val3);
			int tier = BiomeUtils.GetTier(biome);
			if (tier < 0)
			{
				return false;
			}
			foreach (SpawnData item in ev.m_spawn)
			{
				if (!((Object)(object)item?.m_prefab == (Object)null) && !string.IsNullOrEmpty(item.m_requiredGlobalKey) && SpawnSystemNativeBiomePatch.TryGetNativeTier(((Object)item.m_prefab).name, out var nativeTier))
				{
					int num = nativeTier - tier;
					if (num > 2)
					{
						Plugin.Log.LogInfo((object)("BLOCKED RAID " + ev.m_name + " | " + ((Object)item.m_prefab).name + " | " + BiomeUtils.GetName(nativeTier) + " -> " + BiomeUtils.GetName(tier) + " | " + BiomeUtils.FormatDifference(num)));
						return true;
					}
				}
			}
			return false;
		});
	}
}