|
| 1 | +import org.objectweb.asm.ClassReader |
| 2 | +import org.objectweb.asm.ClassWriter |
| 3 | +import org.objectweb.asm.Opcodes |
| 4 | +import org.objectweb.asm.commons.ClassRemapper |
| 5 | +import org.objectweb.asm.commons.Remapper |
| 6 | + |
| 7 | +import java.util.jar.JarEntry |
| 8 | +import java.util.jar.JarFile |
| 9 | +import java.util.jar.JarOutputStream |
| 10 | + |
| 11 | +buildscript { |
| 12 | + dependencies { |
| 13 | + classpath "org.ow2.asm:asm-commons:${project['asm.version']}" |
| 14 | + } |
| 15 | +} |
| 16 | + |
1 | 17 | plugins { |
2 | 18 | id 'net.ltgt.errorprone' |
3 | 19 | } |
|
30 | 46 | attributes 'Can-Set-Native-Method-Prefix': 'true' |
31 | 47 | attributes 'Can-Retransform-Classes': 'true' |
32 | 48 | attributes 'Can-Redefine-Classes': 'true' |
| 49 | + attributes 'Sealed': 'true' |
33 | 50 | } |
34 | 51 |
|
35 | 52 | from(zipTree(project(":patcher").tasks.jar.archiveFile.get().asFile)) { |
|
38 | 55 | } |
39 | 56 |
|
40 | 57 | dependsOn(":patcher:jar") |
| 58 | + |
| 59 | + doLast { |
| 60 | + def jarFileAsFile = archiveFile.get().asFile |
| 61 | + def tempJar = new File(jarFileAsFile.parentFile, jarFileAsFile.name + ".tmp") |
| 62 | + def remapper = new Remapper(Opcodes.ASM9) { |
| 63 | + @Override |
| 64 | + String map(String typeName) { |
| 65 | + if (typeName.startsWith("com/fox2code/hypertale/launcher/InternalMicroJsonScanner")) { |
| 66 | + return typeName.replaceFirst( |
| 67 | + "com/fox2code/hypertale/launcher/", "com/fox2code/hypertale/init/lib/") |
| 68 | + } |
| 69 | + if (typeName.startsWith("com/fox2code/hypertale/utils/HypertalePlatform")) { |
| 70 | + return typeName.replaceFirst( |
| 71 | + "com/fox2code/hypertale/utils/", "com/fox2code/hypertale/init/lib/") |
| 72 | + } |
| 73 | + return super.map(typeName) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + def jf = new JarFile(jarFileAsFile) |
| 78 | + def jos = new JarOutputStream(new FileOutputStream(tempJar)) |
| 79 | + def addedEntries = new HashSet<String>() |
| 80 | + |
| 81 | + jf.entries().each { entry -> |
| 82 | + if (entry.isDirectory()) return |
| 83 | + |
| 84 | + def is = jf.getInputStream(entry) |
| 85 | + def bytes = is.readAllBytes() |
| 86 | + is.close() |
| 87 | + |
| 88 | + def newName = entry.name |
| 89 | + |
| 90 | + if (entry.name.endsWith(".class")) { |
| 91 | + def cr = new ClassReader(bytes) |
| 92 | + def cw = new ClassWriter(0) |
| 93 | + def crMapper = new ClassRemapper(cw, remapper) |
| 94 | + cr.accept(crMapper, 0) |
| 95 | + bytes = cw.toByteArray() |
| 96 | + |
| 97 | + newName = remapper.map(entry.name.replace(".class", "")) + ".class" |
| 98 | + } |
| 99 | + |
| 100 | + // Write to the new temporary JAR |
| 101 | + if (addedEntries.add(newName)) { |
| 102 | + def newEntry = new JarEntry(newName) |
| 103 | + jos.putNextEntry(newEntry) |
| 104 | + jos.write(bytes) |
| 105 | + jos.closeEntry() |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + jos.finish() |
| 110 | + jos.close() |
| 111 | + jf.close() |
| 112 | + |
| 113 | + jarFileAsFile.delete() |
| 114 | + tempJar.renameTo(jarFileAsFile) |
| 115 | + } |
41 | 116 | } |
42 | 117 |
|
43 | 118 | test { |
|
0 commit comments