Skip to content

Commit

Permalink
Working in progress support for remapping from other namespaces than …
Browse files Browse the repository at this point in the history
…obfuscated/official

#8
  • Loading branch information
thecatcore committed May 31, 2024
1 parent a18845e commit 2cbdc84
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 65 deletions.
36 changes: 30 additions & 6 deletions src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -57,12 +58,31 @@ public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.
}
}

for (ModRemapper remapper : remappers) {
Optional<String> sourceNamespace = remapper.getSourceNamespace();

if (sourceNamespace.isPresent()) {
MappingsUtilsImpl.setSourceNamespace(sourceNamespace.get());
break;
}
}

for (ModRemapper remapper : remappers) {
Optional<Supplier<InputStream>> mappings = remapper.getExtraMapping();

if (mappings.isPresent()) {
MappingsUtilsImpl.loadExtraMappings(mappings.get().get());
break;
}
}

MINECRAFT_TREE = MappingsUtilsImpl.getMinecraftMappings();

LOADER_TREE = generateMappings();
MappingsUtilsImpl.addMappingsToContext(LOADER_TREE);

for (MappingTree.ClassMapping classView : MINECRAFT_TREE.getClasses()) {
String className = classView.getName("official");
String className = classView.getName(MappingsUtilsImpl.getSourceNamespace());

if (className != null) {
MC_CLASS_NAMES.add(className);
Expand Down Expand Up @@ -504,7 +524,7 @@ private static TinyRemapper makeRemapper(MappingTree... trees) {
}

for (MappingTree tree : trees) {
builder.withMappings(MappingsUtilsImpl.createProvider(tree, "official", MappingsUtils.getTargetNamespace()));
builder.withMappings(MappingsUtilsImpl.createProvider(tree, MappingsUtilsImpl.getSourceNamespace(), MappingsUtils.getTargetNamespace()));
}

MRAApplyVisitor preApplyVisitor = new MRAApplyVisitor();
Expand All @@ -530,7 +550,11 @@ private static TinyRemapper makeRemapper(MappingTree... trees) {

TinyRemapper remapper = builder.build();

MappingsUtils.addMinecraftJar(remapper);
try {
MappingsUtils.addMinecraftJar(remapper);
} catch (IOException e) {
throw new RuntimeException(e);
}

for (ModRemapper modRemapper : remappers) {
List<RemapLibrary> libraries = new ArrayList<>();
Expand Down Expand Up @@ -562,11 +586,11 @@ private static void remapFiles(TinyRemapper remapper, Map<Path, Path> paths) {
List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);
resourceRemappers.add(new RefmapRemapper());

applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers);
applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true);
}

@ApiStatus.Internal
public static void applyRemapper(TinyRemapper remapper, Map<Path, Path> paths, List<OutputConsumerPath> outputConsumerPaths, List<OutputConsumerPath.ResourceRemapper> resourceRemappers) {
public static void applyRemapper(TinyRemapper remapper, Map<Path, Path> paths, List<OutputConsumerPath> outputConsumerPaths, List<OutputConsumerPath.ResourceRemapper> resourceRemappers, boolean analyzeMapping) {
try {
Map<Path, InputTag> tagMap = new HashMap<>();

Expand All @@ -593,7 +617,7 @@ public static void applyRemapper(TinyRemapper remapper, Map<Path, Path> paths, L
Constants.MAIN_LOGGER.debug("Done 1!");
}

MappingsUtilsImpl.completeMappingsFromTr(remapper.getEnvironment());
if (analyzeMapping) MappingsUtilsImpl.completeMappingsFromTr(remapper.getEnvironment());
} catch (Exception e) {
remapper.finish();
outputConsumerPaths.forEach(o -> {
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@

import java.io.*;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static fr.catcore.modremapperapi.remapping.RemapUtil.getRemapClasspath;

public class MappingsUtils {
@Deprecated
public static String getNativeNamespace() {
if (ModRemappingAPI.BABRIC) {
return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT ? "client" : "server";
Expand Down Expand Up @@ -66,15 +64,18 @@ private static IMappingProvider createBackwardProvider(MappingTree mappings) {
return MappingsUtilsImpl.createProvider(mappings, getTargetNamespace(), "official");
}

private static Path[] getMinecraftJar() throws IOException {
Path[] originalClassPath = getRemapClasspath().toArray(new Path[0]);
private static Path[] getMinecraftJar(List<Path> sourcePaths, String src, String target) throws IOException {
Path[] originalClassPath = sourcePaths.toArray(new Path[0]);

Map<Path, Path> paths = new HashMap<>();

for (Path path :
originalClassPath) {
Constants.MAIN_LOGGER.info(path.toString());
paths.put(path, new File(Constants.LIB_FOLDER, path.toFile().getName()).toPath());
paths.put(path, new File(
new File(Constants.LIB_FOLDER, target),
path.toFile().getName()).toPath()
);
paths.get(path).toFile().delete();
}

Expand All @@ -85,26 +86,28 @@ private static Path[] getMinecraftJar() throws IOException {
.propagatePrivate(true)
.ignoreConflicts(true)
.fixPackageAccess(true)
.withMappings(createBackwardProvider(getMinecraftMappings()));
.withMappings(
MappingsUtilsImpl.createProvider(MappingsUtilsImpl.getMinecraftMappings(), src, target)
);

TinyRemapper remapper = builder.build();

Constants.MAIN_LOGGER.info("Remapping minecraft jar back to obfuscated!");
Constants.MAIN_LOGGER.info("Remapping minecraft jar from " + src + " to " + target + "!");

List<OutputConsumerPath> outputConsumerPaths = new ArrayList<>();

List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);

RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers);
RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, false);

return paths.values().toArray(new Path[0]);
}

@ApiStatus.Internal
public static void addMinecraftJar(TinyRemapper remapper) {
public static void addMinecraftJar(TinyRemapper remapper) throws IOException {
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
try {
remapper.readClassPathAsync(getMinecraftJar());
remapper.readClassPathAsync(getMinecraftJar(getRemapClasspath(), getTargetNamespace(), "official"));
} catch (IOException e) {
throw new RuntimeException("Failed to populate default remap classpath", e);
}
Expand Down Expand Up @@ -139,7 +142,10 @@ public static void addMinecraftJar(TinyRemapper remapper) {

if (realmsJar instanceof Path) list.add((Path) realmsJar);

for (Path path : list) {
for (Path path :
!Objects.equals(MappingsUtilsImpl.getSourceNamespace(), "official") ?
getMinecraftJar(list, "official", MappingsUtilsImpl.getSourceNamespace())
: list.toArray(new Path[0])) {
Constants.MAIN_LOGGER.debug("Appending '%s' to remapper classpath", path);
remapper.readClassPathAsync(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import net.fabricmc.api.EnvType;

import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

public interface ModRemapper {
String[] getJarFolders();
Expand All @@ -20,4 +22,12 @@ default Optional<String> getDefaultPackage() {
}

default void afterRemap() {}

default Optional<String> getSourceNamespace() {
return Optional.empty();
}

default Optional<Supplier<InputStream>> getExtraMapping() {
return Optional.empty();
}
}
Loading

0 comments on commit 2cbdc84

Please sign in to comment.