# Copyright (C) 2024 Loren Eteval # # This file is part of Furious. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . from __future__ import annotations from Furious.Utility.Constants import PLATFORM from abc import ABC from typing import Callable, Union import ujson __all__ = ['CoreFactory'] class CoreFactorySuper: def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) ExitCallbackType = Callable[[CoreFactorySuper, int], None] class CoreFactory(CoreFactorySuper, ABC): class ExitCode: ConfigurationError = 23 # Windows: 4294967295. Darwin, Linux: 255 (-1) ServerStartFailure = 4294967295 if PLATFORM == 'Windows' else 255 # Windows shutting down SystemShuttingDown = 0x40010004 def __init__(self, exitCallback: ExitCallbackType = None): super().__init__() self._exitCallback = exitCallback self._jsonConfig = '' @property def exitCallback(self) -> ExitCallbackType: return self._exitCallback def registerExitCallback(self, exitCallback: ExitCallbackType) -> CoreFactory: self._exitCallback = exitCallback return self def callExitCallback(self, exitcode: int): if callable(self._exitCallback): self._exitCallback(self, exitcode) def registerCurrentJSONConfig(self, config: Union[str, dict]) -> CoreFactory: self._jsonConfig = config return self @staticmethod def name() -> str: raise NotImplementedError @staticmethod def version() -> str: raise NotImplementedError def configJSONString(self) -> str: if isinstance(self._jsonConfig, str): return self._jsonConfig if isinstance(self._jsonConfig, dict): try: return ujson.dumps( self._jsonConfig, ensure_ascii=False, escape_forward_slashes=False, ) except Exception: # Any non-exit exceptions return '' return '' def configJSONObject(self) -> dict: if isinstance(self._jsonConfig, str): try: return ujson.loads(self._jsonConfig) except Exception: # Any non-exit exceptions return {} if isinstance(self._jsonConfig, dict): return self._jsonConfig return {} def start(self, *args, **kwargs) -> bool: raise NotImplementedError def stop(self): raise NotImplementedError