-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrowbar_settings.py
55 lines (43 loc) · 1.85 KB
/
crowbar_settings.py
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
import os
from xml.etree import ElementTree
zm_path = os.path.join(os.environ["APPDATA"], "ZeqMacaw")
is_crowbar_dir = lambda d: os.path.isdir(os.path.join(zm_path, d)) and d.startswith("Crowbar ")
crowbar_versions = [d for d in os.listdir(zm_path) if is_crowbar_dir(d)]
chosen_version = max(crowbar_versions)
print("Using settings from", chosen_version)
chosen_dir = os.path.join(zm_path, chosen_version)
cb_settings_tree = ElementTree.parse(os.path.join(chosen_dir, "Crowbar Settings.xml"))
root = cb_settings_tree.getroot()
game_setups = root.find("GameSetups")
library_paths = root.find("SteamLibraryPaths")
library_path_macros = {}
for lib_path in library_paths:
search = lib_path.find("Macro").text
replace = lib_path.find("LibraryPath").text
library_path_macros[search] = replace
DEFAULT_GAME = object()
def text_setting(name: str):
return root.find(name).text
# Compile options
default_game_index = int(text_setting("CompileGameSetupSelectedIndex"))
nop4 = text_setting("CompileOptionNoP4IsChecked").lower() == "true"
if text_setting("CompileOutputFolderOption") == "WorkFolder":
compile_output_dir = text_setting("CompileOutputFullPath")
else:
compile_output_dir = None
def element_to_dict(element: ElementTree.Element):
return {e.tag: e.text for e in element}
def apply_macros(path: str):
if not path:
return path
for search, replace in library_path_macros.items():
path = path.replace(search, replace)
return path
def get_game_setup(game_name=DEFAULT_GAME):
if game_name is DEFAULT_GAME:
setup_element = game_setups[default_game_index]
else:
setup_element = next(e for e in game_setups if e.find("GameName").text == game_name)
if not setup_element:
raise KeyError('Couldn\'t find settings for "{}"'.format(game_name))
return {e.tag: apply_macros(e.text) for e in setup_element}