Example #1
0
    def __init__(self, name, file_path=None):
        basic_config = BaseAgent.base_create_agent_configurations()

        if file_path is not None and os.path.isfile(file_path):
            file_path = os.path.realpath(file_path)
            basic_config.parse_file(file_path)
        else:
            base_agent_path = os.path.join(get_python_root(), "rlbot",
                                           "agents", "base_agent.py")
            try:
                rel_path = os.path.relpath(base_agent_path, get_python_root())
            except ValueError:
                rel_path = base_agent_path
            basic_config.set_value(BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY,
                                   rel_path)

        python_file_path = os.path.realpath(
            os.path.join(
                os.path.dirname(os.path.realpath(file_path)),
                basic_config.get(BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY)))
        try:
            self.agent_class = import_agent(
                python_file_path).get_loaded_class()
        except (ValueError, ModuleNotFoundError):
            self.agent_class = BaseAgent

        super().__init__(self.agent_class.base_create_agent_configurations(),
                         file_path, name)
        # Make sure the path to the python file actually gets set to that path, even if there was no config at file_path
        self.config.set_value(
            BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY,
            basic_config.get(BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY))
Example #2
0
    def __init__(self, name, file_path=None):

        self.looks_path = None
        self.logger = get_logger('agent_preset')

        if file_path is not None and os.path.isfile(file_path):
            config_bundle = get_bot_config_bundle(file_path)
            self.looks_path = config_bundle.get_absolute_path(
                BOT_CONFIG_MODULE_HEADER, LOOKS_CONFIG_KEY)
            python_file_path = config_bundle.get_absolute_path(
                BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY)

        else:
            python_file_path = inspect.getfile(BaseAgent)

        try:
            self.agent_class = import_agent(
                python_file_path).get_loaded_class()
            super().__init__(
                self.agent_class.base_create_agent_configurations(), file_path,
                name)
        except (ValueError, ModuleNotFoundError, FileNotFoundError) as e:
            raise ValueError(f"Problem when processing {file_path}: {str(e)}")
        except ImportError as e:
            self.logger.debug(
                f"Will not use custom config for {file_path} because we failed to load: {str(e)}"
            )
            super().__init__(BaseAgent.base_create_agent_configurations(),
                             file_path, name)

        # Make sure the path to the python file actually gets set to that path, even if there was no config at file_path
        self.config.set_value(BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY,
                              python_file_path)
Example #3
0
 def __init__(self, config_directory, config_obj: ConfigObject, config_file_name: str = None):
     super().__init__(config_directory, config_obj, config_file_name)
     self.base_agent_config = BaseAgent.base_create_agent_configurations()
     self.base_agent_config.parse_file(self.config_obj, config_directory=config_directory)
     self.python_file = self.get_absolute_path(BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY)
     self.looks_path = self.get_absolute_path(BOT_CONFIG_MODULE_HEADER, LOOKS_CONFIG_KEY)
     self.loadout_generator_file = self.get_absolute_path(BOT_CONFIG_MODULE_HEADER, LOADOUT_GENERATOR_FILE_KEY)
Example #4
0
 def __init__(self,
              config_directory,
              config_obj: ConfigObject,
              config_file_name: str = None):
     self.config_directory = config_directory
     self.config_file_name = config_file_name
     self.config_path = os.path.join(self.config_directory,
                                     self.config_file_name)
     self.config_obj = config_obj
     self.base_agent_config = BaseAgent.base_create_agent_configurations()
     self.base_agent_config.parse_file(self.config_obj,
                                       config_directory=config_directory)
     self.name = config_obj.get(BOT_CONFIG_MODULE_HEADER, BOT_NAME_KEY)
     self.looks_path = self.get_absolute_path(BOT_CONFIG_MODULE_HEADER,
                                              LOOKS_CONFIG_KEY)
     self.python_file = self.get_absolute_path(BOT_CONFIG_MODULE_HEADER,
                                               PYTHON_FILE_KEY)
Example #5
0
 def __init__(self,
              config_directory,
              config_obj: ConfigObject,
              config_file_name: str = None):
     self.config_directory = config_directory
     self.config_file_name = config_file_name
     self.config_path = os.path.join(self.config_directory,
                                     self.config_file_name)
     self.config_obj = config_obj
     self.base_agent_config = BaseAgent.base_create_agent_configurations()
     self.base_agent_config.parse_file(self.config_obj,
                                       config_directory=config_directory)
     self.name = config_obj.get(BOT_CONFIG_MODULE_HEADER, BOT_NAME_KEY)
     self.looks_path = self.get_absolute_path(BOT_CONFIG_MODULE_HEADER,
                                              LOOKS_CONFIG_KEY)
     self.python_file = self.get_absolute_path(BOT_CONFIG_MODULE_HEADER,
                                               PYTHON_FILE_KEY)
     self.loadout_generator_file = self.get_absolute_path(
         BOT_CONFIG_MODULE_HEADER, LOADOUT_GENERATOR_FILE_KEY)
     self.supports_early_start = self.base_agent_config.get(
         BOT_CONFIG_MODULE_HEADER, SUPPORTS_EARLY_START_KEY)
