2023-02-17 07:02:21 +01:00
|
|
|
from typing import Dict, Any
|
2023-02-16 06:06:42 +01:00
|
|
|
import logging, coloredlogs
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
2023-02-16 23:13:41 +01:00
|
|
|
from twisted.web.http import Request
|
2023-02-16 06:06:42 +01:00
|
|
|
|
|
|
|
from core.config import CoreConfig
|
|
|
|
from core.data import Data
|
2023-02-16 23:13:41 +01:00
|
|
|
from core.utils import Utils
|
2023-02-16 06:06:42 +01:00
|
|
|
|
2023-02-16 23:13:41 +01:00
|
|
|
class TitleServlet():
|
2023-02-16 06:06:42 +01:00
|
|
|
def __init__(self, core_cfg: CoreConfig, cfg_folder: str):
|
|
|
|
super().__init__()
|
|
|
|
self.config = core_cfg
|
|
|
|
self.config_folder = cfg_folder
|
2023-02-16 23:13:41 +01:00
|
|
|
self.data = Data(core_cfg)
|
2023-02-17 07:02:21 +01:00
|
|
|
self.title_registry: Dict[str, Any] = {}
|
2023-02-16 23:13:41 +01:00
|
|
|
|
|
|
|
self.logger = logging.getLogger("title")
|
|
|
|
if not hasattr(self.logger, "initialized"):
|
|
|
|
log_fmt_str = "[%(asctime)s] Title | %(levelname)s | %(message)s"
|
|
|
|
log_fmt = logging.Formatter(log_fmt_str)
|
|
|
|
|
|
|
|
fileHandler = TimedRotatingFileHandler("{0}/{1}.log".format(self.config.server.log_dir, "title"), when="d", backupCount=10)
|
|
|
|
fileHandler.setFormatter(log_fmt)
|
|
|
|
|
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
|
|
|
|
|
|
|
self.logger.addHandler(fileHandler)
|
|
|
|
self.logger.addHandler(consoleHandler)
|
|
|
|
|
|
|
|
self.logger.setLevel(core_cfg.title.loglevel)
|
|
|
|
coloredlogs.install(level=core_cfg.title.loglevel, logger=self.logger, fmt=log_fmt_str)
|
|
|
|
self.logger.initialized = True
|
|
|
|
|
2023-02-18 06:00:30 +01:00
|
|
|
plugins = Utils.get_all_titles()
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-02-18 06:00:30 +01:00
|
|
|
for folder, mod in plugins.items():
|
2023-02-17 07:02:21 +01:00
|
|
|
if hasattr(mod, "game_codes") and hasattr(mod, "index"):
|
2023-03-05 03:27:52 +01:00
|
|
|
should_call_setup = True
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-03-05 03:58:51 +01:00
|
|
|
if hasattr(mod.index, "get_allnet_info"):
|
|
|
|
for code in mod.game_codes:
|
2023-03-05 03:27:52 +01:00
|
|
|
enabled, _, _ = mod.index.get_allnet_info(code, self.config, self.config_folder)
|
|
|
|
|
2023-03-05 03:58:51 +01:00
|
|
|
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
|
2023-03-05 03:27:52 +01:00
|
|
|
|
2023-03-05 03:58:51 +01:00
|
|
|
self.title_registry[code] = handler_cls
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.logger.warn(f"Game {folder} has no get_allnet_info")
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
self.logger.error(f"{folder} missing game_code or index in __init__.py")
|
2023-03-05 03:27:52 +01:00
|
|
|
|
2023-02-18 06:00:30 +01:00
|
|
|
self.logger.info(f"Serving {len(self.title_registry)} game codes on port {core_cfg.title.port}")
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
def render_GET(self, request: Request, endpoints: dict) -> bytes:
|
2023-02-17 07:37:59 +01:00
|
|
|
code = endpoints["game"]
|
|
|
|
if code not in self.title_registry:
|
|
|
|
self.logger.warn(f"Unknown game code {code}")
|
2023-03-02 06:14:13 +01:00
|
|
|
request.setResponseCode(404)
|
|
|
|
return b""
|
2023-02-17 07:37:59 +01:00
|
|
|
|
|
|
|
index = self.title_registry[code]
|
|
|
|
if not hasattr(index, "render_GET"):
|
|
|
|
self.logger.warn(f"{code} does not dispatch GET")
|
2023-03-02 06:14:13 +01:00
|
|
|
request.setResponseCode(405)
|
2023-02-17 07:37:59 +01:00
|
|
|
return b""
|
|
|
|
|
|
|
|
return index.render_GET(request, endpoints["version"], endpoints["endpoint"])
|
2023-02-16 23:13:41 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
def render_POST(self, request: Request, endpoints: dict) -> bytes:
|
|
|
|
code = endpoints["game"]
|
|
|
|
if code not in self.title_registry:
|
|
|
|
self.logger.warn(f"Unknown game code {code}")
|
2023-03-02 06:14:13 +01:00
|
|
|
request.setResponseCode(404)
|
|
|
|
return b""
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
index = self.title_registry[code]
|
|
|
|
if not hasattr(index, "render_POST"):
|
2023-02-17 07:37:59 +01:00
|
|
|
self.logger.warn(f"{code} does not dispatch POST")
|
2023-03-02 06:14:13 +01:00
|
|
|
request.setResponseCode(405)
|
2023-02-17 07:37:59 +01:00
|
|
|
return b""
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-02-22 18:20:50 +01:00
|
|
|
return index.render_POST(request, int(endpoints["version"]), endpoints["endpoint"])
|