Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

import os 

from lutris.util.log import logger 

from collections import OrderedDict 

 

 

def get_default_acf(appid, name): 

    userconfig = OrderedDict() 

    userconfig['name'] = name 

    userconfig['gameid'] = appid 

 

    appstate = OrderedDict() 

    appstate['appID'] = appid 

    appstate['Universe'] = "1" 

    appstate['StateFlags'] = "4" 

    appstate['installdir'] = name 

    appstate['UserConfig'] = userconfig 

    return {'AppState': appstate} 

 

 

def vdf_parse(steam_config_file, config): 

    """ Given a steam configuration steam, parse the content and return it as 

        a dict. 

    """ 

    line = " " 

    while line: 

        line = steam_config_file.readline() 

        if not line or line.strip() == "}": 

            return config 

        line_elements = line.strip().split("\"") 

        if len(line_elements) == 3: 

            key = line_elements[1] 

            steam_config_file.readline()  # skip '{' 

            config[key] = vdf_parse(steam_config_file, {}) 

        else: 

            config[line_elements[1]] = line_elements[3] 

    return config 

 

 

def to_vdf(dict_data, level=0): 

    """ Convert a dictionnary to Steam config file format. """ 

    vdf_data = "" 

    for key in dict_data: 

        value = dict_data[key] 

        if isinstance(value, dict): 

            vdf_data += "%s\"%s\"\n" % ("\t" * level, key) 

            vdf_data += "%s{\n" % ("\t" * level) 

            vdf_data += to_vdf(value, level + 1) 

            vdf_data += "%s}\n" % ("\t" * level) 

        else: 

            vdf_data += "%s\"%s\"\t\t\"%s\"\n" % ("\t" * level, key, value) 

    return vdf_data 

 

 

def vdf_write(vdf_path, config): 

    vdf_data = to_vdf(config) 

    with open(vdf_path, "w") as vdf_file: 

        vdf_file.write(vdf_data) 

 

 

def read_config(path_prefix): 

    config_filename = os.path.join(path_prefix, 'config/config.vdf') 

    if not os.path.exists(config_filename): 

        return 

    with open(config_filename, "r") as steam_config_file: 

        config = vdf_parse(steam_config_file, {}) 

    return config['InstallConfigStore']['Software']['Valve']['Steam'] 

 

 

def get_path_from_config(config, appid): 

    """ Given a steam config, return path for game 'appid' """ 

    if not config or 'apps' not in config: 

        return False 

    game_config = config["apps"].get(appid) 

    if not game_config: 

        return False 

    if game_config.get('HasAllLocalContent'): 

        installdir = game_config['installdir'].replace("\\\\", "/") 

        if not installdir: 

            return False 

        if installdir.startswith('C'): 

            installdir = os.path.join(os.path.expanduser('~'), 

                                      '.wine/drive_c', installdir[3:]) 

        elif installdir[1] == ':': 

            # Trim Windows path 

            installdir = installdir[2:] 

        logger.debug("Steam game found at %s" % installdir) 

        if os.path.exists(installdir): 

            return installdir 

        elif os.path.exists(installdir.replace('steamapps', 'SteamApps')): 

            return installdir.replace('steamapps', 'SteamApps') 

        else: 

            logger.debug("Path %s not found" % installdir) 

    return False 

 

 

def get_path_from_appmanifest(steam_path, appid): 

    appmanifest_path = os.path.join(steam_path, 

                                    "SteamApps/appmanifest_%s.acf" % appid) 

    if not os.path.exists(appmanifest_path): 

        logger.debug("No appmanifest file %s" % appmanifest_path) 

        return 

 

    with open(appmanifest_path, "r") as appmanifest_file: 

        config = vdf_parse(appmanifest_file, {}) 

    installdir = config.get('AppState', {}).get('installdir') 

    install_path = os.path.join(steam_path, "SteamApps/common/%s" % installdir) 

    if installdir and os.path.exists(install_path): 

        return install_path