diff --git a/src/main/java/fr/catcore/modremapperapi/ModRemappingAPI.java b/src/main/java/fr/catcore/modremapperapi/ModRemappingAPI.java index bbf33b4..a13ec7e 100644 --- a/src/main/java/fr/catcore/modremapperapi/ModRemappingAPI.java +++ b/src/main/java/fr/catcore/modremapperapi/ModRemappingAPI.java @@ -1,7 +1,7 @@ package fr.catcore.modremapperapi; import fr.catcore.modremapperapi.api.ModRemapper; -import fr.catcore.modremapperapi.utils.FakeModManager; +import io.github.fabriccompatibiltylayers.modremappingapi.impl.ModRemappingAPIImpl; import net.fabricmc.loader.api.FabricLoader; import java.io.File; @@ -10,31 +10,6 @@ public class ModRemappingAPI { - public static final List MOD_REMAPPERS = new ArrayList<>(); - private static final String entrypointName = "mod-remapper-api:modremapper"; - - public static boolean remapClassEdits = false; - public static final boolean BABRIC = FabricLoader.getInstance().getModContainer("fabricloader") .get().getMetadata().getVersion().getFriendlyString().contains("babric"); - - private static boolean init = false; - private static boolean initializing = false; - - public static void init() { - if (!init && !initializing) { - initializing = true; - - FabricLoader.getInstance().getConfigDir().toFile().mkdirs(); - remapClassEdits = new File(FabricLoader.getInstance().getConfigDir().toFile(), ".remapclassedits").exists(); - - MOD_REMAPPERS.addAll(FabricLoader.getInstance().getEntrypoints(entrypointName, ModRemapper.class)); - FakeModManager.init(); - - for (ModRemapper remapper : MOD_REMAPPERS) remapper.afterRemap(); - - initializing = false; - init = true; - } - } } diff --git a/src/main/java/fr/catcore/modremapperapi/api/ModRemapper.java b/src/main/java/fr/catcore/modremapperapi/api/ModRemapper.java index bf51943..14babe9 100644 --- a/src/main/java/fr/catcore/modremapperapi/api/ModRemapper.java +++ b/src/main/java/fr/catcore/modremapperapi/api/ModRemapper.java @@ -10,6 +10,10 @@ import java.util.Map; import java.util.Optional; +/** + * @deprecated Use {@link io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper} instead with entrypoint key "mod-remapper-api:modremapper_v1" + */ +@Deprecated public interface ModRemapper { String[] getJarFolders(); diff --git a/src/main/java/fr/catcore/modremapperapi/api/RemapLibrary.java b/src/main/java/fr/catcore/modremapperapi/api/RemapLibrary.java index aac95b6..1a0f421 100644 --- a/src/main/java/fr/catcore/modremapperapi/api/RemapLibrary.java +++ b/src/main/java/fr/catcore/modremapperapi/api/RemapLibrary.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; +@Deprecated public class RemapLibrary { public final String url; public final Path path; diff --git a/src/main/java/fr/catcore/modremapperapi/remapping/MRAPostApplyVisitor.java b/src/main/java/fr/catcore/modremapperapi/remapping/MRAApplyVisitor.java similarity index 88% rename from src/main/java/fr/catcore/modremapperapi/remapping/MRAPostApplyVisitor.java rename to src/main/java/fr/catcore/modremapperapi/remapping/MRAApplyVisitor.java index 7296feb..dd60eba 100644 --- a/src/main/java/fr/catcore/modremapperapi/remapping/MRAPostApplyVisitor.java +++ b/src/main/java/fr/catcore/modremapperapi/remapping/MRAApplyVisitor.java @@ -5,8 +5,9 @@ import net.fabricmc.tinyremapper.api.TrClass; import org.objectweb.asm.ClassVisitor; -public class MRAPostApplyVisitor implements TinyRemapper.ApplyVisitorProvider { +public class MRAApplyVisitor implements TinyRemapper.ApplyVisitorProvider { private VisitorInfosImpl infos; + @Override public ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next) { final String className = cls.getName(); diff --git a/src/main/java/fr/catcore/modremapperapi/remapping/MRAMethodVisitor.java b/src/main/java/fr/catcore/modremapperapi/remapping/MRAMethodVisitor.java index 0c23151..dc826b6 100644 --- a/src/main/java/fr/catcore/modremapperapi/remapping/MRAMethodVisitor.java +++ b/src/main/java/fr/catcore/modremapperapi/remapping/MRAMethodVisitor.java @@ -1,5 +1,6 @@ package fr.catcore.modremapperapi.remapping; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.VisitorInfos; import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; @@ -40,16 +41,16 @@ public void visitFieldInsn(int opcode, String owner, String name, String descrip String currentDescriptor = descriptor; if (infos.FIELD_REF.containsKey(owner)) { - Map> fields = infos.FIELD_REF.get(owner); + Map> fields = infos.FIELD_REF.get(owner); - Map args = fields.get(name); + Map args = fields.get(name); if (args == null) { args = fields.get(""); } if (args != null) { - io.github.fabriccompatibiltylayers.modremappingapi.api.VisitorInfos.FullClassMember classMember = args.get(descriptor); + VisitorInfos.FullClassMember classMember = args.get(descriptor); if (classMember == null) { classMember = args.get(""); @@ -90,16 +91,16 @@ public void visitMethodInsn(int opcode, String owner, String name, String descri if (!skip && (opcode == INVOKEVIRTUAL || opcode == INVOKESTATIC)) { if (infos.METHOD_INVOCATION.containsKey(owner)) { - Map> methods = infos.METHOD_INVOCATION.get(owner); + Map> methods = infos.METHOD_INVOCATION.get(owner); - Map args = methods.get(currentName); + Map args = methods.get(currentName); if (args == null) { args = methods.get(""); } if (args != null) { - io.github.fabriccompatibiltylayers.modremappingapi.api.VisitorInfos.FullClassMember fullClassMember = args.get(currentDescriptor); + VisitorInfos.FullClassMember fullClassMember = args.get(currentDescriptor); if (fullClassMember == null) { fullClassMember = args.get(""); diff --git a/src/main/java/fr/catcore/modremapperapi/remapping/MappingBuilder.java b/src/main/java/fr/catcore/modremapperapi/remapping/MappingBuilder.java index d3d7f43..a2fe02e 100644 --- a/src/main/java/fr/catcore/modremapperapi/remapping/MappingBuilder.java +++ b/src/main/java/fr/catcore/modremapperapi/remapping/MappingBuilder.java @@ -1,13 +1,16 @@ package fr.catcore.modremapperapi.remapping; -import fr.catcore.modremapperapi.ModRemappingAPI; +import fr.catcore.wfvaio.FabricVariants; +import fr.catcore.wfvaio.WhichFabricVariantAmIOn; import net.fabricmc.mappingio.MappedElementKind; import net.fabricmc.mappingio.MappingVisitor; +import org.jetbrains.annotations.ApiStatus; import java.io.IOException; import java.util.ArrayList; import java.util.List; +@Deprecated public class MappingBuilder { private static final boolean BABRIC = WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC || WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC_NEW_FORMAT; @@ -20,56 +23,41 @@ private MappingBuilder(String obfucated, String intermediary) { this.intermediary = intermediary; } - private static String toString(String... line) { - StringBuilder builder = new StringBuilder(line[0]); - for (int j = 1; j < line.length; j++) { - builder.append('\t'); - builder.append(line[j]); - } - return builder.toString(); - } - + @ApiStatus.Internal public static MappingBuilder create(String obfucated, String intermediary) { return new MappingBuilder(obfucated, intermediary); } + @ApiStatus.Internal public static MappingBuilder create(String name) { return new MappingBuilder(name, name); } + @Deprecated public MappingBuilder field(String obfuscated, String intermediary, String description) { this.entries.add(new Entry(obfuscated, intermediary, description, Type.FIELD)); return this; } + @Deprecated public MappingBuilder field(String name, String description) { this.entries.add(new Entry(name, name, description, Type.FIELD)); return this; } + @Deprecated public MappingBuilder method(String obfuscated, String intermediary, String description) { this.entries.add(new Entry(obfuscated, intermediary, description, Type.METHOD)); return this; } + @Deprecated public MappingBuilder method(String name, String description) { this.entries.add(new Entry(name, name, description, Type.METHOD)); return this; } - public List build() { - List list = new ArrayList<>(); - if (ModRemappingAPI.BABRIC) { - list.add(toString("CLASS", this.intermediary, this.intermediary, this.obfucated, this.obfucated, this.intermediary)); - } else { - list.add(toString("CLASS", this.obfucated, this.intermediary, this.intermediary)); - } - - entries.forEach(entry -> list.add(entry.toString(ModRemappingAPI.BABRIC ? this.intermediary : this.obfucated))); - - return list; - } - + @ApiStatus.Internal public void accept(MappingVisitor visitor) throws IOException { visitor.visitClass(BABRIC ? intermediary : obfucated); visitor.visitDstName(MappedElementKind.CLASS, 0, BABRIC ? obfucated : this.intermediary); @@ -79,6 +67,7 @@ public void accept(MappingVisitor visitor) throws IOException { } } + @ApiStatus.Internal public static class Entry { private final String obfuscated; private final String intermediary; @@ -92,14 +81,7 @@ public Entry(String obfuscated, String intermediary, String description, Type ty this.type = type; } - public String toString(String className) { - if (ModRemappingAPI.BABRIC) { - return MappingBuilder.toString(this.type.name(), className, this.description, this.intermediary, this.intermediary, this.obfuscated, this.obfuscated, this.intermediary); - } else { - return MappingBuilder.toString(this.type.name(), className, this.description, this.obfuscated, this.intermediary, this.intermediary); - } - } - + @ApiStatus.Internal public void accept(MappingVisitor visitor) throws IOException { if (type == Type.FIELD) visitor.visitField(BABRIC ? intermediary : obfuscated, description); else visitor.visitMethod(BABRIC ? intermediary : obfuscated, description); @@ -108,6 +90,7 @@ public void accept(MappingVisitor visitor) throws IOException { } } + @ApiStatus.Internal public enum Type { METHOD, FIELD; } diff --git a/src/main/java/fr/catcore/modremapperapi/remapping/MixinPostApplyVisitor.java b/src/main/java/fr/catcore/modremapperapi/remapping/MixinPostApplyVisitor.java index d957b3d..5d934b4 100644 --- a/src/main/java/fr/catcore/modremapperapi/remapping/MixinPostApplyVisitor.java +++ b/src/main/java/fr/catcore/modremapperapi/remapping/MixinPostApplyVisitor.java @@ -13,13 +13,8 @@ import java.util.ArrayList; import java.util.List; -public class MixinPostApplyVisitor implements TinyRemapper.ApplyVisitorProvider{ - private final List classDefs = new ArrayList<>(); - public MixinPostApplyVisitor(MappingTree[] trees) { - for (MappingTree tree : trees) { - classDefs.addAll(tree.getClasses()); - } - } +public class MixinPostApplyVisitor implements TinyRemapper.ApplyVisitorProvider { + public MixinPostApplyVisitor() {} @Override public ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next) { diff --git a/src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java b/src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java index 8daf49c..8be724d 100644 --- a/src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java +++ b/src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java @@ -1,21 +1,19 @@ package fr.catcore.modremapperapi.remapping; import fr.catcore.modremapperapi.ModRemappingAPI; -import fr.catcore.modremapperapi.api.ModRemapper; -import fr.catcore.modremapperapi.api.RemapLibrary; import fr.catcore.modremapperapi.utils.Constants; import fr.catcore.modremapperapi.utils.FileUtils; import fr.catcore.modremapperapi.utils.MappingsUtils; -import fr.catcore.wfvaio.FabricVariants; -import fr.catcore.wfvaio.WhichFabricVariantAmIOn; import io.github.fabriccompatibiltylayers.modremappingapi.api.MappingUtils; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.RemapLibrary; +import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingBuilderImpl; import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl; import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl; import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.mappingio.MappingVisitor; import net.fabricmc.mappingio.MappingWriter; -import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch; import net.fabricmc.mappingio.format.MappingFormat; import net.fabricmc.mappingio.tree.MappingTree; import net.fabricmc.mappingio.tree.MemoryMappingTree; @@ -35,7 +33,7 @@ import java.util.zip.ZipInputStream; public class RemapUtil { - private static final boolean BABRIC = WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC || WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC_NEW_FORMAT; + private static List remappers; private static MappingTree LOADER_TREE; private static MappingTree MINECRAFT_TREE; private static MappingTree MODS_TREE; @@ -48,10 +46,12 @@ public class RemapUtil { public static final List MC_CLASS_NAMES = new ArrayList<>(); - public static void init() { + public static void init(List modRemappers) { + remappers = modRemappers; + downloadRemappingLibs(); - for (ModRemapper remapper : ModRemappingAPI.MOD_REMAPPERS) { + for (ModRemapper remapper : remappers) { Optional pkg = remapper.getDefaultPackage(); if (pkg.isPresent()) { @@ -75,8 +75,12 @@ public static void init() { private static void downloadRemappingLibs() { try { - for (ModRemapper remapper : ModRemappingAPI.MOD_REMAPPERS) { - for (RemapLibrary library : remapper.getRemapLibraries()) { + for (ModRemapper remapper : remappers) { + List libraries = new ArrayList<>(); + + remapper.addRemapLibraries(libraries, FabricLoader.getInstance().getEnvironmentType()); + + for (RemapLibrary library : libraries) { File libPath = new File(Constants.LIB_FOLDER, library.fileName); if (!libPath.exists() && !library.url.isEmpty()) { @@ -158,7 +162,7 @@ public static List makeModMappings(Path modPath) { for (String file : files) { if (file.endsWith(".class")) { String clName = file.replace(".class", ""); - if (!MC_CLASS_NAMES.contains(clName) || ModRemappingAPI.remapClassEdits) classes.add(clName); + classes.add(clName); } } @@ -204,23 +208,27 @@ else if (file.isDirectory()) { return list; } + @Deprecated public static class MappingList extends ArrayList { public MappingList() { super(); } + @Deprecated public MappingBuilder add(String obfuscated, String intermediary) { MappingBuilder builder = MappingBuilder.create(obfuscated, intermediary); this.add(builder); return builder; } + @Deprecated public MappingBuilder add(String name) { MappingBuilder builder = MappingBuilder.create(name); this.add(builder); return builder; } + @ApiStatus.Internal public void accept(MappingVisitor visitor) throws IOException { for (MappingBuilder builder : this) builder.accept(visitor); } @@ -230,32 +238,18 @@ private static MappingTree generateMappings() { MemoryMappingTree mappingTree = new MemoryMappingTree(); try { - if (BABRIC) { - MappingsUtilsImpl.initializeMappingTree(mappingTree, "intermediary", "official"); - } else { - MappingsUtilsImpl.initializeMappingTree(mappingTree); - } + MappingsUtilsImpl.initializeMappingTree(mappingTree); - MappingList mappingList = new MappingList(); + io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingBuilder builder = new MappingBuilderImpl(mappingTree); - for (ModRemapper remapper : ModRemappingAPI.MOD_REMAPPERS) { - remapper.getMappingList(mappingList); + for (ModRemapper remapper : remappers) { + remapper.registerMappings(builder); } - mappingList.accept(mappingTree); - mappingTree.visitEnd(); MappingWriter mappingWriter = MappingWriter.create(Constants.EXTRA_MAPPINGS_FILE.toPath(), MappingFormat.TINY_2_FILE); mappingTree.accept(mappingWriter); - - if (Objects.equals(mappingTree.getSrcNamespace(), "intermediary")) { - MemoryMappingTree newTree = new MemoryMappingTree(); - MappingVisitor visitor = new MappingSourceNsSwitch(newTree, "official"); - - mappingTree.accept(visitor); - mappingTree = newTree; - } } catch (IOException e) { throw new RuntimeException("Error while generating remappers mappings", e); } @@ -263,20 +257,6 @@ private static MappingTree generateMappings() { return mappingTree; } - /** - * Will convert array to mapping-like string (with tab separator). - * - * @param line array of {@link String} that represents mappings line. - */ - private static String toString(String... line) { - StringBuilder builder = new StringBuilder(line[0]); - for (int j = 1; j < line.length; j++) { - builder.append('\t'); - builder.append(line[j]); - } - return builder.toString(); - } - private static String getLibClassName(String lib, String string) { if (FabricLoader.getInstance().isDevelopmentEnvironment()) { return "net.fabricmc." + lib + "." + string; @@ -337,17 +317,14 @@ private static void preloadClasses() { "fr.catcore.modremapperapi.utils.BArrayList", "fr.catcore.modremapperapi.utils.CollectionUtils", "fr.catcore.modremapperapi.utils.Constants", - "fr.catcore.modremapperapi.utils.DefaultModEntry", - "fr.catcore.modremapperapi.utils.DefaultModRemapper", - "fr.catcore.modremapperapi.utils.FakeModManager", + "io.github.fabriccompatibiltylayers.modremappingapi.impl.DefaultModEntry", + "io.github.fabriccompatibiltylayers.modremappingapi.impl.DefaultModRemapper", "fr.catcore.modremapperapi.utils.FileUtils", "fr.catcore.modremapperapi.utils.MappingsUtils", -// "fr.catcore.modremapperapi.utils.MappingsUtils$1", "fr.catcore.modremapperapi.utils.MixinUtils", - "fr.catcore.modremapperapi.utils.ModDiscoverer", - "fr.catcore.modremapperapi.utils.ModDiscoverer$1", - "fr.catcore.modremapperapi.utils.ModDiscoverer$2", - "fr.catcore.modremapperapi.utils.ModEntry", + "io.github.fabriccompatibiltylayers.modremappingapi.impl.ModDiscoverer", + "io.github.fabriccompatibiltylayers.modremappingapi.impl.ModDiscoverer$1", + "io.github.fabriccompatibiltylayers.modremappingapi.impl.ModEntry", "fr.catcore.modremapperapi.utils.RefmapJson", "fr.catcore.modremapperapi.remapping.MapEntryType", "fr.catcore.modremapperapi.remapping.MappingBuilder", @@ -356,7 +333,7 @@ private static void preloadClasses() { "fr.catcore.modremapperapi.remapping.MixinPostApplyVisitor", "fr.catcore.modremapperapi.remapping.MRAClassVisitor", "fr.catcore.modremapperapi.remapping.MRAMethodVisitor", - "fr.catcore.modremapperapi.remapping.MRAPostApplyVisitor", + "fr.catcore.modremapperapi.remapping.MRAApplyVisitor", "fr.catcore.modremapperapi.remapping.RefmapRemapper", "fr.catcore.modremapperapi.remapping.RemapUtil", "fr.catcore.modremapperapi.remapping.RemapUtil$MappingList", @@ -531,18 +508,23 @@ private static TinyRemapper makeRemapper(MappingTree... trees) { builder.withMappings(MappingsUtilsImpl.createProvider(tree, "official", MappingsUtils.getTargetNamespace())); } - MRAPostApplyVisitor applyVisitor = new MRAPostApplyVisitor(); - MixinPostApplyVisitor mixinPostApplyVisitor = new MixinPostApplyVisitor(trees); + MRAApplyVisitor preApplyVisitor = new MRAApplyVisitor(); + MRAApplyVisitor postApplyVisitor = new MRAApplyVisitor(); + MixinPostApplyVisitor mixinPostApplyVisitor = new MixinPostApplyVisitor(); - VisitorInfosImpl infos = new VisitorInfosImpl(); + VisitorInfosImpl preInfos = new VisitorInfosImpl(); + VisitorInfosImpl postInfos = new VisitorInfosImpl(); - for (ModRemapper modRemapper : ModRemappingAPI.MOD_REMAPPERS) { - modRemapper.registerVisitors(infos); + for (ModRemapper modRemapper : remappers) { + modRemapper.registerPreVisitors(preInfos); + modRemapper.registerPostVisitors(postInfos); } - applyVisitor.setInfos(infos); + preApplyVisitor.setInfos(preInfos); + postApplyVisitor.setInfos(postInfos); - builder.extraPostApplyVisitor(applyVisitor); + builder.extraPreApplyVisitor(preApplyVisitor); + builder.extraPostApplyVisitor(postApplyVisitor); builder.extraPostApplyVisitor(mixinPostApplyVisitor); builder.extension(new MixinExtension(EnumSet.of(MixinExtension.AnnotationTarget.HARD))); @@ -551,9 +533,14 @@ private static TinyRemapper makeRemapper(MappingTree... trees) { MappingsUtils.addMinecraftJar(remapper); - for (ModRemapper modRemapper : ModRemappingAPI.MOD_REMAPPERS) { - for (RemapLibrary library : modRemapper.getRemapLibraries()) { + for (ModRemapper modRemapper : remappers) { + List libraries = new ArrayList<>(); + + modRemapper.addRemapLibraries(libraries, FabricLoader.getInstance().getEnvironmentType()); + + for (RemapLibrary library : libraries) { File libPath = new File(Constants.LIB_FOLDER, library.fileName); + if (libPath.exists()) { remapper.readClassPathAsync(libPath.toPath()); } else { diff --git a/src/main/java/fr/catcore/modremapperapi/utils/BArrayList.java b/src/main/java/fr/catcore/modremapperapi/utils/BArrayList.java index ad563a5..62de7c7 100644 --- a/src/main/java/fr/catcore/modremapperapi/utils/BArrayList.java +++ b/src/main/java/fr/catcore/modremapperapi/utils/BArrayList.java @@ -2,6 +2,7 @@ import java.util.ArrayList; +@Deprecated public class BArrayList extends ArrayList { public BArrayList put(T entry) { diff --git a/src/main/java/fr/catcore/modremapperapi/utils/CollectionUtils.java b/src/main/java/fr/catcore/modremapperapi/utils/CollectionUtils.java index cc1e4d1..14fd783 100644 --- a/src/main/java/fr/catcore/modremapperapi/utils/CollectionUtils.java +++ b/src/main/java/fr/catcore/modremapperapi/utils/CollectionUtils.java @@ -3,6 +3,7 @@ import java.util.*; import java.util.function.Function; +@Deprecated public class CollectionUtils { public static Collection transformList(Collection collection, Function function) { diff --git a/src/main/java/fr/catcore/modremapperapi/utils/DefaultModRemapper.java b/src/main/java/fr/catcore/modremapperapi/utils/DefaultModRemapper.java deleted file mode 100644 index 7ee98bb..0000000 --- a/src/main/java/fr/catcore/modremapperapi/utils/DefaultModRemapper.java +++ /dev/null @@ -1,35 +0,0 @@ -package fr.catcore.modremapperapi.utils; - -import fr.catcore.modremapperapi.api.ModRemapper; -import fr.catcore.modremapperapi.api.RemapLibrary; -import fr.catcore.modremapperapi.remapping.RemapUtil; -import fr.catcore.modremapperapi.remapping.VisitorInfos; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class DefaultModRemapper implements ModRemapper { - @Override - public String[] getJarFolders() { - return new String[] {"mods"}; - } - - @Override - public RemapLibrary[] getRemapLibraries() { - return new RemapLibrary[0]; - } - - @Override - public Map> getExclusions() { - return new HashMap<>(); - } - - @Override - public void getMappingList(RemapUtil.MappingList list) {} - - @Override - public void registerVisitors(VisitorInfos infos) { - - } -} diff --git a/src/main/java/fr/catcore/modremapperapi/utils/FakeModManager.java b/src/main/java/fr/catcore/modremapperapi/utils/FakeModManager.java deleted file mode 100644 index 6a6bd7c..0000000 --- a/src/main/java/fr/catcore/modremapperapi/utils/FakeModManager.java +++ /dev/null @@ -1,28 +0,0 @@ -package fr.catcore.modremapperapi.utils; - -import java.util.*; - -public class FakeModManager { - private static final List MODS = new ArrayList<>(); - - private static boolean loaded = false; - private static boolean loading = false; - - public static void init() { - if (!loaded && !loading) { - loading = true; - ModDiscoverer.init(); - loading = false; - loaded = true; - } - } - - protected static void addModEntry(ModEntry modEntry) { - MODS.add(modEntry); - Constants.MAIN_LOGGER.info("Added " + modEntry.getType() + " mod " + modEntry.modName + " to mod list."); - } - - public static List getMods() { - return MODS; - } -} diff --git a/src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java b/src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java index 38f851a..d48bdb6 100644 --- a/src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java +++ b/src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java @@ -11,6 +11,7 @@ import net.fabricmc.mappingio.tree.MappingTree; import net.fabricmc.mappingio.tree.MemoryMappingTree; import net.fabricmc.tinyremapper.*; +import org.jetbrains.annotations.ApiStatus; import java.io.*; import java.nio.file.Path; @@ -56,6 +57,7 @@ public static MappingTree getMinecraftMappings() { return MappingsUtilsImpl.getVanillaMappings(); } + @Deprecated public static IMappingProvider createProvider(MappingTree mappings) { return MappingsUtilsImpl.createProvider(mappings, getNativeNamespace(), getTargetNamespace()); } @@ -98,6 +100,7 @@ private static Path[] getMinecraftJar() throws IOException { return paths.values().toArray(new Path[0]); } + @ApiStatus.Internal public static void addMinecraftJar(TinyRemapper remapper) { if (FabricLoader.getInstance().isDevelopmentEnvironment()) { try { diff --git a/src/main/java/fr/catcore/modremapperapi/utils/MixinUtils.java b/src/main/java/fr/catcore/modremapperapi/utils/MixinUtils.java index 4f86c33..178bbfe 100644 --- a/src/main/java/fr/catcore/modremapperapi/utils/MixinUtils.java +++ b/src/main/java/fr/catcore/modremapperapi/utils/MixinUtils.java @@ -27,7 +27,6 @@ public class MixinUtils { * @param mixinClassName * @param mixinInfo */ - @Deprecated public static void applyASMMagic(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { CursedMixinExtensions.postApply(targetClass); diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/ModRemappingAPI.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/ModRemappingAPI.java deleted file mode 100644 index 820f0a1..0000000 --- a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/ModRemappingAPI.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.github.fabriccompatibiltylayers.modremappingapi; - -public class ModRemappingAPI { -} diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/ModRemapper.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/ModRemapper.java deleted file mode 100644 index 243a3cb..0000000 --- a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/ModRemapper.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.github.fabriccompatibiltylayers.modremappingapi.api; - -import fr.catcore.modremapperapi.api.RemapLibrary; -import fr.catcore.modremapperapi.remapping.RemapUtil; -import net.fabricmc.api.EnvType; -import net.fabricmc.loader.api.FabricLoader; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -public interface ModRemapper { - String[] getJarFolders(); - - default RemapLibrary[] getRemapLibraries() { - List libraries = new ArrayList<>(); - addRemapLibraries(libraries, FabricLoader.getInstance().getEnvironmentType()); - return libraries.toArray(new RemapLibrary[0]); - } - - @SuppressWarnings("unused") - default void addRemapLibraries(List libraries, EnvType environment) {} - - Map> getExclusions(); - - void getMappingList(RemapUtil.MappingList list); - - void registerVisitors(VisitorInfos infos); - - default Optional getDefaultPackage() { - return Optional.empty(); - } - - default void afterRemap() {} -} diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/MappingBuilder.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/MappingBuilder.java new file mode 100644 index 0000000..fb9e64a --- /dev/null +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/MappingBuilder.java @@ -0,0 +1,14 @@ +package io.github.fabriccompatibiltylayers.modremappingapi.api.v1; + +public interface MappingBuilder { + + ClassMapping addMapping(String sourceName, String targetName); + ClassMapping addMapping(String name); + + public interface ClassMapping { + ClassMapping field(String sourceName, String targetName, String sourceDescriptor); + ClassMapping field(String name, String descriptor); + ClassMapping method(String sourceName, String targetName, String sourceDescriptor); + ClassMapping method(String name, String descriptor); + } +} diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/ModRemapper.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/ModRemapper.java new file mode 100644 index 0000000..863c494 --- /dev/null +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/ModRemapper.java @@ -0,0 +1,23 @@ +package io.github.fabriccompatibiltylayers.modremappingapi.api.v1; + +import net.fabricmc.api.EnvType; + +import java.util.List; +import java.util.Optional; + +public interface ModRemapper { + String[] getJarFolders(); + + void addRemapLibraries(List libraries, EnvType environment); + + void registerMappings(MappingBuilder list); + + void registerPreVisitors(VisitorInfos infos); + void registerPostVisitors(VisitorInfos infos); + + default Optional getDefaultPackage() { + return Optional.empty(); + } + + default void afterRemap() {} +} diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/RemapLibrary.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/RemapLibrary.java new file mode 100644 index 0000000..c0472fa --- /dev/null +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/RemapLibrary.java @@ -0,0 +1,43 @@ +package io.github.fabriccompatibiltylayers.modremappingapi.api.v1; + +import org.jetbrains.annotations.ApiStatus; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +public class RemapLibrary { + public final String url; + public final Path path; + public final List toExclude; + public final String fileName; + + public RemapLibrary(String url, List toExclude, String fileName) { + this.url = url; + this.path = null; + this.toExclude = toExclude; + this.fileName = fileName; + } + + public RemapLibrary(Path path, List toExclude, String fileName) { + this.url = ""; + this.path = path; + this.toExclude = toExclude; + this.fileName = fileName; + } + + public RemapLibrary(Path path, String fileName) { + this.url = ""; + this.path = path; + this.toExclude = new ArrayList<>(); + this.fileName = fileName; + } + + @ApiStatus.Internal + public RemapLibrary(String url, Path path, List toExclude, String fileName) { + this.url = url; + this.path = path; + this.toExclude = toExclude; + this.fileName = fileName; + } +} diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/VisitorInfos.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/VisitorInfos.java similarity index 89% rename from src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/VisitorInfos.java rename to src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/VisitorInfos.java index 42db29c..34b9b70 100644 --- a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/VisitorInfos.java +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/VisitorInfos.java @@ -1,5 +1,6 @@ -package io.github.fabriccompatibiltylayers.modremappingapi.api; +package io.github.fabriccompatibiltylayers.modremappingapi.api.v1; +import io.github.fabriccompatibiltylayers.modremappingapi.api.MappingUtils; import org.jetbrains.annotations.Nullable; public interface VisitorInfos { diff --git a/src/main/java/fr/catcore/modremapperapi/utils/DefaultModEntry.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/DefaultModEntry.java similarity index 80% rename from src/main/java/fr/catcore/modremapperapi/utils/DefaultModEntry.java rename to src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/DefaultModEntry.java index 3414d72..d72b631 100644 --- a/src/main/java/fr/catcore/modremapperapi/utils/DefaultModEntry.java +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/DefaultModEntry.java @@ -1,4 +1,4 @@ -package fr.catcore.modremapperapi.utils; +package io.github.fabriccompatibiltylayers.modremappingapi.impl; import java.io.File; diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/DefaultModRemapper.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/DefaultModRemapper.java new file mode 100644 index 0000000..a718099 --- /dev/null +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/DefaultModRemapper.java @@ -0,0 +1,36 @@ +package io.github.fabriccompatibiltylayers.modremappingapi.impl; + +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingBuilder; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.RemapLibrary; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.VisitorInfos; +import net.fabricmc.api.EnvType; + +import java.util.List; + +public class DefaultModRemapper implements ModRemapper { + @Override + public String[] getJarFolders() { + return new String[] {"mods"}; + } + + @Override + public void addRemapLibraries(List libraries, EnvType environment) { + + } + + @Override + public void registerMappings(MappingBuilder list) { + + } + + @Override + public void registerPreVisitors(VisitorInfos infos) { + + } + + @Override + public void registerPostVisitors(VisitorInfos infos) { + + } +} diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/MappingBuilderImpl.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/MappingBuilderImpl.java new file mode 100644 index 0000000..1222533 --- /dev/null +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/MappingBuilderImpl.java @@ -0,0 +1,76 @@ +package io.github.fabriccompatibiltylayers.modremappingapi.impl; + +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingBuilder; +import net.fabricmc.mappingio.MappedElementKind; +import net.fabricmc.mappingio.tree.MemoryMappingTree; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +@ApiStatus.Internal +public class MappingBuilderImpl implements MappingBuilder { + private final MemoryMappingTree next; + + public MappingBuilderImpl(MemoryMappingTree next) { + this.next = next; + } + + @Override + public ClassMapping addMapping(String sourceName, String targetName) { + this.next.visitClass(sourceName); + this.next.visitDstName(MappedElementKind.CLASS, 0, targetName); + return new ClassMappingImpl(sourceName, targetName, next); + } + + @Override + public ClassMapping addMapping(String name) { + this.next.visitClass(name); + return new ClassMappingImpl(name, null, next); + } + + private static class ClassMappingImpl implements ClassMapping { + private final String sourceName; + private final @Nullable String targetName; + private final MemoryMappingTree next; + + public ClassMappingImpl(String sourceName, @Nullable String targetName, MemoryMappingTree next) { + this.sourceName = sourceName; + this.targetName = targetName; + this.next = next; + } + + private void visit() { + this.next.visitClass(sourceName); + if (targetName != null) this.next.visitDstName(MappedElementKind.CLASS, 0, targetName); + } + + @Override + public ClassMapping field(String sourceName, String targetName, String sourceDescriptor) { + visit(); + + this.next.visitField(sourceName, sourceDescriptor); + if (targetName != null) this.next.visitDstName(MappedElementKind.FIELD, 0, targetName); + + return this; + } + + @Override + public ClassMapping field(String name, String descriptor) { + return this.field(name, null, descriptor); + } + + @Override + public ClassMapping method(String sourceName, String targetName, String sourceDescriptor) { + visit(); + + this.next.visitMethod(sourceName, sourceDescriptor); + if (targetName != null) this.next.visitDstName(MappedElementKind.METHOD, 0, targetName); + + return this; + } + + @Override + public ClassMapping method(String name, String descriptor) { + return this.method(name, null, descriptor); + } + } +} diff --git a/src/main/java/fr/catcore/modremapperapi/utils/ModDiscoverer.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModDiscoverer.java similarity index 71% rename from src/main/java/fr/catcore/modremapperapi/utils/ModDiscoverer.java rename to src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModDiscoverer.java index abfa702..25032e8 100644 --- a/src/main/java/fr/catcore/modremapperapi/utils/ModDiscoverer.java +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModDiscoverer.java @@ -1,10 +1,11 @@ -package fr.catcore.modremapperapi.utils; +package io.github.fabriccompatibiltylayers.modremappingapi.impl; -import fr.catcore.modremapperapi.ModRemappingAPI; -import fr.catcore.modremapperapi.api.ModRemapper; +import fr.catcore.modremapperapi.utils.Constants; +import fr.catcore.modremapperapi.utils.FileUtils; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper; import fr.catcore.modremapperapi.remapping.RemapUtil; +import io.github.fabriccompatibiltylayers.modremappingapi.impl.compatibility.V0ModRemapper; import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.impl.launch.FabricLauncher; import net.fabricmc.loader.impl.launch.FabricLauncherBase; import org.spongepowered.include.com.google.common.collect.ImmutableList; @@ -17,6 +18,7 @@ import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.*; +import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; @@ -24,16 +26,18 @@ public class ModDiscoverer { private static final Map> EXCLUDED = new HashMap<>(); - protected static void init() { - RemapUtil.init(); + protected static void init(List modRemappers, boolean remapClassEdits) { + RemapUtil.init(modRemappers); List mods = new ArrayList<>(); - for (ModRemapper remapper : ModRemappingAPI.MOD_REMAPPERS) { - EXCLUDED.putAll(remapper.getExclusions()); + for (ModRemapper remapper : modRemappers) { + if (remapper instanceof V0ModRemapper) { + EXCLUDED.putAll(((V0ModRemapper) remapper).getExclusions()); + } } - for (ModRemapper remapper : ModRemappingAPI.MOD_REMAPPERS) { + for (ModRemapper remapper : modRemappers) { for (String jarFolder : remapper.getJarFolders()) { File mcSubFolder = new File(FabricLoader.getInstance().getGameDir().toFile(), jarFolder); File cacheFolder = new File(Constants.VERSIONED_FOLDER, jarFolder); @@ -41,33 +45,7 @@ protected static void init() { if (!mcSubFolder.exists()) mcSubFolder.mkdirs(); if (!cacheFolder.exists()) cacheFolder.mkdirs(); - try { - Files.walkFileTree(cacheFolder.toPath(), new FileVisitor() { - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFileFailed(Path file, IOException exc) { - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } catch (IOException e) { - throw new RuntimeException(e); - } + emptyDir(cacheFolder); mods.addAll(discoverModsInFolder(mcSubFolder, cacheFolder)); } @@ -75,54 +53,55 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx File mainTempDir = new File(Constants.VERSIONED_FOLDER, "temp"); if (mainTempDir.exists()) { - try { - Files.walkFileTree(mainTempDir.toPath(), new FileVisitor() { - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { - return FileVisitResult.CONTINUE; - } + emptyDir(mainTempDir); + } - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } + Map modPaths = mods.stream() + .filter(entry -> entry.original != null) + .collect(Collectors.toMap(entry -> entry.original.toPath(), entry -> entry.file.toPath())); - @Override - public FileVisitResult visitFileFailed(Path file, IOException exc) { - return FileVisitResult.CONTINUE; - } + if (!remapClassEdits) { + modPaths = excludeClassEdits(modPaths); + } - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } catch (IOException e) { - throw new RuntimeException(e); - } + for (Path path : modPaths.keySet()) { + RemapUtil.makeModMappings(path); } RemapUtil.generateModMappings(); - Map modPaths = new HashMap<>(); + RemapUtil.remapMods(modPaths); - for (ModEntry entry : mods) { - if (entry.original != null) modPaths.put(entry.original.toPath(), entry.file.toPath()); + modPaths.values().forEach(FabricLauncherBase.getLauncher()::addToClassPath); + } - FakeModManager.addModEntry(entry); - } + private static void emptyDir(File mainTempDir) { + try { + Files.walkFileTree(mainTempDir.toPath(), new FileVisitor() { + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { + return FileVisitResult.CONTINUE; + } - if (!ModRemappingAPI.remapClassEdits) { - modPaths = excludeClassEdits(modPaths); - } + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } - RemapUtil.remapMods(modPaths); + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) { + return FileVisitResult.CONTINUE; + } - for (Path modPath : modPaths.values()) { - Constants.MAIN_LOGGER.debug("Adding path to classpath: " + modPath.toString()); - FabricLauncherBase.getLauncher().addToClassPath(modPath); + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + throw new RuntimeException(e); } } @@ -130,6 +109,7 @@ private static void zipFile(File fileToZip, String fileName, ZipOutputStream zip if (fileToZip.isHidden()) { return; } + if (fileToZip.isDirectory()) { if (!root) { if (fileName.endsWith("/")) { @@ -140,20 +120,26 @@ private static void zipFile(File fileToZip, String fileName, ZipOutputStream zip zipOut.closeEntry(); } } + File[] children = fileToZip.listFiles(); + for (File childFile : children) { zipFile(childFile, (root ? "" : fileName + "/") + childFile.getName(), zipOut, false); } + return; } + FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileName); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; + while ((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); } + zipOut.closeEntry(); } @@ -211,9 +197,7 @@ private static Map excludeClassEdits(Map modPaths) { } } - for (Path path : errored) { - map.remove(path); - } + errored.forEach(map::remove); return map; } @@ -308,14 +292,7 @@ private static List discoverModsInFolder(File folder, File destination } } - if (!modName.isEmpty()) { - RemapUtil.makeModMappings(file.toPath()); - - while (!modName.isEmpty()) { - ModEntry modname = modName.remove(0); - mods.add(modname); - } - } + mods.addAll(modName); } } diff --git a/src/main/java/fr/catcore/modremapperapi/utils/ModEntry.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModEntry.java similarity index 86% rename from src/main/java/fr/catcore/modremapperapi/utils/ModEntry.java rename to src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModEntry.java index 21c4c82..68899bf 100644 --- a/src/main/java/fr/catcore/modremapperapi/utils/ModEntry.java +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModEntry.java @@ -1,4 +1,4 @@ -package fr.catcore.modremapperapi.utils; +package io.github.fabriccompatibiltylayers.modremappingapi.impl; import java.io.File; diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModRemappingAPIImpl.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModRemappingAPIImpl.java new file mode 100644 index 0000000..3ab181f --- /dev/null +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModRemappingAPIImpl.java @@ -0,0 +1,48 @@ +package io.github.fabriccompatibiltylayers.modremappingapi.impl; + +import fr.catcore.wfvaio.FabricVariants; +import fr.catcore.wfvaio.WhichFabricVariantAmIOn; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper; +import io.github.fabriccompatibiltylayers.modremappingapi.impl.compatibility.V0ModRemapper; +import net.fabricmc.loader.api.FabricLoader; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class ModRemappingAPIImpl { + + private static final String v0EntrypointName = "mod-remapper-api:modremapper"; + private static final String v1EntrypointName = "mod-remapper-api:modremapper_v1"; + private static final List modRemappers = new ArrayList<>(); + public static final boolean BABRIC = WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC || WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC_NEW_FORMAT; + + public static boolean remapClassEdits = false; + + private static boolean init = false; + private static boolean initializing = false; + + public static void init() { + if (!init && !initializing) { + initializing = true; + + FabricLoader.getInstance().getConfigDir().toFile().mkdirs(); + remapClassEdits = new File(FabricLoader.getInstance().getConfigDir().toFile(), ".remapclassedits").exists(); + + FabricLoader.getInstance() + .getEntrypoints(v0EntrypointName, fr.catcore.modremapperapi.api.ModRemapper.class) + .stream() + .map(V0ModRemapper::new) + .forEach(modRemappers::add); + + modRemappers.addAll(FabricLoader.getInstance().getEntrypoints(v1EntrypointName, ModRemapper.class)); + + ModDiscoverer.init(modRemappers, remapClassEdits); + + modRemappers.forEach(ModRemapper::afterRemap); + + initializing = false; + init = true; + } + } +} diff --git a/src/main/java/fr/catcore/modremapperapi/ModRemappingApiInit.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModRemappingApiInit.java similarity index 55% rename from src/main/java/fr/catcore/modremapperapi/ModRemappingApiInit.java rename to src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModRemappingApiInit.java index b1089cc..08e1c00 100644 --- a/src/main/java/fr/catcore/modremapperapi/ModRemappingApiInit.java +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModRemappingApiInit.java @@ -1,11 +1,9 @@ -package fr.catcore.modremapperapi; - -import static fr.catcore.modremapperapi.ModRemappingAPI.init; +package io.github.fabriccompatibiltylayers.modremappingapi.impl; public class ModRemappingApiInit implements Runnable { public ModRemappingApiInit() {} @Override public void run() { - init(); + ModRemappingAPIImpl.init(); } } diff --git a/src/main/java/fr/catcore/modremapperapi/RemapperPlugin.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/RemapperPlugin.java similarity index 85% rename from src/main/java/fr/catcore/modremapperapi/RemapperPlugin.java rename to src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/RemapperPlugin.java index d2bb906..5dc0bb5 100644 --- a/src/main/java/fr/catcore/modremapperapi/RemapperPlugin.java +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/RemapperPlugin.java @@ -1,4 +1,4 @@ -package fr.catcore.modremapperapi; +package io.github.fabriccompatibiltylayers.modremappingapi.impl; import org.objectweb.asm.tree.ClassNode; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; @@ -28,7 +28,7 @@ public void acceptTargets(Set myTargets, Set otherTargets){} @Override public List getMixins(){ try { - ((Runnable)Class.forName("fr.catcore.modremapperapi.ModRemappingApiInit").newInstance()).run(); + ((Runnable)Class.forName("io.github.fabriccompatibiltylayers.modremappingapi.impl.ModRemappingApiInit").newInstance()).run(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new RuntimeException(e); } diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/VersionHelper.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/VersionHelper.java index 85b5ce9..f2a80c0 100644 --- a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/VersionHelper.java +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/VersionHelper.java @@ -4,7 +4,9 @@ import net.fabricmc.loader.api.Version; import net.fabricmc.loader.api.VersionParsingException; import net.fabricmc.loader.api.metadata.version.VersionPredicate; +import org.jetbrains.annotations.ApiStatus; +@ApiStatus.Internal public class VersionHelper { private static final Version MC_VERSION = FabricLoader.getInstance().getModContainer("minecraft") .get().getMetadata().getVersion(); diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/VisitorInfosImpl.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/VisitorInfosImpl.java index 4a399c1..db2c14d 100644 --- a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/VisitorInfosImpl.java +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/VisitorInfosImpl.java @@ -1,6 +1,6 @@ package io.github.fabriccompatibiltylayers.modremappingapi.impl; -import io.github.fabriccompatibiltylayers.modremappingapi.api.VisitorInfos; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.VisitorInfos; import org.jetbrains.annotations.ApiStatus; import java.util.HashMap; diff --git a/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/compatibility/V0ModRemapper.java b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/compatibility/V0ModRemapper.java new file mode 100644 index 0000000..d778bc3 --- /dev/null +++ b/src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/compatibility/V0ModRemapper.java @@ -0,0 +1,136 @@ +package io.github.fabriccompatibiltylayers.modremappingapi.impl.compatibility; + +import fr.catcore.modremapperapi.remapping.RemapUtil; +import fr.catcore.wfvaio.FabricVariants; +import fr.catcore.wfvaio.WhichFabricVariantAmIOn; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingBuilder; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.RemapLibrary; +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.VisitorInfos; +import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl; +import net.fabricmc.api.EnvType; +import net.fabricmc.mappingio.MappingVisitor; +import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch; +import net.fabricmc.mappingio.tree.MappingTree; +import net.fabricmc.mappingio.tree.MemoryMappingTree; +import org.jetbrains.annotations.ApiStatus; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +@ApiStatus.Internal +public class V0ModRemapper implements ModRemapper { + private static final boolean BABRIC = WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC || WhichFabricVariantAmIOn.getVariant() == FabricVariants.BABRIC_NEW_FORMAT; + + private final fr.catcore.modremapperapi.api.ModRemapper modRemapper; + + public V0ModRemapper(fr.catcore.modremapperapi.api.ModRemapper modRemapper) { + this.modRemapper = modRemapper; + } + + @Override + public String[] getJarFolders() { + return modRemapper.getJarFolders(); + } + + @Override + public void addRemapLibraries(List libraries, EnvType environment) { + for (fr.catcore.modremapperapi.api.RemapLibrary library : modRemapper.getRemapLibraries()) { + libraries.add(new RemapLibrary(library.url, library.path, library.toExclude, library.fileName)); + } + } + + public Map> getExclusions() { + return modRemapper.getExclusions(); + } + + @Override + public void registerMappings(MappingBuilder list) { + RemapUtil.MappingList mappingList = new RemapUtil.MappingList(); + modRemapper.getMappingList(mappingList); + + MemoryMappingTree mappingTree = convertMappingList(mappingList); + + if (mappingTree != null) { + for (MappingTree.ClassMapping classMapping : mappingTree.getClasses()) { + MappingBuilder.ClassMapping classBuilder = list.addMapping( + classMapping.getName("official"), + classMapping.getName("intermediary") + ); + + for (MappingTree.FieldMapping fieldMapping : classMapping.getFields()) { + classBuilder.field( + fieldMapping.getName("official"), + fieldMapping.getName("intermediary"), + fieldMapping.getDesc("official") + ); + } + + for (MappingTree.MethodMapping methodMapping : classMapping.getMethods()) { + classBuilder.method( + methodMapping.getName("official"), + methodMapping.getName("intermediary"), + methodMapping.getDesc("official") + ); + } + } + } + } + + @Override + public void registerPreVisitors(VisitorInfos infos) { + + } + + @Override + public void registerPostVisitors(VisitorInfos infos) { + modRemapper.registerVisitors((fr.catcore.modremapperapi.remapping.VisitorInfos) infos); + } + + @Override + public Optional getDefaultPackage() { + return modRemapper.getDefaultPackage(); + } + + @Override + public void afterRemap() { + modRemapper.afterRemap(); + } + + /** + * Convert old MappingList to a MemoryMappingTree, fixes some quirks of the previous mapping format on babric + * where "intermediary" is the main namespace instead of "official". + * @param mappingList + * @return + */ + private MemoryMappingTree convertMappingList(RemapUtil.MappingList mappingList) { + MemoryMappingTree mappingTree = new MemoryMappingTree(); + + try { + if (BABRIC) { + MappingsUtilsImpl.initializeMappingTree(mappingTree, "intermediary", "official"); + } else { + MappingsUtilsImpl.initializeMappingTree(mappingTree); + } + + mappingList.accept(mappingTree); + + mappingTree.visitEnd(); + + if (Objects.equals(mappingTree.getSrcNamespace(), "intermediary")) { + MemoryMappingTree newTree = new MemoryMappingTree(); + MappingVisitor visitor = new MappingSourceNsSwitch(newTree, "official"); + + mappingTree.accept(visitor); + mappingTree = newTree; + } + } catch (IOException e) { + throw new RuntimeException("Error while generating remappers mappings", e); + } + + return mappingTree; + } +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index c21ef4c..0c8c05b 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -22,8 +22,8 @@ "icon": "assets/mod-remapping-api/icon.png", "environment": "*", "entrypoints": { - "mod-remapper-api:modremapper": [ - "fr.catcore.modremapperapi.utils.DefaultModRemapper" + "mod-remapper-api:modremapper_v1": [ + "io.github.fabriccompatibiltylayers.modremappingapi.impl.DefaultModRemapper" ], "spasm:raw_transformer": [ "fr.catcore.modremapperapi.ClassTransformer" diff --git a/src/main/resources/mixins.mod-remapping-api.json b/src/main/resources/mixins.mod-remapping-api.json index 88b9fa3..e989ef2 100644 --- a/src/main/resources/mixins.mod-remapping-api.json +++ b/src/main/resources/mixins.mod-remapping-api.json @@ -1,4 +1,4 @@ { "minVersion": "0.8", - "plugin": "fr.catcore.modremapperapi.RemapperPlugin" + "plugin": "io.github.fabriccompatibiltylayers.modremappingapi.impl.RemapperPlugin" } \ No newline at end of file