-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from exejar/master
Mappings and version update to Weave-Gradle
- Loading branch information
Showing
13 changed files
with
223 additions
and
81,636 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
org.gradle.jvmargs = -Xmx2G | ||
# Project properties | ||
projectName = Weave-Gradle | ||
projectGroup = net.weavemc | ||
projectVersion = 1.0.0 | ||
projectGroup = net.weavemc.gradle | ||
projectVersion = 1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package net.weavemc.gradle | ||
|
||
import com.grappenmaker.mappings.remapJar | ||
import kotlinx.serialization.encodeToString | ||
import net.weavemc.gradle.configuration.WeaveMinecraftExtension | ||
import net.weavemc.gradle.configuration.pullDeps | ||
import net.weavemc.gradle.util.Constants | ||
import net.weavemc.gradle.util.mappedJarCache | ||
import net.weavemc.gradle.util.minecraftJarCache | ||
import net.weavemc.internals.MappingsRetrieval | ||
import net.weavemc.internals.MinecraftVersion | ||
import net.weavemc.internals.ModConfig | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.GradleException | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.plugins.JavaPlugin | ||
import org.gradle.api.tasks.InputFile | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.kotlin.dsl.apply | ||
import org.gradle.kotlin.dsl.create | ||
import org.gradle.kotlin.dsl.get | ||
import org.gradle.kotlin.dsl.withType | ||
import org.gradle.language.jvm.tasks.ProcessResources | ||
import java.io.File | ||
|
||
/** | ||
* Gradle build system plugin used to automate the setup of a modding environment. | ||
*/ | ||
class WeaveGradle : Plugin<Project> { | ||
/** | ||
* [Plugin.apply] | ||
* | ||
* @param project The target project. | ||
*/ | ||
override fun apply(project: Project) { | ||
// Applying our default plugins | ||
project.pluginManager.apply(JavaPlugin::class) | ||
|
||
val ext = project.extensions.create("minecraft", WeaveMinecraftExtension::class) | ||
|
||
project.afterEvaluate { | ||
if (!ext.configuration.isPresent) throw GradleException( | ||
"Configuration is missing, make sure to add a configuration through the minecraft {} block!" | ||
) | ||
|
||
if (!ext.version.isPresent) throw GradleException( | ||
"Set a Minecraft version through the minecraft {} block!" | ||
) | ||
|
||
val version = ext.version.getOrElse(MinecraftVersion.V1_8_9) | ||
pullDeps(version, ext.configuration.get().namespace) | ||
} | ||
|
||
project.tasks.withType<ProcessResources>().configureEach { | ||
doLast { | ||
val config = ext.configuration.get().copy(compiledFor = ext.version.get().versionName) | ||
destinationDir.resolve("weave.mod.json").writeText(Constants.JSON.encodeToString(config)) | ||
} | ||
} | ||
|
||
val remapJarTask = project.tasks.register("remapJar", RemapJarTask::class.java) { | ||
val version = ext.version.getOrElse(MinecraftVersion.V1_8_9) | ||
minecraftJar = version.mappedJarCache(ext.configuration.get().namespace) | ||
inputJar = project.tasks["jar"].outputs.files.singleFile | ||
outputJar = inputJar.parentFile.resolve("${inputJar.nameWithoutExtension}-mapped.${inputJar.extension}") | ||
} | ||
|
||
project.tasks.named("assemble") { finalizedBy(remapJarTask) } | ||
} | ||
|
||
/** | ||
* Remaps the jar back to vanilla mappings (obfuscated) | ||
* The jar will then be mapped when loaded by Weave-Loader | ||
*/ | ||
open class RemapJarTask: DefaultTask() { | ||
@Internal | ||
lateinit var minecraftJar: File | ||
|
||
@InputFile | ||
lateinit var inputJar: File | ||
|
||
@OutputFile | ||
lateinit var outputJar: File | ||
|
||
@TaskAction | ||
fun remap() { | ||
val ext = project.extensions["minecraft"] as WeaveMinecraftExtension | ||
val version = ext.version.get() | ||
val fullMappings = version.loadMergedMappings() | ||
|
||
val mid = ext.configuration.get().namespace | ||
require(mid in fullMappings.namespaces) { | ||
"Namespace $mid is not available in mappings! Available namespaces are: ${fullMappings.namespaces}" | ||
} | ||
|
||
remapJar(fullMappings, inputJar, outputJar, mid, "official", files = listOf(minecraftJar)) | ||
} | ||
} | ||
} | ||
|
||
fun MinecraftVersion.loadMergedMappings() = | ||
MappingsRetrieval.loadMergedWeaveMappings(versionName, minecraftJarCache).mappings |
98 changes: 20 additions & 78 deletions
98
src/main/kotlin/net/weavemc/gradle/configuration/DependencyManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,71 @@ | ||
package net.weavemc.gradle.configuration | ||
|
||
import com.grappenmaker.mappings.remapJar | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.decodeFromString | ||
import net.weavemc.gradle.util.AccessWidener | ||
import net.weavemc.gradle.loadMergedMappings | ||
import net.weavemc.gradle.util.Constants | ||
import net.weavemc.gradle.util.DownloadUtil | ||
import net.weavemc.gradle.util.mappedJarCache | ||
import net.weavemc.gradle.util.minecraftJarCache | ||
import net.weavemc.internals.MinecraftVersion | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.maven | ||
import org.objectweb.asm.ClassReader | ||
import org.objectweb.asm.ClassWriter | ||
import org.objectweb.asm.commons.ClassRemapper | ||
import java.io.File | ||
import java.net.URL | ||
import java.util.jar.JarEntry | ||
import java.util.jar.JarFile | ||
import java.util.jar.JarOutputStream | ||
|
||
private inline fun <reified T> String?.decodeJSON() = | ||
if (this != null) Constants.JSON.decodeFromString<T>(this) else null | ||
|
||
/** | ||
* Pulls dependencies from [addMinecraftAssets] and [addMappedMinecraft] | ||
*/ | ||
fun Project.pullDeps(version: MinecraftVersion) { | ||
fun Project.pullDeps(version: MinecraftVersion, namespace: String) { | ||
addMinecraftAssets(version) | ||
addMappedMinecraft(version) | ||
addMappedMinecraft(version, namespace) | ||
} | ||
|
||
/** | ||
* Adds Minecraft as a dependency by providing the jar to the projects file tree. | ||
*/ | ||
private fun Project.addMinecraftAssets(version: MinecraftVersion) { | ||
val manifest = DownloadUtil.fetch(Constants.VERSION_MANIFEST).decodeJSON<VersionManifest>() ?: return | ||
val versionEntry = manifest.versions.find { it.id == version.id } ?: return | ||
val versionEntry = manifest.versions.find { it.id == version.versionName } ?: return | ||
val versionInfo = DownloadUtil.fetch(versionEntry.url).decodeJSON<VersionInfo>() ?: return | ||
|
||
val client = versionInfo.downloads.client | ||
DownloadUtil.downloadAndChecksum(URL(client.url), client.sha1, version.minecraftJarCache.toPath()) | ||
DownloadUtil.checksumAndDownload(URL(client.url), client.sha1, version.minecraftJarCache.toPath()) | ||
|
||
repositories.maven("https://libraries.minecraft.net/") | ||
repositories.maven { | ||
name = "mojang" | ||
setUrl("https://libraries.minecraft.net/") | ||
} | ||
|
||
versionInfo.libraries.filter { "twitch-platform" !in it.name && "twitch-external" !in it.name } | ||
.forEach { dependencies.add("compileOnly", it.name) } | ||
} | ||
|
||
/** | ||
* Adds the mapped Minecraft JAR to the project's dependencies. | ||
* | ||
* @param version The Minecraft version for which to add the mapped JAR. | ||
*/ | ||
private fun Project.addMappedMinecraft(version: MinecraftVersion) = runCatching { | ||
val mapped = File(version.cacheDirectory, "minecraft-mapped.jar") | ||
if (mapped.exists()) { | ||
dependencies.add("compileOnly", files(mapped)) | ||
return@runCatching | ||
} | ||
|
||
val remapper = createMinecraftRemapper(version) | ||
|
||
JarFile(version.minecraftJarCache).use { mcJar -> | ||
JarOutputStream(mapped.outputStream()).use { outputStream -> | ||
for (entry in mcJar.entries()) { | ||
if (!entry.name.endsWith(".class")) continue | ||
|
||
val reader = ClassReader(mcJar.getInputStream(entry)) | ||
val cw = ClassWriter(0) | ||
reader.accept(AccessWidener(ClassRemapper(cw, remapper)), 0) | ||
|
||
val mappedName = remapper.map(reader.className) ?: reader.className | ||
outputStream.putNextEntry(JarEntry("$mappedName.class")) | ||
outputStream.write(cw.toByteArray()) | ||
} | ||
} | ||
private fun Project.addMappedMinecraft(version: MinecraftVersion, namespace: String) = runCatching { | ||
val mapped = version.mappedJarCache(namespace) | ||
if (!mapped.exists()) { | ||
val fullMappings = version.loadMergedMappings() | ||
remapJar(fullMappings, version.minecraftJarCache, mapped, to = namespace) | ||
} | ||
|
||
dependencies.add("compileOnly", files(mapped)) | ||
dependencies.add("compileOnly", project.files(mapped)) | ||
}.onFailure { it.printStackTrace() } | ||
|
||
/** | ||
* Represents a version manifest containing a list of manifest versions | ||
* | ||
* @property versions The list of manifest versions | ||
*/ | ||
@Serializable | ||
private data class VersionManifest(val versions: List<ManifestVersion>) | ||
|
||
/** | ||
* Represents a manifest version with an ID and URL. | ||
* | ||
* @property id The ID of the manifest version. | ||
* @property url The URL of the manifest version. | ||
*/ | ||
@Serializable | ||
private data class ManifestVersion(val id: String, val url: String) | ||
|
||
/** | ||
* Represents version information containing downloads and a list of libraries. | ||
* | ||
* @property downloads The version downloads. | ||
* @property libraries The list of libraries. | ||
*/ | ||
@Serializable | ||
private data class VersionInfo(val downloads: VersionDownloads, val libraries: List<Library>) | ||
|
||
/** | ||
* Represents version downloads containing the client download. | ||
* | ||
* @property client The client download. | ||
*/ | ||
@Serializable | ||
private data class VersionDownloads(val client: VersionDownload) | ||
|
||
/** | ||
* Represents a version download with a URL and SHA1 hash. | ||
* | ||
* @property url The URL of the version download. | ||
* @property sha1 The SHA1 hash of the version download. | ||
*/ | ||
@Serializable | ||
private data class VersionDownload(val url: String, val sha1: String) | ||
|
||
/** | ||
* Represents a library with a name. | ||
* | ||
* @property name The name of the library. | ||
*/ | ||
@Serializable | ||
private data class Library(val name: String) | ||
private data class Library(val name: String) |
Oops, something went wrong.