Decompiled source of Misophonia Friendly v1.0.44

plugins/Digitalroot.Valheim.MisophoniaFriendly.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using Digitalroot.Valheim.Common;
using Digitalroot.Valheim.Common.Json;
using HarmonyLib;
using JetBrains.Annotations;
using Jotunn.Managers;
using Jotunn.Utils;
using SimpleJson;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Digitalroot.Valheim.MisophoniaFriendly")]
[assembly: AssemblyDescription("Digitalroot Misophonia Friendly")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Digitalroot Technologies")]
[assembly: AssemblyProduct("Digitalroot Valheim Mods")]
[assembly: AssemblyCopyright("Copyright © Digitalroot Technologies 2021 - 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3ccc4dc1-5b17-47c1-b996-ca03b8639a61")]
[assembly: AssemblyFileVersion("1.0.44")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.44.0")]
[module: UnverifiableCode]
namespace Digitalroot.Valheim.MisophoniaFriendly
{
	[BepInPlugin("digitalroot.mods.misophoniafriendly", "Digitalroot Misophonia Friendly", "1.0.44")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class Main : BaseUnityPlugin, ITraceableLogging
	{
		private enum PrefabActionType
		{
			All,
			Mute,
			Unmute
		}

		private enum AudioActionType
		{
			Mute,
			Unmute
		}

		public static Main Instance;

		public readonly ConfigEntry<int> NexusId;

		public ConfigEntry<bool> MuteSfxPukeMale;

		public ConfigEntry<bool> MuteSfxPukeFemale;

		public ConfigEntry<bool> MuteSfxCreatureConsume;

		public ConfigEntry<bool> MuteSfxEat;

		public ConfigEntry<bool> MuteSfxChickenEat;

		public ConfigEntry<bool> MuteSfxHareIdleEating;

		public ConfigEntry<bool> MuteSfxPickaxeHit;

		public ConfigEntry<bool> MuteSfxPickaxeSwing;

		public ConfigEntry<bool> MuteSfxRockHit;

		public ConfigEntry<bool> MuteSfxRockDestroyed;

		public const string Version = "1.0.44";

		public const string Name = "Digitalroot Misophonia Friendly";

		public const string Guid = "digitalroot.mods.misophoniafriendly";

		public const string Namespace = "Digitalroot.Valheim.MisophoniaFriendly";

		public string Source => "Digitalroot.Valheim.MisophoniaFriendly";

		public bool EnableTrace { get; }

		public Main()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			Instance = this;
			NexusId = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 1667, new ConfigDescription("Nexus mod ID for updates.", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = false,
				ReadOnly = true
			} }));
			InitConfigs();
			EnableTrace = false;
			Log.RegisterSource(Instance);
			Log.Trace(Instance, "Digitalroot.Valheim.MisophoniaFriendly." + MethodBase.GetCurrentMethod()?.DeclaringType?.Name + "." + MethodBase.GetCurrentMethod()?.Name);
		}

		[UsedImplicitly]
		private void Awake()
		{
			try
			{
				Log.Trace(Instance, "Digitalroot.Valheim.MisophoniaFriendly." + MethodBase.GetCurrentMethod()?.DeclaringType?.Name + "." + MethodBase.GetCurrentMethod()?.Name);
				if (!Utils.IsHeadless())
				{
					PrefabManager.OnVanillaPrefabsAvailable += PrefabManagerOnVanillaPrefabsAvailable;
				}
			}
			catch (Exception e)
			{
				Log.Error(Instance, e);
			}
		}

		[HarmonyWrapSafe]
		private void PrefabManagerOnVanillaPrefabsAvailable()
		{
			try
			{
				Log.Trace(Instance, "Digitalroot.Valheim.MisophoniaFriendly." + MethodBase.GetCurrentMethod()?.DeclaringType?.Name + "." + MethodBase.GetCurrentMethod()?.Name);
				MuteSounds();
			}
			catch (Exception e)
			{
				Log.Error(Instance, e);
			}
			finally
			{
				PrefabManager.OnVanillaPrefabsAvailable += PrefabManagerOnVanillaPrefabsAvailable;
			}
		}

		private void MuteSounds()
		{
			ToggleSounds(AudioActionType.Mute);
		}

		private void UnMuteSounds()
		{
			ToggleSounds(AudioActionType.Unmute);
		}

		private void ToggleSounds(AudioActionType actionType)
		{
			try
			{
				Log.Trace(Instance, "Digitalroot.Valheim.MisophoniaFriendly." + MethodBase.GetCurrentMethod()?.DeclaringType?.Name + "." + MethodBase.GetCurrentMethod()?.Name);
				IEnumerable<string> prefabs;
				string text;
				bool mute;
				switch (actionType)
				{
				default:
					return;
				case AudioActionType.Mute:
					prefabs = GetPrefabs(PrefabActionType.Mute);
					text = "Muting";
					mute = true;
					break;
				case AudioActionType.Unmute:
					prefabs = GetPrefabs(PrefabActionType.Unmute);
					text = "Unmuting";
					mute = false;
					break;
				}
				foreach (string item in prefabs)
				{
					Log.Trace(Instance, text + " " + item);
					Cache.GetPrefab<GameObject>(item).GetComponent<AudioSource>().mute = mute;
				}
			}
			catch (Exception e)
			{
				Log.Error(Instance, e);
			}
		}

		private IEnumerable<string> GetPrefabs(PrefabActionType actionType)
		{
			switch (actionType)
			{
			case PrefabActionType.All:
				yield return "sfx_Puke_male";
				yield return "sfx_Puke_female";
				yield return "sfx_creature_consume";
				yield return "sfx_eat";
				yield return "sfx_chicken_eat";
				yield return "sfx_hare_idle_eating";
				yield return "sfx_pickaxe_hit";
				yield return "sfx_pickaxe_swing";
				yield return "sfx_rock_hit";
				yield return "sfx_rock_destroyed";
				break;
			case PrefabActionType.Mute:
				if (MuteSfxPukeMale.Value)
				{
					yield return "sfx_Puke_male";
				}
				if (MuteSfxPukeFemale.Value)
				{
					yield return "sfx_Puke_female";
				}
				if (MuteSfxCreatureConsume.Value)
				{
					yield return "sfx_creature_consume";
				}
				if (MuteSfxEat.Value)
				{
					yield return "sfx_eat";
				}
				if (MuteSfxChickenEat.Value)
				{
					yield return "sfx_chicken_eat";
				}
				if (MuteSfxHareIdleEating.Value)
				{
					yield return "sfx_hare_idle_eating";
				}
				if (MuteSfxPickaxeHit.Value)
				{
					yield return "sfx_pickaxe_hit";
				}
				if (MuteSfxPickaxeSwing.Value)
				{
					yield return "sfx_pickaxe_swing";
				}
				if (MuteSfxRockHit.Value)
				{
					yield return "sfx_rock_hit";
				}
				if (MuteSfxRockDestroyed.Value)
				{
					yield return "sfx_rock_destroyed";
				}
				break;
			case PrefabActionType.Unmute:
				if (!MuteSfxPukeMale.Value)
				{
					yield return "sfx_Puke_male";
				}
				if (!MuteSfxPukeFemale.Value)
				{
					yield return "sfx_Puke_female";
				}
				if (!MuteSfxCreatureConsume.Value)
				{
					yield return "sfx_creature_consume";
				}
				if (!MuteSfxEat.Value)
				{
					yield return "sfx_eat";
				}
				if (!MuteSfxChickenEat.Value)
				{
					yield return "sfx_chicken_eat";
				}
				if (!MuteSfxHareIdleEating.Value)
				{
					yield return "sfx_hare_idle_eating";
				}
				if (!MuteSfxPickaxeHit.Value)
				{
					yield return "sfx_pickaxe_hit";
				}
				if (!MuteSfxPickaxeSwing.Value)
				{
					yield return "sfx_pickaxe_swing";
				}
				if (!MuteSfxRockHit.Value)
				{
					yield return "sfx_rock_hit";
				}
				if (!MuteSfxRockDestroyed.Value)
				{
					yield return "sfx_rock_destroyed";
				}
				break;
			default:
				throw new ArgumentOutOfRangeException("actionType", actionType, null);
			}
		}

		private void InitConfigs()
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: 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_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Expected O, but got Unknown
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Expected O, but got Unknown
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Expected O, but got Unknown
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Expected O, but got Unknown
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Expected O, but got Unknown
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Expected O, but got Unknown
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Expected O, but got Unknown
			//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Expected O, but got Unknown
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fb: Expected O, but got Unknown
			//IL_0237: Unknown result type (might be due to invalid IL or missing references)
			//IL_023c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0243: Unknown result type (might be due to invalid IL or missing references)
			//IL_024f: Unknown result type (might be due to invalid IL or missing references)
			//IL_025c: Expected O, but got Unknown
			//IL_025c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0266: Expected O, but got Unknown
			//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c7: Expected O, but got Unknown
			//IL_02c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d1: Expected O, but got Unknown
			//IL_030d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0312: Unknown result type (might be due to invalid IL or missing references)
			//IL_0319: Unknown result type (might be due to invalid IL or missing references)
			//IL_0325: Unknown result type (might be due to invalid IL or missing references)
			//IL_0332: Expected O, but got Unknown
			//IL_0332: Unknown result type (might be due to invalid IL or missing references)
			//IL_033c: Expected O, but got Unknown
			//IL_0378: Unknown result type (might be due to invalid IL or missing references)
			//IL_037d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0384: Unknown result type (might be due to invalid IL or missing references)
			//IL_0390: Unknown result type (might be due to invalid IL or missing references)
			//IL_039d: Expected O, but got Unknown
			//IL_039d: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a7: Expected O, but got Unknown
			//IL_03e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_03fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0408: Expected O, but got Unknown
			//IL_0408: Unknown result type (might be due to invalid IL or missing references)
			//IL_0412: Expected O, but got Unknown
			MuteSfxPukeMale = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxPukeMale", true, new ConfigDescription("Mute Sfx Puke Male", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxPukeMale.SettingChanged += ModConfig_SettingChanged;
			MuteSfxPukeFemale = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxPukeFemale", true, new ConfigDescription("Mute Sfx Puke Female", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxPukeFemale.SettingChanged += ModConfig_SettingChanged;
			MuteSfxCreatureConsume = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxCreatureConsume", true, new ConfigDescription("Mute Sfx Creature Consume", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxCreatureConsume.SettingChanged += ModConfig_SettingChanged;
			MuteSfxEat = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxEat", true, new ConfigDescription("Mute Sfx Eat", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxEat.SettingChanged += ModConfig_SettingChanged;
			MuteSfxChickenEat = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxChickenEat", true, new ConfigDescription("Mute Sfx Chicken Eat", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxChickenEat.SettingChanged += ModConfig_SettingChanged;
			MuteSfxHareIdleEating = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxHareIdleEating", true, new ConfigDescription("Mute Sfx Hare Idle Eating", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxHareIdleEating.SettingChanged += ModConfig_SettingChanged;
			MuteSfxPickaxeHit = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxPickaxeHit", true, new ConfigDescription("Mute Sfx Pickaxe Hit", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxPickaxeHit.SettingChanged += ModConfig_SettingChanged;
			MuteSfxPickaxeSwing = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxPickaxeSwing", true, new ConfigDescription("Mute Sfx Pickaxe Swing", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxPickaxeSwing.SettingChanged += ModConfig_SettingChanged;
			MuteSfxRockHit = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxRockHit", true, new ConfigDescription("Mute Rock Hit", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxRockHit.SettingChanged += ModConfig_SettingChanged;
			MuteSfxRockDestroyed = ((BaseUnityPlugin)this).Config.Bind<bool>("Toggles", "MuteSfxRockDestroyed", true, new ConfigDescription("Mute Sfx Rock Destroyed", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = false,
				Browsable = true,
				ReadOnly = false
			} }));
			MuteSfxRockDestroyed.SettingChanged += ModConfig_SettingChanged;
		}

		private void ModConfig_SettingChanged(object sender, EventArgs e)
		{
			UnMuteSounds();
			MuteSounds();
		}
	}
}
namespace Digitalroot.Valheim.Common
{
	internal interface ITraceableLogging
	{
		string Source { get; }

