Decompiled source of EquinoxsDebuggingTools v1.0.2

plugins/EquinoxsDebuggingTools/EquinoxsDebuggingTools.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("EquinoxsDebuggingTools")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("EquinoxsDebuggingTools")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1440f45c-2a3a-4520-9a75-995560e3a63f")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace EquinoxsDebuggingTools;

[BepInPlugin("com.equinox.EquinoxsDebuggingTools", "EquinoxsDebuggingTools", "1.0.2")]
public class EDT : BaseUnityPlugin
{
	private const string MyGUID = "com.equinox.EquinoxsDebuggingTools";

	private const string PluginName = "EquinoxsDebuggingTools";

	private const string VersionString = "1.0.2";

	private static readonly Harmony Harmony = new Harmony("com.equinox.EquinoxsDebuggingTools");

	internal static ManualLogSource log = new ManualLogSource("EquinoxsDebuggingTools");

	internal static EDT instance;

	internal static float sUntilNextPacedLog;

	internal static Dictionary<string, ConfigEntry<bool>> shouldLogStatuses = new Dictionary<string, ConfigEntry<bool>>();

	public static ConfigEntry<bool> forceDebugLoggingOff;

	public static ConfigEntry<bool> developerMode;

	private void Awake()
	{
		instance = this;
		foreach (KeyValuePair<ConfigDefinition, ConfigEntryBase> item in ((BaseUnityPlugin)this).Config)
		{
			if (item.Value is ConfigEntry<bool> value)
			{
				string key = item.Key.Section.Replace("Mods.", "");
				shouldLogStatuses.Add(key, value);
			}
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: EquinoxsDebuggingTools, VersionString: 1.0.2 is loading...");
		Harmony.PatchAll();
		CreateEDTConfigEntries();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: EquinoxsDebuggingTools, VersionString: 1.0.2 is loaded.");
		log = ((BaseUnityPlugin)this).Logger;
	}

	private void Update()
	{
		sUntilNextPacedLog -= Time.deltaTime;
	}

	public static void Log(string category, string message)
	{
		string callingDll = GetCallingDll();
		string callingFunction = GetCallingFunction();
		if (ShouldLogMessage(callingDll, category))
		{
			WriteToLog(callingDll, category, message, callingFunction);
		}
	}

	public static void PacedLog(string category, string message, float delaySeconds = 0f)
	{
		string callingDll = GetCallingDll();
		string callingFunction = GetCallingFunction();
		if (ShouldLogMessage(callingDll, category) && !(sUntilNextPacedLog > 0f))
		{
			WriteToLog(callingDll, category, message, callingFunction);
			if (delaySeconds != 0f)
			{
				sUntilNextPacedLog = delaySeconds;
			}
		}
	}

	public static void SetPacedLogDelay(float delaySeconds)
	{
		if (!(sUntilNextPacedLog > 0f))
		{
			sUntilNextPacedLog = delaySeconds;
		}
	}

	public static bool NullCheck(object obj, string name, bool shouldLog = false)
	{
		if (obj == null)
		{
			log.LogWarning((object)(name + " is null"));
			return false;
		}
		if (shouldLog)
		{
			log.LogInfo((object)(name + " is not null"));
		}
		return true;
	}

	public static void DebugObject(object obj, string name)
	{
		if (!NullCheck(obj, name))
		{
			log.LogError((object)"Can't debug null object");
			return;
		}
		Dictionary<Type, string> dictionary = new Dictionary<Type, string>();
		dictionary.Add(typeof(bool), "bool");
		dictionary.Add(typeof(byte), "byte");
		dictionary.Add(typeof(sbyte), "sbyte");
		dictionary.Add(typeof(char), "char");
		dictionary.Add(typeof(short), "short");
		dictionary.Add(typeof(ushort), "ushort");
		dictionary.Add(typeof(int), "int");
		dictionary.Add(typeof(uint), "uint");
		dictionary.Add(typeof(long), "long");
		dictionary.Add(typeof(ulong), "ulong");
		dictionary.Add(typeof(float), "float");
		dictionary.Add(typeof(double), "double");
		dictionary.Add(typeof(decimal), "decimal");
		dictionary.Add(typeof(string), "string");
		Dictionary<Type, string> dictionary2 = dictionary;
		Type type = obj.GetType();
		FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		log.LogInfo((object)("Debugging " + type.Name + " '" + name + "':"));
		FieldInfo[] array = fields;
		foreach (FieldInfo fieldInfo in array)
		{
			string text = fieldInfo.GetValue(obj).ToString();
			string text2 = (dictionary2.ContainsKey(fieldInfo.FieldType) ? dictionary2[fieldInfo.FieldType] : fieldInfo.FieldType.ToString());
			if (text2 == "char")
			{
				text = "'" + text + "'";
			}
			else if (text2 == "string")
			{
				text = "\"" + text + "\"";
			}
			log.LogInfo((object)("\t" + text2 + " " + fieldInfo.Name + " = " + text));
		}
	}

	private void CreateEDTConfigEntries()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Expected O, but got Unknown
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Expected O, but got Unknown
		forceDebugLoggingOff = ((BaseUnityPlugin)this).Config.Bind<bool>("EDT", "Force Debug Logging Off", true, new ConfigDescription("When enabled, no debug messages from mods using EDT will be logged.", (AcceptableValueBase)null, Array.Empty<object>()));
		developerMode = ((BaseUnityPlugin)this).Config.Bind<bool>("EDT", "Developer Mode", false, new ConfigDescription("When enabled, new config entries will default to true", (AcceptableValueBase)null, Array.Empty<object>()));
	}

	private static bool ShouldLogMessage(string modName, string category)
	{
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Expected O, but got Unknown
		string key = modName + "." + category;
		if (!shouldLogStatuses.ContainsKey(key))
		{
			ConfigEntry<bool> value = ((BaseUnityPlugin)instance).Config.Bind<bool>("Mods." + modName, "Debug " + category, developerMode.Value, new ConfigDescription("Whether debug messages should be logged for " + modName + " - " + category, (AcceptableValueBase)null, Array.Empty<object>()));
			shouldLogStatuses.Add(key, value);
		}
		if (forceDebugLoggingOff.Value)
		{
			return false;
		}
		return shouldLogStatuses[key].Value;
	}

	private static void WriteToLog(string modName, string category, string message, string callingFunction)
	{
		string text = "[" + modName + "|" + category + "|" + callingFunction + "]: " + message;
		log.LogInfo((object)text);
	}

	private static string GetCallingFunction()
	{
		StackTrace stackTrace = new StackTrace();
		StackFrame frame = stackTrace.GetFrame(2);
		MethodBase method = frame.GetMethod();
		return method.DeclaringType.FullName + "." + method.Name;
	}

	private static string GetCallingDll()
	{
		StackTrace stackTrace = new StackTrace();
		StackFrame frame = stackTrace.GetFrame(2);
		MethodBase method = frame.GetMethod();
		Type declaringType = method.DeclaringType;
		Assembly assembly = declaringType.Assembly;
		return assembly.GetName().Name;
	}
}