From 71c85a15695f7acfc093c074b78aeb78e4b39c08 Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Tue, 9 Apr 2024 01:57:16 -0500 Subject: [PATCH] Fast-fail when user forgets to extract from zip --- Tools/BatchPatcher/src/__main__.py | 8 ++++++++ Tools/HoloPatcher/src/__main__.py | 18 ++++++++++++------ Tools/HolocronToolset/src/toolset/__main__.py | 14 +++++++++++++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Tools/BatchPatcher/src/__main__.py b/Tools/BatchPatcher/src/__main__.py index 88c29f96b..7608fdc40 100644 --- a/Tools/BatchPatcher/src/__main__.py +++ b/Tools/BatchPatcher/src/__main__.py @@ -5,6 +5,7 @@ import pathlib import platform import sys +import tempfile import tkinter as tk import traceback @@ -1388,8 +1389,15 @@ def start_patching(self): self.install_button.config(state=tk.DISABLED) return None +def is_running_from_temp(): + app_path = Path(sys.executable) + temp_dir = tempfile.gettempdir() + return str(app_path).startswith(temp_dir) if __name__ == "__main__": + if is_running_from_temp(): + messagebox.showerror("Error", "This application cannot be run from within a zip or temporary directory. Please extract it to a permanent location before running.") + sys.exit("Exiting: Application was run from a temporary or zip directory.") try: root = tk.Tk() APP = KOTORPatchingToolUI(root) diff --git a/Tools/HoloPatcher/src/__main__.py b/Tools/HoloPatcher/src/__main__.py index b8fbaebff..96166dffd 100644 --- a/Tools/HoloPatcher/src/__main__.py +++ b/Tools/HoloPatcher/src/__main__.py @@ -1473,12 +1473,6 @@ def onAppCrash( sys.excepthook = onAppCrash -def main(): - app = App() - app.root.mainloop() - atexit.register(lambda: my_cleanup_function(app)) - - def my_cleanup_function(app: App): """Prevents the patcher from running in the background after sys.exit is called.""" #print("Fully shutting down Holo Patcher...") @@ -1486,6 +1480,18 @@ def my_cleanup_function(app: App): #app.root.destroy() +def main(): + app = App() + app.root.mainloop() + atexit.register(lambda: my_cleanup_function(app)) + +def is_running_from_temp(): + app_path = Path(sys.executable) + temp_dir = tempfile.gettempdir() + return str(app_path).startswith(temp_dir) if __name__ == "__main__": + if is_running_from_temp(): + messagebox.showerror("Error", "This application cannot be run from within a zip or temporary directory. Please extract it to a permanent location before running.") + sys.exit("Exiting: Application was run from a temporary or zip directory.") main() diff --git a/Tools/HolocronToolset/src/toolset/__main__.py b/Tools/HolocronToolset/src/toolset/__main__.py index 6ecccbb44..52e953d03 100644 --- a/Tools/HolocronToolset/src/toolset/__main__.py +++ b/Tools/HolocronToolset/src/toolset/__main__.py @@ -11,7 +11,7 @@ from typing import TYPE_CHECKING from PyQt5.QtCore import QThread -from PyQt5.QtWidgets import QApplication +from PyQt5.QtWidgets import QApplication, QMessageBox if TYPE_CHECKING: from types import TracebackType @@ -76,6 +76,10 @@ def update_sys_path(path: pathlib.Path): update_sys_path(toolset_path.parent) os.chdir(toolset_path) +def is_running_from_temp(): + app_path = Path(sys.executable) + temp_dir = tempfile.gettempdir() + return str(app_path).startswith(temp_dir) if __name__ == "__main__": if os.name == "nt": @@ -107,6 +111,14 @@ def update_sys_path(path: pathlib.Path): app.thread().setPriority(QThread.HighestPriority) sys.excepthook = onAppCrash + if is_running_from_temp(): + # Show error message using PyQt5's QMessageBox + msgBox = QMessageBox() + msgBox.setIcon(QMessageBox.Critical) + msgBox.setWindowTitle("Error") + msgBox.setText("This application cannot be run from within a zip or temporary directory. Please extract it to a permanent location before running.") + msgBox.exec_() + sys.exit("Exiting: Application was run from a temporary or zip directory.") from toolset.gui.windows.main import ToolWindow