Browse Source

Some basics of a wacky plugin system.

Check out the testPlugin source code.
pull/15/head
Nick Faro 6 years ago
parent
commit
963bb08e02

+ 5
- 5
WINmakefile.cmd View File

@@ -7,18 +7,18 @@ del client\*.class
del server\*.class
echo.
echo [[[ MAKING COMMON CODE ]]]
javac -source 1.5 -target 1.5 -cp . common/*.java
javac -source 1.6 -target 1.6 -cp . common/*.java
echo.
echo [[[ MAKING CLIENT CODE ]]]
javac -source 1.5 -target 1.5 -cp . client/*.java
javac -source 1.6 -target 1.6 -cp . client/*.java
echo.
echo [[[ MAKING SERVER CODE ]]]
javac -source 1.5 -target 1.5 -cp . server/*.java
javac -source 1.6 -target 1.6 -cp lib/*;. server/*.java
echo.
echo [[[ MAKING CLIENT JARs ]]]
jar cvfm 64pixels.jar manifests/cli-std.MF common/*.class client/*.class client/rawcga.bin client/*.wav
jar cvfm 64pixels.jar manifests/cli-std.MF common/*.class client/*.class client/rawcga.bin client/*.wav > NUL
echo.
echo [[[ MAKING SERVER JAR ]]]
jar cvfm 64px-srvr.jar manifests/srv-std.MF server/*.class common/*.class
jar cvfm 64px-srvr.jar manifests/srv-std.MF server/*.class common/*.class > NUL
echo.
pause

+ 3
- 0
client.bat View File

@@ -0,0 +1,3 @@
@echo off
java -cp "lib/*" -jar 64pixels.jar
pause

+ 1
- 0
manifests/srv-std.MF View File

@@ -1,3 +1,4 @@
Manifest-Version: 1.0
Created-By: 1.6.0_22 (Sun Microsystems Inc.)
Main-Class: server.Main
Class-Path: lib/minimal-json.jar

+ 2
- 1
server.bat View File

@@ -1,3 +1,4 @@
@echo off
java -jar 64px-srvr.jar
rem java -cp "lib/*" 64px-srvr.server.Main
java -Djava.ext.dirs=lib -jar 64px-srvr.jar
pause

+ 34
- 0
server/Plugin.java View File

@@ -0,0 +1,34 @@
package server;

import java.lang.reflect.*;

public class Plugin
{
private Class plugin;
private Object instance;
//Methods
private Method pluginOnEnable;
private Method pluginOnDisable;
public Plugin(Class plugin) throws InstantiationException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
{
this.plugin = plugin;
this.instance = plugin.newInstance();
pluginOnEnable = plugin.getDeclaredMethod("onEnable");
pluginOnDisable = plugin.getDeclaredMethod("onDisable");
}
void onEnable()
{
try {pluginOnEnable.invoke(instance);}
catch (Exception e) {System.out.println("onEnable error!");}
}
void onDisable()
{
try {pluginOnDisable.invoke(instance);}
catch (Exception e) {System.out.println("onDisable error!");}
}
}

+ 31
- 0
server/PluginHandler.java View File

@@ -0,0 +1,31 @@
package server;

import java.io.File;
import java.util.*;

public class PluginHandler
{
private ArrayList<Plugin> plugins;
public PluginHandler()
{
plugins = new ArrayList<Plugin>();
}
public void reloadPlugins()
{
File dir = new File("plugins");
File[] files = dir.listFiles();
File f;
System.out.println(files);
for (int i = 0; i < files.length; i++)
{
f = files[i];
System.out.println("Loading plugin " + f.getName());
Plugin p = new PluginLoader(f.getName()).getPlugin();
p.onEnable();
plugins.add(p);
}
}
}

+ 56
- 0
server/PluginLoader.java View File

@@ -0,0 +1,56 @@
package server;

import com.eclipsesource.json.*;
import java.util.Scanner;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.io.File;
import java.net.URLClassLoader;
import java.net.URL;

public class PluginLoader
{
private Plugin plugin;
public PluginLoader(String jarFile)
{
try
{
File f = new File("plugins");
if (!f.exists()) f.mkdir();
String myJar = "plugins/" + jarFile;
ZipFile zf = new ZipFile(myJar);
ZipEntry ze = zf.getEntry("config.js");
if (ze == null) throw new MalformedPluginException("Plugin " + jarFile + " has a malformed configuration!");
JsonObject jo = JsonObject.readFrom(new Scanner(zf.getInputStream(ze)).useDelimiter("\\A").next());
String classToLoad = jo.get("fullClassName").asString();
URL[] a = {new File(myJar).toURL()};
URLClassLoader child = new URLClassLoader(a, this.getClass().getClassLoader());
Class type = Class.forName(classToLoad, true, child);
plugin = new Plugin(type);
}
catch (Exception e)
{
System.out.println("Pluginload of " + jarFile + " failed!");
e.printStackTrace();
}
}
public Plugin getPlugin()
{
return plugin;
}
}

class MalformedPluginException extends RuntimeException
{
public MalformedPluginException(String msg)
{
super(msg);
}
}

+ 4
- 0
server/Server.java View File

@@ -39,6 +39,7 @@ public class Server extends ServerShim
public int po = 25566;
public boolean mapLock = false;
public World world;
public PluginHandler pluginHandler;
public int countPlayers()
{
@@ -1021,6 +1022,9 @@ public class Server extends ServerShim
world_names=readNamesFile("worlds.txt");
System.out.println(world_names.length + " worlds!");
worlds.add(world);
pluginHandler = new PluginHandler();
pluginHandler.reloadPlugins();
for(String wn : world_names)
{
WorldMap tm = new WorldMap(true,map_cache_size,wn);

+ 3
- 0
testPlugin/comple.cmd View File

@@ -0,0 +1,3 @@
@echo off
javac plugtest/plug/*.java
pause

+ 3
- 0
testPlugin/plugtest/config.js View File

@@ -0,0 +1,3 @@
{
fullClassName: "plug.plugtest.PlugTest"
}

+ 14
- 0
testPlugin/plugtest/plug/PlugTest.java View File

@@ -0,0 +1,14 @@
package plug;

public class PlugTest
{
public void onEnable()
{
System.out.println("It was enabled.");
}

public void onDisable()
{
System.out.println("It was disabled.");
}
}

Loading…
Cancel
Save