Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit a83665b

Browse files
committed
Deduplicate code + add some javadoc formatting
1 parent b2e0406 commit a83665b

1 file changed

Lines changed: 30 additions & 16 deletions

File tree

patchwork-fml/src/main/java/net/minecraftforge/fml/common/ObfuscationReflectionHelper.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public class ObfuscationReflectionHelper {
6161
/**
6262
* Remaps a name from intermediary to whatever is currently being used at runtime.
6363
*
64-
* @param domain The {@link INameMappingService.Domain} to use to remap the name.
65-
* @param name The name to try and remap.
64+
* @param domain The {@link INameMappingService.Domain} to look up.
65+
* @param name The name to try and remap.
6666
* @return The remapped name, or the original name if it couldn't be remapped.
6767
*/
6868
@Nonnull
@@ -75,37 +75,51 @@ public static String remapName(INameMappingService.Domain domain, String name) {
7575
return MAPPINGS.getDefaultNamespaceClassMap().get(name).getName(NAMED);
7676
}
7777

78-
for (ClassDef classDef : MAPPINGS.getClasses()) {
79-
boolean domainIsMethod = domain == INameMappingService.Domain.METHOD;
78+
String remappedName;
8079

81-
for (Mapped mapped : domainIsMethod ? classDef.getMethods() : classDef.getFields()) {
82-
if (mapped.getName(INTERMEDIARY).equals(name)) {
83-
return mapped.getName(NAMED);
84-
}
80+
for (ClassDef classDef : MAPPINGS.getClasses()) {
81+
remappedName = remapNameInternal(domain, classDef, name);
82+
if(remappedName != null) {
83+
return remappedName;
8584
}
8685
}
8786

88-
// It couldn't be found.
8987
return name;
9088
}
9189

90+
// Begin Patchwork-added methods
9291
/**
9392
* Like {@link ObfuscationReflectionHelper#remapName(INameMappingService.Domain, String)}, but only iterates through members of the target class.
94-
* @param domain The {@link INameMappingService.Domain} to look up.
95-
* @param clazz The class that contains the {@code name} to look up.
96-
* @param name The name to remap.
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.
9796
* @return The remapped name, or the original name if it couldn't be remapped.
9897
*/
98+
@Nonnull
9999
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) {
100115
if (FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace().equals(INTERMEDIARY)) {
101116
return name;
102117
}
103118

104119
if (domain == INameMappingService.Domain.CLASS) {
105-
remapName(domain, name);
120+
return classDef.getName(name);
106121
}
107122

108-
ClassDef classDef = MAPPINGS.getDefaultNamespaceClassMap().get(clazz.getName());
109123
boolean domainIsMethod = domain == INameMappingService.Domain.METHOD;
110124

111125
for (Mapped mapped : domainIsMethod ? classDef.getMethods() : classDef.getFields()) {
@@ -114,9 +128,9 @@ public static String remapNameFast(INameMappingService.Domain domain, Class<?> c
114128
}
115129
}
116130

117-
// It couldn't be found.
118-
return name;
131+
return null;
119132
}
133+
// End Patchwork-added methods
120134

121135
/**
122136
* Gets the value a field with the specified index in the given class.

0 commit comments

Comments
 (0)