Files
Commonwealth-Online-Server/server/dedicated_server.py
T

62 lines
1.8 KiB
Python

#!/usr/bin/env python3
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from config import Config, load_config, validate_config
from server_service import ServerConfig, ServerService
def build_service_config(config: Config, config_path: Path) -> ServerConfig:
return ServerConfig(
host=config.host,
port=config.port,
server_name=config.server_name,
server_description=config.server_description,
max_players=config.max_players,
log_verbosity=config.log_verbosity,
admin_port=config.admin_port,
bans_path=str(config_path.parent / "bans.json"),
enable_gns_transport=config.enable_gns_transport,
gns_bridge_path=config.gns_bridge_path,
)
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Commonwealth Online dedicated server")
parser.add_argument(
"--config",
default="commonwealth-server.json",
help="Path to the server JSON configuration file",
)
args = parser.parse_args(argv)
config_path = Path(args.config).expanduser().resolve()
try:
config = load_config(str(config_path))
except Exception as error:
print(f"[ERROR] Could not load {config_path}: {error}", file=sys.stderr)
return 2
valid, errors = validate_config(config)
if not valid:
for error in errors:
print(f"[ERROR] {error}", file=sys.stderr)
return 2
service = ServerService(build_service_config(config, config_path))
service.add_log_listener(lambda message, **_: print(message, flush=True))
try:
service.serve_forever()
except KeyboardInterrupt:
pass
finally:
service.stop()
return 0
if __name__ == "__main__":
raise SystemExit(main())