import os import requests import zipfile import platform from pathlib import Path 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") 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) 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): 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() if not local_version_path.exists(): local_version_path.with_suffix('.tmp').replace(local_version_path) return True with open(local_version_path, 'r') as local_version_file: local_version = local_version_file.read().strip() 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): 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: print("No update needed. You have the latest version.") if mod_zip_file.exists(): mod_zip_file.unlink()