Skip to content

Commit

Permalink
Allow @tags only to accept lowercase alphanumeric and _
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Dec 29, 2024
1 parent dbc0bd6 commit 7f10856
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ import org.bukkit.inventory.ItemStack

val roadNetworkMaxDistance by snippet("road_network.distance.max", 30.0)

@Tags("road-network")
@Tags("road_network")
interface RoadNetworkEntry : ArtifactEntry {
suspend fun loadRoadNetwork(gson: Gson): RoadNetwork
suspend fun saveRoadNetwork(gson: Gson, network: RoadNetwork)
}

@Tags("road-network-node")
@Tags("road_network_node")
interface RoadNodeEntry : Entry {
val roadNetwork: Ref<RoadNetworkEntry>

@ContentEditor(SelectRoadNodeContentMode::class)
val nodeId: RoadNodeId
}

@Tags("road-network-node-collection")
@Tags("road_network_node_collection")
interface RoadNodeCollectionEntry : Entry {
val roadNetwork: Ref<RoadNetworkEntry>

Expand Down
2 changes: 1 addition & 1 deletion module-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "com.typewritermc.module-plugin"
version = "1.1.0"
version = "1.1.1"

val engineVersion = file("../version.txt").readText().trim().substringBefore("-beta")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
package com.typewritermc.verification

import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.getAnnotationsByType
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.typewritermc.core.extension.annotations.Tags
import com.typewritermc.processors.fullName

private val regex = Regex("^[a-z0-9_]+$")

class TagsValidator : SymbolProcessor {
@OptIn(KspExperimental::class)
override fun process(resolver: Resolver): List<KSAnnotated> {
val entries = resolver.getSymbolsWithAnnotation(Tags::class.qualifiedName!!)
val invalidEntries = entries
.map { it to it.getAnnotationsByType(Tags::class).first() }
.flatMap { (entry, annotation) ->
annotation.tags.filter { !it.matches(regex) }.map { tag ->
if (entry !is KSClassDeclaration) return@map tag
"${entry.fullName}: $tag"
}
}
.toList()

if (invalidEntries.isEmpty()) return emptyList()
throw InvalidTagsException(invalidEntries)
}
}

class InvalidTagsException(tags: List<String>) : Exception(
"""
|Tags can only contain lowercase letters, numbers and underscores.
|The following tags are invalid:
| - ${tags.joinToString("\n - ")}
|
""".trimMargin()
)

class TagsValidatorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
return TagsValidator()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ com.typewritermc.verification.EntryListenerParameterValidatorProvider
com.typewritermc.verification.EntryClassValidatorProvider
com.typewritermc.verification.EntryConstructorMapKeyValidatorProvider
com.typewritermc.verification.EntryParameterIconValidatorProvider
com.typewritermc.verification.TagsValidatorProvider
com.typewritermc.verification.CinematicEntrySegmentsValidatorProvider
com.typewritermc.verification.EntryStatelessValidatorProvider
com.typewritermc.verification.SessionTrackerConstructorValidatorProvider
Expand All @@ -15,7 +16,6 @@ com.typewritermc.verification.EntryOnlyOnEntryValidatorProvider
com.typewritermc.verification.TagsOnlyOnEntryValidatorProvider
com.typewritermc.verification.VariableDataOnlyOnEntryValidatorProvider
com.typewritermc.verification.GenericConstraintOnlyOnEntryValidatorProvider
com.typewritermc.verification.ContextKeysOnlyOnEntryValidatorProvider
com.typewritermc.verification.KeyTypeOnlyOnEntryContextKeysValidatorProvider


Expand Down

0 comments on commit 7f10856

Please sign in to comment.