Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Rewrite to use paper's mapped userdev + reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Apr 14, 2022
1 parent fff3ade commit 672b27e
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 99 deletions.
27 changes: 14 additions & 13 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
plugins {
kotlin("jvm")
kotlin("kapt")
id("com.github.johnrengelman.shadow") version "7.0.0"
id("com.mineinabyss.conventions.publication")
id("de.undercouch.download") version "4.1.2"
// id("com.google.devtools.ksp") version "1.5.21-1.0.0-beta05"
id("com.mineinabyss.conventions.nms")
id("com.google.devtools.ksp") version "1.6.10-1.0.2"
}

repositories {
Expand All @@ -31,32 +31,33 @@ allprojects {
}

dependencies {
compileOnly("org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT")
implementation(project(":protocolburrito-api"))
compileOnly(project(":protocolburrito-generator"))
kapt(project(":protocolburrito-generator"))
ksp(project(":protocolburrito-generator"))
}


sourceSets["main"].java.srcDir(file("$rootDir/protocolburrito-generator/build/generated/burrito/main"))

tasks {
val downloadMappings by register<de.undercouch.gradle.tasks.download.Download>("Download mappings") {
src("https://launcher.mojang.com/v1/objects/f6cae1c5c1255f68ba4834b16a0da6a09621fe13/server.txt")
dest(file("mappings/server.txt"))
}
// val downloadMappings by register<de.undercouch.gradle.tasks.download.Download>("Download mappings") {
// src("https://launcher.mojang.com/v1/objects/f6cae1c5c1255f68ba4834b16a0da6a09621fe13/server.txt")
// dest(file("mappings/server.txt"))
// }
shadowJar {
archiveClassifier.set("")
}

sourcesJar {
from(sourceSets.main.get().allSource)
from("$buildDir/generated/source/kaptKotlin/main")
}

publish {
dependsOn(downloadMappings)
}
// publish {
// dependsOn(downloadMappings)
// }

build {
dependsOn(downloadMappings, project(":protocolburrito-plugin").tasks.build)
dependsOn(/*downloadMappings,*/ project(":protocolburrito-plugin").tasks.build)
}
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version=0.2
group=com.mineinabyss
kotlinVersion=1.6.10
serverVersion=1.17.1-R0.1-SNAPSHOT
idofrontConventions=1.6.10-51
serverVersion=1.18.2-R0.1-SNAPSHOT
idofrontVersion=0.9.82

# Workaround for Kapt not working on jdk 16
org.gradle.jvmargs=--illegal-access=permit
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
1 change: 0 additions & 1 deletion minecraft-data
Submodule minecraft-data deleted from 111735
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mineinabyss.protocolburrito

object FieldHelpers {
fun <T : Any> getField(forObject: Any, type: Class<T>, index: Int): T {
var id = 0
return forObject::class.java.fields.first { it.type.kotlin == type && index == id++ }.get(forObject) as T
}

fun <T : Any> setField(forObject: Any, type: Class<T>, index: Int, value: T) {
var id = 0
forObject::class.java.fields.first { it.type == type && index == id++ }.set(forObject, value)
}
}
36 changes: 31 additions & 5 deletions protocolburrito-generator/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
import io.papermc.paperweight.util.registering

val serverVersion: String by project

plugins {
kotlin("jvm")
kotlin("kapt")
// kotlin("kapt")
// id("com.mineinabyss.conventions.nms")
id("io.papermc.paperweight.userdev")
}

repositories {
mavenCentral()
google()
maven("https://papermc.io/repo/repository/maven-public/")
}

dependencies {
implementation("org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT")
implementation("com.comphenix.protocol:ProtocolLib:4.7.0")
paperDevBundle(serverVersion)
implementation(kotlin("reflect"))
implementation("io.papermc.paper:paper-server:userdev-1.18.2-R0.1-SNAPSHOT")
// implementation("com.comphenix.protocol:ProtocolLib:4.7.0")
implementation("com.nfeld.jsonpathkt:jsonpathkt:2.0.0")
implementation("com.squareup:kotlinpoet:1.10.1")
implementation(project(":protocolburrito-api"))

implementation("com.google.devtools.ksp:symbol-processing-api:1.5.31-1.0.0")
implementation("com.google.devtools.ksp:symbol-processing-api:1.6.10-1.0.2")
compileOnly("com.google.auto.service:auto-service:1.0")
kapt("com.google.auto.service:auto-service:1.0")
// kapt("com.google.auto.service:auto-service:1.0")
implementation("org.reflections:reflections:0.9.12")
implementation(kotlin("reflect"))
}
configurations {
remove(findByName("reobf"))
}

tasks {
val generateBurrito by registering<JavaExec>() {
main = "com.mineinabyss.protocolburrito.generation.MainKt"
classpath = files(configurations.runtimeClasspath, jar)
}
reobfJar {
onlyIf { false }
}
build {
dependsOn(generateBurrito)
}
}
//sourceSets.main {
// java.srcDirs("src/main/kotlin")
//}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.mineinabyss.protocolburrito.generation

import com.google.auto.service.AutoService
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 javax.annotation.processing.*
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement
Expand All @@ -15,17 +20,30 @@ annotation class RunGenerator
@AutoService(Processor::class)
@SupportedSourceVersion(SourceVersion.RELEASE_16)
@SupportedOptions(AnnotationProcessor.KAPT_KOTLIN_GENERATED_OPTION_NAME)
class AnnotationProcessor : AbstractProcessor() {
class AnnotationProcessor(
val environment: SymbolProcessorEnvironment
) : SymbolProcessor {
companion object {
const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
var generatedDir: String = "build/generated/source/kaptKotlin/main/"
var generatedDir: String = "build/generated/burrito/main/"
}

override fun getSupportedAnnotationTypes() = mutableSetOf(RunGenerator::class.java.name)
// override fun getSupportedAnnotationTypes() = mutableSetOf(RunGenerator::class.java.name)

override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
generatedDir = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION_NAME]!!
// override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
// generatedDir = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION_NAME]!!
// main()
// return false
// }

override fun process(resolver: Resolver): List<KSAnnotated> {
main()
return false
return listOf()
}
}

class ProtocolProcessorProvider: SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
return AnnotationProcessor(environment)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mineinabyss.protocolburrito.generation

fun main() {
generateEntityIdMapper()
generateProtocolWrappers()
// generateEntityIdMapper()
// generateProtocolWrappers()
generateProtocolExtensions()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,99 @@
package com.mineinabyss.protocolburrito.generation

import com.comphenix.protocol.events.PacketContainer
import com.mineinabyss.protocolburrito.FieldHelpers
import com.mineinabyss.protocolburrito.WrappedPacket
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import net.minecraft.network.protocol.Packet
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.EntityType
import org.reflections.Reflections
import org.reflections.scanners.SubTypesScanner
import java.io.File
import java.lang.reflect.Field
import java.lang.reflect.WildcardType
import java.util.concurrent.atomic.AtomicInteger
import kotlin.reflect.KVisibility
import kotlin.reflect.javaType
import kotlin.reflect.typeOf

//val SERVER_VERSION = "1.17"
//val PROJECT_ROOT = File("")//File(AnnotationProcessor.generatedDir)//.parentFile.parentFile.parentFile.parentFile.parentFile
//val SERVER_PATH = File(PROJECT_ROOT, "minecraft-data/data/pc/$SERVER_VERSION/")
//val MAPPINGS = File(PROJECT_ROOT, "mappings/server.txt")


@OptIn(DelicateKotlinPoetApi::class, ExperimentalStdlibApi::class)
fun generateProtocolExtensions() {
val classLoader = AnnotationProcessor::class.java.classLoader
val reflections = Reflections("net.minecraft.network.protocol.game", SubTypesScanner(false))
reflections.getSubTypesOf(Packet::class.java).forEach { packetClass ->
val indices = mutableMapOf<Class<*>, AtomicInteger>()
val props = packetClass.declaredFields.mapNotNull { field ->
if(field.type.kotlin.visibility != KVisibility.PUBLIC) return@mapNotNull null
val index = indices.getOrPut(field.type) { AtomicInteger(0) }
PropertySpec.builder(field.name, field.type.let {
if(field.type.typeParameters.isNotEmpty()) {
if(it != EntityType::class.java) return@mapNotNull null
it.asClassName().parameterizedBy(
WildcardTypeName.producerOf(Entity::class.java)
)
}
else it.asTypeName()
})
.receiver(packetClass)
.getter(
FunSpec.getterBuilder()
.addStatement("return %T.getField(this, %T::class.java, ${index.toInt()})", FieldHelpers::class, field.type)
.build()
)
.setter(
FunSpec.setterBuilder()
.addParameter(ParameterSpec.builder("value", field.type).build())
.addCode("%T.setField(this, %T::class.java, ${index.toInt()}, value)", FieldHelpers::class, field.type)
.build()
)
.mutable(true)
.build().also {
index.getAndAdd(1)
}
}

val file = FileSpec.builder("com.mineinabyss.protocolburrito.packets", packetClass.simpleName + "Extensions")
.apply {
for(prop in props) {
addProperty(prop)
}
}
.build()

file.writeTo(File(AnnotationProcessor.generatedDir))
}
}
/*
data class Prop(
val typeInfo: TypeMap.TypeInfo<*>,
val name: String,
)
val SERVER_VERSION = "1.17"
val PROJECT_ROOT = File(AnnotationProcessor.generatedDir).parentFile.parentFile.parentFile.parentFile.parentFile
val SERVER_PATH = File(PROJECT_ROOT, "minecraft-data/data/pc/$SERVER_VERSION/")
val MAPPINGS = File(PROJECT_ROOT, "mappings/server.txt")
fun generateProtocolWrappers() {
var className: String? = null
var props: MutableList<Property>? = null
val mappings = MAPPINGS.forEachLine { line ->
val mappings = MAPPINGS.forEachLine { line ->
when {
line.startsWith("net.minecraft.network.protocol.game") -> {
if(className != null && props?.isNotEmpty() == true) {
if (className != null && props?.isNotEmpty() == true) {
runCatching {
generateWrapper(className!!, props!!)
}.onFailure {
println("Skipping $className due to error")
it.printStackTrace()
}
}
className = line.removePrefix("net.minecraft.network.protocol.game.").substringBefore(" ").substringBefore("$")
className =
line.removePrefix("net.minecraft.network.protocol.game.").substringBefore(" ").substringBefore("$")
props = mutableListOf<Property>()
}
line.startsWith(" ") && props != null -> {
Expand All @@ -37,6 +105,7 @@ fun generateProtocolWrappers() {
}
data class Property(
val typeInfo: TypeMap.TypeInfo<*>,
val name: String,
Expand Down Expand Up @@ -94,3 +163,4 @@ fun generateWrapper(packetName: String, params: List<Property>) {
file.writeTo(File(AnnotationProcessor.generatedDir))
}
*/
Loading

0 comments on commit 672b27e

Please sign in to comment.