예제 #1
0
        private static void Main(string[] args)
        {
            try
            {
                if (!ParseArguments(args, out bool isVerbError))
                {
                    bool exit = true;
                    if (isVerbError)
                    {
                        Console.WriteLine("No command was found");
                        Console.WriteLine("Check if your scripts are updated with 'run' command before other parameters");
                        Console.WriteLine();
                        Console.WriteLine("If you want to start bot with 'run' command added, press Enter");
                        Console.WriteLine("Please note that this option is added for compatibility with older scripts and will be removed soon");
                        Console.WriteLine("Press any other key to exit");
                        while (Console.KeyAvailable)
                        {
                            Console.ReadKey(true);
                        }
                        if (Console.ReadKey(true).Key == ConsoleKey.Enter)
                        {
                            Console.Clear();
                            if (ParseArguments(args.Prepend("run"), out _))
                            {
                                exit = false;
                            }
                        }
                    }
                    if (exit)
                    {
                        return;
                    }
                }

                logger = new Logger(options?.LogFilePath, finishCTS.Token)
                {
                    ShowDebugLogs = options?.ShowDebugLogs ?? false
                };
                logger.LogDebug("Command line: " + Environment.CommandLine);
                HttpWrapper.Logger = logger;

                if (checkUpdates || !options.DisableUpdates)
                {
                    if (UpdateChecker.IsStartingUpdate(logger, checkUpdates) || checkUpdates)
                    {
                        return;
                    }
                }

                try
                {
                    imagePixels = ImageProcessing.PixelColorsByUri(options.ImagePath, logger);
                }
                catch (WebException ex)
                {
                    logger.LogError("Cannot download image");
                    logger.LogDebug($"Main(): error downloading image - {ex.Message}");
                    return;
                }
                catch (ArgumentException ex)
                {
                    logger.LogError("Cannot convert image");
                    logger.LogDebug($"Main(): error converting image - {ex.Message}");
                    return;
                }

                try
                {
                    checked
                    {
                        width  = (ushort)imagePixels.GetLength(0);
                        height = (ushort)imagePixels.GetLength(1);
                        short check;
                        check = (short)(options.LeftX + width);
                        check = (short)(options.TopY + height);
                    }
                }
                catch (OverflowException ex)
                {
                    logger.LogError("Entire image should be inside the map");
                    logger.LogDebug($"Main(): error checking boundaries - {ex.Message}");
                    return;
                }

                logger.LogTechState("Calculating pixel placing order...");
                if (!CalculatePixelOrder())
                {
                    return;
                }
                logger.LogTechInfo("Pixel placing order is calculated");

                cache = new ChunkCache(pixelsToBuild, logger);
                mapUpdatedResetEvent = new ManualResetEvent(false);
                cache.OnMapUpdated  += (o, e) =>
                {
                    logger.LogDebug("Cache.OnMapUpdated handler: Map updated event received");
                    mapUpdatedResetEvent.Set();
                };
                if (options.DefenseMode)
                {
                    gotGriefed          = new AutoResetEvent(false);
                    cache.OnMapUpdated += (o, e) => gotGriefed.Set();
                    waitingGriefLock    = new object();
                }
                statsThread = new Thread(StatsCollectionThreadBody);
                statsThread.Start();
                do
                {
                    try
                    {
                        HttpWrapper.ConnectToApi();
                        MainWorkingBody();
                        return;
                    }
                    catch (PausingException ex)
                    {
                        logger.LogError($"Unhandled exception: {ex.Message}");
                        logger.LogInfo($"Check that problem is resolved and press any key to continue");
                        while (Console.KeyAvailable)
                        {
                            Console.ReadKey(true);
                        }
                        Console.ReadKey(true);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError($"Unhandled exception: {ex.Message}");
                        int delay = repeatingFails ? 30 : 10;
                        repeatingFails = true;
                        logger.LogTechState($"Reconnecting in {delay} seconds...");
                        Thread.Sleep(TimeSpan.FromSeconds(delay));
                        continue;
                    }
                } while (true);
            }
            catch (Exception ex)
            {
                string msg = $"Unhandled app level exception: {ex.Message}";
                if (logger != null)
                {
                    logger.LogError(msg);
                }
                else
                {
                    Console.WriteLine(msg);
                }
            }
            finally
            {
                if (logger != null)
                {
                    logger.LogInfo("Exiting...");
                    logger.LogInfo($"Logs were saved to {logger.LogFilePath}");
                }
                finishCTS.Cancel();
                if (logger != null)
                {
                    Thread.Sleep(500);
                }
                gotGriefed?.Dispose();
                mapUpdatedResetEvent?.Dispose();
                logger?.Dispose();
                finishCTS.Dispose();
                Console.ForegroundColor = ConsoleColor.White;
                Environment.Exit(0);
            }
        }
