This repository was archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.py
More file actions
220 lines (193 loc) · 6.89 KB
/
builder.py
File metadata and controls
220 lines (193 loc) · 6.89 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
import hashlib
from os import path, listdir, mkdir, walk
import zipfile
import json
import time
import shutil
import re
class ModPack:
mods = []
libraries = []
args = None
mcversion = None
name = None
configzip = None
tweaks = []
def format_for_mcupdate_plus(self):
entry = {"mcversion": self.mcversion}
if self.configzip:
entry["config"] = {"file": self.configzip, "version": int(time.time())}
if self.mods:
entry["mods"] = []
for mod in self.mods:
entry["mods"].append(mod.format_for_mcupdater_plus())
if self.libraries:
entry["libraries"] = []
for lib in self.libraries:
entry["libraries"].append(lib.format_for_mcupdater_plus())
if self.args:
entry["additionalArguments"] = self.args
if self.tweaks:
entry["tweakClasses"] = []
for tweak in self.tweaks:
entry["tweakClasses"].append(tweak)
return entry
class LibInfo:
group = None
name = None
version = None
url = None
classifier = None
def format_for_mcupdater_plus(self):
entry = {"group": self.group, "name": self.name, "version": self.version}
if self.url:
entry["url"] = self.url
if self.classifier:
entry["classifier"] = self.classifier
return entry
@staticmethod
def from_maven_line(mavenline):
args = mavenline.split(":",4)
lib = LibInfo()
lib.group = args[0]
lib.name = args[1]
lib.version = args[2]
if len(args) > 3:
if not args[3] == "":
lib.classifier = args[3]
if len(args) > 4:
lib.url = args[4]
return lib
class ModInfo:
forClient = False
forServer = False
sha1Sum = ""
md5Sum = ""
filename = ""
filepath = ""
name = None
modid = None
authors = None
version = None
revision = None
mcversion = None
type = None
def load_from_fml(self, filename):
print("Processing FML Mod: {}".format(filename))
if not path.exists(filename):
raise FileNotFoundError("The provided JAR did not exist for FML import.")
self.filename = path.basename(filename)
self.sha1Sum = sha1_of_file(filename)
self.md5Sum = md5_of_file(filename)
self.type = ModType.fml
if not zipfile.is_zipfile(filename):
raise RuntimeError("The file specified was not a valid archive!")
thearchive = zipfile.ZipFile(filename)
if not "mcmod.info" in thearchive.namelist():
print("The mod {} does not contain a mcmod.info...".format(path.basename(filename)))
self.name = path.basename(filename)
self.authors = "unknown"
self.version = "unknown"
self.modid = path.basename(filename)
else:
mcmodinfo = thearchive.open("mcmod.info")
mcmodjson = json.loads(mcmodinfo.read().decode())
if "modlist" in mcmodjson:
mcmodjson = mcmodjson["modlist"]
if type(mcmodjson) == list:
mcmodjson = mcmodjson[0]
self.name = mcmodjson["name"]
self.version = mcmodjson["version"] if "version" in mcmodjson else "unknown"
self.modid = mcmodjson["modid"]
self.authors = str(mcmodjson["authors"]) if "authors" in mcmodjson else "unknown"
thearchive.close()
del thearchive
def format_for_mcupdater_plus(self):
entry = {}
if self.modid:
entry["modid"] = self.modid
if self.version:
entry["version"] = self.version
if self.revision:
entry["revision"] = self.revision
entry["file"] = '{}/{}'.format(self.filepath, self.filename) if self.filepath else self.filename
if self.type == ModType.classpath:
entry["type"] = "jar"
elif self.type == ModType.fml:
entry["type"] = "forge"
elif self.type == ModType.litemod:
entry["type"] = "liteloader"
entry["md5"] = self.md5Sum
if self.forClient and not self.forServer:
entry["side"] = "client"
elif self.forServer and not self.forClient:
entry["side"] = "server"
return entry
class ModType:
classpath = 1
fml = 2
litemod = 3
def sha1_of_file(filename):
file = open(filename, 'rb')
sha = hashlib.sha1()
while True:
chunk = file.read(2**10)
if not chunk:
break
sha.update(chunk)
file.close()
del file
return sha.hexdigest()
def md5_of_file(filename):
file = open(filename, 'rb')
md = hashlib.md5()
while True:
chunk = file.read(2**10)
if not chunk:
break
md.update(chunk)
file.close()
del file
return md.hexdigest()
def main():
print("Jamcraft MCUpdater Pack Builder")
if path.exists("output"):
shutil.rmtree("output")
mkdir("output")
mkdir(path.join("output", "modpack"))
mkdir(path.join(path.join("output", "modpack"), "latest"))
modpack = ModPack()
if path.exists(path.join("input", "mods-fml")):
for file in listdir(path.join("input", "mods-fml")):
if re.match(".*\\.(?:jar|zip)", file, re.I):
mod = ModInfo()
mod.load_from_fml(path.join(path.join("input", "mods-fml"), file))
modpack.mods.append(mod)
modpack.mcversion = "1.6.4"
if path.exists(path.join("input","libraries.txt")):
libfile = open(path.join("input","libraries.txt"))
for libline in libfile:
print("Addling library: {}".format(libline.strip()))
modpack.libraries.append(LibInfo.from_maven_line(libline.strip()))
libfile.close()
del libfile
if path.exists(path.join("input","tweaks.txt")):
tweaksfile = open(path.join("input","tweaks.txt"))
for tweaksline in tweaksfile:
print("Activating Tweak: {}".format(tweaksline.strip()))
modpack.tweaks.append(tweaksline.strip())
tweaksfile.close()
del tweaksfile
if path.exists(path.join("input","config")):
modpack.configzip = "config.zip"
configzip = zipfile.ZipFile(path.join("output", "modpack", "latest", "config.zip"), "w")
for dirpath, dnames, fnames in walk(path.join("input", "config")):
for fname in fnames:
print("Adding Config File: {}".format(path.relpath(path.join(dirpath, fname), "input")))
configzip.write(path.join(dirpath, fname), path.relpath(path.join(dirpath, fname), "input"))
configzip.close()
del configzip
packjson = open(path.join(path.join("output", "modpack"), path.join("latest", "pack.json")), "w")
json.dump(modpack.format_for_mcupdate_plus(), packjson, sort_keys=True, indent=4)
packjson.close()
main()