using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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.7")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.7.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace ManagerDetector
{
[HarmonyPatch]
[BepInPlugin("Azumatt.ManagerDetector", "ManagerDetector", "1.0.7")]
public class ManagerDetectorPlugin : BaseUnityPlugin
{
internal const string ModName = "ManagerDetector";
internal const string ModVersion = "1.0.7";
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",
LatestVersion = "1.2.9",
Entries = new List<ModEntry>(),
ConsoleColor = ConsoleColor.DarkGreen
},
new ManagerInfo
{
NamespaceName = "ItemManager",
ClassName = "Item",
LatestVersion = "1.2.9",
Entries = new List<ModEntry>(),
ConsoleColor = ConsoleColor.DarkYellow
},
new ManagerInfo
{
NamespaceName = "ItemDataManager",
ClassName = "ItemInfo",
LatestVersion = null,
Entries = new List<ModEntry>(),
ConsoleColor = ConsoleColor.Blue
},
new ManagerInfo
{
NamespaceName = "SkillManager",
ClassName = "Skill",
LatestVersion = "1.7.0",
Entries = new List<ModEntry>(),
ConsoleColor = ConsoleColor.Cyan
},
new ManagerInfo
{
NamespaceName = "LocationManager",
ClassName = "Location",
LatestVersion = null,
Entries = new List<ModEntry>(),
ConsoleColor = ConsoleColor.DarkCyan
},
new ManagerInfo
{
NamespaceName = "CreatureManager",
ClassName = "Creature",
LatestVersion = "1.13.0",
Entries = new List<ModEntry>(),
ConsoleColor = ConsoleColor.DarkMagenta
},
new ManagerInfo
{
NamespaceName = "LocalizationManager",
ClassName = "Localizer",
LatestVersion = "1.4.1",
Entries = new List<ModEntry>(),
ConsoleColor = ConsoleColor.DarkRed
},
new ManagerInfo
{
NamespaceName = "StatusEffectManager",
ClassName = "CustomSE",
LatestVersion = "1.0.0",
Entries = new List<ModEntry>(),
ConsoleColor = ConsoleColor.Magenta
}
};
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;
try
{
types = assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
types = ex.Types;
}
Type[] array = types;
foreach (Type type in array)
{
if (type != null && type.Namespace == namespaceName && type.Name == className)
{
return type;
}
}
return null;
}
private static string? GetManagerVersion(Assembly assembly, string? namespaceName)
{
if (namespaceName == null)
{
return null;
}
string className = namespaceName + "Version";
Type managerType = GetManagerType(assembly, namespaceName, className);
if (managerType == null)
{
return null;
}
return managerType.GetField("Version", BindingFlags.Static | BindingFlags.Public)?.GetValue(null) as string;
}
private static void CheckManagers(Assembly assembly, PluginInfo info)
{
foreach (ManagerInfo manager in Managers)
{
if (GetManagerType(assembly, manager.NamespaceName, manager.ClassName) != null)
{
string managerVersion = GetManagerVersion(assembly, manager.NamespaceName);
string dllName = ((!string.IsNullOrEmpty(assembly.Location)) ? Path.GetFileName(assembly.Location) : (assembly.GetName().Name + ".dll"));
manager.Entries?.Add(new ModEntry
{
Name = info.Metadata.Name,
Version = info.Metadata.Version.ToString(),
Guid = info.Metadata.GUID,
DllName = dllName,
ManagerVersion = managerVersion
});
}
}
}
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.Entries?.Clear();
}
foreach (PluginInfo value in Chainloader.PluginInfos.Values)
{
CheckManagers(((object)value.Instance).GetType().Assembly, value);
}
foreach (ManagerInfo manager2 in Managers)
{
List<ModEntry> entries = manager2.Entries;
if (entries != null && entries.Count > 0)
{
if ((int)Application.platform == 2)
{
HandleWindowsPlatform(manager2);
}
else
{
HandleOtherPlatforms(manager2);
}
}
}
}
private static string FormatVersionStatus(string? detectedVersion, string? latestVersion)
{
if (detectedVersion == null)
{
return "Manager version not found, may need updating";
}
if (latestVersion == null)
{
return "Manager v" + detectedVersion;
}
if (!(detectedVersion == latestVersion))
{
return "Manager v" + detectedVersion + " (latest: v" + latestVersion + ", update available)";
}
return "Manager v" + detectedVersion + " (latest)";
}
private static List<string> FormatAligned(List<ModEntry> entries, string? latestVersion)
{
int num = 0;
int num2 = 0;
int num3 = 0;
foreach (ModEntry entry in entries)
{
int num4 = entry.Name.Length + 2 + entry.Version.Length;
if (num4 > num)
{
num = num4;
}
if (entry.Guid.Length > num2)
{
num2 = entry.Guid.Length;
}
if (entry.DllName.Length > num3)
{
num3 = entry.DllName.Length;
}
}
List<string> list = new List<string>();
foreach (ModEntry entry2 in entries)
{
string text = (entry2.Name + " v" + entry2.Version).PadRight(num);
string text2 = entry2.Guid.PadRight(num2);
string text3 = entry2.DllName.PadRight(num3);
string text4 = FormatVersionStatus(entry2.ManagerVersion, latestVersion);
list.Add(text + " | " + text2 + " | " + text3 + " | " + text4);
}
return list;
}
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.Entries == null)
{
return;
}
List<string> list = FormatAligned(managerInfo.Entries, managerInfo.LatestVersion);
foreach (string item in list)
{
ConsoleManager.StandardOutStream.WriteLine("[Debug :ManagerDetector] " + item);
}
ConsoleManager.SetConsoleColor(ConsoleColor.White);
LogToDisk(text);
foreach (string item2 in list)
{
LogToDisk("[Debug :ManagerDetector] " + item2);
}
}
private static void HandleOtherPlatforms(ManagerInfo managerInfo)
{
ManagerDetectorLogger.LogInfo((object)(Environment.NewLine + "The following mods have " + managerInfo.NamespaceName + ":"));
if (managerInfo.Entries == null)
{
return;
}
foreach (string item in FormatAligned(managerInfo.Entries, managerInfo.LatestVersion))
{
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 string? LatestVersion { get; set; }
public List<ModEntry>? Entries { get; set; }
public ConsoleColor ConsoleColor { get; set; }
}
public class ModEntry
{
public string Name { get; set; } = "";
public string Version { get; set; } = "";
public string Guid { get; set; } = "";
public string DllName { get; set; } = "";
public string? ManagerVersion { get; set; }
}
}