Skip to content

Commit

Permalink
fix: Try to fix the stuck on libraries or assets case
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Oct 19, 2023
1 parent a69dd76 commit 7b9f5c3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions xmcl-keystone-ui/src/composables/instanceVersionDiagnose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function useInstanceVersionDiagnose(runtime: Ref<RuntimeVersions>, resolv
if (librariesIssue.length > 0) {
const options = { named: { count: librariesIssue.length } }
ops.push(async () => {
await installLibraries(librariesIssue.map(v => v.library))
await installLibraries(librariesIssue.map(v => v.library), version.id, librariesIssue.length < 15)
})
items.push(librariesIssue.some(v => v.type === 'corrupted')
? reactive({
Expand All @@ -117,7 +117,7 @@ export function useInstanceVersionDiagnose(runtime: Ref<RuntimeVersions>, resolv
if (assetsIssue.length > 0) {
const options = { named: { count: assetsIssue.length } }
ops.push(async () => {
await installAssets(assetsIssue.map(v => v.asset))
await installAssets(assetsIssue.map(v => v.asset), version.id, assetsIssue.length < 15)
})
items.push(assetsIssue.some(v => v.type === 'corrupted')
? reactive({
Expand Down
4 changes: 2 additions & 2 deletions xmcl-runtime-api/src/services/InstallService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ export interface InstallService {
* Install assets to the version
* @param version The local version id
*/
installAssets(assets: Asset[]): Promise<void>
installAssets(assets: Asset[], version?: string, force?: boolean): Promise<void>
/**
* Download and install a minecraft version
*/
installMinecraft(meta: MinecraftVersion): Promise<void>
/**
* Install provided libraries to game.
*/
installLibraries(libraries: InstallableLibrary[]): Promise<void>
installLibraries(libraries: InstallableLibrary[], version?: string, force?: boolean): Promise<void>
/**
* Install neoForged to the minecraft
*/
Expand Down
34 changes: 29 additions & 5 deletions xmcl-runtime/lib/services/InstallService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AbortableTask, task } from '@xmcl/task'
import { XMLParser } from 'fast-xml-parser'
import { existsSync } from 'fs'
import { ensureFile } from 'fs-extra/esm'
import { readFile, writeFile } from 'fs/promises'
import { readFile, unlink, writeFile } from 'fs/promises'
import { Dispatcher, errors, request } from 'undici'
import { URL } from 'url'
import { LauncherApp } from '../app/LauncherApp'
Expand Down Expand Up @@ -503,10 +503,22 @@ export class InstallService extends AbstractService implements IInstallService {
}

@Lock(LockKey.assets)
async installAssets(assets: Asset[], version?: string) {
async installAssets(assets: Asset[], version?: string, force?: boolean) {
const option = this.getInstallOptions()
const location = this.getPath()
const task = installResolvedAssetsTask(assets, new MinecraftFolder(location), option).setName('installAssets', { id: version })
const folder = new MinecraftFolder(location)
if (force) {
// Remove assets before download
const promises = [] as Promise<void>[]
for (const a of assets) {
const path = folder.getAsset(a.hash)
if (path) {
promises.push(unlink(path).catch(() => { }))
}
}
await Promise.all(promises)
}
const task = installResolvedAssetsTask(assets, folder, option).setName('installAssets', { id: version })
await this.submit(task)
}

Expand Down Expand Up @@ -538,16 +550,28 @@ export class InstallService extends AbstractService implements IInstallService {
}

@Lock(LockKey.libraries)
async installLibraries(libraries: InstallableLibrary[], version?: string) {
async installLibraries(libraries: InstallableLibrary[], version?: string, force?: boolean) {
let resolved: ResolvedLibrary[]
if ('downloads' in libraries[0]) {
resolved = Version.resolveLibraries(libraries)
} else {
resolved = libraries as any
}
const option = this.getInstallOptions()
const task = installResolvedLibrariesTask(resolved, this.getPath(), option).setName('installLibraries', { id: version })
const folder = MinecraftFolder.from(this.getPath())
const task = installResolvedLibrariesTask(resolved, folder, option).setName('installLibraries', { id: version })
try {
if (force) {
// remove lib before download
const promises = [] as Promise<void>[]
for (const lib of resolved) {
const path = folder.getLibraryByPath(lib.path)
if (path) {
promises.push(unlink(path).catch(() => { }))
}
}
await Promise.all(promises)
}
await this.submit(task)
} catch (e) {
this.warn('An error ocurred during install libraries:')
Expand Down

0 comments on commit 7b9f5c3

Please sign in to comment.