From b35f4bfa1d600647c91e369057bcd954d5e624f6 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sat, 1 Sep 2018 20:43:54 -0500 Subject: [PATCH] Moved Logging to new location --- IPA.Injector/Bootstrapper.cs | 3 +- IPA.Injector/Injector.cs | 7 +- .../Logging/UnityLogInterceptor.cs | 34 -- .../Updating/ModsaberML/ApiEndpoint.cs | 4 +- .../Updating/ModsaberML/Updater.cs | 4 +- .../IllusionInjector/Updating/SelfPlugin.cs | 3 +- .../Loader/Composite/CompositeBSPlugin.cs | 97 ++++++ .../Loader/Composite/CompositeIPAPlugin.cs | 77 +++++ IPA.Loader/Loader/PluginComponent.cs | 109 +++++++ IPA.Loader/Loader/PluginManager.cs | 305 ++++++++++++++++++ .../Logging/LogPrinter.cs | 2 +- .../{IllusionPlugin => }/Logging/Logger.cs | 14 +- .../Logging/Printers/ColoredConsolePrinter.cs | 13 +- .../Logging/Printers/GZFilePrinter.cs | 4 +- .../Logging/Printers/GlobalLogFilePrinter.cs | 11 +- .../Logging/Printers/PluginLogFilePrinter.cs | 9 +- .../Logging/StandardLogger.cs | 24 +- IPA.Loader/Logging/UnityLogInterceptor.cs | 33 ++ 18 files changed, 666 insertions(+), 87 deletions(-) delete mode 100644 IPA.Loader/IllusionInjector/Logging/UnityLogInterceptor.cs create mode 100644 IPA.Loader/Loader/Composite/CompositeBSPlugin.cs create mode 100644 IPA.Loader/Loader/Composite/CompositeIPAPlugin.cs create mode 100644 IPA.Loader/Loader/PluginComponent.cs create mode 100644 IPA.Loader/Loader/PluginManager.cs rename IPA.Loader/{IllusionPlugin => }/Logging/LogPrinter.cs (97%) rename IPA.Loader/{IllusionPlugin => }/Logging/Logger.cs (95%) rename IPA.Loader/{IllusionInjector => }/Logging/Printers/ColoredConsolePrinter.cs (56%) rename IPA.Loader/{IllusionInjector => }/Logging/Printers/GZFilePrinter.cs (97%) rename IPA.Loader/{IllusionInjector => }/Logging/Printers/GlobalLogFilePrinter.cs (57%) rename IPA.Loader/{IllusionInjector => }/Logging/Printers/PluginLogFilePrinter.cs (72%) rename IPA.Loader/{IllusionInjector => }/Logging/StandardLogger.cs (89%) create mode 100644 IPA.Loader/Logging/UnityLogInterceptor.cs diff --git a/IPA.Injector/Bootstrapper.cs b/IPA.Injector/Bootstrapper.cs index a006942e..a673a294 100644 --- a/IPA.Injector/Bootstrapper.cs +++ b/IPA.Injector/Bootstrapper.cs @@ -1,5 +1,4 @@ -using IllusionInjector.Logging; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/IPA.Injector/Injector.cs b/IPA.Injector/Injector.cs index 8638e9de..6947a3f4 100644 --- a/IPA.Injector/Injector.cs +++ b/IPA.Injector/Injector.cs @@ -1,11 +1,12 @@ using IllusionInjector; -using IllusionInjector.Logging; +using IPA.Loader; +using IPA.Logging; using System; using System.IO; using System.Reflection; using UnityEngine; -using static IllusionPlugin.Logging.Logger; -using Logger = IllusionInjector.Logging.Logger; +using static IPA.Logging.Logger; +using Logger = IPA.Logging.Logger; namespace IPA.Injector { diff --git a/IPA.Loader/IllusionInjector/Logging/UnityLogInterceptor.cs b/IPA.Loader/IllusionInjector/Logging/UnityLogInterceptor.cs deleted file mode 100644 index 64e42926..00000000 --- a/IPA.Loader/IllusionInjector/Logging/UnityLogInterceptor.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using UnityEngine; -using LoggerBase = IllusionPlugin.Logging.Logger; - -namespace IllusionInjector.Logging -{ - public class UnityLogInterceptor - { - public static LoggerBase Unitylogger = new StandardLogger("UnityEngine"); - - public static LoggerBase.Level LogTypeToLevel(LogType type) - { - switch (type) - { - case LogType.Assert: - return LoggerBase.Level.Debug; - case LogType.Error: - return LoggerBase.Level.Error; - case LogType.Exception: - return LoggerBase.Level.Critical; - case LogType.Log: - return LoggerBase.Level.Info; - case LogType.Warning: - return LoggerBase.Level.Warning; - default: - return LoggerBase.Level.Info; - } - } - } -} diff --git a/IPA.Loader/IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs b/IPA.Loader/IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs index 6465f381..cb31acda 100644 --- a/IPA.Loader/IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs +++ b/IPA.Loader/IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs @@ -1,5 +1,5 @@ -using IllusionInjector.Logging; -using IllusionInjector.Utilities; +using IPA.Logging; +using IPA.Utilities; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; diff --git a/IPA.Loader/IllusionInjector/Updating/ModsaberML/Updater.cs b/IPA.Loader/IllusionInjector/Updating/ModsaberML/Updater.cs index 6555cbce..43ce0f81 100644 --- a/IPA.Loader/IllusionInjector/Updating/ModsaberML/Updater.cs +++ b/IPA.Loader/IllusionInjector/Updating/ModsaberML/Updater.cs @@ -1,5 +1,5 @@ using IllusionInjector.Updating.Backup; -using IllusionInjector.Utilities; +using IPA.Utilities; using IPA.Loader; using Ionic.Zip; using Newtonsoft.Json; @@ -16,7 +16,7 @@ using System.Threading; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; -using Logger = IllusionInjector.Logging.Logger; +using Logger = IPA.Logging.Logger; namespace IllusionInjector.Updating.ModsaberML { diff --git a/IPA.Loader/IllusionInjector/Updating/SelfPlugin.cs b/IPA.Loader/IllusionInjector/Updating/SelfPlugin.cs index 809df0a3..1472876c 100644 --- a/IPA.Loader/IllusionInjector/Updating/SelfPlugin.cs +++ b/IPA.Loader/IllusionInjector/Updating/SelfPlugin.cs @@ -1,5 +1,4 @@ -using IllusionPlugin; -using IPA; +using IPA; using System; using System.Collections.Generic; using System.Linq; diff --git a/IPA.Loader/Loader/Composite/CompositeBSPlugin.cs b/IPA.Loader/Loader/Composite/CompositeBSPlugin.cs new file mode 100644 index 00000000..98169fad --- /dev/null +++ b/IPA.Loader/Loader/Composite/CompositeBSPlugin.cs @@ -0,0 +1,97 @@ +using IPA; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; +using UnityEngine.SceneManagement; +using Logger = IPA.Logging.Logger; + +namespace IPA.Loader.Composite +{ + public class CompositeBSPlugin : IBeatSaberPlugin + { + IEnumerable plugins; + + private delegate void CompositeCall(IBeatSaberPlugin plugin); + + public CompositeBSPlugin(IEnumerable plugins) { + this.plugins = plugins; + } + + public void OnApplicationStart() { + Invoke(plugin => plugin.OnApplicationStart()); + } + + public void OnApplicationQuit() { + Invoke(plugin => plugin.OnApplicationQuit()); + } + + public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) { + foreach (var plugin in plugins) { + try { + plugin.OnSceneLoaded(scene, sceneMode); + } + catch (Exception ex) { + Logger.log.Error($"{plugin.Name}: {ex}"); + } + } + } + + public void OnSceneUnloaded(Scene scene) { + foreach (var plugin in plugins) { + try { + plugin.OnSceneUnloaded(scene); + } + catch (Exception ex) { + Logger.log.Error($"{plugin.Name}: {ex}"); + } + } + } + + public void OnActiveSceneChanged(Scene prevScene, Scene nextScene) { + foreach (var plugin in plugins) { + try { + plugin.OnActiveSceneChanged(prevScene, nextScene); + } + catch (Exception ex) { + Logger.log.Error($"{plugin.Name}: {ex}"); + } + } + } + + private void Invoke(CompositeCall callback) { + foreach (var plugin in plugins) { + try { + callback(plugin); + } + catch (Exception ex) { + Logger.log.Error($"{plugin.Name}: {ex}"); + } + } + } + + public void OnUpdate() { + Invoke(plugin => plugin.OnUpdate()); + } + + public void OnFixedUpdate() { + Invoke(plugin => plugin.OnFixedUpdate()); + } + + public string Name => throw new NotImplementedException(); + + public string Version => throw new NotImplementedException(); + + public Uri UpdateUri => throw new NotImplementedException(); + + public ModsaberModInfo ModInfo => throw new NotImplementedException(); + + public void OnLateUpdate() { + Invoke(plugin => { + if (plugin is IEnhancedBeatSaberPlugin) + ((IEnhancedBeatSaberPlugin) plugin).OnLateUpdate(); + }); + } + } +} \ No newline at end of file diff --git a/IPA.Loader/Loader/Composite/CompositeIPAPlugin.cs b/IPA.Loader/Loader/Composite/CompositeIPAPlugin.cs new file mode 100644 index 00000000..c70714b6 --- /dev/null +++ b/IPA.Loader/Loader/Composite/CompositeIPAPlugin.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using IPA; +using IPA.Old; +using System.Linq; +using System.Text; +using UnityEngine; +using UnityEngine.SceneManagement; +using Logger = IPA.Logging.Logger; + +namespace IPA.Loader.Composite +{ +#pragma warning disable CS0618 // Type or member is obsolete + public class CompositeIPAPlugin : IPlugin + { + IEnumerable plugins; + + private delegate void CompositeCall(IPlugin plugin); + + public CompositeIPAPlugin(IEnumerable plugins) { + this.plugins = plugins; + } + + public void OnApplicationStart() { + Invoke(plugin => plugin.OnApplicationStart()); + } + + public void OnApplicationQuit() { + Invoke(plugin => plugin.OnApplicationQuit()); + } + + private void Invoke(CompositeCall callback) { + foreach (var plugin in plugins) { + try { + callback(plugin); + } + catch (Exception ex) { + Logger.log.Error($"{plugin.Name}: {ex}"); + } + } + } + + public void OnUpdate() { + Invoke(plugin => plugin.OnUpdate()); + } + + public void OnFixedUpdate() { + Invoke(plugin => plugin.OnFixedUpdate()); + } + + public string Name { + get { throw new NotImplementedException(); } + } + + public string Version { + get { throw new NotImplementedException(); } + } + + public void OnLateUpdate() { + Invoke(plugin => { + if (plugin is IEnhancedBeatSaberPlugin) + ((IEnhancedBeatSaberPlugin) plugin).OnLateUpdate(); + }); + } + + public void OnLevelWasLoaded(int level) + { + Invoke(plugin => plugin.OnLevelWasLoaded(level)); + } + + public void OnLevelWasInitialized(int level) + { + Invoke(plugin => plugin.OnLevelWasInitialized(level)); + } + } +#pragma warning restore CS0618 // Type or member is obsolete +} \ No newline at end of file diff --git a/IPA.Loader/Loader/PluginComponent.cs b/IPA.Loader/Loader/PluginComponent.cs new file mode 100644 index 00000000..f38cd221 --- /dev/null +++ b/IPA.Loader/Loader/PluginComponent.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Text; +using UnityEngine; +using UnityEngine.SceneManagement; +using IPA.Loader; +using IPA.Loader.Composite; +using IPA.Logging; + +namespace IPA.Loader +{ + public class PluginComponent : MonoBehaviour + { + private CompositeBSPlugin bsPlugins; + private CompositeIPAPlugin ipaPlugins; + private bool quitting = false; + + public static PluginComponent Create() + { + Application.logMessageReceived += delegate (string condition, string stackTrace, LogType type) + { + var level = UnityLogInterceptor.LogTypeToLevel(type); + UnityLogInterceptor.UnityLogger.Log(level, $"{condition.Trim()}"); + UnityLogInterceptor.UnityLogger.Log(level, $"{stackTrace.Trim()}"); + }; + + return new GameObject("IPA_PluginManager").AddComponent(); + } + + void Awake() + { + DontDestroyOnLoad(gameObject); + + bsPlugins = new CompositeBSPlugin(PluginManager.BSPlugins); + ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins); + + gameObject.AddComponent(); + + bsPlugins.OnApplicationStart(); + ipaPlugins.OnApplicationStart(); + + SceneManager.activeSceneChanged += OnActiveSceneChanged; + SceneManager.sceneLoaded += OnSceneLoaded; + SceneManager.sceneUnloaded += OnSceneUnloaded; + } + + void Update() + { + bsPlugins.OnUpdate(); + ipaPlugins.OnUpdate(); + } + + void LateUpdate() + { + bsPlugins.OnLateUpdate(); + ipaPlugins.OnLateUpdate(); + } + + void FixedUpdate() + { + bsPlugins.OnFixedUpdate(); + ipaPlugins.OnFixedUpdate(); + } + + void OnDestroy() + { + if (!quitting) + { + Create(); + } + } + + void OnApplicationQuit() + { + SceneManager.activeSceneChanged -= OnActiveSceneChanged; + SceneManager.sceneLoaded -= OnSceneLoaded; + SceneManager.sceneUnloaded -= OnSceneUnloaded; + + bsPlugins.OnApplicationQuit(); + ipaPlugins.OnApplicationQuit(); + + quitting = true; + } + + void OnLevelWasLoaded(int level) + { + ipaPlugins.OnLevelWasLoaded(level); + } + + public void OnLevelWasInitialized(int level) + { + ipaPlugins.OnLevelWasInitialized(level); + } + + void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) + { + bsPlugins.OnSceneLoaded(scene, sceneMode); + } + + private void OnSceneUnloaded(Scene scene) { + bsPlugins.OnSceneUnloaded(scene); + } + + private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) { + bsPlugins.OnActiveSceneChanged(prevScene, nextScene); + } + + } +} diff --git a/IPA.Loader/Loader/PluginManager.cs b/IPA.Loader/Loader/PluginManager.cs new file mode 100644 index 00000000..a50cac6c --- /dev/null +++ b/IPA.Loader/Loader/PluginManager.cs @@ -0,0 +1,305 @@ +using IllusionInjector.Updating; +using IPA; +using IPA.Logging; +using IPA.Old; +using IPA.Utilities; +using Mono.Cecil; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace IPA.Loader +{ + public static class PluginManager + { +#pragma warning disable CS0618 // Type or member is obsolete (IPlugin) + + public class BSPluginMeta + { + public IBeatSaberPlugin Plugin { get; internal set; } + public string Filename { get; internal set; } + public ModsaberModInfo ModsaberInfo { get; internal set; } + } + + public static IEnumerable BSPlugins + { + get + { + if(_bsPlugins == null) + { + LoadPlugins(); + } + return _bsPlugins.Select(p => p.Plugin); + } + } + private static List _bsPlugins = null; + internal static IEnumerable BSMetas + { + get + { + if (_bsPlugins == null) + { + LoadPlugins(); + } + return _bsPlugins; + } + } + + public static IEnumerable Plugins + { + get + { + if (_ipaPlugins == null) + { + LoadPlugins(); + } + return _ipaPlugins; + } + } + private static List _ipaPlugins = null; + + + + private static void LoadPlugins() + { + string pluginDirectory = Path.Combine(Environment.CurrentDirectory, "Plugins"); + + // Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL, + // so we need to resort to P/Invoke + string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath); + Logger.log.Info(exeName); + _bsPlugins = new List(); + _ipaPlugins = new List(); + + if (!Directory.Exists(pluginDirectory)) return; + + string cacheDir = Path.Combine(pluginDirectory, ".cache"); + + if (!Directory.Exists(cacheDir)) + { + Directory.CreateDirectory(cacheDir); + } + else + { + foreach (string plugin in Directory.GetFiles(cacheDir, "*")) + { + File.Delete(plugin); + } + } + + //Copy plugins to .cache + string[] originalPlugins = Directory.GetFiles(pluginDirectory, "*.dll"); + foreach (string s in originalPlugins) + { + string pluginCopy = Path.Combine(cacheDir, Path.GetFileName(s)); + File.Copy(Path.Combine(pluginDirectory, s), pluginCopy); + } + + var selfPlugin = new BSPluginMeta + { + Filename = Path.Combine(Environment.CurrentDirectory, "IPA.exe"), + Plugin = new SelfPlugin() + }; + selfPlugin.ModsaberInfo = selfPlugin.Plugin.ModInfo; + + _bsPlugins.Add(selfPlugin); + + //Load copied plugins + string[] copiedPlugins = Directory.GetFiles(cacheDir, "*.dll"); + foreach (string s in copiedPlugins) + { + var result = LoadPluginsFromFile(s, exeName); + _bsPlugins.AddRange(result.Item1); + _ipaPlugins.AddRange(result.Item2); + } + + + // DEBUG + Logger.log.Info($"Running on Unity {UnityEngine.Application.unityVersion}"); + Logger.log.Info($"Game version {UnityEngine.Application.version}"); + Logger.log.Info("-----------------------------"); + Logger.log.Info($"Loading plugins from {LoneFunctions.GetRelativePath(pluginDirectory, Environment.CurrentDirectory)} and found {_bsPlugins.Count + _ipaPlugins.Count}"); + Logger.log.Info("-----------------------------"); + foreach (var plugin in _bsPlugins) + { + Logger.log.Info($"{plugin.Plugin.Name}: {plugin.Plugin.Version}"); + } + Logger.log.Info("-----------------------------"); + foreach (var plugin in _ipaPlugins) + { + Logger.log.Info($"{plugin.Name}: {plugin.Version}"); + } + Logger.log.Info("-----------------------------"); + } + + private static Tuple, IEnumerable> LoadPluginsFromFile(string file, string exeName) + { + List bsPlugins = new List(); + List ipaPlugins = new List(); + + if (!File.Exists(file) || !file.EndsWith(".dll", true, null)) + return new Tuple, IEnumerable>(bsPlugins, ipaPlugins); + + T OptionalGetPlugin(Type t) where T : class + { + // use typeof() to allow for easier renaming (in an ideal world this compiles to a string, but ¯\_(ツ)_/¯) + if (t.GetInterface(typeof(T).Name) != null) + { + try + { + T pluginInstance = Activator.CreateInstance(t) as T; + string[] filter = null; + + if (pluginInstance is IGenericEnhancedPlugin) + { + filter = ((IGenericEnhancedPlugin)pluginInstance).Filter; + } + + if (filter == null || filter.Contains(exeName, StringComparer.OrdinalIgnoreCase)) + return pluginInstance; + } + catch (Exception e) + { + Logger.log.Error($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}"); + } + } + + return null; + } + + try + { + #region Fix assemblies for refactor + + var module = ModuleDefinition.ReadModule(file); + bool modifiedModule = false; + foreach (var @ref in module.AssemblyReferences) + { // fix assembly references + if (@ref.Name == "IllusionPlugin" || @ref.Name == "IllusionInjector") + { + @ref.Name = "IPA.Loader"; + modifiedModule = true; + } + } + if (modifiedModule) + { // types don't need to be fixed if it's already referencing the new version + foreach (var @ref in module.GetTypeReferences()) + { // fix type references + if (@ref.FullName == "IllusionPlugin.IPlugin") @ref.Namespace = "IPA.Old"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.IEnhancedPlugin") @ref.Namespace = "IPA.Old"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.IBeatSaberPlugin") @ref.Namespace = "IPA"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.IEnhancedBeatSaberPlugin") @ref.Namespace = "IPA"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.BeatSaber.ModsaberModInfo") @ref.Namespace = "IPA"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.IniFile") @ref.Namespace = "IPA"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.IModPrefs") @ref.Namespace = "IPA"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.ModPrefs") @ref.Namespace = "IPA"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.Utils.ReflectionUtil") @ref.Namespace = "IPA.Utilities"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.Logging.Logger") @ref.Namespace = "IPA.Logging"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.Logging.LogPrinter") @ref.Namespace = "IPA.Logging"; //@ref.Name = ""; + if (@ref.FullName == "IllusionInjector.PluginManager") @ref.Namespace = "IPA.Loader"; //@ref.Name = ""; + if (@ref.FullName == "IllusionInjector.PluginComponent") @ref.Namespace = "IPA.Loader"; //@ref.Name = ""; + if (@ref.FullName == "IllusionInjector.CompositeBSPlugin") @ref.Namespace = "IPA.Loader.Composite"; //@ref.Name = ""; + if (@ref.FullName == "IllusionInjector.CompositeIPAPlugin") @ref.Namespace = "IPA.Loader.Composite"; //@ref.Name = ""; + if (@ref.FullName == "IllusionInjector.Logging.UnityLogInterceptor") @ref.Namespace = "IPA.Logging"; //@ref.Name = ""; + if (@ref.FullName == "IllusionInjector.Logging.StandardLogger") @ref.Namespace = "IPA.Logging"; //@ref.Name = ""; + if (@ref.Namespace == "IllusionInjector.Utilities") @ref.Namespace = "IPA.Utilities"; //@ref.Name = ""; + if (@ref.Namespace == "IllusionInjector.Logging.Printers") @ref.Namespace = "IPA.Logging.Printers"; //@ref.Name = ""; + } + module.Write(file); + } + + #endregion + + Assembly assembly = Assembly.LoadFrom(file); + + foreach (Type t in assembly.GetTypes()) + { + IBeatSaberPlugin bsPlugin = OptionalGetPlugin(t); + if (bsPlugin != null) + { + try + { + var init = t.GetMethod("Init", BindingFlags.Instance | BindingFlags.Public); + if (init != null) + { + var initArgs = new List(); + var initParams = init.GetParameters(); + + Logger modLogger = null; + IModPrefs modPrefs = null; + + foreach (var param in initParams) + { + var ptype = param.ParameterType; + if (ptype.IsAssignableFrom(typeof(Logger))) { + if (modLogger == null) modLogger = new StandardLogger(bsPlugin.Name); + initArgs.Add(modLogger); + } + else if (ptype.IsAssignableFrom(typeof(IModPrefs))) + { + if (modPrefs == null) modPrefs = new ModPrefs(bsPlugin); + initArgs.Add(modPrefs); + } + else + initArgs.Add(ptype.GetDefault()); + } + + init.Invoke(bsPlugin, initArgs.ToArray()); + } + + bsPlugins.Add(new BSPluginMeta + { + Plugin = bsPlugin, + Filename = file.Replace("\\.cache", ""), // quick and dirty fix + ModsaberInfo = bsPlugin.ModInfo + }); + } + catch (AmbiguousMatchException) + { + Logger.log.Error($"Only one Init allowed per plugin"); + } + } + else + { + IPlugin ipaPlugin = OptionalGetPlugin(t); + if (ipaPlugin != null) + { + ipaPlugins.Add(ipaPlugin); + } + } + } + + } + catch (Exception e) + { + Logger.log.Error($"Could not load {Path.GetFileName(file)}! {e}"); + } + + return new Tuple, IEnumerable>(bsPlugins, ipaPlugins); + } + + public class AppInfo + { + [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)] + private static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length); + private static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero); + public static string StartupPath + { + get + { + StringBuilder stringBuilder = new StringBuilder(260); + GetModuleFileName(NullHandleRef, stringBuilder, stringBuilder.Capacity); + return stringBuilder.ToString(); + } + } + } +#pragma warning restore CS0618 // Type or member is obsolete (IPlugin) + } +} diff --git a/IPA.Loader/IllusionPlugin/Logging/LogPrinter.cs b/IPA.Loader/Logging/LogPrinter.cs similarity index 97% rename from IPA.Loader/IllusionPlugin/Logging/LogPrinter.cs rename to IPA.Loader/Logging/LogPrinter.cs index 441029e6..3458cb61 100644 --- a/IPA.Loader/IllusionPlugin/Logging/LogPrinter.cs +++ b/IPA.Loader/Logging/LogPrinter.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace IllusionPlugin.Logging +namespace IPA.Logging { /// /// The log printer's base class. diff --git a/IPA.Loader/IllusionPlugin/Logging/Logger.cs b/IPA.Loader/Logging/Logger.cs similarity index 95% rename from IPA.Loader/IllusionPlugin/Logging/Logger.cs rename to IPA.Loader/Logging/Logger.cs index befa4ccd..bfc187ec 100644 --- a/IPA.Loader/IllusionPlugin/Logging/Logger.cs +++ b/IPA.Loader/Logging/Logger.cs @@ -4,13 +4,25 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace IllusionPlugin.Logging +namespace IPA.Logging { /// /// The logger base class. Provides the format for console logs. /// public abstract class Logger { + private static Logger _log; + internal static Logger log + { + get + { + if (_log == null) + _log = new StandardLogger("IPA"); + return _log; + } + } + internal static bool LogCreated => _log != null; + /// /// The standard format for log messages. /// diff --git a/IPA.Loader/IllusionInjector/Logging/Printers/ColoredConsolePrinter.cs b/IPA.Loader/Logging/Printers/ColoredConsolePrinter.cs similarity index 56% rename from IPA.Loader/IllusionInjector/Logging/Printers/ColoredConsolePrinter.cs rename to IPA.Loader/Logging/Printers/ColoredConsolePrinter.cs index a4759b28..bc05ded5 100644 --- a/IPA.Loader/IllusionInjector/Logging/Printers/ColoredConsolePrinter.cs +++ b/IPA.Loader/Logging/Printers/ColoredConsolePrinter.cs @@ -4,25 +4,24 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; -using IllusionPlugin.Logging; -using LoggerBase = IllusionPlugin.Logging.Logger; +using IPA.Logging; -namespace IllusionInjector.Logging.Printers +namespace IPA.Logging.Printers { public class ColoredConsolePrinter : LogPrinter { - LoggerBase.LogLevel filter = LoggerBase.LogLevel.All; - public override LoggerBase.LogLevel Filter { get => filter; set => filter = value; } + Logger.LogLevel filter = Logger.LogLevel.All; + public override Logger.LogLevel Filter { get => filter; set => filter = value; } ConsoleColor color = Console.ForegroundColor; public ConsoleColor Color { get => color; set => color = value; } - public override void Print(LoggerBase.Level level, DateTime time, string logName, string message) + public override void Print(Logger.Level level, DateTime time, string logName, string message) { if (((byte)level & (byte)StandardLogger.PrintFilter) == 0) return; Console.ForegroundColor = color; foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) - Console.WriteLine(string.Format(LoggerBase.LogFormat, line, logName, time, level.ToString().ToUpper())); + Console.WriteLine(string.Format(Logger.LogFormat, line, logName, time, level.ToString().ToUpper())); Console.ResetColor(); } } diff --git a/IPA.Loader/IllusionInjector/Logging/Printers/GZFilePrinter.cs b/IPA.Loader/Logging/Printers/GZFilePrinter.cs similarity index 97% rename from IPA.Loader/IllusionInjector/Logging/Printers/GZFilePrinter.cs rename to IPA.Loader/Logging/Printers/GZFilePrinter.cs index 7a3fac36..bc0f9d86 100644 --- a/IPA.Loader/IllusionInjector/Logging/Printers/GZFilePrinter.cs +++ b/IPA.Loader/Logging/Printers/GZFilePrinter.cs @@ -1,4 +1,4 @@ -using IllusionPlugin.Logging; +using IPA.Logging; using Ionic.Zlib; using System; using System.Collections.Generic; @@ -8,7 +8,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; -namespace IllusionInjector.Logging.Printers +namespace IPA.Logging.Printers { public abstract class GZFilePrinter : LogPrinter { diff --git a/IPA.Loader/IllusionInjector/Logging/Printers/GlobalLogFilePrinter.cs b/IPA.Loader/Logging/Printers/GlobalLogFilePrinter.cs similarity index 57% rename from IPA.Loader/IllusionInjector/Logging/Printers/GlobalLogFilePrinter.cs rename to IPA.Loader/Logging/Printers/GlobalLogFilePrinter.cs index 9f617b72..a3b14187 100644 --- a/IPA.Loader/IllusionInjector/Logging/Printers/GlobalLogFilePrinter.cs +++ b/IPA.Loader/Logging/Printers/GlobalLogFilePrinter.cs @@ -4,19 +4,18 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; -using IllusionPlugin.Logging; -using LoggerBase = IllusionPlugin.Logging.Logger; +using IPA.Logging; -namespace IllusionInjector.Logging.Printers +namespace IPA.Logging.Printers { class GlobalLogFilePrinter : GZFilePrinter { - public override LoggerBase.LogLevel Filter { get; set; } = LoggerBase.LogLevel.All; + public override Logger.LogLevel Filter { get; set; } = Logger.LogLevel.All; - public override void Print(IllusionPlugin.Logging.Logger.Level level, DateTime time, string logName, string message) + public override void Print(IPA.Logging.Logger.Level level, DateTime time, string logName, string message) { foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) - fileWriter.WriteLine(string.Format(LoggerBase.LogFormat, line, logName, time, level.ToString().ToUpper())); + fileWriter.WriteLine(string.Format(Logger.LogFormat, line, logName, time, level.ToString().ToUpper())); } protected override FileInfo GetFileInfo() diff --git a/IPA.Loader/IllusionInjector/Logging/Printers/PluginLogFilePrinter.cs b/IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs similarity index 72% rename from IPA.Loader/IllusionInjector/Logging/Printers/PluginLogFilePrinter.cs rename to IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs index cd0c41e4..c34a5481 100644 --- a/IPA.Loader/IllusionInjector/Logging/Printers/PluginLogFilePrinter.cs +++ b/IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs @@ -1,17 +1,16 @@ -using IllusionPlugin.Logging; +using IPA.Logging; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; -using LoggerBase = IllusionPlugin.Logging.Logger; -namespace IllusionInjector.Logging.Printers +namespace IPA.Logging.Printers { class PluginLogFilePrinter : GZFilePrinter { - public override LoggerBase.LogLevel Filter { get; set; } = LoggerBase.LogLevel.All; + public override Logger.LogLevel Filter { get; set; } = Logger.LogLevel.All; private string name; @@ -28,7 +27,7 @@ namespace IllusionInjector.Logging.Printers this.name = name; } - public override void Print(IllusionPlugin.Logging.Logger.Level level, DateTime time, string logName, string message) + public override void Print(IPA.Logging.Logger.Level level, DateTime time, string logName, string message) { foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) fileWriter.WriteLine(string.Format("[{3} @ {2:HH:mm:ss}] {0}", line, logName, time, level.ToString().ToUpper())); diff --git a/IPA.Loader/IllusionInjector/Logging/StandardLogger.cs b/IPA.Loader/Logging/StandardLogger.cs similarity index 89% rename from IPA.Loader/IllusionInjector/Logging/StandardLogger.cs rename to IPA.Loader/Logging/StandardLogger.cs index 3224691e..c1c821cd 100644 --- a/IPA.Loader/IllusionInjector/Logging/StandardLogger.cs +++ b/IPA.Loader/Logging/StandardLogger.cs @@ -1,6 +1,4 @@ -using IllusionInjector.Logging.Printers; -using IllusionPlugin; -using IllusionPlugin.Logging; +using IPA.Logging; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -10,25 +8,11 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using IPA; -using LoggerBase = IllusionPlugin.Logging.Logger; +using LoggerBase = IPA.Logging.Logger; +using IPA.Logging.Printers; -namespace IllusionInjector.Logging +namespace IPA.Logging { - internal static class Logger - { - private static LoggerBase _log; - internal static LoggerBase log - { - get - { - if (_log == null) - _log = new StandardLogger("IPA"); - return _log; - } - } - internal static bool LogCreated => _log != null; - } - public class StandardLogger : LoggerBase { private static readonly IReadOnlyList defaultPrinters = new List() diff --git a/IPA.Loader/Logging/UnityLogInterceptor.cs b/IPA.Loader/Logging/UnityLogInterceptor.cs new file mode 100644 index 00000000..908f11bf --- /dev/null +++ b/IPA.Loader/Logging/UnityLogInterceptor.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace IPA.Logging +{ + internal class UnityLogInterceptor + { + public static Logger UnityLogger = new StandardLogger("UnityEngine"); + + public static Logger.Level LogTypeToLevel(LogType type) + { + switch (type) + { + case LogType.Assert: + return Logger.Level.Debug; + case LogType.Error: + return Logger.Level.Error; + case LogType.Exception: + return Logger.Level.Critical; + case LogType.Log: + return Logger.Level.Info; + case LogType.Warning: + return Logger.Level.Warning; + default: + return Logger.Level.Info; + } + } + } +}