fork/nix-minecraft
git clone https://git.julia.blue/fork/nix-minecraft
cc15499
lazymc
1#!/usr/bin/env nix-shell 2#!nix-shell -i python3 -p python3Packages.packaging python3Packages.requests python3Packages.requests-cache 3 4import argparse 5import base64 6import concurrent .futures 7import json 8import re 9import subprocess 10import zipfile 11from collections import defaultdict 12from pathlib import Path 13from typing import Any ,Dict ,List ,NotRequired ,TypedDict 14 15import requests 16import requests_cache 17from packaging .version import InvalidVersion ,Version 18from requests .adapters import HTTPAdapter ,Retry 19 20# Versions before 20.5 do not always support the "fat jar" feature. As such, 21# they always try to download server mappings, and there's no way to bypass it. 22MIN_SUPPORTED_VERSION = Version ("20.4.240" ) 23 24MINECRAFT_MANIFEST = "https://launchermeta.mojang.com/mc/game/version_manifest_v2.json" 25NEOFORGE_API = ( 26"https://maven.neoforged.net/api/maven/versions/releases/net/neoforged/neoforge" 27) 28NEOFORGE_MAVEN = "https://maven.neoforged.net/releases/net/neoforged/neoforge" 29 30TIMEOUT = 5 31RETRIES = 5 32THREADS = 8 33 34 35class FetchUrl (TypedDict ): 36name :NotRequired [str ] 37url :str 38hash :str 39 40 41class GameVersionLock (TypedDict ): 42mappings :FetchUrl 43 44 45class LoaderLock (TypedDict ): 46src :FetchUrl 47libraries :List [str ] 48 49 50# game version -> build version -> version details 51LoaderLocks = Dict [str ,Dict [str ,LoaderLock ]] 52 53 54class TimeoutHTTPAdapter (HTTPAdapter ): 55def __init__ (self ,* args ,** kwargs ): 56self .timeout = TIMEOUT 57if "timeout" in kwargs : 58self .timeout = kwargs ["timeout" ] 59del kwargs ["timeout" ] 60super ().__init__ (* args ,** kwargs ) 61 62def send (self ,request ,** kwargs ): 63timeout = kwargs .get ("timeout" ) 64if timeout is None : 65kwargs ["timeout" ]= self .timeout 66return super ().send (request ,** kwargs ) 67 68 69def make_client ()-> requests_cache .CachedSession : 70# configure a cache for faster local development 71# mostly for ".sha256" files that should never change 72client = requests_cache .CachedSession (backend = "filesystem" ,cache_control = True ) 73retries = Retry ( 74total = RETRIES ,backoff_factor = 1 ,status_forcelist = [429 ,500 ,502 ,503 ,504 ] 75 ) 76client .mount ("https://" ,TimeoutHTTPAdapter (max_retries = retries )) 77return client 78 79 80def sri_hash (alg :str ,hex :str ): 81return f" { alg } - { base64 . b64encode ( bytes . fromhex ( hex )). decode ( 'utf-8' ) } " 82 83 84def fetch_game_versions (client :requests_cache .CachedSession )-> Dict [str ,str ]: 85"Fetching game versions" ) 86response = client .get (MINECRAFT_MANIFEST ,expire_after = requests_cache .DO_NOT_CACHE ) 87response .raise_for_status () 88data = response .json () 89return {v ["id" ]:v ["url" ]for v in data ["versions" ]} 90 91 92def fetch_mappings_hash (client :requests_cache .CachedSession ,url :str ): 93f"Fetching manifest: { url } " ) 94response = client .get (url ) 95response .raise_for_status () 96data = response .json () 97server_mappings = data ["downloads" ]["server_mappings" ] 98return GameVersionLock ( 99mappings = FetchUrl ( 100name = f" { data [ 'id' ] } -server-mappings.txt" , 101url = str (server_mappings ["url" ]), 102hash = sri_hash ("sha1" ,server_mappings ["sha1" ]), 103 ), 104 ) 105 106 107def fetch_installer_hash (client :requests .Session ,version :str ): 108url = f" { NEOFORGE_MAVEN } / { version } /neoforge- { version } -installer.jar" 109hash_url = f" { url } .sha256" 110response = client .get (hash_url ) 111response .raise_for_status () 112return FetchUrl ( 113url = url , 114hash = sri_hash ("sha256" ,response .text ), 115 ) 116 117 118def fetch_library_hashes (src :FetchUrl )-> Dict [str ,FetchUrl ]: 119# the installer jar is used by the build derivation, so we might as well 120# use nix to fetch/cache it ahead of time 121proc = subprocess .run ( 122 ["nix-prefetch-url" ,src ["url" ],src ["hash" ],"--print-path" ], 123check = True , 124encoding = "UTF-8" , 125stdout = subprocess .PIPE , 126stderr = subprocess .STDOUT , 127 ) 128store_path = proc .stdout .splitlines ()[1 ] 129 130def library_src (library :Dict [str ,Any ]): 131artifact = library ["downloads" ]["artifact" ] 132return FetchUrl ( 133url = artifact ["url" ], 134hash = sri_hash ("sha1" ,artifact ["sha1" ]), 135 ) 136 137with zipfile .ZipFile (store_path ,"r" )as zf : 138with zf .open ("install_profile.json" ,"r" )as f : 139profile_data = json .load (f ) 140libraries = profile_data ["libraries" ] 141 142with zf .open ("version.json" ,"r" )as f : 143version_data = json .load (f ) 144libraries += version_data ["libraries" ] 145 146return {str (lib ["name" ]):library_src (lib )for lib in libraries } 147 148 149def fetch_loader_versions ( 150client :requests_cache .CachedSession , 151)-> Dict [str ,List [str ]]:# game version -> build versions 152"Fetching installer versions" ) 153response = client .get (NEOFORGE_API ,expire_after = requests_cache .DO_NOT_CACHE ) 154response .raise_for_status () 155 156versions = defaultdict (list ) 157data = response .json () 158for version in data ["versions" ]: 159try : 160version = Version (version ) 161except InvalidVersion : 162f"Skipping unparseable version: { version } " ) 163continue 164if version .is_prerelease : 165f"Skipping pre-release version: { version } " ) 166continue 167if MIN_SUPPORTED_VERSION > version : 168f"Skipping unsupported version: { version } " ) 169continue 170 171# first two digits == 1.x game version 172versions [f"1. { version . major } . { version . minor } " .removesuffix (".0" )].append ( 173str (version ) 174 ) 175return versions 176 177 178def main ( 179loader_versions :LoaderLocks , 180game_versions :Dict [str ,GameVersionLock ], 181library_versions :Dict [str ,FetchUrl ], 182version_regex , 183client , 184): 185"Starting fetch" ) 186 187game_manifest = fetch_game_versions (client ) 188loader_manifest = fetch_loader_versions (client ) 189 190to_fetch = [] 191 192for game_version ,build_versions in loader_manifest .items (): 193if game_version not in game_versions : 194game_versions [game_version ]= fetch_mappings_hash ( 195client ,game_manifest [game_version ] 196 ) 197 198for build_version in build_versions : 199if re .match (version_regex ,build_version )is None : 200f"Skip fetching { build_version } : does not match --version" ) 201continue 202if ( 203game_version not in loader_versions 204or build_version not in loader_versions [game_version ] 205 ): 206to_fetch .append ((game_version ,build_version )) 207 208f"Fetching { len ( to_fetch ) } loader versions..." ) 209 210def fetch_build (versions :tuple [str ,str ]): 211game_version ,version = versions 212f"Fetching { version } " ) 213installer = fetch_installer_hash (client ,version ) 214return game_version ,version ,installer ,fetch_library_hashes (installer ) 215 216try : 217with concurrent .futures .ThreadPoolExecutor (max_workers = THREADS )as p : 218for game_version ,version ,src ,library_srcs in p .map ( 219fetch_build ,to_fetch 220 ): 221if game_version not in loader_versions : 222loader_versions [game_version ]= {} 223loader_versions [game_version ][version ]= LoaderLock ( 224libraries = sorted (library_srcs .keys ()), 225src = src , 226 ) 227library_versions |= library_srcs 228except KeyboardInterrupt : 229"Cancelled fetching. Writing and exiting" ) 230 231return (loader_versions ,game_versions ,library_versions ) 232 233 234if __name__ == "__main__" : 235parser = argparse .ArgumentParser () 236parser .add_argument ("--version" ,type = str ,default = r".*" ,required = False ) 237args = parser .parse_args () 238 239folder = Path (__file__ ).parent 240loader_path = folder / "loader_locks.json" 241game_path = folder / "game_locks.json" 242library_path = folder / "library_locks.json" 243with ( 244open (loader_path ,"r" )as loader_locks , 245open (game_path ,"r" )as game_locks , 246open (library_path ,"r" )as library_locks , 247 ): 248loader_versions = ( 249 {}if loader_path .stat ().st_size == 0 else json .load (loader_locks ) 250 ) 251game_versions = {}if game_path .stat ().st_size == 0 else json .load (game_locks ) 252library_versions = ( 253 {}if library_path .stat ().st_size == 0 else json .load (library_locks ) 254 ) 255 256 (loader_versions ,game_versions ,library_versions )= main ( 257loader_versions , 258game_versions , 259library_versions , 260args .version , 261make_client (), 262 ) 263 264with ( 265open (loader_path ,"w" )as loader_locks , 266open (game_path ,"w" )as game_locks , 267open (library_path ,"w" )as library_locks , 268 ): 269json .dump ( 270loader_versions , 271loader_locks , 272indent = 2 , 273sort_keys = True , 274 ) 275json .dump (game_versions ,game_locks ,indent = 2 ,sort_keys = True ) 276json .dump ( 277library_versions , 278library_locks , 279indent = 2 , 280sort_keys = True , 281 )