Skip to content

Commit

Permalink
Allow composite file operation cache keys, simplify run configs
Browse files Browse the repository at this point in the history
  • Loading branch information
MsRandom committed Jan 19, 2025
1 parent cd10078 commit bf9ba41
Show file tree
Hide file tree
Showing 18 changed files with 316 additions and 309 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ org.gradle.configuration-cache=true
# org.gradle.unsafe.isolated-projects=true

group=net.msrandom
version=0.5.2
version=0.5.4
kotlin.code.style=official
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ abstract class AccessWiden : DefaultTask() {
from(inputFile)
}

cacheExpensiveOperation(cacheDirectory.getAsPath(), "access-widened", cacheKey, outputFile.getAsPath()) {
accessWiden(it)
cacheExpensiveOperation(cacheDirectory.getAsPath(), "access-widened", cacheKey, outputFile.getAsPath()) { (output) ->
accessWiden(output)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ internal fun clientJarPath(
version: String,
) = getVanillaExtractJarPath(cacheDirectory, version, "client")

private val operationLocks = ConcurrentHashMap<Path, Lock>()
private val operationLocks = ConcurrentHashMap<Any, Lock>()

fun cacheExpensiveOperation(
cacheDirectory: Path,
operationName: String,
inputFiles: Iterable<File>,
outputPath: Path,
generate: (Path) -> Unit,
vararg outputPaths: Path,
generate: (List<Path>) -> Unit,
) {
val outputPathsList = outputPaths.toList()

val hashes =
runBlocking {
inputFiles.map {
Expand All @@ -121,34 +123,54 @@ fun cacheExpensiveOperation(
.resolve(operationName)
.resolve(directoryName)

val cachedOutput = cachedOperationDirectoryName.resolve(outputPath.fileName)
var allCached = true

if (cachedOutput.exists()) {
outputPath.deleteIfExists()
outputPath.tryLink(cachedOutput)
for (outputPath in outputPathsList) {
val cachedOutput = cachedOperationDirectoryName.resolve(outputPath.fileName)

if (cachedOutput.exists()) {
outputPath.deleteIfExists()
outputPath.tryLink(cachedOutput)

println("Cache hit for $operationName operation for $outputPath")
println("Cache hit for $operationName operation for $outputPath")
} else {
allCached = false
}
}

if (allCached) {
return
}

println("Cache miss for $operationName operation for $outputPath")
println("Cache miss for $operationName operation for $outputPathsList")

val lock = operationLocks.computeIfAbsent(outputPath) {
val lock = operationLocks.computeIfAbsent(outputPathsList) {
ReentrantLock()
}

lock.withLock {
val temporaryPath =
Files.createTempFile("$operationName-${outputPath.nameWithoutExtension}", ".${outputPath.extension}")
temporaryPath.deleteExisting()
val temporaryPaths = outputPathsList.map { outputPath ->
val temporaryPath = Files.createTempFile("$operationName-${outputPath.nameWithoutExtension}", ".${outputPath.extension}")
temporaryPath.deleteExisting()

temporaryPath
}

generate(temporaryPath)
generate(temporaryPaths)

cachedOperationDirectoryName.createDirectories()
temporaryPath.copyTo(cachedOutput, StandardCopyOption.COPY_ATTRIBUTES)

for ((temporaryPath, outputPath) in temporaryPaths.zip(outputPathsList)) {
val cachedOutput = cachedOperationDirectoryName.resolve(outputPath.fileName)

temporaryPath.copyTo(cachedOutput, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING)
}
}

outputPath.deleteIfExists()
outputPath.tryLink(cachedOutput)
for (outputPath in outputPathsList) {
val cachedOutput = cachedOperationDirectoryName.resolve(outputPath.fileName)

outputPath.deleteIfExists()
outputPath.tryLink(cachedOutput)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import kotlin.io.path.createDirectories

open class FabricRunsDefaultsContainer(private val defaults: RunConfigurationDefaultsContainer) {
private fun defaults(sidedMain: FabricInstaller.MainClass.() -> String) {
defaults.builder.jvmArguments("-Dfabric.development=true")
// defaults.builder.jvmArguments("-Dmixin.env.remapRefMap=true")
defaults.configuration.jvmArguments("-Dfabric.development=true")
// defaults.configuration.jvmArguments("-Dmixin.env.remapRefMap=true")

defaults.builder.action {
defaults.configuration.apply {
val remapClasspathDirectory = project.layout.buildDirectory.dir("fabricRemapClasspath")

mainClass.set(
Expand All @@ -41,14 +41,12 @@ open class FabricRunsDefaultsContainer(private val defaults: RunConfigurationDef
},
)
}

defaults.builder.jvmArguments()
}

fun client(version: Provider<String>) {
defaults(FabricInstaller.MainClass::client)

defaults.builder.action {
defaults.configuration.apply {
val assetIndex =
version.map {
cacheParameters
Expand Down Expand Up @@ -82,9 +80,7 @@ open class FabricRunsDefaultsContainer(private val defaults: RunConfigurationDef
fun server() {
defaults(FabricInstaller.MainClass::server)

defaults.builder.apply {
arguments("nogui")
}
defaults.configuration.arguments("nogui")
}

fun data(
Expand All @@ -93,7 +89,7 @@ open class FabricRunsDefaultsContainer(private val defaults: RunConfigurationDef
) {
client(version)

defaults.builder.action {
defaults.configuration.apply {
val data = project.objects.newInstance(FabricDatagenRunConfigurationData::class.java)

action.execute(data)
Expand All @@ -105,12 +101,10 @@ open class FabricRunsDefaultsContainer(private val defaults: RunConfigurationDef
}

private fun gameTest() {
defaults.builder.apply {
jvmArguments(
"-Dfabric-api.gametest",
"-Dfabric.autoTest",
)
}
defaults.configuration.jvmArguments(
"-Dfabric-api.gametest",
"-Dfabric.autoTest",
)
}

fun gameTestServer() {
Expand Down
Loading

0 comments on commit bf9ba41

Please sign in to comment.