-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdelta_install.py
205 lines (189 loc) · 7.97 KB
/
delta_install.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import requests
import shutil
import os
import glob
import json
import sys
import tarfile
import zipfile
from delta_util import *
import tkinter as tk
import tkinter.messagebox as msgbox
import tkinter.simpledialog as dialogs
from modclass import Mod
modfolder = "@ERROR@"
jarfolder = "@ERROR@"
execdir = "@ERROR@"
instance = "@ERROR@"
tkinst = None
def init_config_install(data): #data is a 5-tuple
global modfolder, jarfolder, mc_version, execdir, instance, gui #makes it edit the global vars rather than create new ones
modfolder, jarfolder, mc_version, execdir, instance, gui = data
def recieve_tkinst_install(data):
global tkinst
tkinst = data
def install_mod(modname, version = None):
os.chdir(execdir + "/Data/DeltaMC-Archive")
if(modname == None):
modname = cinput("Enter mod name: ")
if(os.path.exists(modname + ".json")): # Telling user that file exists
for file in glob.glob(modname + ".json"):
cprint(file + " found.")
mod_data = get_mod_from_name(modname)
else:
cprint("Mod "+modname+" not found.")
return -1
if (version == None):
if (not is_any_version_compatible(mod_data)):
cprint('No version of the mod is compatible!')
return
version = get_latest_compatible_version(mod_data)
version_number = 0
for x in range(len(mod_data.versions)):
cprint(mod_data.versions[x]['Version'])
cprint(version)
if (version == mod_data.versions[x]['Version']):
version_number = x
break
# Install
modtype = mod_data._type # Work out which type of mod it is
IsUnstable = mod_data.unstable
if (IsUnstable == True):
if not gui:
a = input("This mod may be unstable. Type OK to install, or anything else to cancel: ") == "OK"
else:
a = msgbox.askokcancel("Confirm Installation", "This mod may be unstable.\nClick OK to install.", parent=tkinst)
if (a):
pass
else:
cprint("Install canceled.")
return
if (mod_installed(modname)): # Making sure that the mod is not already installed
cprint(modname + " is already installed!")
return
originalfile = execdir + "/Data/DeltaMC-Archive/" + modname + ".json" # Saving Modname.json for future reference
if(not os.path.exists(execdir + "/LocalData/ModsDownloaded/"+instance)):
os.mkdir(execdir + "/LocalData/ModsDownloaded/"+instance)
os.chdir(execdir + "/LocalData/ModsDownloaded/"+instance)
newfilename = modname + ".installed"
with open(newfilename, 'w+') as newfile:
installedjson = get_json(modname)
installedjson["Versions"] = [mod_data.versions[version_number]]
newfile.write(json.dumps(installedjson))
requirements = mod_data.requirements
for requirement in requirements:
if (os.path.exists(requirement + ".installed") == False):
cprint("You must install " + requirement + " first!")
if not gui:
wanttoinstall = input("Do you want to install it? Y or n?") == "Y"
else:
wanttoinstall = msgbox.askyesno("Confirm Installation", "This mod requires " + requirement + ".\nInstall "+requirement+"?", parent=tkinst)
if(wanttoinstall):
install_mod(requirement)
elif(not wanttoinstall):
msgbox.showerror("Installation Canceled", "The installation has been canceled due to required mod "+requirement+" not being installed.", parent=tkinst)
return -1
recommended = mod_data.recommended
for recommendation in recommended:
if (os.path.exists(recommendation + ".installed") == False):
cprint("This mod recommends " + recommendation + "!")
if not gui:
wanttoinstall = input("Do you want to install it? Y or n?") == "Y"
else:
wanttoinstall = msgbox.askyesno("Confirm Installation", "This mod recommends " + recommendation + ".\nInstall "+recommendation+"?", parent=tkinst)
if(wanttoinstall):
install_mod(recommendation)
elif(not wanttoinstall):
pass
incompatibilities = mod_data.incompatibilities
for incompatibility in incompatibilities:
if (os.path.exists(incompatibility + ".installed") == True):
msgbox.showerror("Installation Canceled", "The installation has been canceled due to incompatible mod "+incompatibility+" being installed.", parent=tkinst)
return -1
mod_mc_versions = mod_data.versions[version_number]['MCVersion']
if (mc_version not in mod_mc_versions):
cprint("Mod not compatible with current Minecraft Version!")
return
url = get_url(mod_data, version)
cprint(modname + " is at version " + version)
if (modtype == "Basemod"):
os.chdir(execdir + "/Data/temp")
file_name = modname + "-" + version + "-DeltaMCtemp.zip"
cprint("Downloading " + url)
with open(file_name, 'wb') as out_file:
response = requests.get(url)
out_file.write(response.content)
zipfile.ZipFile(file_name).extractall(path="./"+modname)
vname = cinput("Enter name (as displayed in launcher) of minecraft instance to install into (compatible versions: "+display_versions(mcversions)+"): ")
#cannot check for compatibility because you may be installing into a modded jar with a nonstandard name
vpath = os.path.join(jarfolder, vname)
while(not os.path.exists(vpath)):
cprint("The instance you selected was not found. Select another instance.")
vname = cinput("Enter name (as displayed in launcher) of minecraft instance to install into (compatible versions: "+display_versions(mcversions)+"): ")
vpath = os.path.join(jarfolder, vname)
jarname = vname+".jar"
jarpath = os.path.join(vpath, jarname)
foldername = modname + "-" + version #the default version folder name
foldernamefinal = cinput("Enter install folder name or leave blank for default (default: "+foldername+"): ")
if(foldername == ""):
foldernamefinal = foldername
newjarname = foldername+".jar"
cprint("Installing on version "+vname+" as "+foldernamefinal+".")
if(os.path.exists(os.path.join(jarfolder, foldernamefinal))):
if(cinput("The folder "+foldernamefinal+" already exists. Type OK to overwrite, or anything else to choose a new name: ") == "OK"):
shutil.rmtree(os.path.join(jarfolder, foldernamefinal))
else:
foldernamefinal = cinput("Enter new install folder name (current name: "+foldernamefinal+"): ")
folderpath = os.path.join(jarfolder, foldernamefinal)
shutil.copytree(vpath, folderpath)
fix_names(folderpath, vname, foldernamefinal)
zipfile.ZipFile(os.path.join(jarfolder, foldernamefinal, newjarname)).extractall(path="./"+file_name+"DeltaMCtemp")
mergedirs(modname, file_name+"DeltaMCtemp")
os.chdir(file_name+"DeltaMCtemp")
shutil.rmtree("META-INF") #delete META-INF
cprint("Making jar (this might take a while).")
shutil.make_archive("../"+foldername, "zip")
shutil.move("../"+foldername+".zip", folderpath+"/"+foldernamefinal+".jar")
os.chdir("../../LocalData") #get back to LocalData
cprint("Done.")
elif (modtype == "Forge"):
os.chdir(execdir + "/LocalData")
os.chdir(modfolder)
file_name = modname + "-" + version + ".jar"
cprint("Downloading " + url + " as " + file_name)
with open(file_name, 'wb') as out_file:
response = requests.get(url)
out_file.write(response.content)
cprint("Done.")
elif (modtype == "Liteloader"):
os.chdir(execdir + "/LocalData")
file_name = modname + "-" + version + ".litemod"
os.chdir(modfolder)
cprint("Downloading " + url + " as " + file_name)
with open(file_name, 'wb') as out_file:
response = requests.get(url)
out_file.write(response.content)
cprint("Done.")
elif (modtype == "Installer"):
os.chdir(execdir + "/LocalData")
version = get_latest_version(mod_data)
url = get_url(mod_data, version)
cprint(modname + " is at version " + version)
file_name = mod_data.installer_name
os.chdir(execdir)
files = os.listdir(execdir)
cprint("Downloading " + url + " as " + file_name)
with open(file_name, 'wb') as out_file:
response = requests.get(url)
out_file.write(response.content)
if(gui):
msgbox.showinfo("Installer Downloaded", "The installer for "+modname+" has been downloaded.\nRun the installer, then click OK to continue.", parent=tkinst)
cprint("Done. Please run the installer.")
if (gui):
tkinst.mlisti.insert(tk.END, modname)
return 0
def install_deps(modname):
deps = get_deps(modname)
for dep in deps:
if(not mod_installed(dep)):
install_mod(dep)