From a340bcf1dd4ebde9b7a2c204fab6c4b4707900cd Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Sat, 4 Mar 2023 21:27:52 -0500 Subject: [PATCH] change how allnet uri/host is generated --- core/allnet.py | 79 +++++++++++++++++++++----------------- core/title.py | 20 +++++++--- example_config/pokken.yaml | 4 ++ titles/pokken/__init__.py | 11 ------ titles/pokken/base.py | 14 +++---- titles/pokken/config.py | 18 ++++++++- titles/pokken/index.py | 14 +++++++ 7 files changed, 101 insertions(+), 59 deletions(-) diff --git a/core/allnet.py b/core/allnet.py index 71eeb33..792cdff 100644 --- a/core/allnet.py +++ b/core/allnet.py @@ -48,49 +48,58 @@ class AllnetServlet: self.logger.error("No games detected!") for _, mod in plugins.items(): - for code in mod.game_codes: - if hasattr(mod, "use_default_title") and mod.use_default_title: - if hasattr(mod, "include_protocol") and mod.include_protocol: - if hasattr(mod, "title_secure") and mod.title_secure: - uri = "https://" + if hasattr(mod.index, "get_allnet_info"): + for code in mod.game_codes: + enabled, uri, host = mod.index.get_allnet_info(code, self.config, self.config_folder) + + if enabled: + self.uri_registry[code] = (uri, host) + + else: + for code in mod.game_codes: + if hasattr(mod, "use_default_title") and mod.use_default_title: + if hasattr(mod, "include_protocol") and mod.include_protocol: + if hasattr(mod, "title_secure") and mod.title_secure: + uri = "https://" + + else: + uri = "http://" + + else: + uri = "" + + if core_cfg.server.is_develop: + uri += f"{core_cfg.title.hostname}:{core_cfg.title.port}" else: - uri = "http://" + uri += f"{core_cfg.title.hostname}" + + uri += f"/{code}/$v" - else: - uri = "" - - if core_cfg.server.is_develop: - uri += f"{core_cfg.title.hostname}:{core_cfg.title.port}" + if hasattr(mod, "trailing_slash") and mod.trailing_slash: + uri += "/" else: - uri += f"{core_cfg.title.hostname}" - - uri += f"/{code}/$v" + if hasattr(mod, "uri"): + uri = mod.uri + else: + uri = "" - if hasattr(mod, "trailing_slash") and mod.trailing_slash: - uri += "/" - - else: - if hasattr(mod, "uri"): - uri = mod.uri - else: - uri = "" - - if hasattr(mod, "host"): - host = mod.host - - elif hasattr(mod, "use_default_host") and mod.use_default_host: - if core_cfg.server.is_develop: - host = f"{core_cfg.title.hostname}:{core_cfg.title.port}" + if hasattr(mod, "host"): + host = mod.host + + elif hasattr(mod, "use_default_host") and mod.use_default_host: + if core_cfg.server.is_develop: + host = f"{core_cfg.title.hostname}:{core_cfg.title.port}" + + else: + host = f"{core_cfg.title.hostname}" else: - host = f"{core_cfg.title.hostname}" - - else: - host = "" - - self.uri_registry[code] = (uri, host) + host = "" + + self.uri_registry[code] = (uri, host) + self.logger.info(f"Allnet serving {len(self.uri_registry)} games on port {core_cfg.allnet.port}") def handle_poweron(self, request: Request, _: Dict): diff --git a/core/title.py b/core/title.py index 252a130..c704e9a 100644 --- a/core/title.py +++ b/core/title.py @@ -37,16 +37,26 @@ class TitleServlet(): for folder, mod in plugins.items(): if hasattr(mod, "game_codes") and hasattr(mod, "index"): - handler_cls = mod.index(self.config, self.config_folder) - if hasattr(handler_cls, "setup"): - handler_cls.setup() + should_call_setup = True for code in mod.game_codes: - self.title_registry[code] = handler_cls + if hasattr(mod.index, "get_allnet_info"): + enabled, _, _ = mod.index.get_allnet_info(code, self.config, self.config_folder) + + else: + enabled = True + + if enabled: + handler_cls = mod.index(self.config, self.config_folder) + if hasattr(handler_cls, "setup") and should_call_setup: + handler_cls.setup() + should_call_setup = False + + self.title_registry[code] = handler_cls else: self.logger.error(f"{folder} missing game_code or index in __init__.py") - + self.logger.info(f"Serving {len(self.title_registry)} game codes on port {core_cfg.title.port}") def render_GET(self, request: Request, endpoints: dict) -> bytes: diff --git a/example_config/pokken.yaml b/example_config/pokken.yaml index 5523996..e465ceb 100644 --- a/example_config/pokken.yaml +++ b/example_config/pokken.yaml @@ -1,7 +1,11 @@ server: + hostname: "localhost" enable: True loglevel: "info" port: 9000 port_matching: 9001 + port_stun: 9002 + port_turn: 9003 + port_admission: 9004 ssl_cert: cert/pokken.crt ssl_key: cert/pokken.key \ No newline at end of file diff --git a/titles/pokken/__init__.py b/titles/pokken/__init__.py index 3b574fd..6340de8 100644 --- a/titles/pokken/__init__.py +++ b/titles/pokken/__init__.py @@ -4,16 +4,5 @@ from titles.pokken.database import PokkenData index = PokkenServlet database = PokkenData - -use_default_title = True -include_protocol = True -title_secure = True game_codes = [PokkenConstants.GAME_CODE] -trailing_slash = True -use_default_host = False - -include_port = True -uri="https://$h:$p/" -host="$h:$p/" - current_schema_version = 1 \ No newline at end of file diff --git a/titles/pokken/base.py b/titles/pokken/base.py index bd95295..33232e4 100644 --- a/titles/pokken/base.py +++ b/titles/pokken/base.py @@ -35,19 +35,19 @@ class PokkenBase(): regist_pcb.server_time = int(datetime.now().timestamp() / 1000) biwa_setting = { "MatchingServer": { - "host": f"https://{self.core_cfg.title.hostname}", - "port": 9000, + "host": f"https://{self.game_cfg.server.hostname}", + "port": self.game_cfg.server.port_matching, "url": "/matching" }, "StunServer": { - "addr": self.core_cfg.title.hostname, - "port": 3333 + "addr": self.game_cfg.server.hostname, + "port": self.game_cfg.server.port_stun }, "TurnServer": { - "addr": self.core_cfg.title.hostname, - "port": 4444 + "addr": self.game_cfg.server.hostname, + "port": self.game_cfg.server.port_turn }, - "AdmissionUrl": f"ws://{self.core_cfg.title.hostname}:1111", + "AdmissionUrl": f"ws://{self.game_cfg.server.hostname}:{self.game_cfg.server.port_admission}", "locationId": 123, "logfilename": "JackalMatchingLibrary.log", "biwalogfilename": "./biwa.log" diff --git a/titles/pokken/config.py b/titles/pokken/config.py index b6596f2..9b2ae0f 100644 --- a/titles/pokken/config.py +++ b/titles/pokken/config.py @@ -4,6 +4,10 @@ class PokkenServerConfig(): def __init__(self, parent_config: "PokkenConfig"): self.__config = parent_config + @property + def hostname(self) -> str: + return CoreConfig.get_config_field(self.__config, 'pokken', 'server', 'hostname', default="localhost") + @property def enable(self) -> bool: return CoreConfig.get_config_field(self.__config, 'pokken', 'server', 'enable', default=True) @@ -18,7 +22,19 @@ class PokkenServerConfig(): @property def port_matching(self) -> int: - return CoreConfig.get_config_field(self.__config, 'pokken', 'server', 'port', default=9001) + return CoreConfig.get_config_field(self.__config, 'pokken', 'server', 'port_matching', default=9001) + + @property + def port_stun(self) -> int: + return CoreConfig.get_config_field(self.__config, 'pokken', 'server', 'port_stun', default=9002) + + @property + def port_turn(self) -> int: + return CoreConfig.get_config_field(self.__config, 'pokken', 'server', 'port_turn', default=9003) + + @property + def port_admission(self) -> int: + return CoreConfig.get_config_field(self.__config, 'pokken', 'server', 'port_admission', default=9004) @property def ssl_cert(self) -> str: diff --git a/titles/pokken/index.py b/titles/pokken/index.py index a47cac7..13fdf6c 100644 --- a/titles/pokken/index.py +++ b/titles/pokken/index.py @@ -1,3 +1,4 @@ +from typing import Tuple from twisted.web.http import Request from twisted.web import resource, server from twisted.internet import reactor, endpoints @@ -40,6 +41,19 @@ class PokkenServlet(resource.Resource): self.logger.inited = True self.base = PokkenBase(core_cfg, self.game_cfg) + + @classmethod + def get_allnet_info(cls, game_code: str, core_cfg: CoreConfig, cfg_dir: str) -> Tuple[bool, str, str]: + game_cfg = PokkenConfig() + game_cfg.update(yaml.safe_load(open(f"{cfg_dir}/pokken.yaml"))) + + if not game_cfg.server.enable: + return (False, "", "") + + if core_cfg.server.is_develop: + return (True, f"https://{game_cfg.server.hostname}:{game_cfg.server.port}/{game_code}/$v/", f"{game_cfg.server.hostname}:{game_cfg.server.port}/") + + return (True, f"https://{game_cfg.server.hostname}/{game_code}/$v/", f"{game_cfg.server.hostname}/") def setup(self): """