Added loadJsFile() for use at startup only

This commit is contained in:
walterhiggins 2013-01-06 20:03:54 +00:00
parent e597649db5
commit 8dc32f46c9

View file

@ -84,37 +84,9 @@ public class ScriptCraftEvaluator
} }
return result; return result;
} }
protected static Object loadJsFile(File scriptFile,Context cx, Scriptable thisObj)
/**
* Load a javascript source file and evaluate its contents.
*/
public static Object load(Context cx, Scriptable thisObj, Object[] args, Function funObj)
{ {
if (ScriptCraftEvaluator.invoker instanceof Player){
ScriptCraftEvaluator.sc.notifyAdministrators(invoker.toString() + " tried to load a script.\n" +
"Only the console user can load scripts.\n" +
"Players cannot load scripts.");
return null;
}
Object result = null; Object result = null;
File scriptFile = null;
String filename = null;
if (args.length == 0)
{
JFileChooser fc = new javax.swing.JFileChooser();
int rc = fc.showOpenDialog(null);
if (rc ==JFileChooser.APPROVE_OPTION){
scriptFile = fc.getSelectedFile();
}else{
return result;
}
}else{
scriptFile = new File((String)args[0]);
}
FileReader in = null; FileReader in = null;
try { try {
in = new FileReader(scriptFile); in = new FileReader(scriptFile);
@ -122,16 +94,19 @@ public class ScriptCraftEvaluator
catch (FileNotFoundException ex) catch (FileNotFoundException ex)
{ {
ex.printStackTrace(System.err); ex.printStackTrace(System.err);
ScriptCraftEvaluator.sc.notifyAdministrators( "Error - File not found " + args[0]);
Context.reportError("Couldn't open file \"" + scriptFile + "\".");
return null; return null;
} }
filename = scriptFile.getAbsolutePath(); String filename = null;
System.out.println("ScripCraftEvaluator: filename=" + filename);
File parentFile = scriptFile.getParentFile();
String filedir = null; String filedir = null;
File parentFile = null;
try{
filename = scriptFile.getCanonicalPath().replaceAll("\\\\","/");
parentFile = scriptFile.getParentFile();
if (parentFile !=null){ if (parentFile !=null){
filedir = parentFile.getAbsolutePath(); filedir = parentFile.getCanonicalPath().replaceAll("\\\\","/");
}
}catch(IOException ioe){
ioe.printStackTrace(System.err);
} }
// //
// setup the special script-context-only variables // setup the special script-context-only variables
@ -143,9 +118,8 @@ public class ScriptCraftEvaluator
// Here we evalute the entire contents of the file as // Here we evalute the entire contents of the file as
// a script. Text is printed only if the print() function // a script. Text is printed only if the print() function
// is called. // is called.
ScriptCraftEvaluator.sc.notifyAdministrators( "Loading " + filename);
result = cx.evaluateReader(thisObj, in, filename, 1, null); result = cx.evaluateReader(thisObj, in, filename, 1, null);
ScriptCraftEvaluator.sc.notifyAdministrators( "Successfully loaded " + filename);
} }
catch (WrappedException we) { catch (WrappedException we) {
we.printStackTrace(System.err); we.printStackTrace(System.err);
@ -179,6 +153,39 @@ public class ScriptCraftEvaluator
} }
return result; return result;
} }
/**
* Load a javascript source file and evaluate its contents.
*/
public static Object load(Context cx, Scriptable thisObj, Object[] args, Function funObj)
{
if (ScriptCraftEvaluator.invoker instanceof Player){
ScriptCraftEvaluator.sc.notifyAdministrators(invoker.toString() + " tried to load a script.\n" +
"Only the console user can load scripts.\n" +
"Players cannot load scripts.");
return null;
}
Object result = null;
File scriptFile = null;
if (args.length == 0)
{
JFileChooser fc = new javax.swing.JFileChooser();
int rc = fc.showOpenDialog(null);
if (rc ==JFileChooser.APPROVE_OPTION){
scriptFile = fc.getSelectedFile();
}else{
return result;
}
}else{
scriptFile = new File((String)args[0]);
}
result = loadJsFile(scriptFile,cx,thisObj);
return result;
}
public static void help(Context cx, Scriptable thisObj, Object[] args, Function funObj) public static void help(Context cx, Scriptable thisObj, Object[] args, Function funObj)
{ {
String cwd = java.lang.System.getProperty("user.dir"); String cwd = java.lang.System.getProperty("user.dir");