-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeltamc.py
438 lines (402 loc) · 14.4 KB
/
deltamc.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import shutil
import os
import glob
import json
import sys
import tarfile
import zipfile
import argparse
import tkinter as tk
import delta_remove
import delta_upgrade
import delta_install
import delta_importexport
from delta_gui import *
import delta_gui
from delta_util import *
import tkinter.messagebox as msgbox
import tkinter.simpledialog as dialogs
execdir = os.getcwd() # needed for startup
def read_default_instance():
old_cwd = os.getcwd() #to reset cwd afterward
if (os.path.exists("LocalData") == False):
os.mkdir("LocalData")
os.chdir(os.path.join(execdir, "LocalData")) #at this point in startup, old_cwd is execdir
try:
with open("default_instance.txt") as f:
default = f.read().strip() #don't want leading trailing whitespace/newlines
except(FileNotFoundError):
default = "default"
with open("default_instance.txt", "w") as f:
f.write(default)
os.chdir(old_cwd) #restoring cwd
return default
def setup_config(_instance):
global modfolder, jarfolder, mc_version, instance, gui
os.chdir(os.path.join(execdir, "LocalData"))
instance = _instance
init_config_util_guionly(gui) # transferring gui flag to delta_util
modfolder, jarfolder, mc_version = read_config(_instance) # gets config stuff
os.chdir(execdir)
init_config_util((modfolder, jarfolder, mc_version, execdir, instance, gui)) # transferring config data (and Tkinter instance) to all files
delta_install.init_config_install((modfolder, jarfolder, mc_version, execdir, instance, gui))
delta_remove.init_config_remove((modfolder, jarfolder, mc_version, execdir, instance, gui))
delta_upgrade.init_config_upgrade((modfolder, jarfolder, mc_version, execdir, instance, gui))
delta_importexport.init_config_importexport((modfolder, jarfolder, mc_version, execdir, instance, gui))
init_config_gui((modfolder, jarfolder, mc_version, execdir, instance, gui))
def transfer_tkinst():
global tkinst
recieve_tkinst_util(tkinst)
delta_install.recieve_tkinst_install(tkinst)
delta_remove.recieve_tkinst_remove(tkinst)
delta_upgrade.recieve_tkinst_upgrade(tkinst)
delta_importexport.recieve_tkinst_importexport(tkinst)
delta_gui.recieve_tkinst_gui(tkinst)
# Start Program Here:
root = None
tkinst = None
parser = argparse.ArgumentParser(description="DeltaMC: A package manager for Minecraft Mods")
parser.add_argument("-i", "--install", help="install mod", metavar="MOD", default="None")
parser.add_argument("-r", "--remove", help="remove mod", metavar="MOD", default="None")
parser.add_argument("-u", "--upgrade", help="upgrade mod", metavar="MOD", default="None")
parser.add_argument("--info", help="give info about a mod", metavar="MOD", default="None")
parser.add_argument("-e", "--export", help="export a modlist", metavar="FILENAME", default="None")
parser.add_argument("--import", help="import a modlist", metavar="MODLIST", default="None", dest="importa") # importa because import is already taken
parser.add_argument("-I", "--instance", help="sets the Minecraft instance to install into", metavar="INSTANCE", default=instance)
parser.add_argument("-g", "--gui", help="enable GUI", action="store_true")
args = parser.parse_args()
gui = args.gui
if (gui):
root = tk.Tk()
# print(args.gui)
# print(gui)
# not making Data dir here because it is done later
if (os.path.exists("LocalData") == False):
os.mkdir("LocalData")
if (os.path.exists("LocalData/ModsDownloaded") == False):
os.mkdir("LocalData/ModsDownloaded")
if (os.path.exists("LocalData/Modlists") == False):
os.mkdir("LocalData/Modlists")
try:
shutil.rmtree("Data") # deleting Data dir
except(FileNotFoundError): # Data dir not present
pass
if (os.path.exists("Data") == False):
os.mkdir("Data")
if (os.path.exists("Data/temp") == False):
os.mkdir("Data/temp")
instance = read_default_instance()
try:
with open("LocalData/config.json") as json_file:
json_data = json.load(json_file)
insts = json_data.keys()
for inst in insts:
if(not os.path.exists(os.path.join(execdir, "LocalData/ModsDownloaded/"+inst))): # creating modsdownloaded subdirs for each instance
os.mkdir(os.path.join(execdir, "LocalData/ModsDownloaded/"+inst))
except:
pass
execdir = os.getcwd()
setup_config(instance)
if (gui):
tkinst = Gui(root)
transfer_tkinst()
cprint("You are running " + sys.platform)
update_archive(True)
if(gui):
delta_gui.updateinst()
tkinst.update_modlist()
check_for_updates()
cprint("DeltaMC v"+version)
instance = delta_gui.instance
if (args.instance != "None"):
instance = args.instance
if(instance == "@ERROR@"):
instance = delta_gui.instance
cprint("Selected Instance: "+instance)
cprint("Minecraft Version: "+mc_version)
upgradesavailable = delta_upgrade.get_upgrades(instance)
if (upgradesavailable == []):
pass
else:
cprint("The following upgrades are available for instance "+instance+":")
for upgrade in upgradesavailable:
cprint(" "+upgrade[0].name+" (current version: "+upgrade[1].versions[0]["Version"]+", you have: "+upgrade[0].versions[0]["Version"]+")")
if (args.install != "None"):
delta_install.install_mod(args.install)
sys.exit()
if (args.remove != "None"):
delta_remove.remove_mod(args.remove)
sys.exit()
if (args.upgrade != "None"):
delta_upgrade.upgrade_mod(args.upgrade)
sys.exit()
if (args.info != "None"):
get_info(args.info, output=True)
sys.exit()
if (args.export != "None"):
delta_importexport.export_mods(args.export)
sys.exit()
if (args.importa != "None"):
delta_importexport.import_mods(args.importa)
sys.exit()
if (gui == False):
print_help()
def parsecmd(command):
if(command.split(" ")[0] == "update"):
update_archive()
elif(command.split(" ")[0] == "upgrades"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
inst = command.split(" ")[1]
if(inst == "*"):
inst = None
if (not instance_exists(inst) and inst != None):
cprint("Instance "+inst+" does not exist.")
return
update_archive()
delta_upgrade.check_upgrades(True, inst)
elif(len(command.split(" ")) == 1):
inst = input("Enter instance name: ")
if(inst == "*"):
inst = None
if (not instance_exists(inst) and inst != None):
cprint("Instance "+inst+" does not exist.")
return
update_archive()
delta_upgrade.check_upgrades(True, inst)
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "upgrade"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
mod = command.split(" ")[1]
update_archive()
upgrades = delta_upgrade.get_upgrades()
delta_upgrade.upgrade_mod(mod)
elif(len(command.split(" ")) == 1):
mod = None
update_archive()
upgrades = delta_upgrade.get_upgrades()
delta_upgrade.upgrade_mod(mod)
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "upgradeall"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
inst = command.split(" ")[1]
if(inst == "*"):
inst = None
if (not instance_exists(inst) and inst != None):
cprint("Instance "+inst+" does not exist.")
return
elif(len(command.split(" ")) == 1):
inst = input("Enter instance name: ")
if(inst == "*"):
inst = None
if (not instance_exists(inst) and inst != None):
cprint("Instance "+inst+" does not exist.")
return
else:
cprint("Invalid command syntax.")
update_archive()
updates = delta_upgrade.get_upgrades(inst)
if(len(updates) == 0):
cprint("No upgrades available.")
else:
for update in updates:
delta_upgrade.upgrade_mod(update[0]["Name"])
elif(command.split(" ")[0] == "install"):
if(len(command.split(" ")) == 3 and command.split(" ")[2] != ""):
mod = command.split(" ")[1]
mod_version = command.split(" ")[2]
update_archive()
delta_install.install_mod(mod, version)
elif(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
mod = command.split(" ")[1]
update_archive()
delta_install.install_mod(mod)
elif(len(command.split(" ")) == 1):
mod = None
update_archive()
delta_install.install_mod(mod)
else:
cprint("Invalid command syntax.")
cprint(len(command.split(" ")))
elif(command.split(" ")[0] == "remove"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
mod = command.split(" ")[1]
delta_remove.remove_mod(mod)
elif(len(command.split(" ")) == 1):
mod = None
delta_remove.remove_mod(mod)
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "info"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
mod = command.split(" ")[1]
get_info(mod, output=True)
elif(len(command.split(" ")) == 1):
mod = None
get_info(mod, output=True)
elif(command.split(" ")[0] == "installm" or command.split(" ")[0] == "installmany"):
if(len(command.split(" ")) >= 2):
modslist = command.split(" ")[1:] # separate mod names with spaces
update_archive()
string = "Attempting to install: "
for item in modslist:
string = string + item+", "
cprint(string[:-2]+"...") # [:-2] to cut off the extra ", " after the last element
for item in modslist:
delta_install.install_mod(item)
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "removem" or command.split(" ")[0] == "removemany"):
if(len(command.split(" ")) >= 2):
modslist = command.split(" ")[1:] # separate mod names with spaces
update_archive()
string = "Attempting to remove: "
for item in modslist:
string = string + item+", "
cprint(string[:-2]+"...") # [:-2] to cut off the extra ", " after the last element
for item in modslist:
delta_remove.remove_mod(item)
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "upgradem" or command.split(" ")[0] == "upgrademany"):
if(len(command.split(" ")) >= 2):
modslist = command.split(" ")[1:] # separate mod names with spaces
update_archive()
string = "Attempting to upgrade: "
for item in modslist:
string = string + item+", "
cprint(string[:-2]+"...") # [:-2] to cut off the extra ", " after the last element
for item in modslist:
delta_upgrade.upgrade_mod(item)
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "export"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
name = command.split(" ")[1]
update_archive()
delta_importexport.export_mods(name)
elif(len(command.split(" ")) == 1):
name = None
update_archive()
delta_importexport.export_mods(name)
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "import"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
path = command.split(" ")[1]
update_archive()
delta_importexport.import_mods(path)
elif(len(command.split(" ")) == 1):
path = None
update_archive()
delta_importexport.import_mods(path)
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "instance" or command.split(" ")[0] == "inst"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
name = command.split(" ")[1]
if(instance_exists(name)):
if(name == instance):
cprint("Instance "+name+" already selected!")
else:
setup_config(name)
cprint("Switched to instance "+name+".")
else:
cprint("Instance "+name+" does not exist.")
elif(len(command.split(" ")) == 1):
name = input("Enter instance name: ")
if(instance_exists(name)):
if(name == instance):
cprint("Instance "+name+" already selected!")
else:
setup_config(name)
cprint("Switched to instance "+name+".")
else:
cprint("Instance "+name+" does not exist.")
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "setdefaultinstance" or command.split(" ")[0] == "setdefaultinst"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
name = command.split(" ")[1]
if(instance_exists(name)):
if(name == read_default_instance()):
cprint("Instance "+name+" already set as default!")
else:
with open("default_instance.txt", "w") as f:
f.write(name)
cprint("Set default instance as "+name+".")
else:
cprint("Instance "+instance+" does not exist.")
elif(len(command.split(" ")) == 1):
name = input("Enter instance name: ")
if(instance_exists(name)):
if(name == read_default_instance()):
cprint("Instance "+instance+" already set as default!")
else:
with open("default_instance.txt", "w") as f:
f.write(name)
cprint("Set default instance as "+name+".")
else:
cprint("Instance "+name+" does not exist.")
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "addinstance" or command.split(" ")[0] == "addinst"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
name = command.split(" ")[1]
if(instance_exists(name)):
cprint("Instance "+name+" already exists.")
else:
new_config(name)
elif(len(command.split(" ")) == 1):
name = input("Enter instance name: ")
if(instance_exists(name)):
cprint("Instance "+name+" already exists.")
else:
new_config(name)
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "rminstance" or command.split(" ")[0] == "removeinstance" or command.split(" ")[0] == "rminst"):
if(len(command.split(" ")) == 2 and command.split(" ")[1] != ""):
name = command.split(" ")[1]
if(instance_exists(name)):
rm_config(name)
else:
cprint("Instance "+name+" does not exist.")
elif(len(command.split(" ")) == 1):
name = input("Enter instance name: ")
if(instance_exists(name)):
rm_config(name)
else:
cprint("Instance "+name+" does not exist.")
else:
cprint("Invalid command syntax.")
elif(command.split(" ")[0] == "instances" or command.split(" ")[0] == "insts"):
with open("config.json") as json_file:
json_data = json.load(json_file)
insts = json_data.keys()
cprint("Instances:")
for inst in insts:
if(inst == instance):
cprint(inst+" (selected)")
else:
cprint(inst)
elif(command.split(" ")[0] == "list"):
listmods()
elif(command.split(" ")[0] == "version"):
cprint("delta v"+version)
elif(command.split(" ")[0] == "help" or command.split(" ")[0] == "?"):
print_help()
elif(command.split(" ")[0] == "exit"):
sys.exit()
elif(command == ""):
pass # don't print "Unknown command." for empty line
else:
cprint("Unknown command.")
if (gui == True):
tkinst.mainloop()
else:
while(True):
os.chdir(execdir + "/LocalData/") # reset current working dir
command = input("> ")
parsecmd(command)