-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstaller.py
More file actions
128 lines (113 loc) · 3.9 KB
/
installer.py
File metadata and controls
128 lines (113 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
global winshell
import os
import zipfile
import shutil
import tempfile
import requests
import stat
import platform
import sys
if platform.system() == "Windows":
import winshell
# === CONFIGURATION ===
TARGET_DIR = os.path.expanduser("~/Documents/mc")
TAG = "!$!TAG!$!"
ZIP_URL = "https://github.com/StoppedwummPython/minecraft-launcher/archive/refs/tags/" + TAG + ".zip"
def download_zip(url, dest_path):
print(f"Downloading from {url}...")
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(dest_path, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk)
print("Download complete.")
else:
raise Exception(f"Failed to download file: {response.status_code}")
def extract_zip(zip_path, extract_to):
print(f"Extracting {zip_path}...")
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_to)
print("Extraction complete.")
def get_inner_folder(path):
entries = os.listdir(path)
folders = [entry for entry in entries if os.path.isdir(os.path.join(path, entry))]
if len(folders) != 1:
raise Exception("Expected one top-level folder in the ZIP")
return os.path.join(path, folders[0])
def copy_and_replace(src_dir, dest_dir):
print(f"Copying files to {dest_dir}...")
for root, dirs, files in os.walk(src_dir):
rel_path = os.path.relpath(root, src_dir)
target_root = os.path.join(dest_dir, rel_path)
os.makedirs(target_root, exist_ok=True)
for file in files:
src_file = os.path.join(root, file)
dest_file = os.path.join(target_root, file)
shutil.copy2(src_file, dest_file)
print("Copy complete.")
def create_run_scripts(path):
unix_script = f"""#!/bin/bash
cd "{path}"
if [ ! -d "node_modules" ]; then
npm install
fi
if [ ! -f "config.json" ]; then
node config_generator.js
fi
npm start
"""
windows_script = f"""@echo off
cd /d "{path}"
if not exist node_modules (
npm install
)
if not exist config.json (
node config_generator.js
)
npm start
"""
# Create scripts
run_sh_path = os.path.join(path, "run.sh")
run_bat_path = os.path.join(path, "run.bat")
with open(run_sh_path, "w") as f:
f.write(unix_script)
os.chmod(run_sh_path, os.stat(run_sh_path).st_mode | stat.S_IEXEC) # Make executable
with open(run_bat_path, "w") as f:
f.write(windows_script)
print("Launcher scripts created (run.sh, run.bat).")
def main():
with tempfile.TemporaryDirectory() as tmp_dir:
zip_path = os.path.join(tmp_dir, "download.zip")
extract_path = os.path.join(tmp_dir, "extracted")
download_zip(ZIP_URL, zip_path)
extract_zip(zip_path, extract_path)
inner_folder = get_inner_folder(extract_path)
copy_and_replace(inner_folder, TARGET_DIR)
create_run_scripts(TARGET_DIR)
ICON = os.path.join(TARGET_DIR, "logo.ico")
if platform.system() == "Windows":
winshell.shortcut(
os.path.join(TARGET_DIR, "run.bat"),
TARGET_DIR,
"Minecraft Launcher",
ICON,
"Minecraft Launcher"
)
elif platform.system() == "Linux":
# Create a .desktop file for Linux
desktop_file_path = os.path.join(os.path.expanduser("~/.local/share/applications"), "minecraft-launcher.desktop")
with open(desktop_file_path, "w") as f:
f.write(f"""[Desktop Entry]
Name=Minecraft Launcher
Exec={os.path.join(TARGET_DIR, "run.sh")}
Type=Application
Icon={ICON}
Terminal=false
StartupNotify=false
""")
print(f"Desktop entry created at {desktop_file_path}")
else:
print("Unsupported OS for creating shortcuts. Please create a shortcut manually.")
print(f"Launcher installed to {TARGET_DIR}.")
if __name__ == "__main__":
main()