summaryrefslogtreecommitdiff
path: root/updateNeoForge.py
blob: 1db5b43a053782eace3082463cc40059bab22a12 (plain)
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
"""
 Get the source files necessary for generating Forge versions
"""
import copy
import hashlib
import json
import os
import re
import sys
import zipfile
from contextlib import suppress
from datetime import datetime
from pathlib import Path
from pprint import pprint
import urllib.parse

from pydantic import ValidationError

from meta.common import upstream_path, ensure_upstream_dir, static_path, default_session
from meta.common.neoforge import (
    JARS_DIR,
    INSTALLER_INFO_DIR,
    INSTALLER_MANIFEST_DIR,
    VERSION_MANIFEST_DIR,
    FILE_MANIFEST_DIR,
)
from meta.model.neoforge import (
    NeoForgeFile,
    NeoForgeEntry,
    NeoForgeMCVersionInfo,
    DerivedNeoForgeIndex,
    NeoForgeVersion,
    NeoForgeInstallerProfileV2,
    InstallerInfo,
)
from meta.model.mojang import MojangVersion

UPSTREAM_DIR = upstream_path()
STATIC_DIR = static_path()

ensure_upstream_dir(JARS_DIR)
ensure_upstream_dir(INSTALLER_INFO_DIR)
ensure_upstream_dir(INSTALLER_MANIFEST_DIR)
ensure_upstream_dir(VERSION_MANIFEST_DIR)
ensure_upstream_dir(FILE_MANIFEST_DIR)

sess = default_session()


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


def filehash(filename, hashtype, blocksize=65536):
    hashtype = hashtype()
    with open(filename, "rb") as f:
        for block in iter(lambda: f.read(blocksize), b""):
            hashtype.update(block)
    return hashtype.hexdigest()


def find_nth(haystack, needle, n):
    start = haystack.find(needle)
    while start >= 0 and n > 1:
        start = haystack.find(needle, start + len(needle))
        n -= 1
    return start


def get_single_forge_files_manifest(longversion, artifact: str):
    print(f"Getting NeoForge manifest for {longversion}")
    path_thing = UPSTREAM_DIR + "/neoforge/files_manifests/%s.json" % longversion
    files_manifest_file = Path(path_thing)
    from_file = False
    if files_manifest_file.is_file():
        with open(path_thing, "r") as f:
            files_json = json.load(f)
            from_file = True
    else:
        file_url = (
            f"https://maven.neoforged.net/api/maven/details/releases/net%2Fneoforged%2F{artifact}%2F"
            + urllib.parse.quote(longversion)
        )
        r = sess.get(file_url)
        r.raise_for_status()
        files_json = r.json()

    ret_dict = dict()

    for file in files_json.get("files"):
        assert type(file) == dict
        name = file["name"]
        prefix = f"{artifact}-{longversion}"
        assert name.startswith(
            prefix
        ), f"{longversion} classifier {name} doesn't start with {prefix}"
        file_name = name[len(prefix) :]
        if file_name.startswith("-"):
            file_name = file_name[1:]
        if file_name.startswith("."):
            continue

        classifier, ext = os.path.splitext(file_name)

        if ext in [".md5", ".sha1", ".sha256", ".sha512"]:
            continue

        # assert len(extensionObj.items()) == 1
        file_obj = NeoForgeFile(
            artifact=artifact, classifier=classifier, extension=ext[1:]
        )
        ret_dict[classifier] = file_obj

    if not from_file:
        Path(path_thing).parent.mkdir(parents=True, exist_ok=True)
        with open(path_thing, "w", encoding="utf-8") as f:
            json.dump(files_json, f, sort_keys=True, indent=4)

    return ret_dict


