Skip to content

Commit

Permalink
feat: module loading system
Browse files Browse the repository at this point in the history
* Added module test
* Working on improvements to module loading
  • Loading branch information
Im-Fran committed Oct 4, 2023
1 parent 2f487ba commit d66c3f7
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,67 @@
package xyz.theprogramsrc.simplecoreapi.global.models

import xyz.theprogramsrc.simplecoreapi.global.SimpleCoreAPI
import xyz.theprogramsrc.simplecoreapi.global.utils.ILogger
import java.util.UUID

/**
* This interface represents a module.
* It is used to load modules on demand.
*/
interface Module {
open class Module {

/**
* The SimpleCoreAPI instance.
* This is used to access the API instance and its methods.
* @see SimpleCoreAPI
*/
protected val simplecoreapi: SimpleCoreAPI = SimpleCoreAPI.instance

/**
* The ILogger instance.
* This is used to log messages to the console.
* @see ILogger
*/
protected val logger: ILogger = simplecoreapi.logger

/**
* This method is called when the module is loaded.
*/
fun onEnable()
open fun onEnable() {}

/**
* This method is called when the module is unloaded.
*/
fun onDisable()
open fun onDisable() {}

companion object {
val loadedModules: MutableMap<String, Module> = mutableMapOf()

/**
* This method is used to load a module.
*
* Example usage:
* ```kotlin
* val myModule = Module.require<MyModule>()
* ```
*
*
* @param T The module class
* @return The module instance
*/
inline fun <reified T : Module> require(): T {
val moduleName = T::class.java.canonicalName

return loadedModules.computeIfAbsent(moduleName) {
val moduleInstance = T::class.java.getConstructor().newInstance()
moduleInstance.onEnable()
loadedModules[moduleName] = moduleInstance
Runtime.getRuntime().addShutdownHook(Thread {
moduleInstance.onDisable()
})
moduleInstance
} as T
}
}
}

/**
* This method is used to load a module.
*
* Example usage:
* ```kotlin
* val myModule = requireModule<MyModule>()
* ```
*
*
* @param T The module class
* @return The module instance
*/
inline fun <reified T : Module> requireModule(): T {
val moduleName = UUID.nameUUIDFromBytes("${T::class.java.name}${T::class.java.classLoader}${T::class.java.`package`.name}".toByteArray()).toString()
println("Name for ${T::class.java.name}: $moduleName")

return Module.loadedModules.computeIfAbsent(moduleName) {
val moduleInstance = T::class.java.getConstructor().newInstance()
moduleInstance.onEnable()
Module.loadedModules[moduleName] = moduleInstance
Runtime.getRuntime().addShutdownHook(Thread {
moduleInstance.onDisable()
})
moduleInstance
} as T
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package xyz.theprogramsrc.simplecoreapi.global.models

import org.apache.commons.io.FileUtils
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import xyz.theprogramsrc.simplecoreapi.standalone.StandaloneLoader
import java.io.File

class ModuleTest {

@Test
fun `test require() method`() {
val mathModule = requireModule<MathModule>()

assertEquals(1, MathModule.loaded)
assertEquals(3, mathModule.sum(1, 2))
}

companion object {
@BeforeAll
@JvmStatic
fun setUp() {
StandaloneLoader()
}

@AfterAll
@JvmStatic
fun tearDown() {
arrayOf("SimpleCoreAPI/", "plugins/").map { File(it) }.filter{ it.exists() }.forEach { FileUtils.forceDelete(it) }
}
}
}

class MathModule: Module() {

companion object {
var loaded = 0
}

override fun onEnable() {
logger.info("Enabled DummyModule")
loaded++
}

override fun onDisable() {
logger.info("Disabled DummyModule")
}

fun sum(a: Int, b: Int) =
a + b

}

class AlgebraModule: Module() {

override fun onEnable() {
logger.info("Enabled AlgebraModule")
}

override fun onDisable() {
logger.info("Disabled AlgebraModule")
}

fun solveEquation(a: Int, b: Int, c: Int): Int {
val delta = b * b - 4 * a * c
return if (delta < 0) {
-1
} else if (delta == 0) {
0
} else {
1
}
}

}

0 comments on commit d66c3f7

Please sign in to comment.