Skip to content

Commit d37f4da

Browse files
authored
Merge pull request #12 from FabricCompatibilityLayers/feature/other-namespace-support
Support remapping mods from other namespaces than obfuscated/official
2 parents a18845e + 1bc243b commit d37f4da

12 files changed

Lines changed: 13963 additions & 88 deletions

File tree

build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ repositories {
2626
}
2727
}
2828

29+
loom.mods.register(project.name + "-testmod") {
30+
sourceSet project.sourceSets.test
31+
}
32+
2933
dependencies {
3034
minecraft "net.minecraft:minecraft:${project.minecraft_version}"
3135

@@ -105,6 +109,14 @@ remapJar {
105109
inputFile = file(shadowJar.archivePath)
106110
}
107111

112+
tasks.register('testJar', Jar) {
113+
from sourceSets.test.output
114+
destinationDirectory = new File(project.buildDir, "devlibs")
115+
archiveClassifier = "testmod"
116+
}
117+
118+
tasks.build.dependsOn testJar
119+
108120
// configure the maven publication
109121
publishing {
110122
publications {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1G
44

55
# More versions available at: https://skyrising.xyz/legacy-quilt/
66
# Fabric Properties
7-
minecraft_version = 1.12.2
7+
minecraft_version = 1.6.4
88
yarn_build = 458
99
loader_version = 0.15.10
1010
fabric_version = 1.9.1+1.12.2

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

Lines changed: 34 additions & 10 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;
@@ -51,18 +52,26 @@ public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.
5152
for (ModRemapper remapper : remappers) {
5253
Optional<String> pkg = remapper.getDefaultPackage();
5354

54-
if (pkg.isPresent()) {
55-
defaultPackage = pkg.get();
56-
break;
57-
}
55+
pkg.ifPresent(s -> defaultPackage = s);
56+
57+
Optional<String> sourceNamespace = remapper.getSourceNamespace();
58+
59+
sourceNamespace.ifPresent(MappingsUtilsImpl::setSourceNamespace);
60+
61+
Optional<Supplier<InputStream>> mappings = remapper.getExtraMapping();
62+
63+
mappings.ifPresent(inputStreamSupplier -> MappingsUtilsImpl.loadExtraMappings(inputStreamSupplier.get()));
5864
}
5965

6066
MINECRAFT_TREE = MappingsUtilsImpl.getMinecraftMappings();
67+
68+
writeMcMappings();
69+
6170
LOADER_TREE = generateMappings();
6271
MappingsUtilsImpl.addMappingsToContext(LOADER_TREE);
6372

6473
for (MappingTree.ClassMapping classView : MINECRAFT_TREE.getClasses()) {
65-
String className = classView.getName("official");
74+
String className = classView.getName(MappingsUtilsImpl.getSourceNamespace());
6675

6776
if (className != null) {
6877
MC_CLASS_NAMES.add(className);
@@ -131,6 +140,8 @@ public static void remapMods(Map<Path, Path> pathMap) {
131140
Constants.MAIN_LOGGER.debug("Remapper created!");
132141
remapFiles(remapper, pathMap);
133142
Constants.MAIN_LOGGER.debug("Jar remapping done!");
143+
144+
MappingsUtilsImpl.writeFullMappings();
134145
}
135146

136147
public static List<String> makeModMappings(Path modPath) {
@@ -190,6 +201,15 @@ public static void generateModMappings() {
190201
MappingsUtilsImpl.addMappingsToContext(MODS_TREE);
191202
}
192203

204+
public static void writeMcMappings() {
205+
try {
206+
MappingWriter writer = MappingWriter.create(Constants.MC_MAPPINGS_FILE.toPath(), MappingFormat.TINY_2_FILE);
207+
MINECRAFT_TREE.accept(writer);
208+
} catch (IOException e) {
209+
throw new RuntimeException(e);
210+
}
211+
}
212+
193213
private static List<String> generateFolderMappings(File[] files) {
194214
List<String> list = new ArrayList<>();
195215

@@ -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, MappingsUtilsImpl.getSourceNamespace(), MappingsUtils.getTargetNamespace());
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, String srcNamespace, String targetNamespace) {
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(), srcNamespace);
597621
} catch (Exception e) {
598622
remapper.finish();
599623
outputConsumerPaths.forEach(o -> {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class Constants {
1414
);
1515
public static final File EXTRA_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "extra_mappings.tiny");
1616
public static final File REMAPPED_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "remapped_mappings.tiny");
17+
public static final File MC_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "mc_mappings.tiny");
18+
public static final File FULL_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "full_mappings.tiny");
1719

1820
public static final File LIB_FOLDER = new File(VERSIONED_FOLDER, "libs");
1921
public static final Logger MAIN_LOGGER = Logger.get("ModRemappingAPI");

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

Lines changed: 48 additions & 19 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";
@@ -32,7 +30,7 @@ public static String getNativeNamespace() {
3230
}
3331

3432
public static String getTargetNamespace() {
35-
return !FabricLoader.getInstance().isDevelopmentEnvironment() ? "intermediary" : FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace();
33+
return FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace();
3634
}
3735

3836
public static MappingTree loadMappings(Reader reader) {
@@ -62,19 +60,18 @@ public static IMappingProvider createProvider(MappingTree mappings) {
6260
return MappingsUtilsImpl.createProvider(mappings, getNativeNamespace(), getTargetNamespace());
6361
}
6462

65-
private static IMappingProvider createBackwardProvider(MappingTree mappings) {
66-
return MappingsUtilsImpl.createProvider(mappings, getTargetNamespace(), "official");
67-
}
68-
69-
private static Path[] getMinecraftJar() throws IOException {
70-
Path[] originalClassPath = getRemapClasspath().toArray(new Path[0]);
63+
private static Path[] getMinecraftJar(List<Path> sourcePaths, String src, String target) throws IOException {
64+
Path[] originalClassPath = sourcePaths.toArray(new Path[0]);
7165

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

7468
for (Path path :
7569
originalClassPath) {
76-
Constants.MAIN_LOGGER.info(path.toString());
77-
paths.put(path, new File(Constants.LIB_FOLDER, path.toFile().getName()).toPath());
70+
Constants.MAIN_LOGGER.debug(path.toString());
71+
paths.put(path, new File(
72+
new File(Constants.LIB_FOLDER, target),
73+
path.toFile().getName()).toPath()
74+
);
7875
paths.get(path).toFile().delete();
7976
}
8077

@@ -85,26 +82,52 @@ private static Path[] getMinecraftJar() throws IOException {
8582
.propagatePrivate(true)
8683
.ignoreConflicts(true)
8784
.fixPackageAccess(true)
88-
.withMappings(createBackwardProvider(getMinecraftMappings()));
85+
.withMappings(
86+
MappingsUtilsImpl.createProvider(MappingsUtilsImpl.getMinecraftMappings(), src, target)
87+
);
8988

9089
TinyRemapper remapper = builder.build();
9190

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

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

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

98-
RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers);
97+
RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true, src, target);
98+
99+
Constants.MAIN_LOGGER.info("MC jar remapped successfully!");
99100

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

103104
@ApiStatus.Internal
104-
public static void addMinecraftJar(TinyRemapper remapper) {
105+
public static void addMinecraftJar(TinyRemapper remapper) throws IOException {
105106
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
106107
try {
107-
remapper.readClassPathAsync(getMinecraftJar());
108+
Path[] classPath = getMinecraftJar(
109+
Arrays.asList(
110+
getMinecraftJar(
111+
getRemapClasspath(),
112+
getTargetNamespace(),
113+
"intermediary"
114+
)
115+
),
116+
"intermediary",
117+
"official"
118+
);
119+
120+
if (!MappingsUtilsImpl.isSourceNamespaceObf()) {
121+
classPath = getMinecraftJar(
122+
Arrays.asList(
123+
classPath
124+
),
125+
"official",
126+
MappingsUtilsImpl.getSourceNamespace()
127+
);
128+
}
129+
130+
remapper.readClassPathAsync(classPath);
108131
} catch (IOException e) {
109132
throw new RuntimeException("Failed to populate default remap classpath", e);
110133
}
@@ -138,8 +161,14 @@ public static void addMinecraftJar(TinyRemapper remapper) {
138161
Object realmsJar = share.get("fabric-loader:inputRealmsJar");
139162

140163
if (realmsJar instanceof Path) list.add((Path) realmsJar);
164+
165+
Path[] classPath = list.toArray(new Path[0]);
166+
167+
if (!MappingsUtilsImpl.isSourceNamespaceObf()) {
168+
classPath = getMinecraftJar(list, "official", MappingsUtilsImpl.getSourceNamespace());
169+
}
141170

142-
for (Path path : list) {
171+
for (Path path : classPath) {
143172
Constants.MAIN_LOGGER.debug("Appending '%s' to remapper classpath", path);
144173
remapper.readClassPathAsync(path);
145174
}

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)