Skip to content

Commit 2cbdc84

Browse files
committed
Working in progress support for remapping from other namespaces than obfuscated/official
#8
1 parent a18845e commit 2cbdc84

File tree

5 files changed

+204
-65
lines changed

5 files changed

+204
-65
lines changed

src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.file.Path;
2828
import java.nio.file.Paths;
2929
import java.util.*;
30+
import java.util.function.Supplier;
3031
import java.util.stream.Collectors;
3132
import java.util.zip.ZipEntry;
3233
import java.util.zip.ZipInputStream;
@@ -57,12 +58,31 @@ public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.
5758
}
5859
}
5960

61+
for (ModRemapper remapper : remappers) {
62+
Optional<String> sourceNamespace = remapper.getSourceNamespace();
63+
64+
if (sourceNamespace.isPresent()) {
65+
MappingsUtilsImpl.setSourceNamespace(sourceNamespace.get());
66+
break;
67+
}
68+
}
69+
70+
for (ModRemapper remapper : remappers) {
71+
Optional<Supplier<InputStream>> mappings = remapper.getExtraMapping();
72+
73+
if (mappings.isPresent()) {
74+
MappingsUtilsImpl.loadExtraMappings(mappings.get().get());
75+
break;
76+
}
77+
}
78+
6079
MINECRAFT_TREE = MappingsUtilsImpl.getMinecraftMappings();
80+
6181
LOADER_TREE = generateMappings();
6282
MappingsUtilsImpl.addMappingsToContext(LOADER_TREE);
6383

6484
for (MappingTree.ClassMapping classView : MINECRAFT_TREE.getClasses()) {
65-
String className = classView.getName("official");
85+
String className = classView.getName(MappingsUtilsImpl.getSourceNamespace());
6686

6787
if (className != null) {
6888
MC_CLASS_NAMES.add(className);
@@ -504,7 +524,7 @@ private static TinyRemapper makeRemapper(MappingTree... trees) {
504524
}
505525

506526
for (MappingTree tree : trees) {
507-
builder.withMappings(MappingsUtilsImpl.createProvider(tree, "official", MappingsUtils.getTargetNamespace()));
527+
builder.withMappings(MappingsUtilsImpl.createProvider(tree, MappingsUtilsImpl.getSourceNamespace(), MappingsUtils.getTargetNamespace()));
508528
}
509529

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

531551
TinyRemapper remapper = builder.build();
532552

533-
MappingsUtils.addMinecraftJar(remapper);
553+
try {
554+
MappingsUtils.addMinecraftJar(remapper);
555+
} catch (IOException e) {
556+
throw new RuntimeException(e);
557+
}
534558

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

565-
applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers);
589+
applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true);
566590
}
567591

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

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

596-
MappingsUtilsImpl.completeMappingsFromTr(remapper.getEnvironment());
620+
if (analyzeMapping) MappingsUtilsImpl.completeMappingsFromTr(remapper.getEnvironment());
597621
} catch (Exception e) {
598622
remapper.finish();
599623
outputConsumerPaths.forEach(o -> {

src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515

1616
import java.io.*;
1717
import java.nio.file.Path;
18-
import java.util.ArrayList;
19-
import java.util.HashMap;
20-
import java.util.List;
21-
import java.util.Map;
18+
import java.util.*;
2219

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

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

69-
private static Path[] getMinecraftJar() throws IOException {
70-
Path[] originalClassPath = getRemapClasspath().toArray(new Path[0]);
67+
private static Path[] getMinecraftJar(List<Path> sourcePaths, String src, String target) throws IOException {
68+
Path[] originalClassPath = sourcePaths.toArray(new Path[0]);
7169

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

7472
for (Path path :
7573
originalClassPath) {
7674
Constants.MAIN_LOGGER.info(path.toString());
77-
paths.put(path, new File(Constants.LIB_FOLDER, path.toFile().getName()).toPath());
75+
paths.put(path, new File(
76+
new File(Constants.LIB_FOLDER, target),
77+
path.toFile().getName()).toPath()
78+
);
7879
paths.get(path).toFile().delete();
7980
}
8081

@@ -85,26 +86,28 @@ private static Path[] getMinecraftJar() throws IOException {
8586
.propagatePrivate(true)
8687
.ignoreConflicts(true)
8788
.fixPackageAccess(true)
88-
.withMappings(createBackwardProvider(getMinecraftMappings()));
89+
.withMappings(
90+
MappingsUtilsImpl.createProvider(MappingsUtilsImpl.getMinecraftMappings(), src, target)
91+
);
8992

9093
TinyRemapper remapper = builder.build();
9194

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

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

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

98-
RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers);
101+
RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, false);
99102

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

103106
@ApiStatus.Internal
104-
public static void addMinecraftJar(TinyRemapper remapper) {
107+
public static void addMinecraftJar(TinyRemapper remapper) throws IOException {
105108
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
106109
try {
107-
remapper.readClassPathAsync(getMinecraftJar());
110+
remapper.readClassPathAsync(getMinecraftJar(getRemapClasspath(), getTargetNamespace(), "official"));
108111
} catch (IOException e) {
109112
throw new RuntimeException("Failed to populate default remap classpath", e);
110113
}
@@ -139,7 +142,10 @@ public static void addMinecraftJar(TinyRemapper remapper) {
139142

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

142-
for (Path path : list) {
145+
for (Path path :
146+
!Objects.equals(MappingsUtilsImpl.getSourceNamespace(), "official") ?
147+
getMinecraftJar(list, "official", MappingsUtilsImpl.getSourceNamespace())
148+
: list.toArray(new Path[0])) {
143149
Constants.MAIN_LOGGER.debug("Appending '%s' to remapper classpath", path);
144150
remapper.readClassPathAsync(path);
145151
}

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/ModRemapper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import net.fabricmc.api.EnvType;
44

5+
import java.io.InputStream;
56
import java.util.List;
67
import java.util.Optional;
8+
import java.util.function.Supplier;
79

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

2224
default void afterRemap() {}
25+
26+
default Optional<String> getSourceNamespace() {
27+
return Optional.empty();
28+
}
29+
30+
default Optional<Supplier<InputStream>> getExtraMapping() {
31+
return Optional.empty();
32+
}
2333
}

0 commit comments

Comments
 (0)