Beispiel #1
0
        public static void Main(string[] args)
        {
            if (Thread.CurrentThread.Name != "Main Thread")
            {
                Thread.CurrentThread.Name = "Main Thread";
            }

            if (File.Exists("BuildIsXNA.txt"))
            {
                File.Delete("BuildIsXNA.txt");
            }
            if (File.Exists("BuildIsFNA.txt"))
            {
                File.Delete("BuildIsFNA.txt");
            }
            File.WriteAllText($"BuildIs{(typeof(Game).Assembly.FullName.Contains("FNA") ? "FNA" : "XNA")}.txt", "");

            if (File.Exists("launch.txt"))
            {
                args =
                    File.ReadAllLines("launch.txt")
                    .Select(l => l.Trim())
                    .Where(l => !l.StartsWith("#"))
                    .SelectMany(l => l.Split(' '))
                    .Concat(args)
                    .ToArray();
            }
            else
            {
                using (StreamWriter writer = File.CreateText("launch.txt")) {
                    writer.WriteLine("# Add any Everest launch flags here. Lines starting with # are ignored.");
                    writer.WriteLine();
                    writer.WriteLine("# If you're having graphics issues with the FNA version on Windows,");
                    writer.WriteLine("# remove the # from the following line to enable using Direct3D.");
                    writer.WriteLine("#--d3d");
                    writer.WriteLine();
                    writer.WriteLine("# If you've got an Intel GPU, are using the FNA version on Windows and");
                    writer.WriteLine("# are 100% sure that you want to use Intel's possibly broken OpenGL drivers,");
                    writer.WriteLine("# remove the # from the following line to revert to using OpenGL.");
                    writer.WriteLine("#--no-d3d");
                    writer.WriteLine();
                }
            }

            if (args.Contains("--console") && PlatformHelper.Is(MonoMod.Utils.Platform.Windows))
            {
                AllocConsole();
            }

            // PlatformHelper is part of MonoMod.
            // Environment.OSVersion.Platform is good enough as well, but Everest consistently uses PlatformHelper.
            // The following is based off of https://github.com/FNA-XNA/FNA/wiki/4:-FNA-and-Windows-API#direct3d-support
            if (PlatformHelper.Is(MonoMod.Utils.Platform.Windows))
            {
                bool useD3D = args.Contains("--d3d");

                try {
                    // Keep all usage of System.Management in a separate method.
                    // Member references are resolved as soon as a method is called.
                    // This means that if System.Management cannot be found due to
                    // f.e. the use of MonoKickstart, this method won't even get as
                    // far as checking the platform.
                    if (DoesGPUHaveBadOpenGLDrivers())
                    {
                        useD3D = true;
                    }
                } catch {
                    // Silently catch all exceptions: Method and type load errors,
                    // permission / access related exceptions and whatnot.
                }

                if (args.Contains("--no-d3d"))
                {
                    useD3D = false;
                }

                if (useD3D)
                {
                    Environment.SetEnvironmentVariable("FNA_OPENGL_FORCE_ES3", "1");
                    Environment.SetEnvironmentVariable("SDL_OPENGL_ES_DRIVER", "1");
                }
            }

            if (args.Contains("--nolog"))
            {
                MainInner(args);
                Everest.Shutdown();
                return;
            }

            if (File.Exists("log.txt"))
            {
                if (new FileInfo("log.txt").Length > 0)
                {
                    // move the old log.txt to the LogHistory folder.
                    // note that the cleanup will only be done when the core module is loaded: the settings aren't even loaded right now,
                    // so we don't know how many files we should keep.
                    if (!Directory.Exists("LogHistory"))
                    {
                        Directory.CreateDirectory("LogHistory");
                    }
                    File.Move("log.txt", Path.Combine("LogHistory", LogRotationHelper.GetFileNameByDate(File.GetLastWriteTime("log.txt"))));
                }
                else
                {
                    // log is empty! (this actually happens more often than you'd think, because of Steam re-opening Celeste)
                    // just delete it.
                    File.Delete("log.txt");
                }
            }

            using (Stream fileStream = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
                using (StreamWriter fileWriter = new StreamWriter(fileStream, Console.OutputEncoding))
                    using (LogWriter logWriter = new LogWriter {
                        STDOUT = Console.Out,
                        File = fileWriter
                    }) {
                        try {
                            Console.SetOut(logWriter);

                            MainInner(args);
                        } finally {
                            if (logWriter.STDOUT != null)
                            {
                                Console.SetOut(logWriter.STDOUT);
                                logWriter.STDOUT = null;
                            }
                        }
                    }
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            if (Thread.CurrentThread.Name != "Main Thread")
            {
                Thread.CurrentThread.Name = "Main Thread";
            }

            if (File.Exists("BuildIsXNA.txt"))
            {
                File.Delete("BuildIsXNA.txt");
            }
            if (File.Exists("BuildIsFNA.txt"))
            {
                File.Delete("BuildIsFNA.txt");
            }
            File.WriteAllText($"BuildIs{(typeof(Game).Assembly.FullName.Contains("FNA") ? "FNA" : "XNA")}.txt", "");

            // macOS is FUN.
            if (PlatformHelper.Is(MonoMod.Utils.Platform.MacOS))
            {
                // https://github.com/mono/mono/blob/79b6e3f256a59ede74596ce82547f320bf1e9a99/mono/metadata/filewatcher.c#L66
                Environment.SetEnvironmentVariable("MONO_DARWIN_USE_KQUEUE_FSW", "1");
            }

            if (File.Exists("everest-launch.txt"))
            {
                args =
                    File.ReadAllLines("everest-launch.txt")
                    .Select(l => l.Trim())
                    .Where(l => !l.StartsWith("#"))
                    .SelectMany(l => l.Split(' '))
                    .Concat(args)
                    .ToArray();
            }
            else
            {
                using (StreamWriter writer = File.CreateText("everest-launch.txt")) {
                    writer.WriteLine("# Add any Everest launch flags here.");
                    writer.WriteLine("# Lines starting with # are ignored.");
                    writer.WriteLine("# All options here are disabled by default.");
                    writer.WriteLine();
                    writer.WriteLine("# Windows only: open a separate log console window.");
                    writer.WriteLine("#--console");
                    writer.WriteLine();
                    writer.WriteLine("# FNA only: force OpenGL (might be necessary to bypass a load crash on some PCs).");
                    writer.WriteLine("#--graphics OpenGL");
                    writer.WriteLine();

                    if (File.Exists("launch.txt"))
                    {
                        using (StreamReader reader = File.OpenText("launch.txt")) {
                            writer.WriteLine();
                            writer.WriteLine();
                            writer.WriteLine("# The following options are migrated from the old launch.txt and force-disabled.");
                            writer.WriteLine("# Some of them might not work anymore or cause unwanted effects.");
                            writer.WriteLine();
                            writer.WriteLine();
                            for (string line; (line = reader.ReadLine()) != null;)
                            {
                                writer.Write("#");
                                writer.WriteLine(line);
                            }
                        }
                        File.Delete("launch.txt");
                    }
                }
            }

            if (File.Exists("everest-env.txt"))
            {
                foreach (string line in File.ReadAllLines("everest-env.txt"))
                {
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }

                    int index = line.IndexOf('=');
                    if (index == -1)
                    {
                        continue;
                    }

                    string key = line.Substring(0, index).Trim();
                    if (key.StartsWith("'") && key.EndsWith("'"))
                    {
                        key = key.Substring(1, key.Length - 2);
                    }

                    string value = line.Substring(index + 1).Trim();
                    if (value.StartsWith("'") && value.EndsWith("'"))
                    {
                        value = value.Substring(1, value.Length - 2);
                    }

                    if (key.EndsWith("!"))
                    {
                        key = key.Substring(0, key.Length - 1);
                    }
                    else
                    {
                        value = value
                                .Replace("\\r", "\r")
                                .Replace("\\n", "\n")
                                .Replace($"${{{key}}}", Environment.GetEnvironmentVariable(key) ?? "");
                    }

                    Environment.SetEnvironmentVariable(key, value);
                }
            }

            if (args.Contains("--console") && PlatformHelper.Is(MonoMod.Utils.Platform.Windows))
            {
                AllocConsole();
            }

            if (args.Contains("--nolog"))
            {
                MainInner(args);
                return;
            }

            if (File.Exists("log.txt"))
            {
                if (new FileInfo("log.txt").Length > 0)
                {
                    // move the old log.txt to the LogHistory folder.
                    // note that the cleanup will only be done when the core module is loaded: the settings aren't even loaded right now,
                    // so we don't know how many files we should keep.
                    if (!Directory.Exists("LogHistory"))
                    {
                        Directory.CreateDirectory("LogHistory");
                    }
                    File.Move("log.txt", Path.Combine("LogHistory", LogRotationHelper.GetFileNameByDate(File.GetLastWriteTime("log.txt"))));
                }
                else
                {
                    // log is empty! (this actually happens more often than you'd think, because of Steam re-opening Celeste)
                    // just delete it.
                    File.Delete("log.txt");
                }
            }

            using (Stream fileStream = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
                using (StreamWriter fileWriter = new StreamWriter(fileStream, Console.OutputEncoding))
                    using (LogWriter logWriter = new LogWriter {
                        STDOUT = Console.Out,
                        File = fileWriter
                    }) {
                        try {
                            Console.SetOut(logWriter);

                            MainInner(args);
                        } finally {
                            if (logWriter.STDOUT != null)
                            {
                                Console.SetOut(logWriter.STDOUT);
                                logWriter.STDOUT = null;
                            }
                        }
                    }
        }