Esempio n. 1
0
    def load_plugins(self) -> None:
        """(internal)"""
        from ba._general import getclass
        from ba._plugin import Plugin

        # Note: the plugins we load is purely based on what's enabled
        # in the app config. Our meta-scan gives us a list of available
        # plugins, but that is only used to give the user a list of plugins
        # that they can enable. (we wouldn't want to look at meta-scan here
        # anyway because it may not be done yet at this point in the launch)
        plugstates: Dict[str, Dict] = self.config.get('Plugins', {})
        assert isinstance(plugstates, dict)
        plugkeys: List[str] = sorted(key for key, val in plugstates.items()
                                     if val.get('enabled', False))
        for plugkey in plugkeys:
            try:
                cls = getclass(plugkey, Plugin)
            except Exception as exc:
                _ba.log(f"Error loading plugin class '{plugkey}': {exc}",
                        to_server=False)
                continue
            try:
                plugin = cls()
                assert plugkey not in self.active_plugins
                self.active_plugins[plugkey] = plugin
            except Exception:
                from ba import _error
                _error.print_exception(f'Error loading plugin: {plugkey}')
Esempio n. 2
0
def get_game_types() -> List[Type[ba.GameActivity]]:
    """Return available game types."""
    from ba import _general
    from ba import _gameactivity
    gameclassnames = get_scan_results().games
    gameclasses = []
    for gameclassname in gameclassnames:
        try:
            cls = _general.getclass(gameclassname, _gameactivity.GameActivity)
            gameclasses.append(cls)
        except Exception:
            from ba import _error
            _error.print_exception('error importing ' + str(gameclassname))
    unowned = get_unowned_game_types()
    return [cls for cls in gameclasses if cls not in unowned]
