Decompiled source of BetterZeeLog v1.8.0

BetterZeeLog.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
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 ComfyLib;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("BetterZeeLog")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BetterZeeLog")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d1094b63-ab04-4330-b85e-d68e855e2e04")]
[assembly: AssemblyFileVersion("1.8.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.8.0.0")]
[module: UnverifiableCode]
namespace ComfyLib
{
	public static class ConfigFileExtensions
	{
		internal sealed class ConfigurationManagerAttributes
		{
			public Action<ConfigEntryBase> CustomDrawer;

			public bool? Browsable;

			public bool? HideDefaultButton;

			public int? Order;
		}

		private static readonly Dictionary<string, int> _sectionToSettingOrder = new Dictionary<string, int>();

		private static int GetSettingOrder(string section)
		{
			if (!_sectionToSettingOrder.TryGetValue(section, out var value))
			{
				value = 0;
			}
			_sectionToSettingOrder[section] = value - 1;
			return value;
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, AcceptableValueBase acceptableValues)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, acceptableValues, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Order = GetSettingOrder(section)
				}
			}));
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, Action<ConfigEntryBase> customDrawer = null, bool browsable = true, bool hideDefaultButton = false, bool hideSettingName = false)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)null, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Browsable = browsable,
					CustomDrawer = customDrawer,
					HideDefaultButton = hideDefaultButton,
					Order = GetSettingOrder(section)
				}
			}));
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action settingChangedHandler)
		{
			configEntry.SettingChanged += delegate
			{
				settingChangedHandler();
			};
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<T> settingChangedHandler)
		{
			configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				settingChangedHandler((T)((SettingChangedEventArgs)eventArgs).ChangedSetting.BoxedValue);
			};
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<ConfigEntry<T>> settingChangedHandler)
		{
			configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				settingChangedHandler((ConfigEntry<T>)((SettingChangedEventArgs)eventArgs).ChangedSetting.BoxedValue);
			};
		}
	}
}
namespace BetterZeeLog
{
	[BepInPlugin("redseiko.valheim.betterzeelog", "BetterZeeLog", "1.8.0")]
	public sealed class BetterZeeLog : BaseUnityPlugin
	{
		public const string PluginGUID = "redseiko.valheim.betterzeelog";

		public const string PluginName = "BetterZeeLog";

		public const string PluginVersion = "1.8.0";

		private Harmony _harmony;

