static void Main(string[] args) { string configurationImportFile = null; for (int i = 0; i < args.Length; i++) { switch (args[i]) { case "-import": case "-i": configurationImportFile = args[++i]; break; case "-config": case "-c": ConfigurationFile = args[++i]; break; } } using (CancellationTokenSource cancellationSource = new CancellationTokenSource()) using (Starwatch = new StarboundHandler(Configuration.FromFile(ConfigurationFile))) { //Import the previous starbound configuration. if (!string.IsNullOrEmpty(configurationImportFile)) { Console.WriteLine("Preparing Initializer..."); Starwatch.Initialize().Wait(); Console.WriteLine("Importing..."); Starwatch.Server.Configurator.ImportSettingsAsync(configurationImportFile).Wait(); Console.WriteLine("Deinitializing..."); Starwatch.Deinitialize().Wait(); Console.WriteLine("Done!"); return; } //Regular Starwatch instance var context = Starwatch.Run(cancellationSource.Token).ConfigureAwait(false); //infinite loop to proces lines bool process = true; while (process && !context.GetAwaiter().IsCompleted) { string line = Console.ReadLine(); switch (line) { case "stop": case "exit": case "abort": case "quit": Console.WriteLine("Cancelling Token..."); Starwatch.KeepAlive = false; Starwatch?.Server?.Terminate("CLI Stop").Wait(); cancellationSource.Cancel(); process = false; break; case "restart": Starwatch?.Server.Terminate("CLI Restart..."); break; } } //We exited, so lets close things down Console.WriteLine("Done!"); } }
private void RunStarwatch(string[] args) { Starwatch = new StarboundHandler(Configuration.FromFile("Starwatch.json")); var task = Task.Run(async() => { await Starwatch.Run(tokenSource.Token); }, tokenSource.Token); while (readLines && !(task.IsCompleted || task.IsCanceled)) { string line = Console.ReadLine(); if (line.StartsWith("rcon ")) { Console.WriteLine("Attempting Rcon..."); var response = Starwatch?.Server?.Rcon?.ExecuteAsync(line.Substring(5)).Result; Console.WriteLine("Response: {0}", response.HasValue && response.Value.Success); if (response.HasValue) { Console.WriteLine(response.Value.Message); } } else { switch (line) { default: Console.WriteLine("Unkown Command: {0}", line); break; case "restart": Console.WriteLine("Restarting Server..."); Starwatch?.Server?.Terminate("CLI Restart").Wait(); break; case "import": var plevel = Starwatch.Logger.Level; Starwatch.Logger.Level = Logging.Logger.LogLevel.None; Console.WriteLine("Import Filepath [y/n]: "); string importFilepath = Console.ReadLine(); if (!string.IsNullOrWhiteSpace(importFilepath)) { if (File.Exists(importFilepath)) { Console.WriteLine("Triggering Import..."); Starwatch.Logger.Level = plevel; Starwatch?.Server?.Configurator?.ImportSettingsAsync(importFilepath).Wait(); } else { Console.WriteLine("File " + importFilepath + " does not exist."); } } Starwatch.Logger.Level = plevel; Console.WriteLine("Done"); break; case "process-worlds": if (Starwatch == null || Starwatch.Server == null) { Console.WriteLine("Cannot process all worlds because the server doesnt exist."); break; } Console.WriteLine("Processing All Worlds..."); foreach (var filepath in Directory.EnumerateFiles(Starwatch.Server.StorageDirectory + "/universe/", "*.world")) { //Generate the world Console.WriteLine("Processing " + filepath); var world = World.ParseFilename(filepath) as CelestialWorld; //Save the world try { world.GetDetailsAsync(Starwatch.Server).Wait(); Console.WriteLine(" = " + world.Details?.Name); } catch (Exception e) { Console.WriteLine("ERROR: " + e.Message); } } Console.WriteLine("DONE"); break; case "exit": case "quit": case "stop": Console.WriteLine("Cancelling Token..."); Starwatch.KeepAlive = false; Starwatch?.Server?.Terminate("CLI Stop").Wait(); tokenSource.Cancel(); readLines = false; break; } } } Console.WriteLine("Waiting for task to finish... {0}", task.IsCanceled); task.Wait(); Console.WriteLine("Goodbye!\nPress any key to exit..."); Console.ReadKey(); }