2012-06-02 19:13:55 +00:00
/ *
2013-03-08 00:25:48 +00:00
* Forge Mod Loader
* Copyright ( c ) 2012 - 2013 cpw .
* All rights reserved . This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2 . 1
* which accompanies this distribution , and is available at
* http : //www.gnu.org/licenses/old-licenses/gpl-2.0.html
2013-04-21 03:31:22 +00:00
*
2013-03-08 00:25:48 +00:00
* Contributors :
* cpw - implementation
2012-06-02 19:13:55 +00:00
* /
2014-09-23 05:01:24 +00:00
package net.minecraftforge.fml.common ;
2012-06-02 19:13:55 +00:00
import java.lang.reflect.Field ;
2012-08-10 21:42:43 +00:00
import java.lang.reflect.Modifier ;
import java.util.Set ;
2014-09-23 05:01:24 +00:00
import net.minecraftforge.fml.common.discovery.ASMDataTable ;
import net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData ;
import net.minecraftforge.fml.relauncher.Side ;
2013-12-16 16:47:48 +00:00
import org.apache.logging.log4j.Level ;
2012-08-10 21:42:43 +00:00
2013-06-06 14:02:48 +00:00
import com.google.common.base.Strings ;
2012-06-02 19:13:55 +00:00
/ * *
* @author cpw
*
* /
public class ProxyInjector
{
2013-04-21 03:31:22 +00:00
public static void inject ( ModContainer mod , ASMDataTable data , Side side , ILanguageAdapter languageAdapter )
2012-06-02 19:13:55 +00:00
{
2012-08-10 21:42:43 +00:00
FMLLog . fine ( " Attempting to inject @SidedProxy classes into %s " , mod . getModId ( ) ) ;
2012-08-11 06:24:29 +00:00
Set < ASMData > targets = data . getAnnotationsFor ( mod ) . get ( SidedProxy . class . getName ( ) ) ;
2012-08-10 21:42:43 +00:00
ClassLoader mcl = Loader . instance ( ) . getModClassLoader ( ) ;
2012-07-02 16:32:41 +00:00
2012-08-10 21:42:43 +00:00
for ( ASMData targ : targets )
2012-06-02 19:13:55 +00:00
{
2012-08-10 21:42:43 +00:00
try
{
Class < ? > proxyTarget = Class . forName ( targ . getClassName ( ) , true , mcl ) ;
Field target = proxyTarget . getDeclaredField ( targ . getObjectName ( ) ) ;
if ( target = = null )
{
// Impossible?
FMLLog . severe ( " Attempted to load a proxy type into %s.%s but the field was not found " , targ . getClassName ( ) , targ . getObjectName ( ) ) ;
2015-03-27 08:17:57 +00:00
throw new LoaderException ( String . format ( " Attempted to load a proxy type into %s.%s but the field was not found " , targ . getClassName ( ) , targ . getObjectName ( ) ) ) ;
2012-08-10 21:42:43 +00:00
}
2014-01-20 12:55:26 +00:00
target . setAccessible ( true ) ;
2012-07-02 16:32:41 +00:00
2013-06-06 14:02:48 +00:00
SidedProxy annotation = target . getAnnotation ( SidedProxy . class ) ;
if ( ! Strings . isNullOrEmpty ( annotation . modId ( ) ) & & ! annotation . modId ( ) . equals ( mod . getModId ( ) ) )
{
FMLLog . fine ( " Skipping proxy injection for %s.%s since it is not for mod %s " , targ . getClassName ( ) , targ . getObjectName ( ) , mod . getModId ( ) ) ;
continue ;
}
String targetType = side . isClient ( ) ? annotation . clientSide ( ) : annotation . serverSide ( ) ;
2016-01-02 06:59:14 +00:00
if ( targetType . equals ( " " ) )
{
targetType = targ . getClassName ( ) + ( side . isClient ( ) ? " $ClientProxy " : " $ServerProxy " ) ;
}
2012-08-10 21:42:43 +00:00
Object proxy = Class . forName ( targetType , true , mcl ) . newInstance ( ) ;
2013-04-21 03:31:22 +00:00
if ( languageAdapter . supportsStatics ( ) & & ( target . getModifiers ( ) & Modifier . STATIC ) = = 0 )
2012-08-10 21:42:43 +00:00
{
FMLLog . severe ( " Attempted to load a proxy type %s into %s.%s, but the field is not static " , targetType , targ . getClassName ( ) , targ . getObjectName ( ) ) ;
2015-03-27 08:17:57 +00:00
throw new LoaderException ( String . format ( " Attempted to load a proxy type %s into %s.%s, but the field is not static " , targetType , targ . getClassName ( ) , targ . getObjectName ( ) ) ) ;
2012-08-10 21:42:43 +00:00
}
if ( ! target . getType ( ) . isAssignableFrom ( proxy . getClass ( ) ) )
{
FMLLog . severe ( " Attempted to load a proxy type %s into %s.%s, but the types don't match " , targetType , targ . getClassName ( ) , targ . getObjectName ( ) ) ;
2015-03-27 08:17:57 +00:00
throw new LoaderException ( String . format ( " Attempted to load a proxy type %s into %s.%s, but the types don't match " , targetType , targ . getClassName ( ) , targ . getObjectName ( ) ) ) ;
2012-08-10 21:42:43 +00:00
}
2013-04-21 03:31:22 +00:00
languageAdapter . setProxy ( target , proxyTarget , proxy ) ;
2012-08-10 21:42:43 +00:00
}
catch ( Exception e )
2012-06-02 19:13:55 +00:00
{
2013-12-16 16:47:48 +00:00
FMLLog . log ( Level . ERROR , e , " An error occured trying to load a proxy into %s.%s " , targ . getAnnotationInfo ( ) , targ . getClassName ( ) , targ . getObjectName ( ) ) ;
2012-08-10 21:42:43 +00:00
throw new LoaderException ( e ) ;
2012-06-02 19:13:55 +00:00
}
}
2013-05-08 17:04:35 +00:00
// Allow language specific proxy injection.
languageAdapter . setInternalProxies ( mod , side , mcl ) ;
2012-06-02 19:13:55 +00:00
}
}