Exemple #1
0
    def __init__(self):
        # TODO console args
        self.mcdr_state = MCDReforgedState.INITIALIZING
        self.server_state = ServerState.STOPPED
        self.process = None  # type: Optional[PIPE]
        self.flags = MCDReforgedFlag.NONE
        self.starting_server_lock = Lock(
        )  # to prevent multiple start_server() call
        self.stop_lock = Lock()  # to prevent multiple stop() call

        # will be assigned in on_config_changed()
        self.encoding_method = None  # type: Optional[str]
        self.decoding_method = None  # type: Optional[str]

        # --- Constructing fields ---
        self.logger = MCDReforgedLogger(self)
        self.logger.set_file(constant.LOGGING_FILE)
        self.server_interface = ServerInterface(self)
        self.task_executor = TaskExecutor(self)
        self.console_handler = ConsoleHandler(self)
        self.watch_dog = WatchDog(self)
        self.update_helper = UpdateHelper(self)
        self.language_manager = LanguageManager(self.logger)
        self.config = Config(self.logger)
        self.rcon_manager = RconManager(self)
        self.server_handler_manager = ServerHandlerManager(self)
        self.reactor_manager = InfoReactorManager(self)
        self.command_manager = CommandManager(self)
        self.plugin_manager = PluginManager(self)
        self.permission_manager = PermissionManager(self)

        # --- Initialize fields instance ---
        file_missing = False
        try:
            self.load_config(
                allowed_missing_file=False)  # loads config, language, handlers
        except FileNotFoundError:
            self.logger.error('Config is missing, default config generated')
            self.config.save_default()
            file_missing = True
        try:
            self.permission_manager.load_permission_file(
                allowed_missing_file=False)
        except FileNotFoundError:
            self.logger.error(
                'Permission file is missing, default permission file generated'
            )
            self.permission_manager.save_default()
            file_missing = True
        if file_missing:
            self.on_first_start()
            return
        self.plugin_manager.register_permanent_plugins()

        self.set_mcdr_state(MCDReforgedState.INITIALIZED)