		bool EnableTrace { get; }
	}
	internal sealed class Log
	{
		private static readonly Dictionary<string, TraceLogger> TraceLoggers;

		[UsedImplicitly]
		private static Log Instance { get; }

		static Log()
		{
			TraceLoggers = new Dictionary<string, TraceLogger>();
			Instance = new Log();
		}

		private Log()
		{
			TraceLoggers.Add("Digitalroot", new TraceLogger("Digitalroot", enableTrace: false));
		}

		public static void RegisterSource(ITraceableLogging sender)
		{
			if ((!TraceLoggers.ContainsKey(sender.Source) || TraceLoggers[sender.Source].IsTraceEnabled != sender.EnableTrace) && (!TraceLoggers.ContainsKey(sender.Source) || sender.EnableTrace))
			{
				if (TraceLoggers.ContainsKey(sender.Source) && sender.EnableTrace)
				{
					TraceLoggers[sender.Source].EnableTrace();
				}
				else
				{
					TraceLoggers.Add(sender.Source, new TraceLogger(sender.Source, sender.EnableTrace));
				}
			}
		}

		private static TraceLogger GetTraceLogger(ITraceableLogging sender)
		{
			if (!TraceLoggers.ContainsKey(sender.Source))
			{
				return TraceLoggers["Digitalroot"];
			}
			return TraceLoggers[sender.Source];
		}

