2014-11-14 12:02:52 +01:00
/ *
* This file is part of Applied Energistics 2 .
* Copyright ( c ) 2013 - 2014 , AlgorithmX2 , All rights reserved .
*
* Applied Energistics 2 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 , either version 3 of the License , or
* ( at your option ) any later version .
*
* Applied Energistics 2 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 Applied Energistics 2 . If not , see < http : //www.gnu.org/licenses/lgpl>.
* /
2014-09-24 02:26:27 +02:00
package appeng.integration ;
2014-11-28 04:36:46 +01:00
2014-09-24 02:26:27 +02:00
import java.lang.reflect.Field ;
2014-11-28 04:36:46 +01:00
import cpw.mods.fml.common.Loader ;
2015-04-13 19:28:53 +02:00
import cpw.mods.fml.common.ModAPIManager ;
2014-11-28 04:36:46 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.exceptions.ModNotInstalled ;
import appeng.core.AEConfig ;
import appeng.core.AELog ;
2015-04-03 08:54:31 +02:00
2015-04-13 19:28:53 +02:00
public final class IntegrationNode
2014-09-24 02:26:27 +02:00
{
2015-10-08 15:42:42 +02:00
private final String displayName ;
private final String modID ;
private final IntegrationType shortName ;
private IntegrationStage state = IntegrationStage . PRE_INIT ;
private IntegrationStage failedStage = IntegrationStage . PRE_INIT ;
private Throwable exception = null ;
private String name = null ;
private Class < ? > classValue = null ;
private Object instance ;
private IIntegrationModule mod = null ;
2014-09-24 02:26:27 +02:00
2015-09-25 23:10:56 +02:00
public IntegrationNode ( final String displayName , final String modID , final IntegrationType shortName , final String name )
2015-04-03 08:54:31 +02:00
{
2014-09-28 11:47:17 +02:00
this . displayName = displayName ;
this . shortName = shortName ;
this . modID = modID ;
this . name = name ;
2014-09-24 02:26:27 +02:00
}
@Override
public String toString ( )
{
2015-10-08 15:42:42 +02:00
return this . getShortName ( ) . name ( ) + ':' + this . getState ( ) . name ( ) ;
2014-09-24 02:26:27 +02:00
}
2015-10-08 15:42:42 +02:00
boolean isActive ( )
2014-09-24 02:26:27 +02:00
{
2015-10-08 15:42:42 +02:00
if ( this . getState ( ) = = IntegrationStage . PRE_INIT )
2015-04-29 02:30:53 +02:00
{
2015-04-13 19:28:53 +02:00
this . call ( IntegrationStage . PRE_INIT ) ;
2015-04-29 02:30:53 +02:00
}
2015-04-03 08:54:31 +02:00
2015-10-08 15:42:42 +02:00
return this . getState ( ) ! = IntegrationStage . FAILED ;
2015-04-03 08:54:31 +02:00
}
2015-09-25 23:10:56 +02:00
void call ( final IntegrationStage stage )
2015-04-03 08:54:31 +02:00
{
2015-10-08 15:42:42 +02:00
if ( this . getState ( ) ! = IntegrationStage . FAILED )
2014-09-24 02:26:27 +02:00
{
2015-10-08 15:42:42 +02:00
if ( this . getState ( ) . ordinal ( ) > stage . ordinal ( ) )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return ;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
try
{
2015-04-03 08:54:31 +02:00
switch ( stage )
2014-09-24 02:26:27 +02:00
{
2015-04-03 08:54:31 +02:00
case PRE_INIT :
2015-04-13 19:28:53 +02:00
final ModAPIManager apiManager = ModAPIManager . INSTANCE ;
boolean enabled = this . modID = = null | | Loader . isModLoaded ( this . modID ) | | apiManager . hasAPI ( this . modID ) ;
2015-04-03 08:54:31 +02:00
AEConfig . instance . addCustomCategoryComment ( " ModIntegration " , " Valid Values are 'AUTO', 'ON', or 'OFF' - defaults to 'AUTO' ; Suggested that you leave this alone unless your experiencing an issue, or wish to disable the integration for a reason. " ) ;
2015-09-25 23:10:56 +02:00
final String mode = AEConfig . instance . get ( " ModIntegration " , this . displayName . replace ( " " , " " ) , " AUTO " ) . getString ( ) ;
2015-04-03 08:54:31 +02:00
2015-04-13 19:28:53 +02:00
if ( mode . toUpperCase ( ) . equals ( " ON " ) )
2015-04-29 02:30:53 +02:00
{
2015-04-03 08:54:31 +02:00
enabled = true ;
2015-04-29 02:30:53 +02:00
}
2015-04-13 19:28:53 +02:00
if ( mode . toUpperCase ( ) . equals ( " OFF " ) )
2015-04-29 02:30:53 +02:00
{
2015-04-03 08:54:31 +02:00
enabled = false ;
2015-04-29 02:30:53 +02:00
}
2015-04-03 08:54:31 +02:00
if ( enabled )
{
this . classValue = this . getClass ( ) . getClassLoader ( ) . loadClass ( this . name ) ;
this . mod = ( IIntegrationModule ) this . classValue . getConstructor ( ) . newInstance ( ) ;
2015-09-25 23:10:56 +02:00
final Field f = this . classValue . getField ( " instance " ) ;
2015-10-08 15:42:42 +02:00
f . set ( this . classValue , this . setInstance ( this . mod ) ) ;
2015-04-03 08:54:31 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
2015-04-03 08:54:31 +02:00
throw new ModNotInstalled ( this . modID ) ;
2015-04-29 02:30:53 +02:00
}
2015-04-03 08:54:31 +02:00
2015-10-08 15:42:42 +02:00
this . setState ( IntegrationStage . INIT ) ;
2015-04-03 08:54:31 +02:00
break ;
case INIT :
this . mod . init ( ) ;
2015-10-08 15:42:42 +02:00
this . setState ( IntegrationStage . POST_INIT ) ;
2015-04-03 08:54:31 +02:00
break ;
case POST_INIT :
this . mod . postInit ( ) ;
2015-10-08 15:42:42 +02:00
this . setState ( IntegrationStage . READY ) ;
2015-04-03 08:54:31 +02:00
break ;
case FAILED :
default :
break ;
2014-09-24 02:26:27 +02:00
}
}
2015-09-25 23:10:56 +02:00
catch ( final Throwable t )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this . failedStage = stage ;
this . exception = t ;
2015-10-08 15:42:42 +02:00
this . setState ( IntegrationStage . FAILED ) ;
2014-09-24 02:26:27 +02:00
}
}
2015-04-03 08:54:31 +02:00
if ( stage = = IntegrationStage . POST_INIT )
2014-09-24 02:26:27 +02:00
{
2015-10-08 15:42:42 +02:00
if ( this . getState ( ) = = IntegrationStage . FAILED )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
AELog . info ( this . displayName + " - Integration Disabled " ) ;
2015-04-03 08:54:31 +02:00
if ( ! ( this . exception instanceof ModNotInstalled ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
AELog . integration ( this . exception ) ;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
else
{
2014-12-29 15:13:47 +01:00
AELog . info ( this . displayName + " - Integration Enable " ) ;
2014-09-24 02:26:27 +02:00
}
}
}
2015-10-08 15:42:42 +02:00
Object getInstance ( )
{
return this . instance ;
}
private Object setInstance ( final Object instance )
{
this . instance = instance ;
return instance ;
}
IntegrationType getShortName ( )
{
return this . shortName ;
}
IntegrationStage getState ( )
{
return this . state ;
}
private void setState ( final IntegrationStage state )
{
this . state = state ;
}
2014-09-24 02:26:27 +02:00
}