Compare commits

..

No commits in common. "master" and "1.13" have entirely different histories.
master ... 1.13

18 changed files with 53 additions and 552 deletions

View File

@ -3,13 +3,10 @@ plugins {
id 'maven-publish' id 'maven-publish'
id 'java-gradle-plugin' id 'java-gradle-plugin'
id 'com.gradle.plugin-publish' version '0.12.0' id 'com.gradle.plugin-publish' version '0.12.0'
id 'org.jetbrains.kotlin.jvm' version '1.5.10'
} }
apply plugin: 'kotlin'
group 'io.izzel.taboolib' group 'io.izzel.taboolib'
version '1.27' version '1.13'
configurations { configurations {
embed embed
@ -24,10 +21,9 @@ dependencies {
compile 'org.codehaus.groovy:groovy:2.5.13' compile 'org.codehaus.groovy:groovy:2.5.13'
compile gradleApi() compile gradleApi()
compile localGroovy() compile localGroovy()
embed 'org.ow2.asm:asm:9.2' embed 'org.ow2.asm:asm:8.0.1'
embed 'org.ow2.asm:asm-commons:9.2' embed 'org.ow2.asm:asm-commons:8.0.1'
embed 'com.google.code.gson:gson:2.8.7' embed 'com.google.code.gson:gson:2.8.7'
embed 'org.jetbrains.kotlin:kotlin-stdlib'
} }
jar { jar {
@ -58,15 +54,3 @@ publishing {
} }
} }
} }
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https://skymc.oss-cn-shanghai.aliyuncs.com/files/gradle-6.5-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View File

@ -50,6 +50,6 @@ class KotlinAnnotationVisitor extends AnnotationVisitor {
} }
String getKotlinVersionEscape() { String getKotlinVersionEscape() {
return getKotlinVersion().replaceAll("[._-]", "") return getKotlinVersion().replaceAll("[.-]", "_")
} }
} }

View File

@ -1,29 +0,0 @@
package io.izzel.taboolib.gradle
import org.gradle.api.Project
import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.Opcodes
class KotlinMetaAnnotationVisitor extends AnnotationVisitor {
Project project
KotlinMetaAnnotationVisitor(AnnotationVisitor annotationVisitor, project) {
super(Opcodes.ASM7, annotationVisitor)
this.project = project
}
@Override
void visit(String name, Object value) {
if (value instanceof String) {
super.visit(name, value.replace("taboolib", "${project.group.replace('.', '/')}/taboolib"))
} else {
super.visit(name, value)
}
}
@Override
AnnotationVisitor visitArray(String name) {
return new KotlinMetaAnnotationVisitor(super.visitArray(name), project)
}
}

View File

@ -1,17 +0,0 @@
package io.izzel.taboolib.gradle
import groovy.transform.EqualsAndHashCode
import groovy.transform.TupleConstructor
@TupleConstructor
@EqualsAndHashCode
class MethodVisit {
String owner
String name
@Override
String toString() {
return owner.substring(owner.lastIndexOf("/") + 1) + "." + name + "()"
}
}

View File

