Compare commits

...

2 commits

Author SHA1 Message Date
moehreag 3c1ddb496e automatically download assets
- format
2024-07-02 21:21:49 +02:00
moehreag 47b6d93b18 refactor clear cache tasks 2024-07-02 20:47:40 +02:00
9 changed files with 67 additions and 87 deletions

View file

@ -63,8 +63,12 @@ class PhytotelmaPlugin : Plugin<Project> {
c.isCanBeConsumed = false c.isCanBeConsumed = false
when (conf.dependencyType) { when (conf.dependencyType) {
DependencyType.RUNTIME -> project.configurations.getByName(JavaPlugin.RUNTIME_ELEMENTS_CONFIGURATION_NAME).extendsFrom(c) DependencyType.RUNTIME -> project.configurations.getByName(JavaPlugin.RUNTIME_ELEMENTS_CONFIGURATION_NAME)
DependencyType.COMPILE -> project.configurations.getByName(JavaPlugin.API_ELEMENTS_CONFIGURATION_NAME).extendsFrom(c) .extendsFrom(c)
DependencyType.COMPILE -> project.configurations.getByName(JavaPlugin.API_ELEMENTS_CONFIGURATION_NAME)
.extendsFrom(c)
DependencyType.NONE -> {} DependencyType.NONE -> {}
} }
} }
@ -88,31 +92,5 @@ class PhytotelmaPlugin : Plugin<Project> {
companion object { companion object {
lateinit var globalCacheDir: Path lateinit var globalCacheDir: Path
val remappedConfigurationNames = mapOf(
JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME to listOf(
JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME,
JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME,
JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME,
JavaPlugin.TEST_COMPILE_CLASSPATH_CONFIGURATION_NAME
),
JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME to listOf(
JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME,
JavaPlugin.TEST_COMPILE_CLASSPATH_CONFIGURATION_NAME
),
JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME to listOf(
JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME,
JavaPlugin.TEST_COMPILE_CLASSPATH_CONFIGURATION_NAME
),
JavaPlugin.TEST_COMPILE_ONLY_CONFIGURATION_NAME to listOf(
JavaPlugin.TEST_COMPILE_CLASSPATH_CONFIGURATION_NAME
),
JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME to listOf(
JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME,
JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME,
),
JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME to listOf(
JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME,
)
)
} }
} }

View file

