Skip to content

Commit

Permalink
Add global & local caches, use symlinks where possible, use capabilit…
Browse files Browse the repository at this point in the history
…ies for stubs
  • Loading branch information
MsRandom committed Dec 23, 2024
1 parent bd35869 commit 1cba44c
Show file tree
Hide file tree
Showing 19 changed files with 334 additions and 159 deletions.
19 changes: 1 addition & 18 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ subprojects {
apply(plugin = "java")

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(11))
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
withSourcesJar()
withJavadocJar()
}
Expand Down Expand Up @@ -54,23 +54,6 @@ childProjects.values.forEach { project ->
}
}

tasks.compileKotlin {
kotlinOptions {
jvmTarget = "11"

freeCompilerArgs =
listOf(
"-opt-in=kotlin.ExperimentalStdlibApi",
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
"-Xunrestricted-builder-inference",
"-Xjvm-default=all",
)

apiVersion = "1.6"
languageVersion = "1.6"
}
}

tasks.test {
maxHeapSize = "3G"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package net.msrandom.minecraftcodev.accesswidener

import net.msrandom.minecraftcodev.core.resolve.isCodevGeneratedMinecraftJar
import net.msrandom.minecraftcodev.core.utils.*
import net.msrandom.minecraftcodev.core.utils.cacheExpensiveOperation
import net.msrandom.minecraftcodev.core.utils.getAsPath
import net.msrandom.minecraftcodev.core.utils.getLocalCacheDirectoryProvider
import net.msrandom.minecraftcodev.core.utils.toPath
import net.msrandom.minecraftcodev.core.utils.tryLink
import net.msrandom.minecraftcodev.core.utils.walk
import net.msrandom.minecraftcodev.core.utils.zipFileSystem
import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.transform.*
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.FileSystemLocation
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.*
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.Opcodes
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import javax.inject.Inject
import kotlin.io.path.*

@CacheableTask
Expand All @@ -37,6 +42,12 @@ abstract class AccessWiden : DefaultTask() {
abstract val outputFile: RegularFileProperty
@OutputFile get

abstract val cacheDirectory: DirectoryProperty
@Internal get

abstract val objectFactory: ObjectFactory
@Inject get

init {
outputFile.convention(
project.layout.file(
Expand All @@ -45,18 +56,23 @@ abstract class AccessWiden : DefaultTask() {
},
),
)
}

@TaskAction
fun accessWiden() {
outputFile.getAsPath().deleteIfExists()
cacheDirectory.set(getLocalCacheDirectoryProvider(project))
}

private fun accessWiden(outputPath: Path) {
val input = inputFile.get().toPath()

if (accessWideners.isEmpty) {
outputPath.tryLink(input)

return
}

val accessModifiers = loadAccessWideners(accessWideners, namespace.takeIf(Property<*>::isPresent)?.get())

zipFileSystem(input).use { inputZip ->
zipFileSystem(outputFile.getAsPath(), true).use { outputZip ->
zipFileSystem(outputPath, true).use { outputZip ->
inputZip.getPath("/").walk {
for (path in filter(Path::isRegularFile)) {
val name = path.toString()
Expand Down Expand Up @@ -86,4 +102,16 @@ abstract class AccessWiden : DefaultTask() {
}
}
}

@TaskAction
fun accessWiden() {
val cacheKey = objectFactory.fileCollection().apply {
from(accessWideners)
from(inputFile)
}

cacheExpensiveOperation(cacheDirectory.getAsPath(), "access-widened", cacheKey, outputFile.getAsPath()) {
accessWiden(it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package net.msrandom.minecraftcodev.core

import net.msrandom.minecraftcodev.core.resolve.MinecraftVersionList
import net.msrandom.minecraftcodev.core.resolve.getAllDependencies
import net.msrandom.minecraftcodev.core.resolve.getClientDependencies
import net.msrandom.minecraftcodev.core.resolve.setupCommon
import org.gradle.api.artifacts.CacheableRule
import org.gradle.api.artifacts.ComponentMetadataContext
import org.gradle.api.artifacts.ComponentMetadataRule
import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.Category
import org.gradle.api.model.ObjectFactory
import org.gradle.api.artifacts.Dependency
import java.io.File
import java.nio.file.Path
import javax.inject.Inject
Expand All @@ -26,41 +23,66 @@ fun getVersionList(
@CacheableRule
abstract class MinecraftComponentMetadataRule<T : Any> @Inject constructor(
private val cacheDirectory: File,
private val version: String,
private val versions: List<String>,
private val versionManifestUrl: String,
private val isOffline: Boolean,
private val client: Boolean,

private val attribute: Attribute<String>,
private val commonAttributeValue: String,
private val clientAttributeValue: String,
private val commonCapability: String,
private val clientCapability: String,
) : ComponentMetadataRule {
abstract val objectFactory: ObjectFactory
@Inject get

override fun execute(context: ComponentMetadataContext) {
val variantName = if (client) {
clientAttributeValue
} else {
commonAttributeValue
}
private fun ComponentMetadataContext.addVariantDependencies(capabilityName: String, client: Boolean) {
details.addVariant(capabilityName, Dependency.DEFAULT_CONFIGURATION) { variant ->
variant.withCapabilities {
for (capability in it.capabilities) {
it.removeCapability(capability.group, capability.name)
}

context.details.addVariant(variantName) {
it.attributes { attributes ->
attributes
.attribute(Category.CATEGORY_ATTRIBUTE, objectFactory.named(Category::class.java, Category.REGULAR_PLATFORM))
.attribute(attribute, variantName)
it.addCapability("net.msrandom", capabilityName, "0.0.0")
}

it.withDependencies { dependencies ->
val versionMetadata = getVersionList(cacheDirectory.toPath(), versionManifestUrl, isOffline).version(version)
variant.withDependencies { dependencies ->
val versionList = getVersionList(cacheDirectory.toPath(), versionManifestUrl, isOffline)

val versionDependencies = versions.map {
val versionMetadata = versionList.version(versions[0])

if (client) {
getAllDependencies(versionMetadata).forEach(dependencies::add)
} else {
setupCommon(cacheDirectory.toPath(), versionMetadata, isOffline).forEach(dependencies::add)
val dependencies = if (client) {
getAllDependencies(versionMetadata)
} else {
setupCommon(cacheDirectory.toPath(), versionMetadata, isOffline)
}

dependencies.map(ModuleLibraryIdentifier::load)
}

val commonDependencies = versionDependencies.reduce { a, b ->
val moduleVersionsA = a.associate {
(it.group to it.module) to it.version
}

val moduleVersionsB = b.associate {
(it.group to it.module) to it.version
}

val commonModules = moduleVersionsA.keys intersect moduleVersionsB.keys

commonModules.map { module ->
val (group, name) = module
val version = minOf(moduleVersionsA.getValue(module), moduleVersionsB.getValue(module))

ModuleLibraryIdentifier(group, name, version, null)
}
}

for (dependency in commonDependencies) {
dependencies.add(dependency.toString())
}
}
}
}

override fun execute(context: ComponentMetadataContext) {
context.addVariantDependencies(commonCapability, false)
context.addVariantDependencies(clientCapability, true)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.msrandom.minecraftcodev.core.resolve

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import net.msrandom.minecraftcodev.core.resolve.bundled.ServerExtractor
import net.msrandom.minecraftcodev.core.resolve.legacy.ServerFixer
import net.msrandom.minecraftcodev.core.utils.zipFileSystem
Expand Down Expand Up @@ -89,6 +87,8 @@ fun getExtractionState(
Files.createFile(bundledMark)
}

addMinecraftMarker(extractedJar)

return ServerExtractionResult(extractedJar, isBundled, commonLibraries)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ package net.msrandom.minecraftcodev.core.resolve
import net.msrandom.minecraftcodev.core.resolve.MinecraftVersionMetadata.Rule.OperatingSystem
import net.msrandom.minecraftcodev.core.resolve.bundled.BundledClientJarSplitter
import net.msrandom.minecraftcodev.core.resolve.legacy.LegacyJarSplitter
import net.msrandom.minecraftcodev.core.utils.clientJarPath
import net.msrandom.minecraftcodev.core.utils.commonJarPath
import net.msrandom.minecraftcodev.core.utils.osName
import net.msrandom.minecraftcodev.core.utils.osVersion
import net.msrandom.minecraftcodev.core.utils.*
import org.apache.commons.lang3.SystemUtils
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import kotlin.io.path.copyTo
import kotlin.io.path.deleteIfExists
import kotlin.io.path.exists
import kotlin.io.path.notExists
Expand All @@ -26,10 +21,7 @@ internal fun setupCommon(
val (extractedServer, isBundled, libraries) = getExtractionState(cacheDirectory, metadata, isOffline)!!

return if (isBundled) {
if (output != null) {
extractedServer.copyTo(output, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES)
addMinecraftMarker(output)
}
output?.tryLink(extractedServer)

libraries
} else {
Expand All @@ -39,10 +31,7 @@ internal fun setupCommon(
LegacyJarSplitter.split(cacheDirectory, metadata, extractedServer, isOffline)
}

if (output != null) {
commonJarPath.copyTo(output)
addMinecraftMarker(output)
}
output?.tryLink(commonJarPath)

libraries + "net.msrandom:side-annotations:1.0.0"
}
Expand All @@ -59,7 +48,7 @@ internal fun setupClient(
val clientJarPath = clientJarPath(cacheDirectory, metadata.id)

if (clientJarPath.exists()) {
clientJarPath.copyTo(output)
output.tryLink(clientJarPath)

return
}
Expand All @@ -73,15 +62,12 @@ internal fun setupClient(

if (isBundled) {
BundledClientJarSplitter.split(cacheDirectory, metadata, extractedServer, isOffline)

clientJarPath.copyTo(output)
addMinecraftMarker(output)
} else {
LegacyJarSplitter.split(cacheDirectory, metadata, extractedServer, isOffline).client

clientJarPath.copyTo(output)
addMinecraftMarker(output)
}

addMinecraftMarker(clientJarPath)
output.tryLink(clientJarPath)
}

fun getAllDependencies(metadata: MinecraftVersionMetadata) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.msrandom.minecraftcodev.core.task
import net.msrandom.minecraftcodev.core.VERSION_MANIFEST_URL
import net.msrandom.minecraftcodev.core.getVersionList
import net.msrandom.minecraftcodev.core.utils.getAsPath
import net.msrandom.minecraftcodev.core.utils.getCacheDirectoryProvider
import net.msrandom.minecraftcodev.core.utils.getGlobalCacheDirectoryProvider
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
Expand Down Expand Up @@ -32,8 +32,8 @@ abstract class CachedMinecraftParameters {
fun convention(project: Project) {
versionManifestUrl.convention(VERSION_MANIFEST_URL)

directory.convention(getCacheDirectoryProvider(project))
isOffline.convention(project.provider { project.gradle.startParameter.isOffline })
directory.set(getGlobalCacheDirectoryProvider(project))
isOffline.set(project.provider { project.gradle.startParameter.isOffline })
}

fun versionList() = getVersionList(directory.getAsPath(), versionManifestUrl.get(), isOffline.get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package net.msrandom.minecraftcodev.core.task
import net.msrandom.minecraftcodev.core.resolve.MinecraftDownloadVariant
import net.msrandom.minecraftcodev.core.resolve.downloadMinecraftFile
import net.msrandom.minecraftcodev.core.utils.getAsPath
import net.msrandom.minecraftcodev.core.utils.tryLink
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import java.nio.file.StandardCopyOption
import kotlin.io.path.copyTo
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import kotlin.io.path.deleteIfExists

@CacheableTask
abstract class ResolveMinecraftMappings : CachedMinecraftTask() {
Expand Down Expand Up @@ -44,6 +47,7 @@ abstract class ResolveMinecraftMappings : CachedMinecraftTask() {
val downloadPath = downloadMinecraftFile(cacheParameters.directory.getAsPath(), version, variant, cacheParameters.isOffline.get())
?: throw IllegalArgumentException("${version.id} does not have variant $variant")

downloadPath.copyTo(output, StandardCopyOption.REPLACE_EXISTING)
output.deleteIfExists()
output.tryLink(downloadPath)
}
}
Loading

0 comments on commit 1cba44c

Please sign in to comment.