## Command-line configuration for a headless server. ## ## tools/godot.sh -- --server [--seed=N] [--port=N] [--players=N] [--bots=N] ## [--bot-difficulty=mixed|easy|normal|hard] [--ticks=N] ## [--unthrottled] [--log-every=N] ## ## Unknown or malformed arguments are errors, so a typo can't silently run the wrong ## match. See `parse()`. class_name ServerConfig extends RefCounted const DEFAULT_PORT: int = 24650 ## The `--fault` names `HeadlessServer` understands. They exist so tools/smoke.sh can ## prove a script error really produces a non-zero exit. const FAULTS: Array[String] = ["", "script-error"] ## True when `--server` was given. var server: bool = false ## The match seed. `has_seed` is false when none was given and the server must pick one. var match_seed: int = 0 var has_seed: bool = false ## UDP port the server will listen on once networking lands (v0.5). var port: int = DEFAULT_PORT ## Player slots for human clients. Until v0.5 they join as idle players. var players: int = 0 var bots: int = 0 ## Every bot's difficulty, as an index into Tuning.BOT_DIFFICULTIES, or -1 to mix them. var bot_difficulty: int = -1 ## Ticks to run before exiting. 0 runs until the process is stopped. var ticks: int = 0 ## Runs ticks back to back instead of at the real tick rate. For tests and smoke runs. var unthrottled: bool = false ## Logs a progress line every this many ticks. 0 turns progress lines off. var log_every: int = 0 var fault: String = "" ## Human-readable problems found while parsing. Empty when the config is usable. var errors: Array[String] = [] ## Parses arguments. Arguments before `--server` are ignored unless `--server` is absent, ## in which case nothing is parsed and `server` stays false. static func parse(args: PackedStringArray) -> ServerConfig: var config: ServerConfig = ServerConfig.new() var start: int = args.find("--server") if start < 0: return config config.server = true for index: int in range(start + 1, args.size()): config._parse_one(args[index]) config._validate() return config func is_valid() -> bool: return errors.is_empty() ## The values that matter for reproducing a run, for the start log line. func to_dictionary() -> Dictionary: return { "seed": match_seed, "port": port, "players": players, "bots": bots, "bot_difficulty": "mixed" if bot_difficulty < 0 else String(Tuning.BOT_DIFFICULTIES[bot_difficulty]["name"]), "ticks": ticks, "unthrottled": unthrottled, } func _parse_one(arg: String) -> void: if arg == "--unthrottled": unthrottled = true return var parts: PackedStringArray = arg.split("=", true, 1) if parts.size() != 2 or not arg.begins_with("--"): errors.append("unknown argument '%s'" % arg) return var key: String = parts[0] var value: String = parts[1] match key: "--seed": if _check_int(key, value): match_seed = value.to_int() has_seed = true "--port": if _check_int(key, value): port = value.to_int() "--players": if _check_int(key, value): players = value.to_int() "--bots": if _check_int(key, value): bots = value.to_int() "--ticks": if _check_int(key, value): ticks = value.to_int() "--log-every": if _check_int(key, value): log_every = value.to_int() "--bot-difficulty": var found: bool = value == "mixed" for index: int in Tuning.BOT_DIFFICULTIES.size(): if Tuning.BOT_DIFFICULTIES[index]["name"] == value: bot_difficulty = index found = true if not found: errors.append("--bot-difficulty must be mixed, easy, normal or hard, got '%s'" % value) "--fault": fault = value _: errors.append("unknown argument '%s'" % arg) func _check_int(key: String, value: String) -> bool: if value.is_valid_int(): return true errors.append("%s needs a whole number, got '%s'" % [key, value]) return false func _validate() -> void: if port < 1 or port > 65535: errors.append("--port must be between 1 and 65535, got %d" % port) if players < 0 or bots < 0: errors.append("--players and --bots can't be negative") if players + bots > Tuning.SOLO_PLAYERS: errors.append( "--players plus --bots is %d, more than the match size of %d" % [players + bots, Tuning.SOLO_PLAYERS] ) if ticks < 0: errors.append("--ticks can't be negative") if log_every < 0: errors.append("--log-every can't be negative") if fault not in FAULTS: errors.append("unknown --fault '%s'" % fault)