Exemple #1
0
        public static void Run()
        {
            try
            {
                AllocateConsole();

                UnityPatches.Apply();

                Logger.Sources.Add(TraceLogSource.CreateSource());

                PreloaderLog = new PreloaderConsoleListener(Utility.SafeParseBool(Config.GetEntry("preloader-logconsole", "false", "BepInEx")));

                Logger.Listeners.Add(PreloaderLog);


                string consoleTile = $"BepInEx {typeof(Paths).Assembly.GetName().Version} - {Process.GetCurrentProcess().ProcessName}";

                ConsoleWindow.Title = consoleTile;
                Logger.LogMessage(consoleTile);

                //See BuildInfoAttribute for more information about this section.
                object[] attributes = typeof(BuildInfoAttribute).Assembly.GetCustomAttributes(typeof(BuildInfoAttribute), false);

                if (attributes.Length > 0)
                {
                    var attribute = (BuildInfoAttribute)attributes[0];

                    Logger.LogMessage(attribute.Info);
                }

#if UNITY_2018
                Logger.LogMessage("Compiled in Unity v2018 mode");
#else
                Logger.LogMessage("Compiled in Legacy Unity mode");
#endif

                Logger.LogInfo($"Running under Unity v{Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion}");

                Logger.LogMessage("Preloader started");

                string entrypointAssembly = Config.GetEntry("entrypoint-assembly", "UnityEngine.dll", "Preloader");

                AssemblyPatcher.AddPatcher(new PatcherPlugin
                {
                    TargetDLLs = new[] { entrypointAssembly }, Patcher = PatchEntrypoint
                });
                AssemblyPatcher.AddPatchersFromDirectory(Paths.PatcherPluginPath, GetPatcherMethods);

                Logger.LogInfo($"{AssemblyPatcher.PatcherPlugins.Count} patcher plugin(s) loaded");

                AssemblyPatcher.PatchAndLoad(Paths.ManagedPath);

                AssemblyPatcher.DisposePatchers();

                Logger.LogMessage("Preloader finished");

                Logger.Listeners.Remove(PreloaderLog);
                Logger.Listeners.Add(new ConsoleLogListener());

                PreloaderLog.Dispose();
            }
            catch (Exception ex)
            {
                try
                {
                    Logger.LogFatal("Could not run preloader!");
                    Logger.LogFatal(ex);

                    PreloaderLog?.Dispose();

                    if (!ConsoleWindow.IsAttached)
                    {
                        //if we've already attached the console, then the log will already be written to the console
                        AllocateConsole();
                        Console.Write(PreloaderLog);
                    }

                    PreloaderLog = null;
                }
                finally
                {
                    File.WriteAllText(
                        Path.Combine(Paths.GameRootPath, $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log"),
                        PreloaderLog + "\r\n" + ex);

                    PreloaderLog?.Dispose();
                    PreloaderLog = null;
                }
            }
        }
Exemple #2
0
        public static void Run()
        {
            try
            {
                AllocateConsole();

                bool bridgeInitialized = Utility.TryDo(() =>
                {
                    if (ConfigShimHarmony.Value)
                    {
                        HarmonyDetourBridge.Init();
                    }
                }, out var harmonyBridgeException);

                Exception runtimePatchException = null;
                if (bridgeInitialized)
                {
                    Utility.TryDo(() =>
                    {
                        if (ConfigApplyRuntimePatches.Value)
                        {
                            UnityPatches.Apply();
                        }
                    }, out runtimePatchException);
                }

                Logger.Sources.Add(TraceLogSource.CreateSource());

                HarmonyFixes.Apply();

                PreloaderLog = new PreloaderConsoleListener(ConfigPreloaderCOutLogging.Value);
                Logger.Listeners.Add(PreloaderLog);

                string consoleTile = $"BepInEx {typeof(Paths).Assembly.GetName().Version} - {Process.GetCurrentProcess().ProcessName}";
                ConsoleWindow.Title = consoleTile;
                Logger.LogMessage(consoleTile);

                //See BuildInfoAttribute for more information about this section.
                object[] attributes = typeof(BuildInfoAttribute).Assembly.GetCustomAttributes(typeof(BuildInfoAttribute), false);

                if (attributes.Length > 0)
                {
                    var attribute = (BuildInfoAttribute)attributes[0];
                    Logger.LogMessage(attribute.Info);
                }

                Logger.LogInfo($"Running under Unity v{FileVersionInfo.GetVersionInfo(Paths.ExecutablePath).FileVersion}");
                Logger.LogInfo($"CLR runtime version: {Environment.Version}");
                Logger.LogInfo($"Supports SRE: {Utility.CLRSupportsDynamicAssemblies}");

                if (harmonyBridgeException != null)
                {
                    Logger.LogWarning($"Failed to enable fix for Harmony for .NET Standard API. Error message: {harmonyBridgeException.Message}");
                }

                if (runtimePatchException != null)
                {
                    Logger.LogWarning($"Failed to apply runtime patches for Mono. See more info in the output log. Error message: {runtimePatchException.Message}");
                }

                Logger.LogMessage("Preloader started");

                AssemblyPatcher.AddPatcher(new PatcherPlugin
                {
                    TargetDLLs = () => new[] { ConfigEntrypointAssembly.Value },
                    Patcher    = PatchEntrypoint,
                    TypeName   = "BepInEx.Chainloader"
                });

                AssemblyPatcher.AddPatchersFromDirectory(Paths.PatcherPluginPath);

                Logger.LogInfo($"{AssemblyPatcher.PatcherPlugins.Count} patcher plugin(s) loaded");

                AssemblyPatcher.PatchAndLoad(Paths.ManagedPath);
                AssemblyPatcher.DisposePatchers();


                Logger.LogMessage("Preloader finished");

                Logger.Listeners.Remove(PreloaderLog);
                Logger.Listeners.Add(new ConsoleLogListener());

                PreloaderLog.Dispose();
            }
            catch (Exception ex)
            {
                try
                {
                    Logger.LogFatal("Could not run preloader!");
                    Logger.LogFatal(ex);

                    PreloaderLog?.Dispose();

                    if (!ConsoleWindow.IsAttached)
                    {
                        //if we've already attached the console, then the log will already be written to the console
                        AllocateConsole();
                        Console.Write(PreloaderLog);
                    }

                    PreloaderLog = null;
                }
                finally
                {
                    File.WriteAllText(
                        Path.Combine(Paths.GameRootPath, $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log"),
                        PreloaderLog + "\r\n" + ex);

                    PreloaderLog?.Dispose();
                    PreloaderLog = null;
                }
            }
        }
Exemple #3
0
        public static void Run()
        {
            try
            {
                ConsoleManager.Initialize(false);
                AllocateConsole();

                Utility.TryDo(() =>
                {
                    if (ConfigApplyRuntimePatches.Value)
                    {
                        UnityPatches.Apply();
                    }
                }, out var runtimePatchException);

                Logger.InitializeInternalLoggers();
                Logger.Sources.Add(TraceLogSource.CreateSource());

                PreloaderLog = new PreloaderConsoleListener(ConfigPreloaderCOutLogging.Value);
                Logger.Listeners.Add(PreloaderLog);

                string consoleTile = $"BepInEx {typeof(Paths).Assembly.GetName().Version} - {Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().ProcessName)}";

                if (ConsoleManager.ConsoleActive)
                {
                    ConsoleManager.SetConsoleTitle(consoleTile);
                }

                Logger.LogMessage(consoleTile);

                //See BuildInfoAttribute for more information about this section.
                object[] attributes = typeof(BuildInfoAttribute).Assembly.GetCustomAttributes(typeof(BuildInfoAttribute), false);

                if (attributes.Length > 0)
                {
                    var attribute = (BuildInfoAttribute)attributes[0];
                    Logger.LogMessage(attribute.Info);
                }

                Logger.LogInfo($"Running under Unity v{GetUnityVersion()}");
                Logger.LogInfo($"CLR runtime version: {Environment.Version}");
                Logger.LogInfo($"Supports SRE: {Utility.CLRSupportsDynamicAssemblies}");

                if (runtimePatchException != null)
                {
                    Logger.LogWarning($"Failed to apply runtime patches for Mono. See more info in the output log. Error message: {runtimePatchException.Message}");
                }

                Logger.LogMessage("Preloader started");

                AssemblyPatcher.AddPatcher(new PatcherPlugin
                {
                    TargetDLLs = () => new[] { ConfigEntrypointAssembly.Value },
                    Patcher    = PatchEntrypoint,
                    TypeName   = "BepInEx.Chainloader"
                });

                AssemblyPatcher.AddPatchersFromDirectory(Paths.PatcherPluginPath);

                Logger.LogInfo($"{AssemblyPatcher.PatcherPlugins.Count} patcher plugin(s) loaded");

                AssemblyPatcher.PatchAndLoad(Paths.ManagedPath);
                AssemblyPatcher.DisposePatchers();


                Logger.LogMessage("Preloader finished");

                Logger.Listeners.Remove(PreloaderLog);
                Logger.Listeners.Add(new ConsoleLogListener());

                PreloaderLog.Dispose();
            }
            catch (Exception ex)
            {
                try
                {
                    Logger.LogFatal("Could not run preloader!");
                    Logger.LogFatal(ex);

                    if (!ConsoleManager.ConsoleActive)
                    {
                        //if we've already attached the console, then the log will already be written to the console
                        AllocateConsole();
                        Console.Write(PreloaderLog);
                    }
                }
                catch { }

                string log = string.Empty;

                try
                {
                    // We could use platform-dependent newlines, however the developers use Windows so this will be easier to read :)

                    log  = string.Join("\r\n", PreloaderConsoleListener.LogEvents.Select(x => x.ToString()).ToArray());
                    log += "\r\n";

                    PreloaderLog?.Dispose();
                    PreloaderLog = null;
                }
                catch { }

                File.WriteAllText(
                    Path.Combine(Paths.GameRootPath, $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log"),
                    log + ex);
            }
        }