Beispiel #1
0
    public bool Eval(string code)
    {
        EditorApplication.LockReloadAssemblies();

        bool status    = false,
             hasOutput = false;
        object output  = null;
        string res     = null,
               tmpCode = code.Trim();

//    Debug.Log("Evaluating: " + tmpCode);

        try {
            if (tmpCode.StartsWith("="))
            {
                // Special case handling of calculator mode.  The problem is that
                // expressions involving multiplication are grammatically ambiguous
                // without a var declaration or some other grammatical construct.
                // TODO: Change the prompt in calculator mode.  Needs to be done from Shell.
                tmpCode = "(" + tmpCode.Substring(1, tmpCode.Length - 1) + ");";
            }
            res = Evaluate(tmpCode, out output, out hasOutput);
        } catch (EvaluationException) {
            Debug.LogError(@"Error compiling/executing code.  Please double-check syntax, method/variable names, etc.
You can find more information in Unity's `Editor.log` file (*not* the editor console!).");

            output    = new Evaluator.NoValueSet();
            hasOutput = false;
            res       = tmpCode; // Enable continued editing on syntax errors, etc.
        } catch (Exception e) {
            Debug.LogError(e);

            res = tmpCode; // Enable continued editing on unexpected errors.
        } finally {
            status = res == null;
        }

        if (hasOutput)
        {
            if (status)
            {
                try {
                    StringBuilder sb = new StringBuilder();
                    PrettyPrint.PP(sb, output, true);
                    Debug.Log(sb.ToString());
                } catch (Exception e) {
                    Debug.LogError(e.ToString().Trim());
                }
            }
        }

        EditorApplication.UnlockReloadAssemblies();
        return(status);
    }
Beispiel #2
0
    public bool Eval(List <LogEntry> logEntries, string code)
    {
        EditorApplication.LockReloadAssemblies();

        bool     status    = false;
        bool     hasOutput = false;
        object   output    = null;
        LogEntry cmdEntry  = null;

        string tmpCode = code.Trim();

        cmdEntry = new LogEntry()
        {
            logEntryType = LogEntryType.Command,
            command      = tmpCode
        };

        bool isExpression = false;

        try {
            if (tmpCode.StartsWith("="))
            {
                tmpCode      = "(" + tmpCode.Substring(1, tmpCode.Length - 1) + ");";
                isExpression = true;
            }
            Application.RegisterLogCallback(delegate(string cond, string sTrace, LogType lType) {
                cmdEntry.Add(new LogEntry()
                {
                    logEntryType   = LogEntryType.ConsoleLog,
                    condition      = cond,
                    stackTrace     = sTrace,
                    consoleLogType = lType
                });
            });
            status = Evaluator.Evaluate(tmpCode, out output, out hasOutput) == null;
            if (status)
            {
                logEntries.Add(cmdEntry);
            }
        } catch (Exception e) {
            cmdEntry.Add(new LogEntry()
            {
                logEntryType = LogEntryType.EvaluationError,
                error        = e.ToString().Trim() // TODO: Produce a stack trace a la Debug, and put it in stackTrace so we can filter it.
            });

            output    = new Evaluator.NoValueSet();
            hasOutput = false;
            status    = true; // Need this to avoid 'stickiness' where we let user
                              // continue editing due to incomplete code.
            logEntries.Add(cmdEntry);
        } finally {
            Application.RegisterLogCallback(null);
        }

        // Catch compile errors that are not dismissed as a product of interactive
        // editing by Mono.CSharp.Evaluator...
        StringBuilder buffer = FluffReporter();
        string        tmp    = buffer.ToString().Trim();

        buffer.Length = 0;
        if (!String.IsNullOrEmpty(tmp))
        {
            cmdEntry.Add(new LogEntry()
            {
                logEntryType = LogEntryType.SystemConsole,
                error        = tmp
            });
            status = false;
        }

        if (hasOutput && (isExpression || output is REPLMessage))
        {
            if (status)
            {
                outputBuffer.Length = 0;
                PrettyPrint.PP(outputBuffer, output);
                cmdEntry.Add(new LogEntry()
                {
                    logEntryType = LogEntryType.Output,
                    output       = outputBuffer.ToString().Trim()
                });
            }
        }

        EditorApplication.UnlockReloadAssemblies();
        return(status);
    }
Beispiel #3
0
    public bool Eval(string code)
    {
        EditorApplication.LockReloadAssemblies();

        bool status     = false,
         hasOutput  = false;
        object output   = null;
        string res      = null,
           tmpCode  = code.Trim();
        //    Debug.Log("Evaluating: " + tmpCode);

        try {
          if(tmpCode.StartsWith("=")) {
        // Special case handling of calculator mode.  The problem is that
        // expressions involving multiplication are grammatically ambiguous
        // without a var declaration or some other grammatical construct.
        // TODO: Change the prompt in calculator mode.  Needs to be done from Shell.
        tmpCode = "(" + tmpCode.Substring(1, tmpCode.Length - 1) + ");";
          }
          res = Evaluate(tmpCode, out output, out hasOutput);
        } catch(EvaluationException) {
          Debug.LogError(@"Error compiling/executing code.  Please double-check syntax, method/variable names, etc.
        You can find more information in Unity's `Editor.log` file (*not* the editor console!).");

          output    = new Evaluator.NoValueSet();
          hasOutput = false;
          res       = tmpCode; // Enable continued editing on syntax errors, etc.
        } catch(Exception e) {
          Debug.LogError(e);

          res       = tmpCode; // Enable continued editing on unexpected errors.
        } finally {
          status = res == null;
        }

        if(hasOutput) {
          if(status) {
        try {
          StringBuilder sb = new StringBuilder();
          PrettyPrint.PP(sb, output, true);
          Debug.Log(sb.ToString());
        } catch(Exception e) {
          Debug.LogError(e.ToString().Trim());
        }
          }
        }

        EditorApplication.UnlockReloadAssemblies();
        return status;
    }
Beispiel #4
0
    public bool Eval(List<LogEntry> logEntries, string code)
    {
        EditorApplication.LockReloadAssemblies();

        bool status = false,
         hasOutput = false,
         hasAddedLogToEntries = false;
        object output = null;
        string res = null,
           tmpCode = code.Trim();
        LogEntry cmdEntry = new LogEntry() {
          logEntryType = LogEntryType.Command,
          command = tmpCode
        };

        try {
          FluffReporter();

          if(tmpCode.StartsWith("=")) {
        // Special case handling of calculator mode.  The problem is that
        // expressions involving multiplication are grammatically ambiguous
        // without a var declaration or some other grammatical construct.
        tmpCode = "(" + tmpCode.Substring(1, tmpCode.Length-1) + ");";
          }
          Application.RegisterLogCallback(delegate(string cond, string sTrace, LogType lType) {
        cmdEntry.Add(new LogEntry() {
          logEntryType = LogEntryType.ConsoleLog,
          condition = cond,
          stackTrace = sTrace,
          consoleLogType = lType
        });
          });
          res = Evaluator.Evaluate(tmpCode, out output, out hasOutput);
          //if(res == tmpCode)
          //  Debug.Log("Unfinished input...");
        } catch(Exception e) {
          cmdEntry.Add(new LogEntry() {
        logEntryType = LogEntryType.EvaluationError,
        error = e.ToString().Trim() // TODO: Produce a stack trace a la Debug, and put it in stackTrace so we can filter it.
          });

          output = new Evaluator.NoValueSet();
          hasOutput = false;
          status = true; // Need this to avoid 'stickiness' where we let user
                     // continue editing due to incomplete code.
        } finally {
          status = res == null;
          Application.RegisterLogCallback(null);
          if(res != tmpCode) {
        logEntries.Add(cmdEntry);
        hasAddedLogToEntries = true;
          }
          status = CatchMessages(cmdEntry, status);
        }

        if(hasOutput) {
          if(status) {
        outputBuffer.Length = 0;

        try {
          FluffReporter();
          PrettyPrint.PP(outputBuffer, output, true);
        } catch(Exception e) {
          cmdEntry.Add(new LogEntry() {
            logEntryType = LogEntryType.EvaluationError,
            error = e.ToString().Trim() // TODO: Produce a stack trace a la Debug, and put it in stackTrace so we can filter it.
          });
          if(!hasAddedLogToEntries) {
            logEntries.Add(cmdEntry);
            hasAddedLogToEntries = true;
          }
        } finally {
          bool result = CatchMessages(cmdEntry, true);
          if(!result && !hasAddedLogToEntries) {
            logEntries.Add(cmdEntry);
            hasAddedLogToEntries = true;
          }
        }

        string tmp = outputBuffer.ToString().Trim();
        if(!String.IsNullOrEmpty(tmp)) {
          cmdEntry.Add(new LogEntry() {
            logEntryType = LogEntryType.Output,
            output = outputBuffer.ToString().Trim()
          });
          if(!hasAddedLogToEntries) {
            logEntries.Add(cmdEntry);
            hasAddedLogToEntries = true;
          }
        }
          }
        }

        EditorApplication.UnlockReloadAssemblies();
        return status;
    }
Beispiel #5
0
    public bool Eval(List<LogEntry> logEntries, string code)
    {
        EditorApplication.LockReloadAssemblies();

        bool status = false;
        bool hasOutput = false;
        object output = null;
        LogEntry cmdEntry = null;

        string tmpCode = code.Trim();

        cmdEntry = new LogEntry() {
          logEntryType = LogEntryType.Command,
          command = tmpCode
        };

        bool isExpression = false;
        try {
          if(tmpCode.StartsWith("=")) {
        tmpCode = "(" + tmpCode.Substring(1, tmpCode.Length-1) + ");";
        isExpression = true;
          }
          Application.RegisterLogCallback(delegate(string cond, string sTrace, LogType lType) {
        cmdEntry.Add(new LogEntry() {
          logEntryType = LogEntryType.ConsoleLog,
          condition = cond,
          stackTrace = sTrace,
          consoleLogType = lType
        });
          });
          status = Evaluator.Evaluate(tmpCode, out output, out hasOutput) == null;
          if(status)
        logEntries.Add(cmdEntry);
        } catch(Exception e) {
          cmdEntry.Add(new LogEntry() {
        logEntryType = LogEntryType.EvaluationError,
        error = e.ToString().Trim() // TODO: Produce a stack trace a la Debug, and put it in stackTrace so we can filter it.
          });

          output = new Evaluator.NoValueSet();
          hasOutput = false;
          status = true; // Need this to avoid 'stickiness' where we let user
                     // continue editing due to incomplete code.
          logEntries.Add(cmdEntry);
        } finally {
          Application.RegisterLogCallback(null);
        }

        // Catch compile errors that are not dismissed as a product of interactive
        // editing by Mono.CSharp.Evaluator...
        StringBuilder buffer = FluffReporter();
        string tmp = buffer.ToString().Trim();
        buffer.Length = 0;
        if(!String.IsNullOrEmpty(tmp)) {
          cmdEntry.Add(new LogEntry() {
        logEntryType = LogEntryType.SystemConsole,
        error = tmp
          });
          status = false;
        }

        if(hasOutput && (isExpression || output is REPLMessage)) {
          if(status) {
        outputBuffer.Length = 0;
        PrettyPrint.PP(outputBuffer, output);
        cmdEntry.Add(new LogEntry() {
          logEntryType = LogEntryType.Output,
          output = outputBuffer.ToString().Trim()
        });
          }
        }

        EditorApplication.UnlockReloadAssemblies();
        return status;
    }
Beispiel #6
0
    public bool Eval(string code)
    {
        EditorApplication.LockReloadAssemblies();

        bool status    = false,
         hasOutput = false;
        object output = null;
        string res     = null,
           tmpCode = code.Trim();
        Debug.Log("Evaluating: " + tmpCode);

        try {
          if(tmpCode.StartsWith("=")) {
        // Special case handling of calculator mode.  The problem is that
        // expressions involving multiplication are grammatically ambiguous
        // without a var declaration or some other grammatical construct.
        tmpCode = "(" + tmpCode.Substring(1, tmpCode.Length - 1) + ");";
          }
          res = Evaluator.Evaluate(tmpCode, out output, out hasOutput);
          //if(res == tmpCode)
          //  Debug.Log("Unfinished input...");
        } catch(Exception e) {
          Debug.LogError(e);

          output = new Evaluator.NoValueSet();
          hasOutput = false;
          status = true; // Need this to avoid 'stickiness' where we let user
          // continue editing due to incomplete code.
        } finally {
          status = res == null;
        }

        if(hasOutput) {
          if(status) {
        try {
          StringBuilder sb = new StringBuilder();
          PrettyPrint.PP(sb, output, true);
          Debug.Log(sb.ToString());
        } catch(Exception e) {
          Debug.LogError(e.ToString().Trim());
        }
          }
        }

        EditorApplication.UnlockReloadAssemblies();
        return status;
    }