Search in sources :

Example 1 with Script

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());
    }
}
Also used : Script(com.laytonsmith.core.Script) AliasCore(com.laytonsmith.core.AliasCore) ArrayList(java.util.ArrayList) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) File(java.io.File) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException)

Example 2 with Script

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);
        }
    }
}
Also used : Script(com.laytonsmith.core.Script) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment)

Example 3 with Script

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;
}
Also used : Variable(com.laytonsmith.core.constructs.Variable) MCPlayer(com.laytonsmith.abstraction.MCPlayer) ArrayList(java.util.ArrayList) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) Profiles(com.laytonsmith.core.Profiles) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Script(com.laytonsmith.core.Script) AbstractCREException(com.laytonsmith.core.exceptions.CRE.AbstractCREException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) MethodScriptComplete(com.laytonsmith.core.MethodScriptComplete) Environment(com.laytonsmith.core.environments.Environment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) File(java.io.File)

Aggregations

Script (com.laytonsmith.core.Script)3 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)2 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)2 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 MCPlayer (com.laytonsmith.abstraction.MCPlayer)1 AliasCore (com.laytonsmith.core.AliasCore)1 MethodScriptComplete (com.laytonsmith.core.MethodScriptComplete)1 Profiles (com.laytonsmith.core.Profiles)1 Variable (com.laytonsmith.core.constructs.Variable)1 Environment (com.laytonsmith.core.environments.Environment)1 AbstractCREException (com.laytonsmith.core.exceptions.CRE.AbstractCREException)1 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1