		[UsedImplicitly]
		public static void SetEnableTrace(ITraceableLogging sender, bool value)
		{
			if (value)
			{
				GetTraceLogger(sender).EnableTrace();
			}
			else
			{
				GetTraceLogger(sender).DisableTrace();
			}
		}

		[UsedImplicitly]
		public static void SetEnableTraceForAllLoggers(bool value)
		{
			foreach (TraceLogger value2 in TraceLoggers.Values)
			{
				if (value)
				{
					value2.EnableTrace();
				}
				else
				{
					value2.DisableTrace();
				}
			}
		}

		[UsedImplicitly]
		public static void Debug(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogDebug(value);
		}

		[UsedImplicitly]
		public static void Error(ITraceableLogging sender, Exception e, int i = 1)
		{
			Error(sender, "Message: " + e.Message);
			Error(sender, $"TargetSite: {e.TargetSite}");
			Error(sender, "StackTrace: " + e.StackTrace);
			Error(sender, "Source: " + e.Source);
			if (e.Data.Count > 0)
			{
				foreach (object key in e.Data.Keys)
				{
					Error(sender, $"key: {key}, value: {e.Data[key]}");
				}
			}
			if (e.InnerException != null)
			{
				Error(sender, $"--- InnerException [{i}][Start] ---");
				Error(sender, e.InnerException, ++i);
			}
		}

		[UsedImplicitly]
		public static void Error(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogError(value);
		}

		[UsedImplicitly]
		public static void Info(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogInfo(value);
		}

		[UsedImplicitly]
		public static void Fatal(ITraceableLogging sender, Exception e, int i = 1)
		{
			Fatal(sender, "Message: " + e.Message);
			Fatal(sender, $"TargetSite: {e.TargetSite}");
			Fatal(sender, "StackTrace: " + e.StackTrace);
			Fatal(sender, "Source: " + e.Source);
			if (e.Data.Count > 0)
			{
				foreach (object key in e.Data.Keys)
				{
					Fatal(sender, $"key: {key}, value: {e.Data[key]}");
				}
			}
			if (e.InnerException != null)
			{
				Fatal(sender, $"--- InnerException [{i}][Start] ---");
				Fatal(sender, e.InnerException, ++i);
			}
		}

		[UsedImplicitly]
		public static void Fatal(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogFatal(value);
		}

		[UsedImplicitly]
		public static void Message(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogMessage(value);
		}

		[UsedImplicitly]
		public static void Trace(ITraceableLogging sender, object value)
		{
			if (GetTraceLogger(sender).IsTraceEnabled || sender.EnableTrace)
			{
				GetTraceLogger(sender).LoggerRef.Log((LogLevel)63, value);
			}
		}

