SidedProxy should work again - anywhere. Make sure it's on a static field though

This commit is contained in:
Christian 2012-08-10 17:42:43 -04:00
parent 7b2d09844c
commit dd319a2938
6 changed files with 82 additions and 48 deletions

View File

@ -267,10 +267,12 @@ public class FMLModContainer implements ModContainer
modClassLoader.addFile(source);
Class<?> clazz = Class.forName(className, true, modClassLoader);
ASMDataTable asmHarvestedAnnotations = event.getASMHarvestedData();
// TODO
asmHarvestedAnnotations.getAnnotationsFor(this);
annotations = gatherAnnotations(clazz);
isNetworkMod = FMLNetworkHandler.instance().registerNetworkMod(this, clazz, event.getASMHarvestedData());
modInstance = clazz.newInstance();
ProxyInjector.inject(this, event.getASMHarvestedData(), FMLCommonHandler.instance().getSide());
processFieldAnnotations();
}
catch (Throwable e)

View File

@ -370,7 +370,7 @@ public class Loader
modController.transition(LoaderState.CONSTRUCTING);
modController.distributeStateMessage(LoaderState.CONSTRUCTING, modClassLoader, disc.getASMTable());
modController.transition(LoaderState.PREINITIALIZATION);
modController.distributeStateMessage(LoaderState.PREINITIALIZATION, disc.getASMTable());
modController.distributeStateMessage(LoaderState.PREINITIALIZATION, disc.getASMTable(), canonicalConfigDir);
modController.transition(LoaderState.INITIALIZATION);
}

View File

@ -119,7 +119,13 @@ public @interface Mod
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Instance {}
public @interface Instance {
/**
* The mod object to inject into this field
* @return
*/
String value() default "";
}
/**
* Populate the annotated field with the mod's metadata.
* @author cpw
@ -127,7 +133,13 @@ public @interface Mod
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Metadata {}
public @interface Metadata {
/**
* The mod id specifying the metadata to load here
* @return
*/
String value() default "";
}
/**
* Populate the annotated field with an instance of the Block as specified
* @author cpw

View File

@ -15,6 +15,14 @@
package cpw.mods.fml.common;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Set;
import java.util.logging.Level;
import org.objectweb.asm.Type;
import cpw.mods.fml.common.discovery.ASMDataTable;
import cpw.mods.fml.common.discovery.ASMDataTable.ASMData;
/**
* @author cpw
@ -22,55 +30,45 @@ import java.lang.reflect.Field;
*/
public class ProxyInjector
{
private String clientName;
private String serverName;
private String bukkitName;
private Field target;
public ProxyInjector(String clientName, String serverName, String bukkitName, Field target)
public static void inject(ModContainer mod, ASMDataTable data, Side side)
{
this.clientName = clientName;
this.serverName = serverName;
this.bukkitName = bukkitName;
this.target = target;
}
FMLLog.fine("Attempting to inject @SidedProxy classes into %s", mod.getModId());
Set<ASMData> targets = data.getAnnotationsFor(mod).get(Type.getDescriptor(SidedProxy.class));
ClassLoader mcl = Loader.instance().getModClassLoader();
public boolean isValidFor(Side type)
{
if (type == Side.CLIENT)
for (ASMData targ : targets)
{
return !this.clientName.isEmpty();
}
else if (type == Side.SERVER)
{
return !this.serverName.isEmpty();
}
else if (type == Side.BUKKIT)
{
return !this.bukkitName.isEmpty();
}
return false;
}
public void inject(ModContainer mod, Side side)
{
String targetType = side == Side.CLIENT ? clientName : serverName;
try
{
Object proxy=Class.forName(targetType, false, Loader.instance().getModClassLoader()).newInstance();
if (target.getType().isAssignableFrom(proxy.getClass()))
try
{
target.set(mod.getMod(), proxy);
} else {
FMLCommonHandler.instance().getFMLLogger().severe(String.format("Attempted to load a proxy type %s into %s, but the types don't match", targetType, target.getName()));
throw new LoaderException();
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());
throw new LoaderException();
}
String targetType = side.isClient() ? target.getAnnotation(SidedProxy.class).clientSide() : target.getAnnotation(SidedProxy.class).serverSide();
Object proxy=Class.forName(targetType, true, mcl).newInstance();
if ((target.getModifiers() & Modifier.STATIC) != 0 )
{
FMLLog.severe("Attempted to load a proxy type %s into %s.%s, but the field is not static", targetType, targ.getClassName(), targ.getObjectName());
throw new LoaderException();
}
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());
throw new LoaderException();
}
target.set(null, proxy);
}
catch (Exception e)
{
FMLLog.log(Level.SEVERE, e, "An error occured trying to load a proxy into %s.%s", targ.getAnnotationInfo(), targ.getClassName(), targ.getObjectName());
throw new LoaderException(e);
}
}
catch (Exception e)
{
FMLCommonHandler.instance().getFMLLogger().severe(String.format("An error occured trying to load a proxy type %s into %s", targetType, target.getName()));
FMLCommonHandler.instance().getFMLLogger().throwing("ProxyInjector", "inject", e);
throw new LoaderException(e);
}
}
}

View File

@ -5,6 +5,7 @@ import java.io.File;
import com.google.common.eventbus.EventBus;
import cpw.mods.fml.common.LoaderState.ModState;
import cpw.mods.fml.common.discovery.ASMDataTable;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.ModMetadata;
@ -12,10 +13,15 @@ public class FMLPreInitializationEvent extends FMLStateEvent
{
private ModMetadata modMetadata;
private File sourceFile;
private File configurationDir;
private File suggestedConfigFile;
private ASMDataTable asmData;
public FMLPreInitializationEvent(Object... data)
{
super(data);
this.asmData = (ASMDataTable)data[0];
this.configurationDir = (File)data[1];
}
@Override
@ -29,6 +35,7 @@ public class FMLPreInitializationEvent extends FMLStateEvent
{
this.modMetadata = activeContainer.getMetadata();
this.sourceFile = activeContainer.getSource();
this.suggestedConfigFile = new File(configurationDir, activeContainer.getModId()+".cfg");
}
public File getSourceFile()
@ -40,4 +47,19 @@ public class FMLPreInitializationEvent extends FMLStateEvent
{
return modMetadata;
}
public File getModConfigurationDirectory()
{
return configurationDir;
}
public File getSuggestedConfigurationFile()
{
return suggestedConfigFile;
}
public ASMDataTable getAsmData()
{
return asmData;
}
}

View File

@ -78,7 +78,6 @@ import cpw.mods.fml.common.TickType;
public class ModLoaderModContainer implements ModContainer
{
private static final ProxyInjector NULLPROXY = new ProxyInjector("","","",null);
public BaseModProxy mod;
private File modSource;
public List<ArtifactVersion> requirements = Lists.newArrayList();
@ -498,6 +497,7 @@ public class ModLoaderModContainer implements ModContainer
configureMod(modClazz, event.getASMHarvestedData());
isNetworkMod = FMLNetworkHandler.instance().registerNetworkMod(this, modClazz, event.getASMHarvestedData());
mod = (BaseModProxy)modClazz.newInstance();
ProxyInjector.inject(this, event.getASMHarvestedData(), FMLCommonHandler.instance().getSide());
}
catch (Exception e)
{