		private void Awake()
		{
			PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
			if (PluginConfig.IsModEnabled.Value)
			{
				_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "redseiko.valheim.betterzeelog");
				if (PluginConfig.RemoveStackTraceForNonErrorLogType.Value)
				{
					Application.SetStackTraceLogType((LogType)3, (StackTraceLogType)0);
					Application.SetStackTraceLogType((LogType)2, (StackTraceLogType)0);
				}
			}
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(Projectile))]
	internal static class ProjectilePatch
	{
		[HarmonyTranspiler]
		[HarmonyPatch("FixedUpdate")]
		private static IEnumerable<CodeInstruction> FixedUpdateTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Expected O, but got Unknown
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Expected O, but got Unknown
			if (PluginConfig.CheckProjectFixedUpdateZeroVelocity.Value)
			{
				return new CodeMatcher(instructions, (ILGenerator)null).Start().MatchForward(false, (CodeMatch[])(object)new CodeMatch[3]
				{
					new CodeMatch((OpCode?)OpCodes.Ldfld, (object)AccessTools.Field(typeof(Projectile), "m_vel"), (string)null),
					new CodeMatch((OpCode?)OpCodes.Call, (object)AccessTools.Method(typeof(Quaternion), "LookRotation", new Type[1] { typeof(Vector3) }, (Type[])null), (string)null),
					new CodeMatch((OpCode?)OpCodes.Callvirt, (object)AccessTools.PropertySetter(typeof(Transform), "rotation"), (string)null)
				}).ThrowIfInvalid("Could not patch Projectile.FixedUpdate()! (LookRotation)")
					.Advance(1)
					.SetInstructionAndAdvance(Transpilers.EmitDelegate<Func<Vector3, Quaternion>>((Func<Vector3, Quaternion>)LookRotationDelegate))
					.InstructionEnumeration();
			}
			return instructions;
		}

		private static Quaternion LookRotationDelegate(Vector3 velocity)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: 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_000d: Unknown result type (might be due to invalid IL or missing references)
			if (velocity == Vector3.zero)
			{
				return Quaternion.identity;
			}
			return Quaternion.LookRotation(velocity);
		}
	}
	[HarmonyPatch(typeof(ZLog))]
	internal static class ZLogPatch
	{
		private static string DateTimeNowDelegate(string dateTimeNow)
		{
			return "[" + dateTimeNow + "] ";
		}

		[HarmonyPrefix]
		[HarmonyPatch("Log")]
		private static bool LogPrefix(ref object o)
		{
			if (o.ToString().StartsWith("Console: "))
			{
				return false;
			}
			return true;
		}

		[HarmonyTranspiler]
		[HarmonyPatch("Log")]
		private static IEnumerable<CodeInstruction> LogTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: 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)
			//IL_0026: Expected O, but got Unknown
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
			{
				new CodeMatch((OpCode?)OpCodes.Ldstr, (object)": ", (string)null)
			}).ThrowIfInvalid("Could not patch ZLog.Log() colon instruction.").InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<string, string>>((Func<string, string>)DateTimeNowDelegate) })
				.SetOperandAndAdvance((object)string.Empty)
				.MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((OpCode?)OpCodes.Ldstr, (object)"\n", (string)null)
				})
				.ThrowIfInvalid("Could not patch ZLog.Log() newline instruction.")
				.SetOperandAndAdvance((object)string.Empty)
				.InstructionEnumeration();
		}

		[HarmonyTranspiler]
		[HarmonyPatch("LogWarning")]
		private static IEnumerable<CodeInstruction> LogWarningTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: 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)
			//IL_0026: Expected O, but got Unknown
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
			{
				new CodeMatch((OpCode?)OpCodes.Ldstr, (object)": ", (string)null)
			}).ThrowIfInvalid("Could not patch ZLog.LogWarning() colon instruction.").InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<string, string>>((Func<string, string>)DateTimeNowDelegate) })
				.SetOperandAndAdvance((object)string.Empty)
				.MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((OpCode?)OpCodes.Ldstr, (object)"\n", (string)null)
				})
				.ThrowIfInvalid("Could not patch ZLog.LogWarning() newline instruction.")
				.SetOperandAndAdvance((object)string.Empty)
				.InstructionEnumeration();
		}

		[HarmonyTranspiler]
		[HarmonyPatch("LogError")]
		private static IEnumerable<CodeInstruction> LogErrorTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: 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)
			//IL_0026: Expected O, but got Unknown
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
			{
				new CodeMatch((OpCode?)OpCodes.Ldstr, (object)": ", (string)null)
			}).ThrowIfInvalid("Could not patch ZLog.LogError() colon instruction.").InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<string, string>>((Func<string, string>)DateTimeNowDelegate) })
				.SetOperandAndAdvance((object)string.Empty)
				.MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((OpCode?)OpCodes.Ldstr, (object)"\n", (string)null)
				})
				.ThrowIfInvalid("Could not patch ZLog.LogError() newline instruction.")
				.SetOperandAndAdvance((object)string.Empty)
				.InstructionEnumeration();
		}

		[HarmonyTranspiler]
		[HarmonyPatch("DevLog")]
		private static IEnumerable<CodeInstruction> DevLogTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: 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)
			//IL_0026: Expected O, but got Unknown
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
			{
				new CodeMatch((OpCode?)OpCodes.Ldstr, (object)": ", (string)null)
			}).ThrowIfInvalid("Could not patch ZLog.DevLog() colon instruction.").InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<string, string>>((Func<string, string>)DateTimeNowDelegate) })
				.SetOperandAndAdvance((object)string.Empty)
				.MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((OpCode?)OpCodes.Ldstr, (object)"\n", (string)null)
				})
				.ThrowIfInvalid("Could not patch ZLog.DevLog() newline instruction.")
				.SetOperandAndAdvance((object)string.Empty)
				.InstructionEnumeration();
		}
	}
	[HarmonyPatch(typeof(ZSteamSocket))]
	internal static class ZSteamSocketPatch
	{
		[HarmonyTranspiler]
		[HarmonyPatch("SendQueuedPackages")]
		private static IEnumerable<CodeInstruction> SendQueuedPackagesTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected O, but got Unknown
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Expected O, but got Unknown
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Expected O, but got Unknown
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Expected O, but got Unknown
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Expected O, but got Unknown
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Expected O, but got Unknown
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Expected O, but got Unknown
			if (PluginConfig.RemoveFailedToSendDataLogging.Value)
			{
				return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[6]
				{
					new CodeMatch((OpCode?)OpCodes.Ldstr, (object)"Failed to send data ", (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldloca_S, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Constrained, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Callvirt, (object)AccessTools.Method(typeof(object), "ToString", (Type[])null, (Type[])null), (string)null),
					new CodeMatch((OpCode?)OpCodes.Call, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Call, (object)AccessTools.Method(typeof(ZLog), "Log", (Type[])null, (Type[])null), (string)null)
				}).ThrowIfInvalid("Could not patch ZSteamSocket.SendQueuedPackages()! (ZLog.Log)").Advance(1)
					.SetInstructionAndAdvance(new CodeInstruction(OpCodes.Pop, (object)null))
					.SetInstructionAndAdvance(new CodeInstruction(OpCodes.Nop, (object)null))
					.SetInstructionAndAdvance(new CodeInstruction(OpCodes.Nop, (object)null))
					.SetInstructionAndAdvance(new CodeInstruction(OpCodes.Nop, (object)null))
					.SetInstructionAndAdvance(new CodeInstruction(OpCodes.Nop, (object)null))
					.InstructionEnumeration();
			}
			return instructions;
		}
	}
	public static class PluginConfig
	{
		public static ConfigEntry<bool> IsModEnabled { get; private set; }

		public static ConfigEntry<bool> RemoveStackTraceForNonErrorLogType { get; private set; }

		public static ConfigEntry<bool> RemoveFailedToSendDataLogging { get; private set; }

		public static ConfigEntry<bool> CheckProjectFixedUpdateZeroVelocity { get; private set; }

		public static void BindConfig(ConfigFile config)
		{
			IsModEnabled = config.BindInOrder("_Global", "isModEnabled", defaultValue: true, "Globally enable or disable this mod (restart required).");
			RemoveStackTraceForNonErrorLogType = config.BindInOrder("Logging", "removeStackTraceForNonErrorLogType", defaultValue: true, "Disables the stack track for 'Info' and 'Warning' log types (restart required).");
			RemoveFailedToSendDataLogging = config.BindInOrder("Logging", "removeFailedToSendDataLogging", defaultValue: true, "Removes (NOPs out) 'Failed to send data' logging in ZSteamSocket (restart required).");
			CheckProjectFixedUpdateZeroVelocity = config.BindInOrder("Quaternion.LookRotation", "checkProjectFixedUpdatedZeroVelocity", defaultValue: true, "Checks for zero `m_vel` in `Projectile.FixedUpdate()` (restart required).");
		}
	}
}