diff --git a/backend.py b/backend.py index f826e78..acbcb26 100644 --- a/backend.py +++ b/backend.py @@ -1,4 +1,102 @@ import platform import requests +from os import popen +from time import sleep +import re +import pathlib -PATH: str +def getArch() -> str : + arch = platform.machine() + + if arch in ['x86_64','AMD64'] : + return 'x86_64' + elif arch in ['arm64','aarch64'] : + return 'arm64' + +APP_PATH: str + +TOKEN: str + +OS = platform.system() + +ARCH = getArch() + +def requestGitHubInfo() -> list : + if len(TOKEN) > 0 : + response = requests.get("https://api.github.com/repos/godotengine/godot/releases?per_page=100",headers={"Accept":"application/vnd.github+json","Authorization":f"Bearer {TOKEN}","X-GitHub-Api-Version":"2022-11-28"}) + else : + return [] + + return response.json() + +# Get the versions of the Godot Engine and sort them from latest to oldest + +def verOptions() -> list: + tag_list = [] + response = requestGitHubInfo() + + for i in response : + tag_list.append(i['tag_name']) + + tag_list.sort(reverse=True) + + tag_list.pop() + + return tag_list + +def downloadGodot(cur_version: str) -> None : + + response = requestGitHubInfo() + + VERSION = cur_version + + response.sort(key=lambda x: x['tag_name'],reverse=True) + + ASSETS = getAssets(response, VERSION) + + if ASSETS != [] : + for asset in ASSETS : + if OS == 'Linux' : + regex = re.findall("linux|x11",asset['name']) + re.findall("x86_64|64",asset['name']) + if re.findall("server|headless",asset['name']) == [] and regex in [['linux','x86_64'],['x11','64']] :#(asset['name'].find('linux') > 0 or asset['name'].find('x11') > 0) and (asset['name'].find(ARCH) > 0 or asset['name'].find('64') > 0) and asset['name'].find('server') == -1: + popen(f"curl -Lo {APP_PATH}/Godot.zip {asset['browser_download_url']}") + sleep(10) + popen(f"unzip -d {APP_PATH} {APP_PATH}/Godot.zip") + sleep(10) + popen(f"rm -f {APP_PATH}/Godot.zip") + elif OS == 'Windows' : + if 'arm64' in re.findall('win',asset['name']) + re.findall('arm64',asset['name']) : + popen(f"curl -Lo {APP_PATH}/Godot.zip {asset['browser_download_url']}") + popen(f"tar -xf {APP_PATH}/Godot.zip -C {APP_PATH}") + popen(f"DEL {APP_PATH}/Godot.zip") + else : + popen(f"curl -Lo {APP_PATH}/Godot.zip {asset['browser_download_url']}") + popen(f"tar -xf {APP_PATH}/Godot.zip -C {APP_PATH}") + popen(f"DEL {APP_PATH}/Godot.zip") + else : + raise Exception("Incompatible OS.") + else : + raise Exception("No fitting assets found.") + +def getAssets(response: list, tag: str) -> list : + + for i in response : + if i['tag_name'] == tag : + return i['assets'] + else : + continue + + return [] + +def launch(cur_version: str) -> int : + + VERSION = cur_version.split('-')[0] + + appDir = pathlib.Path(APP_PATH) + + for i in appDir.iterdir() : + if re.findall(VERSION,i.name) == [VERSION] : + popen(f"{APP_PATH}/{i.name}") + return 0 + + return 1 \ No newline at end of file diff --git a/main.py b/main.py index 725c046..7fb089e 100644 --- a/main.py +++ b/main.py @@ -1,20 +1,19 @@ import tkinter as tk import tkinter.ttk as ttk -import platform -import requests +import backend -def verOptions() -> list: - tag_list = [] - response = requests.get("https://api.github.com/repos/godotengine/godot/releases?per_page=100",headers={"Accept":"application/vnd.github+json","Authorization":"Bearer {TOKEN}","X-GitHub-Api-Version":"2022-11-28"}) +def tryDownload() -> None : + backend.downloadGodot(strvar.get()) - for i in response.json() : - tag_list.append(i['tag_name']) - - tag_list.sort(reverse=True) +def tryLaunch() -> None : + if backend.launch(strvar.get()) == 0 : + print("Launched successfully") + else : + print("Godot couldn't be launched") - return tag_list -CUR_VERSION = verOptions() + +CUR_VERSION = backend.verOptions() FRONTEND = tk.Tk() FRONTEND.geometry("650x480") @@ -25,8 +24,8 @@ ttk.Style().map("M.TCombobox",background=[('active','#aaa')]) CANVAS = tk.Canvas(FRONTEND,bg="#4811a9") -LAUNCH_BUTTON = tk.Button(CANVAS,activebackground="#aaa",bg="#888",text="Launch") -DOWNLOAD_BUTTON = tk.Button(CANVAS,activebackground="#aaa",bg="#888",text="Download") +LAUNCH_BUTTON = tk.Button(CANVAS,activebackground="#aaa",bg="#888",text="Launch",command=tryLaunch) +DOWNLOAD_BUTTON = tk.Button(CANVAS,activebackground="#aaa",bg="#888",text="Download",command=tryDownload) strvar = tk.StringVar(CANVAS,CUR_VERSION[0]) VERSION_MENU = ttk.Combobox(master=CANVAS,textvariable=strvar,values=CUR_VERSION,state='readonly',font="Sans 12",style='M.TCombobox')