Decompiled source of BiomeObserver v1.0.4

BiomeObserver.dll

Decompiled 7 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("BiomeObserver")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("BiomeObserver")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("E0E2F92E-557C-4A05-9D89-AA92A0BD75C4")]
[assembly: AssemblyFileVersion("1.0.4")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.0")]
[module: UnverifiableCode]
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;
		}
	}
}
namespace BiomeObserver
{
	[BepInPlugin("Azumatt.BiomeObserver", "BiomeObserver", "1.0.4")]
	public class BiomeObserverPlugin : BaseUnityPlugin
	{
		public enum Toggle
		{
			On = 1,
			Off = 0
		}

		private class ConfigurationManagerAttributes
		{
			[UsedImplicitly]
			public int? Order;

			[UsedImplicitly]
			public bool? Browsable;

			[UsedImplicitly]
			public string? Category;

			[UsedImplicitly]
			public Action<ConfigEntryBase>? CustomDrawer;
		}

		internal const string ModName = "BiomeObserver";

		internal const string ModVersion = "1.0.4";

		internal const string Author = "Azumatt";

		private const string ModGUID = "Azumatt.BiomeObserver";

		private static string ConfigFileName = "Azumatt.BiomeObserver.cfg";

		private static string ConfigFileFullPath;

		internal static string ConnectionError;

		private readonly Harmony _harmony = new Harmony("Azumatt.BiomeObserver");

		public static readonly ManualLogSource BiomeObserverLogger;

		internal static ConfigEntry<Toggle> modEnabled;

		internal static ConfigEntry<Toggle> playSound;

		internal static ConfigEntry<float> cooldownTime;

		public void Awake()
		{
			modEnabled = config("General", "Enabled", Toggle.On, "Enable this mod");
			playSound = config("General", "Play Biome Found Sound", Toggle.On, "Allow playing the sound when the biome message is shown");
			cooldownTime = config("General", "Cooldown Time", 5f, "Cooldown time between biome message displays in seconds");
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony.PatchAll(executingAssembly);
			SetupWatcher();
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				BiomeObserverLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				BiomeObserverLogger.LogError((object)("There was an issue loading your " + ConfigFileName));
				BiomeObserverLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, ConfigDescription description)
		{
			return ((BaseUnityPlugin)this).Config.Bind<T>(group, name, value, description);
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, string description)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			return config(group, name, value, new ConfigDescription(description, (AcceptableValueBase)null, Array.Empty<object>()));
		}

		static BiomeObserverPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			ConnectionError = "";
			BiomeObserverLogger = Logger.CreateLogSource("BiomeObserver");
			modEnabled = null;
			playSound = null;
			cooldownTime = null;
		}
	}
	[HarmonyPatch(typeof(Player), "AddKnownBiome")]
	internal static class PlayerAddKnownBiomePatch
	{
		private static Dictionary<string, DateTime> _lastMessageTime = new Dictionary<string, DateTime>();

		private static void Postfix(Player __instance, Biome biome)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			if (BiomeObserverPlugin.modEnabled.Value == BiomeObserverPlugin.Toggle.On && (int)biome != 0)
			{
				string text = "$biome_" + ((object)(Biome)(ref biome)).ToString().ToLower();
				if (!_lastMessageTime.ContainsKey(text) || (DateTime.UtcNow - _lastMessageTime[text]).TotalSeconds >= (double)BiomeObserverPlugin.cooldownTime.Value)
				{
					MessageHud.instance.ShowBiomeFoundMsg(text, BiomeObserverPlugin.playSound.Value == BiomeObserverPlugin.Toggle.On);
					_lastMessageTime[text] = DateTime.UtcNow;
				}
			}
		}
	}
}