fork/nix-minecraft
git clone https://git.julia.blue/fork/nix-minecraft
64a768c
feat/abduco
1#!/usr/bin/env nix-shell 2#!nix-shell -i python3.10 -p python310Packages.requests 3 4import json 5import subprocess 6import requests 7import re 8from pathlib import Path 9 10 11def versiontuple (v ): 12return tuple (map (int , (v .partition ("-" )[0 ].split ("." )))) 13 14 15ENDPOINT = "https://meta.quiltmc.org/v3/versions" 16 17# These filters specify which Quilt loader and Minecraft game versions to package. 18 19# Only package Quilt versions greater than 0.17.0 (using QuiltServerLauncher main class) 20LOADER_VERSION_FILTER = lambda version : ( 21version ["separator" ]== "." and versiontuple (version ["version" ])>= (0 ,17 ,0 ) 22) 23 24SNAPSHOT_REGEX = re .compile ("([0-9]{2})w([0-9]{1,2})[a-z]+" ) 25 26# Package all game versions supported by Quilt 27def GAME_VERSION_FILTER (version ): 28snapshotmatch = re .fullmatch (SNAPSHOT_REGEX ,version ["version" ]) 29 30if snapshotmatch == None : 31return versiontuple (version ["version" ])>= (1 ,18 ,2 ) 32else : 33return tuple (map (int ,snapshotmatch .groups ()))>= (22 ,11 ) 34 35 36# Uncomment to package only major releases: 37# GAME_VERSION_FILTER = lambda version: version["stable"] and versiontuple(version["version"]) > (1, 18, 2) 38 39 40def get_game_versions (): 41""" 42Returns a list of game versions that the Quilt loader supports, filtered 43using the GAME_VERSION_FILTER above. The `version` variable is in the format 44{"verson": string, "stable": bool} 45""" 46"Fetching game versions" ) 47data = requests .get (f" { ENDPOINT } /game" ).json () 48return [version ["version" ]for version in data if GAME_VERSION_FILTER (version )] 49 50 51def get_loader_versions (): 52""" 53Returns a list of the Quilt loader versions that should be packaged, filtered 54using the LOADER_VERSION_FILTER above. The `version` variable is in the format 55{"separater": string, "build": int, "maven": string, "version": string, "stable": bool} 56""" 57"Fetching loader versions" ) 58data = requests .get (f" { ENDPOINT } /loader" ).json () 59return [version ["version" ]for version in data if LOADER_VERSION_FILTER (version )] 60 61 62def fetch_version (game_version ,loader_version ): 63""" 64Return the server json for a given game and loader version 65""" 66return requests .get ( 67f" { ENDPOINT } /loader/ { game_version } / { loader_version } /server/json" 68 ).json () 69 70 71def gen_locks (version ,libraries ): 72""" 73Return the lock information for a given server json, returned in the format 74{ 75"mainClass": string, 76"libraries": [ 77{"name": string, "url": string, "sha256": string}, 78... 79] 80} 81""" 82ret = {"mainClass" :version ["mainClass" ],"libraries" : []} 83 84for library in version ["libraries" ]: 85name ,url = library ["name" ],library ["url" ] 86 87if not name in libraries : 88f"- - - Fetching library { name } " ) 89ldir ,lname ,lversion = name .split (":" ) 90lfilename = f" { lname } - { lversion } .zip" 91lurl = f" { url } { ldir . replace ( '.' , '/' ) } / { lname } / { lversion } / { lname } - { lversion } .jar" 92 93lhash = subprocess .run ( 94 ["nix-prefetch-url" ,lurl ],capture_output = True ,encoding = "UTF-8" 95 ).stdout .rstrip ("\n" ) 96 97libraries [name ]= {"name" :lfilename ,"url" :lurl ,"sha256" :lhash } 98else : 99pass 100# print(f"- - - Using cached library {name}") 101 102ret ["libraries" ].append (name ) 103 104return ret 105 106 107def main (versions ,libraries ,locks ,lib_locks ): 108""" 109Fetch the relevant information and update the lockfiles. 110`versions` and `libraries` are data from the existing files, while 111`locks` and `lib_locks` are file objects to be written to 112""" 113loader_versions = get_loader_versions () 114game_versions = get_game_versions () 115 116"Starting fetch" ) 117try : 118for loader_version in loader_versions : 119f"- Loader: { loader_version } " ) 120if not versions .get (loader_version ,None ): 121versions [loader_version ]= {} 122 123for game_version in game_versions : 124if not versions [loader_version ].get (game_version ,None ): 125f"- - Game: { game_version } " ) 126versions [loader_version ][game_version ]= gen_locks ( 127fetch_version (game_version ,loader_version ),libraries 128 ) 129else : 130f"- - Game: { game_version } : Already locked" ) 131 132except KeyboardInterrupt : 133"Cancelled fetching, writing and exiting" ) 134 135json .dump (versions ,locks ,indent = 2 ) 136json .dump (libraries ,lib_locks ,indent = 2 ) 137locks .write ("\n" ) 138lib_locks .write ("\n" ) 139 140 141if __name__ == "__main__" : 142folder = Path (__file__ ).parent 143lo = folder / "locks.json" 144li = folder / "libraries.json" 145lo .touch () 146li .touch () 147 148with ( 149open (lo ,"r" )as locks , 150open (li ,"r" )as lib_locks , 151 ): 152versions = {}if lo .stat ().st_size == 0 else json .load (locks ) 153libraries = {}if li .stat ().st_size == 0 else json .load (lib_locks ) 154 155with ( 156open (lo ,"w" )as locks , 157open (li ,"w" )as lib_locks , 158 ): 159main (versions ,libraries ,locks ,lib_locks )