From d2da5a71e239fbf012386711c5a28cdd39d48aab Mon Sep 17 00:00:00 2001 From: autaut03 Date: Wed, 13 Feb 2019 00:06:19 +0200 Subject: [PATCH] Little refactor --- .../forgelin/FMLKotlinModContainer.kt | 60 +++++++++---------- .../forgelin/FMLKotlinModLanguageProvider.kt | 13 +--- .../forgelin/FMLKotlinModTarget.kt | 7 +-- 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModContainer.kt b/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModContainer.kt index 2faa513..2e6eff2 100644 --- a/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModContainer.kt +++ b/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModContainer.kt @@ -3,7 +3,6 @@ package net.shadowfacts.forgelin import net.minecraftforge.eventbus.EventBusErrorMessage import net.minecraftforge.eventbus.api.Event import net.minecraftforge.eventbus.api.IEventBus -import net.minecraftforge.eventbus.api.IEventExceptionHandler import net.minecraftforge.eventbus.api.IEventListener import net.minecraftforge.fml.* import net.minecraftforge.fml.Logging.LOADING @@ -15,19 +14,19 @@ import java.util.* import java.util.function.Consumer class FMLKotlinModContainer( - private val info: IModInfo, + info: IModInfo, private val className: String, - private val modClassLoader: ClassLoader, + modClassLoader: ClassLoader, private val scanResults: ModFileScanData ) : ModContainer(info) { - private val log = LogManager.getLogger() + private val logger = LogManager.getLogger() val eventBus: IEventBus private var mod: Any? = null private val modClass: Class<*> init { - log.debug(LOADING, "Creating FMLModContainer instance for {} with classLoader {} & {}", className, modClassLoader, javaClass.classLoader) + logger.debug(LOADING, "Creating FMLModContainer instance for {} with classLoader {} & {}", className, modClassLoader, javaClass.classLoader) triggerMap[ModLoadingStage.CONSTRUCT] = dummy().andThen(::beforeEvent).andThen(::constructMod).andThen(::afterEvent) triggerMap[ModLoadingStage.CREATE_REGISTRIES] = dummy().andThen(::beforeEvent).andThen(::fireEvent).andThen(::afterEvent) triggerMap[ModLoadingStage.LOAD_REGISTRIES] = dummy().andThen(::beforeEvent).andThen(::fireEvent).andThen(::afterEvent) @@ -43,20 +42,34 @@ class FMLKotlinModContainer( // Here, we won't init the class, meaning static {} blocks (init {} in kotlin) won't get triggered // but we will still have to do it later, on CONSTRUCT phase. modClass = Class.forName(className, false, modClassLoader) - log.debug(LOADING, "Loaded modclass {} with {}", modClass.name, modClass.classLoader) + logger.debug(LOADING, "Loaded modclass {} with {}", modClass.name, modClass.classLoader) } catch (e: Throwable) { - log.error(LOADING, "Failed to load class {}", className, e) + logger.error(LOADING, "Failed to load class {}", className, e) throw ModLoadingException(info, ModLoadingStage.CONSTRUCT, "fml.modloading.failedtoloadmodclass", e) } } - private fun dummy(): Consumer = Consumer {} + private fun constructMod(event: LifecycleEventProvider.LifecycleEvent) { + try { + logger.debug(LOADING, "Loading mod instance {} of type {}", getModId(), modClass.name) + // Now we can load the class, so that static {} block gets called + Class.forName(className) + // Then we check whether it's a kotlin object and return it, or if not we create a new instance of kotlin class. + this.mod = modClass.kotlin.objectInstance ?: modClass.newInstance() + logger.debug(LOADING, "Loaded mod instance {} of type {}", getModId(), modClass.name) + } catch (e: Throwable) { + logger.error(LOADING, "Failed to create mod instance. ModID: {}, class {}", getModId(), modClass.name, e) + throw ModLoadingException(modInfo, event.fromStage(), "fml.modloading.failedtoloadmod", e, modClass) + } - private fun onEventFailed(iEventBus: IEventBus, event: Event, iEventListeners: Array, i: Int, throwable: Throwable) { - log.error(EventBusErrorMessage(event, i, iEventListeners, throwable)) + logger.debug(LOADING, "Injecting Automatic event subscribers for {}", getModId()) + ForgelinAutomaticEventSubscriber.inject(this, this.scanResults, this.modClass.classLoader) + logger.debug(LOADING, "Completed Automatic event subscribers for {}", getModId()) } + private fun dummy(): Consumer = Consumer {} + private fun beforeEvent(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) { FMLKotlinModLoadingContext.get().activeContainer = this ModThreadContext.get().activeContainer = this @@ -64,12 +77,12 @@ class FMLKotlinModContainer( private fun fireEvent(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) { val event = lifecycleEvent.getOrBuildEvent(this) - log.debug(LOADING, "Firing event for modid {} : {}", this.getModId(), event) + logger.debug(LOADING, "Firing event for modid {} : {}", this.getModId(), event) try { eventBus.post(event) - log.debug(LOADING, "Fired event for modid {} : {}", this.getModId(), event) + logger.debug(LOADING, "Fired event for modid {} : {}", this.getModId(), event) } catch (e: Throwable) { - log.error(LOADING, "Caught exception during event {} dispatch for modid {}", event, this.getModId(), e) + logger.error(LOADING, "Caught exception during event {} dispatch for modid {}", event, this.getModId(), e) throw ModLoadingException(modInfo, lifecycleEvent.fromStage(), "fml.modloading.errorduringevent", e) } @@ -79,29 +92,14 @@ class FMLKotlinModContainer( ModThreadContext.get().activeContainer = null FMLKotlinModLoadingContext.get().activeContainer = null if (currentState == ModLoadingStage.ERROR) { - log.error(LOADING, "An error occurred while dispatching event {} to {}", lifecycleEvent.fromStage(), getModId()) + logger.error(LOADING, "An error occurred while dispatching event {} to {}", lifecycleEvent.fromStage(), getModId()) } } - private fun constructMod(event: LifecycleEventProvider.LifecycleEvent) { - try { - log.debug(LOADING, "Loading mod instance {} of type {}", getModId(), modClass.name) - // Now we can load the class, so that static {} block gets called - Class.forName(className) - // Then we check whether it's a kotlin object and return it, or if not we create a new instance of kotlin class. - this.mod = modClass.kotlin.objectInstance ?: modClass.newInstance() - log.debug(LOADING, "Loaded mod instance {} of type {}", getModId(), modClass.name) - } catch (e: Throwable) { - log.error(LOADING, "Failed to create mod instance. ModID: {}, class {}", getModId(), modClass.name, e) - throw ModLoadingException(modInfo, event.fromStage(), "fml.modloading.failedtoloadmod", e, modClass) - } - - log.debug(LOADING, "Injecting Automatic event subscribers for {}", getModId()) - ForgelinAutomaticEventSubscriber.inject(this, this.scanResults, this.modClass.classLoader) - log.debug(LOADING, "Completed Automatic event subscribers for {}", getModId()) + private fun onEventFailed(iEventBus: IEventBus, event: Event, iEventListeners: Array, i: Int, throwable: Throwable) { + logger.error(EventBusErrorMessage(event, i, iEventListeners, throwable)) } override fun matches(mod: Any): Boolean = mod === this.mod - override fun getMod(): Any? = mod } \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModLanguageProvider.kt b/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModLanguageProvider.kt index 31cccfc..48966b5 100644 --- a/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModLanguageProvider.kt +++ b/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModLanguageProvider.kt @@ -10,23 +10,16 @@ import java.util.function.Consumer import java.util.function.Supplier import java.util.stream.Collectors -/** - * Forge {@link ILanguageAdapter} for Kotlin - * Usage: Set the {@code modLanguageAdapter} field in your {@code @Mod} annotation to {@code net.shadowfacts.forgelin.FMLKotlinModLanguageProvider} - * @author shadowfacts - */ class FMLKotlinModLanguageProvider : IModLanguageProvider { - private val log = LogManager.getLogger() + private val logger = LogManager.getLogger() - override fun name(): String { - return "kotlinfml" - } + override fun name(): String = "kotlinfml" override fun getFileVisitor(): Consumer { return Consumer { scanResult -> val modTargetMap = scanResult.annotations.stream() .filter { ad -> ad.annotationType == FMLJavaModLanguageProvider.MODANNOTATION } - .peek { ad -> log.debug(SCAN, "Found @Mod class {} with id {}", ad.classType.className, ad.annotationData["value"]) } + .peek { ad -> logger.debug(SCAN, "Found @Mod class {} with id {}", ad.classType.className, ad.annotationData["value"]) } .map { ad -> FMLKotlinModTarget(ad.classType.className, ad.annotationData["value"] as String) } .collect(Collectors.toMap(java.util.function.Function { it.modId }, java.util.function.Function.identity())) scanResult.addLanguageLoader(modTargetMap) diff --git a/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModTarget.kt b/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModTarget.kt index 587685b..27193d4 100644 --- a/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModTarget.kt +++ b/src/main/kotlin/net/shadowfacts/forgelin/FMLKotlinModTarget.kt @@ -5,10 +5,9 @@ import net.minecraftforge.forgespi.language.IModInfo import net.minecraftforge.forgespi.language.IModLanguageProvider import net.minecraftforge.forgespi.language.ModFileScanData import org.apache.logging.log4j.LogManager -import java.lang.reflect.InvocationTargetException class FMLKotlinModTarget(private val className: String, val modId: String) : IModLanguageProvider.IModLanguageLoader { - private val log = LogManager.getLogger() + private val logger = LogManager.getLogger() override fun loadMod(info: IModInfo, modClassLoader: ClassLoader, modFileScanResults: ModFileScanData): T { // This language class is loaded in the system level classloader - before the game even starts @@ -16,11 +15,11 @@ class FMLKotlinModTarget(private val className: String, val modId: String) : IMo // in the classloader of the game - the context classloader is appropriate here. try { val fmlContainer = Class.forName("net.shadowfacts.forgelin.FMLKotlinModContainer", true, Thread.currentThread().contextClassLoader) - log.debug(LOADING, "Loading FMLKotlinModContainer from classloader {} - got {}", Thread.currentThread().contextClassLoader, fmlContainer.classLoader) + logger.debug(LOADING, "Loading FMLKotlinModContainer from classloader {} - got {}", Thread.currentThread().contextClassLoader, fmlContainer.classLoader) val constructor = fmlContainer.getConstructor(IModInfo::class.java, String::class.java, ClassLoader::class.java, ModFileScanData::class.java) return constructor.newInstance(info, className, modClassLoader, modFileScanResults) as T } catch (e: ReflectiveOperationException) { - log.fatal(LOADING, "Unable to load FMLKotlinModContainer, wut?", e) + logger.fatal(LOADING, "Unable to load FMLKotlinModContainer, wut?", e) throw e } }