예제 #2
0
        private static void Main(string[] args)
        {
            try
            {
                if (!ParseArguments(args, out bool isVerbError))
                {
                    bool exit = true;
                    if (isVerbError)
                    {
                        Console.WriteLine("No command were found");
                        Console.WriteLine("Check if your scripts are updated with 'run' command before other parameters");
                        Console.WriteLine();
                        Console.WriteLine("If you want to start app with 'run' command added, press Enter");
                        Console.WriteLine("Please note that this option is added for compatibility with older scripts and will be removed soon");
                        Console.WriteLine("Press any other key to exit");
                        while (Console.KeyAvailable)
                        {
                            Console.ReadKey(true);
                        }
                        if (Console.ReadKey(true).Key == ConsoleKey.Enter)
                        {
                            Console.Clear();
                            if (ParseArguments(args.Prepend("run"), out _))
                            {
                                exit = false;
                            }
                        }
                    }
                    if (exit)
                    {
                        return;
                    }
                }

                logger = new Logger(options?.LogFilePath, finishCTS.Token)
                {
                    ShowDebugLogs = options?.ShowDebugLogs ?? false
                };
                logger.LogDebug("Command line: " + Environment.CommandLine);
                HttpWrapper.Logger = logger;

                if (checkUpdates || !options.DisableUpdates)
                {
                    if (UpdateChecker.IsStartingUpdate(logger, checkUpdates) || checkUpdates)
                    {
                        return;
                    }
                }

                cache = new ChunkCache(options.LeftX, options.TopY, options.RightX, options.BottomY, logger);
                bool initialMapSavingStarted = false;
                saveThread = new Thread(SaveChangesThreadBody);
                saveThread.Start();
                if (string.IsNullOrWhiteSpace(options.FileName))
                {
                    options.FileName = string.Format("pixels_({0};{1})-({2};{3})_{4:yyyy.MM.dd_HH-mm}.bin", options.LeftX, options.TopY, options.RightX, options.BottomY, DateTime.Now);
                }
                Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(options.FileName)));
                do
                {
                    try
                    {
                        HttpWrapper.ConnectToApi();
                        using (WebsocketWrapper wrapper = new WebsocketWrapper(logger, true))
                        {
                            cache.Wrapper = wrapper;
                            if (!initialMapSavingStarted)
                            {
                                logger.LogDebug("Main(): initiating map saving");
                                initialMapSavingStarted = true;
                                lockingStreamTask       = Task.Run(SaveInitialMapState);
                            }
                            wrapper.OnPixelChanged += Wrapper_OnPixelChanged;
                            stopListening           = wrapper.StopListening;
                            Console.CancelKeyPress += (o, e) =>
                            {
                                logger.LogDebug("Console.CancelKeyPress received");
                                e.Cancel = true;
                                wrapper.StopListening();
                            };
                            logger.LogInfo("Press Ctrl+C to stop");
                            wrapper.StartListening();
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.LogError($"Unhandled exception: {ex.Message}");
                        Thread.Sleep(1000);
                    }
                } while (true);
            }
            catch (Exception ex)
            {
                logger?.LogError($"Unhandled app level exception: {ex.Message}");
            }
            finally
            {
                if (logger != null)
                {
                    logger.LogInfo("Exiting when everything is saved...");
                    logger.LogInfo($"Logs were saved to {logger.LogFilePath}");
                }
                finishCTS.Cancel();
                if (logger != null)
                {
                    Thread.Sleep(500);
                }
                finishCTS.Dispose();
                logger?.Dispose();
                if (saveThread != null && !saveThread.Join(TimeSpan.FromMinutes(1)))
                {
                    Console.WriteLine("Save thread doesn't finish, aborting");
                }
                Console.ForegroundColor = ConsoleColor.White;
                Environment.Exit(0);
            }
        }