|
| 1 | +/* |
| 2 | + * Minecraft Forge, Patchwork Project |
| 3 | + * Copyright (c) 2016-2020, 2019-2020 |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation version 2.1 |
| 8 | + * of the License. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | +package com.patchworkmc.impl.fml; |
| 21 | + |
| 22 | +import javax.annotation.Nonnull; |
| 23 | +import javax.annotation.Nullable; |
| 24 | + |
| 25 | +import cpw.mods.modlauncher.api.INameMappingService; |
| 26 | + |
| 27 | +import net.fabricmc.loader.api.FabricLoader; |
| 28 | +import net.fabricmc.loader.launch.common.FabricLauncherBase; |
| 29 | +import net.fabricmc.mapping.tree.ClassDef; |
| 30 | +import net.fabricmc.mapping.tree.Mapped; |
| 31 | +import net.fabricmc.mapping.tree.TinyTree; |
| 32 | + |
| 33 | +public class PatchworkMappingService { |
| 34 | + // We're technically messing with Loader's internal APIs here, if Loader ever gets a better mapping resolution system this class should be refactored. |
| 35 | + private static final TinyTree MAPPINGS = FabricLauncherBase.getLauncher().getMappingConfiguration().getMappings(); |
| 36 | + private static final String INTERMEDIARY = "intermediary"; |
| 37 | + private static final String NAMED = "named"; |
| 38 | + |
| 39 | + private PatchworkMappingService() { |
| 40 | + // NO-OP |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Remaps a name from intermediary to whatever is currently being used at runtime. |
| 45 | + * |
| 46 | + * @param domain The {@link INameMappingService.Domain} to look up. |
| 47 | + * @param name The name to try and remap. |
| 48 | + * @return The remapped name, or the original name if it couldn't be remapped. |
| 49 | + */ |
| 50 | + @Nonnull |
| 51 | + public static String remapName(INameMappingService.Domain domain, String name) { |
| 52 | + if (runtimeNamespaceIsIntermediary()) { |
| 53 | + return name; |
| 54 | + } |
| 55 | + |
| 56 | + if (domain == INameMappingService.Domain.CLASS) { |
| 57 | + return MAPPINGS.getDefaultNamespaceClassMap().get(name).getName(NAMED); |
| 58 | + } |
| 59 | + |
| 60 | + String remappedName; |
| 61 | + |
| 62 | + for (ClassDef classDef : MAPPINGS.getClasses()) { |
| 63 | + remappedName = PatchworkMappingService.remapNameInternal(domain, classDef, name); |
| 64 | + |
| 65 | + if (remappedName != null) { |
| 66 | + return remappedName; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return name; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Like {@link PatchworkMappingService#remapName(INameMappingService.Domain, String)}, but only iterates through members of the target class. |
| 75 | + * @param domain The {@link INameMappingService.Domain} to look up. |
| 76 | + * @param clazz The class that contains the {@code name} to look up. |
| 77 | + * @param name The name to remap. |
| 78 | + * @return The remapped name, or the original name if it couldn't be remapped. |
| 79 | + */ |
| 80 | + @Nonnull |
| 81 | + public static String remapNameFast(INameMappingService.Domain domain, Class<?> clazz, String name) { |
| 82 | + if (runtimeNamespaceIsIntermediary()) { |
| 83 | + return name; |
| 84 | + } |
| 85 | + |
| 86 | + ClassDef classDef = MAPPINGS.getDefaultNamespaceClassMap().get(clazz.getName()); |
| 87 | + String remappedName = remapNameInternal(domain, classDef, name); |
| 88 | + |
| 89 | + return remappedName != null ? remappedName : name; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Like {@link PatchworkMappingService#remapNameFast(INameMappingService.Domain, Class, String)}, but takes a {@link ClassDef} instead of a {@link Class}. |
| 94 | + * @param domain The {@link INameMappingService.Domain} to look up. |
| 95 | + * @param classDef The classDef that contains the {@code name} to look up. |
| 96 | + * @param name The name to remap. |
| 97 | + * @return The remapped name, or null if it couldn't be remapped. |
| 98 | + */ |
| 99 | + @Nullable |
| 100 | + private static String remapNameInternal(INameMappingService.Domain domain, ClassDef classDef, String name) { |
| 101 | + if (runtimeNamespaceIsIntermediary()) { |
| 102 | + return name; |
| 103 | + } |
| 104 | + |
| 105 | + if (domain == INameMappingService.Domain.CLASS) { |
| 106 | + return classDef.getName(name); |
| 107 | + } |
| 108 | + |
| 109 | + boolean domainIsMethod = domain == INameMappingService.Domain.METHOD; |
| 110 | + |
| 111 | + for (Mapped mapped : domainIsMethod ? classDef.getMethods() : classDef.getFields()) { |
| 112 | + if (mapped.getName(INTERMEDIARY).equals(name)) { |
| 113 | + return mapped.getName(NAMED); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + private static boolean runtimeNamespaceIsIntermediary() { |
| 121 | + return FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace().equals(INTERMEDIARY); |
| 122 | + } |
| 123 | +} |
0 commit comments