Skip to content

Commit

Permalink
Implement support for mods to load transformers and remove Minecraft …
Browse files Browse the repository at this point in the history
…runtime deobfuscation from the loader
  • Loading branch information
ChachyDev committed Aug 5, 2021
1 parent f01433e commit eb2b654
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 111 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ext {
}

group "org.bookmc"
version "0.7.0${local ? "+local" : ""}"
version "0.7.1${local ? "+local" : ""}"

repositories {
mavenCentral()
Expand Down Expand Up @@ -52,8 +52,6 @@ dependencies {
include "org.ow2.asm:asm-commons:9.2"

include "com.github.zafarkhaja:java-semver:0.9.0"

include "com.github.BookMC:srg-processor:1.1.0"
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.bookmc.loader.api.classloader;

import org.bookmc.loader.api.launch.transform.QuiltRemapper;
import org.bookmc.loader.api.launch.transform.QuiltTransformer;
import org.bookmc.loader.impl.launch.Launcher;
import org.bookmc.loader.impl.obfuscation.MinecraftObfuscationTransformer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -37,7 +37,9 @@ default byte[] getClassBytes(String name, boolean transform) {
e.printStackTrace();
}

classBytes = MinecraftObfuscationTransformer.INSTANCE.transform(name, classBytes);
for (QuiltRemapper remapper : Launcher.getQuiltClassLoader().getRemappers()) {
classBytes = remapper.transform(name, classBytes);
}

if (transform) {
for (QuiltTransformer transformer : Launcher.getQuiltClassLoader().getTransformers()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.bookmc.loader.api.launch.transform;

/**
* A specialised transformer for remapping the game at runtime
* via a transformer.
*
* @author ChachyDev
* @since 0.7.1
*/
public interface QuiltRemapper extends QuiltTransformer {
}
4 changes: 4 additions & 0 deletions src/main/java/org/bookmc/loader/api/vessel/ModVessel.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ default String[] getTransformers() {
return new String[0];
}

default String[] getRemappers() {
return new String[0];
}

default boolean isInternal() {
return getAbstractedClassLoader().getClassLoader() instanceof QuiltClassLoader;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/bookmc/loader/impl/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bookmc.loader.api.classloader.ModClassLoader;
import org.bookmc.loader.api.compat.CompatiblityLayer;
import org.bookmc.loader.api.exception.IllegalDependencyException;
import org.bookmc.loader.api.launch.transform.QuiltRemapper;
import org.bookmc.loader.api.launch.transform.QuiltTransformer;
import org.bookmc.loader.api.vessel.ModVessel;
import org.bookmc.loader.api.vessel.dependency.ModDependency;
Expand All @@ -35,6 +36,7 @@

public class Loader {
public static final List<ModVessel> loaded = new ArrayList<>();
private static final Logger LOGGER = LogManager.getLogger(Loader.class);
private static final List<ModResolver> resolvers = new ArrayList<>();
private static final Map<String, ModVessel> modVessels = new HashMap<>();
private static final List<ModCandidate> candidates = new ArrayList<>();
Expand Down Expand Up @@ -214,6 +216,25 @@ public static void discoverAndLoad(File modsDirectory, Environment environment)

for (ModVessel vessel : Loader.getModVessels()) {
Loader.loadCompatibilityLayer(vessel, vessel.getAbstractedClassLoader());
for (String transformer : vessel.getTransformers()) {
try {
Class<? extends QuiltTransformer> clazz = Launcher.loadClass(transformer, true)
.asSubclass(QuiltTransformer.class);
Launcher.getQuiltClassLoader().registerTransformer(clazz);
} catch (Exception e) {
LOGGER.error("Could not load transformer {}", transformer, e);
}
}

for (String remapper : vessel.getRemappers()) {
try {
Class<? extends QuiltRemapper> clazz = Launcher.loadClass(remapper, true)
.asSubclass(QuiltRemapper.class);
Launcher.getQuiltClassLoader().registerRemapper(clazz);
} catch (Exception e) {
LOGGER.error("Could not load remapper {}", remapper, e);
}
}
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/main/java/org/bookmc/loader/impl/launch/Quilt.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package org.bookmc.loader.impl.launch;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bookmc.loader.impl.launch.bootstrap.QuiltBootstrap;
import org.bookmc.loader.impl.launch.provider.ArgumentHandler;
import org.bookmc.loader.impl.launch.provider.DefaultGameProvider;
import org.bookmc.loader.impl.launch.transform.QuiltClassLoader;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.MixinEnvironment;

import java.lang.reflect.Method;

public class Quilt {
static QuiltClassLoader classLoader;
private static final Logger LOGGER = LogManager.getLogger(Quilt.class);

public static void main(String[] args) throws Throwable {
classLoader = new QuiltClassLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.bookmc.loader.api.vessel.environment.Environment;
import org.bookmc.loader.impl.Loader;
import org.bookmc.loader.impl.launch.Launcher;
import org.bookmc.loader.impl.launch.Quilt;
import org.bookmc.loader.impl.vessel.dummy.BookLoaderVessel;
import org.bookmc.loader.impl.vessel.dummy.JavaModVessel;
import org.bookmc.loader.impl.vessel.dummy.MinecraftModVessel;
Expand Down Expand Up @@ -48,7 +47,6 @@ public static void plug() {
} else {
LOGGER.error("Failed to detect the game version! Mods inside the game version's mod folder will not be loaded!");
}

// We have to bootstrap mixin after all the mods have been discovered
// so that mods can have their mixin files discovered.
boostrapMixin();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bookmc.loader.impl.launch.transform;

import org.bookmc.loader.api.classloader.IQuiltClassLoader;
import org.bookmc.loader.api.launch.transform.QuiltRemapper;
import org.bookmc.loader.api.launch.transform.QuiltTransformer;
import org.bookmc.loader.impl.launch.Launcher;

Expand All @@ -10,7 +11,8 @@

public class QuiltClassLoader extends URLClassLoader implements IQuiltClassLoader {
private final Map<String, byte[]> classCache = new HashMap<>();
private final List<QuiltTransformer> transformers = new ArrayList<>();
private final Map<String, QuiltTransformer> transformers = new HashMap<>();
private final Map<String, QuiltRemapper> quiltRemappers = new HashMap<>();
private final List<String> exclusions = new ArrayList<>();

public QuiltClassLoader() {
Expand All @@ -20,6 +22,7 @@ public QuiltClassLoader() {
addClassLoaderExclusion("java.");
addClassLoaderExclusion("javax.");
addClassLoaderExclusion("org.lwjgl.");
addClassLoaderExclusion("org.objectweb.");
addClassLoaderExclusion("org.apache.logging.");
addClassLoaderExclusion("org.bookmc.loader.");
}
Expand Down Expand Up @@ -102,11 +105,27 @@ public Class<?> getLoadedClass(String name) {
}
}

public void registerTransformer(Class<? extends QuiltTransformer> transformer) {
public void registerTransformer(Class<? extends QuiltTransformer> clazz) {
try {
transformers.add((QuiltTransformer) loadClass(transformer.getName()).newInstance());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
String name = clazz.getName();
QuiltTransformer transformer = (QuiltTransformer) loadClass(name).newInstance();
if (!transformers.containsKey(name)) {
transformers.put(name, transformer);
}
} catch (Throwable t) {
t.printStackTrace();
}
}

public void registerRemapper(Class<? extends QuiltRemapper> clazz) {
try {
String name = clazz.getName();
QuiltRemapper remapper = (QuiltRemapper) loadClass(name).newInstance();
if (!quiltRemappers.containsKey(name)) {
quiltRemappers.put(name, remapper);
}
} catch (Throwable t) {
t.printStackTrace();
}
}

Expand All @@ -115,10 +134,14 @@ public void addClassLoaderExclusion(String exclusion) {
}

public List<QuiltTransformer> getTransformers() {
return Collections.unmodifiableList(transformers);
return Collections.unmodifiableList(new ArrayList<>(transformers.values()));
}

public List<String> getExclusions() {
return Collections.unmodifiableList(exclusions);
}

public List<QuiltRemapper> getRemappers() {
return Collections.unmodifiableList(new ArrayList<>(quiltRemappers.values()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.bookmc.loader.impl.launch.Launcher;
import org.bookmc.loader.impl.launch.Quilt;
import org.bookmc.loader.shared.utils.ClassUtils;
import org.spongepowered.asm.lib.tree.ClassNode;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.service.IClassBytecodeProvider;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,16 @@ public String getLanguageAdapter() {

@Override
public String[] getTransformers() {
if (!object.has("transformers") || !object.has("asm")) return new String[0];
if (!object.has("transformers") && !object.has("asm")) return new String[0];
return toString(object.has("transformers") ? object.getAsJsonArray("transformers") : object.getAsJsonArray("asm"));
}

@Override
public String[] getRemappers() {
if (!object.has("remappers")) return new String[0];
return toString(object.getAsJsonArray("remappers"));
}

private String[] toString(JsonArray array) {
int size = array.size();
String[] strings = new String[size];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.github.zafarkhaja.semver.Version;

public enum VersionOperation {
GREATER {
GREATER_THAN {
@Override
public boolean allow(Version required, Version other) {
return required.greaterThan(other);
Expand Down Expand Up @@ -34,12 +34,10 @@ public boolean allow(Version required, Version other) {
}
};

public abstract boolean allow(Version required, Version other);

public static VersionOperation of(String required) {
if (required.startsWith(">")) return GREATER;
if (required.startsWith(">=")) return GREATER_THAN_OR_EQUAL_TO;
if (required.startsWith("<=")) return LESS_THAN_OR_EQUAL_TO;
if (required.startsWith(">")) return GREATER_THAN;
if (required.startsWith("<")) return LESS_THAN;
return EQUALS;
}
Expand All @@ -50,4 +48,6 @@ public static String clean(String current) {
.replace(">", "")
.replace(">", "");
}

public abstract boolean allow(Version required, Version other);
}

0 comments on commit eb2b654

Please sign in to comment.