Change version.properties search slightly, allow access to found file from the preinit event

This commit is contained in:
Christian 2012-09-16 21:42:59 -04:00
parent 83a57f5259
commit 9b081b9d46
2 changed files with 45 additions and 13 deletions

View file

@ -157,11 +157,17 @@ public class FMLModContainer implements ModContainer
internalVersion = (String) descriptor.get("version");
if (Strings.isNullOrEmpty(internalVersion) && getSource().isFile())
{
internalVersion = searchForVersionProperties();
Properties versionProps = searchForVersionProperties();
if (versionProps != null)
{
internalVersion = versionProps.getProperty(getModId()+".version");
FMLLog.fine("Found version %s for mod %s in version.properties", internalVersion, getModId());
}
}
if (Strings.isNullOrEmpty(internalVersion) && !Strings.isNullOrEmpty(modMetadata.version))
{
FMLLog.warning("Mod %s is missing the required element 'version'. Falling back to metadata version %s", getModId(), modMetadata.version);
FMLLog.warning("Mod %s is missing the required element 'version' and a version.properties file could not be found. Falling back to metadata version %s", getModId(), modMetadata.version);
internalVersion = modMetadata.version;
}
if (Strings.isNullOrEmpty(internalVersion))
@ -171,22 +177,34 @@ public class FMLModContainer implements ModContainer
}
}
private String searchForVersionProperties()
public Properties searchForVersionProperties()
{
try
{
FMLLog.fine("Attempting to load the file version.properties and from %s locate a version", getSource().getName());
String version = null;
ZipFile source = new ZipFile(getSource());
ZipEntry versionFile = source.getEntry("version.properties");
if (versionFile!=null)
FMLLog.fine("Attempting to load the file version.properties from %s to locate a version number for %s", getSource().getName(), getModId());
Properties version = null;
if (getSource().isFile())
{
Properties versionProps = new Properties();
versionProps.load(source.getInputStream(versionFile));
version = versionProps.getProperty(getModId()+".version");
FMLLog.fine("Found version %s for mod %s in version.properties", version, getModId());
ZipFile source = new ZipFile(getSource());
ZipEntry versionFile = source.getEntry("version.properties");
if (versionFile!=null)
{
version = new Properties();
version.load(source.getInputStream(versionFile));
}
source.close();
}
else if (getSource().isDirectory())
{
File propsFile = new File(getSource(),"version.properties");
if (propsFile.exists() && propsFile.isFile())
{
version = new Properties();
FileInputStream fis = new FileInputStream(propsFile);
version.load(fis);
fis.close();
}
}
source.close();
return version;
}
catch (Exception e)

View file

@ -1,8 +1,10 @@
package cpw.mods.fml.common.event;
import java.io.File;
import java.util.Properties;
import cpw.mods.fml.common.LoaderState.ModState;
import cpw.mods.fml.common.FMLModContainer;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.ModMetadata;
import cpw.mods.fml.common.discovery.ASMDataTable;
@ -14,6 +16,7 @@ public class FMLPreInitializationEvent extends FMLStateEvent
private File configurationDir;
private File suggestedConfigFile;
private ASMDataTable asmData;
private ModContainer modContainer;
public FMLPreInitializationEvent(Object... data)
{
@ -31,6 +34,7 @@ public class FMLPreInitializationEvent extends FMLStateEvent
@Override
public void applyModContainer(ModContainer activeContainer)
{
this.modContainer = activeContainer;
this.modMetadata = activeContainer.getMetadata();
this.sourceFile = activeContainer.getSource();
this.suggestedConfigFile = new File(configurationDir, activeContainer.getModId()+".cfg");
@ -60,4 +64,14 @@ public class FMLPreInitializationEvent extends FMLStateEvent
{
return asmData;
}
public Properties getVersionProperties()
{
if (this.modContainer instanceof FMLModContainer)
{
return ((FMLModContainer)(this.modContainer)).searchForVersionProperties();
}
return null;
}
}