-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
139 lines (117 loc) · 4.23 KB
/
functions.py
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
129
130
131
132
133
134
135
136
137
138
139
import requests
import zipfile
import json
import os
import io
import platform
import subprocess
import shutil
def load_json(file):
with open(file, 'r') as f:
return json.load(f)
def open_settings():
return load_json(".\\settings.json")
def open_folder(path):
if path is None:
print(f"Error: Attempted to open a folder with a None path: {path}")
return
if not os.path.exists(path):
print(f"Error: The specified path does not exist: {path}")
return
if platform.system() == "Windows":
os.startfile(str(path))
elif platform.system() == "Darwin":
subprocess.run(["open", str(path)])
else:
subprocess.run(["xdg-open", str(path)])
def download_and_extract(url, extract_to):
response = requests.get(url)
if response.status_code == 200:
with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref:
zip_ref.extractall(extract_to)
print(f"Extracted {url} to {extract_to}")
return True
else:
print(f"Failed to download {url}")
return False
def get_latest_bepinex_url():
try:
response = requests.get("https://api.github.com/repos/BepInEx/BepInEx/releases/latest")
response.raise_for_status()
release_info = response.json()
assets = release_info.get('assets', [])
user_os = platform.system().lower()
if user_os == "windows":
user_os = "win"
elif user_os == "darwin":
user_os = "macos"
else:
user_os = "linux"
for asset in assets:
if f"{user_os}_x64" in asset['name'] and asset['name'].endswith('.zip'):
return asset['browser_download_url']
except requests.exceptions.RequestException as e:
print(f"Failed to fetch release info from https://api.github.com/repos/BepInEx/BepInEx/releases/latest: {e}")
return None
class bind_func:
settings = open_settings()
game_directory = settings["gameDir"]
@staticmethod
def print(txt):
print(txt)
@staticmethod
def open_folder(dir):
open_folder(dir)
@staticmethod
def open_game_folder():
if not bind_func.game_directory:
return False, "Game directory is not set"
if not os.path.exists(bind_func.game_directory):
return False, f"Game directory does not exist: {bind_func.game_directory}"
open_folder(bind_func.game_directory)
return True, f"Opened game folder: {bind_func.game_directory}"
@staticmethod
def open_bepinex_folder():
dir = f"{bind_func.game_directory}\\BepInEx"
if not dir:
return False, "BepInEx directory is not set"
if not os.path.exists(dir):
return False, f"BepInEx directory does not exist: {dir}"
open_folder(dir)
return True, f"Opened BepInEx folder: {dir}"
@staticmethod
def open_settings():
return open_settings()
@staticmethod
def get_os():
return platform.system()
@staticmethod
def set_game_directory(dir):
bind_func.game_directory = dir
return True
@staticmethod
def install_bepinex():
success = True
messages = []
# Install BepInEx
bepinex_url = get_latest_bepinex_url()
if bepinex_url:
if download_and_extract(bepinex_url, bind_func.game_directory):
messages.append("BepInEx installed successfully")
else:
success = False
messages.append("Failed to install BepInEx")
else:
success = False
messages.append("Failed to get the latest BepInEx download URL")
@staticmethod
def uninstall_bepinex():
shutil.rmtree(f"{bind_func.game_directory}\\BepInEx")
files = [
f"{bind_func.game_directory}\\.doorstop_version",
f"{bind_func.game_directory}\\changelog.txt",
f"{bind_func.game_directory}\\doorstop_config.ini",
f"{bind_func.game_directory}\\winhttp.dll"
]
for file in files:
os.remove(file)