mirror of
https://github.com/HMCore/Core.git
synced 2025-12-12 13:56:19 +00:00
added module loading
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package org.hmcore;
|
package org.hmcore;
|
||||||
|
|
||||||
import org.hmcore.modules.Module;
|
import org.hmcore.modules.Module;
|
||||||
|
import org.hmcore.modules.ModuleLoader;
|
||||||
import org.hmcore.modules.ModuleManager;
|
import org.hmcore.modules.ModuleManager;
|
||||||
import org.hmcore.modules.RegistryModule;
|
import org.hmcore.modules.RegistryModule;
|
||||||
|
|
||||||
|
|||||||
58
src/main/java/org/hmcore/modules/ModuleLoader.java
Normal file
58
src/main/java/org/hmcore/modules/ModuleLoader.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,9 @@ import org.hmcore.HMCore;
|
|||||||
import org.hmcore.api.ModuleState;
|
import org.hmcore.api.ModuleState;
|
||||||
import org.hmcore.api.exceptions.ModuleErroredException;
|
import org.hmcore.api.exceptions.ModuleErroredException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
public class ModuleManager {
|
public class ModuleManager {
|
||||||
|
|
||||||
static ModuleState overallState = ModuleState.QUEUED;
|
static ModuleState overallState = ModuleState.QUEUED;
|
||||||
@@ -11,7 +14,11 @@ public class ModuleManager {
|
|||||||
public static void loadModules() {
|
public static void loadModules() {
|
||||||
if(overallState != ModuleState.QUEUED) return;
|
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;
|
overallState = ModuleState.LOADED;
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user