2013-03-08 00:25:48 +00:00
|
|
|
/*
|
2016-06-23 03:49:47 +00:00
|
|
|
* Minecraft Forge
|
|
|
|
* Copyright (c) 2016.
|
2013-09-25 15:46:11 +00:00
|
|
|
*
|
2016-06-23 03:49:47 +00:00
|
|
|
* This library 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 version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library 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 this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2013-03-08 00:25:48 +00:00
|
|
|
*/
|
|
|
|
|
2014-09-23 05:01:24 +00:00
|
|
|
package net.minecraftforge.fml.common;
|
2012-07-22 14:26:38 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
2014-09-07 08:51:54 +00:00
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.util.Map;
|
2012-07-23 19:03:17 +00:00
|
|
|
import java.util.regex.Pattern;
|
2012-07-22 14:26:38 +00:00
|
|
|
|
2014-09-23 05:01:24 +00:00
|
|
|
import net.minecraftforge.fml.common.discovery.ModCandidate;
|
|
|
|
import net.minecraftforge.fml.common.discovery.asm.ASMModParser;
|
|
|
|
import net.minecraftforge.fml.common.discovery.asm.ModAnnotation;
|
|
|
|
|
2014-09-07 08:51:54 +00:00
|
|
|
import org.apache.logging.log4j.Level;
|
2012-07-22 14:26:38 +00:00
|
|
|
import org.objectweb.asm.Type;
|
|
|
|
|
2014-09-07 08:51:54 +00:00
|
|
|
import com.google.common.base.Throwables;
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
2012-07-22 14:26:38 +00:00
|
|
|
public class ModContainerFactory
|
|
|
|
{
|
2014-09-07 08:51:54 +00:00
|
|
|
public static Map<Type, Constructor<? extends ModContainer>> modTypes = Maps.newHashMap();
|
2012-07-23 19:03:17 +00:00
|
|
|
private static Pattern modClass = Pattern.compile(".*(\\.|)(mod\\_[^\\s$]+)$");
|
2012-07-22 14:26:38 +00:00
|
|
|
private static ModContainerFactory INSTANCE = new ModContainerFactory();
|
2015-02-22 02:33:30 +00:00
|
|
|
|
2014-09-07 08:51:54 +00:00
|
|
|
private ModContainerFactory() {
|
|
|
|
// We always know about Mod type
|
|
|
|
registerContainerType(Type.getType(Mod.class), FMLModContainer.class);
|
|
|
|
}
|
2012-07-22 14:26:38 +00:00
|
|
|
public static ModContainerFactory instance() {
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
2015-02-22 02:33:30 +00:00
|
|
|
|
2014-09-07 08:51:54 +00:00
|
|
|
public void registerContainerType(Type type, Class<? extends ModContainer> container)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Constructor<? extends ModContainer> constructor = container.getConstructor(new Class<?>[] { String.class, ModCandidate.class, Map.class });
|
|
|
|
modTypes.put(type, constructor);
|
|
|
|
} catch (Exception e) {
|
|
|
|
FMLLog.log(Level.ERROR, e, "Critical error : cannot register mod container type %s, it has an invalid constructor");
|
|
|
|
Throwables.propagate(e);
|
|
|
|
}
|
|
|
|
}
|
2012-08-18 14:09:53 +00:00
|
|
|
public ModContainer build(ASMModParser modParser, File modSource, ModCandidate container)
|
2012-07-22 14:26:38 +00:00
|
|
|
{
|
2012-07-23 19:03:17 +00:00
|
|
|
String className = modParser.getASMType().getClassName();
|
2012-08-23 04:04:34 +00:00
|
|
|
if (modParser.isBaseMod(container.getRememberedBaseMods()) && modClass.matcher(className).find())
|
2012-07-22 14:26:38 +00:00
|
|
|
{
|
2013-11-10 16:49:06 +00:00
|
|
|
FMLLog.severe("Found a BaseMod type mod %s", className);
|
|
|
|
FMLLog.severe("This will not be loaded and will be ignored. ModLoader mechanisms are no longer available.");
|
2012-07-22 14:26:38 +00:00
|
|
|
}
|
2012-09-04 15:39:51 +00:00
|
|
|
else if (modClass.matcher(className).find())
|
|
|
|
{
|
|
|
|
FMLLog.fine("Identified a class %s following modloader naming convention but not directly a BaseMod or currently seen subclass", className);
|
|
|
|
container.rememberModCandidateType(modParser);
|
|
|
|
}
|
2012-08-23 04:04:34 +00:00
|
|
|
else if (modParser.isBaseMod(container.getRememberedBaseMods()))
|
|
|
|
{
|
|
|
|
FMLLog.fine("Found a basemod %s of non-standard naming format", className);
|
|
|
|
container.rememberBaseModType(className);
|
|
|
|
}
|
2012-07-30 21:01:27 +00:00
|
|
|
|
|
|
|
for (ModAnnotation ann : modParser.getAnnotations())
|
2012-07-22 14:26:38 +00:00
|
|
|
{
|
2014-09-07 08:51:54 +00:00
|
|
|
if (modTypes.containsKey(ann.getASMType()))
|
2012-07-22 14:26:38 +00:00
|
|
|
{
|
2014-09-07 08:51:54 +00:00
|
|
|
FMLLog.fine("Identified a mod of type %s (%s) - loading", ann.getASMType(), className);
|
|
|
|
try {
|
2015-02-22 02:33:30 +00:00
|
|
|
ModContainer ret = modTypes.get(ann.getASMType()).newInstance(className, container, ann.getValues());
|
|
|
|
if (!ret.shouldLoadInEnvironment())
|
|
|
|
{
|
|
|
|
FMLLog.fine("Skipping mod %s, container opted to not load.", className);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return ret;
|
2014-09-07 08:51:54 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
FMLLog.log(Level.ERROR, e, "Unable to construct %s container", ann.getASMType().getClassName());
|
|
|
|
return null;
|
|
|
|
}
|
2012-07-22 14:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-30 21:01:27 +00:00
|
|
|
|
2012-07-22 14:26:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|