2012-07-22 14:26:38 +00:00
|
|
|
/*
|
2016-06-23 03:49:47 +00:00
|
|
|
* Minecraft Forge
|
|
|
|
* Copyright (c) 2016.
|
2012-07-22 14:26:38 +00:00
|
|
|
*
|
2016-06-23 03:49:47 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
* of the License.
|
2012-07-22 14:26:38 +00:00
|
|
|
*
|
2016-06-23 03:49:47 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2012-07-22 14:26:38 +00:00
|
|
|
*
|
2016-06-23 03:49:47 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2012-07-22 14:26:38 +00:00
|
|
|
*/
|
2014-09-23 05:01:24 +00:00
|
|
|
package net.minecraftforge.fml.common;
|
2012-07-22 14:26:38 +00:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
2014-09-23 05:01:24 +00:00
|
|
|
import net.minecraftforge.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper;
|
|
|
|
import net.minecraftforge.fml.relauncher.ReflectionHelper;
|
|
|
|
import net.minecraftforge.fml.relauncher.ReflectionHelper.UnableToAccessFieldException;
|
|
|
|
import net.minecraftforge.fml.relauncher.ReflectionHelper.UnableToFindFieldException;
|
|
|
|
|
|
|
|
import org.apache.logging.log4j.Level;
|
2012-07-22 14:26:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Some reflection helper code.
|
2012-07-31 02:31:07 +00:00
|
|
|
*
|
2012-07-22 14:26:38 +00:00
|
|
|
* @author cpw
|
2012-07-31 02:31:07 +00:00
|
|
|
*
|
2012-07-22 14:26:38 +00:00
|
|
|
*/
|
|
|
|
public class ObfuscationReflectionHelper
|
|
|
|
{
|
|
|
|
public static <T, E> T getPrivateValue(Class<? super E> classToAccess, E instance, int fieldIndex)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return ReflectionHelper.getPrivateValue(classToAccess, instance, fieldIndex);
|
|
|
|
}
|
|
|
|
catch (UnableToAccessFieldException e)
|
|
|
|
{
|
2013-12-16 16:47:48 +00:00
|
|
|
FMLLog.log(Level.ERROR, e, "There was a problem getting field index %d from %s", fieldIndex, classToAccess.getName());
|
2012-07-22 14:26:38 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-09 22:36:31 +00:00
|
|
|
public static String[] remapFieldNames(String className, String... fieldNames)
|
|
|
|
{
|
2013-03-09 22:58:18 +00:00
|
|
|
String internalClassName = FMLDeobfuscatingRemapper.INSTANCE.unmap(className.replace('.', '/'));
|
2013-03-09 22:36:31 +00:00
|
|
|
String[] mappedNames = new String[fieldNames.length];
|
|
|
|
int i = 0;
|
|
|
|
for (String fName : fieldNames)
|
|
|
|
{
|
|
|
|
mappedNames[i++] = FMLDeobfuscatingRemapper.INSTANCE.mapFieldName(internalClassName, fName, null);
|
|
|
|
}
|
|
|
|
return mappedNames;
|
|
|
|
}
|
2013-11-10 16:49:06 +00:00
|
|
|
|
2012-07-22 14:26:38 +00:00
|
|
|
public static <T, E> T getPrivateValue(Class<? super E> classToAccess, E instance, String... fieldNames)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-03-09 22:36:31 +00:00
|
|
|
return ReflectionHelper.getPrivateValue(classToAccess, instance, remapFieldNames(classToAccess.getName(),fieldNames));
|
2012-07-22 14:26:38 +00:00
|
|
|
}
|
|
|
|
catch (UnableToFindFieldException e)
|
|
|
|
{
|
2013-12-16 16:47:48 +00:00
|
|
|
FMLLog.log(Level.ERROR,e,"Unable to locate any field %s on type %s", Arrays.toString(fieldNames), classToAccess.getName());
|
2012-07-22 14:26:38 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
catch (UnableToAccessFieldException e)
|
|
|
|
{
|
2013-12-16 16:47:48 +00:00
|
|
|
FMLLog.log(Level.ERROR, e, "Unable to access any field %s on type %s", Arrays.toString(fieldNames), classToAccess.getName());
|
2012-07-22 14:26:38 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, E value, int fieldIndex)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ReflectionHelper.setPrivateValue(classToAccess, instance, value, fieldIndex);
|
|
|
|
}
|
|
|
|
catch (UnableToAccessFieldException e)
|
|
|
|
{
|
2013-12-16 16:47:48 +00:00
|
|
|
FMLLog.log(Level.ERROR, e, "There was a problem setting field index %d on type %s", fieldIndex, classToAccess.getName());
|
2012-07-22 14:26:38 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, E value, String... fieldNames)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-03-09 22:36:31 +00:00
|
|
|
ReflectionHelper.setPrivateValue(classToAccess, instance, value, remapFieldNames(classToAccess.getName(), fieldNames));
|
2012-07-22 14:26:38 +00:00
|
|
|
}
|
|
|
|
catch (UnableToFindFieldException e)
|
|
|
|
{
|
2013-12-16 16:47:48 +00:00
|
|
|
FMLLog.log(Level.ERROR, e, "Unable to locate any field %s on type %s", Arrays.toString(fieldNames), classToAccess.getName());
|
2012-07-22 14:26:38 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
catch (UnableToAccessFieldException e)
|
|
|
|
{
|
2013-12-16 16:47:48 +00:00
|
|
|
FMLLog.log(Level.ERROR, e, "Unable to set any field %s on type %s", Arrays.toString(fieldNames), classToAccess.getName());
|
2012-07-22 14:26:38 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|