diff options
Diffstat (limited to 'plugin/src/main/kotlin/moe/nea')
6 files changed, 244 insertions, 140 deletions
diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/AbstractTransformerRepository.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/AbstractTransformerRepository.kt index 601cb19..a50552a 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/AbstractTransformerRepository.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/AbstractTransformerRepository.kt @@ -57,7 +57,7 @@ abstract class AbstractTransformerRepository<T : CHashable>( val provider = providers[hash] ?: error("Unregistered archenemy ${this.repoIdentifier} dependeny") val coordinate = getCoordinate(provider.t) require(coordinate.startsWith(identifier.group + ":" + identifier.name + ":" + identifier.version)) - if (identifier.extension == "pom") return Artifact.none() + if (identifier.extension == "pom") return Artifact.none() // TODO: resolve original POM (potentially transformed???) return getArtifact( getNullsafeIdentifier(identifier), provider.t, diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt index d27f948..8c009e0 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt @@ -9,122 +9,136 @@ import java.io.File import java.net.URI abstract class ArchenemyMojangExtension(val project: Project) { - val sharedExtension = project.rootProject.extensions.getByType(ArchenemySharedExtension::class.java) - - private val _registerMinecraftProvider by lazy { - GradleRepositoryAdapter.add( - project.repositories, - "Minecraft Provider", - sharedExtension.getLocalCacheDirectory().resolve("minecraft-provider"), - sharedExtension.minecraftProvider - ) - GradleRepositoryAdapter.add( - project.repositories, - "Minecraft Mapped Provider", - getLocalCacheDirectory().resolve("minecraft-mapped-provider"), - mappedRepositoryProvider - ) - GradleRepositoryAdapter.add( - project.repositories, - "Minecraft Merged Provider", - getLocalCacheDirectory().resolve("minecraft-merged-provider"), - mergedRepositoryProvider - ) - project.repositories.maven { - it.name = "Minecraft Libraries" - it.url = URI("https://libraries.minecraft.net/") - } - } - - private val mappedRepositoryProvider = MappedRepositoryProvider(this) - private val mergedRepositoryProvider = MergedRepositoryProvider(this) - - fun yarnMappings(dependency: Dependency): MappingDependency { - dependency as ModuleDependency - return YarnMappingDependency(this, dependency) - } - - fun officialMappings(version: String, side: MCSide): MappingDependency { - _registerMinecraftProvider - val dependency by lazy { - realize( - sharedExtension.minecraftProvider.getMappingsDependencyCoordinate( - MinecraftProvider.MinecraftCoordinate( - version, - side - ) - ) - ) - } - return OfficialMappingDependency(side, version, project.providers.provider { dependency }).also { it.get() } - } - - fun libraries(version: String): List<String> { - return sharedExtension.getDownloadVersionMetadataTask(version) - .getVersionMetadataNow().libraries - .map { it.name } - .filter { !it.contains("twitch-platform") && !it.contains("twitch-external-platform") } - } - - fun intermediaryMappings(version: String): MappingDependency { - return yarnMappings(project.dependencies.create("net.fabricmc:intermediary:$version:v2")) - } - - fun mergeJar( - base: Dependency, - overlay: Dependency, - ): Dependency { - base as ModuleDependency - overlay as ModuleDependency - _registerMinecraftProvider - return realize( - mergedRepositoryProvider.getCoordinate( - MergedRepositoryProvider.Coordinate(base, overlay) - ) - ) - } - - fun mapJar( - dependency: Dependency, - mappings: MappingDependency, - sourceNamespace: String, - destinationNamespace: String - ): Dependency { - dependency as ModuleDependency - _registerMinecraftProvider - return realize( - mappedRepositoryProvider.getDependencyCoordiante( - MappedRepositoryProvider.MappedCoordinates( - dependency, mappings, sourceNamespace, destinationNamespace - ) - ) - ) - } - - fun minecraft(version: String, side: MCSide): Dependency { - _registerMinecraftProvider - return realize( - sharedExtension.minecraftProvider.getDependencyCoordinate( - MinecraftProvider.MinecraftCoordinate( - version, - side, - ) - ) - ) - } - - fun realize(dependency: String): Dependency { - return realize(project.dependencies.create(dependency)) - } - - fun realize(dependency: Dependency): Dependency { - project.configurations.detachedConfiguration(dependency).resolve() - return dependency - } - - fun getLocalCacheDirectory(): File { - return sharedExtension.getLocalCacheDirectory().resolve("projectspecific") - .resolve(if (project == project.rootProject) "__root" else project.path.replace(":", "_")) - } + val sharedExtension = project.rootProject.extensions.getByType(ArchenemySharedExtension::class.java) + + private val _registerMinecraftProvider by lazy { + GradleRepositoryAdapter.add( + project.repositories, + "Minecraft Provider", + sharedExtension.getLocalCacheDirectory().resolve("minecraft-provider"), + sharedExtension.minecraftProvider + ) + GradleRepositoryAdapter.add( + project.repositories, + "Minecraft Mapped Provider", + getLocalCacheDirectory().resolve("minecraft-mapped-provider"), + mappedRepositoryProvider + ) + GradleRepositoryAdapter.add( + project.repositories, + "Minecraft Merged Provider", + getLocalCacheDirectory().resolve("minecraft-merged-provider"), + mergedRepositoryProvider + ) + project.repositories.maven { + it.name = "Minecraft Libraries" + it.url = URI("https://libraries.minecraft.net/") + } + } + + private val mappedRepositoryProvider = MappedRepositoryProvider(this) + private val mergedRepositoryProvider = MergedRepositoryProvider(this) + + fun yarnMappings(dependency: Dependency): MappingDependency { + dependency as ModuleDependency + return YarnMappingDependency(this, dependency) + } + + fun officialMappings(version: String, side: MCSide): MappingDependency { + _registerMinecraftProvider + val dependency by lazy { + realize( + sharedExtension.minecraftProvider.getMappingsDependencyCoordinate( + MinecraftProvider.MinecraftCoordinate( + version, + side + ) + ) + ) + } + return OfficialMappingDependency(side, version, project.providers.provider { dependency }).also { it.get() } + } + + fun natives(version: String): ExtractNativesTask { + val taskName = "extractNatives$version" + var extractNativesTask = project.tasks.findByName(taskName) + if (extractNativesTask != null) return extractNativesTask as ExtractNativesTask + + extractNativesTask = project.tasks.create(taskName, ExtractNativesTask::class.java) { + it.natives.set(sharedExtension.getDownloadVersionMetadataTask(version) + .getVersionMetadataNow().getFilteredLibraries()) + it.nativeDirectory.set(getLocalCacheDirectory().resolve("extractedNatives/$version")) + } + return extractNativesTask + } + + fun libraries(version: String): List<String> { + return sharedExtension.getDownloadVersionMetadataTask(version) + .getVersionMetadataNow().getFilteredLibraries() + .map { + it.getArtifactCoordinate() + } + } + + fun intermediaryMappings(version: String): MappingDependency { + return yarnMappings(project.dependencies.create("net.fabricmc:intermediary:$version:v2")) + } + + fun mergeJar( + base: Dependency, + overlay: Dependency, + ): Dependency { + base as ModuleDependency + overlay as ModuleDependency + _registerMinecraftProvider + return realize( + mergedRepositoryProvider.getCoordinate( + MergedRepositoryProvider.Coordinate(base, overlay) + ) + ) + } + + fun mapJar( + dependency: Dependency, + mappings: MappingDependency, + sourceNamespace: String, + destinationNamespace: String + ): Dependency { + dependency as ModuleDependency + _registerMinecraftProvider + return realize( + mappedRepositoryProvider.getDependencyCoordiante( + MappedRepositoryProvider.MappedCoordinates( + dependency, mappings, sourceNamespace, destinationNamespace + ) + ) + ) + } + + fun minecraft(version: String, side: MCSide): Dependency { + _registerMinecraftProvider + return realize( + sharedExtension.minecraftProvider.getDependencyCoordinate( + MinecraftProvider.MinecraftCoordinate( + version, + side, + ) + ) + ) + } + + fun realize(dependency: String): Dependency { + return realize(project.dependencies.create(dependency)) + } + + fun realize(dependency: Dependency): Dependency { + project.configurations.detachedConfiguration(dependency).resolve() + return dependency + } + + fun getLocalCacheDirectory(): File { + return sharedExtension.getLocalCacheDirectory().resolve("projectspecific") + .resolve(if (project == project.rootProject) "__root" else project.path.replace(":", "_")) + } }
\ No newline at end of file diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ExtractNativesTask.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ExtractNativesTask.kt new file mode 100644 index 0000000..a3c33ed --- /dev/null +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ExtractNativesTask.kt @@ -0,0 +1,45 @@ +package moe.nea.archenemy.mojang + +import moe.nea.archenemy.util.readZipFs +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.provider.ListProperty +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.TaskAction +import java.io.File + +abstract class ExtractNativesTask : DefaultTask() { + + @get:OutputDirectory + abstract val nativeDirectory: DirectoryProperty + + @get:Input + abstract val natives: ListProperty<MojangVersionMetadata.Library> + + @Internal + fun getNativeDirectoryPath(): File { + return nativeDirectory.get().asFile + } + + @TaskAction + fun extractNatives() { + val nativeDirectory = nativeDirectory.get().asFile + natives.get().forEach { + val extract = it.extract ?: return@forEach + val file = project.configurations + .detachedConfiguration(project.dependencies.create(it.getArtifactCoordinate())) + .files + .single().toPath() + file.readZipFs { path, inputStream -> + if (extract.exclude.any { path.startsWith(it) }) return@readZipFs + val targetFile = nativeDirectory.resolve(path) + targetFile.parentFile.mkdirs() + targetFile.outputStream().use { output -> + inputStream.copyTo(output) + } + } + } + } +}
\ No newline at end of file diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MojangVersionMetadata.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MojangVersionMetadata.kt index 4ef3313..1a6cf87 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MojangVersionMetadata.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MojangVersionMetadata.kt @@ -1,31 +1,48 @@ package moe.nea.archenemy.mojang import kotlinx.serialization.Serializable +import moe.nea.archenemy.util.OSUtil @Serializable data class MojangVersionMetadata( - val assetIndex: AssetIndex, - val downloads: Map<String, Download>, - val libraries: List<Library> + val assetIndex: AssetIndex, + val downloads: Map<String, Download>, + val libraries: List<Library> ) { - @Serializable - data class Library( - val name: String, - ) - @Serializable - data class Download( - val sha1: String, - val size: Long, - val url: String, - ) - - @Serializable - data class AssetIndex( - val id: String, - val sha1: String, - val size: Long, - val totalSize: Long, - val url: String, - ) + + fun getFilteredLibraries(): List<Library> { + return libraries.filter { !it.name.contains("twitch-platform") && !it.name.contains("twitch-external-platform") } + } + + @Serializable + data class Library( + val name: String, + val natives: Map<String, String>? = null, + val extract: ExtractOptions? = null, + ) : java.io.Serializable { + fun getArtifactCoordinate(): String { + val classifier = natives?.get(OSUtil.getOsClassifier()) + return name + (if (classifier != null) ":$classifier" else "") + } + + @Serializable + data class ExtractOptions(val exclude: List<String> = listOf()) : java.io.Serializable + } + + @Serializable + data class Download( + val sha1: String, + val size: Long, + val url: String, + ) + + @Serializable + data class AssetIndex( + val id: String, + val sha1: String, + val size: Long, + val totalSize: Long, + val url: String, + ) }
\ No newline at end of file diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/YarnMappingDependency.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/YarnMappingDependency.kt index 46b428e..25d2b35 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/YarnMappingDependency.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/YarnMappingDependency.kt @@ -66,6 +66,7 @@ class YarnMappingDependency( ) ) .build() + // TODO: this does not copy over the non class file resources, seemingly OutputConsumerPath.Builder(targetFile.toPath()).build().use { output -> remapper.readInputs(sourceFile.toPath()) remapper.apply(output) diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/util/OSUtil.kt b/plugin/src/main/kotlin/moe/nea/archenemy/util/OSUtil.kt new file mode 100644 index 0000000..bd7a158 --- /dev/null +++ b/plugin/src/main/kotlin/moe/nea/archenemy/util/OSUtil.kt @@ -0,0 +1,27 @@ +package moe.nea.archenemy.util + + +object OSUtil { + // TODO: replace this with a library + enum class OsKind { + WINDOWS, + LINUX, + OSX, + } + + fun getOs(): OsKind { + val osName = System.getProperty("os.name").lowercase() + if (osName.contains("win")) return OsKind.WINDOWS + if (osName.contains("nix") || osName.contains("nux")) return OsKind.LINUX + if (osName.contains("mac")) return OsKind.OSX + error("Unknown OS") + } + + fun getOsClassifier(): String { + return when (getOs()) { + OsKind.WINDOWS -> "windows" + OsKind.LINUX -> "linux" + OsKind.OSX -> "osx" + } + } +}
\ No newline at end of file |