Exemple #1
0
        /// <summary>
        ///     Allocates a console window for use by BepInEx safely.
        /// </summary>
        public static void AllocateConsole()
        {
            if (!ConsoleWindow.ConfigConsoleEnabled.Value)
            {
                return;
            }

            try
            {
                ConsoleWindow.Attach();

                var encoding = (uint)Encoding.UTF8.CodePage;

                if (ConsoleWindow.ConfigConsoleShiftJis.Value)
                {
                    encoding = 932;
                }

                ConsoleEncoding.ConsoleCodePage = encoding;
                Console.OutputEncoding          = ConsoleEncoding.GetEncoding(encoding);
            }
            catch (Exception ex)
            {
                Logger.LogError("Failed to allocate console!");
                Logger.LogError(ex);
            }
        }
Exemple #2
0
        /// <summary>
        ///     Allocates a console window for use by BepInEx safely.
        /// </summary>
        public static void AllocateConsole()
        {
            bool console  = Utility.SafeParseBool(Config.GetEntry("console", "false", "BepInEx"));
            bool shiftjis = Utility.SafeParseBool(Config.GetEntry("console-shiftjis", "false", "BepInEx"));

            if (!console)
            {
                return;
            }

            try
            {
                ConsoleWindow.Attach();

                var encoding = (uint)Encoding.UTF8.CodePage;

                if (shiftjis)
                {
                    encoding = 932;
                }

                ConsoleEncoding.ConsoleCodePage = encoding;
                Console.OutputEncoding          = ConsoleEncoding.GetEncoding(encoding);
            }
            catch (Exception ex)
            {
                Logger.LogError("Failed to allocate console!");
                Logger.LogError(ex);
            }
        }
Exemple #3
0
 public void SetConsoleEncoding(uint codepage)
 {
     // Make sure of ConsoleEncoding helper class because on some Monos
     // Encoding.GetEncoding throws NotImplementedException on most codepages
     ConsoleEncoding.ConsoleCodePage = codepage;
     Console.OutputEncoding          = ConsoleEncoding.GetEncoding(codepage);
 }
Exemple #4
0
        /// <summary>
        /// Initializes BepInEx to be able to start the chainloader.
        /// </summary>
        public static void Initialize(string gameExePath, bool startConsole = true)
        {
            if (_initialized)
            {
                return;
            }

            ThreadingHelper.Initialize();

            // Set vitals
            if (gameExePath != null)
            {
                // Checking for null allows a more advanced initialization workflow, where the Paths class has been initialized before calling Chainloader.Initialize
                // This is used by Preloader to use environment variables, for example
                Paths.SetExecutablePath(gameExePath);
            }

            // Start logging
            if (ConsoleWindow.ConfigConsoleEnabled.Value && startConsole)
            {
                ConsoleWindow.Attach();
                Logger.Listeners.Add(new ConsoleLogListener());
            }

            // Fix for standard output getting overwritten by UnityLogger
            if (ConsoleWindow.StandardOut != null)
            {
                Console.SetOut(ConsoleWindow.StandardOut);

                var encoding = ConsoleWindow.ConfigConsoleShiftJis.Value ? 932 : (uint)Encoding.UTF8.CodePage;
                ConsoleEncoding.ConsoleCodePage = encoding;
                Console.OutputEncoding          = ConsoleEncoding.GetEncoding(encoding);
            }

            Logger.Listeners.Add(new UnityLogListener());

            if (ConfigDiskLogging.Value)
            {
                Logger.Listeners.Add(new DiskLogListener("LogOutput.log", ConfigDiskConsoleDisplayedLevel.Value, ConfigDiskAppend.Value, ConfigDiskWriteUnityLog.Value));
            }

            if (!TraceLogSource.IsListening)
            {
                Logger.Sources.Add(TraceLogSource.CreateSource());
            }

            if (ConfigUnityLogging.Value)
            {
                Logger.Sources.Add(new UnityLogSource());
            }


            Logger.LogMessage("Chainloader ready");

            _initialized = true;
        }
