-
Notifications
You must be signed in to change notification settings - Fork 12
/
unityhub.py
executable file
·72 lines (52 loc) · 1.82 KB
/
unityhub.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
from xml.etree import ElementTree
import base64
import os
import subprocess
import sys
def to_base64(s):
return base64.b64encode(s.encode('ascii')).decode('ascii')
def edit_pref(root, pref, type, func):
element = root.find(f'*[@name="{pref}"]')
if element is None:
element = ElementTree.SubElement(root, 'pref', {
'type': type,
'name': pref
})
new_value = func(element.text)
if element.text != new_value:
element.text = new_value
return True
else:
return False
def replace_string_pref(root, pref, value):
return edit_pref(root, pref, 'string', lambda _: value)
def main():
prefs = os.path.join(os.environ['XDG_DATA_HOME'], 'unity3d', 'prefs')
if not os.path.exists(prefs):
os.makedirs(os.path.dirname(prefs), exist_ok=True)
with open(prefs, 'w') as fp:
print('<unity_prefs version_major="1" version_minor="1">', file=fp)
print('</unity_prefs>', file=fp)
b64_editor = to_base64('/app/bin/code')
b64_args = to_base64('$(File)')
tree = ElementTree.parse(prefs)
root = tree.getroot()
was_changed = any([
replace_string_pref(root, 'kScriptsDefaultApp', b64_editor),
replace_string_pref(root, 'kScriptEditorArgs', b64_args),
replace_string_pref(root, 'kScriptEditorArgs/app/bin/code', b64_args),
])
if was_changed:
tmp = prefs + '.tmp'
with open(tmp, 'wb') as fp:
tree.write(fp)
os.rename(tmp, prefs)
env = os.environ.copy()
env['UNITY_DATADIR'] = env['XDG_DATA_HOME']
env['TMPDIR'] = f'{env["XDG_CACHE_HOME"]}/tmp'
os.execvpe('zypak-wrapper',
['zypak-wrapper', '/app/extra/unityhub-bin', *sys.argv[1:]],
env)
if __name__ == '__main__':
main()