@ -1,3 +1,5 @@
@file:Suppress("unused")
package dev.frogmc.phytotelma package dev.frogmc.phytotelma
import com.google.gson.Gson import com.google.gson.Gson
@ -5,9 +7,11 @@ import com.google.gson.GsonBuilder
import dev.frogmc.phytotelma.common.CachingHttpClient import dev.frogmc.phytotelma.common.CachingHttpClient
import org.gradle.api.Project import org.gradle.api.Project
import java.net.URI import java.net.URI
import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.* import kotlin.io.path.createDirectories
import kotlin.io.path.exists
import kotlin.io.path.writeBytes
import kotlin.io.path.writeText
object VersionChecker { object VersionChecker {
private var validVersions = mutableListOf<VersionUrl>() private var validVersions = mutableListOf<VersionUrl>()
@ -77,17 +81,6 @@ object VersionChecker {
return fetchVersionData(version).downloads.client return fetchVersionData(version).downloads.client
} }
fun downloadAssetIndex(version: String, path: Path): String {
val index = fetchVersionData(version).assetIndex
val dest = path.resolve(index.id + ".json")
if (dest.notExists()) {
CachingHttpClient.getUncached(URI.create(index.url)).use {
Files.copy(it, dest)
}
}
return index.id
}
private fun rawDownload(url: String): ByteArray { private fun rawDownload(url: String): ByteArray {
return CachingHttpClient.getUncached(URI.create(url)).readAllBytes() return CachingHttpClient.getUncached(URI.create(url)).readAllBytes()
} }

View file

@ -21,11 +21,13 @@ import org.gradle.api.Project
import org.gradle.api.model.ObjectFactory import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.plugins.JavaPlugin import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.Delete
import org.gradle.configurationcache.extensions.capitalized import org.gradle.configurationcache.extensions.capitalized
import org.jetbrains.java.decompiler.main.Fernflower import org.jetbrains.java.decompiler.main.Fernflower
import org.jetbrains.java.decompiler.main.decompiler.PrintStreamLogger import org.jetbrains.java.decompiler.main.decompiler.PrintStreamLogger
import org.jetbrains.java.decompiler.main.decompiler.SingleFileSaver import org.jetbrains.java.decompiler.main.decompiler.SingleFileSaver
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences
import java.io.OutputStream
import java.io.PrintStream import java.io.PrintStream
import java.nio.file.FileSystems import java.nio.file.FileSystems
import java.nio.file.Files import java.nio.file.Files
@ -98,7 +100,7 @@ abstract class PhytotelmaGradleExtensionImpl @Inject constructor(
project.tasks.register(Constants.DOWNLOAD_ASSETS_TASK) { task -> project.tasks.register(Constants.DOWNLOAD_ASSETS_TASK) { task ->
task.group = Constants.TASK_GROUP task.group = Constants.TASK_GROUP
task.doFirst { task.doFirst {
AssetDownloader.download(it.project) AssetDownloader.download(it.project, true)
} }
} }
@ -173,18 +175,14 @@ abstract class PhytotelmaGradleExtensionImpl @Inject constructor(
} }
} }
project.tasks.register(Constants.CLEAR_LOCAL_CACHE_TASK) { task -> project.tasks.register(Constants.CLEAR_LOCAL_CACHE_TASK, Delete::class.java) { task ->
task.group = Constants.TASK_GROUP task.group = Constants.TASK_GROUP
task.actions.add { task.delete = setOf(ProjectStorage.get(project).localCacheDir)
clearLocalCache()
}
} }
project.tasks.register(Constants.CLEAR_GLOBAL_CACHE_TASK) { task -> project.tasks.register(Constants.CLEAR_GLOBAL_CACHE_TASK, Delete::class.java) { task ->
task.group = Constants.TASK_GROUP task.group = Constants.TASK_GROUP
task.actions.add { task.delete = setOf(PhytotelmaPlugin.globalCacheDir)
clearGlobalCache()
}
} }
project.afterEvaluate { project.afterEvaluate {
@ -192,17 +190,10 @@ abstract class PhytotelmaGradleExtensionImpl @Inject constructor(
} }
} }
@OptIn(ExperimentalPathApi::class)
private fun clearLocalCache() {
ProjectStorage.get(project).localCacheDir?.deleteRecursively()
}
@OptIn(ExperimentalPathApi::class)
private fun clearGlobalCache() {
PhytotelmaPlugin.globalCacheDir.deleteRecursively()
}
private fun remapModDependencies() { private fun remapModDependencies() {
val out = System.out
// Mute the output from thyroxine as there may be a lot of remapping operations here
System.setOut(PrintStream(OutputStream.nullOutputStream()))
ModConfigurations.configurations.forEach { conf -> ModConfigurations.configurations.forEach { conf ->
val artifacts = project.configurations.getByName("mod" + conf.name.capitalized()) val artifacts = project.configurations.getByName("mod" + conf.name.capitalized())
.resolvedConfiguration.resolvedArtifacts .resolvedConfiguration.resolvedArtifacts
@ -231,7 +222,8 @@ abstract class PhytotelmaGradleExtensionImpl @Inject constructor(
val groupname = (group + "_" + name).replace(".", "_") val groupname = (group + "_" + name).replace(".", "_")
val version = artifact.moduleVersion.id.version val version = artifact.moduleVersion.id.version
val classifier = artifact.classifier val classifier = artifact.classifier
val remappedPath = targetPath.resolve(groupname).resolve(version).resolve(groupname+"-"+version+(classifier?.let { "-$it" }?:"")+".jar") val remappedPath = targetPath.resolve(groupname).resolve(version)
.resolve(groupname + "-" + version + (classifier?.let { "-$it" } ?: "") + ".jar")
remappedPath.createParentDirectories() remappedPath.createParentDirectories()
remappedPaths.add(remappedPath) remappedPaths.add(remappedPath)
@ -250,15 +242,17 @@ abstract class PhytotelmaGradleExtensionImpl @Inject constructor(
Thyroxine.remap(mappings, artifact.file.toPath(), remappedPath, false, false) Thyroxine.remap(mappings, artifact.file.toPath(), remappedPath, false, false)
project.dependencies.add(target.name, "dev.frogmc.phytotelma.remapped_mods:$groupname:$version"+(classifier?.let { ":$it" }?:"")) project.dependencies.add(
target.name,
"dev.frogmc.phytotelma.remapped_mods:$groupname:$version" + (classifier?.let { ":$it" } ?: "")
)
} }
} }
System.setOut(out)
} }
override fun minecraft(action: Action<MinecraftConfiguration>) { override fun minecraft(action: Action<MinecraftConfiguration>) {
println("Setting up Tasks...")
setupTasks() setupTasks()
println("Setting up Minecraft...")
val mcConf = objects.newInstance(MinecraftConfiguration::class.java) val mcConf = objects.newInstance(MinecraftConfiguration::class.java)
action.execute(mcConf) action.execute(mcConf)
@ -281,7 +275,6 @@ abstract class PhytotelmaGradleExtensionImpl @Inject constructor(
projectData.localCacheDir!!.resolve("net/minecraft/client/$version/client-$version-remapped.jar") projectData.localCacheDir!!.resolve("net/minecraft/client/$version/client-$version-remapped.jar")
remappedJar.createParentDirectories() remappedJar.createParentDirectories()
projectData.remappedGameJarPath = remappedJar projectData.remappedGameJarPath = remappedJar
println("Time to setup Minecraft!")
val applyAW = AccessWidener.needsUpdate(project) val applyAW = AccessWidener.needsUpdate(project)
if (remappedJar.notExists() || applyAW || mcConf.mappingsName != projectData.mappingsName) { if (remappedJar.notExists() || applyAW || mcConf.mappingsName != projectData.mappingsName) {
projectData.mappingsName = mcConf.mappingsName projectData.mappingsName = mcConf.mappingsName
@ -303,8 +296,8 @@ abstract class PhytotelmaGradleExtensionImpl @Inject constructor(
VersionChecker.savePomFile(version, remappedJar.parent) VersionChecker.savePomFile(version, remappedJar.parent)
project.dependencies.add(Constants.MINECRAFT_CONFIGURATION, "net.minecraft:client:$version:remapped") project.dependencies.add(Constants.MINECRAFT_CONFIGURATION, "net.minecraft:client:$version:remapped")
println("Generating run configurations...")
RunConfigGenerator.generate(project) RunConfigGenerator.generate(project)
AssetDownloader.download(project)
if (applyAW) { if (applyAW) {
project.afterEvaluate { project.afterEvaluate {

View file

@ -9,43 +9,59 @@ import dev.frogmc.phytotelma.common.CachingHttpClient
import org.gradle.api.Project import org.gradle.api.Project
import java.net.URI import java.net.URI
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.createDirectories import kotlin.io.path.createDirectories
import kotlin.io.path.notExists
import kotlin.io.path.reader import kotlin.io.path.reader
object AssetDownloader { object AssetDownloader {
private const val ASSETS_URL = "https://resources.download.minecraft.net" private const val ASSETS_URL = "https://resources.download.minecraft.net"
fun download(project: Project) { fun download(project: Project, manualInvocation: Boolean = false) {
val version = ProjectStorage.get(project).minecraftVersion!! val version = ProjectStorage.get(project).minecraftVersion!!
val path = PhytotelmaPlugin.globalCacheDir.resolve("assets") val path = PhytotelmaPlugin.globalCacheDir.resolve("assets")
val id = VersionChecker.downloadAssetIndex(version, path.resolve("indexes")) val assetIndex = VersionChecker.fetchVersionData(version).assetIndex
val dest = path.resolve("indexes").resolve(assetIndex.id + ".json")
var overwrite = manualInvocation
if (dest.notExists()) {
CachingHttpClient.getUncached(URI.create(assetIndex.url)).use {
Files.copy(it, dest)
}
overwrite = true
}
val index = Gson().fromJson( val index = Gson().fromJson(
path.resolve("indexes").resolve("$id.json").reader(StandardCharsets.UTF_8), path.resolve("indexes").resolve("${assetIndex.id}.json").reader(StandardCharsets.UTF_8),
JsonObject::class.java JsonObject::class.java
) )
val objectsPath = path.resolve("objects") val objectsPath = path.resolve("objects")
objectsPath.createDirectories() objectsPath.createDirectories()
val entries = index.getAsJsonObject("objects").entrySet() val entries = index.getAsJsonObject("objects").entrySet()
val totalSize = entries.size val totalSize = entries.size
if (overwrite) {
print("Downloading $totalSize assets... ()") print("Downloading $totalSize assets... ()")
System.out.flush() System.out.flush()
}
entries.forEachIndexed { i, it -> entries.forEachIndexed { i, it ->
val hash = it.value.asJsonObject.get("hash").asString val hash = it.value.asJsonObject.get("hash").asString
/*val size = it.value.asJsonObject.get("size").asInt /*val size = it.value.asJsonObject.get("size").asInt
val name = it.key*/ val name = it.key*/
// TODO asset downloading for versions <=1.7 (legacy) // TODO asset downloading for versions <=1.7 (legacy)
if (overwrite) {
print("\rDownloading $totalSize assets... ($i/$totalSize): $hash" + " ".repeat(30)) print("\rDownloading $totalSize assets... ($i/$totalSize): $hash" + " ".repeat(30))
System.out.flush() System.out.flush()
get(objectsPath, hash)
} }
get(objectsPath, hash, overwrite)
}
if (overwrite) {
println("\rDownloading $totalSize assets... Done!" + " ".repeat(50)) println("\rDownloading $totalSize assets... Done!" + " ".repeat(50))
} }
}
private fun get(localDir: Path, hash: String) { private fun get(localDir: Path, hash: String, overwrite: Boolean) {
val shortHash = hash.substring(0, 2) val shortHash = hash.substring(0, 2)
val path = localDir.resolve("$shortHash/$hash") val path = localDir.resolve("$shortHash/$hash")
CachingHttpClient.downloadTo(URI.create("$ASSETS_URL/$shortHash/$hash"), path, true) CachingHttpClient.downloadTo(URI.create("$ASSETS_URL/$shortHash/$hash"), path, overwrite)
} }
} }

View file

@ -34,7 +34,7 @@ object RunConfigGenerator {
assetIndexPath.createDirectories() assetIndexPath.createDirectories()
} }
val projectData = ProjectStorage.get(project) val projectData = ProjectStorage.get(project)
val indexId = VersionChecker.downloadAssetIndex(projectData.minecraftVersion!!, assetIndexPath) val indexId = VersionChecker.fetchVersionData(projectData.minecraftVersion!!).assetIndex.id
val projectName = if (project.rootDir == project.projectDir) "" else " (${project.name})" val projectName = if (project.rootDir == project.projectDir) "" else " (${project.name})"

View file

@ -29,7 +29,7 @@ abstract class RunGameTask @Inject constructor(env: Env) : JavaExec() {
if (assetIndexPath.notExists()) { if (assetIndexPath.notExists()) {
assetIndexPath.createDirectories() assetIndexPath.createDirectories()
} }
val indexId = VersionChecker.downloadAssetIndex(ProjectStorage.get(project).minecraftVersion!!, assetIndexPath) val indexId = VersionChecker.fetchVersionData(ProjectStorage.get(project).minecraftVersion!!).assetIndex.id
val log4jPath = project.rootDir.resolve(".gradle/phytotelma/log4j.xml").toPath().absolute() val log4jPath = project.rootDir.resolve(".gradle/phytotelma/log4j.xml").toPath().absolute()
if (log4jPath.notExists()) { if (log4jPath.notExists()) {
log4jPath.createParentDirectories() log4jPath.createParentDirectories()