Skip to content

Instantly share code, notes, and snippets.

@TrainerGuy22
Created July 16, 2013 15:18
Show Gist options
  • Save TrainerGuy22/6009687 to your computer and use it in GitHub Desktop.
Save TrainerGuy22/6009687 to your computer and use it in GitHub Desktop.
package themike.core.script;
import groovy.lang.GroovyClassLoader;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import themike.core.MCore;
public class ScriptLoader {
public static GroovyClassLoader classLoader = new GroovyClassLoader();
private static ArrayList<Class> classes = new ArrayList<Class>();
public static ArrayList<ScriptMeta> meta = new ArrayList<ScriptMeta>();
public static void load() {
File[] files = MCore.directory.listFiles();
if (files != null) {
for (File file : files) {
if (!file.getName().endsWith(".groovy")) {
System.out.println("Not Loading " + file.getName() + ": Not .groovy!");
continue;
}
Class clazz;
try {
clazz = classLoader.parseClass(file);
} catch (Exception e) {
System.out.println("Not Loading " + file.getName() + ": Invalid Syntax!");
e.printStackTrace();
continue;
}
classes.add(clazz);
}
}
}
public static ArrayList<Class> getClasses() {
return classes;
}
public static void preInitialize() {
}
public static void initialize() {
Method loadMethod = null;
Method metaMethod = null;
for (Class clazz : classes) {
try {
loadMethod = clazz.getMethod("init");
} catch (NoSuchMethodException e) {}
try {
loadMethod = clazz.getMethod("getMeta");
} catch (NoSuchMethodException e) {
System.out.println("MScript Class " + clazz.getName() + " does not have a getInit method! Ignoring....");
}
if (metaMethod != null) {
try {
meta.add((ScriptMeta) metaMethod.invoke("getMeta"));
} catch (Exception e) {
e.printStackTrace();
}
}
if (loadMethod != null) {
try {
loadMethod.invoke("init");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void postInitialize() {
Method postMethod = null;
for (Class clazz : classes) {
try {
postMethod = clazz.getMethod("postInit");
} catch (NoSuchMethodException e) {}
if (postMethod != null) {
try {
// postMethod.invoke("postInit");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment