Skip to content

Commit

Permalink
fix forge
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Jun 25, 2024
1 parent 7dc324d commit 4bf13b1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
49 changes: 43 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ 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.Type
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 @@ -288,26 +287,64 @@ fun transformClass(bytes: ByteArray): ByteArray {
// Disabled as I don't feel ok with people being able to remove these
//node.fields.removeIf { fieldNode: FieldNode -> removeIfDevMixin(fieldNode.visibleAnnotations) }

if (node.visibleAnnotations != null) {
// Cache the field, so we don't CME the list during the remove
val annotationNodes = node.visibleAnnotations.toList()
for (annotationNode in annotationNodes) {
if (annotationNode.desc.equals("Lcom/railwayteam/railways/annotation/compiletime/ImplementsToExtends;")) {
node.visibleAnnotations.remove(annotationNode)

val type = getValueFromAnnotation<Type>(annotationNode, "value")!!
val loaderEnum = getValueFromAnnotation<Array<String>>(annotationNode, "loader")!!

if (project.name == loaderEnum[1]) {
node.interfaces.remove(type.internalName)

node.superName = type.internalName
}
}
}
}

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")))
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")
if (annotationNode.desc == "Lcom/railwayteam/railways/annotation/mixin/DevEnvMixin;")
return true
}
}
}

return false
}

fun <T> getValueFromAnnotation(annotation: AnnotationNode?, key: String): T? {
var getNextValue = false

if (annotation?.values == null) {
return null
}

// Keys and value are stored in successive pairs, search for the key and if found return the following entry
for (value in annotation.values) {
if (getNextValue) {
@Suppress("UNCHECKED_CAST")
return value as T
}
if (value == key) {
getNextValue = true
}
}

return null
}

tasks.create("railwaysPublish") {
when (val platform = System.getenv("PLATFORM")) {
"both" -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Steam 'n' Rails
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.railwayteam.railways.annotation.compiletime;

import com.railwayteam.railways.multiloader.Loader;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Remapping doesn't exist for this hell, don't expect this to work with minecraft classes/interfaces
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface ImplementsToExtends {
Class<?> value();

Loader loader();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Vector3f;
import com.railwayteam.railways.annotation.compiletime.ImplementsToExtends;
import com.railwayteam.railways.mixin_interfaces.IForceRenderingSodium;
import com.railwayteam.railways.mixin_interfaces.IHasCustomOutline;
import com.railwayteam.railways.multiloader.Loader;
import com.railwayteam.railways.registry.CRShapes;
import com.simibubi.create.content.equipment.extendoGrip.ExtendoGripItem;
import com.simibubi.create.content.equipment.wrench.IWrenchable;
Expand Down Expand Up @@ -67,6 +69,7 @@
import java.util.Locale;
import java.util.function.Predicate;

@ImplementsToExtends(value = ReducedDestroyEffects.class, loader = Loader.FORGE)
public class BoilerBlock extends Block implements IWrenchable, IForceRenderingSodium, IHasCustomOutline, ReducedDestroyEffects {
public static final int placementHelperId = PlacementHelpers.register(new PlacementHelper());

Expand Down

0 comments on commit 4bf13b1

Please sign in to comment.