From 678ca8f9036d204dc742fe16b83bfab7ef43b6f7 Mon Sep 17 00:00:00 2001 From: tappi287 Date: Sun, 9 Jul 2023 20:47:52 +0200 Subject: [PATCH] implemented location component and accept overwrite location only if detected as valid --- app.py | 112 ++++++++++--------- rf2settings/app/app_chat_fn.py | 8 +- rf2settings/app/app_main_fn.py | 21 +++- vue/src/App.vue | 48 ++------ vue/src/components/RfLocation.vue | 81 ++++++++++++++ vue/src/components/pages/PreferencesPage.vue | 13 ++- 6 files changed, 183 insertions(+), 100 deletions(-) create mode 100644 vue/src/components/RfLocation.vue diff --git a/app.py b/app.py index ae8bc63..46a6622 100644 --- a/app.py +++ b/app.py @@ -26,44 +26,82 @@ setup_logging() -def start_eel(npm_serve=True): +def in_restore_mode() -> bool: + """ Return True if App is started in Restore Mode """ + if len(sys.argv) > 1 and sys.argv[1] == '-b': + logging.warning('Found restore mode argument. Beginning to restore rFactor 2 settings.') + restore_backup() + logging.warning('\nFinished Restore Mode. Exiting application.') + return True + return False + + +def prepare_app_start() -> bool: + """ Return True if npm_serve should be used """ logging.info('\n\n\n') logging.info('#######################################################') logging.info('################ Starting APP ###########') logging.info('#######################################################\n\n\n') logging.info(f'Args: {sys.argv}') + """ + THIS WILL DISABLE ctypes support! But it will make sure "Launch rFactor2" + or basically any executable that is loading DLLs will work. + """ + if sys.platform == "win32": + import ctypes + ctypes.windll.kernel32.SetDllDirectoryA(None) + """ + // + """ + + AppSettings.load() + AppSettings.copy_default_presets() + AppSettings.delete_current_settings_presets() + if FROZEN: - npm_serve = False # Set Exception hook sys.excepthook = AppExceptionHook.exception_hook + return False + return True - # -- Restore Mode - if len(sys.argv) > 1 and sys.argv[1] == '-b': - logging.warning(f'Found restore mode argument. Beginning to restore rFactor 2 settings.') - restore_backup() - logging.warning('\nFinished Restore Mode. Exiting application.') - return - AppSettings.load() - AppSettings.copy_default_presets() - AppSettings.delete_current_settings_presets() +def _main_app_loop(): + # -- Game Controller Greenlet + cg = eel.spawn(controller_greenlet) + + # -- Headlights Greenlet + hg = eel.spawn(headlights_greenlet) + + # -- rFactor Greenlet + rg = eel.spawn(rfactor_greenlet) + + # -- YouTUbe Greenlet + yg = eel.spawn(youtube_greenlet) + + # -- Run until window/tab closed + logging.debug('Entering Event Loop') + while not CLOSE_EVENT.is_set(): + # Game controller event loop + controller_event_loop() + # rFactor 2 event loop + rfactor_event_loop() + # YouTube live chat event loop + youtube_eventloop() + # Capture exception events + AppExceptionHook.exception_event_loop() + + # -- Shutdown Greenlets + logging.debug('Shutting down Greenlets.') + gevent.joinall((cg, hg, rg, yg), timeout=15.0, raise_error=True) + +def start_eel(npm_serve=True): # This will ask for and re-run with admin rights # if setting needs_admin set. if AppSettings.needs_admin and not run_as_admin(): return - """ - THIS WILL DISABLE ctypes support! But it will make sure "Launch rFactor2" - or basically any executable that is loading DLLs will work. - """ - if sys.platform == "win32": - import ctypes - ctypes.windll.kernel32.SetDllDirectoryA(None) - """ - // - """ host = 'localhost' page = 'index.html' port = 8123 @@ -78,7 +116,6 @@ def start_eel(npm_serve=True): url_port = port eel.init('web') - # TODO: fetch OSError port in use edge_cmd = f"{os.path.expandvars('%PROGRAMFILES(x86)%')}\\Microsoft\\Edge\\Application\\msedge.exe" start_url = f'http://{host}:{url_port}' @@ -102,37 +139,12 @@ def start_eel(npm_serve=True): # Open system default web browser webbrowser.open_new(start_url) - # -- Game Controller Greenlet - cg = eel.spawn(controller_greenlet) - - # -- Headlights Greenlet - hg = eel.spawn(headlights_greenlet) - - # -- rFactor Greenlet - rg = eel.spawn(rfactor_greenlet) - - # -- YouTUbe Greenlet - yg = eel.spawn(youtube_greenlet) - - # -- Run until window/tab closed - logging.debug('Entering Event Loop') - while not CLOSE_EVENT.is_set(): - # Game controller event loop - controller_event_loop() - # rFactor 2 event loop - rfactor_event_loop() - # YouTube live chat event loop - youtube_eventloop() - # Capture exception events - AppExceptionHook.exception_event_loop() - - # -- Shutdown Greenlets - logging.debug('Shutting down Greenlets.') - gevent.joinall((cg, hg, rg, yg), timeout=15.0, raise_error=True) + _main_app_loop() if __name__ == '__main__': - start_eel() + if not in_restore_mode(): + start_eel(prepare_app_start()) # -- Shutdown logging logging.info('\n\n\n') diff --git a/rf2settings/app/app_chat_fn.py b/rf2settings/app/app_chat_fn.py index f67d991..9ec62b8 100644 --- a/rf2settings/app/app_chat_fn.py +++ b/rf2settings/app/app_chat_fn.py @@ -84,9 +84,13 @@ def uninstall_plugin(): @capture_app_exceptions def get_plugin_version(): - plugin_path = Path(_get_rf_location(RFACTOR_PLUGIN_PATH)) / CHAT_PLUGIN_NAME + try: + plugin_path = Path(_get_rf_location(RFACTOR_PLUGIN_PATH)) / CHAT_PLUGIN_NAME + except Exception as e: + logging.error(f'Error location Plugin: {e}') + plugin_path = None - if plugin_path.exists() and plugin_path.is_file(): + if plugin_path and plugin_path.exists() and plugin_path.is_file(): version = AppSettings.chat_plugin_version.get(get_file_hash(plugin_path)) logging.debug('Found ChatTransceiver Plugin version: %s', version) return json.dumps({'result': True, 'version': version}) diff --git a/rf2settings/app/app_main_fn.py b/rf2settings/app/app_main_fn.py index 2c93225..8b22f5f 100644 --- a/rf2settings/app/app_main_fn.py +++ b/rf2settings/app/app_main_fn.py @@ -27,16 +27,25 @@ def _get_rf_location(sub_path): @capture_app_exceptions def overwrite_rf_location(value): - result = False + def reset_location(): + AppSettings.rf_overwrite_location = '' + RfactorLocation.overwrite_location(None) + if Path(value).exists() and Path(value).is_dir() and Path(value) != Path('.'): AppSettings.rf_overwrite_location = Path(value).as_posix() RfactorLocation.overwrite_location(AppSettings.rf_overwrite_location) - logging.warning('Overwriting rf2 location: %s', Path(value).as_posix()) - result = True + RfactorLocation.get_location() + if not RfactorLocation.is_valid: + logging.warning(f'Invalid overwrite location: {value}. Resetting rF2 location.') + reset_location() + result = False + else: + logging.warning('Overwriting rf2 location: %s', Path(value).as_posix()) + result = True else: + reset_location() logging.warning('Overwriting rf2 location cleared!') - AppSettings.rf_overwrite_location = '' - RfactorLocation.overwrite_location(None) + result = True AppSettings.save() return result @@ -112,7 +121,7 @@ def run_steamvr(): subprocess.Popen(cmd) except Exception as e: return json.dumps({'result': False, 'msg': f'Error launching SteamVR: {e}'}) - return json.dumps({'result': True, 'msg': f'Launched SteamVR'}) + return json.dumps({'result': True, 'msg': 'Launched SteamVR'}) @capture_app_exceptions diff --git a/vue/src/App.vue b/vue/src/App.vue index 78e41a3..f5673a2 100644 --- a/vue/src/App.vue +++ b/vue/src/App.vue @@ -38,22 +38,11 @@ request administrative privileges on start up: Reset UAC - -
rFactor 2 Location
- If you have trouble locating the correct rFactor 2 installation path, enter it manually here and restart the app. - - - - - - Reset - - - -
+ + +
rFactor 2 Location
+ + + + + + \ No newline at end of file diff --git a/vue/src/components/pages/PreferencesPage.vue b/vue/src/components/pages/PreferencesPage.vue index 27f3037..0a64965 100644 --- a/vue/src/components/pages/PreferencesPage.vue +++ b/vue/src/components/pages/PreferencesPage.vue @@ -29,7 +29,7 @@ - + @@ -42,16 +42,23 @@ + + + + +