Move unsafehacks to separate JAR built separately..

Signed-off-by: cpw <cpw+github@weeksfamily.ca>
This commit is contained in:
cpw 2019-02-18 15:54:14 -05:00
parent 60315d079b
commit 1d6536a53b
No known key found for this signature in database
GPG Key ID: 8EB3DF749553B1B7
4 changed files with 3 additions and 105 deletions

View File

@ -293,6 +293,7 @@ project(':forge') {
installer 'net.minecraftforge:eventbus:0.7.+:service'
installer 'net.minecraftforge:forgespi:0.6.+'
installer 'net.minecraftforge:coremods:0.3.+'
installer 'net.minecraftforge:unsafe:0.2.+'
installer 'com.electronwill.night-config:core:3.4.2'
installer 'com.electronwill.night-config:toml:3.4.2'
installer 'org.jline:jline:3.9.0'

View File

@ -1,101 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* 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
*/
package net.minecraftforge.fml;
import java.lang.reflect.Field;
import java.util.Optional;
import sun.misc.Unsafe;
@SuppressWarnings("restriction")
public class UnsafeHacks
{
private static final Unsafe UNSAFE;
static
{
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);
}
}
@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> clazz)
{
try
{
return (T) UNSAFE.allocateInstance(clazz);
}
catch (InstantiationException e)
{
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public static <T> T getField(Field field, Object object)
{
final long l = UNSAFE.objectFieldOffset(field);
return (T) UNSAFE.getObject(object, l);
}
public static void setField(Field data, Object object, Object value)
{
long offset = UNSAFE.objectFieldOffset(data);
UNSAFE.putObject(object, offset, value);
}
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);
}
// 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));
}
}

View File

@ -19,7 +19,6 @@
package net.minecraftforge.fml.network;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceArrayMap;
import net.minecraft.network.Packet;
import net.minecraft.network.PacketBuffer;
@ -28,14 +27,13 @@ import net.minecraft.network.login.server.SPacketCustomPayloadLogin;
import net.minecraft.network.play.client.CPacketCustomPayload;
import net.minecraft.network.play.server.SPacketCustomPayload;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.UnsafeHacks;
import net.minecraftforge.fml.unsafe.UnsafeHacks;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

View File

@ -28,7 +28,7 @@ import net.minecraft.network.play.client.CPacketCustomPayload;
import net.minecraft.network.play.server.SPacketCustomPayload;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.UnsafeHacks;
import net.minecraftforge.fml.unsafe.UnsafeHacks;
import org.apache.commons.lang3.tuple.Pair;
import java.util.function.BiFunction;