Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support remapping mods from other namespaces than obfuscated/official #12

Merged
merged 9 commits into from
Sep 6, 2024
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ repositories {
}
}

loom.mods.register(project.name + "-testmod") {
sourceSet project.sourceSets.test
}

dependencies {
minecraft "net.minecraft:minecraft:${project.minecraft_version}"

Expand Down Expand Up @@ -105,6 +109,14 @@ remapJar {
inputFile = file(shadowJar.archivePath)
}

tasks.register('testJar', Jar) {
from sourceSets.test.output
destinationDirectory = new File(project.buildDir, "devlibs")
archiveClassifier = "testmod"
}

tasks.build.dependsOn testJar

// configure the maven publication
publishing {
publications {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1G

# More versions available at: https://skyrising.xyz/legacy-quilt/
# Fabric Properties
minecraft_version = 1.12.2
minecraft_version = 1.6.4
yarn_build = 458
loader_version = 0.15.10
fabric_version = 1.9.1+1.12.2
Expand Down
44 changes: 34 additions & 10 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 All @@ -51,18 +52,26 @@ public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.
for (ModRemapper remapper : remappers) {
Optional<String> pkg = remapper.getDefaultPackage();

if (pkg.isPresent()) {
defaultPackage = pkg.get();
break;
}
pkg.ifPresent(s -> defaultPackage = s);

Optional<String> sourceNamespace = remapper.getSourceNamespace();

sourceNamespace.ifPresent(MappingsUtilsImpl::setSourceNamespace);

Optional<Supplier<InputStream>> mappings = remapper.getExtraMapping();

mappings.ifPresent(inputStreamSupplier -> MappingsUtilsImpl.loadExtraMappings(inputStreamSupplier.get()));
}

MINECRAFT_TREE = MappingsUtilsImpl.getMinecraftMappings();

writeMcMappings();

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 @@ -131,6 +140,8 @@ public static void remapMods(Map<Path, Path> pathMap) {
Constants.MAIN_LOGGER.debug("Remapper created!");
remapFiles(remapper, pathMap);
Constants.MAIN_LOGGER.debug("Jar remapping done!");

MappingsUtilsImpl.writeFullMappings();
}

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

public static void writeMcMappings() {
try {
MappingWriter writer = MappingWriter.create(Constants.MC_MAPPINGS_FILE.toPath(), MappingFormat.TINY_2_FILE);
MINECRAFT_TREE.accept(writer);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static List<String> generateFolderMappings(File[] files) {
List<String> list = new ArrayList<>();

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, MappingsUtilsImpl.getSourceNamespace(), MappingsUtils.getTargetNamespace());
}

@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, String srcNamespace, String targetNamespace) {
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(), srcNamespace);
} catch (Exception e) {
remapper.finish();
outputConsumerPaths.forEach(o -> {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/fr/catcore/modremapperapi/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Constants {
);
public static final File EXTRA_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "extra_mappings.tiny");
public static final File REMAPPED_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "remapped_mappings.tiny");
public static final File MC_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "mc_mappings.tiny");
public static final File FULL_MAPPINGS_FILE = new File(VERSIONED_FOLDER, "full_mappings.tiny");

public static final File LIB_FOLDER = new File(VERSIONED_FOLDER, "libs");
public static final Logger MAIN_LOGGER = Logger.get("ModRemappingAPI");
Expand Down
67 changes: 48 additions & 19 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 All @@ -32,7 +30,7 @@ public static String getNativeNamespace() {
}

public static String getTargetNamespace() {
return !FabricLoader.getInstance().isDevelopmentEnvironment() ? "intermediary" : FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace();
return FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace();
}

public static MappingTree loadMappings(Reader reader) {
Expand Down Expand Up @@ -62,19 +60,18 @@ public static IMappingProvider createProvider(MappingTree mappings) {
return MappingsUtilsImpl.createProvider(mappings, getNativeNamespace(), getTargetNamespace());
}

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());
Constants.MAIN_LOGGER.debug(path.toString());
paths.put(path, new File(
new File(Constants.LIB_FOLDER, target),
path.toFile().getName()).toPath()
);
paths.get(path).toFile().delete();
}

Expand All @@ -85,26 +82,52 @@ 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, true, src, target);

Constants.MAIN_LOGGER.info("MC jar remapped successfully!");

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());
Path[] classPath = getMinecraftJar(
Arrays.asList(
getMinecraftJar(
getRemapClasspath(),
getTargetNamespace(),
"intermediary"
)
),
"intermediary",
"official"
);

if (!MappingsUtilsImpl.isSourceNamespaceObf()) {
classPath = getMinecraftJar(
Arrays.asList(
classPath
),
"official",
MappingsUtilsImpl.getSourceNamespace()
);
}

remapper.readClassPathAsync(classPath);
} catch (IOException e) {
throw new RuntimeException("Failed to populate default remap classpath", e);
}
Expand Down Expand Up @@ -138,8 +161,14 @@ public static void addMinecraftJar(TinyRemapper remapper) {
Object realmsJar = share.get("fabric-loader:inputRealmsJar");

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

Path[] classPath = list.toArray(new Path[0]);

if (!MappingsUtilsImpl.isSourceNamespaceObf()) {
classPath = getMinecraftJar(list, "official", MappingsUtilsImpl.getSourceNamespace());
}

for (Path path : list) {
for (Path path : classPath) {
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
Loading