import { MinecraftFolder , MinecraftLocation , Version } from "@xmcl/core" ; import { fetchJson , getLastModified , Timestamped } from "./http/fetch" ; import { ensureFile , InstallOptions , writeFile } from "./utils" ; export const YARN_MAVEN_URL = "https://maven.fabricmc.net/net/fabricmc/yarn/maven-metadata.xml" ; export const LOADER_MAVEN_URL = "https://maven.fabricmc.net/net/fabricmc/fabric-loader/maven-metadata.xml" ; see https export interface FabricYarnVersionList extends Timestamped { versions : FabricArtifactVersion [ ] ; } see https export interface FabricLoaderVersionList extends Timestamped { versions : FabricArtifactVersion [ ] ; } export interface FabricArtifactVersion { gameVersion ? : string ; separator ? : string ; build ? : number ; maven : string ; version : string ; stable : boolean ; } export interface FabricArtifacts { mappings : FabricArtifactVersion [ ] ; loader : FabricArtifactVersion [ ] ; } export interface FabricLoaderArtifact { loader : FabricArtifactVersion ; intermediary : FabricArtifactVersion ; launcherMeta : { version : number ; libraries : { client : { name : string ; url : string ; } [ ] ; common : { name : string ; url : string ; } [ ] ; server : { name : string ; url : string ; } [ ] ; } ; mainClass : { client : string ; server : string ; } ; } ; } export const DEFAULT_FABRIC_API = "https://meta.fabricmc.net/v2" ; param remote beta export function getFabricArtifacts ( remote = DEFAULT_FABRIC_API ) { return fetchJson ( remote + "/versions" ) ; } param remote beta export function getYarnArtifactList ( remote = DEFAULT_FABRIC_API ) { return fetchJson ( remote + "/versions/yarn" ) ; } param minecraft param remote beta export function getYarnArtifactListFor ( minecraft , remote = DEFAULT_FABRIC_API ) { return fetchJson ( remote + "/versions/yarn/" + minecraft ) ; } param remote beta export function getLoaderArtifactList ( remote = DEFAULT_FABRIC_API ) { return fetchJson ( remote + "/versions/loader" ) ; } param minecraft param remote beta export function getLoaderArtifactListFor ( minecraft , remote = DEFAULT_FABRIC_API ) { return fetchJson ( remote + "/versions/loader/" + minecraft ) ; } param minecraft param loader param remote beta export function getFabricLoaderArtifact ( minecraft , loader , remote = DEFAULT_FABRIC_API ) { return fetchJson ( remote + "/versions/loader/" + minecraft + "/" + loader ) ; } beta export async function getYarnVersionListFromXML ( option = { } ) { let [ modified , timestamp ] = await getLastModified ( YARN_MAVEN_URL , option . original ?. timestamp ) ; if ( modified || ! option . original ) { let versions = await getYarnArtifactList ( option . remote ) ; return { versions : versions , timestamp : timestamp ?? "" , } ; } return option . original ; } beta export async function getLoaderVersionListFromXML ( option ) { let [ modified , timestamp ] = await getLastModified ( LOADER_MAVEN_URL , option . original ?. timestamp ) ; if ( modified || ! option . original ) { let versions = await getLoaderArtifactList ( option . remote ) ; return { versions : versions , timestamp : timestamp ?? "" , } ; } return option . original ; } param yarnVersion param loaderVersion param minecraft returns export async function installFabricYarnAndLoader ( yarnVersion , loaderVersion , minecraft , options = { } ) { const folder = MinecraftFolder . from ( minecraft ) ; const mcversion = yarnVersion . split ( "+" ) [ 0 ] ; const id = options . versionId || `${ mcversion }-fabric${ yarnVersion }-${ loaderVersion }` ; const jsonFile = folder . getVersionJson ( id ) ; const body = await fetchJson ( `https://fabricmc.net/download/technic/?yarn=${ encodeURIComponent ( yarnVersion ) }&loader=${ encodeURIComponent ( loaderVersion ) }` ) ; body . id = id ; if ( typeof options . inheritsFrom === "string" ) { body . inheritsFrom = options . inheritsFrom ; } await ensureFile ( jsonFile ) ; await writeFile ( jsonFile , JSON . stringify ( body ) ) ; return id ; } export interface FabricInstallOptions extends InstallOptions { side ? : "client" | "server" ; yarnVersion ? : string | FabricArtifactVersion ; } param side param yarnVersion param loader param minecraft param options beta export async function installFabric ( loader , minecraft , options = { } ) { const folder = MinecraftFolder . from ( minecraft ) ; let yarn ; let side = options . side ?? "client" ; let id = options . versionId ; let mcversion ; if ( options . yarnVersion ) { let yarnVersion = options . yarnVersion ; if ( typeof yarnVersion === "string" ) { yarn = yarnVersion ; mcversion = yarn . split ( "+" ) [ 0 ] ; } else { yarn = yarnVersion . version ; mcversion = yarnVersion . gameVersion || yarn . split ( "+" ) [ 0 ] ; } } else { mcversion = loader . intermediary . version ; } if ( ! id ) { id = mcversion ; if ( yarn ) { id += `-fabric${ yarn }-loader${ loader . loader . version }` ; } else { id += `-fabric${ loader . loader . version }` ; } } let libraries = [ { name : loader . loader . maven , url : "https://maven.fabricmc.net/" } , { name : loader . intermediary . maven , url : "https://maven.fabricmc.net/" } , ... ( options . yarnVersion ? [ { name : `net.fabricmc:yarn:${ yarn }` , url : "https://maven.fabricmc.net/" } ] : [ ] ) , ... loader . launcherMeta . libraries . common , ... loader . launcherMeta . libraries [ side ] , ] ; let mainClass = loader . launcherMeta . mainClass [ side ] ; let inheritsFrom = options . inheritsFrom || mcversion ; let jsonFile = folder . getVersionJson ( id ) ; await ensureFile ( jsonFile ) ; await writeFile ( jsonFile , JSON . stringify ( { id , inheritsFrom , mainClass , libraries , arguments : { game : [ ] , jvm : [ ] , } , releaseTime : new Date ( ) . toJSON ( ) , time : new Date ( ) . toJSON ( ) , } ) ) ; return id ; }