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

Commit e09b0ed

Browse files
committed
Move helper methods to PatchworkMappingService
1 parent a83665b commit e09b0ed

2 files changed

Lines changed: 124 additions & 73 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 static net.minecraftforge.fml.common.ObfuscationReflectionHelper.INTERMEDIARY;
23+
import static net.minecraftforge.fml.common.ObfuscationReflectionHelper.MAPPINGS;
24+
import static net.minecraftforge.fml.common.ObfuscationReflectionHelper.NAMED;
25+
26+
import javax.annotation.Nonnull;
27+
import javax.annotation.Nullable;
28+
29+
import cpw.mods.modlauncher.api.INameMappingService;
30+
31+
import net.fabricmc.loader.api.FabricLoader;
32+
import net.fabricmc.mapping.tree.ClassDef;
33+
import net.fabricmc.mapping.tree.Mapped;
34+
35+
public class PatchworkMappingService {
36+
private PatchworkMappingService() {
37+
// NO-OP
38+
}
39+
40+
/**
41+
* Remaps a name from intermediary to whatever is currently being used at runtime.
42+
*
43+
* @param domain The {@link INameMappingService.Domain} to look up.
44+
* @param name The name to try and remap.
45+
* @return The remapped name, or the original name if it couldn't be remapped.
46+
*/
47+
@Nonnull
48+
public static String remapName(INameMappingService.Domain domain, String name) {
49+
if (FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace().equals(INTERMEDIARY)) {
50+
return name;
51+
}
52+
53+
if (domain == INameMappingService.Domain.CLASS) {
54+
return MAPPINGS.getDefaultNamespaceClassMap().get(name).getName(NAMED);
55+
}
56+
57+
String remappedName;
58+
59+
for (ClassDef classDef : MAPPINGS.getClasses()) {
60+
remappedName = PatchworkMappingService.remapNameInternal(domain, classDef, name);
61+
62+
if (remappedName != null) {
63+
return remappedName;
64+
}
65+
}
66+
67+
return name;
68+
}
69+
70+
/**
71+
* Like {@link PatchworkMappingService#remapName(INameMappingService.Domain, String)}, but only iterates through members of the target class.
72+
* @param domain The {@link INameMappingService.Domain} to look up.
73+
* @param clazz The class that contains the {@code name} to look up.
74+
* @param name The name to remap.
75+
* @return The remapped name, or the original name if it couldn't be remapped.
76+
*/
77+
@Nonnull
78+
public static String remapNameFast(INameMappingService.Domain domain, Class<?> clazz, String name) {
79+
ClassDef classDef = MAPPINGS.getDefaultNamespaceClassMap().get(clazz.getName());
80+
String remappedName = remapNameInternal(domain, classDef, name);
81+
82+
return remappedName != null ? remappedName : name;
83+
}
84+
85+
/**
86+
* Like {@link PatchworkMappingService#remapNameFast(INameMappingService.Domain, Class, String)}, but takes a {@link ClassDef} instead of a {@link Class}.
87+
* @param domain The {@link INameMappingService.Domain} to look up.
88+
* @param classDef The classDef that contains the {@code name} to look up.
89+
* @param name The name to remap.
90+
* @return The remapped name, or null if it couldn't be remapped.
91+
*/
92+
@Nullable
93+
public static String remapNameInternal(INameMappingService.Domain domain, ClassDef classDef, String name) {
94+
if (FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace().equals(INTERMEDIARY)) {
95+
return name;
96+
}
97+
98+
if (domain == INameMappingService.Domain.CLASS) {
99+
return classDef.getName(name);
100+
}
101+
102+
boolean domainIsMethod = domain == INameMappingService.Domain.METHOD;
103+
104+
for (Mapped mapped : domainIsMethod ? classDef.getMethods() : classDef.getFields()) {
105+
if (mapped.getName(INTERMEDIARY).equals(name)) {
106+
return mapped.getName(NAMED);
107+
}
108+
}
109+
110+
return null;
111+
}
112+
}

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

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@
3434
import org.apache.logging.log4j.Marker;
3535
import org.apache.logging.log4j.MarkerManager;
3636

37-
import net.fabricmc.loader.api.FabricLoader;
3837
import net.fabricmc.loader.launch.common.FabricLauncherBase;
39-
import net.fabricmc.mapping.tree.ClassDef;
40-
import net.fabricmc.mapping.tree.Mapped;
4138
import 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

Comments
 (0)