Skip to content

Commit

Permalink
Allow specifying @ExpectPlatform package extension
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Nov 14, 2023
1 parent 0a64248 commit ea4e0ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ public class BuiltinProperties {
public static final String REFMAP_NAME = "architectury.refmap.name";
public static final String MCMETA_VERSION = "architectury.mcmeta.version";
public static final String PLATFORM_NAME = "architectury.platform.name";
public static final String PLATFORM_PACKAGE = "architectury.platform.package";
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import dev.architectury.transformer.transformers.base.ClassEditTransformer;
import dev.architectury.transformer.transformers.base.edit.TransformerContext;
import dev.architectury.transformer.util.Logger;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
Expand All @@ -43,10 +44,13 @@
import static dev.architectury.transformer.transformers.RemapInjectables.getUniqueIdentifier;

public class TransformExpectPlatform implements AssetEditTransformer, ClassEditTransformer {
private String platformPackage = null;
private String uniqueIdentifier = null;

@Override
public void supplyProperties(JsonObject json) {
platformPackage = json.has(BuiltinProperties.PLATFORM_PACKAGE) ?
json.getAsJsonPrimitive(BuiltinProperties.PLATFORM_PACKAGE).getAsString() : null;
uniqueIdentifier = json.has(BuiltinProperties.UNIQUE_IDENTIFIER) ?
json.getAsJsonPrimitive(BuiltinProperties.UNIQUE_IDENTIFIER).getAsString() : null;
}
Expand Down Expand Up @@ -109,7 +113,7 @@ public ClassNode doEdit(String name, ClassNode node) {
stackIndex += argumentType.getSize();
}

method.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, getPlatformClass(node.name), method.name, method.desc));
method.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, getPlatformClass(platformPackage, node.name), method.name, method.desc));
method.instructions.add(new InsnNode(type.getReturnType().getOpcode(Opcodes.IRETURN)));

method.maxStack = -1;
Expand All @@ -124,11 +128,14 @@ public ClassNode doEdit(String name, ClassNode node) {
return node;
}

private static String getPlatformClass(String lookupClass) {
String platform = System.getProperty(BuiltinProperties.PLATFORM_NAME);
Preconditions.checkNotNull(platform, BuiltinProperties.PLATFORM_NAME + " is not present!");
if (platform.equals("quilt")) platform = "fabric";
if (platform.equals("neoforge")) platform = "forge";
private static String getPlatformClass(@Nullable String platformPackage, String lookupClass) {
String platform = MoreObjects.firstNonNull(platformPackage, System.getProperty(BuiltinProperties.PLATFORM_PACKAGE));
if (platform == null) {
platform = System.getProperty(BuiltinProperties.PLATFORM_NAME);
Preconditions.checkNotNull(platform, BuiltinProperties.PLATFORM_NAME + " is not present!");
if (platform.equals("quilt")) platform = "fabric";
}

String lookupType = lookupClass.replace("$", "") + "Impl";

return lookupType.substring(0, lookupType.lastIndexOf('/')) + "/" + platform + "/" +
Expand Down

0 comments on commit ea4e0ec

Please sign in to comment.