diff --git a/fishy/__main__.py b/fishy/__main__.py index 532b912..1d4939e 100644 --- a/fishy/__main__.py +++ b/fishy/__main__.py @@ -31,9 +31,9 @@ def initialize(window_to_hide): Migration.migrate() helper.create_shortcut_first() - helper.initialize_uid() new_session = web.get_session() + if new_session is None: logging.error("Couldn't create a session, some features might not work") print(f"created session {new_session}") @@ -58,9 +58,9 @@ def main(): if not gui.check_eula(): return + finish_splash = splash.start() config.start_backup_scheduler() active.init() - finish_splash = splash.start() hotkey.init() def on_gui_load(): diff --git a/fishy/gui/main_gui.py b/fishy/gui/main_gui.py index d3e0a99..2cf9b2c 100644 --- a/fishy/gui/main_gui.py +++ b/fishy/gui/main_gui.py @@ -57,7 +57,7 @@ def _create(gui: 'GUI'): filemenu.add_checkbutton(label="Dark Mode", command=_toggle_mode, variable=dark_mode_var) if config.get("dont_ask_update", False): - filemenu.add_command(label="Update", command=helper.update) + filemenu.add_command(label="Update", command=lambda: helper.update(gui)) def installer(): if filemenu.entrycget(4, 'label') == "Remove FishyQR": @@ -82,7 +82,6 @@ def _create(gui: 'GUI'): logging.debug("Restart to update the changes") debug_menu.add_checkbutton(label="Keep Console", command=keep_console, variable=debug_var) - debug_menu.add_command(label="Restart", command=helper.restart) menubar.add_cascade(label="Debug", menu=debug_menu) help_menu = tk.Menu(menubar, tearoff=0) diff --git a/fishy/helper/__init__.py b/fishy/helper/__init__.py index 45db834..6e852ce 100644 --- a/fishy/helper/__init__.py +++ b/fishy/helper/__init__.py @@ -1,8 +1,8 @@ from .config import Config from .helper import (addon_exists, create_shortcut, create_shortcut_first, - get_addonversion, get_savedvarsdir, initialize_uid, + get_addonversion, get_savedvarsdir, install_addon, install_thread_excepthook, manifest_file, not_implemented, open_web, playsound_multiple, - remove_addon, restart, unhandled_exception_logging, + remove_addon, unhandled_exception_logging, update, install_required_addons) from .luaparser import sv_color_extract diff --git a/fishy/helper/active_poll.py b/fishy/helper/active_poll.py index 6807b80..1f2a5ca 100644 --- a/fishy/helper/active_poll.py +++ b/fishy/helper/active_poll.py @@ -1,3 +1,5 @@ +import logging + from event_scheduler import EventScheduler from fishy.web import web @@ -13,11 +15,14 @@ class active: active._scheduler = EventScheduler() active._scheduler.start() + logging.debug("active scheduler initialized") @staticmethod def start(): active._scheduler.enter_recurring(60, 1, web.ping) + logging.debug("active scheduler started") @staticmethod def stop(): active._scheduler.stop(hard_stop=True) + logging.debug("active scheduler stopped") diff --git a/fishy/helper/config.py b/fishy/helper/config.py index fa4e0b9..6a050a6 100644 --- a/fishy/helper/config.py +++ b/fishy/helper/config.py @@ -57,14 +57,17 @@ class Config: else: self._config_dict = dict() + logging.debug("config initialized") def start_backup_scheduler(self): self._create_backup() self._scheduler.start() self._scheduler.enter_recurring(5 * 60, 1, self._create_backup) + logging.debug("scheduler started") def stop(self): self._scheduler.stop(True) + logging.debug("config stopped") def _create_backup(self): with open(temp_file, 'w') as f: diff --git a/fishy/helper/helper.py b/fishy/helper/helper.py index 01c31c9..9369723 100644 --- a/fishy/helper/helper.py +++ b/fishy/helper/helper.py @@ -15,6 +15,7 @@ from zipfile import ZipFile import requests import winshell +from fishy.gui import update_dialog from playsound import playsound from win32com.client import Dispatch from win32comext.shell import shell, shellcon @@ -66,19 +67,6 @@ def open_web(website): Thread(target=lambda: webbrowser.open(website, new=2)).start() -def initialize_uid(): - from .config import config - - if config.get("uid") is not None: - return - - new_uid = web.register_user() - if new_uid is not None: - config.set("uid", new_uid) - else: - logging.error("Couldn't register uid, some features might not work") - - def _create_new_uid(): """ Creates a unique id for user @@ -239,20 +227,16 @@ def get_documents(): return shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, None, 0) -def restart(): - os.execl(sys.executable, *([sys.executable] + sys.argv)) - - def log_raise(msg): logging.error(msg) raise Exception(msg) -def update(): +def update(gui): from .config import config config.delete("dont_ask_update") - restart() + update_dialog.check_update(gui) def is_eso_active(): diff --git a/fishy/web/web.py b/fishy/web/web.py index 83da91a..8b4ec7d 100644 --- a/fishy/web/web.py +++ b/fishy/web/web.py @@ -1,3 +1,5 @@ +import logging + import requests from whatsmyip.ip import get_ip from whatsmyip.providers import GoogleDnsProvider @@ -112,24 +114,48 @@ def unsub(): return result["success"] -@fallback(None) def get_session(lazy=True): global _session_id + # lazy loading logic if lazy and _session_id is not None: return _session_id - body = {"uid": config.get("uid"), "apiversion": apiversion} + # check if user has uid + uid = config.get("uid") + + # then create session + if uid: + _session_id, online = _create_new_session(uid) + # if not, create new id then try creating session again + else: + uid = register_user() + logging.debug("New User, generated new uid") + if uid: + _session_id, online = _create_new_session(uid) + else: + online = False + + # when the user is already registered but session is not created as uid is not found + if online and not _session_id: + logging.error("user not found, generating new uid.. contact dev if you don't want to loose data") + new_uid = register_user() + _session_id, online = _create_new_session(new_uid) + config.set("uid", new_uid) + config.set("old_uid", uid) + + return _session_id + + +@fallback((None, False)) +def _create_new_session(uid): + body = {"uid": uid, "apiversion": apiversion} response = requests.post(urls.session, params=body) if response.status_code == 405: - config.delete("uid") - helper.restart() - return None - - _session_id = response.json()["session_id"] - return _session_id + return None, True + return response.json()["session_id"], True @fallback(False) def has_beta():