use of com.laytonsmith.core.Script in project CommandHelper by EngineHub.
the class MSLPMaker method start.
public static void start(String path) throws IOException {
File start = new File(path);
if (!start.exists()) {
StreamUtils.GetSystemErr().println("The specified file does not exist!");
return;
}
File output = new File(start.getParentFile(), start.getName() + ".mslp");
if (output.exists()) {
pl("The file " + output.getName() + " already exists, would you like to overwrite? (Y/N)");
String overwrite = prompt();
if (!overwrite.equalsIgnoreCase("y")) {
return;
}
}
// First attempt to compile it, and make sure it doesn't fail
AliasCore.LocalPackage localPackage = new AliasCore.LocalPackage();
AliasCore.GetAuxAliases(start, localPackage);
boolean error = false;
for (AliasCore.LocalPackage.FileInfo fi : localPackage.getMSFiles()) {
try {
MethodScriptCompiler.compile(MethodScriptCompiler.lex(fi.contents(), fi.file(), true));
} catch (ConfigCompileException e) {
error = true;
ConfigRuntimeException.HandleUncaughtException(e, "Compile error in script. Compilation will attempt to continue, however.", null);
} catch (ConfigCompileGroupException ex) {
error = true;
ConfigRuntimeException.HandleUncaughtException(ex, null);
}
}
List<Script> allScripts = new ArrayList<>();
for (AliasCore.LocalPackage.FileInfo fi : localPackage.getMSAFiles()) {
List<Script> tempScripts;
try {
tempScripts = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(fi.contents(), fi.file(), false));
for (Script s : tempScripts) {
try {
s.compile();
s.checkAmbiguous(allScripts);
allScripts.add(s);
} catch (ConfigCompileException e) {
error = true;
ConfigRuntimeException.HandleUncaughtException(e, "Compile error in script. Compilation will attempt to continue, however.", null);
} catch (ConfigCompileGroupException e) {
error = true;
ConfigRuntimeException.HandleUncaughtException(e, "Compile errors in script. Compilation will attempt to continue, however.", null);
}
}
} catch (ConfigCompileException e) {
error = true;
ConfigRuntimeException.HandleUncaughtException(e, "Could not compile file " + fi.file() + " compilation will halt.", null);
}
}
if (!error) {
ZipMaker.MakeZip(start, output.getName());
pl(GREEN + "The MSLP file has been created at " + output.getAbsolutePath() + reset());
} else {
pl(RED + "MSLP file has not been created due to compile errors. Correct the errors, and try again." + reset());
}
}
use of com.laytonsmith.core.Script in project CommandHelper by EngineHub.
the class StaticTest method RunCommand.
public static void RunCommand(String combinedScript, MCCommandSender player, String command, Environment env) throws Exception {
InstallFakeServerFrontend();
if (env == null) {
env = StaticTest.env;
}
env.getEnv(CommandHelperEnvironment.class).SetCommandSender(player);
List<Script> scripts = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(combinedScript, null, false));
for (Script s : scripts) {
s.compile();
if (s.match(command)) {
s.run(s.getVariables(command), env, null);
}
}
}
use of com.laytonsmith.core.Script in project CommandHelper by EngineHub.
the class ExampleScript method getOutput.
public String getOutput() throws IOException, DataSourceException, URISyntaxException {
if (output != null) {
return output;
}
Script s = Script.GenerateScript(script, Static.GLOBAL_PERMISSION);
Environment env;
try {
env = Static.GenerateStandaloneEnvironment();
} catch (Profiles.InvalidProfileException ex) {
throw new RuntimeException(ex);
}
Class[] interfaces = new Class[] { MCPlayer.class };
MCPlayer p = (MCPlayer) Proxy.newProxyInstance(ExampleScript.class.getClassLoader(), interfaces, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("getName") || method.getName().equals("getDisplayName")) {
return "Player";
}
if (method.getName().equals("sendMessage")) {
playerOutput.append(args[0].toString()).append("\n");
}
if (method.getName().equals("isOnline")) {
return true;
}
return genericReturn(method.getReturnType());
}
});
// TODO: Remove this dependency. Make MCPlayer implement a generic "User" and make that
// part of the GlobalEnv.
env.getEnv(CommandHelperEnvironment.class).SetPlayer(p);
final StringBuilder finalOutput = new StringBuilder();
String thrown = null;
try {
List<Variable> vars = new ArrayList<>();
try {
MethodScriptCompiler.execute(originalScript, new File("/" + functionName + ".ms"), true, env, new MethodScriptComplete() {
@Override
public void done(String output) {
if (output != null) {
finalOutput.append(output);
}
}
}, null, vars);
} catch (ConfigCompileException | ConfigCompileGroupException ex) {
// We already checked for compile errors, so this won't happen
}
} catch (ConfigRuntimeException e) {
String name = e.getClass().getName();
if (e instanceof AbstractCREException) {
name = ((AbstractCREException) e).getName();
}
thrown = "\n(Throws " + name + ": " + e.getMessage() + ")";
}
String playerOut = playerOutput.toString().trim();
String finalOut = finalOutput.toString().trim();
String out = (playerOut.isEmpty() ? "" : playerOut) + (finalOut.isEmpty() || !playerOut.trim().isEmpty() ? "" : ":" + finalOut);
if (thrown != null) {
out += thrown;
}
return out;
}
Aggregations