-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
updateForge.py
executable file
·359 lines (311 loc) · 14.1 KB
/
updateForge.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
#!/usr/bin/python3
'''
Get the source files necessary for generating Forge versions
'''
from __future__ import print_function
import sys
import requests
from cachecontrol import CacheControl
from cachecontrol.caches import FileCache
import json
import copy
import re
import zipfile
from metautil import *
from jsonobject import *
from forgeutil import *
import os.path
import datetime
import hashlib
from pathlib import Path
from contextlib import suppress
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def filehash(filename, hashtype, blocksize=65536):
hash = hashtype()
with open(filename, "rb") as f:
for block in iter(lambda: f.read(blocksize), b""):
hash.update(block)
return hash.hexdigest()
forever_cache = FileCache('http_cache', forever=True)
sess = CacheControl(requests.Session(), forever_cache)
# get the remote version list fragments
r = sess.get('https://files.minecraftforge.net/net/minecraftforge/forge/maven-metadata.json')
r.raise_for_status()
main_json = r.json()
assert type(main_json) == dict
r = sess.get('https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json')
r.raise_for_status()
promotions_json = r.json()
assert type(promotions_json) == dict
promotedKeyExpression = re.compile("(?P<mc>[^-]+)-(?P<promotion>(latest)|(recommended))(-(?P<branch>[a-zA-Z0-9\\.]+))?")
recommendedSet = set()
newIndex = DerivedForgeIndex()
# FIXME: does not fully validate that the file has not changed format
# NOTE: For some insane reason, the format of the versions here is special. It having a branch at the end means it affects that particular branch
# We don't care about Forge having branches.
# Therefore we only use the short version part for later identification and filter out the branch-specific promotions (among other errors).
print("Processing promotions:")
for promoKey, shortversion in promotions_json.get('promos').items():
match = promotedKeyExpression.match(promoKey)
if not match:
print('Skipping promotion %s, the key did not parse:' % promoKey)
pprint(promoKey)
assert match
if not match.group('mc'):
print('Skipping promotion %s, because it has no Minecraft version.' % promoKey)
continue
if match.group('branch'):
print('Skipping promotion %s, because it on a branch only.' % promoKey)
continue
elif match.group('promotion') == 'recommended':
recommendedSet.add(shortversion)
print ('%s added to recommended set' % shortversion)
elif match.group('promotion') == 'latest':
pass
else:
assert False
versionExpression = re.compile("^(?P<mc>[0-9a-zA-Z_\\.]+)-(?P<ver>[0-9\\.]+\\.(?P<build>[0-9]+))(-(?P<branch>[a-zA-Z0-9\\.]+))?$")
def getSingleForgeFilesManifest(longversion):
pathThing = "upstream/forge/files_manifests/%s.json" % longversion
files_manifest_file = Path(pathThing)
from_file = False
if files_manifest_file.is_file():
with open(pathThing, 'r') as f:
files_json=json.load(f)
from_file = True
else:
fileUrl = 'https://files.minecraftforge.net/net/minecraftforge/forge/%s/meta.json' % longversion
r = sess.get(fileUrl)
r.raise_for_status()
files_json = r.json()
retDict = dict()
for classifier, extensionObj in files_json.get('classifiers').items():
assert type(classifier) == str
assert type(extensionObj) == dict
# assert len(extensionObj.items()) == 1
index = 0
count = 0
while index < len(extensionObj.items()):
mutableCopy = copy.deepcopy(extensionObj)
extension, hash = mutableCopy.popitem()
if not type(classifier) == str:
pprint(classifier)
pprint(extensionObj)
if not type(hash) == str:
pprint(classifier)
pprint(extensionObj)
print('%s: Skipping missing hash for extension %s:' % (longversion, extension))
index = index + 1
continue
assert type(classifier) == str
processedHash = re.sub(r"\W", "", hash)
if not len(processedHash) == 32:
print('%s: Skipping invalid hash for extension %s:' % (longversion, extension))
pprint(extensionObj)
index = index + 1
continue
fileObj = ForgeFile(
classifier=classifier,
hash=processedHash,
extension=extension
)
if count == 0:
retDict[classifier] = fileObj
index = index + 1
count = count + 1
else:
print('%s: Multiple objects detected for classifier %s:' % (longversion, classifier))
pprint(extensionObj)
assert False
if not from_file:
with open(pathThing, 'w', encoding='utf-8') as f:
json.dump(files_json, f, sort_keys=True, indent=4)
return retDict
print("")
print("Making dirs...")
os.makedirs("upstream/forge/jars/", exist_ok=True)
os.makedirs("upstream/forge/installer_info/", exist_ok=True)
os.makedirs("upstream/forge/installer_manifests/", exist_ok=True)
os.makedirs("upstream/forge/version_manifests/", exist_ok=True)
os.makedirs("upstream/forge/files_manifests/", exist_ok=True)
print("")
print("Processing versions:")
for mcversion, value in main_json.items():
assert type(mcversion) == str
assert type(value) == list
for longversion in value:
assert type(longversion) == str
match = versionExpression.match(longversion)
if not match:
pprint(longversion)
assert match
assert match.group('mc') == mcversion
files = getSingleForgeFilesManifest(longversion)
build = int(match.group('build'))
version = match.group('ver')
branch = match.group('branch')
isRecommended = (version in recommendedSet)
entry = ForgeEntry(
longversion=longversion,
mcversion=mcversion,
version=version,
build=build,
branch=branch,
# NOTE: we add this later after the fact. The forge promotions file lies about these.
latest=False,
recommended=isRecommended,
files=files
)
newIndex.versions[longversion] = entry
if not newIndex.by_mcversion:
newIndex.by_mcversion = dict()
if not mcversion in newIndex.by_mcversion:
newIndex.by_mcversion.setdefault(mcversion, ForgeMcVersionInfo())
newIndex.by_mcversion[mcversion].versions.append(longversion)
# NOTE: we add this later after the fact. The forge promotions file lies about these.
#if entry.latest:
#newIndex.by_mcversion[mcversion].latest = longversion
if entry.recommended:
newIndex.by_mcversion[mcversion].recommended = longversion
print("")
print("Post processing promotions and adding missing 'latest':")
for mcversion, info in newIndex.by_mcversion.items():
latestVersion = info.versions[-1]
info.latest = latestVersion
newIndex.versions[latestVersion].latest = True
print("Added %s as latest for %s" % (latestVersion, mcversion))
print("")
print("Dumping index files...")
with open("upstream/forge/maven-metadata.json", 'w', encoding='utf-8') as f:
json.dump(main_json, f, sort_keys=True, indent=4)
with open("upstream/forge/promotions_slim.json", 'w', encoding='utf-8') as f:
json.dump(promotions_json, f, sort_keys=True, indent=4)
with open("upstream/forge/derived_index.json", 'w', encoding='utf-8') as f:
json.dump(newIndex.to_json(), f, sort_keys=True, indent=4)
versions = []
legacyinfolist = ForgeLegacyInfoList()
tsPath = "static/forge-legacyinfo.json"
fuckedVersions = []
print("Grabbing installers and dumping installer profiles...")
# get the installer jars - if needed - and get the installer profiles out of them
for id, entry in newIndex.versions.items():
eprint ("Updating Forge %s" % id)
if entry.mcversion == None:
eprint ("Skipping %d with invalid MC version" % entry.build)
continue
version = ForgeVersion(entry)
if version.url() == None:
eprint ("Skipping %d with no valid files" % version.build)
continue
jarFilepath = "upstream/forge/jars/%s" % version.filename()
if version.usesInstaller():
installerInfoFilepath = "upstream/forge/installer_info/%s.json" % version.longVersion
profileFilepath = "upstream/forge/installer_manifests/%s.json" % version.longVersion
versionJsonFilepath = "upstream/forge/version_manifests/%s.json" % version.longVersion
installerRefreshRequired = False
if not os.path.isfile(profileFilepath):
installerRefreshRequired = True
if not os.path.isfile(installerInfoFilepath):
installerRefreshRequired = True
if installerRefreshRequired:
# grab the installer if it's not there
if not os.path.isfile(jarFilepath):
eprint ("Downloading %s" % version.url())
rfile = sess.get(version.url(), stream=True)
rfile.raise_for_status()
with open(jarFilepath, 'wb') as f:
for chunk in rfile.iter_content(chunk_size=128):
f.write(chunk)
eprint ("Processing %s" % version.url())
# harvestables from the installer
if not os.path.isfile(profileFilepath):
print(jarFilepath)
with zipfile.ZipFile(jarFilepath, 'r') as jar:
with suppress(KeyError):
with jar.open('version.json', 'r') as profileZipEntry:
versionJsonData = profileZipEntry.read();
versionJsonJson = json.loads(versionJsonData)
profileZipEntry.close()
# Process: does it parse?
doesItParse = MojangVersionFile(versionJsonJson)
with open(versionJsonFilepath, 'wb') as versionJsonFile:
versionJsonFile.write(versionJsonData)
versionJsonFile.close()
with jar.open('install_profile.json', 'r') as profileZipEntry:
installProfileJsonData = profileZipEntry.read()
profileZipEntry.close()
# Process: does it parse?
installProfileJsonJson = json.loads(installProfileJsonData)
atLeastOneFormatWorked = False
exception = None
try:
doesItParseV1 = ForgeInstallerProfile(installProfileJsonJson)
atLeastOneFormatWorked = True
except BaseException as err:
exception = err
try:
doesItParseV2 = ForgeInstallerProfileV2(installProfileJsonJson)
atLeastOneFormatWorked = True
except BaseException as err:
exception = err
# NOTE: Only here for 1.12.2-14.23.5.2851
try:
doesItParseV1_5 = ForgeInstallerProfileV1_5(installProfileJsonJson)
atLeastOneFormatWorked = True
except BaseException as err:
exception = err
if not atLeastOneFormatWorked:
if version.isSupported():
raise exception
else:
eprint ("Version %s is not supported and won't be generated later." % version.longVersion)
with open(profileFilepath, 'wb') as profileFile:
profileFile.write(installProfileJsonData)
profileFile.close()
# installer info v1
if not os.path.isfile(installerInfoFilepath):
installerInfo = InstallerInfo()
eprint ("SHA1 %s" % jarFilepath)
installerInfo.sha1hash = filehash(jarFilepath, hashlib.sha1)
eprint ("SHA256 %s" % jarFilepath)
installerInfo.sha256hash = filehash(jarFilepath, hashlib.sha256)
eprint ("SIZE %s" % jarFilepath)
installerInfo.size = os.path.getsize(jarFilepath)
eprint ("DUMP %s" % jarFilepath)
with open(installerInfoFilepath, 'w', encoding='utf-8') as installerInfoFile:
json.dump(installerInfo.to_json(), installerInfoFile, sort_keys=True, indent=4)
installerInfoFile.close()
else:
pass
# ignore the two versions without install manifests and jar mod class files
# TODO: fix those versions?
if version.mcversion_sane == "1.6.1":
continue
# only gather legacy info if it's missing
if not os.path.isfile(tsPath):
# grab the jar/zip if it's not there
if not os.path.isfile(jarFilepath):
rfile = sess.get(version.url(), stream=True)
rfile.raise_for_status()
with open(jarFilepath, 'wb') as f:
for chunk in rfile.iter_content(chunk_size=128):
f.write(chunk)
# find the latest timestamp in the zip file
tstamp = datetime.datetime.fromtimestamp(0)
with zipfile.ZipFile(jarFilepath, 'r') as jar:
allinfo = jar.infolist()
for info in allinfo:
tstampNew = datetime.datetime(*info.date_time)
if tstampNew > tstamp:
tstamp = tstampNew
legacyInfo = ForgeLegacyInfo()
legacyInfo.releaseTime = tstamp
legacyInfo.sha1 = filehash(jarFilepath, hashlib.sha1)
legacyInfo.sha256 = filehash(jarFilepath, hashlib.sha256)
legacyInfo.size = os.path.getsize(jarFilepath)
legacyinfolist.number[id] = legacyInfo
# only write legacy info if it's missing
if not os.path.isfile(tsPath):
with open(tsPath, 'w') as outfile:
json.dump(legacyinfolist.to_json(), outfile, sort_keys=True, indent=4)