private static async Task Main(string[] args) { try { if (!ParseArguments(args)) { return; } logger = new Logger(options?.LogFilePath, finishCTS.Token) { ShowDebugLogs = options?.ShowDebugLogs ?? false }; logger.LogDebug("Command line: " + Environment.CommandLine); logger.LogTechState("Connecting to API..."); PixelPlanetHttpApi api = new PixelPlanetHttpApi { ProxySettings = proxySettings }; user = await api.GetMeAsync(); logger.LogTechInfo("Successfully connected"); CanvasModel canvas = user.Canvases[options.Canvas]; if (canvas.Is3D) { throw new Exception("3D canvas is not supported"); } LoggerExtensions.MaxCoordXYLength = 1 + (int)Math.Log10(canvas.Size / 2); PixelMap.MapSize = canvas.Size; try { if (options.LeftX < -(canvas.Size / 2) || options.RightX >= canvas.Size / 2) { throw new Exception("X"); } if (options.TopY < -(canvas.Size / 2) || options.BottomY >= canvas.Size / 2) { throw new Exception("Y"); } } catch (Exception ex) { throw new Exception($"Entire rectangle should be inside the map (failed by {ex.Message})"); } colorNameResolver = new ColorNameResolver(options.Canvas); if (checkUpdates || !options.DisableUpdates) { if (UpdateChecker.IsStartingUpdate(logger, checkUpdates) || checkUpdates) { return; } } cache = new ChunkCache2D(options.LeftX, options.TopY, options.RightX, options.BottomY, logger, options.Canvas); 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 { using (WebsocketWrapper wrapper = new WebsocketWrapper(logger, true, proxySettings, null, options.Canvas)) { cache.Wrapper = wrapper; if (!initialMapSavingStarted) { logger.LogDebug("Main(): initiating map saving"); initialMapSavingStarted = true; lockingStreamTask = Task.Run(SaveInitialMapState); } wrapper.OnMapChanged += Wrapper_OnMapChanged; 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}"); logger?.LogDebug(ex.ToString()); } 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(); Console.ForegroundColor = ConsoleColor.White; if (saveThread != null && !saveThread.Join(TimeSpan.FromMinutes(1))) { Console.WriteLine("Save thread doesn't finish, aborting"); Environment.Exit(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); } }