GUI updater
This commit is contained in:
parent
091950d126
commit
60a8a2b309
114
main.py
114
main.py
|
@ -1,9 +1,13 @@
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
import zipfile
|
import zipfile
|
||||||
import platform
|
|
||||||
import shutil
|
import shutil
|
||||||
|
import platform
|
||||||
from pathlib import Path
|
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():
|
def get_hl_path():
|
||||||
"""Get the Half-Life installation path based on the operating system."""
|
"""Get the Half-Life installation path based on the operating system."""
|
||||||
|
@ -14,6 +18,56 @@ def get_hl_path():
|
||||||
else:
|
else:
|
||||||
raise OSError("Unsupported operating system")
|
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()
|
hl_path = get_hl_path()
|
||||||
spacelife_path = hl_path / "SpaceLife"
|
spacelife_path = hl_path / "SpaceLife"
|
||||||
version_file = hl_path / "SpaceLifeVersion.txt"
|
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)
|
spacelife_path.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
def download_file(url, dest):
|
root = tk.Tk()
|
||||||
response = requests.get(url, stream=True)
|
root.title("SpaceLife Updater")
|
||||||
with open(dest, 'wb') as file:
|
root.geometry("345x200")
|
||||||
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):
|
background_url = "https://deadzone.tf/background.png"
|
||||||
download_file(server_version_url, local_version_path.with_suffix('.tmp'))
|
response = requests.get(background_url)
|
||||||
with open(local_version_path.with_suffix('.tmp'), 'r') as server_version_file:
|
img_data = response.content
|
||||||
server_version = server_version_file.read().strip()
|
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():
|
top_frame = tk.Frame(root, bg='blue')
|
||||||
local_version_path.with_suffix('.tmp').replace(local_version_path)
|
top_frame.pack(side='top', fill='x')
|
||||||
return True
|
|
||||||
|
|
||||||
with open(local_version_path, 'r') as local_version_file:
|
version_label = tk.Label(top_frame, text=f"Version: {get_version()}", bg='blue', fg='black')
|
||||||
local_version = local_version_file.read().strip()
|
version_label.pack(side='right', padx=10, pady=20)
|
||||||
|
|
||||||
if local_version != server_version:
|
update_needed = is_new_version_available(version_file, version_url)
|
||||||
local_version_path.with_suffix('.tmp').replace(local_version_path)
|
if update_needed:
|
||||||
return True
|
update_button = tk.Button(top_frame, text="Update", command=update_mod)
|
||||||
else:
|
update_button.pack(side='left', padx=10)
|
||||||
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!")
|
|
||||||
else:
|
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():
|
root.mainloop()
|
||||||
mod_zip_file.unlink()
|
|
||||||
|
|
Loading…
Reference in New Issue