-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6693277
commit 253d81c
Showing
16 changed files
with
600 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
import java.util.* | ||
|
||
plugins { | ||
kotlin("jvm") version "1.9.22" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
val asmVersion: String = project.properties["asmVersion"]?.toString() ?: run { | ||
projectDir.parentFile.resolve("gradle.properties").inputStream().use { | ||
val props = Properties() | ||
props.load(it) | ||
props.getProperty("asmVersion") as String | ||
} | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
tasks.withType<JavaCompile> { | ||
val targetVersion = 8 | ||
if (JavaVersion.current().isJava9Compatible) { | ||
options.release.set(targetVersion) | ||
} | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions.jvmTarget = "1.8" | ||
} | ||
|
||
dependencies { | ||
implementation(gradleApi()) | ||
|
||
// commons compress | ||
implementation("org.apache.commons:commons-compress:1.26.1") | ||
|
||
implementation("org.ow2.asm:asm:${asmVersion}") | ||
implementation("org.ow2.asm:asm-commons:${asmVersion}") | ||
implementation("org.ow2.asm:asm-tree:${asmVersion}") | ||
} |
64 changes: 64 additions & 0 deletions
64
buildSrc/src/main/kotlin/xyz/wagyourtail/gradle/shadow/PackageRelocateReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package xyz.wagyourtail.gradle.shadow | ||
|
||
import org.objectweb.asm.ClassReader | ||
import org.objectweb.asm.ClassWriter | ||
import org.objectweb.asm.commons.ClassRemapper | ||
import xyz.wagyourtail.gradle.utils.MustSet | ||
import java.io.ByteArrayInputStream | ||
import java.io.ByteArrayOutputStream | ||
import java.io.FilterReader | ||
import java.io.Reader | ||
import java.nio.charset.StandardCharsets | ||
|
||
class PackageRelocateReader(input: Reader): FilterReader(input) { | ||
|
||
var remapper: PackageRelocator by MustSet() | ||
|
||
val contents = ByteArrayOutputStream().use { out -> | ||
out.writer(StandardCharsets.ISO_8859_1).use { writer -> | ||
input.copyTo(writer) | ||
} | ||
input.close() | ||
out.toByteArray() | ||
} | ||
|
||
val changedContents: Reader by lazy { | ||
val reader = ClassReader(contents) | ||
val writer = ClassWriter(0) | ||
reader.accept(ClassRemapper(writer, remapper), 0) | ||
ByteArrayInputStream(writer.toByteArray()).bufferedReader(StandardCharsets.ISO_8859_1) | ||
} | ||
|
||
override fun read(): Int { | ||
return changedContents.read() | ||
} | ||
|
||
override fun read(cbuf: CharArray, off: Int, len: Int): Int { | ||
return changedContents.read(cbuf, off, len) | ||
} | ||
|
||
override fun skip(n: Long): Long { | ||
return changedContents.skip(n) | ||
} | ||
|
||
override fun ready(): Boolean { | ||
return changedContents.ready() | ||
} | ||
|
||
override fun markSupported(): Boolean { | ||
return changedContents.markSupported() | ||
} | ||
|
||
override fun mark(readAheadLimit: Int) { | ||
changedContents.mark(readAheadLimit) | ||
} | ||
|
||
override fun reset() { | ||
changedContents.reset() | ||
} | ||
|
||
override fun close() { | ||
changedContents.close() | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
buildSrc/src/main/kotlin/xyz/wagyourtail/gradle/shadow/PackageRelocator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package xyz.wagyourtail.gradle.shadow | ||
|
||
import org.objectweb.asm.commons.Remapper | ||
|
||
class PackageRelocator(val map: Map<String, String>) : Remapper() { | ||
|
||
override fun map(internalName: String): String { | ||
for ((from, to) in map) { | ||
if (internalName.startsWith(from)) { | ||
return to + internalName.substring(from.length) | ||
} | ||
} | ||
return internalName | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
buildSrc/src/main/kotlin/xyz/wagyourtail/gradle/shadow/ShadowJar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package xyz.wagyourtail.gradle.shadow | ||
|
||
import org.gradle.api.file.FileCollection | ||
import org.gradle.api.provider.ListProperty | ||
import org.gradle.api.provider.MapProperty | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.api.tasks.Optional | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.jvm.tasks.Jar | ||
import java.nio.charset.StandardCharsets | ||
|
||
abstract class ShadowJar : Jar() { | ||
|
||
@get:Internal | ||
abstract val shadowContents: ListProperty<FileCollection> | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val relocatePackages: MapProperty<String, String> | ||
|
||
init { | ||
group = "Shadow" | ||
description = "Shadow the jar with the specified configurations" | ||
|
||
shadowContents.convention(mutableListOf()).finalizeValueOnRead() | ||
relocatePackages.convention(mutableMapOf()).finalizeValueOnRead() | ||
archiveClassifier.convention("all") | ||
} | ||
|
||
fun relocate(from: String, to: String) { | ||
relocatePackages.put(from, to) | ||
} | ||
|
||
@TaskAction | ||
fun runTask() { | ||
for (fileCollection in shadowContents.get()) { | ||
for (file in fileCollection) { | ||
if (!file.exists()) continue | ||
if (file.isDirectory) { | ||
// copy directory | ||
from(file) | ||
} else { | ||
// copy file | ||
from(project.zipTree(file)) | ||
} | ||
} | ||
} | ||
|
||
filteringCharset = StandardCharsets.ISO_8859_1.name() | ||
includeEmptyDirs = false | ||
|
||
if (relocatePackages.getOrElse(emptyMap()).isNotEmpty()) { | ||
val map = relocatePackages.get() | ||
.mapKeys { it.key.replace('.', '/') } | ||
.mapKeys { if (!it.key.endsWith("/")) it.key + "/" else it.key } | ||
.mapValues { it.value.replace('.', '/') } | ||
.mapValues { if (!it.value.endsWith("/")) it.value + "/" else it.value } | ||
val rel = PackageRelocator(map) | ||
eachFile { | ||
if (!it.path.endsWith(".class")) return@eachFile | ||
it.path = rel.map(it.path) | ||
it.filter(mapOf("remapper" to rel), PackageRelocateReader::class.java) | ||
} | ||
} | ||
|
||
// call super | ||
copy() | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
buildSrc/src/main/kotlin/xyz/wagyourtail/gradle/utils/MustSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package xyz.wagyourtail.gradle.utils | ||
|
||
import kotlin.properties.ReadWriteProperty | ||
import kotlin.reflect.KProperty | ||
|
||
class MustSet<T> : ReadWriteProperty<Any?, T> { | ||
|
||
@Suppress("ClassName") | ||
private object UNINITIALIZED_VALUE | ||
|
||
private var prop: Any? = UNINITIALIZED_VALUE | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun getValue(thisRef: Any?, property: KProperty<*>): T { | ||
return if (prop == UNINITIALIZED_VALUE) { | ||
synchronized(this) { | ||
return if (prop == UNINITIALIZED_VALUE) throw IllegalStateException("Property ${property.name} must be set before use") else prop as T | ||
} | ||
} else prop as T | ||
} | ||
|
||
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { | ||
synchronized(this) { | ||
prop = value | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/agent/java/xyz/wagyourtail/unimined/expect/ExpectPlatformAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package xyz.wagyourtail.unimined.expect; | ||
|
||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.tree.ClassNode; | ||
|
||
import java.lang.instrument.ClassFileTransformer; | ||
import java.lang.instrument.Instrumentation; | ||
import java.security.ProtectionDomain; | ||
|
||
public class ExpectPlatformAgent { | ||
private static final String EXPECT_PLATFORM = "expect.platform"; | ||
private static final String REMAP = "expect.remap"; | ||
private static final String platform = System.getProperty(EXPECT_PLATFORM); | ||
private static final String remap = System.getProperty(REMAP); | ||
|
||
|
||
public static void premain(String args, Instrumentation inst) { | ||
if (platform == null) { | ||
throw new IllegalStateException("-D" + EXPECT_PLATFORM + " not set"); | ||
} | ||
inst.addTransformer(new ExpectPlatformTransformer()); | ||
} | ||
|
||
public static void agentmain(String args, Instrumentation inst) { | ||
premain(args, inst); | ||
} | ||
|
||
public static class ExpectPlatformTransformer implements ClassFileTransformer { | ||
TransformPlatform transformPlatform = new TransformPlatform(platform, remap); | ||
|
||
@Override | ||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { | ||
ClassReader reader = new ClassReader(classfileBuffer); | ||
ClassNode classNode = new ClassNode(); | ||
reader.accept(classNode, 0); | ||
|
||
transformPlatform.transform(classNode); | ||
|
||
ClassWriter writer = new ClassWriter(reader, 0); | ||
classNode.accept(writer); | ||
|
||
return writer.toByteArray(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.