import sys from PyQt6.QtWidgets import ( QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton, QLabel, QSpacerItem, QSizePolicy, ) from PyQt6.QtCore import Qt, QThread, pyqtSignal from PyQt6.QtGui import QFont import requests from pathlib import Path import subprocess import os INSTALL_DIR = Path.home() / ".local" / "share" / "osu-launcher" APPIMAGE_PATH = INSTALL_DIR / "osu.AppImage" TEMP_PATH = INSTALL_DIR / "osu.AppImage.temp" VERSION_FILE = INSTALL_DIR / "version.txt" class DownloadWorker(QThread): progress_signal = pyqtSignal(int) status_signal = pyqtSignal(str) finished_signal = pyqtSignal(bool, str) def __init__( self, download_url, temp_path, appimage_path, version_file, version_tag ): super().__init__() self.download_url = download_url self.temp_path = temp_path self.appimage_path = appimage_path self.version_file = version_file self.version_tag = version_tag def run(self): """This method runs automatically in the background when you call .start()""" try: self.status_signal.emit("Downloading update...") self.temp_path.parent.mkdir(parents=True, exist_ok=True) with requests.get(self.download_url, stream=True, timeout=10) as r: r.raise_for_status() total_size = int(r.headers.get("content-length", 0)) downloaded = 0 chunk_size = 1024 * 1024 # 1MB chunks with open(self.temp_path, "wb") as f: for chunk in r.iter_content(chunk_size=chunk_size): if chunk: f.write(chunk) downloaded += len(chunk) if total_size > 0: percent = int((downloaded / total_size) * 100) self.progress_signal.emit(percent) self.status_signal.emit("Finalizing installation...") self.temp_path.rename(self.appimage_path) self.appimage_path.chmod(0o755) self.version_file.write_text(self.version_tag) self.finished_signal.emit(True, "Update complete! Ready to play.") except Exception as e: if self.temp_path.exists(): self.temp_path.unlink() self.finished_signal.emit(False, f"Download failed: {str(e)}") class OsuLauncher(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("osu! Launcher") self.setFixedSize(400, 500) # Main widget and layout self.central_widget = QWidget() self.setCentralWidget(self.central_widget) self.layout = QVBoxLayout(self.central_widget) self.layout.setContentsMargins(40, 40, 40, 40) self.layout.setSpacing(15) self.download_url = None self.latest_version = None self.setStyleSheet(""" QMainWindow { background-color: #1a1a24; } QLabel { color: #ffffff; font-family: 'Segoe UI', Arial, sans-serif; } """) self.init_ui() def init_ui(self): self.title_label = QLabel("osu!") self.title_label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.title_label.setStyleSheet( "font-size: 64px; font-weight: bold; color: #ff66aa;" ) self.layout.addWidget(self.title_label) self.layout.addSpacerItem( QSpacerItem( 20, 40, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding ) ) self.status_label = QLabel("Checking for updates...") self.status_label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.status_label.setStyleSheet("font-size: 14px; color: #a0a0b0;") self.layout.addWidget(self.status_label) self.update_btn = QPushButton("Update Available") self.update_btn.setCursor(Qt.CursorShape.PointingHandCursor) self.update_btn.setStyleSheet(""" QPushButton { background-color: #2d2d3d; color: #66ccff; border: 1px solid #66ccff; border-radius: 8px; padding: 10px; font-size: 14px; font-weight: bold; } QPushButton:hover { background-color: #3a3a4d; } QPushButton:pressed { background-color: #1a1a24; } """) self.update_btn.hide() self.update_btn.clicked.connect(self.start_update) self.layout.addWidget(self.update_btn) self.play_btn = QPushButton("PLAY") self.play_btn.setCursor(Qt.CursorShape.PointingHandCursor) self.play_btn.setStyleSheet(""" QPushButton { background-color: #ff66aa; color: white; border: none; border-radius: 8px; padding: 15px; font-size: 24px; font-weight: bold; } QPushButton:hover { background-color: #ff88bd; } QPushButton:pressed { background-color: #e65c99; } QPushButton:disabled { background-color: #4a4a59; color: #808090; } """) self.play_btn.clicked.connect(self.launch_game) self.layout.addWidget(self.play_btn) self.check_for_updates() def check_for_updates(self): try: response = requests.get( "https://api.github.com/repos/ppy/osu/releases/latest" ) data = response.json() latest_version = data.get("tag_name") self.latest_version = latest_version version_path = Path(f"{INSTALL_DIR}/version.txt") current_version = "none" if version_path.exists(): current_version = version_path.read_text().strip() if latest_version != current_version: for asset in data.get("assets", []): if asset["name"] == "osu.AppImage": self.download_url = asset["browser_download_url"] break self.status_label.setText(f"Version {latest_version} is available.") self.update_btn.setText(f"Update to {latest_version}") self.update_btn.show() else: self.launch_game() except Exception: if APPIMAGE_PATH.exists(): self.launch_game() else: self.status_label.setText("No internet & osu! is not installed.") self.play_btn.setEnabled(False) def start_update(self): if not self.download_url: return self.update_btn.hide() self.play_btn.setEnabled(False) self.worker = DownloadWorker( self.download_url, TEMP_PATH, APPIMAGE_PATH, VERSION_FILE, self.latest_version, ) self.worker.progress_signal.connect(self.on_download_progress) self.worker.status_signal.connect(self.on_status_update) self.worker.finished_signal.connect(self.on_download_finished) self.worker.start() def on_status_update(self, message): self.status_label.setText(message) def on_download_progress(self, percent): self.status_label.setText(f"Downloading update... {percent}%") def on_download_finished(self, success, message): self.status_label.setText(message) if success: self.play_btn.setEnabled(True) else: self.update_btn.show() def launch_game(self): if not APPIMAGE_PATH.exists(): self.status_label.setText("Error: osu.AppImage not found!") return self.status_label.setText("Launching osu!...") env = os.environ.copy() env["APPIMAGELAUNCHER_DISABLE"] = "1" try: subprocess.Popen( [str(APPIMAGE_PATH)], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True, ) sys.exit(0) except Exception as e: self.status_label.setText(f"Failed to launch: {e}") self.play_btn.setEnabled(True) if __name__ == "__main__": app = QApplication(sys.argv) window = OsuLauncher() window.show() sys.exit(app.exec())