Decompiled source of ManagerDetector v1.0.5

ManagerDetector.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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ManagerDetector")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("ManagerDetector")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4358610B-F3F4-4843-B7AF-98B7BC60DCDE")]
[assembly: AssemblyFileVersion("1.0.5")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.5.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 ManagerDetector
{
	[HarmonyPatch]
	[BepInPlugin("Azumatt.ManagerDetector", "ManagerDetector", "1.0.5")]
	public class ManagerDetectorPlugin : BaseUnityPlugin
	{
		internal const string ModName = "ManagerDetector";

		internal const string ModVersion = "1.0.5";

		internal const string Author = "Azumatt";

		private const string ModGUID = "Azumatt.ManagerDetector";

		private static readonly ManualLogSource ManagerDetectorLogger = Logger.CreateLogSource("ManagerDetector");

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

		private static readonly List<ManagerInfo> Managers = new List<ManagerInfo>
		{
			new ManagerInfo
			{
				NamespaceName = "PieceManager",
				ClassName = "BuildPiece",
				List = new List<string>(),
				ConsoleColor = ConsoleColor.DarkGreen
			},
			new ManagerInfo
			{
				NamespaceName = "ItemManager",
				ClassName = "Item",
				List = new List<string>(),
				ConsoleColor = ConsoleColor.DarkYellow
			},
			new ManagerInfo
			{
				NamespaceName = "ItemDataManager",
				ClassName = "ItemInfo",
				List = new List<string>(),
				ConsoleColor = ConsoleColor.Blue
			},
			new ManagerInfo
			{
				NamespaceName = "SkillManager",
				ClassName = "Skill",
				List = new List<string>(),
				ConsoleColor = ConsoleColor.Cyan
			},
			new ManagerInfo
			{
				NamespaceName = "LocationManager",
				ClassName = "Location",
				List = new List<string>(),
				ConsoleColor = ConsoleColor.DarkCyan
			},
			new ManagerInfo
			{
				NamespaceName = "CreatureManager",
				ClassName = "Creature",
				List = new List<string>(),
				ConsoleColor = ConsoleColor.DarkMagenta
			}
		};

		private static bool Patched;

		public void Awake()
		{
			_harmony.PatchAll();
		}

		[HarmonyPatch(typeof(ZNet), "Awake")]
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		private static void LogBeforeConnect()
		{
			LogTheManagers();
		}

		[HarmonyPatch(typeof(FejdStartup), "Awake")]
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		private static void DoPatch()
		{
			ManagerDetectorLogger.LogWarning((object)"Checking for managers.");
			if (!Patched)
			{
				Patched = true;
				LogTheManagers();
			}
		}

		private static Type? GetManagerType(Assembly assembly, string? namespaceName, string? className)
		{
			Type[] types = assembly.GetTypes();
			foreach (Type type in types)
			{
				if (type.Namespace == namespaceName && type.Name == className)
				{
					return type;
				}
			}
			return null;
		}

		private static void CheckManagers(Assembly assembly, PluginInfo info)
		{
			foreach (ManagerInfo manager in Managers)
			{
				if (GetManagerType(assembly, manager.NamespaceName, manager.ClassName) != null)
				{
					manager.List?.Add($"{info.Metadata.Name} [{info.Metadata.GUID} {info.Metadata.Version}]");
				}
			}
		}

		private static void LogTheManagers()
		{
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Invalid comparison between Unknown and I4
			foreach (ManagerInfo manager in Managers)
			{
				manager.List?.Clear();
			}
			foreach (PluginInfo value in Chainloader.PluginInfos.Values)
			{
				CheckManagers(((object)value.Instance).GetType().Assembly, value);
			}
			foreach (ManagerInfo manager2 in Managers)
			{
				List<string> list = manager2.List;
				if (list == null || list.Count > 0)
				{
					if ((int)Application.platform == 2)
					{
						HandleWindowsPlatform(manager2);
					}
					else
					{
						HandleOtherPlatforms(manager2);
					}
				}
			}
		}

		private static void HandleWindowsPlatform(ManagerInfo managerInfo)
		{
			ConsoleManager.SetConsoleColor(managerInfo.ConsoleColor);
			string text = Environment.NewLine + "[Debug  :ManagerDetector] The following mods have " + managerInfo.NamespaceName + ":";
			ConsoleManager.ConsoleStream.WriteLine(text);
			if (managerInfo.List == null)
			{
				return;
			}
			foreach (string item in managerInfo.List)
			{
				ConsoleManager.StandardOutStream.WriteLine("[Debug  :ManagerDetector] " + item);
			}
			ConsoleManager.SetConsoleColor(ConsoleColor.White);
			LogToDisk(text);
			foreach (string item2 in managerInfo.List)
			{
				LogToDisk("[Debug  :ManagerDetector] " + item2);
			}
		}

		private static void HandleOtherPlatforms(ManagerInfo managerInfo)
		{
			ManagerDetectorLogger.LogInfo((object)(Environment.NewLine + "The following mods have " + managerInfo.NamespaceName + ":"));
			if (managerInfo.List == null)
			{
				return;
			}
			foreach (string item in managerInfo.List)
			{
				ManagerDetectorLogger.LogInfo((object)(item ?? ""));
			}
		}

		private static void LogToDisk(string message)
		{
			foreach (ILogListener listener in Logger.Listeners)
			{
				DiskLogListener val = (DiskLogListener)(object)((listener is DiskLogListener) ? listener : null);
				if (val != null && val.LogWriter != null)
				{
					val.LogWriter.WriteLine(message);
				}
			}
		}
	}
	public class ManagerInfo
	{
		public string? NamespaceName { get; set; }

		public string? ClassName { get; set; }

		public List<string>? List { get; set; }

		public ConsoleColor ConsoleColor { get; set; }
	}
}