更换至 Gradle
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package me.skymc.taboolib.common.versioncontrol;
|
||||
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.FieldVisitor;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* 我不信 ClassNotFoundException 的邪,自己写了一个发现还是一样。。。
|
||||
*
|
||||
* @Author sky
|
||||
* @Since 2018-09-19 21:17
|
||||
*/
|
||||
public class SimpleClassVisitor extends ClassVisitor {
|
||||
|
||||
private final SimpleVersionControl simpleVersionControl;
|
||||
|
||||
public SimpleClassVisitor(SimpleVersionControl simpleVersionControl, ClassVisitor classVisitor) {
|
||||
super(Opcodes.ASM5, classVisitor);
|
||||
this.simpleVersionControl = simpleVersionControl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
|
||||
super.visit(version, access, translate(name), translate(signature), translate(superName), translate(interfaces));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitInnerClass(String name, String outerName, String innerName, int access) {
|
||||
super.visitInnerClass(translate(name), translate(outerName), translate(innerName), access);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
|
||||
return super.visitField(access, translate(name), translate(descriptor), translate(signature), value instanceof String ? translate((String) value) : value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitOuterClass(String owner, String name, String descriptor) {
|
||||
super.visitOuterClass(translate(owner), translate(name), translate(descriptor));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
|
||||
return new SimpleMethodVisitor(simpleVersionControl, super.visitMethod(access, translate(name), translate(descriptor), translate(signature), translate(exceptions)));
|
||||
}
|
||||
|
||||
private String translate(String target) {
|
||||
return target == null ? null : simpleVersionControl.replace(target);
|
||||
}
|
||||
|
||||
private String[] translate(String[] target) {
|
||||
if (target == null) {
|
||||
return target;
|
||||
}
|
||||
IntStream.range(0, target.length).forEach(i -> target[i] = translate(target[i]));
|
||||
return target;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package me.skymc.taboolib.common.versioncontrol;
|
||||
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* 我不信 ClassNotFound 的邪,自己写了一个发现还是一样。。。
|
||||
*
|
||||
* @Author sky
|
||||
* @Since 2018-9-19 21:33
|
||||
*/
|
||||
public class SimpleMethodVisitor extends MethodVisitor {
|
||||
|
||||
private final SimpleVersionControl simpleVersionControl;
|
||||
|
||||
public SimpleMethodVisitor(SimpleVersionControl simpleVersionControl, MethodVisitor methodVisitor) {
|
||||
super(Opcodes.ASM5, methodVisitor);
|
||||
this.simpleVersionControl = simpleVersionControl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitParameter(String name, int access) {
|
||||
super.visitParameter(translate(name), access);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitMethodInsn(int opcode, String owner, String name, String descriptor) {
|
||||
super.visitMethodInsn(opcode, translate(owner), translate(name), translate(descriptor));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
|
||||
super.visitMethodInsn(opcode, translate(owner), translate(name), translate(descriptor), isInterface);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitLdcInsn(Object value) {
|
||||
if (value instanceof Type) {
|
||||
super.visitLdcInsn(Type.getType(translate(((Type) value).getDescriptor())));
|
||||
} else {
|
||||
super.visitLdcInsn(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitTypeInsn(int opcode, String type) {
|
||||
super.visitTypeInsn(opcode, translate(type));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitFieldInsn(int opcode, String owner, String name, String descriptor) {
|
||||
super.visitFieldInsn(opcode, translate(owner), translate(name), translate(descriptor));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) {
|
||||
super.visitLocalVariable(translate(name), translate(descriptor), translate(signature), start, end, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) {
|
||||
super.visitInvokeDynamicInsn(translate(name), translate(descriptor), bootstrapMethodHandle, bootstrapMethodArguments);
|
||||
}
|
||||
|
||||
private String translate(String target) {
|
||||
return target == null ? null : simpleVersionControl.replace(target);
|
||||
}
|
||||
|
||||
private String[] translate(String[] target) {
|
||||
if (target == null) {
|
||||
return target;
|
||||
}
|
||||
IntStream.range(0, target.length).forEach(i -> target[i] = translate(target[i]));
|
||||
return target;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package me.skymc.taboolib.common.versioncontrol;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.ilummc.tlib.util.asm.AsmClassLoader;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-09-19 21:05
|
||||
*/
|
||||
public class SimpleVersionControl {
|
||||
|
||||
private static Map<String, Class<?>> cacheClasses = new HashMap<>();
|
||||
private String target;
|
||||
private String to;
|
||||
private List<String> from = Lists.newArrayList();
|
||||
private Plugin plugin;
|
||||
private boolean useCache;
|
||||
private boolean useNMS;
|
||||
|
||||
SimpleVersionControl() {
|
||||
useCache = false;
|
||||
}
|
||||
|
||||
public static SimpleVersionControl create() {
|
||||
return new SimpleVersionControl().to(TabooLib.getVersion()).plugin(Main.getInst());
|
||||
}
|
||||
|
||||
public static SimpleVersionControl create(String toVersion) {
|
||||
return new SimpleVersionControl().to(toVersion).plugin(Main.getInst());
|
||||
}
|
||||
|
||||
public static SimpleVersionControl createSimple(String target, String... from) {
|
||||
return create().target(target).from(from);
|
||||
}
|
||||
|
||||
public static SimpleVersionControl createNMS(String target) {
|
||||
return create().target(target).useNMS();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置转换类地址,写法如:me.skymc.taboolib.packet.InternalPacket
|
||||
*/
|
||||
public SimpleVersionControl target(String target) {
|
||||
this.target = target;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置原版本,写法如:v1_8_R3
|
||||
*/
|
||||
public SimpleVersionControl from(String from) {
|
||||
this.from.add(from.startsWith("v") ? from : "v" + from);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 设置原版本,写法如:v1_8_R3, v1_12_R1
|
||||
*/
|
||||
public SimpleVersionControl from(String... from) {
|
||||
Arrays.stream(from).forEach(v -> this.from.add(v.startsWith("v") ? v : "v" + v));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置目标版本
|
||||
*/
|
||||
public SimpleVersionControl to(String to) {
|
||||
this.to = to.startsWith("v") ? to : "v" + to;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置插件,不填默认指向 TabooLib
|
||||
*/
|
||||
public SimpleVersionControl plugin(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换类将会保存在 TabooLib 中,防止出现 NoClassDefFoundError 异常
|
||||
*/
|
||||
public SimpleVersionControl useCache() {
|
||||
this.useCache = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动转换所有使用到的 NMS 或 OBC 方法
|
||||
*/
|
||||
public SimpleVersionControl useNMS() {
|
||||
this.useNMS = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Class<?> translate() throws IOException {
|
||||
return translate(plugin);
|
||||
}
|
||||
|
||||
public Class<?> translate(Plugin plugin) throws IOException {
|
||||
if (useCache && cacheClasses.containsKey(target)) {
|
||||
return cacheClasses.get(target);
|
||||
}
|
||||
ClassReader classReader = new ClassReader(FileUtils.getResource(plugin, target.replace(".", "/") + ".class"));
|
||||
ClassWriter classWriter = new ClassWriter(0);
|
||||
ClassVisitor classVisitor = new SimpleClassVisitor(this, classWriter);
|
||||
classReader.accept(classVisitor, 0);
|
||||
classWriter.visitEnd();
|
||||
classVisitor.visitEnd();
|
||||
Class<?> newClass = AsmClassLoader.createNewClass(target, classWriter.toByteArray());
|
||||
if (useCache) {
|
||||
cacheClasses.put(target, newClass);
|
||||
}
|
||||
return newClass;
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public List<String> getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public String replace(String origin) {
|
||||
if (useNMS) {
|
||||
origin = origin.replaceAll("net/minecraft/server/.*?/", "net/minecraft/server/" + to + "/").replaceAll("org/bukkit/craftbukkit/.*?/", "org/bukkit/craftbukkit/" + to + "/");
|
||||
}
|
||||
for (String from : from) {
|
||||
origin = origin.replace("/" + from + "/", "/" + to + "/");
|
||||
}
|
||||
return origin;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user