diff --git a/src/main/java/org/hmcore/HMCore.java b/src/main/java/org/hmcore/HMCore.java index b3e6dcc..5f11dd3 100644 --- a/src/main/java/org/hmcore/HMCore.java +++ b/src/main/java/org/hmcore/HMCore.java @@ -1,6 +1,7 @@ package org.hmcore; import org.hmcore.modules.Module; +import org.hmcore.modules.ModuleLoader; import org.hmcore.modules.ModuleManager; import org.hmcore.modules.RegistryModule; diff --git a/src/main/java/org/hmcore/modules/ModuleLoader.java b/src/main/java/org/hmcore/modules/ModuleLoader.java new file mode 100644 index 0000000..77aeef8 --- /dev/null +++ b/src/main/java/org/hmcore/modules/ModuleLoader.java @@ -0,0 +1,58 @@ +package org.hmcore.modules; + +import com.google.gson.GsonBuilder; +import org.hmcore.HMCore; +import org.hmcore.registration.config.ModuleReadable; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Objects; +import java.util.Scanner; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +public class ModuleLoader { + + void loadModules() throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + File moduleDir = new File("hm/modules"); + if(!moduleDir.exists()) moduleDir.mkdirs(); + for (String path: + Objects.requireNonNull(moduleDir.list())) { + loadModule(path); + } + } + + private void loadModule(String path) throws IOException, ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { + System.out.println("Loading module at: " + path); + JarFile jarFile = new JarFile(path); + JarEntry jarEntry = jarFile.getJarEntry("module.json"); + String content = getClassPath(jarFile.getInputStream(jarEntry)); + jarFile.close(); + ModuleReadable readable = new GsonBuilder().create().fromJson(content, ModuleReadable.class); + URLClassLoader child = new URLClassLoader( + new URL[] {new File(path).toURI().toURL()}, + this.getClass().getClassLoader() + ); + Class classToLoad = Class.forName(readable.getClassPath(), true, child); + Module instance = (Module) classToLoad.getDeclaredConstructor().newInstance(); + HMCore.modules.put(instance.getName(), instance); + System.out.println("Loaded " + instance.getName() + " v" + readable.getVersion() + " by " + readable.getCreator()); + } + + private String getClassPath(InputStream jarEntry) { + StringBuilder builder = new StringBuilder(); + + Scanner myReader = new Scanner(jarEntry); + while (myReader.hasNextLine()) { + builder.append(myReader.nextLine()); + } + myReader.close(); + + return builder.toString(); + } + +} diff --git a/src/main/java/org/hmcore/modules/ModuleManager.java b/src/main/java/org/hmcore/modules/ModuleManager.java index e0ae98a..2f0c6d2 100644 --- a/src/main/java/org/hmcore/modules/ModuleManager.java +++ b/src/main/java/org/hmcore/modules/ModuleManager.java @@ -4,6 +4,9 @@ import org.hmcore.HMCore; import org.hmcore.api.ModuleState; import org.hmcore.api.exceptions.ModuleErroredException; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + public class ModuleManager { static ModuleState overallState = ModuleState.QUEUED; @@ -11,7 +14,11 @@ public class ModuleManager { public static void loadModules() { if(overallState != ModuleState.QUEUED) return; - //TODO: LOAD MODULES HERE LATER + try { + new ModuleLoader().loadModules(); + } catch (IOException | ClassNotFoundException | InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } overallState = ModuleState.LOADED; diff --git a/src/main/java/org/hmcore/registration/config/ModuleReadable.java b/src/main/java/org/hmcore/registration/config/ModuleReadable.java new file mode 100644 index 0000000..50aa512 --- /dev/null +++ b/src/main/java/org/hmcore/registration/config/ModuleReadable.java @@ -0,0 +1,26 @@ +package org.hmcore.registration.config; + +public class ModuleReadable { + + final String classPath; + final String version; + final String creator; + + public ModuleReadable(String classPath, String version, String creator) { + this.classPath = classPath; + this.version = version; + this.creator = creator; + } + + public String getClassPath() { + return classPath; + } + + public String getVersion() { + return version; + } + + public String getCreator() { + return creator; + } +}