Skip to content

Commit

Permalink
Merge branch 'refs/heads/1.19/asm-fun' into 1.19/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Jun 7, 2024
2 parents a0af067 + fc821c1 commit c6fec3f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
43 changes: 41 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ import groovy.json.JsonSlurper
import net.fabricmc.loom.api.LoomGradleExtensionAPI
import net.fabricmc.loom.task.RemapJarTask
import org.gradle.configurationcache.extensions.capitalized
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.tree.AnnotationNode
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.tree.FieldNode
import org.objectweb.asm.tree.MethodNode
import java.io.ByteArrayOutputStream
import java.util.*
import java.util.function.Predicate
import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.jar.JarOutputStream
Expand Down Expand Up @@ -159,7 +167,7 @@ subprojects {
dependsOn(shadowJar)
archiveClassifier = null
doLast {
squishJar(outputs.files.singleFile)
transformJar(outputs.files.singleFile)
}
}

Expand Down Expand Up @@ -237,7 +245,7 @@ subprojects {
}
}

fun squishJar(jar: File) {
fun transformJar(jar: File) {
val contents = linkedMapOf<String, ByteArray>()
JarFile(jar).use {
it.entries().asIterator().forEach { entry ->
Expand All @@ -257,6 +265,8 @@ fun squishJar(jar: File) {

if (name.endsWith(".json") || name.endsWith(".mcmeta")) {
data = (JsonOutput.toJson(JsonSlurper().parse(data)).toByteArray())
} else if (name.endsWith(".class")) {
data = transformClass(data)
}

out.putNextEntry(JarEntry(name))
Expand All @@ -268,6 +278,35 @@ fun squishJar(jar: File) {
}
}

fun transformClass(bytes: ByteArray): ByteArray {
val node = ClassNode()
ClassReader(bytes).accept(node, 0)

// Remove Methods & Field Annotated with @DevEnvMixin
node.methods.removeIf { methodNode: MethodNode -> removeIfDevMixin(node.name, methodNode.visibleAnnotations) }
// Disabled as I don't feel ok with people being able to remove these
//node.fields.removeIf { fieldNode: FieldNode -> removeIfDevMixin(fieldNode.visibleAnnotations) }

return ClassWriter(0).also { node.accept(it) }.toByteArray()
}

fun removeIfDevMixin(nodeName: String, visibleAnnotations: List<AnnotationNode>?): Boolean {
// Don't remove methods if it's not a GHA build/Release build
if (buildNumber == null || !nodeName.lowercase(Locale.ROOT).matches(Regex(".*\\/mixin\\/.*Mixin")))
return false

if (visibleAnnotations != null) {
for (annotationNode in visibleAnnotations) {
if (annotationNode.desc == "Lcom/railwayteam/railways/annotation/mixin/DevEnvMixin;") {
println("Removed Method/Field Annotated With @DevEnvMixin from: $nodeName")
return true
}
}
}

return false
}

tasks.create("railwaysPublish") {
when (val platform = System.getenv("PLATFORM")) {
"both" -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
/**
* You bear all the consequences of using this. Don't shoot yourself in the foot I guess? Try wearing proper mixin equipment?
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DevMixin { }
public @interface DevEnvMixin {}
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ private void autoWhistle(BlockState pState, Level pLevel, BlockPos pPos, Player
return;
}

//System.out.println("DB1");

if (train.runtime.getSchedule() != null && !train.runtime.isAutoSchedule) {
ItemStack scheduleStack = train.runtime.returnSchedule();
if (!scheduleStack.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.railwayteam.railways.Railways;
import com.railwayteam.railways.annotation.mixin.DevEnvMixin;
import com.railwayteam.railways.util.DevCapeUtils;
import com.railwayteam.railways.util.Utils;
import net.minecraft.client.multiplayer.PlayerInfo;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
Expand All @@ -47,15 +47,14 @@ public class MixinPlayerInfo {
@Unique private boolean railways$texturesLoaded;
@Unique private static final ResourceLocation DEV_CAPE = Railways.asResource("textures/misc/dev_cape.png");

// Replaces skin inside the dev env with the conductor skin, won't crash if it fails to apply
@Inject(method = "getSkinLocation", at = @At("HEAD"), require = 0)
// Replaces skin inside the dev env with the conductor skin
@DevEnvMixin
@Inject(method = "getSkinLocation", at = @At("HEAD"))
private void registerSkinTextures(CallbackInfoReturnable<ResourceLocation> cir) {
if (Utils.isDevEnv()) {
this.textureLocations.put(
MinecraftProfileTexture.Type.SKIN,
Railways.asResource("textures/misc/devenv_skin.png")
);
}
this.textureLocations.put(
MinecraftProfileTexture.Type.SKIN,
Railways.asResource("textures/misc/devenv_skin.png")
);
}

@Inject(method = "getCapeLocation", at = @At("HEAD"))
Expand All @@ -74,16 +73,14 @@ private void skipCapeIfNeeded(CallbackInfoReturnable<ResourceLocation> cir) {
}

// Replaces skin model inside the dev env with the default "steve" skin model
@DevEnvMixin
@Inject(method = "registerTextures",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/resources/SkinManager;registerSkins(Lcom/mojang/authlib/GameProfile;Lnet/minecraft/client/resources/SkinManager$SkinTextureCallback;Z)V"
),
require = 0 // Dev Env Mixin, Shouldn't crash if it fails
)
)
private void railways$setModelToLarge(CallbackInfo ci) {
if (Utils.isDevEnv()) {
skinModel = "default";
}
skinModel = "default";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.railwayteam.railways.util;

import com.railwayteam.railways.annotation.mixin.ConditionalMixin;
import com.railwayteam.railways.annotation.mixin.DevMixin;
import com.railwayteam.railways.annotation.mixin.DevEnvMixin;
import com.railwayteam.railways.compat.Mods;
import com.railwayteam.railways.mixin.CRMixinPlugin;
import org.objectweb.asm.Type;
Expand All @@ -51,7 +51,7 @@ public static boolean shouldApply(String className) {
shouldApply = anyModsLoaded == applyIfPresent;
CRMixinPlugin.LOGGER.debug("{} is{}being applied because the mod(s) {} are{}loaded", className, shouldApply ? " " : " not ", mods, anyModsLoaded ? " " : " not ");
}
if (node.desc.equals(Type.getDescriptor(DevMixin.class))) {
if (node.desc.equals(Type.getDescriptor(DevEnvMixin.class))) {
shouldApply &= Utils.isDevEnv();
}
}
Expand Down

0 comments on commit c6fec3f

Please sign in to comment.