From 9e145278d514414d7089c400c3f256a76b7fe850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathis=20Dr=C3=B6ge?= <34034631+CommandMC@users.noreply.github.com> Date: Fri, 24 Jun 2022 12:44:28 +0200 Subject: [PATCH] [cli/utils] Open CMD when exe is double-clicked (#436) This opens up CMD if - the exe file was double-clicked - no arguments are provided - we're on Windows --- legendary/cli.py | 5 +++++ legendary/utils/windows_helpers.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/legendary/cli.py b/legendary/cli.py index f63e8a8..9b3f5f9 100644 --- a/legendary/cli.py +++ b/legendary/cli.py @@ -28,6 +28,7 @@ from legendary.utils.env import is_windows_mac_or_pyi from legendary.utils.eos import add_registry_entries, query_registry_entries, remove_registry_entries from legendary.utils.lfs import validate_files, clean_filename from legendary.utils.selective_dl import get_sdl_appname +from legendary.utils.windows_helpers import double_clicked from legendary.utils.wine_helpers import read_registry, get_shell_folders # todo custom formatter for cli logger (clean info, highlighted error/warning) @@ -2903,6 +2904,10 @@ def main(): continue print(f'\nCommand: {choice}') print(subparser.format_help()) + elif os.name == 'nt' and double_clicked(): + print('Please note that this is not the intended way to run Legendary.') + print('Follow https://github.com/derrod/legendary/wiki/Setup-Instructions to set it up properly') + subprocess.Popen(['cmd', '/K', 'echo>nul']) return cli = LegendaryCLI(override_config=args.config_file, api_timeout=args.api_timeout) diff --git a/legendary/utils/windows_helpers.py b/legendary/utils/windows_helpers.py index 3a5597d..ec20098 100644 --- a/legendary/utils/windows_helpers.py +++ b/legendary/utils/windows_helpers.py @@ -1,5 +1,6 @@ import logging import winreg +import ctypes _logger = logging.getLogger('WindowsHelpers') @@ -80,3 +81,16 @@ def set_registry_value(hive, key, value, data, reg_type=winreg.REG_SZ, use_32bit except Exception as e: _logger.debug(f'Setting "{key}":"{value}" to "{data}" failed with {repr(e)}') winreg.CloseKey(k) + + +def double_clicked() -> bool: + # Thanks https://stackoverflow.com/a/55476145 + + # Load kernel32.dll + kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) + # Create an array to store the processes in. This doesn't actually need to + # be large enough to store the whole process list since GetConsoleProcessList() + # just returns the number of processes if the array is too small. + process_array = (ctypes.c_uint * 1)() + num_processes = kernel32.GetConsoleProcessList(process_array, 1) + return num_processes < 3