if (!isPluginLoaded(pluginName)) {
throw new UnknownDependencyException(pluginName);
}
}
PyList pythonpath = Py.getSystemState().path;
PyString filepath = new PyString(file.getAbsolutePath());
if (data.shouldAddPathEntry()) {
if (pythonpath.__contains__(filepath)) {
throw new InvalidPluginException(new Exception("path " + filepath
+ " already on pythonpath!")); //can't imagine how this would happen, but better safe than sorry
}
pythonpath.append(filepath);
}
String mainfile = description.getMain();
InputStream instream = null;
try {
instream = data.getStream(mainfile);
if (instream == null) {
mainfile = "plugin.py";
instream = data.getStream(mainfile);
}
if (instream == null) {
mainfile = "main.py";
instream = data.getStream(mainfile);
}
} catch (IOException e) {
throw new InvalidPluginException(e);
}
if (instream == null) {
throw new InvalidPluginException(new FileNotFoundException("Data file does not contain "+mainfile));
}
try {
PythonHooks hook = new PythonHooks(description);
PythonInterpreter interp = new PythonInterpreter();
interp.set("hook", hook);
interp.set("info", description);
// Decorator Enhancements
interp.exec("import __builtin__");
interp.exec("__builtin__.hook = hook");
interp.exec("__builtin__.info = info");
// Hardcoded for now, may be worth thinking about generalizing it as sort of "addons" for the PythonPluginLoader
// Could be used to extend the capabilities of python plugins the same way the metaclass decorators do, without requiring any changes to the PythonPluginLoader itself
String[] pre_plugin_scripts = {"imports.py", "meta_decorators.py"};
String[] post_plugin_scripts = {"meta_loader.py"};
// Run scripts designed to be run before plugin creation
for (String script : pre_plugin_scripts) {
InputStream metastream = this.getClass().getClassLoader().getResourceAsStream("scripts/"+script);
interp.execfile(metastream);
metastream.close();
}
interp.execfile(instream);
instream.close();
try {
if (!hasyml) {
Object name = interp.get("__plugin_name__");
Object version = interp.get("__plugin_version__");
Object website = interp.get("__plugin_website__");
Object main = interp.get("__plugin_mainclass__");
hassolidmeta = name != null && version != null;
if (name != null)
ReflectionHelper.setPrivateValue(description, "name", name.toString());
if (version != null)
ReflectionHelper.setPrivateValue(description, "version", version.toString());
if (website != null)
ReflectionHelper.setPrivateValue(description, "website", website.toString());
if (main != null)
ReflectionHelper.setPrivateValue(description, "main", main.toString());
}
} catch (Throwable t) {
Logger.getLogger("Minecraft").log(Level.SEVERE, "Error while setting python-set description values", t);
}
String mainclass = description.getMain();
PyObject pyClass = interp.get(mainclass);
if (pyClass == null)
pyClass = interp.get("Plugin");
if (pyClass == null)
result = new PythonPlugin();
else
result = (PythonPlugin) pyClass.__call__().__tojava__(PythonPlugin.class);
interp.set("pyplugin", result);
result.hooks = hook;
result.interp = interp;
// Run scripts designed to be run after plugin creation
for (String script : post_plugin_scripts) {
InputStream metastream = this.getClass().getClassLoader().getResourceAsStream("scripts/"+script);
interp.execfile(metastream);
metastream.close();
}
result.initialize(this, server, description, dataFolder, file);
result.setDataFile(data);
} catch (Throwable t) {
if (data.shouldAddPathEntry() && pythonpath.__contains__(filepath)) {
pythonpath.remove(filepath);
}
throw new InvalidPluginException(t);
}
if (data.getNeedsSolidMeta() && !hassolidmeta) {