SpaceLife-Updater/main.py

106 lines
3.5 KiB
Python
Raw Normal View History

2024-01-23 13:43:58 -05:00
import os
import requests
import zipfile
2024-01-23 15:07:13 -05:00
import shutil
2024-01-23 16:32:33 -05:00
import platform
2024-01-23 13:43:58 -05:00
from pathlib import Path
2024-01-23 16:32:33 -05:00
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
from io import BytesIO
2024-01-23 13:43:58 -05:00
2024-01-23 13:45:46 -05:00
def get_hl_path():
"""Get the Half-Life installation path based on the operating system."""
if platform.system() == "Windows":
return Path(os.getenv('ProgramFiles(x86)')) / "Steam/steamapps/common/Half-Life"
elif platform.system() == "Linux":
return Path.home() / ".steam/steam/steamapps/common/Half-Life"
else:
raise OSError("Unsupported operating system")
2024-01-23 13:43:58 -05:00
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()
2024-01-23 16:32:33 -05:00
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())
2024-01-23 13:43:58 -05:00
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)
2024-01-23 13:43:58 -05:00
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_to_path)
2024-01-23 16:32:33 -05:00
def update_mod():
print("Downloading new version...")
2024-01-23 13:43:58 -05:00
download_file(mod_url, mod_zip_file)
print(f"Unzipping the mod into {spacelife_path}...")
unzip_file(mod_zip_file, spacelife_path)
2024-01-23 16:32:33 -05:00
update_version_file(version_url, version_file)
2024-01-23 13:43:58 -05:00
print("Update complete!")
2024-01-23 16:32:33 -05:00
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"
mod_url = "https://deadzone.tf/SpaceLife.zip"
version_url = "https://deadzone.tf/SpaceLifeVersion.txt"
mod_zip_file = hl_path / "SpaceLife.zip"
spacelife_path.mkdir(parents=True, exist_ok=True)
root = tk.Tk()
root.title("SpaceLife Updater")
root.geometry("345x200")
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)
top_frame = tk.Frame(root, bg='blue')
top_frame.pack(side='top', fill='x')
version_label = tk.Label(top_frame, text=f"Version: {get_version()}", bg='blue', fg='black')
version_label.pack(side='right', padx=10, pady=20)
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)
2024-01-23 13:43:58 -05:00
else:
2024-01-23 16:32:33 -05:00
version_label.config(text=f"Version: {get_version()} (up to date)")
2024-01-23 13:43:58 -05:00
2024-01-23 16:32:33 -05:00
root.mainloop()