		[UsedImplicitly]
		public static void Warning(ITraceableLogging sender, object value)
		{
			GetTraceLogger(sender).LoggerRef.LogWarning(value);
		}
	}
	internal class TraceLogger
	{
		internal readonly ManualLogSource LoggerRef;

		private readonly string _source;

		private readonly FileInfo _traceFileInfo;

		public bool IsTraceEnabled { get; private set; }

		private DirectoryInfo AssemblyDirectory => new FileInfo(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)).Directory;

		public TraceLogger(string source, bool enableTrace)
		{
			_source = source;
			IsTraceEnabled = enableTrace;
			LoggerRef = Logger.CreateLogSource(_source);
			_traceFileInfo = new FileInfo(Path.Combine(Paths.BepInExRootPath ?? AssemblyDirectory.FullName, "logs", _source + ".Trace.log"));
			if (_traceFileInfo.DirectoryName != null)
			{
				Directory.CreateDirectory(_traceFileInfo.DirectoryName);
			}
			if (_traceFileInfo.Exists)
			{
				_traceFileInfo.Delete();
				_traceFileInfo.Refresh();
			}
			LoggerRef.LogEvent += OnLogEvent;
		}

		public void EnableTrace()
		{
			IsTraceEnabled = true;
		}

		public void DisableTrace()
		{
			IsTraceEnabled = false;
		}

		[UsedImplicitly]
		public void StopTrace()
		{
			LoggerRef.LogEvent -= OnLogEvent;
		}

		private void OnLogEvent(object sender, LogEventArgs e)
		{
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			if (e.Source.SourceName != _source || !IsTraceEnabled)
			{
				return;
			}
			using Mutex mutex = new Mutex(initiallyOwned: false, "Digitalroot.Valheim.Common.TraceLogger." + _source);
			mutex.WaitOne();
			try
			{
				if (e.Data is string)
				{
					string contents = $"[{e.Level,-7}:{e.Source.SourceName,10}] {e.Data}{Environment.NewLine}";
					File.AppendAllText(_traceFileInfo.FullName, contents, Encoding.UTF8);
				}
				else
				{
					string contents2 = $"[{e.Level,-7}:{e.Source.SourceName,10}] {JsonSerializationProvider.Serialize(e.Data)}{Environment.NewLine}";
					File.AppendAllText(_traceFileInfo.FullName, contents2, Encoding.UTF8);
				}
			}
			finally
			{
				mutex.ReleaseMutex();
			}
		}
	}
}
namespace Digitalroot.Valheim.Common.Json
{
	[UsedImplicitly]
	internal static class JsonSerializationProvider
	{
		[Obsolete("Use Deserialize<T>()")]
		public static T FromJson<T>(string json)
		{
			return Deserialize<T>(json);
		}

		public static T Deserialize<T>(string json)
		{
			return SimpleJson.DeserializeObject<T>(json, (IJsonSerializerStrategy)(object)new DigitalrootJsonSerializerStrategy());
		}

		[Obsolete("Use Serialize()")]
		public static string ToJson(object obj, bool pretty = false)
		{
			return Serialize(obj);
		}