Esempio n. 3
0
def filter_playlist(playlist: PlaylistType,
                    sessiontype: Type[_session.Session],
                    add_resolved_type: bool = False,
                    remove_unowned: bool = True,
                    mark_unowned: bool = False) -> PlaylistType:
    """Return a filtered version of a playlist.

    Strips out or replaces invalid or unowned game types, makes sure all
    settings are present, and adds in a 'resolved_type' which is the actual
    type.
    """
    # pylint: disable=too-many-locals
    # pylint: disable=too-many-branches
    # pylint: disable=too-many-statements
    import _ba
    from ba import _map
    from ba import _general
    from ba import _gameactivity
    goodlist: List[Dict] = []
    unowned_maps: Sequence[str]
    if remove_unowned or mark_unowned:
        unowned_maps = _map.get_unowned_maps()
        unowned_game_types = _ba.app.meta.get_unowned_game_types()
    else:
        unowned_maps = []
        unowned_game_types = set()

    for entry in copy.deepcopy(playlist):
        # 'map' used to be called 'level' here.
        if 'level' in entry:
            entry['map'] = entry['level']
            del entry['level']

        # We now stuff map into settings instead of it being its own thing.
        if 'map' in entry:
            entry['settings']['map'] = entry['map']
            del entry['map']

        # Update old map names to new ones.
        entry['settings']['map'] = _map.get_filtered_map_name(
            entry['settings']['map'])
        if remove_unowned and entry['settings']['map'] in unowned_maps:
            continue

        # Ok, for each game in our list, try to import the module and grab
        # the actual game class. add successful ones to our initial list
        # to present to the user.
        if not isinstance(entry['type'], str):
            raise TypeError('invalid entry format')
        try:
            # Do some type filters for backwards compat.
            if entry['type'] in ('Assault.AssaultGame',
                                 'Happy_Thoughts.HappyThoughtsGame',
                                 'bsAssault.AssaultGame',
                                 'bs_assault.AssaultGame'):
                entry['type'] = 'bastd.game.assault.AssaultGame'
            if entry['type'] in ('King_of_the_Hill.KingOfTheHillGame',
                                 'bsKingOfTheHill.KingOfTheHillGame',
                                 'bs_king_of_the_hill.KingOfTheHillGame'):
                entry['type'] = 'bastd.game.kingofthehill.KingOfTheHillGame'
            if entry['type'] in ('Capture_the_Flag.CTFGame',
                                 'bsCaptureTheFlag.CTFGame',
                                 'bs_capture_the_flag.CTFGame'):
                entry['type'] = (
                    'bastd.game.capturetheflag.CaptureTheFlagGame')
            if entry['type'] in ('Death_Match.DeathMatchGame',
                                 'bsDeathMatch.DeathMatchGame',
                                 'bs_death_match.DeathMatchGame'):
                entry['type'] = 'bastd.game.deathmatch.DeathMatchGame'
            if entry['type'] in ('ChosenOne.ChosenOneGame',
                                 'bsChosenOne.ChosenOneGame',
                                 'bs_chosen_one.ChosenOneGame'):
                entry['type'] = 'bastd.game.chosenone.ChosenOneGame'
            if entry['type'] in ('Conquest.Conquest', 'Conquest.ConquestGame',
                                 'bsConquest.ConquestGame',
                                 'bs_conquest.ConquestGame'):
                entry['type'] = 'bastd.game.conquest.ConquestGame'
            if entry['type'] in ('Elimination.EliminationGame',
                                 'bsElimination.EliminationGame',
                                 'bs_elimination.EliminationGame'):
                entry['type'] = 'bastd.game.elimination.EliminationGame'
            if entry['type'] in ('Football.FootballGame',
                                 'bsFootball.FootballTeamGame',
                                 'bs_football.FootballTeamGame'):
                entry['type'] = 'bastd.game.football.FootballTeamGame'
            if entry['type'] in ('Hockey.HockeyGame', 'bsHockey.HockeyGame',
                                 'bs_hockey.HockeyGame'):
                entry['type'] = 'bastd.game.hockey.HockeyGame'
            if entry['type'] in ('Keep_Away.KeepAwayGame',
                                 'bsKeepAway.KeepAwayGame',
                                 'bs_keep_away.KeepAwayGame'):
                entry['type'] = 'bastd.game.keepaway.KeepAwayGame'
            if entry['type'] in ('Race.RaceGame', 'bsRace.RaceGame',
                                 'bs_race.RaceGame'):
                entry['type'] = 'bastd.game.race.RaceGame'
            if entry['type'] in ('bsEasterEggHunt.EasterEggHuntGame',
                                 'bs_easter_egg_hunt.EasterEggHuntGame'):
                entry['type'] = 'bastd.game.easteregghunt.EasterEggHuntGame'
            if entry['type'] in ('bsMeteorShower.MeteorShowerGame',
                                 'bs_meteor_shower.MeteorShowerGame'):
                entry['type'] = 'bastd.game.meteorshower.MeteorShowerGame'
            if entry['type'] in ('bsTargetPractice.TargetPracticeGame',
                                 'bs_target_practice.TargetPracticeGame'):
                entry['type'] = (
                    'bastd.game.targetpractice.TargetPracticeGame')

            gameclass = _general.getclass(entry['type'],
                                          _gameactivity.GameActivity)

            if remove_unowned and gameclass in unowned_game_types:
                continue
            if add_resolved_type:
                entry['resolved_type'] = gameclass
            if mark_unowned and entry['settings']['map'] in unowned_maps:
                entry['is_unowned_map'] = True
            if mark_unowned and gameclass in unowned_game_types:
                entry['is_unowned_game'] = True

            # Make sure all settings the game defines are present.
            neededsettings = gameclass.get_available_settings(sessiontype)
            for setting in neededsettings:
                if setting.name not in entry['settings']:
                    entry['settings'][setting.name] = setting.default
            goodlist.append(entry)
        except ImportError as exc:
            print(f'Import failed while scanning playlist: {exc}')
        except Exception:
            from ba import _error
            _error.print_exception()
    return goodlist