Skip to content

Commit

Permalink
Rework external library handling. (#846)
Browse files Browse the repository at this point in the history
* Generate installer jsons

* Bump to version2

* Include file size

* Add min java version

* Exclude signature info from shaded jars

* Add a little script to aid installing a test version to the minecraft launcher.
  • Loading branch information
modmuss50 authored Nov 13, 2023
1 parent fe03387 commit 41e71f0
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 134 deletions.
61 changes: 46 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,55 @@ sourceSets {
}

configurations {
include
include {
transitive = false
}

implementation {
extendsFrom include
}

installer {
transitive = false
}
installerLaunchWrapper {
transitive = false
extendsFrom installer
}

api {
extendsFrom installer
}
}

dependencies {
// fabric-loader dependencies
api "org.ow2.asm:asm:${project.asm_version}"
api "org.ow2.asm:asm-analysis:${project.asm_version}"
api "org.ow2.asm:asm-commons:${project.asm_version}"
api "org.ow2.asm:asm-tree:${project.asm_version}"
api "org.ow2.asm:asm-util:${project.asm_version}"

api("net.fabricmc:sponge-mixin:${project.mixin_version}") {
exclude module: 'launchwrapper'
exclude module: 'guava'
repositories {
maven {
name = 'Mojang'
url = 'https://libraries.minecraft.net/'
content {
includeGroup "net.minecraft"
}
}
api 'net.fabricmc:tiny-mappings-parser:0.3.0+build.17'
api 'net.fabricmc:tiny-remapper:0.8.2'
api 'net.fabricmc:access-widener:2.1.0'
}


dependencies {
// fabric-loader dependencies
installer "org.ow2.asm:asm:${project.asm_version}"
installer "org.ow2.asm:asm-analysis:${project.asm_version}"
installer "org.ow2.asm:asm-commons:${project.asm_version}"
installer "org.ow2.asm:asm-tree:${project.asm_version}"
installer "org.ow2.asm:asm-util:${project.asm_version}"
installer "net.fabricmc:sponge-mixin:${project.mixin_version}"
// removed with mapping-io PR
installer "net.fabricmc:tiny-mappings-parser:0.3.0+build.17"
installerLaunchWrapper "net.minecraft:launchwrapper:1.12"

// impl dependencies
include 'org.ow2.sat4j:org.ow2.sat4j.core:2.3.6'
include 'org.ow2.sat4j:org.ow2.sat4j.pb:2.3.6'
include "net.fabricmc:tiny-remapper:0.8.2"
include "net.fabricmc:access-widener:2.1.0"

testCompileOnly 'org.jetbrains:annotations:23.0.0'

Expand All @@ -87,6 +111,9 @@ dependencies {
testRuntimeOnly('org.junit.platform:junit-platform-launcher')
}

apply from: rootProject.file('gradle/installer-json.gradle')
apply from: rootProject.file('gradle/launcher.gradle')

processResources {
inputs.property "version", project.version

Expand Down Expand Up @@ -156,10 +183,14 @@ task fatJar(type: ShadowJar, dependsOn: getSat4jAbout) {
configurations = [project.configurations.include]

relocate 'org.sat4j', 'net.fabricmc.loader.impl.lib.sat4j'
relocate 'net.fabricmc.accesswidener', 'net.fabricmc.loader.impl.lib.accesswidener'
relocate 'net.fabricmc.tinyremapper', 'net.fabricmc.loader.impl.lib.tinyremapper'

exclude 'about.html'
exclude 'sat4j.version'
exclude 'META-INF/maven/org.ow2.sat4j/*/**'
exclude 'META-INF/*.RSA'
exclude 'META-INF/*.SF'

outputs.upToDateWhen { false }
}
Expand Down
107 changes: 107 additions & 0 deletions gradle/installer-json.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import groovy.json.JsonOutput

tasks.register("generateInstallerJson", GenerateInstallerJsonTask) {
configurations = [ common: "installer" ]
outputFile = file("src/main/resources/fabric-installer.json")
options = [
mainClasses: [
client: "net.fabricmc.loader.impl.launch.knot.KnotClient",
server: "net.fabricmc.loader.impl.launch.knot.KnotServer"
]
]
}

tasks.register("generateLaunchWrapperInstallerJson", GenerateInstallerJsonTask) {
configurations = [ common: "installerLaunchWrapper" ]
outputFile = file("src/main/resources/fabric-installer.launchwrapper.json")
options = [
mainClass: "net.minecraft.launchwrapper.Launch",
arguments: [
client: [],
common: [],
server: [],
],
launchwrapper: [
tweakers: [
client: [
"net.fabricmc.loader.impl.game.minecraft.launchwrapper.FabricClientTweaker"
],
common: [],
server: [
"net.fabricmc.loader.impl.game.minecraft.launchwrapper.FabricServerTweaker"
]
]
]
]
}

abstract class GenerateInstallerJsonTask extends DefaultTask {
@Input
abstract MapProperty<String, String> getConfigurations()

@Input
abstract MapProperty<String, Object> getOptions()

@OutputFile
abstract RegularFileProperty getOutputFile()

GenerateInstallerJsonTask() {
outputs.upToDateWhen { false }
}

@TaskAction
def run() {

def json = [
version: 2,
min_java_version: 8,
libraries: [
client: [],
common: [],
server: [],
development: [],
]
]

configurations.get().each { side, name ->
def resolvedArtifacts = project.configurations.getByName(name).resolvedConfiguration.resolvedArtifacts
for (final def artifact in resolvedArtifacts) {
def library = [name: artifact.moduleVersion.toString(), url: "https://maven.fabricmc.net/"]
library.putAll(resolveHashes(artifact.moduleVersion.toString()))

json.libraries[side].add(library)
}
}

json.putAll(options.get())
getOutputFile().get().asFile.text = JsonOutput.prettyPrint(JsonOutput.toJson(json))
}

Map<String, Object> resolveHashes(String artifact) {
if (artifact.startsWith("net.minecraft:launchwrapper")) {
// Launch wrapper only has sha1 hashes on its maven.
return [
md5: resolveHash(artifact, "sha1"),
size: resolveSize(artifact),
]
}

return [
md5: resolveHash(artifact, "md5"),
sha1: resolveHash(artifact, "sha1"),
sha256: resolveHash(artifact, "sha256"),
sha512: resolveHash(artifact, "sha512"),
size: resolveSize(artifact),
]
}

String resolveHash(String artifact, String hash) {
def config = project.configurations.detachedConfiguration(project.dependencies.create("${artifact}@jar.${hash}"))
return config.singleFile.text
}

long resolveSize(String artifact) {
def config = project.configurations.detachedConfiguration(project.dependencies.create("${artifact}@jar"))
return config.singleFile.size()
}
}
88 changes: 88 additions & 0 deletions gradle/launcher.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import groovy.json.JsonOutput
import groovy.json.JsonSlurper

// A task to install the development version of Fabric Loader to the Minecraft launcher
tasks.register("installDevelopmentVersion", InstallDevelopmentVersionTask) {
installerJson = file("src/main/resources/fabric-installer.json")
loaderJar = proguardJar.outputs.files.singleFile
dependsOn proguardJar
}

abstract class InstallDevelopmentVersionTask extends DefaultTask {
@Input
@Option(option = "mc-version", description = "The Minecraft version to install")
abstract Property<String> getMinecraftVersion()

@Input
@Option(option = "version-name", description = "The profile name to use")
abstract Property<String> getVersionName()

@InputFile
abstract RegularFileProperty getInstallerJson()

@InputFile
abstract RegularFileProperty getLoaderJar()

@OutputFile
abstract RegularFileProperty getVersionJsonFile()

@OutputFile
abstract RegularFileProperty getLoaderLibraryFile()

InstallDevelopmentVersionTask() {
versionName.convention("fabric-loader-${project.version.toString()}")
minecraftVersion.convention("1.20.2")
versionJsonFile.set(project.layout.file(project.provider {
new File(getDotMinecraftDirectory(), "versions/${versionName.get()}/${versionName.get()}.json")
}))
loaderLibraryFile.set(project.layout.file(project.provider {
new File(getDotMinecraftDirectory(), "libraries/net/fabricmc/fabric-loader/${project.version.toString()}/fabric-loader-${project.version.toString()}.jar")
}))
}

@TaskAction
void runTask() {
loaderLibraryFile.get().getAsFile().parentFile.mkdirs()
loaderLibraryFile.get().getAsFile().bytes = loaderJar.get().getAsFile().bytes

def installerJson = new JsonSlurper().parse(installerJson.get().getAsFile())

// Creates the versions json for the Minecraft launcher
def versionMeta = [
id: versionName.get(),
time: new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
releaseTime: new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
type: "release",
inheritsFrom: minecraftVersion.get(),
mainClass: installerJson.mainClasses.client,
libraries: installerJson.libraries.common,
]

versionMeta.libraries.add([
name: "net.fabricmc:fabric-loader:${project.version.toString()}",
url: "https://maven.fabricmc.net/",
])

versionMeta.libraries.add([
name: "net.fabricmc:intermediary:${minecraftVersion.get()}",
url: "https://maven.fabricmc.net/",
])

versionJsonFile.get().getAsFile().parentFile.mkdirs()
versionJsonFile.get().asFile.text = JsonOutput.prettyPrint(JsonOutput.toJson(versionMeta))

project.logger.lifecycle("Installed Fabric Loader ${project.version.toString()} for Minecraft ${minecraftVersion.get()} as ${versionName.get()}")
}

static File getDotMinecraftDirectory() {
def os = System.getProperty("os.name").toLowerCase(Locale.ROOT)
if (os.contains("win")) {
return new File(System.getenv("APPDATA"), ".minecraft")
} else if (os.contains("mac")) {
return new File(System.getProperty("user.home"), "Library/Application Support/minecraft")
} else {
return new File(System.getProperty("user.home"), ".minecraft")
}
}
}

Loading

0 comments on commit 41e71f0

Please sign in to comment.