		public static string Serialize(object obj)
		{
			return SimpleJson.SerializeObject(obj, (IJsonSerializerStrategy)(object)new DigitalrootJsonSerializerStrategy());
		}
	}
	internal class DigitalrootJsonSerializerStrategy : PocoJsonSerializerStrategy
	{
		public override bool TrySerializeNonPrimitiveObject(object input, out object output)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: 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_006b: Unknown result type (might be due to invalid IL or missing references)
			if (!(input is Vector3 val))
			{
				if (input is Quaternion val2)
				{
					output = new float[4] { val2.x, val2.y, val2.z, val2.w };
					return true;
				}
				return ((PocoJsonSerializerStrategy)this).TrySerializeNonPrimitiveObject(input, ref output);
			}
			output = new float[3] { val.x, val.y, val.z };
			return true;
		}

		public override object DeserializeObject(object value, Type type)
		{
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			if (type == null)
			{
				throw new ArgumentNullException("type");
			}
			if (value == null)
			{
				throw new ArgumentNullException("value");
			}
			if (value is string value2)
			{
				if (string.IsNullOrWhiteSpace(value2))
				{
					throw new ArgumentNullException("value");
				}
				if (type == typeof(Vector3))
				{
					if (!(((PocoJsonSerializerStrategy)this).DeserializeObject(value, typeof(float[])) is float[] array) || (array != null && array.Length != 3))
					{
						throw new ArgumentException(string.Format("The value '{0}' can be converted to a {1}.", value, "Vector3"), "value");
					}
					return (object)new Vector3(array[0], array[1], array[2]);
				}
				if (type == typeof(Quaternion))
				{
					if (!(((PocoJsonSerializerStrategy)this).DeserializeObject(value, typeof(float[])) is float[] array2) || (array2 != null && array2.Length != 4))
					{
						throw new ArgumentException(string.Format("The value '{0}' can be converted to a {1}.", value, "Quaternion"), "value");
					}
					return (object)new Quaternion(array2[0], array2[1], array2[2], array2[3]);
				}
				return ((PocoJsonSerializerStrategy)this).DeserializeObject(value, type);
			}
			throw new ArgumentException($"The value '{value}' can be converted to a {type.Name}.", "value");
		}
	}
}
namespace Digitalroot.Valheim.Common
{
	internal static class Utils
	{
		private static readonly ITraceableLogging Logger = GetLogger();

		[UsedImplicitly]
		public static DirectoryInfo AssemblyDirectory => new FileInfo(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)).Directory;

		[UsedImplicitly]
		public static bool IsDedicated => ZNet.instance.IsDedicated();

		[UsedImplicitly]
		public static bool IsServer => ZNet.instance.IsServer();

		public static bool IsRunningFromNUnit => AppDomain.CurrentDomain.GetAssemblies().Any((Assembly a) => a.FullName.ToLowerInvariant().StartsWith("nunit.framework"));

		public static string Namespace => "Digitalroot.Valheim.Common";

		private static ITraceableLogging GetLogger()
		{
			return new StaticSourceLogger();
		}