Example #6
0
    def import_agent(self, config_path):
        """
		does not init the agent class. only returns it using rlbot framework
		:return: the agent class
		"""
        my_path = os.path.dirname(os.path.realpath(__file__))

        #retriving python file path
        base_config = BaseAgent.base_create_agent_configurations()
        base_config.parse_file(config_path)
        agent_python_file = base_config.get(BOT_CONFIG_MODULE_HEADER,
                                            PYTHON_FILE_KEY)

        #importing agent class
        python_path = os.path.join(os.path.dirname(config_path),
                                   agent_python_file)
        agent_class = import_agent(python_path).get_loaded_class()

        #retriving agent config
        agent_config = agent_class.base_create_agent_configurations()
        agent_config.parse_file(config_path)
        return agent_class, agent_config
Example #7
0
def bootstrap_python_bot(bot_name, directory):
    sanitized_name = convert_to_filename(bot_name)
    bot_directory = Path(directory or '.')

    with tempfile.TemporaryDirectory() as tmpdirname:
        tmpdir = Path(tmpdirname)
        print('created temporary directory', tmpdir)

        download_and_extract_zip(
            download_url=
            'https://github.com/RLBot/RLBotPythonExample/archive/master.zip',
            local_zip_path=tmpdir / 'RLBotPythonExample.zip',
            local_folder_path=tmpdir)

        try:
            move(tmpdir / 'RLBotPythonExample-master',
                 bot_directory / sanitized_name)
        except FileExistsError:
            return {
                'error':
                f'There is already a bot named {sanitized_name}, please choose a different name!'
            }

    # Choose appropriate file names based on the bot name
    code_dir = bot_directory / sanitized_name / sanitized_name
    python_file = code_dir / f'{sanitized_name}.py'
    config_file = code_dir / f'{sanitized_name}.cfg'

    # We're making some big assumptions here that the file structure / names in RLBotPythonExample will not change.
    move(bot_directory / sanitized_name / 'python_example', code_dir)
    move(code_dir / 'python_example.py', python_file)
    move(code_dir / 'python_example.cfg', config_file)

    # Update the config file to point to the renamed files, and show the correct bot name.

    # This is not an ideal way of modifying the config file, because:
    # - It uses our custom ConfigObject class, which is limited / buggy, and we should be moving away from it
    # - The ConfigObject class is not capable of 'round-tripping', i.e. if you parse a config file and then
    #   write it again, the comment lines will not be preserved.
    # - It can still write comments, but only if they have a 'description' that has been added programmatically
    #   (see base_create_agent_configurations).
    #
    # One route is to add 'description' items for all the stuff we care about, including the stuff here
    # https://github.com/RLBot/RLBotPythonExample/blob/master/python_example/python_example.cfg#L11-L27
    # so that the resulting file is sortof the same (might have slightly different formatting). That's annoying
    # and hard to maintain though, and again we'd be investing more in this custom config class that I would
    # prefer to get rid of.
    #
    # Alternatives:
    #
    # Use the configobj library https://configobj.readthedocs.io/en/latest/configobj.html
    # - I tried this in https://github.com/IamEld3st/RLBotGUI/commit/a30308940dd5a0e4a45db6ccc088e6e75a9f69f0
    #   which worked well for me, but people reported issues during installation. If we can get the installation
    #   ironed out, it will be a nice solution for modifying cfg files in general.
    #
    # Do a simple find-and-replace in the file
    # - Very crude, but it can be reliable if we use specific commit hashes like I did in
    #   https://github.com/IamEld3st/RLBotGUI/commit/a30308940dd5a0e4a45db6ccc088e6e75a9f69f0
    # - It would get us up and running with the new features until we can figure out a proper config modification
    #   solution.
    raw_bot_config = configparser.RawConfigParser()
    raw_bot_config.read(config_file, encoding='utf8')
    agent_config = BaseAgent.base_create_agent_configurations()
    agent_config.parse_file(raw_bot_config)
    agent_config.set_value(BOT_CONFIG_MODULE_HEADER, BOT_NAME_KEY, bot_name)
    agent_config.set_value(BOT_CONFIG_MODULE_HEADER, PYTHON_FILE_KEY,
                           f'{sanitized_name}.py')
    with open(config_file, 'w', encoding='utf8') as f:
        f.write(str(agent_config))

    # This is intended to open the example python file in the default system editor for .py files.
    # Hopefully this will be VS Code or notepad++ or something. If it gets executed as a python script, no harm done.
    os.startfile(python_file)

    return config_file
Example #8
0
 def __init__(self, config_directory, config_obj):
     self.config_directory = config_directory
     self.config_obj = config_obj
     self.base_agent_config = BaseAgent.base_create_agent_configurations()
     self.base_agent_config.parse_file(self.config_obj)