summaryrefslogtreecommitdiffstats
path: root/gamechestcli/gamechest/gamedb.py
blob: 69756c1be9cc60c7190352dc93e1acc0b990afb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import yaml

from .gameconfig import config


class GameDB:

    def __init__(self):
        database_path = config.get_games_database_path()
        with open(database_path, 'rb') as fdin:
            self.db = yaml.safe_load(fdin)
        self.last_game_info = None

    def get_game_info(self, game_id=None):
        if self.last_game_info and self.last_game_info['id'] == game_id:
            return self.last_game_info
        game_info = next(game_info
                         for game_info in self.db['games']
                         if game_info['id'] == game_id)
        self.last_game_info = game_info
        return game_info

    def get_game_env(self, profile_id, game_id=None):
        # TODO
        raise Error

    def get_game_command(self, profile_id, game_id=None):
        game_info = self.get_game_info(game_id)
        game_mods = game_info.get('mods', [])
        game_mods += ['locked-run-profiledir']

        game_dir = config.get_games_install_basedir() / game_info['id']
        # note: glob('*/') globs regular files too, thus I filter on is_dir.
        game_dir = list(item
                        for item in game_dir.glob('*') if item.is_dir())[-1]

        tools_bin_path = config.get_games_saves_tools_bin_path()
        profile_dir = config.get_profile_dir(profile_id)

        command = [
            *[f'{tools_bin_path}/mod-{item}' for item in game_mods],
            profile_dir,
            game_dir,
            *[item if index > 0 else f'{tools_bin_path}/{item}'
              for index, item in enumerate(game_info['command'])],
        ]

        return command

    def get_ids(self):
        for game_info in self.db['games']:
            yield game_info['id']