|
1 | 1 | package io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings;
|
2 | 2 |
|
| 3 | +import fr.catcore.modremapperapi.utils.Constants; |
| 4 | +import fr.catcore.wfvaio.WhichFabricVariantAmIOn; |
| 5 | +import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingBuilder; |
| 6 | +import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingBuilderImpl; |
| 7 | +import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl; |
| 8 | +import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils; |
| 9 | +import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.VersionHelper; |
| 10 | +import net.fabricmc.loader.api.FabricLoader; |
| 11 | +import net.fabricmc.loader.impl.launch.MappingConfiguration; |
| 12 | +import net.fabricmc.mappingio.MappingVisitor; |
| 13 | +import net.fabricmc.mappingio.tree.MappingTree; |
| 14 | +import net.fabricmc.mappingio.tree.MemoryMappingTree; |
3 | 15 | import org.jetbrains.annotations.ApiStatus;
|
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.net.URL; |
| 21 | +import java.net.URLConnection; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Locale; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.zip.ZipError; |
| 27 | + |
| 28 | +import static fr.catcore.modremapperapi.remapping.RemapUtil.defaultPackage; |
4 | 29 |
|
5 | 30 | @ApiStatus.Internal
|
6 | 31 | public class MappingsRegistry {
|
| 32 | + public static final MemoryMappingTree VANILLA; |
| 33 | + public static MemoryMappingTree FORMATTED = new MemoryMappingTree(); |
| 34 | + public static boolean generated = false; |
| 35 | + |
| 36 | + public static MemoryMappingTree MODS; |
| 37 | + |
| 38 | + static { |
| 39 | + try { |
| 40 | + MODS = MappingTreeHelper.createMappingTree(); |
| 41 | + } catch (IOException e) { |
| 42 | + throw new RuntimeException(e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static MemoryMappingTree FULL = new MemoryMappingTree(); |
| 47 | + |
| 48 | + static { |
| 49 | + URL url = MappingConfiguration.class.getClassLoader().getResource("mappings/mappings.tiny"); |
| 50 | + |
| 51 | + if (url != null) { |
| 52 | + try { |
| 53 | + URLConnection connection = url.openConnection(); |
| 54 | + |
| 55 | + VANILLA = MappingTreeHelper.readMappings(connection.getInputStream()); |
| 56 | + } catch (IOException | ZipError e) { |
| 57 | + throw new RuntimeException("Error reading "+url, e); |
| 58 | + } |
| 59 | + } else { |
| 60 | + VANILLA = null; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static void generateFormattedMappings(@Nullable InputStream extraStream) throws IOException { |
| 65 | + generated = true; |
| 66 | + |
| 67 | + Map<String, String> renames = new HashMap<>(); |
| 68 | + boolean switchNamespace = false; |
| 69 | + |
| 70 | + switch (WhichFabricVariantAmIOn.getVariant()) { |
| 71 | + case BABRIC: |
| 72 | + renames.put(FabricLoader.getInstance().getEnvironmentType().name().toLowerCase(Locale.ENGLISH), "official"); |
| 73 | + switchNamespace = true; |
| 74 | + break; |
| 75 | + case ORNITHE_V2: |
| 76 | + Boolean merged = VersionHelper.predicate(">=1.3"); |
| 77 | + if (merged != null && !merged) { |
| 78 | + renames.put(FabricLoader.getInstance().getEnvironmentType().name().toLowerCase(Locale.ENGLISH) + "Official", "official"); |
| 79 | + switchNamespace = true; |
| 80 | + } |
| 81 | + break; |
| 82 | + case BABRIC_NEW_FORMAT: |
| 83 | + renames.put(FabricLoader.getInstance().getEnvironmentType().name().toLowerCase(Locale.ENGLISH) + "Official", "official"); |
| 84 | + switchNamespace = true; |
| 85 | + break; |
| 86 | + default: |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + MemoryMappingTree tempTree = new MemoryMappingTree(); |
| 91 | + MappingVisitor visitor = MappingTreeHelper.getNsReorderingVisitor(tempTree, switchNamespace, renames); |
| 92 | + |
| 93 | + VANILLA.accept(visitor); |
| 94 | + |
| 95 | + if (extraStream == null) { |
| 96 | + tempTree.accept(FORMATTED); |
| 97 | + } else { |
| 98 | + MappingTree extra = MappingTreeHelper.readMappings(extraStream); |
| 99 | + |
| 100 | + MappingTreeHelper.mergeIntoNew( |
| 101 | + FORMATTED, |
| 102 | + tempTree, |
| 103 | + extra |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + FORMATTED.accept(FULL); |
| 108 | + } |
| 109 | + |
| 110 | + public static void addModMappings(Path path) { |
| 111 | + MappingBuilder mappingBuilder = new MappingBuilderImpl(MODS); |
| 112 | + |
| 113 | + try { |
| 114 | + FileUtils.listPathContent(path) |
| 115 | + .stream() |
| 116 | + .filter(file -> file.endsWith(".class")) |
| 117 | + .map(file -> file.replace(".class", "")) |
| 118 | + .forEach(cl -> mappingBuilder.addMapping(cl, (cl.contains("/") ? "" : defaultPackage) + cl)); |
| 119 | + } catch (IOException e) { |
| 120 | + throw new RuntimeException(e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public static void generateModMappings() { |
| 125 | + try { |
| 126 | + MODS.visitEnd(); |
| 127 | + |
| 128 | + MappingTreeHelper.exportMappings(MODS, Constants.REMAPPED_MAPPINGS_FILE.toPath()); |
| 129 | + } catch (IOException e) { |
| 130 | + throw new RuntimeException("Error while generating mods mappings", e); |
| 131 | + } |
| 132 | + |
| 133 | + MappingsUtilsImpl.addMappingsToContext(MODS); |
| 134 | + } |
7 | 135 | }
|
0 commit comments