Skip to content

Commit

Permalink
Turbo ASM
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsePattern committed May 27, 2024
1 parent ccbd702 commit 0d373e9
Show file tree
Hide file tree
Showing 17 changed files with 1,093 additions and 123 deletions.
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A library for 1.7.10 with lots of useful stuff, licensed under the LGPLv3 licens
| [optifine](src/main/java/com/falsepattern/lib/optifine) | Tools for messing with OptiFine |
| [text](src/main/java/com/falsepattern/lib/text) | Better Chat and GUI text processing |
| [toasts](src/main/java/com/falsepattern/lib/toasts) | The toast system from newer versions, with some extras |
| [turboasm](src/main/java/com/falsepattern/lib/turboasm) | A forge-style class transformer port of RFB's transformer API. |
| [util](src/main/java/com/falsepattern/lib/util) | Additional utilities that do not fit the above categories, see below for more information |

The contents of the [util](src/main/java/com/falsepattern/lib/util) package so far:
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/falsepattern/lib/StableAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
* You may set the {@link #since()} attribute to "__INTERNAL__", this will signal that even though the specific class/member
* has been marked as stable, it is still for internal use only. This may be done for reference purposes in multi-developer
* projects, where you need to communicate intent even in internal code.
* <p>
* Similarly, you may also set the {@link #since()} attribute to "__EXPERIMENTAL__", this will signal that the given API
* is highly experimental and may be removed or change at any time, however, using it in third party projects is not an
* error, unlike __INTERNAL__, but if any issues arise from the usage of the API or the API changing, the API developer
* shall not be blamed for it.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
*/
package com.falsepattern.lib.asm;

import com.falsepattern.lib.DeprecationDetails;
import com.falsepattern.lib.StableAPI;
import org.objectweb.asm.tree.ClassNode;

/**
* See: {@link com.falsepattern.lib.turboasm.TurboClassTransformer}.
* This class will not be removed, for backwards compatibility reasons.
*/
@StableAPI(since = "0.10.0")
@Deprecated
@DeprecationDetails(deprecatedSince = "1.2.0")
public interface IClassNodeTransformer {
@StableAPI.Expose
String getName();
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/falsepattern/lib/asm/SmartTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package com.falsepattern.lib.asm;

import com.falsepattern.lib.DeprecationDetails;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.internal.asm.CoreLoadingPlugin;
import lombok.val;
Expand All @@ -36,9 +37,12 @@
import java.util.List;

/**
* An ASM transformation dispatcher utility, inspired by mixins.
* See: {@link com.falsepattern.lib.turboasm.MergeableTurboTransformer}.
* This class will not be removed, for backwards compatibility reasons.
*/
@StableAPI(since = "0.10.0")
@Deprecated
@DeprecationDetails(deprecatedSince = "1.2.0")
public interface SmartTransformer extends IClassTransformer {
@StableAPI.Expose
Logger logger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class FalsePatternLib {
@SidedProxy(clientSide = Tags.GROUPNAME + ".internal.proxy.ClientProxy",
serverSide = Tags.GROUPNAME + ".internal.proxy.CommonProxy")
private static CommonProxy proxy;
static {
CoreLoadingPlugin.mergeTurboTransformers();
}

public FalsePatternLib() {
FPLog.LOG.info("Version " + Tags.VERSION + " initialized!");
Expand All @@ -74,9 +77,6 @@ public void init(FMLInitializationEvent e) {

@Mod.EventHandler
public void postInit(FMLPostInitializationEvent e) {
if (Loader.isModLoaded("gasstation")) {
CoreLoadingPlugin.validateGasStation();
}
proxy.postInit(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@
import com.falsepattern.lib.internal.Tags;
import com.falsepattern.lib.internal.impl.dependencies.DependencyLoaderImpl;
import com.falsepattern.lib.mapping.MappingManager;
import com.falsepattern.lib.turboasm.MergeableTurboTransformer;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.val;

import net.minecraft.launchwrapper.IClassTransformer;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import cpw.mods.fml.relauncher.IFMLLoadingPlugin;
import cpw.mods.fml.relauncher.IFMLLoadingPlugin.MCVersion;
import cpw.mods.fml.relauncher.IFMLLoadingPlugin.Name;
import cpw.mods.fml.relauncher.IFMLLoadingPlugin.SortingIndex;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.util.List;
import java.util.Map;

import static com.falsepattern.lib.mixin.MixinInfo.isClassPresentSafe;
Expand All @@ -44,7 +54,7 @@
@MCVersion("1.7.10")
@Name(Tags.MODID)
@SortingIndex(500)
@IFMLLoadingPlugin.TransformerExclusions({Tags.GROUPNAME + ".internal.asm", Tags.GROUPNAME + ".asm"})
@IFMLLoadingPlugin.TransformerExclusions({Tags.GROUPNAME + ".internal.asm", Tags.GROUPNAME + ".asm", Tags.GROUPNAME + ".turboasm"})
public class CoreLoadingPlugin implements IFMLLoadingPlugin {
@Getter
private static boolean obfuscated;
Expand Down Expand Up @@ -117,25 +127,6 @@ private static Error skillIssue(String message) {
return skillIssue;
}

public static void validateGasStation() {
FPLog.LOG.info("Got any gas?");
//Make sure everything is loaded correctly, crash if gasstation is bugged
// @formatter:off
if (!isClassPresentSafe("com.falsepattern.gasstation.core.GasStationCore") //Validate core class
|| !isClassPresentSafe("makamys.mixingasm.api.TransformerInclusions") //Validate the mixingasm compat
|| !isClassPresentSafe("ru.timeconqueror.spongemixins.core.SpongeMixinsCore") //Validate the spongemixins compat
|| !isClassPresentSafe("io.github.tox1cozz.mixinbooterlegacy.MixinBooterLegacyPlugin") //Validate the MBL compat
|| !isClassPresentSafe("org.spongepowered.asm.lib.Opcodes") //Validate correct mixins class
|| isClassPresentSafe("org.spongepowered.libraries.org.objectweb.asm.Opcodes")
) {
FPLog.LOG.fatal("Somebody put diesel in my gas tank!");
throw new Error("Failed to validate your GasStation mixin plugin installation. "
+ "Please make sure you have the latest GasStation installed from the official source: "
+ "https:/FalsePattern/GasStation");
}
// @formatter:on
}

@Override
public String[] getASMTransformerClass() {
return new String[]{Tags.GROUPNAME + ".internal.asm.FPTransformer"};
Expand All @@ -154,10 +145,32 @@ public String getSetupClass() {
@Override
public void injectData(Map<String, Object> data) {
obfuscated = (Boolean) data.get("runtimeDeobfuscationEnabled");
mergeTurboTransformers();
}

@Override
public String getAccessTransformerClass() {
return null;
}

@SneakyThrows
public static synchronized void mergeTurboTransformers() {
val f = LaunchClassLoader.class.getDeclaredField("transformers");
f.setAccessible(true);

val transformers = (List<IClassTransformer>) f.get(Launch.classLoader);
for (int i = 0; i < transformers.size() - 1; i++) {
val a = transformers.get(i);
if (!(a instanceof MergeableTurboTransformer))
continue;

val b = transformers.get(i + 1);
if (!(b instanceof MergeableTurboTransformer))
continue;

transformers.remove(i + 1);
transformers.set(i, MergeableTurboTransformer.merge((MergeableTurboTransformer) a, (MergeableTurboTransformer) b));
i--;
}
}
}
30 changes: 7 additions & 23 deletions src/main/java/com/falsepattern/lib/internal/asm/FPTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,25 @@
package com.falsepattern.lib.internal.asm;

import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.asm.IClassNodeTransformer;
import com.falsepattern.lib.asm.SmartTransformer;
import com.falsepattern.lib.internal.Tags;
import com.falsepattern.lib.internal.asm.transformers.ConfigOrderTransformer;
import com.falsepattern.lib.internal.asm.transformers.GasStationValidatorTransformer;
import com.falsepattern.lib.internal.asm.transformers.IMixinPluginTransformer;
import com.falsepattern.lib.internal.asm.transformers.ITypeDiscovererTransformer;
import com.falsepattern.lib.internal.asm.transformers.MixinPluginTransformer;
import com.falsepattern.lib.internal.asm.transformers.TypeDiscovererModuleInfoSilencer;
import com.falsepattern.lib.internal.impl.optifine.OptiFineTransformerHooksImpl;
import lombok.Getter;
import com.falsepattern.lib.turboasm.MergeableTurboTransformer;
import lombok.experimental.Accessors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Arrays;
import java.util.List;

@Accessors(fluent = true)
@StableAPI(since = "__INTERNAL__")
public class FPTransformer implements SmartTransformer {
public static final Logger LOG = LogManager.getLogger(Tags.MODNAME + " ASM");

@Getter
private final List<IClassNodeTransformer> transformers;

@Getter
private final Logger logger = LOG;

public class FPTransformer extends MergeableTurboTransformer {
static {
OptiFineTransformerHooksImpl.init();
}

public FPTransformer() {
transformers = Arrays.asList(new IMixinPluginTransformer(),
new ITypeDiscovererTransformer(),
new GasStationValidatorTransformer(),
new ConfigOrderTransformer());
super(Arrays.asList(new MixinPluginTransformer(),
new TypeDiscovererModuleInfoSilencer(),
new ConfigOrderTransformer()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,36 @@

package com.falsepattern.lib.internal.asm.transformers;

import com.falsepattern.lib.asm.IClassNodeTransformer;
import com.falsepattern.lib.config.Config;
import com.falsepattern.lib.internal.Tags;
import com.falsepattern.lib.internal.impl.config.DeclOrderInternal;
import com.falsepattern.lib.turboasm.ClassNodeHandle;
import com.falsepattern.lib.turboasm.TurboClassTransformer;
import lombok.val;
import org.jetbrains.annotations.NotNull;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;

public class ConfigOrderTransformer implements IClassNodeTransformer {
public class ConfigOrderTransformer implements TurboClassTransformer {
private static final String DESC_CONFIG = Type.getDescriptor(Config.class);
private static final String DESC_CONFIG_IGNORE = Type.getDescriptor(Config.Ignore.class);
private static final String DESC_ORDER = Type.getDescriptor(DeclOrderInternal.class);

@Override
public String getName() {
public String name() {
return "ConfigOrderTransformer";
}

@Override
public boolean shouldTransform(ClassNode cn, String transformedName, boolean obfuscated) {
public String owner() {
return Tags.MODNAME;
}

@Override
public boolean shouldTransformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) {
val cn = classNode.getNode();
if (cn == null)
return false;
if (cn.visibleAnnotations != null) {
for (val ann : cn.visibleAnnotations) {
if (DESC_CONFIG.equals(ann.desc)) {
Expand All @@ -54,7 +64,10 @@ public boolean shouldTransform(ClassNode cn, String transformedName, boolean obf
}

@Override
public void transform(ClassNode cn, String transformedName, boolean obfuscated) {
public void transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) {
val cn = classNode.getNode();
if (cn == null)
return;
int order = 0;
outer:
for (val field : cn.fields) {
Expand Down

This file was deleted.

Loading

0 comments on commit 0d373e9

Please sign in to comment.