first commit
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { existsSync } from 'fs'
|
||||
import { join } from 'path'
|
||||
import { getVersionList, install, installNeoForged } from '@xmcl/installer'
|
||||
import { paths } from './paths'
|
||||
import { emit } from './events'
|
||||
import { downloadDispatcher, DOWNLOAD_CONCURRENCY, withRetries } from './net'
|
||||
|
||||
/** Options de téléchargement communes : dispatcher tolérant + concurrence bridée. */
|
||||
const downloadOptions = {
|
||||
dispatcher: downloadDispatcher,
|
||||
assetsDownloadConcurrency: DOWNLOAD_CONCURRENCY,
|
||||
librariesDownloadConcurrency: DOWNLOAD_CONCURRENCY
|
||||
}
|
||||
|
||||
/**
|
||||
* Installe le runtime Minecraft (vanilla 1.21.1) puis NeoForge dans
|
||||
* paths.gameRoot. Les deux étapes sont idempotentes : si la version est déjà
|
||||
* présente sur le disque, on ne refait pas le travail.
|
||||
*
|
||||
* Retourne l'id de version NeoForge à lancer (ex. "neoforge-21.1.73").
|
||||
*/
|
||||
|
||||
/** true si un dossier de version <id> avec son JSON existe déjà. */
|
||||
function versionInstalled(id: string): boolean {
|
||||
return existsSync(join(paths.gameRoot, 'versions', id, `${id}.json`))
|
||||
}
|
||||
|
||||
/** Installe Minecraft vanilla <version> (json + jar + assets + libraries). */
|
||||
export async function installMinecraft(version: string): Promise<void> {
|
||||
if (versionInstalled(version)) return
|
||||
|
||||
emit.progress({ phase: 'minecraft', message: `Minecraft ${version} : métadonnées…`, progress: undefined })
|
||||
const list = await getVersionList()
|
||||
const meta = list.versions.find((v) => v.id === version)
|
||||
if (!meta) throw new Error(`Version Minecraft introuvable : ${version}`)
|
||||
|
||||
emit.progress({
|
||||
phase: 'minecraft',
|
||||
message: `Installation de Minecraft ${version} (assets + libs)…`,
|
||||
progress: undefined
|
||||
})
|
||||
|
||||
// ~3700 assets : on réessaie l'install entière plusieurs fois. Chaque passe
|
||||
// ignore les fichiers déjà valides et ne reprend que les manquants.
|
||||
await withRetries(
|
||||
() => install(meta, paths.gameRoot, downloadOptions),
|
||||
5,
|
||||
(attempt) =>
|
||||
emit.progress({
|
||||
phase: 'minecraft',
|
||||
message: `Reprise des téléchargements (tentative ${attempt + 1})…`,
|
||||
progress: undefined
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Installe NeoForge <neoforge> par-dessus Minecraft <minecraft>.
|
||||
* Nécessite un Java (les "processors" de l'installeur tournent en Java).
|
||||
*/
|
||||
export async function installNeoForge(
|
||||
neoforge: string,
|
||||
minecraft: string,
|
||||
javaPath: string
|
||||
): Promise<string> {
|
||||
// @xmcl nomme la version installée "neoforge-<version>" (ex. neoforge-21.1.224).
|
||||
// Si elle est déjà présente, on saute l'install (idempotent, comme Minecraft).
|
||||
const expectedId = `neoforge-${neoforge}`
|
||||
if (versionInstalled(expectedId)) {
|
||||
void minecraft
|
||||
return expectedId
|
||||
}
|
||||
|
||||
emit.progress({ phase: 'neoforge', message: `Installation de NeoForge ${neoforge}…`, progress: undefined })
|
||||
|
||||
// installNeoForged retourne l'id de version installée à lancer.
|
||||
const versionId = await withRetries(
|
||||
() =>
|
||||
installNeoForged('neoforge', neoforge, paths.gameRoot, {
|
||||
java: javaPath,
|
||||
dispatcher: downloadDispatcher,
|
||||
librariesDownloadConcurrency: DOWNLOAD_CONCURRENCY
|
||||
}),
|
||||
3,
|
||||
(attempt) =>
|
||||
emit.progress({
|
||||
phase: 'neoforge',
|
||||
message: `Reprise de l'installation NeoForge (tentative ${attempt + 1})…`,
|
||||
progress: undefined
|
||||
})
|
||||
)
|
||||
|
||||
if (!versionInstalled(versionId)) {
|
||||
throw new Error(`NeoForge installé mais version "${versionId}" introuvable sur le disque.`)
|
||||
}
|
||||
void minecraft // (info de contexte ; la version NeoForge hérite déjà de Minecraft)
|
||||
return versionId
|
||||
}
|
||||
Reference in New Issue
Block a user