Decompiled source of EquinoxsDebugTools v1.0.1

plugins/EquinoxsDebugTools/EquinoxsDebugTools.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.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("EquinoxsDebugTools")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("EquinoxsDebugTools")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f9bb4391-06ca-44b2-abe0-784a8a4ec4bb")]
[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 EquinoxsDebugTools;

public static class EDT
{
	public static class Logging
	{
		private static Dictionary<string, ConfigEntry<bool>> shouldLogStatuses = new Dictionary<string, ConfigEntry<bool>>();

		private static Dictionary<string, DateTime> functionLogBlockingLimits = new Dictionary<string, DateTime>();

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

		public static void PacedLog(string category, string message, float delaySeconds = 0f)
		{
			string callingDLL = GetCallingDLL();
			string callingFunction = GetCallingFunction();
			if (ShouldLogMessage(callingDLL, category) && (!functionLogBlockingLimits.TryGetValue(callingFunction, out var value) || !(DateTime.Now < value)))
			{
				WriteToLog(category, message, callingFunction);
				if (delaySeconds > 0f)
				{
					functionLogBlockingLimits[callingFunction] = DateTime.Now.AddSeconds(delaySeconds);
				}
			}
		}

		public static void SetPacedLogDelay(float delaySeconds)
		{
			string callingFunction = GetCallingFunction();
			DateTime value = DateTime.Now.AddSeconds(delaySeconds);
			if (!functionLogBlockingLimits.TryGetValue(callingFunction, out var value2) || !(DateTime.Now < value2))
			{
				functionLogBlockingLimits[callingFunction] = value;
			}
		}

		internal static void AddConfigEntry(string key, ConfigEntry<bool> entry)
		{
			shouldLogStatuses.Add(key, entry);
		}

		private static string GetCallingDLL()
		{
			return new StackTrace().GetFrame(2).GetMethod().DeclaringType.Assembly.GetName().Name;
		}

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

		private static bool ShouldLogMessage(string modName, string category)
		{
			string key = modName + "." + category;
			if (!shouldLogStatuses.ContainsKey(key))
			{
				ConfigEntry<bool> value = EquinoxsDebugToolsConfig.CreateConfigEntryForCategory(modName, category);
				shouldLogStatuses.Add(key, value);
			}
			return shouldLogStatuses[key].Value;
		}

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

	internal static ManualLogSource Log => EquinoxsDebugToolsPlugin.Log;

	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>
		{
			{
				typeof(bool),
				"bool"
			},
			{
				typeof(byte),
				"byte"
			},
			{
				typeof(sbyte),
				"sbyte"
			},
			{
				typeof(char),
				"char"
			},
			{
				typeof(short),
				"short"
			},
			{
				typeof(ushort),
				"ushort"
			},
			{
				typeof(int),
				"int"
			},
			{
				typeof(uint),
				"uint"
			},
			{
				typeof(long),
				"long"
			},
			{
				typeof(ulong),
				"ulong"
			},
			{
				typeof(float),
				"float"
			},
			{
				typeof(double),
				"double"
			},
			{
				typeof(decimal),
				"decimal"
			},
			{
				typeof(string),
				"string"
			}
		};
		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() ?? "null";
			string text2 = (dictionary.ContainsKey(fieldInfo.FieldType) ? dictionary[fieldInfo.FieldType] : fieldInfo.FieldType.ToString());
			if (text2 == "char")
			{
				text = "'" + text + "'";
			}
			else if (text2 == "string")
			{
				text = "\"" + text + "\"";
			}
			Log.LogInfo((object)("\t" + text2 + " " + fieldInfo.Name + " = " + text));
		}
	}
}
public class EquinoxsDebugToolsConfig
{
	internal static ConfigEntry<bool> developerMode;

	internal static BaseUnityPlugin pluginInstance;

	internal static void CreateConfigEntries(BaseUnityPlugin plugin)
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		pluginInstance = plugin;
		developerMode = plugin.Config.Bind<bool>("EDT", "Developer Mode", false, new ConfigDescription("When enabled, new config entires will default to true", (AcceptableValueBase)null, Array.Empty<object>()));
	}

	internal static ConfigEntry<bool> CreateConfigEntryForCategory(string modName, string category)
	{
		return pluginInstance.Config.Bind<bool>("Mods." + modName, "Debug " + category, developerMode.Value, "Whether debug messages should be logged for " + modName + " - " + category);
	}
}
[BepInPlugin("com.equinox.EquinoxsDebugTools", "EquinoxsDebugTools", "1.0.1")]
public class EquinoxsDebugToolsPlugin : BaseUnityPlugin
{
	internal const string MyGUID = "com.equinox.EquinoxsDebugTools";

	private const string PluginName = "EquinoxsDebugTools";

	private const string VersionString = "1.0.1";

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

	internal static ManualLogSource Log = new ManualLogSource("EquinoxsDebugTools");

	private void Awake()
	{
		((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: EquinoxsDebugTools, VersionString: 1.0.1 is loading...");
		foreach (KeyValuePair<ConfigDefinition, ConfigEntryBase> item in ((BaseUnityPlugin)this).Config)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Checking entry");
			if (item.Value is ConfigEntry<bool> val)
			{
				string text = item.Key.Section.Replace("Mods.", "");
				EDT.Logging.AddConfigEntry(text, val);
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Added entry: {text}, {val.Value}");
			}
		}
		EquinoxsDebugToolsConfig.CreateConfigEntries((BaseUnityPlugin)(object)this);
		((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: EquinoxsDebugTools, VersionString: 1.0.1 has loaded.");
		Log = ((BaseUnityPlugin)this).Logger;
	}
}