This repository has been archived on 2021-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
scriptcraft/src/main/javascript/core/_scriptcraft.js

404 lines
14 KiB
JavaScript
Raw Normal View History

/*
This file defines commonly used functions by all plugins...
load (filename) - loads and evaluates a javascript file, returning the evaluated object.
save (object, filename) - saves an object to a file.
2013-01-18 00:28:12 +01:00
plugin (name, interface, isPersistent)
- defines a new plugin. If isPersistent is true then
the plugin doesn't have to worry about loading and saving
2013-01-23 01:02:57 +01:00
state - that will be done by the framework. Just make sure
that anything you want to save (and restore) is in the 'store'
property - this will be created automatically if not already defined.
(its type is object {} )
ready (function) - specifies code to be executed only when all the plugins have loaded.
command (name, function) - defines a command that can be used by non-operators.
*/
2013-01-08 00:59:42 +01:00
var global = this;
2013-01-18 00:39:43 +01:00
var verbose = verbose || false;
2013-01-25 00:47:36 +01:00
/*
wph 20130124 - make self, plugin and bukkit public - these are far more useful now that tab-complete works.
*/
var bukkit = org.bukkit.Bukkit;
2013-01-08 00:59:42 +01:00
//
// private implementation
//
(function(){
2013-01-13 22:06:46 +01:00
//
// don't execute this more than once
//
if (typeof load == "function")
return ;
var _canonize = function(file){ return file.getCanonicalPath().replaceAll("\\\\","/"); };
2013-01-13 22:06:46 +01:00
var _originalScript = __script;
2013-01-18 00:28:12 +01:00
var parentFileObj = new java.io.File(__script).getParentFile();
var jsPluginsRootDir = parentFileObj.getParentFile();
var jsPluginsRootDirName = _canonize(jsPluginsRootDir);
var _loaded = {};
2013-01-18 00:28:12 +01:00
/*
Load the contents of the file and evaluate as javascript
*/
var _load = function(filename,warnOnFileNotFound)
2013-01-18 00:28:12 +01:00
{
var result = null;
var file = new java.io.File(filename);
2013-01-18 00:28:12 +01:00
var canonizedFilename = _canonize(file);
//
// wph 20130123 don't load the same file more than once.
//
if (_loaded[canonizedFilename])
return;
2013-01-18 00:28:12 +01:00
if (verbose)
print("loading " + canonizedFilename);
2013-01-13 22:06:46 +01:00
if (file.exists()){
var parent = file.getParentFile();
var reader = new java.io.FileReader(file);
__engine.put("__script",canonizedFilename);
2013-01-13 22:06:46 +01:00
__engine.put("__folder",(parent?_canonize(parent):"")+"/");
try{
result = __engine.eval(reader);
_loaded[canonizedFilename] = true;
}catch (e){
__plugin.logger.severe("Error evaluating " + filename + ", " + e );
}
2013-01-13 22:06:46 +01:00
}else{
2013-01-23 01:02:57 +01:00
if (warnOnFileNotFound)
__plugin.logger.warning(canonizedFilename + " not found");
2013-01-13 22:06:46 +01:00
}
2013-01-18 00:28:12 +01:00
return result;
2013-01-13 22:06:46 +01:00
};
2013-01-18 00:28:12 +01:00
/*
recursively walk the given directory and return a list of all .js files
*/
2013-01-13 22:06:46 +01:00
var _listJsFiles = function(store,dir)
{
if (typeof dir == "undefined"){
dir = new java.io.File(_originalScript).getParentFile().getParentFile();
2013-01-13 22:06:46 +01:00
}
var files = dir.listFiles();
for (var i = 0;i < files.length; i++){
2013-01-18 00:28:12 +01:00
var file = files[i];
if (file.isDirectory()){
_listJsFiles(store,file);
2013-01-13 22:06:46 +01:00
}else{
if (file.getCanonicalPath().endsWith(".js") &&
!(file.getName().startsWith("_")) &&
2013-01-18 00:28:12 +01:00
file.exists())
2013-01-13 22:06:46 +01:00
{
store.push(file);
2013-01-13 22:06:46 +01:00
}
}
}
};
2013-01-18 00:28:12 +01:00
/*
Reload all of the .js files in the given directory
*/
2013-01-13 22:06:46 +01:00
var _reload = function(pluginDir)
{
_loaded = [];
2013-01-13 22:06:46 +01:00
var jsFiles = [];
_listJsFiles(jsFiles,pluginDir);
//
// script files whose name begins with _ (underscore)
// will not be loaded automatically at startup.
// These files are assumed to be dependencies/private to plugins
//
// E.g. If you have a plugin called myMiniGame.js in the myMiniGame directory
// and which in addition to myMiniGame.js also includes _myMiniGame_currency.js _myMiniGame_events.js etc.
// then it's assumed that _myMiniGame_currency.js and _myMiniGame_events.js will be loaded
// as dependencies by myMiniGame.js and do not need to be loaded via js reload
//
var len = jsFiles.length;
for (var i = 0;i < len; i++){
2013-01-23 01:02:57 +01:00
load(_canonize(jsFiles[i]),true);
2013-01-13 22:06:46 +01:00
}
};
2013-01-18 00:28:12 +01:00
/*
Save a javascript object to a file (saves using JSON notation)
*/
var _save = function(object, filename){
print(filename);
2013-01-23 01:02:57 +01:00
var objectToStr = null;
try{
objectToStr = JSON.stringify(object);
}catch(e){
print("ERROR: " + e.getMessage() + " while saving " + filename);
return;
}
2013-01-18 00:28:12 +01:00
var f = new java.io.File(filename);
var out = new java.io.PrintWriter(new java.io.FileWriter(f));
out.println("__data = " + objectToStr);
out.close();
};
/*
plugin management
*/
var _plugins = {};
var _plugin = function(/* String */ moduleName, /* Object */ moduleObject, isPersistent)
{
//
// don't load plugin more than once
//
if (typeof _plugins[moduleName] != "undefined")
return;
2013-01-18 00:28:12 +01:00
var pluginData = {persistent: isPersistent, module: moduleObject};
moduleObject.store = moduleObject.store || {};
_plugins[moduleName] = pluginData;
2013-01-18 00:28:12 +01:00
if (isPersistent)
moduleObject.store = load(jsPluginsRootDirName + "/" + moduleName + "-store.txt") || {};
global[moduleName] = moduleObject;
return moduleObject;
};
/*
allow for deferred execution (once all modules have loaded)
*/
var _deferred = [];
var _ready = function( func ){
_deferred.push(func);
};
2013-01-23 01:02:57 +01:00
var _cmdInterceptors = [];
2013-01-18 00:28:12 +01:00
/*
command management - allow for non-ops to execute approved javascript code.
*/
var _commands = {};
var _command = function(name,func,options,intercepts){
2013-01-18 00:28:12 +01:00
if (typeof name == "undefined"){
// it's an invocation from the Java Plugin!
if (__cmdArgs.length === 0)
throw new Error("Usage: jsp command-name command-parameters");
var name = __cmdArgs[0];
2013-01-23 01:02:57 +01:00
var cmd = _commands[name];
if (typeof cmd === "undefined"){
2013-01-23 01:02:57 +01:00
// it's not a global command - pass it on to interceptors
var intercepted = false;
for (var i = 0;i < _cmdInterceptors.length;i++){
if (_cmdInterceptors[i](__cmdArgs))
intercepted = true;
}
if (!intercepted)
2013-01-25 00:47:36 +01:00
self.sendMessage("Command '" + name + "' is not recognised");
2013-01-23 01:02:57 +01:00
}else{
func = cmd.callback;
var params = [];
for (var i =1; i < __cmdArgs.length;i++){
params.push("" + __cmdArgs[i]);
}
2013-01-18 00:28:12 +01:00
return func(params);
2013-01-23 01:02:57 +01:00
}
2013-01-18 00:28:12 +01:00
}else{
2013-01-23 01:02:57 +01:00
if (typeof options == "undefined")
options = [];
_commands[name] = {callback: func, options: options};
2013-01-23 01:02:57 +01:00
if (intercepts)
_cmdInterceptors.push(func);
2013-01-18 00:28:12 +01:00
return func;
}
};
2013-01-23 01:02:57 +01:00
var _rmCommand = function(name){
delete _commands[name];
};
2013-01-18 00:28:12 +01:00
/*
Tab Completion of the /js and /jsp commands
*/
2013-01-13 22:06:46 +01:00
var _isJavaObject = function(o){
var result = false;
try {
o.hasOwnProperty("testForJava");
}catch (e){
// java will throw an error when an attempt is made to access the
// hasOwnProperty method. (it won't exist for Java objects)
result = true;
}
return result;
};
var _javaLangObjectMethods = ["equals","getClass","class","getClass","hashCode","notify","notifyAll","toString","wait","clone","finalize"];
var _getProperties = function(o)
{
var result = [];
if (_isJavaObject(o))
2013-01-18 00:28:12 +01:00
{
2013-01-13 22:06:46 +01:00
propertyLoop:
for (var i in o)
2013-01-18 00:28:12 +01:00
{
2013-01-13 22:06:46 +01:00
//
// don't include standard Object methods
//
var isObjectMethod = false;
for (var j = 0;j < _javaLangObjectMethods.length; j++)
if (_javaLangObjectMethods[j] == i)
continue propertyLoop;
if (typeof o[i] == "function")
result.push(i+"()");
else
result.push(i);
}
}else{
for (var i in o){
if (!o.hasOwnProperty(i))
continue;
if (i.match(/^[^_]/)){
if (typeof o[i] == "function")
result.push(i+"()");
else
result.push(i);
}
}
}
return result.sort();
};
2013-01-18 00:28:12 +01:00
/*
Tab completion for the /jsp commmand
*/
var __onTabCompleteJSP = function() {
var result = global.__onTC_result;
var args = global.__onTC_args;
2013-01-23 01:02:57 +01:00
var cmd = _commands[args[0]];
if (cmd)
for (var i = 0;i < cmd.options.length; i++)
result.add(cmd.options[i]);
else
for (var i in _commands)
result.add(i);
2013-01-18 00:28:12 +01:00
return result;
};
/*
Tab completion for the /js command
*/
var __onTabCompleteJS = function()
2013-01-13 22:06:46 +01:00
{
2013-01-18 00:28:12 +01:00
if (__onTC_cmd.name == "jsp")
return __onTabCompleteJSP()
2013-01-13 22:06:46 +01:00
var _globalSymbols = _getProperties(global)
var result = global.__onTC_result;
var args = global.__onTC_args;
var propsOfLastArg = [];
var statement = args.join(" ");
2013-01-18 00:28:12 +01:00
statement = statement.replace(/^\s+/,"").replace(/\s+$/,"");
if (statement.length == 0)
2013-01-18 00:28:12 +01:00
propsOfLastArg = _globalSymbols;
else{
2013-01-13 22:06:46 +01:00
var statementSyms = statement.split(/[^a-zA-Z0-9_\.]/);
var lastSymbol = statementSyms[statementSyms.length-1];
//
// try to complete the object ala java IDEs.
//
var parts = lastSymbol.split(/\./);
var name = parts[0];
var symbol = global[name];
var lastGoodSymbol = symbol;
if (typeof symbol != "undefined")
{
for (var i = 1; i < parts.length;i++){
name = parts[i];
symbol = symbol[name];
if (typeof symbol == "undefined")
break;
lastGoodSymbol = symbol;
}
if (typeof symbol == "undefined"){
2013-01-18 00:28:12 +01:00
//
2013-01-13 22:06:46 +01:00
// look up partial matches against last good symbol
//
var objectProps = _getProperties(lastGoodSymbol);
if (name == ""){
2013-01-18 00:28:12 +01:00
// if the last symbol looks like this..
// ScriptCraft.
//
2013-01-13 22:06:46 +01:00
for (var i =0;i < objectProps.length;i++)
propsOfLastArg.push(statement+objectProps[i]);
2013-01-18 00:28:12 +01:00
}else{
// it looks like this..
// ScriptCraft.co
//
var li = statement.lastIndexOf(name);
statement = statement.substring(0,li);
2013-01-08 00:59:42 +01:00
for (var i = 0; i < objectProps.length;i++)
if (objectProps[i].indexOf(name) == 0)
2013-01-13 22:06:46 +01:00
propsOfLastArg.push(statement + objectProps[i]);
2013-01-18 00:28:12 +01:00
2013-01-13 22:06:46 +01:00
}
}else{
var objectProps = _getProperties(symbol);
for (var i = 0; i < objectProps.length; i++)
2013-01-13 22:06:46 +01:00
propsOfLastArg.push(statement + objectProps[i]);
}
}else{
// loop thru globalSymbols looking for a good match
for (var i = 0;i < _globalSymbols.length; i++)
if (_globalSymbols[i].indexOf(lastSymbol) == 0)
2013-01-13 22:06:46 +01:00
propsOfLastArg.push(statement.replace(lastSymbol,_globalSymbols[i]));
2013-01-18 00:28:12 +01:00
2013-01-13 22:06:46 +01:00
}
}
for (var i = 0;i < propsOfLastArg.length; i++)
2013-01-13 22:06:46 +01:00
result.add(propsOfLastArg[i]);
};
2013-01-18 00:28:12 +01:00
/*
utility function - convert a Location to a string
*/
var _locToString = function(location){
return JSON.stringify([""+location.world.name,location.x, location.y, location.z]);
};
2013-01-23 01:02:57 +01:00
var _getPlayerObject = function(player){
if (typeof player == "undefined")
2013-01-25 00:47:36 +01:00
return self;
2013-01-23 01:02:57 +01:00
if (typeof player == "string")
return org.bukkit.Bukkit.getPlayer(player);
return player;
};
2013-01-18 00:28:12 +01:00
global.load = _load;
global.save = _save;
global.plugin = _plugin;
global.ready = _ready;
global.command = _command;
global._onTabComplete = __onTabCompleteJS;
2013-01-18 00:28:12 +01:00
global.locationToString = _locToString;
2013-01-23 01:02:57 +01:00
global.getPlayerObject = _getPlayerObject;
2013-01-13 22:06:46 +01:00
//
// assumes this was loaded from js-plugins/core/
2013-01-18 00:28:12 +01:00
// load all of the plugins.
2013-01-13 22:06:46 +01:00
//
2013-01-24 21:22:22 +01:00
_reload(jsPluginsRootDir);
2013-01-13 22:06:46 +01:00
2013-01-18 00:28:12 +01:00
//
// all modules have loaded
//
for (var i =0;i < _deferred.length;i++)
_deferred[i]();
events.on("server.PluginDisableEvent",function(l,e){
//
// save all plugins which have persistent data
//
for (var moduleName in _plugins){
var pluginData = _plugins[moduleName];
if (pluginData.persistent)
save(pluginData.module.store, jsPluginsRootDirName + "/" + moduleName + "-store.txt");
}
});
2013-01-13 22:06:46 +01:00
}());
2013-01-08 00:59:42 +01:00