|
| 1 | +import os |
| 2 | +import zipfile |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +import requests |
| 6 | +import stat |
| 7 | +import platform |
| 8 | + |
| 9 | +# === CONFIGURATION === |
| 10 | +ZIP_URL = "https://github.com/StoppedwummPython/minecraft-launcher/archive/main.zip" |
| 11 | +TARGET_DIR = os.path.expanduser("~/Documents/mc") |
| 12 | + |
| 13 | +def download_zip(url, dest_path): |
| 14 | + print(f"Downloading from {url}...") |
| 15 | + response = requests.get(url, stream=True) |
| 16 | + if response.status_code == 200: |
| 17 | + with open(dest_path, 'wb') as f: |
| 18 | + for chunk in response.iter_content(1024): |
| 19 | + f.write(chunk) |
| 20 | + print("Download complete.") |
| 21 | + else: |
| 22 | + raise Exception(f"Failed to download file: {response.status_code}") |
| 23 | + |
| 24 | +def extract_zip(zip_path, extract_to): |
| 25 | + print(f"Extracting {zip_path}...") |
| 26 | + with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 27 | + zip_ref.extractall(extract_to) |
| 28 | + print("Extraction complete.") |
| 29 | + |
| 30 | +def get_inner_folder(path): |
| 31 | + entries = os.listdir(path) |
| 32 | + folders = [entry for entry in entries if os.path.isdir(os.path.join(path, entry))] |
| 33 | + if len(folders) != 1: |
| 34 | + raise Exception("Expected one top-level folder in the ZIP") |
| 35 | + return os.path.join(path, folders[0]) |
| 36 | + |
| 37 | +def copy_and_replace(src_dir, dest_dir): |
| 38 | + print(f"Copying files to {dest_dir}...") |
| 39 | + for root, dirs, files in os.walk(src_dir): |
| 40 | + rel_path = os.path.relpath(root, src_dir) |
| 41 | + target_root = os.path.join(dest_dir, rel_path) |
| 42 | + os.makedirs(target_root, exist_ok=True) |
| 43 | + for file in files: |
| 44 | + src_file = os.path.join(root, file) |
| 45 | + dest_file = os.path.join(target_root, file) |
| 46 | + shutil.copy2(src_file, dest_file) |
| 47 | + print("Copy complete.") |
| 48 | + |
| 49 | +def create_run_scripts(path): |
| 50 | + unix_script = f"""#!/bin/bash |
| 51 | +cd "{path}" |
| 52 | +if [ ! -d "node_modules" ]; then |
| 53 | + npm install |
| 54 | +fi |
| 55 | +if [ ! -f "config.json" ]; then |
| 56 | + node config_generator.js |
| 57 | +fi |
| 58 | +npm start |
| 59 | +""" |
| 60 | + |
| 61 | + windows_script = f"""@echo off |
| 62 | +cd /d "{path}" |
| 63 | +if not exist node_modules ( |
| 64 | + npm install |
| 65 | +) |
| 66 | +if not exist config.json ( |
| 67 | + node config_generator.js |
| 68 | +) |
| 69 | +npm start |
| 70 | +""" |
| 71 | + |
| 72 | + # Create scripts |
| 73 | + run_sh_path = os.path.join(path, "run.sh") |
| 74 | + run_bat_path = os.path.join(path, "run.bat") |
| 75 | + |
| 76 | + with open(run_sh_path, "w") as f: |
| 77 | + f.write(unix_script) |
| 78 | + os.chmod(run_sh_path, os.stat(run_sh_path).st_mode | stat.S_IEXEC) # Make executable |
| 79 | + |
| 80 | + with open(run_bat_path, "w") as f: |
| 81 | + f.write(windows_script) |
| 82 | + |
| 83 | + print("Launcher scripts created (run.sh, run.bat).") |
| 84 | + |
| 85 | +def main(): |
| 86 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 87 | + zip_path = os.path.join(tmp_dir, "download.zip") |
| 88 | + extract_path = os.path.join(tmp_dir, "extracted") |
| 89 | + |
| 90 | + download_zip(ZIP_URL, zip_path) |
| 91 | + extract_zip(zip_path, extract_path) |
| 92 | + |
| 93 | + inner_folder = get_inner_folder(extract_path) |
| 94 | + copy_and_replace(inner_folder, TARGET_DIR) |
| 95 | + create_run_scripts(TARGET_DIR) |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments