added module loading

This commit is contained in:
UnrealValentin
2021-04-29 21:47:03 +02:00
parent 0e6ff1fd6b
commit 6135d2a213
4 changed files with 93 additions and 1 deletions

View File

@@ -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;

View File

@@ -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();
}
}

View File

@@ -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;

View File

@@ -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;
}
}