forked from Angry-Pixel/The-Betweenlands
-
Notifications
You must be signed in to change notification settings - Fork 2
/
validateCoreJar.gradle
81 lines (70 loc) · 2.51 KB
/
validateCoreJar.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//This script checks if there are any dependencies in the core mod to code outside of the
//core mod package and throws an exception if so
buildscript {
repositories {
jcenter()
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
}
dependencies { classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT' }
}
apply plugin: 'net.minecraftforge.gradle.forge'
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.ClassRemapper;
import org.objectweb.asm.commons.Remapper;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
class ClassValidator extends Remapper {
String cls;
@Override
public String map(final String type) {
checkClassType(type);
return super.map(type);
}
private void checkClassType(final String type) {
if(type.startsWith("thebetweenlands") && !type.startsWith("thebetweenlands/core/")) {
throw new RuntimeException(String.format("Core mod class %s has illegal dependency to %s!", this.cls, type));
}
}
}
class CoreModImportsValidationTask extends DefaultTask {
@TaskAction
def validate() {
def coreJarFiles = project.fileTree(project.sourceSets.main.output.classesDir.path).matching { include 'thebetweenlands/core/**' }
coreJarFiles.visit { FileVisitDetails details ->
if(details.file.isFile() && details.file.name.endsWith(".class")) {
validateClassFile(details.file)
}
}
}
def validateClassFile(File file) {
file.withInputStream { is ->
ClassReader classReader = new ClassReader(is);
final ClassValidator validator = new ClassValidator();
ClassVisitor visitor = new ClassVisitor(Opcodes.ASM5) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
if(validator.cls == null) {
validator.cls = name;
}
super.visit(version, access, name, signature, superName, interfaces);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
return new MethodVisitor(Opcodes.ASM5) { };
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
return new FieldVisitor(Opcodes.ASM5) { };
}
};
classReader.accept(new ClassRemapper(visitor, validator), 0);
}
}
}
task validateCoreJar(type: CoreModImportsValidationTask) { }