Exemple #5
0
        /// <summary>
        /// Initializes BepInEx to be able to start the chainloader.
        /// </summary>
        public static void Initialize(string containerExePath, bool startConsole = true)
        {
            if (_initialized)
            {
                return;
            }

            //Set vitals
            Paths.SetExecutablePath(containerExePath);

            Paths.SetPluginPath(ConfigPluginsDirectory.Value);

            //Start logging
            if (ConsoleWindow.ConfigConsoleEnabled.Value && startConsole)
            {
                ConsoleWindow.Attach();
                Logger.Listeners.Add(new ConsoleLogListener());
            }

            //Fix for standard output getting overwritten by UnityLogger
            if (ConsoleWindow.StandardOut != null)
            {
                Console.SetOut(ConsoleWindow.StandardOut);

                var encoding = ConsoleWindow.ConfigConsoleShiftJis.Value ? 932 : (uint)Encoding.UTF8.CodePage;
                ConsoleEncoding.ConsoleCodePage = encoding;
                Console.OutputEncoding          = ConsoleEncoding.GetEncoding(encoding);
            }

            Logger.Listeners.Add(new UnityLogListener());
            Logger.Listeners.Add(new DiskLogListener());

            if (!TraceLogSource.IsListening)
            {
                Logger.Sources.Add(TraceLogSource.CreateSource());
            }

            if (ConfigUnityLogging.Value)
            {
                Logger.Sources.Add(new UnityLogSource());
            }


            Logger.LogMessage("Chainloader ready");

            _initialized = true;
        }
Exemple #6
0
        private static Encoding GetEncoding(int codePage)
        {
            // In most scenarios, 437 is the codepage used for Console encoding. However this encoding is not available
            // by default or on all platforms, and so we use the try{} catch{} pattern and use UTF8 in case of failure.
            // This ensures that if the user uses Encoding.RegisterProvider to register the encoding the Console class
            // can automatically get the codepage as well.
            Encoding enc;

            try
            {
                enc = Encoding.GetEncoding(codePage);
                Debug.Assert(!(enc is UnicodeEncoding)); // if this ever changes, will need to update how we read/write Windows console streams
                enc = new ConsoleEncoding(enc);          // ensure encoding doesn't output a preamble
            }
            catch (NotSupportedException)
            {
                enc = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
            }
            return(enc);
        }
Exemple #7
0
        /// <summary>
        /// Initializes BepInEx to be able to start the chainloader.
        /// </summary>
        public static void Initialize(string gameExePath, bool startConsole = true, ICollection <LogEventArgs> preloaderLogEvents = null)
        {
            if (_initialized)
            {
                return;
            }

            ThreadingHelper.Initialize();

            // Set vitals
            if (gameExePath != null)
            {
                // Checking for null allows a more advanced initialization workflow, where the Paths class has been initialized before calling Chainloader.Initialize
                // This is used by Preloader to use environment variables, for example
                Paths.SetExecutablePath(gameExePath);
            }

            // Start logging
            if (ConsoleWindow.ConfigConsoleEnabled.Value && startConsole)
            {
                ConsoleWindow.Attach();
                Logger.Listeners.Add(new ConsoleLogListener());
            }

            // Fix for standard output getting overwritten by UnityLogger
            if (ConsoleWindow.StandardOut != null)
            {
                Console.SetOut(ConsoleWindow.StandardOut);

                var encoding = ConsoleWindow.ConfigConsoleShiftJis.Value ? 932 : (uint)Encoding.UTF8.CodePage;
                ConsoleEncoding.ConsoleCodePage = encoding;
                Console.OutputEncoding          = ConsoleEncoding.GetEncoding(encoding);
            }

            Logger.Listeners.Add(new UnityLogListener());

            if (ConfigDiskLogging.Value)
            {
                Logger.Listeners.Add(new DiskLogListener("LogOutput.log", ConfigDiskConsoleDisplayedLevel.Value, ConfigDiskAppend.Value, ConfigDiskWriteUnityLog.Value));
            }

            if (!TraceLogSource.IsListening)
            {
                Logger.Sources.Add(TraceLogSource.CreateSource());
            }

            if (ConfigUnityLogging.Value)
            {
                Logger.Sources.Add(new UnityLogSource());
            }


            // Temporarily disable the console log listener as we replay the preloader logs
            var logListener = Logger.Listeners.FirstOrDefault(logger => logger is ConsoleLogListener);

            if (logListener != null)
            {
                Logger.Listeners.Remove(logListener);
            }

            // Write preloader log events if there are any, including the original log source name
            if (preloaderLogEvents != null)
            {
                var preloaderLogSource = Logger.CreateLogSource("Preloader");

                foreach (var preloaderLogEvent in preloaderLogEvents)
                {
                    preloaderLogSource.Log(preloaderLogEvent.Level, $"[{preloaderLogEvent.Source.SourceName,10}] {preloaderLogEvent.Data}");
                }

                Logger.Sources.Remove(preloaderLogSource);
            }

            if (logListener != null)
            {
                Logger.Listeners.Add(logListener);
            }

            Logger.LogMessage("Chainloader ready");

            _initialized = true;
        }