diff options
author | Jesusaves <cpntb1@ymail.com> | 2024-02-04 13:39:36 -0300 |
---|---|---|
committer | Jesusaves <cpntb1@ymail.com> | 2024-02-04 13:39:36 -0300 |
commit | 68269379fa79c358aa24acfb55079a724adb2bea (patch) | |
tree | cb1cb2f7aeb475391202798ca1d9784442acd147 | |
parent | 6fe796dfc3f40d5a7a643fd8a7816b15cc61f6d0 (diff) | |
download | tkinter-68269379fa79c358aa24acfb55079a724adb2bea.tar.gz tkinter-68269379fa79c358aa24acfb55079a724adb2bea.tar.bz2 tkinter-68269379fa79c358aa24acfb55079a724adb2bea.tar.xz tkinter-68269379fa79c358aa24acfb55079a724adb2bea.zip |
Multiplataform support
-rwxr-xr-x | __main__.py | 167 |
1 files changed, 67 insertions, 100 deletions
diff --git a/__main__.py b/__main__.py index f6bdcd7..8525f4a 100755 --- a/__main__.py +++ b/__main__.py @@ -9,9 +9,10 @@ ## Published under the MIT License ##################################### -import requests, json, traceback, subprocess, hmac, struct, time, os, uuid, base64 +import requests, json, traceback, subprocess, sys, time, os, uuid, base64 +#import hmac, struct # < - TODO: Necessary for TOTP-remember support import tkinter as tk -from tkinter.messagebox import showinfo, showerror, askyesno +from tkinter.messagebox import showerror, askyesno from functools import partial OBFS = uuid.getnode() % 256 # Get a number which *usually* doesn't change @@ -24,12 +25,9 @@ def _savePref(): if not saveSettings: return dump = json.dumps(pref) - #print(repr(dump)) dump = bytearray(ord(x) ^ int(OBFS) for x in dump) - #print(repr(dump)) dump = base64.b64encode(dump) dump = dump.decode('ascii') - #print(repr(dump)) with open("settings.b64", 'w') as raw: raw.write(str(dump)) #print("Preferences updated...") @@ -39,27 +37,23 @@ def _savePref(): try: with open("settings.b64") as raw: pref = base64.b64decode(raw.read()) - #print(repr(pref)) - #print(pref[0]) - #print(int(pref[0])) # <- XXX: use ord() if fail, and replace accordingly - #print(int(pref[0]) ^ int(OBFS)) + ## XXX: use ord() if int(x) fails pref = bytearray(int(x) ^ int(OBFS) for x in pref) - #print(repr(pref)) pref = pref.decode('utf8') pref = json.loads(str(pref)) - #print(repr(pref)) print("User settings loaded...") except: print("Failed to load user settings!") err = traceback.format_exc().split("\n")[-2] print("Error: %s" % err) ## Create new dummy prefs so it doesn't crash - pref = {"user": "", - "pass": "", - "totp": "", - "mana": False, - "plus": False, - "shell": True} + pref = {"user": "", # <- User Email + "pass": "", # <- User Password + "totp": "", # <- NYI + "mana": False, # <- Prefer Mana over ManaVerse + "plus": False, # <- Prefer ManaPlus over ManaVerse + "local": True, # <- system-wide vs manaplus/ folder + "shell": True} # <- output to console ## If it is an error, give an option to not destroy the old settings if ("FileNotFoundError" in err): _savePref() @@ -86,7 +80,6 @@ class HoverButton(tk.Button): self.bind("<Leave>", self.exit) ## TODO: Get from `sys` args parameters like world auto-selection -## TODO: Use Config and Local folders, not sure if relative path works ##################################### #VAULT_HOST = "https://localhost:13370" @@ -127,30 +120,15 @@ def san(cmd): def now(): return int(time.time()) -# Search for array[?][key]==search in an array of dicts -# Returns the dictionary, or returns None -def dl_search(array, key, search): - try: - r=(item for item in array if item[key] == search).next() - except: - r=None - if r is None: - stdout("dlsearch: r is None") - return r - -# Search for array[?][key]==search in an array of dicts -# Returns the index, or returns -1 -def dl_search_idx(array, key, search): - try: - r=next((i for i, item in enumerate(array) if item[key] == search), None) - except: - traceback.print_exc() - r=-1 - return ifte(r is None, -1, r) +## Update preferences +if not "local" in list(pref.keys()): + pref["local"] = True +## Does nothing, validation purposes only def nullp(arg): return +## Validates a server entry def validate_server(srv): name="Unknown Server"; ok=False try: @@ -187,8 +165,7 @@ def update_serverlist(hosti): ## If we reached here, then everything is likely fine ## We can now build the persistent data - print("Fetching server list and assets...") - slist=len(j) + print("Fetching server list and assets... %d servers" % len(j)) for server in j: if (validate_server(server)): serverlist.append(server) @@ -235,15 +212,39 @@ def launch_game(idx): traceback.print_exc() return -1 - ## Handle server type - ## TODO: Do not use system-wide install unless specified so in prefs + ## Using system-wide versus local path + if pref["local"]: + CMD=os.getcwd()+"/manaplus/" + + ## TODO: Handle server type and client if not sys.platform.startswith('win'): - CMD="manaplus" + if pref["plus"]: + CMD+="manaplus" + elif pref["mana"]: + CMD+="mana" + else: + CMD+="ManaVerse" + if pref["local"]: + CMD+=".AppImage" else: - CMD="manaplus/Mana/manaplus.exe" # FIXME untested + ## Mana and M+ are not available on Windows (TODO) + if pref["local"]: + CMD+="Mana/manaplus.exe" # FIXME untested + else: + CMD+="manaplus.exe" # FIXME untested + + ## Build the server options OPT="-s %s -y %s -p %s -S" % (HOST, serverlist[idx]["Type"], PORT) + ## Local or System-Wide Config folders + if pref["local"]: + if not sys.platform.startswith('win'): + OPT+=" -C %s/Config -L %s/Local" % (os.getcwd()+"/manaplus", os.getcwd()+"/manaplus") + else: + OPT+=" -C %s\\Config -L %s\\Local" % (os.getcwd()+"\\manaplus", os.getcwd()+"\\manaplus") + pass ## Execute the app + ## TODO: Threading, MLP if pref["shell"]: app=execute(san("%s %s%s" % (CMD, OPT, PWD)), shell=True) # nosec else: @@ -279,6 +280,8 @@ def world_select(): ypos = 60 for w in serverlist: ## TODO: Do not block main thread, launch this in a threading + ## TODO: Make the button width fixed, so they align better + ## TODO: Image button if an icon can be found button = HoverButton(text=w["Name"], command=partial(launch_game, serverlist.index(w)), bg="#cc6600", fg="#fff", activebackground="#ee9933") ## TODO: Handle MLP (on the threading?) ## TODO: First login greeting? @@ -402,62 +405,26 @@ print("Thanks for playing!") exit(0) # <- FIXME: Not necessary? Just delete stuff below ################################################################################# -try: +""" while True: - command=str(input("> ")) - # No command inserted? Do nothing - if command == "": - continue - # We have a command, prepare it for processing - stdout("CONSOLE: %s" % command) - cmd=command.split(' ')[0].lower() - com=" ".join(command.split(' ')[1:]) - # Parse the command - # TODO: grant gems to an user - if cmd in ["help", "h"]: - print("help - Shows this help text") - print("list - List servers") - print("exit - Closes the program") - print("connect <world> - Connects to <world>") - print("") - print("You need ManaPlus installed for Evol2 worlds.") - elif cmd in ["list", "servers"]: - for w in serverlist: - print("\033[1m%s\033[0m" % w["Name"]) - print("Type: %s" % w["Type"]) - print("Help: %s" % w["Help"]) - elif cmd in ["exit", "quit", "close"]: - break - elif cmd in ["connect", "run", "world"]: - target=dl_search_idx(serverlist, "Name", com) - print("Target \"%s\" - %s" % (com, str(target))) - try: - idx=int(target) - except: - stdout("ERROR: Unknown world.") - continue - - app=launch_game(idx) - - ## TODO: Handle MLP - while app == 7: - stdout("[CLIENT] Mirror Lake triggered.") - r=vault.post(VAULT_HOST+"/getlake", json=auth, timeout=15.0) - if r.status_code == 200: - goto=r.json()["world"] - stdout("MLP Target: %s" % str(goto)) - if goto == "" or goto.lower() == "vault": - stdout("Mirror Lake False Positive") - break - try: - idx=int(dl_search_idx(serverlist, "UUID", goto)) - app=launch_game(idx) - except: - traceback.print_exc() - break + app=launch_game(idx) + + ## TODO: Handle MLP + while app == 7: + stdout("[CLIENT] Mirror Lake triggered.") + r=vault.post(VAULT_HOST+"/getlake", json=auth, timeout=15.0) + if r.status_code == 200: + goto=r.json()["world"] + stdout("MLP Target: %s" % str(goto)) + if goto == "" or goto.lower() == "vault": + stdout("Mirror Lake False Positive") + break + try: + idx=int(dl_search_idx(serverlist, "UUID", goto)) + app=launch_game(idx) + except: + traceback.print_exc() + break else: stdout("ERROR: Unknown command. Try \"help\".") -except: - stdout("Abrupt error: Terminating!") - traceback.print_exc() - +""" |