diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-settings b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-settings index 7214160bd6..28f93cf97c 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-settings +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr-settings @@ -28,10 +28,27 @@ def write(path, value): os.replace(temporary, path) +def saved_preferences(path): + try: + backup = read(path) + if not isinstance(backup.get('section'), bool) or not isinstance(backup.get('values'), dict): + raise ValueError('invalid backup object') + for key in OVERRIDES: + entry = backup['values'].get(key) + if not isinstance(entry, list) or len(entry) != 2 or not isinstance(entry[0], bool): + raise ValueError('invalid saved preference: ' + key) + return backup + except ValueError as error: + raise ValueError(f'Invalid preference backup {path}; repair the saved section/values and retry restore. ' + 'The backup and current preferences have been preserved.') from error + + def update(operation, settings_path, backup_path): settings = read(settings_path) if operation == 'apply': - if not backup_path.exists(): + if backup_path.exists(): + saved_preferences(backup_path) + else: section = settings.get('steamvr', {}) write(backup_path, {'section': 'steamvr' in settings, 'values': {key: [key in section, section.get(key)] for key in OVERRIDES}}) @@ -40,7 +57,7 @@ def update(operation, settings_path, backup_path): elif operation == 'restore': if not backup_path.exists(): return - backup = read(backup_path) + backup = saved_preferences(backup_path) section = settings.setdefault('steamvr', {}) for key in OVERRIDES: present, value = backup['values'][key] diff --git a/tools/tests/test_steamvr_helper.py b/tools/tests/test_steamvr_helper.py index 2a8a58fe4e..e7fb038a1c 100644 --- a/tools/tests/test_steamvr_helper.py +++ b/tools/tests/test_steamvr_helper.py @@ -134,6 +134,32 @@ class SteamVrHelperTest(unittest.TestCase): self.assertEqual(json.loads(path.read_text()), {'steamvr': {'motionSmoothing': True, 'somePreference': 42}, 'dashboard': {'changedDuringSession': True}}) + def test_invalid_backup_preserves_preferences_until_repaired(self): + settings = self.root / 'steamvr.vrsettings' + backup = self.root / 'quest-backup.json' + helper = SOURCE.with_name('droiddeck-steamvr-settings') + original = {'steamvr': {'motionSmoothing': True}, 'dashboard': {'unchanged': 42}} + settings.write_text(json.dumps(original)) + subprocess.run([str(helper), 'apply', str(settings), str(backup)], check=True) + valid_backup = backup.read_bytes() + applied = settings.read_bytes() + invalid = ['truncated {', '{}', '{"section": true, "values": []}', + '{"section": false, "values": {"motionSmoothing": [false]}}'] + for content in invalid: + for operation in ('apply', 'restore'): + with self.subTest(content=content, operation=operation): + backup.write_text(content) + result = subprocess.run([str(helper), operation, str(settings), str(backup)], + capture_output=True, text=True) + self.assertNotEqual(result.returncode, 0) + self.assertIn('repair the saved section/values and retry restore', result.stderr) + self.assertEqual(backup.read_text(), content) + self.assertEqual(settings.read_bytes(), applied) + backup.write_bytes(valid_backup) + subprocess.run([str(helper), 'restore', str(settings), str(backup)], check=True) + self.assertEqual(json.loads(settings.read_text()), original) + self.assertFalse(backup.exists()) + def test_quest_update_preserves_previous_cvp_backup(self): self.environment['DROIDDECK_VR_PRESENT'] = 'questwsi' cvp = self.steamvr / 'drivers/cv/bin/linuxarm64/libArcturusPerception.so'