2018-09-21 02:17:20 -07:00
|
|
|
/*
|
|
|
|
|
* Minecraft Forge
|
|
|
|
|
* Copyright (c) 2016-2018.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2018-08-29 21:30:15 -04:00
|
|
|
package net.minecraftforge.fml;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2019-01-17 13:11:54 -05:00
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
import sun.misc.Unsafe;
|
2018-08-29 21:30:15 -04:00
|
|
|
|
2019-01-17 13:11:54 -05:00
|
|
|
@SuppressWarnings("restriction")
|
2018-08-29 21:30:15 -04:00
|
|
|
public class UnsafeHacks
|
|
|
|
|
{
|
|
|
|
|
private static final Unsafe UNSAFE;
|
2019-01-17 13:11:54 -05:00
|
|
|
static
|
|
|
|
|
{
|
2018-08-29 21:30:15 -04:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
|
|
|
|
theUnsafe.setAccessible(true);
|
|
|
|
|
UNSAFE = (Unsafe)theUnsafe.get(null);
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalAccessException | NoSuchFieldException e)
|
|
|
|
|
{
|
|
|
|
|
throw new RuntimeException("BARF!", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 20:23:45 -04:00
|
|
|
@SuppressWarnings("unchecked")
|
2019-01-17 13:11:54 -05:00
|
|
|
public static <T> T newInstance(Class<T> clazz)
|
2018-09-04 20:23:45 -04:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-01-17 13:11:54 -05:00
|
|
|
return (T) UNSAFE.allocateInstance(clazz);
|
2018-09-04 20:23:45 -04:00
|
|
|
}
|
|
|
|
|
catch (InstantiationException e)
|
|
|
|
|
{
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 21:30:15 -04:00
|
|
|
@SuppressWarnings("unchecked")
|
2019-01-17 13:11:54 -05:00
|
|
|
public static <T> T getField(Field field, Object object)
|
|
|
|
|
{
|
2018-08-29 21:30:15 -04:00
|
|
|
final long l = UNSAFE.objectFieldOffset(field);
|
|
|
|
|
return (T) UNSAFE.getObject(object, l);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 13:11:54 -05:00
|
|
|
public static void setField(Field data, Object object, Object value)
|
2018-09-04 20:23:45 -04:00
|
|
|
{
|
|
|
|
|
long offset = UNSAFE.objectFieldOffset(data);
|
2019-01-17 13:11:54 -05:00
|
|
|
UNSAFE.putObject(object, offset, value);
|
2018-09-04 20:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int getIntField(Field f, Object obj)
|
|
|
|
|
{
|
|
|
|
|
long offset = UNSAFE.objectFieldOffset(f);
|
|
|
|
|
return UNSAFE.getInt(obj, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setIntField(Field data, Object object, int value)
|
|
|
|
|
{
|
|
|
|
|
long offset = UNSAFE.objectFieldOffset(data);
|
|
|
|
|
UNSAFE.putInt(object, offset, value);
|
2018-08-29 21:30:15 -04:00
|
|
|
}
|
2019-01-17 13:11:54 -05:00
|
|
|
|
|
|
|
|
// Make sure we don't crash if any future versions change field names
|
|
|
|
|
private static Optional<Field> findField(Class<?> clazz, String name)
|
|
|
|
|
{
|
|
|
|
|
for (Field f : clazz.getDeclaredFields())
|
|
|
|
|
{
|
|
|
|
|
if (f.getName().equals(name))
|
|
|
|
|
{
|
|
|
|
|
return Optional.of(f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void cleanEnumCache(Class<? extends Enum<?>> enumClass) throws Exception
|
|
|
|
|
{
|
|
|
|
|
findField(Class.class, "enumConstantDirectory").ifPresent(f -> setField(f, enumClass, null));
|
|
|
|
|
findField(Class.class, "enumConstants").ifPresent(f -> setField(f, enumClass, null));
|
|
|
|
|
}
|
2018-08-29 21:30:15 -04:00
|
|
|
}
|