Files
DLSS-Override-For-All-Games/DLSS Override+.py
T

487 lines
18 KiB
Python
Raw Normal View History

2025-02-10 00:55:16 +03:00
import sys, os, json, shutil, stat, getpass, hashlib
2025-02-10 10:15:25 +03:00
# Import standard libraries for system interaction, file handling, JSON parsing,
# file copying, file permissions, user info retrieval, and hashing.
2025-02-10 00:55:16 +03:00
from PyQt6 import QtWidgets, QtGui, QtCore
2025-02-10 10:15:25 +03:00
# Import PyQt6 modules for building the graphical user interface.
2025-02-10 00:55:16 +03:00
2025-02-10 10:15:25 +03:00
# Mapping from JSON override keys to their abbreviated representations for logging.
2025-02-10 00:55:16 +03:00
KEY_MAPPING = {
"Disable_FG_Override": "FG",
"Disable_RR_Override": "RR",
"Disable_SR_Override": "SR",
"Disable_RR_Model_Override": "RR-M",
"Disable_SR_Model_Override": "SR-M",
}
def compute_file_hash(path):
2025-02-10 10:15:25 +03:00
"""
Computes the SHA-256 hash of the file at the given path.
Reads the file in chunks to handle large files without loading everything into memory.
Args:
path (str): Path to the file.
Returns:
str: Hexadecimal digest of the file's hash.
"""
2025-02-10 00:55:16 +03:00
h = hashlib.sha256()
try:
with open(path, "rb") as f:
2025-02-10 10:15:25 +03:00
# Read the file in 8KB chunks.
2025-02-10 00:55:16 +03:00
for chunk in iter(lambda: f.read(8192), b""):
h.update(chunk)
except Exception as e:
print(f"Error computing hash: {e}")
return h.hexdigest()
def create_backup(main_path, backup_path, meta_path, log_func):
2025-02-10 10:15:25 +03:00
"""
Creates a backup of the main file and saves metadata including the file hash.
Steps:
- Copies the main file to backup_path.
- Computes its hash and stores it as both the original and modified hash in meta.
- Logs the backup creation.
Args:
main_path (str): Path of the original file.
backup_path (str): Path where the backup will be stored.
meta_path (str): Path to store the backup metadata JSON.
log_func (function): Logging function to output messages.
Returns:
dict or None: The metadata dictionary if backup is created, otherwise None.
"""
2025-02-10 00:55:16 +03:00
try:
2025-02-10 10:15:25 +03:00
# Create a backup copy preserving metadata.
2025-02-10 00:55:16 +03:00
shutil.copy2(main_path, backup_path)
original_hash = compute_file_hash(main_path)
meta = {"original_hash": original_hash, "modified_hash": original_hash}
2025-02-10 10:15:25 +03:00
# Write metadata to the meta file.
2025-02-10 00:55:16 +03:00
with open(meta_path, "w", encoding="utf-8") as f:
json.dump(meta, f)
log_func(f"No backup found, creating new backup.\nBackup created at: {backup_path}")
return meta
except Exception as e:
log_func(f"Error creating backup: {e}")
return None
def load_backup_meta(meta_path):
2025-02-10 10:15:25 +03:00
"""
Loads backup metadata from the JSON file at meta_path.
Args:
meta_path (str): Path to the metadata file.
Returns:
dict or None: The metadata if successfully loaded; otherwise, None.
"""
2025-02-10 00:55:16 +03:00
try:
with open(meta_path, "r", encoding="utf-8") as f:
meta = json.load(f)
return meta
except Exception:
return None
def save_backup_meta(meta_path, meta):
2025-02-10 10:15:25 +03:00
"""
Saves the provided metadata dictionary to the specified meta_path.
Args:
meta_path (str): Path to store the metadata.
meta (dict): Metadata to be saved.
"""
2025-02-10 00:55:16 +03:00
try:
with open(meta_path, "w", encoding="utf-8") as f:
json.dump(meta, f)
except Exception as e:
print(f"Error saving backup meta: {e}")
def update_backup_if_obsolete(main_path, backup_path, meta_path, log_func):
2025-02-10 10:15:25 +03:00
"""
Checks if the backup exists and is up-to-date with the main file.
If the backup is missing or the main file has changed externally,
a new backup is created.
Args:
main_path (str): Path to the main file.
backup_path (str): Path to the backup file.
meta_path (str): Path to the metadata file.
log_func (function): Logging function to output messages.
Returns:
dict: The (possibly updated) metadata.
"""
# Create a new backup if either backup or meta file doesn't exist.
2025-02-10 00:55:16 +03:00
if not os.path.exists(backup_path) or not os.path.exists(meta_path):
return create_backup(main_path, backup_path, meta_path, log_func)
meta = load_backup_meta(meta_path)
if meta is None:
log_func("Backup meta is invalid, creating new backup.")
return create_backup(main_path, backup_path, meta_path, log_func)
current_hash = compute_file_hash(main_path)
2025-02-10 10:15:25 +03:00
# If the main file's current hash doesn't match the stored modified hash,
# it means an external change occurred.
2025-02-10 00:55:16 +03:00
if current_hash != meta["modified_hash"]:
log_func("External update detected. Updating backup to current file as new baseline.")
return create_backup(main_path, backup_path, meta_path, log_func)
return meta
def recursive_process(obj, keys_to_update, updates):
2025-02-10 10:15:25 +03:00
"""
Recursively searches through the JSON object (which can contain nested dictionaries or lists)
for specified keys. If a key is found with a True value, it is set to False.
The function also collects an update log per item, using an identifier from the object.
Args:
obj (dict or list): The JSON object to process.
keys_to_update (list): Keys to look for and update.
updates (dict): Dictionary to record which keys have been modified.
Returns:
bool: True if any modification was made, False otherwise.
"""
2025-02-10 00:55:16 +03:00
modified = False
if isinstance(obj, dict):
2025-02-10 10:15:25 +03:00
# Check each key in the current dictionary.
2025-02-10 00:55:16 +03:00
for key in keys_to_update:
if key in obj and obj[key] is True:
2025-02-10 10:15:25 +03:00
obj[key] = False # Modify the value to False.
2025-02-10 00:55:16 +03:00
modified = True
2025-02-10 10:15:25 +03:00
# Use available fields as an identifier (LocalId or DisplayName).
2025-02-10 00:55:16 +03:00
identifier = obj.get("LocalId") or obj.get("DisplayName") or "Unknown"
updates.setdefault(identifier, set()).add(KEY_MAPPING.get(key, key))
2025-02-10 10:15:25 +03:00
# Specifically process the 'Application' nested dictionary if it exists.
2025-02-10 00:55:16 +03:00
if "Application" in obj and isinstance(obj["Application"], dict):
for key in keys_to_update:
if key in obj["Application"] and obj["Application"][key] is True:
obj["Application"][key] = False
modified = True
identifier = obj["Application"].get("DisplayName") or obj.get("LocalId") or "Unknown"
updates.setdefault(identifier, set()).add(KEY_MAPPING.get(key, key))
2025-02-10 10:15:25 +03:00
# Recursively process each value in the dictionary.
2025-02-10 00:55:16 +03:00
for value in obj.values():
if isinstance(value, (dict, list)):
if recursive_process(value, keys_to_update, updates):
modified = True
elif isinstance(obj, list):
2025-02-10 10:15:25 +03:00
# Process each item in the list if it is a dictionary or list.
2025-02-10 00:55:16 +03:00
for item in obj:
if isinstance(item, (dict, list)):
if recursive_process(item, keys_to_update, updates):
modified = True
return modified
def modify_file(main_path, log_func):
2025-02-10 10:15:25 +03:00
"""
Modifies the JSON file at main_path by setting specific boolean override keys from True to False.
Process:
- Ensures an up-to-date backup exists.
- Loads the JSON file.
- Recursively updates keys (using recursive_process).
- Writes the updated JSON back to the file if changes occurred.
- Updates backup metadata with the new file hash.
- Logs a summary of the modifications.
Args:
main_path (str): Path to the main JSON file.
log_func (function): Function to log messages.
Returns:
tuple: (modified (bool), meta (dict)) indicating if changes were made and the backup metadata.
"""
2025-02-10 00:55:16 +03:00
backup_path = main_path + ".backup"
meta_path = main_path + ".backup.meta"
2025-02-10 10:15:25 +03:00
# Update or create backup if needed.
2025-02-10 00:55:16 +03:00
meta = update_backup_if_obsolete(main_path, backup_path, meta_path, log_func)
try:
with open(main_path, "r", encoding="utf-8") as f:
data = json.load(f)
except Exception as e:
log_func(f"Error reading JSON: {e}")
return False, None
keys_to_update = list(KEY_MAPPING.keys())
2025-02-10 02:27:55 +03:00
updates = {}
2025-02-10 10:15:25 +03:00
# Recursively process the JSON data to update keys.
2025-02-10 00:55:16 +03:00
modified = recursive_process(data, keys_to_update, updates)
if modified:
try:
with open(main_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
log_func("File has been updated.")
mod_hash = compute_file_hash(main_path)
meta["modified_hash"] = mod_hash
save_backup_meta(meta_path, meta)
except Exception as e:
log_func(f"Error writing JSON: {e}")
return False, None
else:
log_func("No modifications were made. Either keys were not found or already set to False.")
2025-02-10 10:15:25 +03:00
# Log a summary for each modified application.
2025-02-10 00:55:16 +03:00
for app, changes in updates.items():
summary = ", ".join(f"{abbr} ✓" for abbr in sorted(changes))
log_func(f"{app}: {summary}")
if modified:
log_func("Reboot recommended for changes to take effect.")
return modified, meta
def revert_file(main_path, log_func):
2025-02-10 10:15:25 +03:00
"""
Reverts the main JSON file back to its backup version.
The function checks if a backup and its metadata exist and ensures that the file
has not been externally modified since the last update. If all checks pass, the backup
is restored.
Args:
main_path (str): Path to the main JSON file.
log_func (function): Function to log messages.
Returns:
bool: True if revert was successful, False otherwise.
"""
2025-02-10 00:55:16 +03:00
backup_path = main_path + ".backup"
meta_path = main_path + ".backup.meta"
if not os.path.exists(backup_path) or not os.path.exists(meta_path):
log_func("No backup available to revert.")
return False
meta = load_backup_meta(meta_path)
current_hash = compute_file_hash(main_path)
if current_hash != meta["modified_hash"]:
log_func("Cannot revert: file has been externally modified since our last update.")
return False
try:
2025-02-10 10:15:25 +03:00
# Remove read-only attribute by setting the file as writable.
2025-02-10 00:55:16 +03:00
os.chmod(main_path, stat.S_IWRITE)
2025-02-10 10:15:25 +03:00
# Restore the backup file.
2025-02-10 00:55:16 +03:00
shutil.copy2(backup_path, main_path)
log_func("Reverted to backup.")
meta["modified_hash"] = meta["original_hash"]
save_backup_meta(meta_path, meta)
return True
except Exception as e:
log_func(f"Error during revert: {e}")
return False
class DLSSOverrideApp(QtWidgets.QMainWindow):
2025-02-10 10:15:25 +03:00
"""
Main window for the DLSS Override Editor application.
Provides a graphical interface to:
- Browse and select the JSON file.
- Process modifications on the file.
- Revert changes from backup.
- Set the file as read-only post-modification.
"""
2025-02-10 00:55:16 +03:00
def __init__(self):
super().__init__()
self.setWindowTitle("DLSS Override Editor")
self.resize(800, 400)
2025-02-10 10:15:25 +03:00
self.session_processed = False # Tracks whether modifications were made this session.
self.setup_ui() # Build the GUI components.
self.apply_dark_theme() # Apply a dark theme to the interface.
2025-02-10 00:55:16 +03:00
def setup_ui(self):
2025-02-10 10:15:25 +03:00
"""
Constructs the user interface layout including file path entry, buttons, checkbox, and log area.
"""
2025-02-10 00:55:16 +03:00
central = QtWidgets.QWidget()
self.setCentralWidget(central)
layout = QtWidgets.QVBoxLayout(central)
2025-02-10 10:15:25 +03:00
# Layout for the file path input and browse button.
2025-02-10 00:55:16 +03:00
path_layout = QtWidgets.QHBoxLayout()
self.path_edit = QtWidgets.QLineEdit()
2025-02-10 10:15:25 +03:00
# Use the current user's directory to set a default path.
2025-02-10 00:55:16 +03:00
username = getpass.getuser()
default_path = fr"C:\Users\{username}\AppData\Local\NVIDIA Corporation\NVIDIA app\NvBackend\ApplicationStorage.json"
self.path_edit.setText(default_path)
path_layout.addWidget(self.path_edit)
self.browse_button = QtWidgets.QPushButton("Browse")
self.browse_button.clicked.connect(self.browse_file)
path_layout.addWidget(self.browse_button)
layout.addLayout(path_layout)
2025-02-10 10:15:25 +03:00
# Checkbox to optionally set the file to read-only after modifications.
2025-02-10 00:55:16 +03:00
self.readonly_checkbox = QtWidgets.QCheckBox("Set file as read-only after modifications")
self.readonly_checkbox.setChecked(True)
layout.addWidget(self.readonly_checkbox)
2025-02-10 10:15:25 +03:00
# Layout for process and revert action buttons.
2025-02-10 00:55:16 +03:00
btn_layout = QtWidgets.QHBoxLayout()
self.process_button = QtWidgets.QPushButton("Process")
self.process_button.clicked.connect(self.process_file)
btn_layout.addWidget(self.process_button)
self.revert_button = QtWidgets.QPushButton("Revert")
self.revert_button.clicked.connect(self.revert_file)
btn_layout.addWidget(self.revert_button)
layout.addLayout(btn_layout)
2025-02-10 10:15:25 +03:00
# Text area for logging application messages.
2025-02-10 00:55:16 +03:00
self.log_text = QtWidgets.QTextEdit()
self.log_text.setReadOnly(True)
layout.addWidget(self.log_text)
def apply_dark_theme(self):
2025-02-10 10:15:25 +03:00
"""
Applies a dark theme stylesheet to the entire application.
"""
2025-02-10 00:55:16 +03:00
style = """
QWidget {
background-color: #1e1e1e;
color: #e0e0e0;
font-family: "Segoe UI", sans-serif;
}
QLineEdit, QTextEdit {
background-color: #2d2d30;
border: 1px solid #3e3e42;
padding: 5px;
border-radius: 3px;
color: #e0e0e0;
}
QPushButton {
background-color: #007ACC;
border: none;
padding: 6px 12px;
border-radius: 4px;
color: #ffffff;
font-weight: bold;
}
QPushButton:hover {
background-color: #005A9E;
}
QPushButton:pressed {
background-color: #003F73;
}
QCheckBox {
spacing: 5px;
}
QCheckBox::indicator {
width: 16px;
height: 16px;
}
QCheckBox::indicator:unchecked {
border: 1px solid #555555;
background-color: #2d2d30;
}
QCheckBox::indicator:checked {
border: 1px solid #007ACC;
background-color: #007ACC;
}
"""
self.setStyleSheet(style)
def log(self, message):
2025-02-10 10:15:25 +03:00
"""
Appends a log message to the log text area.
Args:
message (str): The message to be logged.
"""
2025-02-10 00:55:16 +03:00
self.log_text.append(message)
def browse_file(self):
2025-02-10 10:15:25 +03:00
"""
Opens a file dialog for selecting a JSON file.
Updates the path input with the chosen file path.
"""
2025-02-10 00:55:16 +03:00
current_path = self.path_edit.text().strip()
initial_dir = os.path.dirname(current_path) if os.path.exists(current_path) else ""
2025-02-10 10:15:25 +03:00
file_path, _ = QtWidgets.QFileDialog.getOpenFileName(
self,
"Select ApplicationStorage.json file",
initial_dir,
"JSON Files (*.json);;All Files (*)"
)
2025-02-10 00:55:16 +03:00
if file_path:
self.path_edit.setText(file_path)
def process_file(self):
2025-02-10 10:15:25 +03:00
"""
Processes the JSON file to update override flags:
- Validates file existence.
- Confirms user intention.
- Invokes modify_file to update keys.
- Optionally sets the file to read-only.
"""
2025-02-10 00:55:16 +03:00
file_path = self.path_edit.text().strip()
if not os.path.exists(file_path):
QtWidgets.QMessageBox.critical(self, "Error", f"File not found:\n{file_path}")
return
2025-02-10 10:15:25 +03:00
reply = QtWidgets.QMessageBox.question(
self,
"Confirm",
f"Are you sure you want to modify this file?\n{file_path}",
QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No
)
2025-02-10 00:55:16 +03:00
if reply != QtWidgets.QMessageBox.StandardButton.Yes:
self.log("Operation cancelled by user.")
return
modified, _ = modify_file(file_path, self.log)
if modified:
self.session_processed = True
if modified and self.readonly_checkbox.isChecked():
try:
os.chmod(file_path, stat.S_IREAD)
self.log("File set to read-only.")
except Exception as e:
self.log(f"Error setting file to read-only: {e}")
def revert_file(self):
2025-02-10 10:15:25 +03:00
"""
Reverts the JSON file to its backup version:
- Validates file existence.
- Confirms user action.
- Invokes revert_file to restore the backup.
"""
2025-02-10 00:55:16 +03:00
file_path = self.path_edit.text().strip()
if not os.path.exists(file_path):
QtWidgets.QMessageBox.critical(self, "Error", f"File not found:\n{file_path}")
return
2025-02-10 10:15:25 +03:00
reply = QtWidgets.QMessageBox.question(
self,
"Confirm Revert",
f"Are you sure you want to revert changes to this file?\n{file_path}",
QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No
)
2025-02-10 00:55:16 +03:00
if reply != QtWidgets.QMessageBox.StandardButton.Yes:
self.log("Revert cancelled by user.")
return
if revert_file(file_path, self.log):
self.log("Revert successful.")
2025-02-10 10:15:25 +03:00
# Toggle session flag based on whether modifications were processed.
self.session_processed = not self.session_processed
2025-02-10 00:55:16 +03:00
else:
self.log("Revert failed or no valid backup available.")
def closeEvent(self, event: QtGui.QCloseEvent) -> None:
2025-02-10 10:15:25 +03:00
"""
Overrides the default window close event.
If modifications were made during the session, prompts the user to reboot.
"""
2025-02-10 00:55:16 +03:00
if self.session_processed:
2025-02-10 10:15:25 +03:00
reply = QtWidgets.QMessageBox.question(
self,
"Reboot Recommended",
"Changes have been made that require a reboot to take effect.\nWould you like to reboot now?",
QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No
)
2025-02-10 00:55:16 +03:00
if reply == QtWidgets.QMessageBox.StandardButton.Yes:
os.system("shutdown /r /t 0")
event.accept()
def main():
2025-02-10 10:15:25 +03:00
"""
Entry point of the application.
- Initializes the Qt application.
- Creates and displays the main window.
- Starts the event loop.
"""
2025-02-10 00:55:16 +03:00
app = QtWidgets.QApplication(sys.argv)
window = DLSSOverrideApp()
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
2025-02-10 10:15:25 +03:00
main()