Things. they work. Obviously still WAY more to do.

This commit is contained in:
cpw authored and LexManos committed 2018-09-05 11:53:54 -07:00
1 parent 1b1ee6f0b8
commit baa9dfe55a
95 files changed
+852 -786

No files matched your search

@@ -19,23 +19,28 @@
package net.minecraftforge.fml.javafmlmod;
import net.minecraftforge.fml.language.ModContainer;
import net.minecraftforge.fml.language.IModLanguageProvider;
import net.minecraftforge.fml.language.IModInfo;
import net.minecraftforge.fml.language.ModFileScanData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.objectweb.asm.Type;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import static net.minecraftforge.fml.Logging.LOADING;
import static net.minecraftforge.fml.Logging.SCAN;
import static net.minecraftforge.fml.Logging.fmlLog;
public class FMLJavaModLanguageProvider implements IModLanguageProvider
{
private static class FMLModTarget implements IModLanguageProvider.IModLanguageLoader {
private static final Logger LOGGER = LogManager.getLogger("FML");
private final String className;
private final String modId;
@@ -50,10 +55,21 @@ public class FMLJavaModLanguageProvider implements IModLanguageProvider
return modId;
}
@SuppressWarnings("unchecked")
@Override
public ModContainer loadMod(final IModInfo info, final ClassLoader modClassLoader, final ModFileScanData modFileScanResults)
public <T> T loadMod(final IModInfo info, final ClassLoader modClassLoader, final ModFileScanData modFileScanResults)
{
return new FMLModContainer(info, className, modClassLoader, modFileScanResults);
try
{
final Constructor<?> constructor = Class.forName("net.minecraftforge.fml.javafmlmod.FMLModContainer", true, Thread.currentThread().getContextClassLoader()).
getConstructor(IModInfo.class, String.class, ClassLoader.class, ModFileScanData.class);
return (T)constructor.newInstance(info, className, modClassLoader, modFileScanResults);
}
catch (NoSuchMethodException | ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException e)
{
LOGGER.error(LOADING,"Unable to load FMLModContainer, wut?", e);
throw new RuntimeException(e);
}
}
}
@@ -71,7 +87,7 @@ public class FMLJavaModLanguageProvider implements IModLanguageProvider
final Map<String, FMLModTarget> modTargetMap = scanResult.getAnnotations().stream()
.filter(ad -> ad.getAnnotationType().equals(MODANNOTATION))
.peek(ad -> fmlLog.debug(SCAN, "Found @Mod class {} with id {}", ad.getClassType().getClassName(), ad.getAnnotationData().get("modid")))
.map(ad -> new FMLModTarget(ad.getClassType().getClassName(), (String)ad.getAnnotationData().get("modid")))
.map(ad -> new FMLModTarget(ad.getClassType().getClassName(), (String)ad.getAnnotationData().get("value")))
.collect(Collectors.toMap(FMLModTarget::getModId, Function.identity()));
scanResult.addLanguageLoader(modTargetMap);
};
@@ -18,26 +18,22 @@
*/
package net.minecraftforge.fml.javafmlmod;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;
import net.minecraftforge.eventbus.EventBusErrorMessage;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.IEventListener;
import net.minecraftforge.fml.LifecycleEventProvider;
import net.minecraftforge.fml.loading.ModLoadingStage;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.language.ModContainer;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.fml.common.event.ModLifecycleEvent;
import net.minecraftforge.fml.AutomaticEventSubscriber;
import net.minecraftforge.fml.ModLoadingStage;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.language.IModInfo;
import net.minecraftforge.fml.language.ModFileScanData;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.objectweb.asm.Type;
import java.util.function.Consumer;
import static net.minecraftforge.fml.Logging.LOADING;
@@ -45,6 +41,7 @@ public class FMLModContainer extends ModContainer
{
private static final Logger LOGGER = LogManager.getLogger("FML");
private final ModFileScanData scanResults;
private final IEventBus eventBus;
private Object modInstance;
private final Class<?> modClass;
@@ -52,43 +49,65 @@ public class FMLModContainer extends ModContainer
{
super(info);
this.scanResults = modFileScanResults;
triggerMap.put(ModLoadingStage.BEGIN, this::constructMod);
triggerMap.put(ModLoadingStage.PREINIT, this::preinitMod);
triggerMap.put(ModLoadingStage.BEGIN, dummy().andThen(this::beforeEvent).andThen(this::constructMod).andThen(this::afterEvent));
triggerMap.put(ModLoadingStage.CONSTRUCT, dummy().andThen(this::beforeEvent).andThen(this::preinitMod).andThen(this::fireEvent).andThen(this::afterEvent));
triggerMap.put(ModLoadingStage.PREINIT, dummy().andThen(this::beforeEvent).andThen(this::initMod).andThen(this::fireEvent).andThen(this::afterEvent));
this.eventBus = IEventBus.create(this::onEventFailed);
try
{
modClass = Class.forName(className, true, modClassLoader);
LOGGER.error(LOADING,"Loaded modclass {} with {}", modClass.getName(), modClass.getClassLoader());
}
catch (ClassNotFoundException e)
catch (Throwable e)
{
LOGGER.error(LOADING, "Failed to load class {}", className, e);
throw new RuntimeException(e);
}
}
private void preinitMod(LifecycleEventProvider.LifecycleEvent lifecycleEvent)
private void initMod(LifecycleEventProvider.LifecycleEvent lifecycleEvent)
{
List<Pair<String, String>> instanceFields = scanResults.getAnnotations().stream().
filter(a -> Objects.equals(a.getAnnotationType(), Type.getType(Instance.class)) &&
Objects.equals(a.getClassType(), Type.getType(modClass))).
map(a->Pair.of(a.getMemberName(), (String)a.getAnnotationData().get("value"))).
collect(Collectors.toList());
}
instanceFields.forEach(p->{
try
{
final Field field = modClass.getDeclaredField(p.getLeft());
field.setAccessible(true);
field.set(modInstance, FMLLoader.getModLoader().getModList().getModObjectById(p.getRight()).get());
}
catch (NoSuchFieldException | IllegalAccessException e)
{
LOGGER.error(LOADING, "Unable to set field {} to mod with ID {}", p.getLeft(), p.getRight(), e);
}
});
private Consumer<LifecycleEventProvider.LifecycleEvent> dummy() { return (s) -> {}; }
private void onEventFailed(IEventBus iEventBus, Event event, IEventListener[] iEventListeners, int i, Throwable throwable)
{
LOGGER.error(new EventBusErrorMessage(event, i, iEventListeners, throwable));
}
private void beforeEvent(LifecycleEventProvider.LifecycleEvent lifecycleEvent) {
ModLoadingContext.get().activeContainer = this;
}
private void fireEvent(LifecycleEventProvider.LifecycleEvent lifecycleEvent) {
final ModLifecycleEvent event = lifecycleEvent.buildModEvent(this);
LOGGER.debug(LOADING, "Firing event {} for modid {}", event, this.getModId());
try
{
eventBus.post(event);
LOGGER.debug(LOADING, "Fired event {} for modid {}", event, this.getModId());
}
catch (Throwable e)
{
LOGGER.error(LOADING,"Caught exception during event {} dispatch for modid {}", event, this.getModId(), e);
modLoadingStage = ModLoadingStage.ERROR;
modLoadingError.add(e);
}
}
private void afterEvent(LifecycleEventProvider.LifecycleEvent lifecycleEvent) {
ModLoadingContext.get().activeContainer = null;
if (getCurrentState() == ModLoadingStage.ERROR) {
LOGGER.error(LOADING,"An error occurred while dispatching event {} to {}", lifecycleEvent.fromStage(), getModId());
}
}
private void preinitMod(LifecycleEventProvider.LifecycleEvent lifecycleEvent)
{
AutomaticEventSubscriber.inject(this, this.scanResults, this.modClass.getClassLoader());
}
private void constructMod(LifecycleEventProvider.LifecycleEvent event)
@@ -99,10 +118,11 @@ public class FMLModContainer extends ModContainer
this.modInstance = modClass.newInstance();
LOGGER.debug(LOADING, "Loaded mod instance {} of type {}", getModId(), modClass.getName());
}
catch (InstantiationException | IllegalAccessException e)
catch (Throwable e)
{
LOGGER.error("Failed to create mod instance. ModID: {}, class {}", getModId(), modClass.getName(), e);
throw new RuntimeException("Failed to create mod instance", e);
LOGGER.error(LOADING,"Failed to create mod instance. ModID: {}, class {}", getModId(), modClass.getName(), e);
modLoadingStage = ModLoadingStage.ERROR;
modLoadingError.add(e);
}
}
@@ -118,9 +138,8 @@ public class FMLModContainer extends ModContainer
return modInstance;
}
@Override
public <T> T getCustomExtension(final String name)
public IEventBus getEventBus()
{
return ((Function<String,T>)modInstance).apply(name);
return this.eventBus;
}
}
@@ -0,0 +1,43 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* 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
*/
package net.minecraftforge.fml.javafmlmod;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.language.ExtensionPoint;
import java.util.function.Supplier;
public class ModLoadingContext
{
private static ThreadLocal<ModLoadingContext> context = ThreadLocal.withInitial(ModLoadingContext::new);
FMLModContainer activeContainer;
public static ModLoadingContext get() {
return context.get();
}
public <T> void registerExtensionPoint(ExtensionPoint<T> point, Supplier<T> extension) {
activeContainer.registerExtensionPoint(point, extension);
}
public IEventBus getModEventBus()
{
return activeContainer.getEventBus();
}
}