3434import org .apache .logging .log4j .Marker ;
3535import org .apache .logging .log4j .MarkerManager ;
3636
37- import net .fabricmc .loader .api .FabricLoader ;
3837import net .fabricmc .loader .launch .common .FabricLauncherBase ;
39- import net .fabricmc .mapping .tree .ClassDef ;
40- import net .fabricmc .mapping .tree .Mapped ;
4138import net .fabricmc .mapping .tree .TinyTree ;
4239
40+ import com .patchworkmc .impl .fml .PatchworkMappingService ;
41+
4342/**
4443 * Some reflection helper code.
4544 * This may not work properly in Java 9 with its new, more restrictive, reflection management.
@@ -54,83 +53,23 @@ public class ObfuscationReflectionHelper {
5453 private static final Logger LOGGER = LogManager .getLogger ();
5554 private static final Marker REFLECTION = MarkerManager .getMarker ("REFLECTION" );
5655 // We're technically messing with Loader's internal APIs here, if Loader ever gets a better mapping resolution system this class should be refactored.
57- private static final TinyTree MAPPINGS = FabricLauncherBase .getLauncher ().getMappingConfiguration ().getMappings ();
58- private static final String INTERMEDIARY = "intermediary" ;
59- private static final String NAMED = "named" ;
56+ public static final TinyTree MAPPINGS = FabricLauncherBase .getLauncher ().getMappingConfiguration ().getMappings ();
57+ public static final String INTERMEDIARY = "intermediary" ;
58+ public static final String NAMED = "named" ;
6059
6160 /**
6261 * Remaps a name from intermediary to whatever is currently being used at runtime.
6362 *
6463 * @param domain The {@link INameMappingService.Domain} to look up.
6564 * @param name The name to try and remap.
6665 * @return The remapped name, or the original name if it couldn't be remapped.
66+ * @deprecated Fabric mods should use {@link PatchworkMappingService#remapName(INameMappingService.Domain, String)} instead.
6767 */
6868 @ Nonnull
69+ @ Deprecated
6970 public static String remapName (INameMappingService .Domain domain , String name ) {
70- if (FabricLoader .getInstance ().getMappingResolver ().getCurrentRuntimeNamespace ().equals (INTERMEDIARY )) {
71- return name ;
72- }
73-
74- if (domain == INameMappingService .Domain .CLASS ) {
75- return MAPPINGS .getDefaultNamespaceClassMap ().get (name ).getName (NAMED );
76- }
77-
78- String remappedName ;
79-
80- for (ClassDef classDef : MAPPINGS .getClasses ()) {
81- remappedName = remapNameInternal (domain , classDef , name );
82- if (remappedName != null ) {
83- return remappedName ;
84- }
85- }
86-
87- return name ;
88- }
89-
90- // Begin Patchwork-added methods
91- /**
92- * Like {@link ObfuscationReflectionHelper#remapName(INameMappingService.Domain, String)}, but only iterates through members of the target class.
93- * @param domain The {@link INameMappingService.Domain} to look up.
94- * @param clazz The class that contains the {@code name} to look up.
95- * @param name The name to remap.
96- * @return The remapped name, or the original name if it couldn't be remapped.
97- */
98- @ Nonnull
99- public static String remapNameFast (INameMappingService .Domain domain , Class <?> clazz , String name ) {
100- ClassDef classDef = MAPPINGS .getDefaultNamespaceClassMap ().get (clazz .getName ());
101- String remappedName = remapNameInternal (domain , classDef , name );
102-
103- return remappedName != null ? remappedName : name ;
104- }
105-
106- /**
107- * Like {@link ObfuscationReflectionHelper#remapNameFast(INameMappingService.Domain, Class, String)}, but takes a {@link ClassDef} instead of a {@link Class}
108- * @param domain The {@link INameMappingService.Domain} to look up.
109- * @param classDef The classDef that contains the {@code name} to look up.
110- * @param name The name to remap.
111- * @return The remapped name, or null if it couldn't be remapped.
112- */
113- @ Nullable
114- public static String remapNameInternal (INameMappingService .Domain domain , ClassDef classDef , String name ) {
115- if (FabricLoader .getInstance ().getMappingResolver ().getCurrentRuntimeNamespace ().equals (INTERMEDIARY )) {
116- return name ;
117- }
118-
119- if (domain == INameMappingService .Domain .CLASS ) {
120- return classDef .getName (name );
121- }
122-
123- boolean domainIsMethod = domain == INameMappingService .Domain .METHOD ;
124-
125- for (Mapped mapped : domainIsMethod ? classDef .getMethods () : classDef .getFields ()) {
126- if (mapped .getName (INTERMEDIARY ).equals (name )) {
127- return mapped .getName (NAMED );
128- }
129- }
130-
131- return null ;
71+ return PatchworkMappingService .remapName (domain , name );
13272 }
133- // End Patchwork-added methods
13473
13574 /**
13675 * Gets the value a field with the specified index in the given class.
@@ -184,11 +123,11 @@ public static <T, E> T getPrivateValue(Class<? super E> classToAccess, E instanc
184123 // We use remapName instead of remapNameFast because the member wasn't found, which means it's probably not in that class.
185124 } catch (UnableToFindFieldException e ) {
186125 LOGGER .error (REFLECTION , "Unable to locate field {} ({}) on type {}" , fieldName ,
187- remapName (INameMappingService .Domain .FIELD , fieldName ), classToAccess .getName (), e );
126+ PatchworkMappingService . remapName (INameMappingService .Domain .FIELD , fieldName ), classToAccess .getName (), e );
188127 throw e ;
189128 } catch (IllegalAccessException e ) {
190129 LOGGER .error (REFLECTION , "Unable to access field {} ({}) on type {}" , fieldName ,
191- remapName (INameMappingService .Domain .FIELD , fieldName ), classToAccess .getName (), e );
130+ PatchworkMappingService . remapName (INameMappingService .Domain .FIELD , fieldName ), classToAccess .getName (), e );
192131 throw new UnableToAccessFieldException (e );
193132 }
194133 }
@@ -271,7 +210,7 @@ public static Method findMethod(@Nonnull final Class<?> clazz, @Nonnull final St
271210 Preconditions .checkNotNull (parameterTypes , "Parameter types of method to find cannot be null." );
272211
273212 try {
274- String name = remapNameFast (INameMappingService .Domain .METHOD , clazz , methodName );
213+ String name = PatchworkMappingService . remapNameFast (INameMappingService .Domain .METHOD , clazz , methodName );
275214 Method method = clazz .getDeclaredMethod (name , parameterTypes );
276215 method .setAccessible (true );
277216 return method ;
@@ -341,7 +280,7 @@ public static <T> Field findField(@Nonnull final Class<? super T> clazz, @Nonnul
341280 Preconditions .checkArgument (!fieldName .isEmpty (), "Name of field to find cannot be empty." );
342281
343282 try {
344- String name = remapNameFast (INameMappingService .Domain .FIELD , clazz , fieldName );
283+ String name = PatchworkMappingService . remapNameFast (INameMappingService .Domain .FIELD , clazz , fieldName );
345284 Field field = clazz .getDeclaredField (name );
346285 field .setAccessible (true );
347286 return field ;
0 commit comments