Refactor launcher profiles and port automation
This commit is contained in:
@@ -53,7 +53,7 @@ async function downloadSourceToFile(source, destination){
|
||||
const localSource = resolveLocalSource(source)
|
||||
const localStat = await fs.stat(localSource)
|
||||
if(localStat.isDirectory()){
|
||||
throw new Error(`Directory source is not supported for archive cache: ${localSource}`)
|
||||
throw new Error(`Directory source is not supported for file cache: ${localSource}`)
|
||||
}
|
||||
await fs.copy(localSource, destination, { overwrite: true })
|
||||
return destination
|
||||
@@ -83,21 +83,38 @@ async function extractZipToDirectory(zipPath, destination){
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureCachedArchive(profileId, source, fileName){
|
||||
async function ensureCachedFile(profileId, source, fileName){
|
||||
const cachePath = getProfileCacheFile(profileId, fileName)
|
||||
await downloadSourceToFile(source, cachePath)
|
||||
return cachePath
|
||||
}
|
||||
|
||||
async function ensureServerWorldInstalled(profile, serverDirectory){
|
||||
if(!profile.worldArchiveUrl){
|
||||
return null
|
||||
}
|
||||
|
||||
const targetWorldDirectory = path.join(serverDirectory, 'world')
|
||||
if(isRemoteSource(profile.worldArchiveUrl) || profile.worldArchiveUrl.endsWith('.zip') || profile.worldArchiveUrl.startsWith('file://')){
|
||||
const cachePath = await ensureCachedFile(profile.id, profile.worldArchiveUrl, 'world.zip')
|
||||
await extractZipToDirectory(cachePath, targetWorldDirectory)
|
||||
} else {
|
||||
await fs.remove(targetWorldDirectory)
|
||||
await fs.copy(resolveLocalSource(profile.worldArchiveUrl), targetWorldDirectory, { overwrite: true })
|
||||
}
|
||||
|
||||
return targetWorldDirectory
|
||||
}
|
||||
|
||||
exports.getProfileBaseDirectory = getProfileBaseDirectory
|
||||
exports.getServerBundleDirectory = getServerBundleDirectory
|
||||
|
||||
exports.prefetchProfileAssets = async function(profile){
|
||||
if(profile.worldArchiveUrl){
|
||||
await ensureCachedArchive(profile.id, profile.worldArchiveUrl, 'world.zip')
|
||||
await ensureCachedFile(profile.id, profile.worldArchiveUrl, 'world.zip')
|
||||
}
|
||||
if(profile.serverBundleUrl){
|
||||
await ensureCachedArchive(profile.id, profile.serverBundleUrl, 'server.zip')
|
||||
if(profile.serverJarUrl){
|
||||
await ensureCachedFile(profile.id, profile.serverJarUrl, 'server.jar')
|
||||
}
|
||||
|
||||
const currentState = ConfigManager.getLibraryProfileAssetState(profile.id)
|
||||
@@ -118,7 +135,7 @@ exports.ensureWorldInstalled = async function(profile, serverId){
|
||||
|
||||
await fs.ensureDir(savesDirectory)
|
||||
if(isRemoteSource(profile.worldArchiveUrl) || profile.worldArchiveUrl.endsWith('.zip') || profile.worldArchiveUrl.startsWith('file://')){
|
||||
const cachePath = await ensureCachedArchive(profile.id, profile.worldArchiveUrl, 'world.zip')
|
||||
const cachePath = await ensureCachedFile(profile.id, profile.worldArchiveUrl, 'world.zip')
|
||||
await extractZipToDirectory(cachePath, targetWorldDirectory)
|
||||
} else {
|
||||
await fs.remove(targetWorldDirectory)
|
||||
@@ -136,33 +153,36 @@ exports.ensureWorldInstalled = async function(profile, serverId){
|
||||
return targetWorldDirectory
|
||||
}
|
||||
|
||||
exports.ensureServerBundleInstalled = async function(profile){
|
||||
if(!profile.serverBundleUrl){
|
||||
exports.ensureServerJarInstalled = async function(profile){
|
||||
if(!profile.serverJarUrl){
|
||||
return null
|
||||
}
|
||||
|
||||
const targetDirectory = getServerBundleDirectory(profile)
|
||||
if(isRemoteSource(profile.serverBundleUrl) || profile.serverBundleUrl.endsWith('.zip') || profile.serverBundleUrl.startsWith('file://')){
|
||||
const cachePath = await ensureCachedArchive(profile.id, profile.serverBundleUrl, 'server.zip')
|
||||
await extractZipToDirectory(cachePath, targetDirectory)
|
||||
} else {
|
||||
await fs.remove(targetDirectory)
|
||||
await fs.copy(resolveLocalSource(profile.serverBundleUrl), targetDirectory, { overwrite: true })
|
||||
}
|
||||
const targetJarPath = path.join(targetDirectory, 'server.jar')
|
||||
await fs.ensureDir(targetDirectory)
|
||||
|
||||
const cachePath = await ensureCachedFile(profile.id, profile.serverJarUrl, 'server.jar')
|
||||
await fs.copy(cachePath, targetJarPath, { overwrite: true })
|
||||
await ensureServerWorldInstalled(profile, targetDirectory)
|
||||
|
||||
ConfigManager.setLibraryProfileAssetState(profile.id, {
|
||||
serverBundleInstalledAt: new Date().toISOString(),
|
||||
serverBundleDirectory: targetDirectory
|
||||
serverInstalledAt: new Date().toISOString(),
|
||||
serverDirectory: targetDirectory,
|
||||
serverJarPath: targetJarPath
|
||||
})
|
||||
ConfigManager.save()
|
||||
|
||||
return targetDirectory
|
||||
return {
|
||||
serverDirectory: targetDirectory,
|
||||
serverJarPath: targetJarPath
|
||||
}
|
||||
}
|
||||
|
||||
exports.ensureServerBundleInstalled = exports.ensureServerJarInstalled
|
||||
|
||||
exports.prepareProfileForLaunch = async function(profile, serverId){
|
||||
if(profile.kind === 'map'){
|
||||
return exports.ensureWorldInstalled(profile, serverId)
|
||||
}
|
||||
|
||||
return null
|
||||
return exports.ensureWorldInstalled(profile, serverId)
|
||||
}
|
||||
|
||||
exports.ensureServerWorldInstalled = ensureServerWorldInstalled
|
||||
|
||||
Reference in New Issue
Block a user