-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
778 lines (777 loc) · 28.6 KB
/
main.py
File metadata and controls
778 lines (777 loc) · 28.6 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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
# Copyright (C) 2012,2013 yogpstop
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the
# GNU Lesser General Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
import os, sys, shutil, zipfile, time, copy, re, urllib, logging, subprocess, getopt
from ConfigParser import SafeConfigParser
from xml.etree.ElementTree import ElementTree, Element
from xml.etree.ElementTree import tostring as TreeToStr
_path_ = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def isbinary(file):##text file contains only HT,LF,CR and basic characters
f = open(file,"rb")
for b in f.read():
value = ord(b)
if value < 9:
return True
if 10 < value and value < 13:
return True
if 13 < value and value < 32:
return True
if value == 127:
return True
f.close()
return False
def cmp_version(a,b):
cr = re.compile("[^0-9]")
sa = cr.split(a)
salen = len(sa)
sb = cr.split(b)
sblen = len(sb)
i = 0
while True:
if i >= salen:
if not (i >= sblen):
return -1-int(sb[i])
return 0
if i >= sblen:
return 1+int(sa[i])
resu = int(sa[i]) - int(sb[i])
if not (resu == 0):
return resu
i+=1
def select_one():
dirs=[]
for dir in os.listdir(_path_):
if dir.startswith("."):
continue
if not os.path.isdir(os.path.join(_path_,dir)):
continue
if not os.path.isfile(os.path.join(_path_,dir,"build.cfg")):
continue
dirs.append(dir)
i = 0
while(i<len(dirs)):
print str(i)+" "+dirs[i]
i+=1
while True:
num = raw_input("select target project: ")
if num.isdigit():
if int(num)<len(dirs):
return dirs[int(num)]
def install(version=None):
forge_zip = os.path.join(_path_,"forge.zip")
print "> Start forge install"###############################################################################################
builds = {}
fl = urllib.urlopen("http://files.minecraftforge.net/minecraftforge/index_legacy.html").read().decode().splitlines()
regax = re.compile("http://files.minecraftforge.net/minecraftforge/minecraftforge-src-([^-]+)-([^-]+).zip")
for build in fl:
m = regax.search(build)
if m:
if cmp_version("4.0.0.188",m.group(2)) > 0:
continue
if not builds.has_key(m.group(1)):
builds[m.group(1)] = {}
builds[m.group(1)][m.group(2)]=m.group(0)
builds["1.6.4"]["9.11.1.965"]="http://files.minecraftforge.net/maven/net/minecraftforge/forge/1.6.4-9.11.1.965/forge-1.6.4-9.11.1.965-src.zip"
builds["1.6.4"]["9.11.1.953"]="http://files.minecraftforge.net/maven/net/minecraftforge/forge/1.6.4-9.11.1.953/forge-1.6.4-9.11.1.953-src.zip"
############################################################################################################################
if version is not None:
vlist = version.split("-", 1)
mcversion = vlist[0]
build = vlist[1]
print "> Downloading MinecraftForge"
urllib.urlretrieve(builds[mcversion][build],forge_zip)
else:
for m in sorted(builds.keys(), cmp=cmp_version):
print m
while True:
mcversion = raw_input("select Minecraft version: ")
if builds.has_key(mcversion):
break
############################################################################################################################
for build in sorted(builds[mcversion].keys(), cmp=cmp_version):
print build
done = False
while True:
bnum = raw_input("select Forge build: ")
for build in builds[mcversion].keys():
if build==bnum or build.split(".")[-1]==bnum:
print "> Downloading MinecraftForge"
urllib.urlretrieve(builds[mcversion][build],forge_zip)
done = True
break
if done:
break
md5_patch = (mcversion == "1.6.4")
scala_patch = False
if cmp_version("7.7.1.672", build) <= 0 and cmp_version("7.8.1.738", build) >= 0:
scala_patch = True
print "> Extracting MinecraftForge"#########################################################################################
zf = zipfile.ZipFile(forge_zip,"r")
zf.extractall(_path_)
zf.close()
os.remove(forge_zip)
############################################################################################################################
forge_dir = os.path.join(_path_,"forge")
fml_dir = os.path.join(forge_dir,"fml")
mcp_dir = os.path.join(_path_,".api","Forge"+mcversion+"-"+build)
if md5_patch:
cfg = SafeConfigParser()
mcvs=os.path.join(fml_dir,"mc_versions.cfg")
mcvs_fp = open(mcvs, "rb")
cfg.readfp(mcvs_fp)
mcvs_fp.close()
cfg.set("1.6.4", "server_md5", "abcf286a14f7aee82e8bf89270433509");
mcvs_fp = open(mcvs, "wb")
cfg.write(mcvs_fp)
mcvs_fp.close()
if os.path.isdir(mcp_dir):
shutil.rmtree(mcp_dir)
sys.path.append(fml_dir)
sys.path.append(forge_dir)
import forge,fml
print "> Forge ModLoader Setup Start"
try:
fml.download_mcp(fml_dir=fml_dir, mcp_dir=mcp_dir)
except AttributeError:
pass
try:
fml.setup_mcp(fml_dir=fml_dir, mcp_dir=mcp_dir, gen_conf=False)
except TypeError:
fml.setup_mcp(fml_dir=fml_dir, mcp_dir=mcp_dir, dont_gen_conf=True)
if scala_patch:
scala_jar = os.path.join(mcp_dir, "lib")
os.makedirs(scala_jar)
scala_jar = os.path.join(scala_jar, "scala-library.jar")
urllib.urlretrieve("http://files.minecraftforge.net/fmllibs/scala-library.jar.stash", scala_jar)
cfg = os.path.join(mcp_dir, "conf", "astyle.cfg")
fobj = open(cfg, "rb")
astyledata=fobj.read()
fobj.close()
fobj = open(cfg, "wb")
fobj.write(re.sub("max-instatement-indent=[0-9]+", "max-instatement-indent=40", astyledata))
fobj.close()
try:
fml.setup_fml(fml_dir=fml_dir, mcp_dir=mcp_dir)
except AttributeError:
fml.decompile_minecraft(fml_dir=fml_dir, mcp_dir=mcp_dir)
src_dir = os.path.join(mcp_dir,"src")
fml.apply_fml_patches(fml_dir=fml_dir, mcp_dir=mcp_dir, src_dir=src_dir)
fml.finish_setup_fml(fml_dir=fml_dir, mcp_dir=mcp_dir)
print "> Forge ModLoader Setup End"
print "> Minecraft Forge Setup Start"
print "> Applying forge patches"
forge.apply_forge_patches(fml_dir=fml_dir, mcp_dir=mcp_dir, forge_dir=forge_dir, src_dir=src_dir)
config = SafeConfigParser()
conffile = os.path.join(mcp_dir,"conf","mcp.cfg")
conffilep = open(conffile, "rb")
config.readfp(conffilep)
conffilep.close()
sys.path.append(mcp_dir)
os.chdir(mcp_dir)
from runtime.mcp import updatenames_side,updatemd5_side,recompile_side
from runtime.commands import Commands,CLIENT,SERVER
cmd = Commands()
if os.path.isdir(os.path.join(mcp_dir,os.path.normpath(config.get("OUTPUT","srcclient")))):
updatenames_side(cmd,CLIENT)
updatemd5_side(cmd,CLIENT)
recompile_side(cmd,CLIENT)
if os.path.isdir(os.path.join(mcp_dir,os.path.normpath(config.get("OUTPUT","srcserver")))):
updatenames_side(cmd,SERVER)
updatemd5_side(cmd,SERVER)
recompile_side(cmd,SERVER)
fml.reset_logger()
print "> Minecraft Forge Setup Finished"
shutil.rmtree(forge_dir)
print "> Editing eclipse workspace"#########################################################################################
#----------Initialize Directories
mcploc = "$%7BWORKSPACE_LOC%7D/.api/Forge"+mcversion+"-"+build
basedir = os.path.join(mcp_dir,config.get("DEFAULT","DirEclipse"))
dbg=os.path.join(basedir,".metadata",".plugins","org.eclipse.debug.core",".launches")
#----------.project Fixes
tree = ElementTree()
pj=os.path.join(basedir,"Minecraft",".project")
tree.parse(pj)
linkedResources = tree.find("linkedResources")
common_src_dir = os.path.join(src_dir,"common")
common_src_dir_exist = os.path.isdir(common_src_dir)
for link in linkedResources.findall("link")[:]:
name = link.findtext("name")
if name == "src" or name == "common":
linkedResources.remove(link)
for vari in tree.find("variableList").findall("variable"):
if vari.findtext("name") == "MCP_LOC":
vari.find("value").text = mcploc
break
tree.find("name").text = "@PROJECT_NAME@"
tree.write(pj)
#---------.classpath Fixes
tree = ElementTree()
cp=os.path.join(basedir,"Minecraft",".classpath")
tree.parse(cp)
for cpe in tree.getroot().findall("classpathentry")[:]:
path = cpe.get("path")
if path == "src" or path == "common":
tree.getroot().remove(cpe)
tree.getroot().insert(0,Element("classpathentry",
{"kind":"lib","path":"lib/deobfMC.jar","sourcepath":"lib/deobfMC-src.jar"}))
outputfile = open(cp,"wb")
outputfile.write(TreeToStr(tree.getroot()).replace("Minecraft/","@PROJECT_NAME@/"))
outputfile.close()
#----------Client.launch Fixes
tree = ElementTree()
tree.parse(os.path.join(dbg,"Client.launch"))
for cache in tree.getroot().findall("listAttribute"):
if cache.get("key") == "org.eclipse.debug.core.MAPPED_RESOURCE_PATHS":
for cache2 in cache.findall("listEntry"):
if cache2.get("value").endswith(".java"):
cache2.set("value","/@PROJECT_NAME@/lib/deobfMC.jar")
if cache2.get("value").startswith("/Minecraft/"):
cache2.set("value",cache2.get("value").replace("/Minecraft/","/@PROJECT_NAME@/",1))
break
for cache in tree.getroot().findall("stringAttribute"):
if cache.get("key") == "org.eclipse.jdt.launching.PROJECT_ATTR":
cache.set("value","@PROJECT_NAME@")
if cache.get("key") == "org.eclipse.jdt.launching.WORKING_DIRECTORY":
cache.set("value","${workspace_loc:@PROJECT_NAME@/jars}")
if cache.get("key") == "org.eclipse.jdt.launching.VM_ARGUMENTS":
cache.set("value",cache.get("value")+" -Dfml.ignoreInvalidMinecraftCertificates=true")
tree.write(os.path.join(dbg,"Client.launch"))
#----------Server.launch Fixes
tree = ElementTree()
tree.parse(os.path.join(dbg,"Server.launch"))
for cache in tree.getroot().findall("listAttribute"):
if cache.get("key") == "org.eclipse.debug.core.MAPPED_RESOURCE_PATHS":
for cache2 in cache.findall("listEntry"):
if cache2.get("value").endswith(".java"):
cache2.set("value","/@PROJECT_NAME@/lib/deobfMC.jar")
if cache2.get("value").startswith("/Minecraft/"):
cache2.set("value",cache2.get("value").replace("/Minecraft/","/@PROJECT_NAME@/",1))
break
for cache in tree.getroot().findall("stringAttribute"):
if cache.get("key") == "org.eclipse.jdt.launching.PROJECT_ATTR":
cache.set("value","@PROJECT_NAME@")
if cache.get("key") == "org.eclipse.jdt.launching.WORKING_DIRECTORY":
cache.set("value","${workspace_loc:@PROJECT_NAME@/jars}")
tree.write(os.path.join(dbg,"Server.launch"))
print "> Creating minecraft libraries"######################################################################################
lib_dir = os.path.join(mcp_dir,config.get("DEFAULT","DirLib"))
cl_bin_dir = os.path.join(mcp_dir,os.path.normpath(config.get("RECOMPILE","binclient")))
cl_src_dir = os.path.join(mcp_dir,os.path.normpath(config.get("OUTPUT","srcclient")))
cdir = os.path.join(os.path.join(lib_dir,"deobfMC"))
cdirs = os.path.join(os.path.join(lib_dir,"deobfMC-src"))
shutil.rmtree(cdir,True)
shutil.rmtree(cdirs,True)
for dpath,dnames,fnames in os.walk(cl_bin_dir):
for fname in fnames:
p = os.path.join(dpath,fname)
zp = os.path.join(cdir,os.path.relpath(p,cl_bin_dir))
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
for dpath,dnames,fnames in os.walk(cl_src_dir):
for fname in fnames:
p = os.path.join(dpath,fname)
zp = os.path.join(cdirs,os.path.relpath(p,cl_src_dir))
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
if fname.endswith(".java"):
continue
zp = os.path.join(cdir,os.path.relpath(p,cl_src_dir))
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
if common_src_dir_exist:
for dpath,dnames,fnames in os.walk(common_src_dir):
for fname in fnames:
p = os.path.join(dpath,fname)
zp = os.path.join(cdirs,os.path.relpath(p,common_src_dir))
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
if fname.endswith(".java"):
continue
zp = os.path.join(cdir,os.path.relpath(p,common_src_dir))
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
subprocess.check_call(["jar","cf",os.path.join(lib_dir,"deobfMC.jar"),"-C",cdir,"."])
subprocess.check_call(["jar","cf",os.path.join(lib_dir,"deobfMC-src.jar"),"-C",cdirs,"."])
shutil.rmtree(cdir)
shutil.rmtree(cdirs)
print "> Editing config file"###############################################################################################
config.set("OUTPUT","TestClient","dummy")
confobj = open(conffile,"wb")
config.write(confobj)
confobj.close()
return mcversion+"-"+build
def get_versions(allow_empty=False):
versions = []
for dir in os.listdir(os.path.join(_path_,".api")):
if not dir.startswith("Forge"):
continue
versions.append(dir.replace("Forge",""))
if len(versions)==0 and not allow_empty:
versions.append(install())
versions = sorted(versions, cmp=cmp_version)
return versions
def get_newest():
return get_versions()[-1]
def i_eclipse(dir,version=""):
todir = os.path.join(_path_,dir)
cf = os.path.join(todir,"build.cfg")
if not os.path.isfile(cf):
print dir+" doesn't have build.cfg. Create it!"
return
if not version in get_versions():
print "> Minecraft version is invalid. Change to newest"
version = get_newest()
print "> Copying eclipse project files to "+dir
#-----build.cfg Fixes
config = SafeConfigParser()
cff = open(cf,"rb")
config.readfp(cff)
cff.close()
config.set("pj","mcv",version)
cff = open(cf,"wb")
config.write(cff)
cff.close()
#-----InitializeDirectories
fromdir = os.path.join(_path_,".api","Forge"+version,"eclipse")
l_fromdir = os.path.join(fromdir,".metadata",".plugins","org.eclipse.debug.core",".launches")
l_todir = os.path.join(_path_,".metadata",".plugins","org.eclipse.debug.core",".launches")
if not os.path.isdir(l_todir):
os.makedirs(l_todir)
#-----.classpath File
tree = ElementTree()
tree.parse(os.path.join(fromdir,"Minecraft",".classpath"))
for src in config.get("pj","src").split(":"):
if src.endswith("/"):
tree.getroot().append(Element("classpathentry",{"kind":"src","path":src[:-1]}))
else:
point = src.rfind("/")
if point == -1:
bsrc = ""
nsrc = src
else:
bsrc = src[:point]
nsrc = src[point+1:]+"/"
done = False
for entry in tree.getroot().findall("classpathentry"):
if entry.get("kind") == "src" and entry.get("path") == bsrc:
entry.set("including",entry.get("including")+"|"+nsrc)
done = True
if not done:
tree.getroot().append(Element("classpathentry",{"kind":"src","path":bsrc,"including":nsrc}))
if config.has_option("pj","res"):
for src in config.get("pj","res").split(":"):
if src.endswith("/"):
tree.getroot().append(Element("classpathentry",{"kind":"src","path":src[:-1]}))
else:
point = src.rfind("/")
if point == -1:
bsrc = ""
nsrc = src
else:
bsrc = src[:point]
nsrc = src[point+1:]+"/"
done = False
for entry in tree.getroot().findall("classpathentry"):
if entry.get("kind") == "src" and entry.get("path") == bsrc:
entry.set("including",entry.get("including")+"|"+nsrc)
done = True
if not done:
tree.getroot().append(Element("classpathentry",{"kind":"src","path":bsrc,"including":nsrc}))
if config.has_option("pj","api"):
for api in config.get("pj","api").split(":"):
tree.getroot().append(Element("classpathentry",
{"kind":"lib","path":"lib/"+api+".jar","sourcepath":"lib/"+api+"-src.jar"}))
outputfile = open(os.path.join(todir,".classpath"),"wb")
outputfile.write(TreeToStr(tree.getroot()).replace("@PROJECT_NAME@",dir))
outputfile.close()
#-----.project File
inputfile = open(os.path.join(fromdir,"Minecraft",".project"),"rb")
filedata = inputfile.read()
inputfile.close()
outputfile = open(os.path.join(todir,".project"),"wb")
outputfile.write(filedata.replace("@PROJECT_NAME@",dir))
outputfile.close()
#-----ClientBootFile
inputfile = open(os.path.join(l_fromdir,"Client.launch"),"rb")
filedata = inputfile.read()
inputfile.close()
outputfile = open(os.path.join(l_todir,dir+"Client.launch"),"wb")
outputfile.write(filedata.replace("@PROJECT_NAME@",dir))
outputfile.close()
#-----ServerBootFile
inputfile = open(os.path.join(l_fromdir,"Server.launch"),"rb")
filedata = inputfile.read()
inputfile.close()
outputfile = open(os.path.join(l_todir,dir+"Server.launch"),"wb")
outputfile.write(filedata.replace("@PROJECT_NAME@",dir))
outputfile.close()
def i_select(version=None,all=False):
if version == None:
version = get_newest()
if not version in get_versions():
print "> Minecraft version is invalid. Change to newest"
version = get_newest()
for dir in os.listdir(_path_):
if dir.startswith("."):
continue
if not os.path.isdir(os.path.join(_path_,dir)):
continue
if not os.path.isfile(os.path.join(_path_,dir,"build.cfg")):
continue
if all:
i_eclipse(dir,version)
else:
while True:
ret = raw_input(dir+" is MinecraftForge project[y/n]?")
if ret[0] == "y" or ret[0] == "Y":
i_eclipse(dir,version)
break
if ret[0] == "n" or ret[0] == "N":
break
def build(pname):
starttime = time.time()
pj_dir = os.path.join(_path_,pname)
pj_cfg_f = os.path.join(pj_dir,"build.cfg")
if not os.path.isfile(pj_cfg_f):
print dir+" doesn't have build.cfg. Create it!"
return
pj_cfg = SafeConfigParser()
pj_cfg_fp = open(pj_cfg_f, "rb")
pj_cfg.readfp(pj_cfg_fp)
pj_cfg_fp.close()
if pj_cfg.has_option("pj","mcv"):
forge_v = pj_cfg.get("pj","mcv")
else:
forge_v = ""
if not forge_v in get_versions(True):
forge_v = install(forge_v)
mcp_dir = os.path.join(_path_,".api","Forge"+forge_v)
mcp_cfg_f = os.path.join(mcp_dir,"conf","mcp.cfg")
mcp_cfg = SafeConfigParser()
mcp_cfg_fp = open(mcp_cfg_f, "rb")
mcp_cfg.readfp(mcp_cfg_fp)
mcp_cfg_fp.close()
mcp_src_dir = os.path.join(mcp_dir,mcp_cfg.get("DEFAULT","DirSrc"))
_temp_ = os.path.join(mcp_dir,os.path.normpath(mcp_cfg.get("OUTPUT","SrcClient")))
if os.path.isdir(os.path.join(mcp_src_dir,"common")):
mcp_src_dir = os.path.join(mcp_src_dir,"common")
mcp_cl_src_dir = _temp_
else:
mcp_src_dir = _temp_
mcp_bin_dir = os.path.join(mcp_dir,os.path.normpath(mcp_cfg.get("RECOMPILE","BinClient")))
mcp_reobf_dir = os.path.join(mcp_dir,os.path.normpath(mcp_cfg.get("REOBF","ReobfDirClient")))
mcp_lib_dir = os.path.join(mcp_dir,mcp_cfg.get("DEFAULT","DirLib"))
mod_v = pj_cfg.get("pj","version")
# Output File
if pj_cfg.has_option("pj","out"):
pj_out_f = os.path.join(pj_dir,pj_cfg.get("pj","out").replace("/",os.sep))
else:
pj_out_f = os.path.join(pj_dir,"build","libs",pname+"-"+mod_v+".jar")
srces = []
reses = []
apies = []
repes = {}
repes["@VERSION@"]=mod_v
repes["@MCVERSION@"]=forge_v.split("-")[0]
repes["@MC_VERSION@"]=forge_v.split("-")[0]
repes["@FORGEVERSION@"]=forge_v.split("-")[1]
repes["@FORGE_VERSION@"]=forge_v.split("-")[1]
repes["@FORGEBUILD@"]=forge_v.split(".")[-1]
repes["@FORGE_BUILD@"]=forge_v.split(".")[-1]
os.chdir(mcp_dir)
sys.path.append(mcp_dir)
from runtime.mcp import reobfuscate_side
from runtime.commands import Commands,CLIENT,SERVER
cmd = Commands()
for dir in pj_cfg.get("pj","src").replace("/",os.sep).split(":"):
ndir = os.path.abspath(os.path.join(pj_dir,os.path.dirname(dir)))
dir = os.path.abspath(os.path.join(pj_dir,dir))
cache = []
res_cache = []
for fpath, dnames, fnames in os.walk(dir):
for fname in fnames:
if fname.endswith(".java"):
cache.append(os.path.relpath(os.path.join(fpath,fname),ndir))
else:
res_cache.append(os.path.relpath(os.path.join(fpath,fname),ndir))
if len(cache) > 0:
tap = (ndir,cache)
srces.append(tap)
if len(res_cache) > 0:
tap = (ndir,res_cache)
reses.append(tap)
if pj_cfg.has_option("pj","res"):
for dir in pj_cfg.get("pj","res").replace("/",os.sep).split(":"):
ndir = os.path.abspath(os.path.join(pj_dir,os.path.dirname(dir)))
dir = os.path.abspath(os.path.join(pj_dir,dir))
cache = []
if os.path.isfile(dir):
cache.append(os.path.relpath(dir,ndir))
elif os.path.isdir(dir):
for fpath, dnames, fnames in os.walk(dir):
for fname in fnames:
cache.append(os.path.relpath(os.path.join(fpath,fname),ndir))
tap = (ndir,cache)
reses.append(tap)
if pj_cfg.has_option("pj","api"):
for name in pj_cfg.get("pj","api").replace("/",os.sep).split(":"):
apies.append(name)
if pj_cfg.has_option("pj","rep"):
for rep in pj_cfg.get("pj","rep").split("|"):
bef,aft=rep.split("=")
repes[bef] = aft
for repfrom,repto in repes.items():
pj_out_f = pj_out_f.replace(repfrom,repto)
pj_out_dir = os.path.dirname(pj_out_f)
if not os.path.isdir(pj_out_dir):
os.makedirs(pj_out_dir)
srg = True
if pj_cfg.has_option("pj","srg"):
srg = pj_cfg.getboolean("pj","srg")
cmd.logger.info("> Cleaning directories")###################################################################################
for path,dnames,fnames in os.walk(mcp_src_dir):
for fname in fnames:
os.remove(os.path.join(path,fname))
try:
for path,dnames,fnames in os.walk(mcp_cl_src_dir):
for fname in fnames:
os.remove(os.path.join(path,fname))
except NameError:
pass
dummyjava = open(os.path.join(mcp_src_dir,"dummy.java"),"wb")
dummyjava.write("class dummy{}")
dummyjava.close()
cmd.cleanbindirs(CLIENT)
cmd.logger.info("> Recompiling")############################################################################################
cmd.recompile(CLIENT)
cmd.logger.info("> Extracting forge binaries")##############################################################################
deobfjar = zipfile.ZipFile(os.path.join(mcp_lib_dir,"deobfMC.jar"),"r")
for file in deobfjar.namelist():
cache = os.path.join(mcp_bin_dir,file)
if not os.path.exists(cache):
if file.endswith("/"):
os.mkdir(cache)
else:
if file.endswith(".class"):
deobfjar.extract(file,mcp_bin_dir)
deobfjar.close()
if len(apies) > 0:##########################################################################################################
cmd.logger.info("> Extracting api binaries")
for api in apies:
zf = zipfile.ZipFile(os.path.join(mcp_lib_dir,api+".jar"),"r")
for file in zf.namelist():
cache = os.path.join(mcp_bin_dir,file)
if not os.path.exists(cache):
if file.endswith(".class"):
todir = os.path.dirname(os.path.join(mcp_bin_dir,file))
if not os.path.exists(todir):
os.makedirs(todir)
zf.extract(file,mcp_bin_dir)
zf.close()
cmd.logger.info("> Generating md5s")########################################################################################
cmd.gathermd5s(CLIENT)
cmd.logger.info("> Copying Mod sources")####################################################################################
for base, list in srces:
for src in list:
tofile = os.path.join(mcp_src_dir,src)
fromfile = os.path.join(base,src)
todir = os.path.dirname(tofile)
if not os.path.isdir(todir):
os.makedirs(todir)
if isbinary(fromfile):
shutil.copy2(fromfile,tofile)
else:
inputfile = open(fromfile,"rb")
filedata = inputfile.read()
inputfile.close()
for repfrom,repto in repes.items():
filedata = filedata.replace(repfrom,repto)
outputfile = open(tofile,"wb")
outputfile.write(filedata)
outputfile.close()
cmd.logger.info("> Recompiling")############################################################################################
cmd.recompile(CLIENT)
cmd.logger.info("> Creating Retroguard config files")#######################################################################
if srg:
try:
cmd.createreobfsrg()
cmd.creatergcfg(reobf=True, srg_names=True)
except AttributeError:
srg=False;
if not srg:
cmd.creatergcfg(reobf=True)
cmd.logger.info("> Reobfuscating")##########################################################################################
if srg:
reobfuscate_side(cmd,CLIENT,srg_names=True)
else:
reobfuscate_side(cmd,CLIENT)
cmd.logger.info("> Creating output ZipFile")################################################################################
cdir = pj_out_f+".c"
shutil.rmtree(cdir,True)
os.makedirs(cdir)
for base,dnames,fnames in os.walk(mcp_reobf_dir):
for fname in fnames:
p = os.path.join(base,fname)
zp = os.path.join(cdir,os.path.relpath(p,mcp_reobf_dir))
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
for base,list in reses:
for res in list:
p = os.path.join(base,res)
zp = os.path.join(cdir,res)
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
if not isbinary(p):
fobj = open(zp,"rb")
filedata = fobj.read()
fobj.close()
for repfrom,repto in repes.items():
filedata = filedata.replace(repfrom,repto)
fobj = open(zp,"wb")
fobj.write(filedata)
fobj.close()
call = ["jar"]
if pj_cfg.has_option("pj","man"):
inputfile = open(os.path.join(pj_dir, pj_cfg.get("pj","man").replace("/",os.sep)),"rb")
tofile = pj_out_f+".man"
filedata = inputfile.read()
inputfile.close()
for repfrom,repto in repes.items():
filedata = filedata.replace(repfrom,repto)
outputfile = open(tofile,"wb")
outputfile.write(filedata)
outputfile.close()
call.extend(["cmf",tofile])
else:
call.extend(["cf"])
call.extend([pj_out_f,"-C",cdir,"."])
subprocess.check_call(call)
shutil.rmtree(cdir)
if pj_cfg.has_option("pj","man"):
os.remove(tofile)
if pj_cfg.has_option("pj","capif"):
api_lib_f = pj_cfg.get("pj","capif")
cmd.logger.info("> Cleaning bin directory")#############################################################################
cmd.cleanbindirs(CLIENT)
cmd.logger.info("> Recompiling")########################################################################################
cmd.recompile(CLIENT)
cmd.logger.info("> Creating libraries")#################################################################################
cdir = os.path.join(mcp_lib_dir,api_lib_f)
shutil.rmtree(cdir,True)
os.makedirs(cdir)
for dpath,dnames,fnames in os.walk(mcp_bin_dir):
for fname in fnames:
p = os.path.join(dpath,fname)
if not p==os.path.abspath(os.path.join(mcp_bin_dir,"dummy.class")):
zp = os.path.join(cdir,os.path.relpath(p,mcp_bin_dir))
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
for base,list in reses:
for res in list:
p = os.path.join(base,res)
zp = os.path.join(cdir,res)
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
if not isbinary(p):
fobj = open(zp,"rb")
filedata = fobj.read()
fobj.close()
for repfrom,repto in repes.items():
filedata = filedata.replace(repfrom,repto)
fobj = open(zp,"wb")
fobj.write(filedata)
fobj.close()
subprocess.check_call(["jar","cf",cdir+".jar","-C",cdir,"."])
shutil.rmtree(cdir)
os.makedirs(cdir)
for dpath,dnames,fnames in os.walk(mcp_src_dir):
for fname in fnames:
p = os.path.join(dpath,fname)
if not p==os.path.abspath(os.path.join(mcp_src_dir,"dummy.java")):
zp = os.path.join(cdir,os.path.relpath(p,mcp_src_dir))
dzp = os.path.dirname(zp)
if not os.path.isdir(dzp):
os.makedirs(dzp)
shutil.copy2(p,zp)
subprocess.check_call(["jar","cf",cdir+"-src.jar","-C",cdir,"."])
shutil.rmtree(cdir)
cmd.logger.info("- All Done in %.2f seconds", time.time() - starttime)
logger = logging.getLogger()
while len(logger.handlers) > 0:
logger.removeHandler(logger.handlers[0])
def main(cur=None):
if cur == None:
cur = get_newest()
while True:
print "0. exit"
print "1. build project (an project chosen by user) *current minecraft version is ignored"
print "2. update eclipse project file (all projects)"
print "3. update eclipse project file (some projects chosen by user)"
print "4. update eclipse project file (an project chosen by user)"
print "5. install new minecraft forge"
print "6. change minecraft version"
print "current minecraft version : "+cur
input = raw_input(">")
if input=="0":
return
elif input=="1":
build(select_one())
elif input=="2":
i_select(cur,True)
elif input=="3":
i_select(cur)
elif input=="4":
i_eclipse(select_one(),cur)
elif input=="5":
cur = install()
elif input=="6":
while True:
for v in get_versions():
print v
input = raw_input("select new minecraft version: ")
for version in get_versions():
if version==input or version.split(".")[-1]==input:
cur = version
done = True
break
if done:
break
if __name__ == "__main__":
opts, args = getopt.getopt(sys.argv[1:], "b:", ["build="])
build_n = None
for o, a in opts:
if o in ("-b", "--build"):
build_n = a
if not build_n is None:
build(build_n)
else:
main()