Skip to content

Commit

Permalink
Jar-mods without package after obfuscation now get remapped to net/mi…
Browse files Browse the repository at this point in the history
…necraft/src to match location in dev env. Enables mixins into non-vanilla classes of jar-mods much easier.
  • Loading branch information
Arminias committed Mar 1, 2022
1 parent 73d7be8 commit af3f998
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
package net.fabricmc.loader.launch.common;

import net.fabricmc.api.EnvType;
import net.fabricmc.loader.util.mappings.TinyRemapperMappingsHelper;
import net.fabricmc.loader.launch.knot.Knot;
import net.fabricmc.loader.util.Arguments;
import net.fabricmc.loader.util.UrlConversionException;
import net.fabricmc.loader.util.UrlUtil;
import net.fabricmc.loader.util.Arguments;
import net.fabricmc.loader.util.mappings.TinyRemapperMappingsHelper;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.tinyremapper.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.objectweb.asm.commons.Remapper;
import org.spongepowered.asm.mixin.MixinEnvironment;

import java.io.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -43,7 +45,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.jar.JarFile;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -317,11 +318,18 @@ private static Path deobfuscate_backend(String gameId, String gameVersion, Path
*/
@Override
public String map(String internalName) {
if (internalName.contains("/"))
return internalName.replace("net/minecraft/src/", "net/minecraft/");
else {
return "net/minecraft/" + internalName;
}
if (Knot.RELOCATE_SRC == 0)
if (internalName.contains("/")) {
return internalName.replace("net/minecraft/src/", "net/minecraft/");
}
else {
return "net/minecraft/" + internalName;
}
else if (Knot.RELOCATE_SRC == 1)
if (!internalName.contains("/")) {
return "net/minecraft/src/" + internalName;
}
return internalName;
}
})
.build()
Expand Down
81 changes: 13 additions & 68 deletions src/main/java/net/fabricmc/loader/launch/knot/Knot.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.stream.Collectors;

public final class Knot extends FabricLauncherBase {
public static final int RELOCATE_SRC = 1;
protected Map<String, Object> properties = new HashMap<>();

private KnotClassLoaderInterface classLoader;
Expand Down Expand Up @@ -142,11 +143,18 @@ protected ClassLoader init(String[] args) {
*/
@Override
public String map(String internalName) {
if (internalName.contains("/"))
return internalName.replace("net/minecraft/src/", "net/minecraft/");
else {
return "net/minecraft/" + internalName;
}
if (RELOCATE_SRC == 0)
if (internalName.contains("/")) {
return internalName.replace("net/minecraft/src/", "net/minecraft/");
}
else {
return "net/minecraft/" + internalName;
}
else if (RELOCATE_SRC == 1)
if (!internalName.contains("/")) {
return "net/minecraft/src/" + internalName;
}
return internalName;
}
})
.build()
Expand Down Expand Up @@ -191,69 +199,6 @@ public String map(String internalName) {
Thread.currentThread().setContextClassLoader(cl);

loader.load();
for (ModContainer m : loader.mods) {
String[] tmp = m.getOriginUrl().toExternalForm().split("/");
if (!m.getInfo().getEntrypointKeys().isEmpty() && tmp[tmp.length-2].equals("mods")) {
String sidedOrigin = getEnvironmentType().name().toLowerCase(Locale.ENGLISH);
TinyRemapperWithOverwrites remapper = new TinyRemapperWithOverwrites(TinyRemapper.newRemapper()
.withMappings(TinyRemapperMappingsHelper.create(
getMappingConfiguration().getMappings(), getMappingConfiguration().getMappings().getMetadata().getNamespaces().contains(sidedOrigin)
? sidedOrigin : "official", getMappingConfiguration().getTargetNamespace())
)
.rebuildSourceFilenames(true)
.fixPackageAccess(!isDevelopment()).extraRemapper(new Remapper() {
/**
* Maps the internal name of a class to its new name. The default implementation of this method
* returns the given name, unchanged. Subclasses can override.
*
* @param internalName the internal name of a class.
* @return the new internal name.
*/
@Override
public String map(String internalName) {
if (internalName.contains("/"))
return internalName.replace("net/minecraft/src/", "net/minecraft/");
else {
return "net/minecraft/" + internalName;
}
}
})
.build()
);
String versionedId = provider.getNormalizedGameVersion().isEmpty() ? provider.getGameId() : String.format("%s-%s", provider.getGameId(), provider.getNormalizedGameVersion());
Path jarPath = null;
try {
jarPath = provider.getLaunchDirectory().resolve(".fabric").resolve("remappedModJars").
resolve(versionedId).resolve(getMappingConfiguration().
getTargetNamespace() + "-" + UrlUtil.asPath(m.getOriginUrl()).getFileName());
} catch (UrlConversionException e) {
e.printStackTrace();
}
try (OutputConsumerPath o = new OutputConsumerPath.Builder(jarPath)
// force jar despite the .tmp extension
.assumeArchive(true)
// don't accept class names from a blacklist of dependencies that Fabric itself utilizes
// TODO: really could use a better solution, as always...
.filter(clsName -> !clsName.startsWith("com/google/common/")
&& !clsName.startsWith("com/google/gson/")
&& !clsName.startsWith("com/google/thirdparty/")
&& !clsName.startsWith("org/apache/logging/log4j/"))
.build()) {
remapper.readInputs(UrlUtil.asPath(m.getOriginUrl()));
remapper.INSTANCE.apply(o);
} catch (IOException | UrlConversionException e) {
throw new RuntimeException("Failed to remap '" + m.getOriginUrl() + "'!", e);
} finally {
remapper.INSTANCE.finish();
}
try {
propose(UrlUtil.asUrl(jarPath));
} catch (UrlConversionException e) {
e.printStackTrace();
}
}
}


loader.freeze();

Expand Down

0 comments on commit af3f998

Please sign in to comment.