[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
This commit is contained in:
Mathis Dröge 2022-06-24 12:44:28 +02:00 committed by GitHub
parent 594e60e850
commit 9e145278d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View file

@ -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)

View file

@ -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