Skip to content

Commit

Permalink
Add code to handle old server jar dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Dec 6, 2024
1 parent 999449c commit 9b9587e
Showing 1 changed file with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.transform.CompileStatic
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction

import javax.inject.Inject
import java.util.jar.JarInputStream
import java.util.zip.ZipFile

@CompileStatic
Expand Down Expand Up @@ -163,6 +170,42 @@ abstract class GenerateModuleMetadata extends DefaultTask implements HasMinecraf

private static final List<String> platforms = ["windows", "osx", "linux"]

private Set<String> getDirectoryListing(String artifactId) {
var listingFile = new File(temporaryDir, artifactId.replace(':', '-') + ".txt")
if (listingFile.exists()) {
return new HashSet<>(listingFile.readLines())
}

var parts = artifactId.split(":")
String relativeUrl
if (parts.length == 3) {
relativeUrl = parts[0].replace('.', '/') + "/" + parts[1] + "/" + parts[2] + "/" + parts[1] + "-" + parts[2] + ".jar"
} else {
relativeUrl = parts[0].replace('.', '/') + "/" + parts[1] + "/" + parts[2] + "/" + parts[1] + "-" + parts[2] + "-" + parts[3] + ".jar"
}
var url = URI.create("https://libraries.minecraft.net/" + relativeUrl).toURL()
println("Getting $url")
var folders = new HashSet<String>()
url.openStream().with { input ->
var jin = new JarInputStream(input)
for (var entry = jin.nextJarEntry; entry != null; entry = jin.nextJarEntry) {
if (!entry.name.startsWith("META-INF/") && entry.name.contains("/") && !entry.name.endsWith("/")) {
folders.add(entry.name.substring(0, entry.name.lastIndexOf('/')))
}
}
}

println("Found ${folders.size()} folders in jar")

try (var writer = listingFile.newWriter()) {
for (var folder in folders) {
writer.writeLine(folder)
}
}

return folders
}

private void getMcDeps(List<String> server, List<String> client, Map<String, List<String>> clientNatives) {
Map metaJson = new JsonSlurper().parse(meta.get().asFile) as Map
(metaJson.libraries as List<Map<String, Object>>).each { Map lib ->
Expand All @@ -188,15 +231,61 @@ abstract class GenerateModuleMetadata extends DefaultTask implements HasMinecraf
}
}
try (def zf = new ZipFile(serverJar.get().getAsFile())) {
def entry = zf.getEntry('META-INF/libraries.list')
if (entry != null) {
try (def is = zf.getInputStream(entry)) {
def librariesListEntry = zf.getEntry('META-INF/libraries.list')
if (librariesListEntry != null) {
try (def is = zf.getInputStream(librariesListEntry)) {
is.readLines().each {
server.add(it.split('\t')[1])
}
}
} else {
throw new RuntimeException("Could not find libraries.list inside of server.jar")
// This will be slow.

// List all directories found in the server jar
var dirnames = new HashSet<String>();
zf.entries().iterator().each {
if (it.name.contains("/") && !it.name.endsWith("/")) {
var dirname = it.name.substring(0, it.name.lastIndexOf('/'))
if (
// the annotation packages are re-added by NeoForm/MCP anyway and are not required to run or compile against the code
dirname != "javax/annotation"
&& dirname != "javax/annotation/concurrent"
&& dirname != "javax/annotation/meta"
&& !dirname.startsWith("net/minecraft/")
&& !dirname.startsWith("data/")
&& !dirname.startsWith("assets/")
&& dirname != "assets"
&& dirname != "data"
&& dirname != "META-INF"
&& !dirname.startsWith("META-INF/")) {
dirnames.add(dirname)
}
}
}

var mappedDirnames = new HashSet<String>()
var libraries = new LinkedHashSet<String>()

// For each client library, find the list of folders contained within
// We need to do this in order of the declared libraries since mojang chose to fix log4j2 issues
// by introducing a higherpriority library that contains files from log4j2 and netty
for (var artifactId in client) {
var folderListing = getDirectoryListing(artifactId)
for (var folder in folderListing) {
if (dirnames.contains(folder)) {
libraries.add(artifactId)
mappedDirnames.add(folder)
}
}
}

// Now report any directories in the server jar we could NOT map to a library
dirnames.removeAll(mappedDirnames)
if (!dirnames.isEmpty()) {
throw new GradleException("Couldn't map the following directories from the server jar to libraries: " + dirnames)
}

server.addAll(libraries)
}
}
}
Expand Down

0 comments on commit 9b9587e

Please sign in to comment.