Files
DroidDeck/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-settings
T

64 lines
2.2 KiB
Python

#!/usr/bin/python3
"""Preserve SteamVR preferences across Quest's required compositor overrides."""
import json
import os
from pathlib import Path
import sys
OVERRIDES = {'enableLinuxVulkanAsync': True, 'motionSmoothing': False}
def read(path):
try:
with path.open() as source:
settings = json.load(source)
except FileNotFoundError:
return {}
if not isinstance(settings, dict) or not isinstance(settings.get('steamvr', {}), dict):
raise ValueError('Invalid SteamVR settings object')
return settings
def write(path, value):
path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
temporary = path.with_name(path.name + '.tmp')
with os.fdopen(os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600), 'w') as destination:
json.dump(value, destination, indent=3)
destination.write('\n')
os.replace(temporary, path)
def update(operation, settings_path, backup_path):
settings = read(settings_path)
if operation == 'apply':
if not backup_path.exists():
section = settings.get('steamvr', {})
write(backup_path, {'section': 'steamvr' in settings,
'values': {key: [key in section, section.get(key)] for key in OVERRIDES}})
settings.setdefault('steamvr', {}).update(OVERRIDES)
write(settings_path, settings)
elif operation == 'restore':
if not backup_path.exists():
return
backup = read(backup_path)
section = settings.setdefault('steamvr', {})
for key in OVERRIDES:
present, value = backup['values'][key]
if present:
section[key] = value
else:
section.pop(key, None)
if not section and not backup['section']:
settings.pop('steamvr')
write(settings_path, settings)
backup_path.unlink()
else:
raise ValueError('Expected apply or restore')
if __name__ == '__main__':
try:
update(sys.argv[1], Path(sys.argv[2]), Path(sys.argv[3]))
except (OSError, ValueError, KeyError, IndexError, TypeError) as error:
sys.exit('Could not update SteamVR settings: ' + str(error))