@ -41,72 +41,30 @@ class RelocateJar extends DefaultTask {
@TaskAction @TaskAction
def relocate() { def relocate() {
//
def optimize = []
def isolated = new TreeMap<String, List<String>>() def isolated = new TreeMap<String, List<String>>()
def methodVisits = new TreeMap<String, Set<MethodVisit>>()
//
def mapping = relocations.collectEntries { [(it.key.replace('.', '/')), it.value.replace('.', '/')] } def mapping = relocations.collectEntries { [(it.key.replace('.', '/')), it.value.replace('.', '/')] }
def remapper = new RelocateRemapper(relocations, mapping as Map<String, String>) def remapper = new RelocateRemapper(relocations, mapping as Map<String, String>)
//
def index = inJar.name.lastIndexOf('.') def index = inJar.name.lastIndexOf('.')
def name = inJar.name.substring(0, index) + (classifier == null ? "" : "-" + classifier) + inJar.name.substring(index) def name = inJar.name.substring(0, index) + (classifier == null ? "" : "-" + classifier) + inJar.name.substring(index)
def outJar = new File(inJar.getParentFile(), name) def outJar = new File(inJar.getParentFile(), name)
def tempOut1 = File.createTempFile(name, ".jar") def tempOut1 = File.createTempFile(name, ".jar")
//
new JarOutputStream(new FileOutputStream(tempOut1)).withCloseable { out -> new JarOutputStream(new FileOutputStream(tempOut1)).withCloseable { out ->
int n int n
def buf = new byte[32768] def buf = new byte[32768]
new JarFile(inJar).withCloseable { jarFile -> new JarFile(inJar).withCloseable { jarFile ->
jarFile.entries().each { JarEntry jarEntry -> jarFile.entries().each { def jarEntry ->
def path = jarEntry.name if (tabooExt.exclude.none { String e -> jarEntry.name.matches(e) }) {
//
if (tabooExt.exclude.stream().any { String e -> path.startsWith(e) }) {
return
}
//
if (path.endsWith(".kotlin_module")) {
return
}
//
if (path.startsWith("META-INF/tf") && path.endsWith(".json")) {
optimize.add(Bridge.newOptimizeFileReader(project, jarFile.getInputStream(jarEntry)))
return
}
// Kotlin
def options = tabooExt.options
if (path == "taboolib/common/env/KotlinEnv.class" && options.contains("skip-kotlin")) {
return
}
//
if (path.startsWith("taboolib/common/env") && options.contains("skip-env")) {
return
}
// common
if (path.startsWith("taboolib/library/asm") || path.startsWith("taboolib/library/jarrelocator")) {
if (options.contains("skip-env") || options.contains("skip-env-relocate")) {
return
}
}
jarFile.getInputStream(jarEntry).withCloseable { jarFile.getInputStream(jarEntry).withCloseable {
if (path.endsWith(".class")) { if (jarEntry.name.endsWith(".class")) {
def reader = new ClassReader(it) def reader = new ClassReader(it)
def writer = new ClassWriter(0) def writer = new ClassWriter(0)
def visitor = new TabooLibClassVisitor(writer, project) def visitor = new TabooLibClassVisitor(writer, project)
def rem = new ClassRemapper(visitor, remapper) def rem = new ClassRemapper(visitor, remapper)
remapper.remapper = rem remapper.remapper = rem
reader.accept(rem, 0) reader.accept(rem, 0)
//
isolated.putAll(visitor.isolated) isolated.putAll(visitor.isolated)
// 访
methodVisits.put(relocate(project, jarEntry.name), visitor.methodVisits)
//
//
try { try {
out.putNextEntry(new JarEntry(remapper.map(path))) out.putNextEntry(new JarEntry(remapper.map(jarEntry.name)))
} catch(ZipException zipException) { } catch(ZipException zipException) {
println(zipException) println(zipException)
return true return true
@ -114,7 +72,7 @@ class RelocateJar extends DefaultTask {
out.write(writer.toByteArray()) out.write(writer.toByteArray())
} else { } else {
try { try {
out.putNextEntry(new JarEntry(remapper.map(path))) out.putNextEntry(new JarEntry(remapper.map(jarEntry.name)))
} catch(ZipException ex) { } catch(ZipException ex) {
println(ex) println(ex)
return true return true
@ -128,9 +86,7 @@ class RelocateJar extends DefaultTask {
} }
} }
} }
}
//
// -> 使
def use = new TreeMap<String, Set<String>>() def use = new TreeMap<String, Set<String>>()
remapper.use.each { remapper.use.each {
it.value.each { e -> it.value.each { e ->
@ -139,19 +95,11 @@ class RelocateJar extends DefaultTask {
use.computeIfAbsent(key) { new HashSet() }.add(value) use.computeIfAbsent(key) { new HashSet() }.add(value)
} }
} }
def visits = methodVisits[methodVisits.keySet().first()]
if (visits != null) {
visits.each {
println(it)
}
}
def transfer = new TreeMap() def transfer = new TreeMap()
isolated.each { isolated.each {
transfer[relocate(project, it.key)] = it.value.stream().map { i -> relocate(project, i) }.collect(Collectors.toList()) transfer[relocate(project, it.key)] = it.value.stream().map { i -> relocate(project, i) }.collect(Collectors.toList())
} }
isolated = transfer isolated = transfer
//
def tempOut2 = File.createTempFile(name, ".jar") def tempOut2 = File.createTempFile(name, ".jar")
new JarOutputStream(new FileOutputStream(tempOut2)).withCloseable { out -> new JarOutputStream(new FileOutputStream(tempOut2)).withCloseable { out ->
int n int n
@ -159,16 +107,14 @@ class RelocateJar extends DefaultTask {
def del = new HashSet() def del = new HashSet()
def exclude = new HashSet() def exclude = new HashSet()
new JarFile(tempOut1).withCloseable { jarFile -> new JarFile(tempOut1).withCloseable { jarFile ->
jarFile.entries().each { JarEntry jarEntry -> jarFile.entries().each { def jarEntry ->
if (optimize.any { it.exclude(jarEntry.name, use) }) {
return
}
jarFile.getInputStream(jarEntry).withCloseable { jarFile.getInputStream(jarEntry).withCloseable {
if (jarEntry.name.endsWith(".class")) { if (jarEntry.name.endsWith(".class")) {
def nameWithOutExtension = getNameWithOutExtension(jarEntry.name) def nameWithOutExtension = getNameWithOutExtension(jarEntry.name)
if (use.containsKey(nameWithOutExtension.toString()) && !exclude.contains(nameWithOutExtension)) { if (use.containsKey(nameWithOutExtension.toString()) && !exclude.contains(nameWithOutExtension)) {
exclude.add(nameWithOutExtension) exclude.add(nameWithOutExtension)
if (isIsolated(use, use[nameWithOutExtension], isolated, nameWithOutExtension)) { if (isIsolated(use, use[nameWithOutExtension], isolated, nameWithOutExtension)) {
println(" Isolated ${nameWithOutExtension}")
del.add(nameWithOutExtension) del.add(nameWithOutExtension)
} }
} }
@ -227,10 +173,4 @@ class RelocateJar extends DefaultTask {
return false return false
} }
} }
@Override
public String toString() {
return "RelocateJar{}";
}
} }

View File

@ -29,9 +29,6 @@ class RelocateRemapper extends Remapper {
if (remapper != null) { if (remapper != null) {
use.computeIfAbsent(remapper.className) { new HashSet() }.add(internalName) use.computeIfAbsent(remapper.className) { new HashSet() }.add(internalName)
} }
if (internalName.startsWith('kotlin/Metadata')) {
return internalName
}
def match = slash.find { internalName.startsWith(it.key) } def match = slash.find { internalName.startsWith(it.key) }
if (match) { if (match) {
return match.value + internalName.substring(match.key.length()) return match.value + internalName.substring(match.key.length())

View File

@ -3,19 +3,14 @@ package io.izzel.taboolib.gradle
import org.gradle.api.Project import org.gradle.api.Project
import org.objectweb.asm.AnnotationVisitor import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.ClassVisitor import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes import org.objectweb.asm.Opcodes
class TabooLibClassVisitor extends ClassVisitor { class TabooLibClassVisitor extends ClassVisitor {
String name String name
Project project Project project
Map<String, List<String>> isolated = new HashMap() Map<String, List<String>> isolated = new HashMap()
Set<MethodVisit> methodVisits = new HashSet<>()
List<String> annotations = [ List<String> annotations = [
"Lorg/spongepowered/api/plugin/Plugin;", "Lorg/spongepowered/api/plugin/Plugin;",
"Lorg/spongepowered/plugin/jvm/Plugin;", "Lorg/spongepowered/plugin/jvm/Plugin;",
@ -33,16 +28,9 @@ class TabooLibClassVisitor extends ClassVisitor {
super.visit(version, access, name, signature, superName, interfaces) super.visit(version, access, name, signature, superName, interfaces)
} }
@Override
MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
return new TabooLibMethodVisitor(super.visitMethod(access, name, descriptor, signature, exceptions), this)
}
@Override @Override
AnnotationVisitor visitAnnotation(String descriptor, boolean visible) { AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
if (descriptor == "Lkotlin/Metadata;") { if (descriptor == "L${project.group.replace('.', '/')}/taboolib/common/Isolated;") {
return new KotlinMetaAnnotationVisitor(super.visitAnnotation(descriptor, visible), project)
} else if (descriptor == "L${project.group.replace('.', '/')}/taboolib/common/Isolated;") {
return new IsolatedAnnotationVisitor(super.visitAnnotation(descriptor, visible), project, name, this) return new IsolatedAnnotationVisitor(super.visitAnnotation(descriptor, visible), project, name, this)
} else if (descriptor == "L${project.group.replace('.', '/')}/taboolib/common/env/RuntimeDependency;") { } else if (descriptor == "L${project.group.replace('.', '/')}/taboolib/common/env/RuntimeDependency;") {
return new KotlinAnnotationVisitor(super.visitAnnotation(descriptor, visible), project) return new KotlinAnnotationVisitor(super.visitAnnotation(descriptor, visible), project)

View File

@ -19,16 +19,10 @@ class TabooLibExtension {
Map<String, String> relocation = new LinkedHashMap<>() Map<String, String> relocation = new LinkedHashMap<>()
List<String> options = []
def install(String... name) { def install(String... name) {
name.each { modules += it } name.each { modules += it }
} }
def options(String... opt) {
opt.each { options += it }
}
def exclude(String match) { def exclude(String match) {
exclude += match exclude += match
} }

View File

@ -1,183 +0,0 @@
package io.izzel.taboolib.gradle
import groovy.transform.EqualsAndHashCode
import groovy.transform.TupleConstructor
import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.Attribute
import org.objectweb.asm.Handle
import org.objectweb.asm.Label
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes
import org.objectweb.asm.TypePath
class TabooLibMethodVisitor extends MethodVisitor {
TabooLibClassVisitor classVisitor
TabooLibMethodVisitor(MethodVisitor methodVisitor, TabooLibClassVisitor classVisitor) {
super(Opcodes.ASM7, methodVisitor)
this.classVisitor = classVisitor
}
@Override
void visitParameter(String name, int access) {
super.visitParameter(name, access)
}
@Override
AnnotationVisitor visitAnnotationDefault() {
return super.visitAnnotationDefault()
}
@Override
AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
return super.visitAnnotation(descriptor, visible)
}
@Override
AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String descriptor, boolean visible) {
return super.visitTypeAnnotation(typeRef, typePath, descriptor, visible)
}
@Override
void visitAnnotableParameterCount(int parameterCount, boolean visible) {
super.visitAnnotableParameterCount(parameterCount, visible)
}
@Override
AnnotationVisitor visitParameterAnnotation(int parameter, String descriptor, boolean visible) {
return super.visitParameterAnnotation(parameter, descriptor, visible)
}
@Override
void visitAttribute(Attribute attribute) {
super.visitAttribute(attribute)
}
@Override
void visitCode() {
super.visitCode()
}
@Override
void visitFrame(int type, int numLocal, Object[] local, int numStack, Object[] stack) {
super.visitFrame(type, numLocal, local, numStack, stack)
}
@Override
void visitInsn(int opcode) {
super.visitInsn(opcode)
}
@Override
void visitIntInsn(int opcode, int operand) {
super.visitIntInsn(opcode, operand)
}
@Override
void visitVarInsn(int opcode, int i) {
super.visitVarInsn(opcode, i)
}
@Override
void visitTypeInsn(int opcode, String type) {
super.visitTypeInsn(opcode, type)
}
@Override
void visitFieldInsn(int opcode, String owner, String name, String descriptor) {
super.visitFieldInsn(opcode, owner, name, descriptor)
}
@Override
void visitMethodInsn(int opcode, String owner, String name, String descriptor) {
super.visitMethodInsn(opcode, owner, name, descriptor)
}
@Override
void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
classVisitor.methodVisits.add(new MethodVisit(owner, name))
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface)
}
@Override
void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) {
super.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments)
}
@Override
void visitJumpInsn(int opcode, Label label) {
super.visitJumpInsn(opcode, label)
}
@Override
void visitLabel(Label label) {
super.visitLabel(label)
}
@Override
void visitLdcInsn(Object value) {
super.visitLdcInsn(value)
}
@Override
void visitIincInsn(int i, int increment) {
super.visitIincInsn(i, increment)
}
@Override
void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) {
super.visitTableSwitchInsn(min, max, dflt, labels)
}
@Override
void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
super.visitLookupSwitchInsn(dflt, keys, labels)
}
@Override
void visitMultiANewArrayInsn(String descriptor, int numDimensions) {
super.visitMultiANewArrayInsn(descriptor, numDimensions)
}
@Override
AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath, String descriptor, boolean visible) {
return super.visitInsnAnnotation(typeRef, typePath, descriptor, visible)
}
@Override
void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
super.visitTryCatchBlock(start, end, handler, type)
}
@Override
AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath, String descriptor, boolean visible) {
return super.visitTryCatchAnnotation(typeRef, typePath, descriptor, visible)
}
@Override
void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) {
super.visitLocalVariable(name, descriptor, signature, start, end, index)
}
@Override
AnnotationVisitor visitLocalVariableAnnotation(int typeRef, TypePath typePath, Label[] start, Label[] end, int[] index, String descriptor, boolean visible) {
return super.visitLocalVariableAnnotation(typeRef, typePath, start, end, index, descriptor, visible)
}
@Override
void visitLineNumber(int line, Label start) {
super.visitLineNumber(line, start)
}
@Override
void visitMaxs(int maxStack, int maxLocals) {
super.visitMaxs(maxStack, maxLocals)
}
@Override
void visitEnd() {
super.visitEnd()
}
}

View File

@ -22,7 +22,7 @@ class TabooLibPlugin implements Plugin<Project> {
project.tasks.jar.configure { Jar task -> project.tasks.jar.configure { Jar task ->
task.from(taboo.collect { it.isDirectory() ? it : project.zipTree(it) }) task.from(taboo.collect { it.isDirectory() ? it : project.zipTree(it) })
} }
def kv = project.plugins.findPlugin("org.jetbrains.kotlin.jvm").kotlinPluginVersion.replaceAll("[._-]", "") def kv = project.plugins.findPlugin("org.jetbrains.kotlin.jvm").kotlinPluginVersion.replaceAll("[.-]", "_")
def jarTask = project.tasks.jar as Jar def jarTask = project.tasks.jar as Jar
tabooTask.configure { RelocateJar task -> tabooTask.configure { RelocateJar task ->
task.tabooExt = tabooExt task.tabooExt = tabooExt
@ -30,10 +30,8 @@ class TabooLibPlugin implements Plugin<Project> {
task.inJar = task.inJar ?: jarTask.archivePath task.inJar = task.inJar ?: jarTask.archivePath
task.relocations = tabooExt.relocation task.relocations = tabooExt.relocation
task.classifier = tabooExt.classifier task.classifier = tabooExt.classifier
task.relocations['kotlin'] = 'taboolib.library.kotlin_' + kv
task.relocations['taboolib'] = project.group.toString() + '.taboolib' task.relocations['taboolib'] = project.group.toString() + '.taboolib'
if (!tabooExt.options.contains("skip-kotlin") && !tabooExt.options.contains("skip-kotlin-relocate")) {
task.relocations['kotlin.'] = 'kotlin' + kv + '.'
}
} }
} }
} }

View File

@ -13,8 +13,8 @@ class BuilderBungee extends Builder {
write(body, description.lin.links['homepage'], 'website') write(body, description.lin.links['homepage'], 'website')
writeLine(body) writeLine(body)
// authors // authors
def con = description.con.contributors.collect { it.name }.join(', ') def con = description.con.contributors.collect { it.name }
write(body, con, 'author') writeList(body, con, 'authors')
writeLine(body) writeLine(body)
// dependency // dependency
writeList(body, description.dep.dependencies writeList(body, description.dep.dependencies

View File

@ -8,6 +8,7 @@ class BuilderVelocity extends Builder {
@Override @Override
byte[] build(Description description, Project project) { byte[] build(Description description, Project project) {
def json = new JsonArray()
def info = new JsonObject() def info = new JsonObject()
info.addProperty('id', project.name.toLowerCase()) info.addProperty('id', project.name.toLowerCase())
info.addProperty('name', project.name) info.addProperty('name', project.name)
@ -20,6 +21,7 @@ class BuilderVelocity extends Builder {
writeList(info, description.dep.dependencies writeList(info, description.dep.dependencies
.findAll { it.with == null || it.with.equalsIgnoreCase('velocity') } .findAll { it.with == null || it.with.equalsIgnoreCase('velocity') }
.collect { it.name }, 'dependencies') .collect { it.name }, 'dependencies')
return bytes(info) json.add(info)
return bytes(json)
} }
} }

View File

@ -23,10 +23,5 @@ class Links {
this.url = url this.url = url
return this return this
} }
@Override
String toString() {
return url
}
} }
} }

View File

@ -2,25 +2,23 @@ package io.izzel.taboolib.gradle.description
enum Platforms { enum Platforms {
BUKKIT('Bukkit', 'platform-bukkit', 'plugin.yml', new BuilderBukkit()), BUKKIT('platform-bukkit', 'plugin.yml', new BuilderBukkit()),
NUKKIT('Nukkit', 'platform-nukkit', 'nukkit.yml', new BuilderNukkit()), NUKKIT('platform-nukkit', 'nukkit.yml', new BuilderNukkit()),
BUNGEE('Bungee', 'platform-bungee', 'bungee.yml', new BuilderBungee()), BUNGEE('platform-bungee', 'bungee.yml', new BuilderBungee()),
VELOCITY('Velocity', 'platform-velocity', 'velocity-plugin.json', new BuilderVelocity()), VELOCITY('platform-velocity', 'velocity-plugin.json', new BuilderVelocity()),
SPONGE7('Sponge7', 'platform-sponge-api7', 'mcmod.info', new BuilderSponge7()), SPONGE7('platform-sponge-api7', 'mcmod.info', new BuilderSponge7()),
SPONGE8('Sponge8', 'platform-sponge-api8', 'META-INF/plugins.json', new BuilderSponge8()); SPONGE8('platform-sponge-api8', 'META-INF/plugins.json', new BuilderSponge8());
String key
String module String module
String file String file
Builder builder Builder builder
Platforms(key, module, file, builder) { Platforms(module, file, builder) {
this.key = key
this.module = module this.module = module
this.file = file this.file = file
this.builder = builder this.builder = builder

View File

@ -1,19 +0,0 @@
package io.izzel.taboolib.gradle;
import org.gradle.api.Project;
import java.io.InputStream;
/**
* taboolib-gradle-plugin
* io.izzel.taboolib.gradle.KotlinFactory
*
* @author sky
* @since 2021/8/14 1:43 下午
*/
public class Bridge {
public static OptimizeFileReader newOptimizeFileReader(Project project, InputStream inputStream) {
return new OptimizeFileReader(project, inputStream);
}
}

View File

@ -1,134 +0,0 @@
package io.izzel.taboolib.gradle
import com.google.gson.JsonParser
import org.gradle.api.Project
import java.io.InputStream
import java.nio.charset.StandardCharsets
/**
* taboolib-gradle-plugin
* io.izzel.taboolib.gradle.OptimizeFileReader
*
* @author sky
* @since 2021/8/14 1:18 下午
*/
class OptimizeFileReader(project: Project, input: InputStream) {
val optimize = ArrayList<Optimize>()
val group = ArrayList<Group>()
val exclude = ArrayList<String>()
init {
try {
val jsonSource = input.readBytes().toString(StandardCharsets.UTF_8)
val json = JsonParser.parseString(jsonSource).asJsonObject
if (json.has("optimize")) {
json.getAsJsonArray("optimize").forEach { ele ->
optimize += Optimize(
relocate(project, ele.asJsonObject["class"].asString),
Optimize.Type.valueOf(ele.asJsonObject["type"].asString)
)
}
}
if (json.has("group")) {
json.getAsJsonArray("group").forEach { ele ->
var depend: Group.Depend? = null
if (ele.asJsonObject.has("depend")) {
depend = Group.Depend(
ele.asJsonObject["depend"].asJsonObject["name"].asJsonArray.map { relocate(project, it.asString) },
ele.asJsonObject["depend"].asJsonObject["exclude"].asJsonArray.map { relocate(project, it.asString) }
)
}
group += Group(
ele.asJsonObject["check"].asJsonArray.map { relocate(project, it.asString) },
ele.asJsonObject["member"].asJsonArray.map { relocate(project, it.asString) },
ele.asJsonObject["exclude"]?.asJsonArray?.map { relocate(project, it.asString) }?.toList() ?: emptyList(),
depend,
Group.Mode.valueOf(ele.asJsonObject["mode"].asString)
)
}
}
if (json.has("exclude")) {
exclude += json.getAsJsonArray("exclude").map { relocate(project, it.asString) }
}
} catch (ex: Throwable) {
ex.printStackTrace()
}
}
fun relocate(project: Project, name: String): String {
return if (name.startsWith("taboolib")) {
project.group.toString().replace('.', '/') + '/' + name.replace('.', '/')
} else {
name.replace('.', '/')
}
}
fun exclude(name: String, use: Map<String, Set<String>>): Boolean {
if (exclude.any { name.startsWith(it) }) {
return true
}
if (group.any { it.exclude(name, use) }) {
return true
}
return false
}
class Optimize(val path: String, val type: Type) {
enum class Type {
METHOD
}
}
class Group(check: List<String>, member: List<String>, exclude: List<String>, val depend: Depend? = null, val mode: Mode) {
val check = check.platformFlatten()
val member = member.platformFlatten()
val exclude = exclude.platformFlatten()
class Depend(name: List<String>, exclude: List<String>) {
val name = name.platformFlatten()
val exclude = exclude.platformFlatten()
}
enum class Mode {
REMOVE
}
fun exclude(name: String, use: Map<String, Set<String>>): Boolean {
if (member.any { name.startsWith(it) }) {
if (depend != null) {
val fail = depend.name.any { n ->
val set = use[n]?.toMutableList() ?: ArrayList()
set.remove(n)
set.removeAll(depend.exclude)
set.isNotEmpty()
}
if (fail) {
return false
}
}
for (s in check) {
val set = use[s]?.toMutableList() ?: ArrayList()
set.remove(s)
set.removeAll(check)
set.removeAll(member)
set.removeAll(exclude)
if (set.isNotEmpty()) {
return false
}
}
return true
} else {
return false
}
}
}
}

View File

@ -1,13 +0,0 @@
package io.izzel.taboolib.gradle
val platforms = listOf("Bukkit", "Nukkit", "Bungee", "Sponge7", "Sponge8", "Velocity")
fun List<String>.platformFlatten(): List<String> {
return flatMap {
if (it.contains("{platform}")) {
platforms.map { p -> it.replace("{platform}", p) }
} else {
listOf(it)
}
}
}