From 6f3c00c6ab2f88a094d4d870d15468ab0efc4317 Mon Sep 17 00:00:00 2001 From: EOT3000 <43685885+EOT3000@users.noreply.github.com> Date: Mon, 8 Jul 2019 15:40:49 -0400 Subject: [PATCH] start plugin stuff --- .../console/LoggingOutputStream.java | 7 +- .../org/geysermc/connector/plugin/Loader.java | 81 +++++++++++++++++++ .../geysermc/connector/plugin/PluginYML.java | 14 ++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/plugin/Loader.java create mode 100644 connector/src/main/java/org/geysermc/connector/plugin/PluginYML.java diff --git a/connector/src/main/java/org/geysermc/connector/console/LoggingOutputStream.java b/connector/src/main/java/org/geysermc/connector/console/LoggingOutputStream.java index 6afbd3e9..2652fc42 100644 --- a/connector/src/main/java/org/geysermc/connector/console/LoggingOutputStream.java +++ b/connector/src/main/java/org/geysermc/connector/console/LoggingOutputStream.java @@ -14,11 +14,10 @@ package org.geysermc.connector.console; -import com.google.common.base.Charsets; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.logging.Level; import java.util.logging.Logger; @@ -37,7 +36,7 @@ public class LoggingOutputStream extends ByteArrayOutputStream { super.write(b); try { - String contents = toString(Charsets.UTF_8.name()); + String contents = toString(StandardCharsets.UTF_8.name()); if (!contents.isEmpty() && !contents.equals(System.getProperty("line.separator"))) logger.logp(level, "", "", contents); } catch (UnsupportedEncodingException ex) { @@ -47,7 +46,7 @@ public class LoggingOutputStream extends ByteArrayOutputStream { @Override public synchronized void flush() throws IOException { - String contents = toString(Charsets.UTF_8.name()); + String contents = toString(StandardCharsets.UTF_8.name()); super.reset(); if (!contents.isEmpty() && !contents.equals(System.getProperty("line.separator"))) diff --git a/connector/src/main/java/org/geysermc/connector/plugin/Loader.java b/connector/src/main/java/org/geysermc/connector/plugin/Loader.java new file mode 100644 index 00000000..4775a6a4 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/plugin/Loader.java @@ -0,0 +1,81 @@ +package org.geysermc.connector.plugin; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import org.geysermc.api.plugin.Plugin; + +import java.io.File; +import java.io.InputStream; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class Loader extends ClassLoader { + static { + System.out.println("a"); + Loader l = new Loader(); + System.out.println("b"); + File dir = new File("plugins"); + System.out.println(dir.getAbsoluteFile()); + + if(!dir.exists()) { + dir.mkdir(); + } + + for(File f : dir.listFiles()) { + if(f.getName().endsWith(".jar")) { + try { + ZipFile file = new ZipFile(f); + + ZipEntry e = file.getEntry("plugin.yml"); + + if(e == null || e.isDirectory()) { + System.err.println("Plugin " + f.getName() + " has no valid plugin.yml!"); + continue; + } + + file.stream().forEach((x) -> { + if(x.getName().endsWith(".class")) { + try { + InputStream is = file.getInputStream(x); + byte[] b = new byte[is.available()]; + is.read(b); + l.defineClass(x.getName().replace(".class", "").replaceAll("/", "."), b, 0, b.length); + is.close(); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + }); + + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + + InputStream is = file.getInputStream(e); + + PluginYML yml; + + yml = mapper.readValue(is, PluginYML.class); + + is.close(); + + ((Plugin) Class.forName(yml.main, true, l).newInstance()).onEnable(); + + } catch (Exception e) { + System.out.println("Error loading plugin " + f.getName()); + e.printStackTrace(); + } + } + } + LOADER = l; + } + + public static final Loader LOADER; + + public static void start() { + + } + + private Loader() { + + } +} diff --git a/connector/src/main/java/org/geysermc/connector/plugin/PluginYML.java b/connector/src/main/java/org/geysermc/connector/plugin/PluginYML.java new file mode 100644 index 00000000..9552bcb5 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/plugin/PluginYML.java @@ -0,0 +1,14 @@ +package org.geysermc.connector.plugin; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class PluginYML { + @JsonProperty("name") + String name; + + @JsonProperty("version") + String version; + + @JsonProperty("main") + String main; +}