From 60a8a2b309fadd8e0f38ad03cf8f260864c19e64 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Tue, 23 Jan 2024 16:32:33 -0500 Subject: [PATCH] GUI updater --- main.py | 114 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 75 insertions(+), 39 deletions(-) diff --git a/main.py b/main.py index d643f7d..b7b659f 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,13 @@ import os import requests import zipfile -import platform import shutil +import platform from pathlib import Path +import tkinter as tk +from tkinter import messagebox +from PIL import Image, ImageTk +from io import BytesIO def get_hl_path(): """Get the Half-Life installation path based on the operating system.""" @@ -14,6 +18,56 @@ def get_hl_path(): else: raise OSError("Unsupported operating system") +def download_file(url, dest): + response = requests.get(url, stream=True) + with open(dest, 'wb') as file: + for chunk in response.iter_content(chunk_size=8192): + if chunk: + file.write(chunk) + +def is_new_version_available(local_version_path, server_version_url): + if not local_version_path.exists(): + return True + + with open(local_version_path, 'r') as local_version_file: + local_version = local_version_file.read().strip() + + response = requests.get(server_version_url) + server_version = response.text.strip() + + return local_version != server_version + +def update_version_file(version_url, version_file_path): + response = requests.get(version_url) + with open(version_file_path, 'w') as file: + file.write(response.text.strip()) + +def unzip_file(zip_path, extract_to_path): + if extract_to_path.exists(): + shutil.rmtree(extract_to_path) + extract_to_path.mkdir(parents=True, exist_ok=True) + with zipfile.ZipFile(zip_path, 'r') as zip_ref: + zip_ref.extractall(extract_to_path) + +def update_mod(): + print("Downloading new version...") + download_file(mod_url, mod_zip_file) + print(f"Unzipping the mod into {spacelife_path}...") + unzip_file(mod_zip_file, spacelife_path) + update_version_file(version_url, version_file) + print("Update complete!") + messagebox.showinfo("Update Complete", "Your mod has been updated to the latest version.") + version_label.config(text=f"Version: {get_version()} (up to date)") + update_button.pack_forget() + if mod_zip_file.exists(): + mod_zip_file.unlink() + +def get_version(): + if version_file.exists(): + with open(version_file, 'r') as file: + return file.read().strip() + return "Unknown" + hl_path = get_hl_path() spacelife_path = hl_path / "SpaceLife" version_file = hl_path / "SpaceLifeVersion.txt" @@ -23,47 +77,29 @@ mod_zip_file = hl_path / "SpaceLife.zip" spacelife_path.mkdir(parents=True, exist_ok=True) -def download_file(url, dest): - response = requests.get(url, stream=True) - with open(dest, 'wb') as file: - for chunk in response.iter_content(chunk_size=8192): - if chunk: - file.write(chunk) +root = tk.Tk() +root.title("SpaceLife Updater") +root.geometry("345x200") -def is_new_version_available(local_version_path, server_version_url): - download_file(server_version_url, local_version_path.with_suffix('.tmp')) - with open(local_version_path.with_suffix('.tmp'), 'r') as server_version_file: - server_version = server_version_file.read().strip() +background_url = "https://deadzone.tf/background.png" +response = requests.get(background_url) +img_data = response.content +img = Image.open(BytesIO(img_data)) +background_image = ImageTk.PhotoImage(img) +background_label = tk.Label(root, image=background_image) +background_label.place(x=0, y=0, relwidth=1, relheight=1) - if not local_version_path.exists(): - local_version_path.with_suffix('.tmp').replace(local_version_path) - return True +top_frame = tk.Frame(root, bg='blue') +top_frame.pack(side='top', fill='x') - with open(local_version_path, 'r') as local_version_file: - local_version = local_version_file.read().strip() +version_label = tk.Label(top_frame, text=f"Version: {get_version()}", bg='blue', fg='black') +version_label.pack(side='right', padx=10, pady=20) - if local_version != server_version: - local_version_path.with_suffix('.tmp').replace(local_version_path) - return True - else: - local_version_path.with_suffix('.tmp').unlink() - return False - -def unzip_file(zip_path, extract_to_path): - if extract_to_path.exists(): - shutil.rmtree(extract_to_path) - extract_to_path.mkdir(parents=True, exist_ok=True) - with zipfile.ZipFile(zip_path, 'r') as zip_ref: - zip_ref.extractall(extract_to_path) - -if is_new_version_available(version_file, version_url): - print("New version available, downloading...") - download_file(mod_url, mod_zip_file) - print(f"Unzipping the mod into {spacelife_path}...") - unzip_file(mod_zip_file, spacelife_path) - print("Update complete!") +update_needed = is_new_version_available(version_file, version_url) +if update_needed: + update_button = tk.Button(top_frame, text="Update", command=update_mod) + update_button.pack(side='left', padx=10) else: - print("No update needed. You have the latest version.") + version_label.config(text=f"Version: {get_version()} (up to date)") -if mod_zip_file.exists(): - mod_zip_file.unlink() +root.mainloop()