Exemple #2
0
	def __init__(self, *, generate_default_only: bool = False, initialize_environment: bool = False):
		"""
		:param generate_default_only: If set to true, MCDR will only generate the default configure and permission files
		"""
		self.mcdr_state = MCDReforgedState.INITIALIZING
		self.server_state = ServerState.STOPPED
		self.server_information = ServerInformation()
		self.process = None  # type: Optional[PIPE]
		self.flags = MCDReforgedFlag.NONE
		self.starting_server_lock = Lock()  # to prevent multiple start_server() call
		self.stop_lock = Lock()  # to prevent multiple stop() call

		# will be assigned in on_config_changed()
		self.encoding_method = None  # type: Optional[str]
		self.decoding_method = None  # type: Optional[str]

		# --- Constructing fields --- #
		self.logger = MCDReforgedLogger()
		self.config = Config(self.logger)
		self.permission_manager = PermissionManager(self)
		self.basic_server_interface = ServerInterface(self)
		self.task_executor = TaskExecutor(self)
		self.console_handler = ConsoleHandler(self)
		self.watch_dog = WatchDog(self)
		self.update_helper = UpdateHelper(self)
		self.translation_manager = TranslationManager(self.logger)
		self.rcon_manager = RconManager(self)
		self.server_handler_manager = ServerHandlerManager(self)
		self.reactor_manager = InfoReactorManager(self)
		self.command_manager = CommandManager(self)
		self.plugin_manager = PluginManager(self)
		self.preference_manager = PreferenceManager(self)

		self.__check_environment()

		# --- Input arguments "generate_default_only" processing --- #
		if generate_default_only:
			self.config.save_default()
			self.permission_manager.save_default()
			return

		# --- Initialize fields instance --- #
		self.translation_manager.load_translations()  # translations are used for logging, so load them first
		if initialize_environment:
			# Prepare config / permission files if they're missing
			if not self.config.file_presents():
				self.config.save_default()
				default_config = self.config.get_default_yaml()
				file_util.touch_directory(default_config['working_directory'])  # create server/ folder
			if not self.permission_manager.file_presents():
				self.permission_manager.save_default()

		# Check if there's any file missing
		# If there's any, MCDR environment might not be probably setup
		file_missing = False

		def load(kind: str, func: Callable[[], Any]) -> bool:
			nonlocal file_missing
			try:
				func()
			except FileNotFoundError:
				self.logger.error('{} is missing'.format(kind.title()))
				file_missing = True
			except YAMLError as e:
				self.logger.error('Failed to load {}: {}'.format(kind, type(e).__name__))
				for line in str(e).splitlines():
					self.logger.error(line)
				return False
			else:
				return True

		# load_config: config, language, handlers, plugin directories, reactors, handlers
		# load_permission_file: permission
		# config change will lead to creating plugin folders
		loading_success = \
			load('configure', lambda: self.load_config(allowed_missing_file=False, echo=not initialize_environment)) and \
			load('permission', lambda: self.permission_manager.load_permission_file(allowed_missing_file=False))
		if file_missing:
			self.__on_file_missing()
			return
		if not loading_success:
			return

		# MCDR environment has been setup, so continue creating default folders and loading stuffs
		self.logger.set_file(core_constant.LOGGING_FILE)  # will create logs/ folder
		self.plugin_manager.touch_directory()  # will create config/ folder

		# --- Done --- #
		self.set_mcdr_state(MCDReforgedState.INITIALIZED)
    def __init__(self,
                 *,
                 generate_default_only: bool = False,
                 initialize_environment: bool = False):
        """
		:param generate_default_only: If set to true, MCDR will only generate the default configure and permission files
		"""
        self.mcdr_state = MCDReforgedState.INITIALIZING
        self.server_state = ServerState.STOPPED
        self.process = None  # type: Optional[PIPE]
        self.flags = MCDReforgedFlag.NONE
        self.starting_server_lock = Lock(
        )  # to prevent multiple start_server() call
        self.stop_lock = Lock()  # to prevent multiple stop() call

        # will be assigned in on_config_changed()
        self.encoding_method = None  # type: Optional[str]
        self.decoding_method = None  # type: Optional[str]

        # --- Constructing fields --- #
        self.logger = MCDReforgedLogger(self)
        self.config = Config(self.logger)
        self.permission_manager = PermissionManager(self)
        self.basic_server_interface = ServerInterface(self)
        self.task_executor = TaskExecutor(self)
        self.console_handler = ConsoleHandler(self)
        self.watch_dog = WatchDog(self)
        self.update_helper = UpdateHelper(self)
        self.translation_manager = TranslationManager(self.logger)
        self.rcon_manager = RconManager(self)
        self.server_handler_manager = ServerHandlerManager(self)
        self.reactor_manager = InfoReactorManager(self)
        self.command_manager = CommandManager(self)
        self.plugin_manager = PluginManager(self)

        # --- Input arguments "generate_default_only" processing --- #
        if generate_default_only:
            self.config.save_default()
            self.permission_manager.save_default()
            return

        # --- Initialize fields instance --- #
        self.translation_manager.load_translations(
        )  # translations are used for logging, so load them first
        if initialize_environment:
            # Prepare config / permission files if they're missing
            if not self.config.file_presents():
                self.config.save_default()
                default_config = self.config.get_default_yaml()
                file_util.touch_directory(default_config['working_directory']
                                          )  # create server/ folder
            if not self.permission_manager.file_presents():
                self.permission_manager.save_default()

        # Check if there's any file missing
        # If there's any, MCDR environment might not be probably setup
        file_missing = False
        try:
            # loads config, language, handlers
            # config change will lead to creating plugin folders
            self.load_config(allowed_missing_file=False,
                             echo=not initialize_environment)
        except FileNotFoundError:
            self.logger.error('Configure is missing')
            file_missing = True
        try:
            self.permission_manager.load_permission_file(
                allowed_missing_file=False)
        except FileNotFoundError:
            self.logger.error('Permission file is missing')
            file_missing = True
        if file_missing:
            self.on_file_missing()
            return

        # MCDR environment has been setup, so continue creating default folders and loading stuffs
        self.logger.set_file(
            core_constant.LOGGING_FILE)  # will create logs/ folder
        self.plugin_manager.touch_directory()  # will create config/ folder

        # --- Done --- #
        self.set_mcdr_state(MCDReforgedState.INITIALIZED)