Beispiel #1
0
        // Handles the restarting of stuff by checking if it crashes.
        private void HandleRestarting()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        var title = "Clashers' Republic Restarter v" + Assembly.GetExecutingAssembly().GetName().Version + " Count: " + RestartedTimes.Count;
                        Console.Title = title;

                        // Check if hasCrashed.
                        var hasCrashed = HasCrashed;
                        if (hasCrashed)
                        {
                            ConsoleUtils.WriteLineCenterRed("||");
                            ConsoleUtils.WriteLineCenterRed("Detected that " + ConfigurationManager.AppSettings["FileName"] + " has been crashed.");
                            ConsoleUtils.WriteLineCenterYellow("-> Restarting " + ConfigurationManager.AppSettings["FileName"] + " at " + DateTime.Now);

                            // Kill WerFault.exe to cause the EXE to exit.
                            var werFault = GetWerFaultProcess();
                            werFault.Kill();

                            Restart();
                        }

                        // Check if it has exited.
                        var hasExited = _process.HasExited;
                        if (hasExited)
                        {
                            ConsoleUtils.WriteLineCenterRed("||");
                            ConsoleUtils.WriteLineCenterRed("Detected that " + ConfigurationManager.AppSettings["FileName"] + " has been closed.");
                            ConsoleUtils.WriteLineCenterYellow("-> Restarting " + ConfigurationManager.AppSettings["FileName"] + " at " + DateTime.Now);

                            Restart();
                        }

                        // Be sure to sleep because we don't want to kill the CPU.
                        Thread.Sleep(100);
                    }
                    catch (Exception ex)
                    {
                        ConsoleUtils.WriteLineCenterRed("||");
                        ConsoleUtils.WriteLineCenterRed("Exception occurred while running.");

                        var exStr = ex.ToString().Split(Environment.NewLine.ToCharArray());
                        foreach (var str in exStr)
                        {
                            ConsoleUtils.WriteLineCenterRed(str);
                        }
                    }
                }
            }
            catch (ThreadAbortException)
            {
                // We don't care about these types of exceptions.
            }
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            const int  GWL_EXSTYLE   = -20;
            const int  WS_EX_LAYERED = 0x80000;
            const uint LWA_ALPHA     = 0x2;
            //int LWA_COLORKEY = 0x1;

            // Obtain our handle (hWnd)
            var Handle = GetConsoleWindow();

            SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED);
            // Opacity = 0.5 = (255/2)
            SetLayeredWindowAttributes(Handle, 0, 210, LWA_ALPHA);

            if (Console.WindowHeight != 35 || Console.WindowWidth != 130)
            {
                Console.SetWindowSize(130, 35);
            }

            Console.Title = "ObjectReset Restarter v" + Assembly.GetExecutingAssembly().GetName().Version +
                            " - Not Running";
            Console.Clear();
            ConsoleUtils.Welcome();

            // Make sure we have at least 1 argument.
            if (args.Length < 1)
            {
                ConsoleUtils.WriteLineCenterGreen("||");
                ConsoleUtils.WriteLineCenterGreen("Loaded file path from config: " +
                                                  ConfigurationManager.AppSettings["FileName"]);

                var filePath = ConfigurationManager.AppSettings["FileName"];
                args = new[]
                {
                    filePath
                };
            }

            // Make sure the argument (file) provided exists.
            if (!File.Exists(args[0]))
            {
                ConsoleUtils.WriteLineCenterRed("||");
                ConsoleUtils.WriteLineCenterRed(
                    $"File '{args[0]}' does not exists! Check your 'restarter.config' file and try again.");
                ConsoleUtils.WriteLineCenterYellow("Press ENTER to exit...");
                while (Console.ReadKey(true).Key != ConsoleKey.Enter)
                {
                    ;
                }

                Environment.Exit(0);
            }

            // Make sure the argument (file) provided is an .exe file.
            if (Path.GetExtension(args[0]) != ".exe")
            {
                ConsoleUtils.WriteLineCenterRed("||");
                ConsoleUtils.WriteLineCenterRed(
                    $"File '{args[0]}' is not a .exe! Check your 'restarter.config' file and try again.");
                ConsoleUtils.WriteLineCenterYellow("Press ENTER to exit...");

                while (Console.ReadKey(true).Key != ConsoleKey.Enter)
                {
                    ;
                }

                Environment.Exit(0);
            }

            // Make sure the argument (file) provided is not the restarter itself.
            var restarterEXE = Assembly.GetExecutingAssembly().ManifestModule.ToString();

            if (Path.GetFileName(args[0]) == restarterEXE)
            {
                ConsoleUtils.WriteLineCenterRed("||");
                ConsoleUtils.WriteLineCenterRed(
                    $"File '{args[0]}' is the restarter itself! Check your 'restarter.config' file and try again.");
                ConsoleUtils.WriteLineCenterYellow("Press ENTER to exit...");

                while (Console.ReadKey(true).Key != ConsoleKey.Enter)
                {
                    ;
                }

                Environment.Exit(0);
            }

            Console.Title = "Clashers' Republic Restarter";

            // Pass argument to the Restarter.
            Restarter = new Restarter(args[0]);
            Restarter.Start();

            Thread.Sleep(Timeout.Infinite);
        }