Fix download of forge libraries for older versions.

Added function to determine extension type by checking the forge server.
This commit is contained in:
Daniel Scalzi
2020-05-30 01:19:19 -04:00
parent 87e5299af6
commit 407032df61
2 changed files with 61 additions and 5 deletions

View File

@@ -70,4 +70,28 @@ export abstract class BaseMavenRepo extends BaseFileStructure {
}) })
} }
public async headArtifactById(url: string, mavenIdentifier: string, extension?: string): Promise<boolean> {
return this.headArtifactBase(url, MavenUtil.mavenIdentifierToString(mavenIdentifier, extension) as string)
}
public async headArtifactByComponents(
url: string, group: string, artifact: string, version: string, classifier?: string, extension?: string
): Promise<boolean> {
return this.headArtifactBase(url,
MavenUtil.mavenComponentsToString(group, artifact, version, classifier, extension))
}
private async headArtifactBase(url: string, relative: string): Promise<boolean> {
const resolvedURL = resolveURL(url, relative).toString()
try {
const response = await axios({
method: 'head',
url: resolvedURL
})
return response.status === 200
} catch (ignored) {
return false
}
}
} }

View File

@@ -11,12 +11,16 @@ import { VersionUtil } from '../../../util/versionutil'
import { ForgeResolver } from '../forge.resolver' import { ForgeResolver } from '../forge.resolver'
import { MinecraftVersion } from '../../../util/MinecraftVersion' import { MinecraftVersion } from '../../../util/MinecraftVersion'
type ArrayElement<A> = A extends readonly (infer T)[] ? T : never
export class ForgeGradle2Adapter extends ForgeResolver { export class ForgeGradle2Adapter extends ForgeResolver {
public static isForVersion(version: MinecraftVersion): boolean { public static isForVersion(version: MinecraftVersion): boolean {
return VersionUtil.isVersionAcceptable(version, [7, 8, 9, 10, 11, 12]) return VersionUtil.isVersionAcceptable(version, [7, 8, 9, 10, 11, 12])
} }
protected readonly MOJANG_REMOTE_REPOSITORY = 'https://libraries.minecraft.net/'
constructor( constructor(
absoluteRoot: string, absoluteRoot: string,
relativeRoot: string, relativeRoot: string,
@@ -100,8 +104,8 @@ export class ForgeGradle2Adapter extends ForgeResolver {
} }
console.debug(`Processing ${lib.name}..`) console.debug(`Processing ${lib.name}..`)
const extension = this.determineExtension(lib.checksums) const extension = await this.determineExtension(lib, libRepo)
const localPath = libRepo.getArtifactById(lib.name, extension) as string const localPath = libRepo.getArtifactById(lib.name, extension)
const postProcess = extension === 'jar.pack.xz' const postProcess = extension === 'jar.pack.xz'
@@ -126,7 +130,7 @@ export class ForgeGradle2Adapter extends ForgeResolver {
} }
if (queueDownload) { if (queueDownload) {
await libRepo.downloadArtifactById(lib.url || 'https://libraries.minecraft.net/', lib.name, extension) await libRepo.downloadArtifactById(lib.url || this.MOJANG_REMOTE_REPOSITORY, lib.name, extension)
libBuf = await readFile(localPath) libBuf = await readFile(localPath)
} else { } else {
console.debug('Using local copy.') console.debug('Using local copy.')
@@ -176,14 +180,42 @@ export class ForgeGradle2Adapter extends ForgeResolver {
return forgeModule return forgeModule
} }
private determineExtension(checksums: string[] | undefined): string { private async determineExtension(lib: ArrayElement<VersionManifestFG2['libraries']>, libRepo: LibRepoStructure): Promise<string> {
return checksums != null && checksums.length > 1 ? 'jar.pack.xz' : 'jar' if(lib.url == null) {
return 'jar'
}
console.log('Determing extension..')
const possibleExt = [
'jar.pack.xz',
'jar'
]
// Check locally.
for(const ext of possibleExt) {
const localPath = libRepo.getArtifactById(lib.name, ext)
const exists = await libRepo.artifactExists(localPath)
if(exists) {
return ext
}
}
// Check remote.
for(const ext of possibleExt) {
const exists = await libRepo.headArtifactById(this.REMOTE_REPOSITORY, lib.name, ext)
if(exists) {
return ext
}
}
// Default to jar.
return 'jar'
} }
private async processPackXZFiles( private async processPackXZFiles(
processingQueue: Array<{id: string, localPath: string}> processingQueue: Array<{id: string, localPath: string}>
): Promise<Array<{id: string, MD5: string}>> { ): Promise<Array<{id: string, MD5: string}>> {
if(processingQueue.length == 0) {
return []
}
const accumulator = [] const accumulator = []
const tempDir = this.repoStructure.getTempDirectory() const tempDir = this.repoStructure.getTempDirectory()