		[UsedImplicitly]
		public static bool IsHeadless()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Invalid comparison between Unknown and I4
			return (int)SystemInfo.graphicsDeviceType == 4;
		}

		[UsedImplicitly]
		public static List<T> AllOf<T>()
		{
			return Enum.GetValues(typeof(T)).OfType<T>().ToList();
		}

		[UsedImplicitly]
		public static IEnumerable<string> AllNames(Type type)
		{
			foreach (FieldInfo item in from f1 in type.GetFields()
				where f1.FieldType == typeof(string)
				select f1)
			{
				yield return item.GetValue(null).ToString();
			}
		}

		[UsedImplicitly]
		public static bool DoesPluginExist(string pluginGuid)
		{
			return Chainloader.PluginInfos.Any((KeyValuePair<string, PluginInfo> keyValuePair) => keyValuePair.Value.Metadata.GUID == pluginGuid);
		}

		[UsedImplicitly]
		public static Vector3 GetGroundHeight(int x, int z)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			return Utils.GetGroundHeight(new Vector3Int(x, 500, z));
		}

		[UsedImplicitly]
		public static Vector3 GetGroundHeight(float x, float z)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			return Utils.GetGroundHeight(new Vector3(x, 500f, z));
		}

		public static Vector3 GetGroundHeight(Vector3Int vector3)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3((float)((Vector3Int)(ref vector3)).x, ZoneSystem.instance.GetGroundHeight(Vector3Int.op_Implicit(vector3)), (float)((Vector3Int)(ref vector3)).z);
		}

		public static Vector3 GetGroundHeight(Vector3 vector3)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3(vector3.x, ZoneSystem.instance.GetGroundHeight(vector3), vector3.z);
		}

		[UsedImplicitly]
		public static GameObject GetItemPrefab(string itemName)
		{
			if (!IsObjectDBReady())
			{
				return null;
			}
			return GetObjectDB().GetItemPrefab(itemName);
		}

		[UsedImplicitly]
		public static GameObject GetItemPrefab(int hash)
		{
			if (!IsObjectDBReady())
			{
				return null;
			}
			return GetObjectDB().GetItemPrefab(hash);
		}

		[UsedImplicitly]
		public static Player GetLocalPlayer()
		{
			return Player.m_localPlayer;
		}

		[UsedImplicitly]
		public static Vector3 GetLocalPlayersPosition()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			return ((Component)Player.m_localPlayer).transform.position;
		}

		[UsedImplicitly]
		public static ObjectDB GetObjectDB()
		{
			return ObjectDB.instance;
		}

		[UsedImplicitly]
		public static string GetPluginPath(Type modPluginType)
		{
			return Path.GetDirectoryName(modPluginType.Assembly.Location);
		}

		[UsedImplicitly]
		public static GameObject GetPrefab(string itemName)
		{
			if (!IsZNetSceneReady())
			{
				return null;
			}
			return ZNetScene.instance.GetPrefab(itemName);
		}

		[UsedImplicitly]
		public static GameObject GetPrefab(int hash)
		{
			if (!IsZNetSceneReady())
			{
				return null;
			}
			return ZNetScene.instance.GetPrefab(hash);
		}

		[UsedImplicitly]
		public static T GetPrivateField<T>(object instance, string name)
		{
			FieldInfo field = instance.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
			if (field != null)
			{
				return (T)field.GetValue(instance);
			}
			Log.Error(Logger, "Variable " + name + " does not exist on type: " + instance.GetType());
			return default(T);
		}

		[UsedImplicitly]
		public static object InvokePrivate(object instance, string name, object[] args = null)
		{
			MethodInfo method = instance.GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic);
			if (method == null)
			{
				Type[] types = ((args == null) ? Type.EmptyTypes : args.Select((object arg) => arg.GetType()).ToArray());
				method = instance.GetType().GetMethod(name, types);
			}
			if (method == null)
			{
				Log.Error(Logger, "Method " + name + " does not exist on type: " + instance.GetType());
				return null;
			}
			return method.Invoke(instance, args);
		}

		[UsedImplicitly]
		public static bool IsGameInMainScene()
		{
			return (Object)(object)ZNetScene.instance != (Object)null;
		}

		[UsedImplicitly]
		public static bool IsObjectDBReady()
		{
			if (!((Object)(object)GetObjectDB() != (Object)null) || GetObjectDB().m_items.Count == 0 || !((Object)(object)GetItemPrefab("Amber") != (Object)null))
			{
				return IsRunningFromNUnit;
			}
			return true;
		}

		[UsedImplicitly]
		public static bool IsPlayerReady()
		{
			return (Object)(object)GetLocalPlayer() != (Object)null;
		}

		[UsedImplicitly]
		public static bool IsZNetSceneReady()
		{
			if ((Object)(object)ZNetScene.instance != (Object)null)
			{
				List<GameObject> list = ZNetScene.instance?.m_prefabs;
				if (list != null)
				{
					return list.Count > 0;
				}
				return false;
			}
			return false;
		}

		[UsedImplicitly]
		public static bool IsZNetReady()
		{
			return (Object)(object)ZNet.instance != (Object)null;
		}

		[UsedImplicitly]
		public static string Localize(string value)
		{
			return Localization.instance.Localize(value);
		}

		[UsedImplicitly]
		public static Vector3 GetStartTemplesPosition()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: 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)
			LocationInstance val = default(LocationInstance);
			if (ZoneSystem.instance.FindClosestLocation("StartTemple", Vector3.zero, ref val))
			{
				Log.Trace(Logger, $"[GetStartTemplesPosition] StartTemple at {val.m_position}");
				return val.m_position;
			}
			Log.Error(Logger, "[GetStartTemplesPosition] Can't find StartTemple");
			return Vector3.zero;
		}

		[UsedImplicitly]
		public static void SetPrivateField(object instance, string name, object value)
		{
			FieldInfo field = instance.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
			if (field == null)
			{
				Log.Error(Logger, "Variable " + name + " does not exist on type: " + instance.GetType());
			}
			else
			{
				field.SetValue(instance, value);
			}
		}

		[UsedImplicitly]
		public static GameObject Spawn([NotNull] string prefabName, Vector3 location, [CanBeNull] Transform parent = null)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			Log.Trace(Logger, $"{Namespace}.{MethodBase.GetCurrentMethod().DeclaringType?.Name}.{MethodBase.GetCurrentMethod().Name}({prefabName}, {location}, {((parent != null) ? ((Object)parent).name : null)})");
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(StringExtensionMethods.GetStableHashCode(prefabName));
			if (!((Object)(object)itemPrefab == (Object)null))
			{
				return Spawn(itemPrefab, location, parent);
			}
			return null;
		}

		[UsedImplicitly]
		public static GameObject Spawn([NotNull] GameObject prefab, Vector3 location, [CanBeNull] Transform parent = null)
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: 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_0074: Unknown result type (might be due to invalid IL or missing references)
			Log.Trace(Logger, $"{Namespace}.{MethodBase.GetCurrentMethod().DeclaringType?.Name}.{MethodBase.GetCurrentMethod().Name}({((Object)prefab).name}, {location}, {((parent != null) ? ((Object)parent).name : null)})");
			if ((Object)(object)parent == (Object)null)
			{
				return Object.Instantiate<GameObject>(prefab, location, Quaternion.identity);
			}
			return Object.Instantiate<GameObject>(prefab, location, Quaternion.identity, parent);
		}

		public static AssetBundle LoadAssetBundleFromResources(string bundleName, Assembly resourceAssembly)
		{
			if (resourceAssembly == null)
			{
				throw new ArgumentNullException("resourceAssembly");
			}
			string text = null;
			try
			{
				text = resourceAssembly.GetManifestResourceNames().Single((string str) => str.EndsWith(bundleName));
			}
			catch (Exception)
			{
			}
			if (text == null)
			{
				Log.Error(Logger, "AssetBundle " + bundleName + " not found in assembly manifest");
				return null;
			}
			using Stream stream = resourceAssembly.GetManifestResourceStream(text);
			return AssetBundle.LoadFromStream(stream);
		}
	}
	[DebuggerDisplay("Source = {Source}, EnableTrace = {EnableTrace}", Name = "{Source}")]
	internal class StaticSourceLogger : ITraceableLogging
	{
		public static StaticSourceLogger PreMadeTraceableInstance = new StaticSourceLogger(enableTrace: true);

		public static StaticSourceLogger PreMadeNonTraceableInstance = new StaticSourceLogger();

		public string Source { get; }

		public bool EnableTrace { get; }

		public StaticSourceLogger(bool enableTrace = false)
			: this("Digitalroot", enableTrace)
		{
		}

		public StaticSourceLogger(string source, bool enableTrace = false)
		{
			Source = source;
			EnableTrace = enableTrace;
		}
	}
}
internal class DigitalrootValheimMisophoniaFriendly_ProcessedByFody
{
	internal const string FodyVersion = "6.6.0.0";

	internal const string ILMerge = "1.22.0.0";
}