Decompiled source of WiresAltBiomeInfo v0.1.6

WiresAltBiomeInfo.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("WiresAltBiomeInfo")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WiresAltBiomeInfo")]
[assembly: AssemblyTitle("WiresAltBiomeInfo")]
[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;
		}
	}
}
namespace WiresAltBiomeInfo
{
	[BepInPlugin("wire.valheim.altbiomeinfo", "Wire's Alt Biome Info", "0.1.6")]
	[BepInProcess("valheim.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "wire.valheim.altbiomeinfo";

		public const string Name = "Wire's Alt Biome Info";

		public const string Version = "0.1.6";

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Expected O, but got Unknown
			TranslationFiles.Initialize(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), ((BaseUnityPlugin)this).Config);
			_harmony = new Harmony("wire.valheim.altbiomeinfo");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Wire's Alt Biome Info 0.1.6 loaded.");
		}

		private void OnDestroy()
		{
			TranslationFiles.Shutdown();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class TranslationFiles
	{
		private const string DefaultFormat = "{modifiers} {biome}";

		private static readonly Dictionary<string, string> BuiltInEnglish = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
		{
			{ "Mushroom", "Fungal" },
			{ "Lantern", "Lantern" },
			{ "Bones", "Skeletal" },
			{ "Menhir", "Menhir" },
			{ "Dark Meadows", "Dark" },
			{ "Peaceful Meadows", "Peaceful" },
			{ "Dandelion Meadows", "Dandelion" },
			{ "Raspberry Meadows", "Bountiful" },
			{ "Smalltree Meadows", "Sapling" },
			{ "Birch Meadows", "Birch" },
			{ "Troll Black Forest", "Trollish" },
			{ "Root Black Forest", "Rooty" },
			{ "Ruin Black Forest", "Ruined" },
			{ "Rock Black Forest", "Rocky" },
			{ "Pinetree Black Forest", "Piney" },
			{ "Blueberry Black Forest", "Bountiful" },
			{ "Kalhygge Black Forest", "Kalhygge" },
			{ "Hut Swamp", "Hut" },
			{ "Bog Swamp", "Boggy" },
			{ "Bat Swamp", "Swarming" },
			{ "Abomination Swamp", "Abominable" },
			{ "Wolf Mountain", "Lupine" },
			{ "Drake Mountain", "Drake" },
			{ "Fortress Mountain", "Fortified" },
			{ "Lox Plains", "Teeming" },
			{ "Goblin Plains", "Overrun" },
			{ "Death Plains", "Death" },
			{ "Rockless Mistlands", "Rockless" },
			{ "Trees Mistlands", "Sylvan" },
			{ "Swords Mistlands", "Gladial" },
			{ "Hare Mistlands", "Teeming" },
			{ "BroodSwarm Mistlands", "Infested" }
		};

		private static readonly Dictionary<string, string> Values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

		private static string _pluginDirectory;

		private static string _translationDirectory;

		private static string _nameFormat = "{modifiers} {biome}";

		private static ConfigEntry<string> _language;

		internal static void Initialize(string pluginDirectory, ConfigFile config)
		{
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Expected O, but got Unknown
			_pluginDirectory = pluginDirectory ?? "";
			_translationDirectory = Path.Combine(_pluginDirectory, "translations");
			List<string> list = new List<string> { "English" };
			AddLanguagesFromDirectory(_translationDirectory, list);
			AddLanguagesFromDirectory(_pluginDirectory, list);
			list.Sort(StringComparer.OrdinalIgnoreCase);
			_language = config.Bind<string>("Translations", "Language", "English", new ConfigDescription("Language overlay to load. English defaults are built into the DLL.", (AcceptableValueBase)(object)new AcceptableValueList<string>(list.ToArray()), Array.Empty<object>()));
			_language.SettingChanged += OnLanguageChanged;
			LoadSelectedLanguage();
		}

		internal static void Shutdown()
		{
			if (_language != null)
			{
				_language.SettingChanged -= OnLanguageChanged;
			}
		}

		private static void OnLanguageChanged(object sender, EventArgs args)
		{
			LoadSelectedLanguage();
		}

		internal static void LoadSelectedLanguage()
		{
			Values.Clear();
			_nameFormat = "{modifiers} {biome}";
			foreach (KeyValuePair<string, string> item in BuiltInEnglish)
			{
				Values[item.Key] = item.Value;
			}
			string text = ((_language != null) ? _language.Value : "English");
			if (!string.IsNullOrWhiteSpace(text) && !string.Equals(text, "English", StringComparison.OrdinalIgnoreCase))
			{
				string text2 = FindLanguageFile(text);
				if (!string.IsNullOrWhiteSpace(text2))
				{
					LoadFile(text2);
				}
			}
		}

		internal static string Get(string key, string fallback)
		{
			if (string.IsNullOrWhiteSpace(key))
			{
				return fallback ?? "";
			}
			if (Values.TryGetValue(key.Trim(), out var value) && !string.IsNullOrWhiteSpace(value))
			{
				return value.Trim();
			}
			return fallback ?? "";
		}

		internal static string FormatName(string modifiers, string biome)
		{
			return (string.IsNullOrWhiteSpace(_nameFormat) ? "{modifiers} {biome}" : _nameFormat).Replace("{modifiers}", modifiers ?? "").Replace("{biome}", biome ?? "");
		}

		private static void AddLanguagesFromDirectory(string directory, List<string> languages)
		{
			if (string.IsNullOrWhiteSpace(directory) || !Directory.Exists(directory))
			{
				return;
			}
			string[] files = Directory.GetFiles(directory, "*.txt");
			for (int i = 0; i < files.Length; i++)
			{
				string language = Path.GetFileNameWithoutExtension(files[i]);
				if (!string.IsNullOrWhiteSpace(language) && !languages.Exists((string value) => string.Equals(value, language, StringComparison.OrdinalIgnoreCase)))
				{
					languages.Add(language);
				}
			}
		}

		private static string FindLanguageFile(string language)
		{
			string fileName = Path.GetFileName(language);
			string text = Path.Combine(_translationDirectory ?? "", fileName + ".txt");
			if (File.Exists(text))
			{
				return text;
			}
			string text2 = Path.Combine(_pluginDirectory ?? "", fileName + ".txt");
			if (!File.Exists(text2))
			{
				return null;
			}
			return text2;
		}

		private static void LoadFile(string path)
		{
			if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
			{
				return;
			}
			string[] array = File.ReadAllLines(path);
			foreach (string text in array)
			{
				if (string.IsNullOrWhiteSpace(text))
				{
					continue;
				}
				string text2 = text.Trim();
				if (text2.StartsWith("#"))
				{
					continue;
				}
				int num = text2.IndexOf('=');
				if (num <= 0)
				{
					continue;
				}
				string text3 = text2.Substring(0, num).Trim();
				string text4 = text2.Substring(num + 1).Trim();
				if (string.Equals(text3, "Format", StringComparison.OrdinalIgnoreCase))
				{
					if (!string.IsNullOrWhiteSpace(text4))
					{
						_nameFormat = text4;
					}
				}
				else if (!string.IsNullOrWhiteSpace(text3))
				{
					Values[text3] = text4;
				}
			}
		}
	}
	internal static class BiomeSubtypeNames
	{
		private static readonly Dictionary<string, string> SingularModifierWords = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
		{
			{ "Tree", "Wooded" },
			{ "Trees", "Wooded" },
			{ "Rock", "Rocky" },
			{ "Rocks", "Rocky" },
			{ "Bone", "Bone-strewn" },
			{ "Bones", "Bone-strewn" },
			{ "Bat", "Bat-haunted" },
			{ "Bats", "Bat-haunted" },
			{ "Abomination", "Abominable" },
			{ "Abominations", "Abominable" },
			{ "Blueberry", "Blueberry-rich" },
			{ "Blueberries", "Blueberry-rich" },
			{ "Pinetree", "Pine-clad" },
			{ "Pinetrees", "Pine-clad" },
			{ "Pines", "Pine-clad" },
			{ "Birch", "Birch-clad" },
			{ "Birches", "Birch-clad" },
			{ "Oak", "Oak-clad" },
			{ "Oaks", "Oak-clad" },
			{ "Mushroom", "Fungal" },
			{ "Mushrooms", "Fungal" },
			{ "Drake", "Drake" },
			{ "Drakes", "Drake" },
			{ "Wolf", "Wolven" },
			{ "Wolves", "Wolven" },
			{ "Troll", "Troll-haunted" },
			{ "Trolls", "Troll-haunted" },
			{ "Skeleton", "Skeleton-haunted" },
			{ "Skeletons", "Skeleton-haunted" },
			{ "Draugr", "Draugr-haunted" },
			{ "Draugrs", "Draugr-haunted" },
			{ "Greydwarf", "Greydwarf-haunted" },
			{ "Greydwarfs", "Greydwarf-haunted" },
			{ "Greydwarves", "Greydwarf-haunted" }
		};

		internal static string Get(BiomeSector sector)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			if (sector == null)
			{
				return "";
			}
			string text = Localize(BiomeSector.GetBiomeName(sector.Biome));
			string biome = text;
			List<string> list = new List<string>();
			List<string> list2 = new List<string>();
			if (sector.AltBiomes != null)
			{
				for (int i = 0; i < sector.AltBiomes.Count; i++)
				{
					AltBiome val = sector.AltBiomes[i];
					if (val != null)
					{
						bool flag = !string.IsNullOrWhiteSpace(val.m_nameOverride);
						bool flag2 = !string.IsNullOrWhiteSpace(val.m_namePrefix);
						bool flag3 = !string.IsNullOrWhiteSpace(val.m_nameSuffix);
						if (flag)
						{
							biome = CollapseWhitespace(Localize(val.m_nameOverride));
						}
						if (flag2)
						{
							AddUnique(list, NormalizeModifier(val.m_namePrefix, text));
						}
						else if (!flag && !flag3 && !string.IsNullOrWhiteSpace(val.m_name))
						{
							AddUnique(list, NormalizeModifier(val.m_name, text));
						}
						if (flag3)
						{
							AddUnique(list2, NormalizeModifier(val.m_nameSuffix, text));
						}
					}
				}
			}
			string modifiers = string.Join(" ", list);
			StringBuilder stringBuilder = new StringBuilder();
			AppendPart(stringBuilder, TranslationFiles.FormatName(modifiers, biome));
			for (int j = 0; j < list2.Count; j++)
			{
				AppendPart(stringBuilder, list2[j]);
			}
			return CollapseWhitespace(stringBuilder.ToString());
		}

		private static string NormalizeModifier(string raw, string baseName)
		{
			if (string.IsNullOrWhiteSpace(raw))
			{
				return "";
			}
			string text = raw.Trim();
			string text2 = CollapseWhitespace(PrettyInternalName(text.StartsWith("$") ? Localize(text) : text));
			text2 = StripBiomeName(text2, baseName);
			text2 = StripKnownEnglishBiomeName(text2);
			if (SingularModifierWords.TryGetValue(text2, out var value))
			{
				text2 = value;
			}
			return TranslationFiles.Get(text, text2);
		}

		private static string StripKnownEnglishBiomeName(string text)
		{
			string text2 = CollapseWhitespace(text);
			string[] array = new string[10] { "Black Forest", "Deep North", "Meadows", "Swamp", "Mountain", "Plains", "Ashlands", "Ash Lands", "Ocean", "Mistlands" };
			for (int i = 0; i < array.Length; i++)
			{
				text2 = StripBiomeName(text2, array[i]);
			}
			return text2;
		}

		private static string StripBiomeName(string text, string baseName)
		{
			string text2 = CollapseWhitespace(text);
			string text3 = CollapseWhitespace(baseName);
			if (string.IsNullOrEmpty(text2) || string.IsNullOrEmpty(text3))
			{
				return text2;
			}
			bool flag;
			do
			{
				flag = false;
				if (string.Equals(text2, text3, StringComparison.OrdinalIgnoreCase))
				{
					return "";
				}
				string text4 = " " + text3;
				if (text2.EndsWith(text4, StringComparison.OrdinalIgnoreCase))
				{
					text2 = text2.Substring(0, text2.Length - text4.Length).Trim();
					flag = true;
					continue;
				}
				string text5 = text3 + " ";
				if (text2.StartsWith(text5, StringComparison.OrdinalIgnoreCase))
				{
					text2 = text2.Substring(text5.Length).Trim();
					flag = true;
				}
			}
			while (flag);
			return text2;
		}

		private static void AddUnique(List<string> parts, string part)
		{
			if (string.IsNullOrWhiteSpace(part))
			{
				return;
			}
			for (int i = 0; i < parts.Count; i++)
			{
				if (string.Equals(parts[i], part, StringComparison.OrdinalIgnoreCase))
				{
					return;
				}
			}
			parts.Add(part);
		}

		private static void AppendPart(StringBuilder builder, string part)
		{
			if (!string.IsNullOrWhiteSpace(part))
			{
				if (builder.Length > 0)
				{
					builder.Append(' ');
				}
				builder.Append(part.Trim());
			}
		}

		private static string Localize(string value)
		{
			if (string.IsNullOrWhiteSpace(value))
			{
				return "";
			}
			if (Localization.instance == null)
			{
				return value.Trim();
			}
			return Localization.instance.Localize(value.Trim());
		}

		private static string PrettyInternalName(string value)
		{
			if (string.IsNullOrWhiteSpace(value))
			{
				return "";
			}
			string text = value.Trim().Replace('_', ' ').Replace('-', ' ');
			StringBuilder stringBuilder = new StringBuilder(text.Length + 8);
			for (int i = 0; i < text.Length; i++)
			{
				char c = text[i];
				if (i > 0 && char.IsUpper(c) && char.IsLower(text[i - 1]) && text[i - 1] != ' ')
				{
					stringBuilder.Append(' ');
				}
				stringBuilder.Append(c);
			}
			return CollapseWhitespace(stringBuilder.ToString());
		}

		private static string CollapseWhitespace(string value)
		{
			if (string.IsNullOrWhiteSpace(value))
			{
				return "";
			}
			string[] value2 = value.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries);
			return string.Join(" ", value2);
		}
	}
	[HarmonyPatch(typeof(BiomeSector), "GetName")]
	internal static class BiomeSectorNamePatch
	{
		[HarmonyPostfix]
		private static void Postfix(BiomeSector __instance, bool debug, ref string __result)
		{
			if (!debug && __instance != null && __instance.AltBiomes != null && __instance.AltBiomes.Count != 0)
			{
				__result = BiomeSubtypeNames.Get(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(Minimap), "Start")]
	internal static class MinimapLayoutPatch
	{
		[HarmonyPostfix]
		private static void Postfix(Minimap __instance)
		{
			if (!((Object)(object)__instance == (Object)null))
			{
				ConfigureSmall(__instance.m_biomeNameSmall);
				ConfigureLarge(__instance.m_biomeNameLarge);
			}
		}

		private static void ConfigureSmall(TMP_Text text)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)text == (Object)null))
			{
				text.enableAutoSizing = false;
				text.textWrappingMode = (TextWrappingModes)1;
				text.overflowMode = (TextOverflowModes)0;
				text.alignment = (TextAlignmentOptions)260;
				Vector4 margin = text.margin;
				margin.y = Mathf.Max(0f, margin.y);
				margin.w = Mathf.Max(0f, margin.w);
				text.margin = margin;
				RectTransform rectTransform = text.rectTransform;
				if ((Object)(object)rectTransform != (Object)null)
				{
					Vector2 offsetMin = rectTransform.offsetMin;
					offsetMin.y -= 56f;
					rectTransform.offsetMin = offsetMin;
				}
			}
		}

		private static void ConfigureLarge(TMP_Text text)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)text == (Object)null))
			{
				text.enableAutoSizing = false;
				text.textWrappingMode = (TextWrappingModes)0;
				text.overflowMode = (TextOverflowModes)0;
				text.horizontalAlignment = (HorizontalAlignmentOptions)4;
				Vector4 margin = text.margin;
				margin.x = 0f;
				margin.z = 0f;
				text.margin = margin;
				RectTransform rectTransform = text.rectTransform;
				if ((Object)(object)rectTransform != (Object)null)
				{
					Vector2 offsetMin = rectTransform.offsetMin;
					offsetMin.x -= 4096f;
					rectTransform.offsetMin = offsetMin;
				}
			}
		}
	}
	[HarmonyPatch(typeof(MessageHud), "UpdateBiomeFound")]
	internal static class BiomeFoundBannerLayoutPatch
	{
		private static int _configuredInstanceId = int.MinValue;

		[HarmonyPostfix]
		private static void Postfix(GameObject ___m_biomeMsgInstance)
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)___m_biomeMsgInstance == (Object)null)
			{
				_configuredInstanceId = int.MinValue;
				return;
			}
			int instanceID = ((Object)___m_biomeMsgInstance).GetInstanceID();
			if (instanceID == _configuredInstanceId)
			{
				return;
			}
			Transform val = Utils.FindChild(___m_biomeMsgInstance.transform, "Title", (IterativeSearchType)0);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			TMP_Text component = ((Component)val).GetComponent<TMP_Text>();
			if (!((Object)(object)component == (Object)null))
			{
				component.enableAutoSizing = false;
				component.textWrappingMode = (TextWrappingModes)0;
				component.overflowMode = (TextOverflowModes)0;
				component.horizontalAlignment = (HorizontalAlignmentOptions)2;
				Vector4 margin = component.margin;
				margin.x = 0f;
				margin.z = 0f;
				component.margin = margin;
				RectTransform rectTransform = component.rectTransform;
				if ((Object)(object)rectTransform != (Object)null)
				{
					Vector2 anchorMin = rectTransform.anchorMin;
					Vector2 anchorMax = rectTransform.anchorMax;
					Vector2 pivot = rectTransform.pivot;
					Vector2 anchoredPosition = rectTransform.anchoredPosition;
					Vector2 sizeDelta = rectTransform.sizeDelta;
					anchorMin.x = 0.5f;
					anchorMax.x = 0.5f;
					pivot.x = 0.5f;
					anchoredPosition.x = 0f;
					sizeDelta.x = 4096f;
					rectTransform.anchorMin = anchorMin;
					rectTransform.anchorMax = anchorMax;
					rectTransform.pivot = pivot;
					rectTransform.anchoredPosition = anchoredPosition;
					rectTransform.sizeDelta = sizeDelta;
				}
				_configuredInstanceId = instanceID;
			}
		}
	}
	[HarmonyPatch(typeof(Minimap), "OnLanguageChange")]
	internal static class MinimapLanguagePatch
	{
		[HarmonyPostfix]
		private static void Postfix(Minimap __instance)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Invalid comparison between Unknown and I4
			if (!((Object)(object)__instance == (Object)null) && !((Object)(object)__instance.m_biomeNameSmall == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null))
			{
				BiomeSector currentBiomeData = Player.m_localPlayer.GetCurrentBiomeData();
				__instance.m_biomeNameSmall.text = BiomeSubtypeNames.Get(currentBiomeData);
				if ((int)__instance.m_mode != 2 && (Object)(object)__instance.m_biomeNameLarge != (Object)null)
				{
					__instance.m_biomeNameLarge.text = BiomeSubtypeNames.Get(currentBiomeData);
				}
			}
		}
	}
}