#if !WPF using System.Net; namespace codelanguage; public static class HeadlessProgram { public static int Main(string[] args) { if (Array.Exists(args, arg => string.Equals(arg, "--parity-smoke", StringComparison.OrdinalIgnoreCase))) return RunParitySmoke(); if (HasFlag(args, "--daemon")) return RunDaemon(args); Console.Error.WriteLine("Usage: Craft++ --parity-smoke"); Console.Error.WriteLine(" Craft++ --daemon [--port N] [--hub-url URL] [--hub-path account.game] [--hub-password SECRET] [--hub-title TITLE] [--hub-visibility 0|1|2] [--public-address HOST] [--safe|--trusted]"); return 2; } private static int RunParitySmoke() { try { ByondHeadlessSmoke.VerifyAsync().GetAwaiter().GetResult(); var audit = ByondParityAudit.MeasureApiSurface(); Console.WriteLine($"Global procedure handlers registered: {audit.GlobalProcedures.ImplementedProcedures}/{audit.GlobalProcedures.DocumentedProcedures}"); Console.WriteLine($"Modern type fields registered: {audit.ModernTypeSurface.ImplementedFields}/{audit.ModernTypeSurface.DocumentedFields}"); Console.WriteLine($"Type procedures registered: {audit.TypeProcedures.ImplementedFields}/{audit.TypeProcedures.DocumentedFields}"); Console.WriteLine("Craft++ API registration smoke: PASS (registration is not behavioral parity)"); return 0; } catch (Exception ex) { Console.Error.WriteLine($"Craft++ registration smoke: FAIL\n{ex.Message}"); return 1; } } private static int RunDaemon(string[] args) { string? world = ValueAfter(args, "--daemon"); if (string.IsNullOrWhiteSpace(world) || world.StartsWith("--", StringComparison.Ordinal)) { Console.Error.WriteLine("Craft++ daemon needs a world file after --daemon."); return 2; } int port = 6112; string? portText = ValueAfter(args, "--port"); if (!string.IsNullOrWhiteSpace(portText) && !int.TryParse(portText, out port)) { Console.Error.WriteLine("Invalid --port."); return 2; } var options = new WorldHostOptions { HubUrl = ValueAfter(args, "--hub-url"), HubPath = ValueAfter(args, "--hub-path"), HubPassword = ValueAfter(args, "--hub-password"), HubTitle = ValueAfter(args, "--hub-title"), HubStatus = "Online", HubVisibility = NullableIntAfter(args, "--hub-visibility"), PublicAddress = ValueAfter(args, "--public-address"), TrustedMode = !HasFlag(args, "--safe"), PublishToHub = !string.IsNullOrWhiteSpace(ValueAfter(args, "--hub-path")) }; using var stop = new CancellationTokenSource(); Console.CancelKeyPress += (_, eventArgs) => { eventArgs.Cancel = true; stop.Cancel(); }; var host = new WorldHost { TrustedMode = options.TrustedMode }; host.Logged += line => Console.WriteLine(line); try { host.LoadAsync(world).GetAwaiter().GetResult(); host.StartAsync(port, options).GetAwaiter().GetResult(); var bound = host.Server; Console.WriteLine($"Craft++ Daemon hosting {Path.GetFileName(world)} on {bound?.BindAddress}:{bound?.Port}."); Console.WriteLine("Press Ctrl+C to stop."); Task.Delay(Timeout.Infinite, stop.Token).GetAwaiter().GetResult(); } catch (OperationCanceledException) { Console.WriteLine("Stopping."); } catch (Exception ex) { Console.Error.WriteLine(ex.Message); host.DisposeAsync().AsTask().GetAwaiter().GetResult(); return 1; } host.DisposeAsync().AsTask().GetAwaiter().GetResult(); return 0; } private static bool HasFlag(string[] args, string flag) => Array.Exists(args, arg => string.Equals(arg, flag, StringComparison.OrdinalIgnoreCase)); private static string? ValueAfter(string[] args, string flag) { for (int i = 0; i < args.Length - 1; i++) { if (string.Equals(args[i], flag, StringComparison.OrdinalIgnoreCase)) return args[i + 1]; } return null; } private static int? NullableIntAfter(string[] args, string flag) => int.TryParse(ValueAfter(args, flag), out int value) ? value : null; } #endif