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); modClassLoader.addFile(source);
Class<?> clazz = Class.forName(className, true, modClassLoader); Class<?> clazz = Class.forName(className, true, modClassLoader);
ASMDataTable asmHarvestedAnnotations = event.getASMHarvestedData(); ASMDataTable asmHarvestedAnnotations = event.getASMHarvestedData();
// TODO
asmHarvestedAnnotations.getAnnotationsFor(this); asmHarvestedAnnotations.getAnnotationsFor(this);
annotations = gatherAnnotations(clazz); annotations = gatherAnnotations(clazz);
isNetworkMod = FMLNetworkHandler.instance().registerNetworkMod(this, clazz, event.getASMHarvestedData()); isNetworkMod = FMLNetworkHandler.instance().registerNetworkMod(this, clazz, event.getASMHarvestedData());
modInstance = clazz.newInstance(); modInstance = clazz.newInstance();
ProxyInjector.inject(this, event.getASMHarvestedData(), FMLCommonHandler.instance().getSide());
processFieldAnnotations(); processFieldAnnotations();
} }
catch (Throwable e) catch (Throwable e)

View File

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

View File

@ -119,7 +119,13 @@ public @interface Mod
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) @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. * Populate the annotated field with the mod's metadata.
* @author cpw * @author cpw
@ -127,7 +133,13 @@ public @interface Mod
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) @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 * Populate the annotated field with an instance of the Block as specified
* @author cpw * @author cpw

View File

@ -15,6 +15,14 @@
package cpw.mods.fml.common; package cpw.mods.fml.common;
import java.lang.reflect.Field; 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 * @author cpw
@ -22,55 +30,45 @@ import java.lang.reflect.Field;
*/ */
public class ProxyInjector public class ProxyInjector
{ {
private String clientName; public static void inject(ModContainer mod, ASMDataTable data, Side side)
private String serverName;
private String bukkitName;
private Field target;
public ProxyInjector(String clientName, String serverName, String bukkitName, Field target)
{ {
this.clientName = clientName; FMLLog.fine("Attempting to inject @SidedProxy classes into %s", mod.getModId());
this.serverName = serverName; Set<ASMData> targets = data.getAnnotationsFor(mod).get(Type.getDescriptor(SidedProxy.class));
this.bukkitName = bukkitName; ClassLoader mcl = Loader.instance().getModClassLoader();
this.target = target;
}
public boolean isValidFor(Side type) for (ASMData targ : targets)
{
if (type == Side.CLIENT)
{ {
return !this.clientName.isEmpty(); try
}
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()))
{ {
target.set(mod.getMod(), proxy); Class<?> proxyTarget = Class.forName(targ.getClassName(), true, mcl);
} else { Field target = proxyTarget.getDeclaredField(targ.getObjectName());
FMLCommonHandler.instance().getFMLLogger().severe(String.format("Attempted to load a proxy type %s into %s, but the types don't match", targetType, target.getName())); if (target == null)
throw new LoaderException(); {
// 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 com.google.common.eventbus.EventBus;
import cpw.mods.fml.common.LoaderState.ModState; 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.ModContainer;
import cpw.mods.fml.common.ModMetadata; import cpw.mods.fml.common.ModMetadata;
@ -12,10 +13,15 @@ public class FMLPreInitializationEvent extends FMLStateEvent
{ {
private ModMetadata modMetadata; private ModMetadata modMetadata;
private File sourceFile; private File sourceFile;
private File configurationDir;
private File suggestedConfigFile;
private ASMDataTable asmData;
public FMLPreInitializationEvent(Object... data) public FMLPreInitializationEvent(Object... data)
{ {
super(data); super(data);
this.asmData = (ASMDataTable)data[0];
this.configurationDir = (File)data[1];
} }
@Override @Override
@ -29,6 +35,7 @@ public class FMLPreInitializationEvent extends FMLStateEvent
{ {
this.modMetadata = activeContainer.getMetadata(); this.modMetadata = activeContainer.getMetadata();
this.sourceFile = activeContainer.getSource(); this.sourceFile = activeContainer.getSource();
this.suggestedConfigFile = new File(configurationDir, activeContainer.getModId()+".cfg");
} }
public File getSourceFile() public File getSourceFile()
@ -40,4 +47,19 @@ public class FMLPreInitializationEvent extends FMLStateEvent
{ {
return modMetadata; 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 public class ModLoaderModContainer implements ModContainer
{ {
private static final ProxyInjector NULLPROXY = new ProxyInjector("","","",null);
public BaseModProxy mod; public BaseModProxy mod;
private File modSource; private File modSource;
public List<ArtifactVersion> requirements = Lists.newArrayList(); public List<ArtifactVersion> requirements = Lists.newArrayList();
@ -498,6 +497,7 @@ public class ModLoaderModContainer implements ModContainer
configureMod(modClazz, event.getASMHarvestedData()); configureMod(modClazz, event.getASMHarvestedData());
isNetworkMod = FMLNetworkHandler.instance().registerNetworkMod(this, modClazz, event.getASMHarvestedData()); isNetworkMod = FMLNetworkHandler.instance().registerNetworkMod(this, modClazz, event.getASMHarvestedData());
mod = (BaseModProxy)modClazz.newInstance(); mod = (BaseModProxy)modClazz.newInstance();
ProxyInjector.inject(this, event.getASMHarvestedData(), FMLCommonHandler.instance().getSide());
} }
catch (Exception e) catch (Exception e)
{ {