summaryrefslogtreecommitdiffstats
path: root/gamechestcli/gamechest/gamedb.py
blob: 99d3c9ac7697b83e66b274d3516d2614076e5db0 (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
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_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 = next(item for item in game_dir.glob('*') if item.is_dir())

        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