Exemplo n.º 1
0
        /// <summary>
        /// Main.
        /// </summary>
        /// <param name="args">Args from the command line.</param>
        static void Main(string[] args)
        {
            try
            {
                // disallow ability to close this console window while we're running
                DisableConsoleExit();

                // set console properties and display header
                System.Console.Title = "Oberon - Deviant Art Chat Bot";
                System.Console.WindowHeight = System.Console.LargestWindowHeight / 2;
                System.Console.Clear();
                System.Console.WriteLine(new string('*', System.Console.WindowWidth - 1));
                System.Console.WriteLine();
                System.Console.WriteLine(" Oberon - Deviant Art Chat Bot");
                System.Console.WriteLine();
                System.Console.WriteLine(new string('*', System.Console.WindowWidth - 1));
                System.Console.WriteLine(" In order to close this program type 'exit' or 'quit'.");
                System.Console.WriteLine(" See http://oberon.thehomeofjon.net for the latest bot info.");
                System.Console.WriteLine(new string('*', System.Console.WindowWidth - 1));

                // ensure we only have one instance running
                if (IsPriorProcessRunning())
                {
                    System.Console.WriteLine("HEY! Only one instance of the application can be started.");
                    System.Console.WriteLine("Shutting down in 10 seconds.");
                    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));
                    return;
                }

                // create background worker to run the bot
                var botWorker = new BackgroundWorker();
                botWorker.WorkerReportsProgress = false;
                botWorker.WorkerSupportsCancellation = false;
                botWorker.DoWork += (sender, a) =>
                {
                    // create our IoC container
                    IUnityContainer globalContainer = new UnityContainer();

                    // setup the default bot dependencies
                    Bot.Setup(globalContainer);

                    // get a reference to the bot
                    Bot = globalContainer.Resolve<Bot>();

                    // check for updates
                    Bot.NotifyUserOfBotUpdates();

                    // run the bot
                    Bot.Run();

                    // keep worker running until bot shuts down
                    while (Bot.IsAlive) ;
                };

                // start the background worker
                botWorker.RunWorkerAsync();

                // listen for user input from the console
                while (true)
                {
                    string input = ReadFromConsole();

                    if (!string.IsNullOrEmpty(input))
                    {
                        // if we receive the command, then shutdown
                        if (input.ToUpperInvariant() == "EXIT" ||
                            input.ToUpperInvariant() == "QUIT")
                        {
                            Bot.Console.Notice("Initiating shutdown.");

                            // send the shutdown command
                            Bot.Shutdown();

                            // since we're the main thread, don't exit until
                            // the bot itself shuts down so we don't have an
                            // orphaned thread gets left or gets terminated too
                            // early.
                            while (Bot.IsAlive) ;

                            break;
                        }
                    }

                    // check and make sure bot isn't dead
                    if (Bot != null && Bot.IsAlive == false)
                        break;
                }

                // display exit message
                System.Console.WriteLine(new string('*', System.Console.WindowWidth - 1));
                System.Console.WriteLine("Console closing.");
            }
            catch (Exception ex)
            {
                // dump exception to log file
                try
                {
                    string currentDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) ?? string.Empty;
                    string errorFile = Path.Combine(currentDirectory, "error.log");
                    string header = "Fatal Error occured. See the detailed exception below." + Environment.NewLine +
                                    "------------------------------------------------------" + Environment.NewLine;
                    File.WriteAllText(errorFile, header + ex.ToString());
                }
                catch { }

                // errors shouldn't bubble up to here, but if they do, fail gracfully.
                System.Console.WriteLine("Fatal Error occurred. See error.log file in the bot directory for details.");
                System.Console.WriteLine("Shutting down in 10 seconds...");
                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10));
            }
        }