Skip to content

Commit

Permalink
feat(generator-accessor-plugin): namespace configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
zlataovce committed Apr 10, 2024
1 parent 9619f55 commit e49dc99
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,16 @@ abstract class AccessorGeneratorExtension(protected val project: Project, protec
abstract val accessorType: Property<AccessorType>

/**
* Namespaces that should be used in accessors, empty if all namespaces should be used.
* Namespaces that should be used in accessors, defaults to all supported namespaces ("mojang", "spigot", "yarn", "quilt", "searge", "intermediary" and "hashed").
*/
abstract val accessedNamespaces: ListProperty<String>
abstract val namespaces: ListProperty<String>

/**
* Alias for [namespaces].
*/
@Deprecated("Use namespaces.", ReplaceWith("namespaces"))
val accessedNamespaces: ListProperty<String>
get() = namespaces

/**
* Namespaces that should be used for computing history, defaults to "mojang", "spigot", "searge" and "intermediary".
Expand All @@ -127,6 +134,7 @@ abstract class AccessorGeneratorExtension(protected val project: Project, protec
cacheDirectory.convention(project.layout.buildDirectory.dir("takenaka/cache"))
codeLanguage.convention(CodeLanguage.JAVA)
accessorType.convention(AccessorType.NONE)
namespaces.convention(listOf("mojang", "spigot", "yarn", "quilt", "searge", "intermediary", "hashed"))
historyNamespaces.convention(listOf("mojang", "spigot", "searge", "intermediary"))
historyIndexNamespace.convention(DEFAULT_INDEX_NS)
relaxedCache.convention(true)
Expand Down Expand Up @@ -261,12 +269,20 @@ abstract class AccessorGeneratorExtension(protected val project: Project, protec
}

/**
* Adds new namespaces to the [accessedNamespaces] property.
* Adds new namespaces to the [namespaces] property.
*
* @param accessedNamespaces the namespaces
* @param namespaces the namespaces
*/
fun namespaces(vararg namespaces: String) {
this.namespaces.addAll(*namespaces)
}

/**
* Alias for [namespaces].
*/
@Deprecated("Use namespaces(vararg String).", ReplaceWith("namespaces(*accessedNamespaces)"))
fun accessedNamespaces(vararg accessedNamespaces: String) {
this.accessedNamespaces.addAll(*accessedNamespaces)
this.namespaces(*accessedNamespaces)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class AccessorGeneratorPlugin : Plugin<Project> {

this.cacheDir.set(config.cacheDirectory)
this.versions.set(config.versions)
this.namespaces.set(project.provider { config.namespaces.get() + config.historyNamespaces.get() })
this.relaxedCache.set(config.relaxedCache)
this.manifest.set(manifest)
this.mappingBundle.fileProvider(mappingBundle.flatMap { mb ->
Expand All @@ -84,7 +85,7 @@ class AccessorGeneratorPlugin : Plugin<Project> {
this.accessors.set(config.accessors)
this.codeLanguage.set(config.codeLanguage)
this.accessorType.set(config.accessorType)
this.accessedNamespaces.set(config.accessedNamespaces)
this.namespaces.set(config.namespaces)
this.historyNamespaces.set(config.historyNamespaces)
this.historyIndexNamespace.set(config.historyIndexNamespace)
this.namingStrategy.set(config.namingStrategy)
Expand All @@ -100,7 +101,7 @@ class AccessorGeneratorPlugin : Plugin<Project> {
this.accessors.set(config.accessors)
this.codeLanguage.set(config.codeLanguage)
this.accessorType.set(config.accessorType)
this.accessedNamespaces.set(config.accessedNamespaces)
this.namespaces.set(config.namespaces)
this.historyNamespaces.set(config.historyNamespaces)
this.historyIndexNamespace.set(config.historyIndexNamespace)
this.namingStrategy.set(config.namingStrategy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class GenerateAccessorsTask : GenerationTask() {
codeLanguage = codeLanguage.get(),
accessorType = accessorType.get(),
namespaceFriendlinessIndex = namespaceFriendlinessIndex.get(),
accessedNamespaces = accessedNamespaces.get(),
accessedNamespaces = namespaces.get(),
craftBukkitVersionReplaceCandidates = craftBukkitVersionReplaceCandidates.get(),
namingStrategy = namingStrategy.get(),
runtimePackage = runtimePackage.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,20 @@ abstract class GenerationTask : DefaultTask() {
abstract val accessorType: Property<AccessorType>

/**
* Namespaces that should be used in accessors, empty if all namespaces should be used.
* Namespaces that should be used in accessors, defaults to all supported namespaces ("mojang", "spigot", "yarn", "quilt", "searge", "intermediary" and "hashed").
*
* @see me.kcra.takenaka.generator.accessor.plugin.AccessorGeneratorExtension.accessedNamespaces
* @see me.kcra.takenaka.generator.accessor.plugin.AccessorGeneratorExtension.namespaces
*/
@get:Input
abstract val accessedNamespaces: ListProperty<String>
abstract val namespaces: ListProperty<String>

/**
* Alias for [namespaces].
*/
@get:Internal
@Deprecated("Use namespaces.", ReplaceWith("namespaces"))
val accessedNamespaces: ListProperty<String>
get() = namespaces

/**
* Namespaces that should have [me.kcra.takenaka.core.mapping.adapter.replaceCraftBukkitNMSVersion] applied
Expand Down Expand Up @@ -163,6 +171,7 @@ abstract class GenerationTask : DefaultTask() {
codeLanguage.convention(CodeLanguage.JAVA)
accessorType.convention(AccessorType.NONE)
craftBukkitVersionReplaceCandidates.convention(listOf("spigot"))
namespaces.convention(listOf("mojang", "spigot", "yarn", "quilt", "searge", "intermediary", "hashed"))
historyNamespaces.convention(listOf("mojang", "spigot", "searge", "intermediary"))
historyIndexNamespace.convention(DEFAULT_INDEX_NS)
@Suppress("DEPRECATION")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import kotlinx.coroutines.runBlocking
import me.kcra.takenaka.core.Version
import me.kcra.takenaka.core.VersionManifest
import me.kcra.takenaka.core.compositeWorkspace
import me.kcra.takenaka.core.mapping.MappingContributor
import me.kcra.takenaka.core.mapping.adapter.*
import me.kcra.takenaka.core.mapping.analysis.impl.AnalysisOptions
import me.kcra.takenaka.core.mapping.analysis.impl.MappingAnalyzerImpl
import me.kcra.takenaka.core.mapping.resolve.impl.*
import me.kcra.takenaka.core.util.md5Digest
import me.kcra.takenaka.core.util.objectMapper
import me.kcra.takenaka.core.util.updateAndHex
import me.kcra.takenaka.generator.accessor.plugin.PlatformTristate
import me.kcra.takenaka.generator.common.provider.impl.BundledMappingProvider
import me.kcra.takenaka.generator.common.provider.impl.ResolvingMappingProvider
Expand All @@ -35,6 +38,7 @@ import net.fabricmc.mappingio.tree.MappingTree
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
Expand Down Expand Up @@ -85,6 +89,17 @@ abstract class ResolveMappingsTask : DefaultTask() {
@get:Input
abstract val versions: SetProperty<String>

/**
* Namespaces to be resolved, defaults to all supported namespaces ("mojang", "spigot", "yarn", "quilt", "searge", "intermediary" and "hashed").
*
* *This is ignored if a [mappingBundle] is specified.*
*
* @see me.kcra.takenaka.generator.accessor.plugin.AccessorGeneratorExtension.namespaces
* @see me.kcra.takenaka.generator.accessor.plugin.AccessorGeneratorExtension.historyNamespaces
*/
@get:Input
abstract val namespaces: ListProperty<String>

/**
* Whether output cache verification constraints should be relaxed, defaults to true.
*
Expand Down Expand Up @@ -144,6 +159,7 @@ abstract class ResolveMappingsTask : DefaultTask() {
}

init {
namespaces.convention(listOf("mojang", "spigot", "yarn", "quilt", "searge", "intermediary", "hashed"))
cacheDir.convention(project.layout.buildDirectory.dir("takenaka/cache"))
relaxedCache.convention(true)
platform.convention(PlatformTristate.SERVER)
Expand Down Expand Up @@ -185,6 +201,12 @@ abstract class ResolveMappingsTask : DefaultTask() {

val requiredPlatform = platform.get()
val requiredVersions = versions.get().toList()

val requiredNamespaces = namespaces.get().toSet()
fun <T : MappingContributor> MutableCollection<T>.addIfSupported(contributor: T) {
if (contributor.targetNamespace in requiredNamespaces) add(contributor)
}

val resolvedMappings = runBlocking {
// resolve mappings on this system, if a bundle is not available
if (mappingBundle.isPresent) {
Expand Down Expand Up @@ -217,49 +239,78 @@ abstract class ResolveMappingsTask : DefaultTask() {

buildList {
if (requiredPlatform.wantsServer) {
add(VanillaServerMappingContributor(versionWorkspace, mojangProvider, relaxedCache.get()))
add(MojangServerMappingResolver(versionWorkspace, mojangProvider))
add(
VanillaServerMappingContributor(
versionWorkspace,
mojangProvider,
relaxedCache.get()
)
)
addIfSupported(MojangServerMappingResolver(versionWorkspace, mojangProvider))
}
if (requiredPlatform.wantsClient) {
add(VanillaClientMappingContributor(versionWorkspace, mojangProvider, relaxedCache.get()))
add(MojangClientMappingResolver(versionWorkspace, mojangProvider))
add(
VanillaClientMappingContributor(
versionWorkspace,
mojangProvider,
relaxedCache.get()
)
)
addIfSupported(MojangClientMappingResolver(versionWorkspace, mojangProvider))
}

add(IntermediaryMappingResolver(versionWorkspace, sharedCacheWorkspace))
add(HashedMappingResolver(versionWorkspace))
add(YarnMappingResolver(versionWorkspace, yarnProvider, relaxedCache.get()))
add(QuiltMappingResolver(versionWorkspace, quiltProvider, relaxedCache.get()))
add(SeargeMappingResolver(versionWorkspace, sharedCacheWorkspace, relaxedCache = relaxedCache.get()))
addIfSupported(IntermediaryMappingResolver(versionWorkspace, sharedCacheWorkspace))
addIfSupported(HashedMappingResolver(versionWorkspace))
addIfSupported(YarnMappingResolver(versionWorkspace, yarnProvider, relaxedCache.get()))
addIfSupported(QuiltMappingResolver(versionWorkspace, quiltProvider, relaxedCache.get()))
addIfSupported(
SeargeMappingResolver(
versionWorkspace,
sharedCacheWorkspace,
relaxedCache = relaxedCache.get()
)
)

// Spigot resolvers have to be last
if (requiredPlatform.wantsServer) {
val link = LegacySpigotMappingPrepender.Link()

add(
addIfSupported(
// 1.16.5 mappings have been republished with proper packages, even though the reobfuscated JAR does not have those
// See: https://hub.spigotmc.org/stash/projects/SPIGOT/repos/builddata/commits/80d35549ec67b87a0cdf0d897abbe826ba34ac27
link.createPrependingContributor(
SpigotClassMappingResolver(versionWorkspace, xmlMapper, spigotProvider, relaxedCache.get()),
SpigotClassMappingResolver(
versionWorkspace,
xmlMapper,
spigotProvider,
relaxedCache.get()
),
prependEverything = versionWorkspace.version.id == "1.16.5"
)
)
add(link.createPrependingContributor(SpigotMemberMappingResolver(versionWorkspace, xmlMapper, spigotProvider, relaxedCache.get())))
addIfSupported(
link.createPrependingContributor(
SpigotMemberMappingResolver(
versionWorkspace,
xmlMapper,
spigotProvider,
relaxedCache.get()
)
)
)
}
}
}

joinedOutputPath { workspace ->
val fileName = when (requiredPlatform) {
PlatformTristate.CLIENT_SERVER -> "client+server.tiny"
PlatformTristate.CLIENT -> "client.tiny"
else -> "server.tiny"
}
val hash = md5Digest.updateAndHex(requiredNamespaces.sorted().joinToString(","))

workspace[fileName]
workspace["$hash.$requiredPlatform.tiny"]
}
}

val analyzer = MappingAnalyzerImpl(
// if the namespaces are missing, nothing happens anyway - no need to configure based on resolvedNamespaces
AnalysisOptions(
innerClassNameCompletionCandidates = setOf("spigot"),
inheritanceAdditionalNamespaces = setOf("searge") // mojang could be here too for maximal parity, but that's in exchange for a little bit of performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ abstract class TraceAccessorsTask : GenerationTask() {
codeLanguage = codeLanguage.get(),
accessorType = accessorType.get(),
namespaceFriendlinessIndex = namespaceFriendlinessIndex.get(),
accessedNamespaces = accessedNamespaces.get(),
accessedNamespaces = namespaces.get(),
craftBukkitVersionReplaceCandidates = craftBukkitVersionReplaceCandidates.get(),
namingStrategy = namingStrategy.get(),
runtimePackage = runtimePackage.get()
Expand Down

0 comments on commit e49dc99

Please sign in to comment.