def main():
    # get the remote version list fragments
    r = sess.get(
        "https://maven.neoforged.net/api/maven/versions/releases/net%2Fneoforged%2Fforge"
    )
    r.raise_for_status()
    main_json = r.json()["versions"]
    assert type(main_json) == list

    # get the new remote version list fragments
    r = sess.get(
        "https://maven.neoforged.net/api/maven/versions/releases/net%2Fneoforged%2Fneoforge"
    )
    r.raise_for_status()
    new_main_json = r.json()["versions"]
    assert type(new_main_json) == list

    main_json += new_main_json

    new_index = DerivedNeoForgeIndex()

    version_expression = re.compile(
        r"^(?P<mc>[0-9a-zA-Z_\.]+)-(?P<ver>[0-9\.]+\.(?P<build>[0-9]+))(-(?P<branch>[a-zA-Z0-9\.]+))?$"
    )
    neoforge_version_re = re.compile(
        r"^(?P<mcminor>\d+).(?P<mcpatch>\d+).(?P<number>\d+)(?:-(?P<tag>\w+))?$"
    )

    print("")
    print("Processing versions:")
    for long_version in main_json:
        assert type(long_version) == str

        match = version_expression.match(long_version)
        if match:
            mc_version = match.group("mc")
            build = int(match.group("build"))
            version = match.group("ver")
            branch = match.group("branch")
            artifact = "forge"

        match_nf = neoforge_version_re.match(long_version)
        if match_nf:
            mc_version = f"1.{match_nf.group('mcminor')}.{match_nf.group('mcpatch')}"
            build = int(match_nf.group("number"))
            version = match_nf.group("number")
            branch = match_nf.group("tag")
            match = match_nf
            artifact = "neoforge"

        assert match, f"{long_version} doesn't match version regex"
        try:
            files = get_single_forge_files_manifest(long_version, artifact)
        except:
            continue

        # TODO: what *is* recommended?
        is_recommended = False

        entry = NeoForgeEntry(
            artifact=artifact,
            long_version=long_version,
            mc_version=mc_version,
            version=version,
            build=build,
            branch=branch,
            # NOTE: we add this later after the fact. The forge promotions file lies about these.
            latest=False,
            recommended=is_recommended,
            files=files,
        )
        new_index.versions[long_version] = entry
        if not new_index.by_mc_version:
            new_index.by_mc_version = dict()
        if mc_version not in new_index.by_mc_version:
            new_index.by_mc_version.setdefault(mc_version, NeoForgeMCVersionInfo())
        new_index.by_mc_version[mc_version].versions.append(long_version)
        # NOTE: we add this later after the fact. The forge promotions file lies about these.
        # if entry.latest:
        # new_index.by_mc_version[mc_version].latest = long_version
        if entry.recommended:
            new_index.by_mc_version[mc_version].recommended = long_version

    print("")
    print("Dumping index files...")

    with open(
        UPSTREAM_DIR + "/neoforge/maven-metadata.json", "w", encoding="utf-8"
    ) as f:
        json.dump(main_json, f, sort_keys=True, indent=4)

    new_index.write(UPSTREAM_DIR + "/neoforge/derived_index.json")

    print("Grabbing installers and dumping installer profiles...")
    # get the installer jars - if needed - and get the installer profiles out of them
    for key, entry in new_index.versions.items():
        eprint("Updating NeoForge %s" % key)
        if entry.mc_version is None:
            eprint("Skipping %d with invalid MC version" % entry.build)
            continue

        version = NeoForgeVersion(entry)
        if version.url() is None:
            eprint("Skipping %d with no valid files" % version.build)
            continue
        if not version.uses_installer():
            eprint(f"version {version.long_version} does not use installer")
            continue

        jar_path = os.path.join(UPSTREAM_DIR, JARS_DIR, version.filename())

        installer_info_path = (
            UPSTREAM_DIR + "/neoforge/installer_info/%s.json" % version.long_version
        )
        profile_path = (
            UPSTREAM_DIR
            + "/neoforge/installer_manifests/%s.json" % version.long_version
        )
        version_file_path = (
            UPSTREAM_DIR + "/neoforge/version_manifests/%s.json" % version.long_version
        )

        installer_refresh_required = not os.path.isfile(
            profile_path
        ) or not os.path.isfile(installer_info_path)

        if installer_refresh_required:
            # grab the installer if it's not there
            if not os.path.isfile(jar_path):
                eprint("Downloading %s" % version.url())
                try:
                    rfile = sess.get(version.url(), stream=True)
                    rfile.raise_for_status()
                    Path(jar_path).parent.mkdir(parents=True, exist_ok=True)
                    with open(jar_path, "wb") as f:
                        for chunk in rfile.iter_content(chunk_size=128):
                            f.write(chunk)
                except Exception as e:
                    eprint("Failed to download %s" % version.url())
                    eprint("Error is %s" % e)
                    continue

        eprint("Processing %s" % version.url())
        # harvestables from the installer
        if not os.path.isfile(profile_path):
            print(jar_path)
            with zipfile.ZipFile(jar_path) as jar:
                with suppress(KeyError):
                    with jar.open("version.json") as profile_zip_entry:
                        version_data = profile_zip_entry.read()

                        # Process: does it parse?
                        MojangVersion.parse_raw(version_data)

                        Path(version_file_path).parent.mkdir(
                            parents=True, exist_ok=True
                        )
                        with open(version_file_path, "wb") as versionJsonFile:
                            versionJsonFile.write(version_data)
                            versionJsonFile.close()

                with jar.open("install_profile.json") as profile_zip_entry:
                    install_profile_data = profile_zip_entry.read()

                    # Process: does it parse?
                    is_parsable = False
                    exception = None
                    try:
                        NeoForgeInstallerProfileV2.parse_raw(install_profile_data)
                        is_parsable = True
                    except ValidationError as err:
                        exception = err

                    if not is_parsable:
                        if version.is_supported():
                            raise exception
                        else:
                            eprint(
                                "Version %s is not supported and won't be generated later."
                                % version.long_version
                            )

                    Path(profile_path).parent.mkdir(parents=True, exist_ok=True)
                    with open(profile_path, "wb") as profileFile:
                        profileFile.write(install_profile_data)
                        profileFile.close()

        # installer info v1
        if not os.path.isfile(installer_info_path):
            installer_info = InstallerInfo()
            installer_info.sha1hash = filehash(jar_path, hashlib.sha1)
            installer_info.sha256hash = filehash(jar_path, hashlib.sha256)
            installer_info.size = os.path.getsize(jar_path)
            installer_info.write(installer_info_path)


if __name__ == "__main__":
    main()