void CheckIfUsingsAreDereferencedInEvaluatorOnRemove()
        {
            _engine.RemoveUsings("using UnityEngine.Advertisements; using UCompile; using System.CodeDom;using UnityEngine;");

            _engine.CompileCode(@"Debug.Log(""Success"");");

            Assert.IsFalse(_lastCompilationSucceeded);

            _engine.AddUsings("using UnityEngine;");

            _engine.CompileCode(@"Debug.Log(""Success"");");

            Assert.IsTrue(_lastCompilationSucceeded);
        }
Exemplo n.º 2
0
    //-------------------------------------------------------------------------------------------------

    //add using directives to CSScriptEngine, to control what is exposed to dynamic code
    void AddUsings()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddOnCompilationFailedHandler(OnCompilationFailedAction);

        //There's no usings right now in system, so nothing, apart from basic types is accesible to dynamic code
        engine.CompileCode("Debug.Log(\"This will result in an error, because Debug is a part of UnityEngine namespace\");");

        //Here we add using UnityEngine to system, so everything in this namespace is accessible now
        engine.AddUsings("using UnityEngine;");

        engine.CompileCode("Debug.Log(\"Now compilation of this code will succeed\");").Execute();

        engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction);
    }
Exemplo n.º 3
0
    //-------------------------------------------------------------------------------------------------

    //Compile custom type, and then invoke it's method via CompileCode
    void CompileType()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddUsings("using UnityEngine;");

        string typeCode = @"
                
                                public class SimpleType
                                {
                                    public void PrintHey()
                                    {
                                        Debug.Log(""Hey!"");
                                    }
                                }

                              ";

        engine.CompileType("SimpleType", typeCode);

        IScript result = engine.CompileCode(@"
                                                 SimpleType sm = new SimpleType();sm.PrintHey();
                                               ");

        result.Execute();
    }
Exemplo n.º 4
0
    //-------------------------------------------------------------------------------------------------

    //Removes usings from system, so it's namespace resources become inaccessible for dynamic code
    void RemoveUsings()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddOnCompilationFailedHandler(OnCompilationFailedAction);

        engine.AddUsings("using SomeCustomNamespace;");

        engine.CompileCode("HeyPrinter hp = new HeyPrinter(); hp.PrintHey();").Execute();

        engine.RemoveUsings("using SomeCustomNamespace;");

        //Now this will result in error
        engine.CompileCode("HeyPrinter hp = new HeyPrinter(); hp.PrintHey();");

        engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction);
    }
Exemplo n.º 5
0
    //-------------------------------------------------------------------------------------------------

    //Add using of custom namespace, this way you can restrict access of resources to only your custom namespace
    void AddCustomNamespaceUsings()
    {
        CSScriptEngine engine = new CSScriptEngine();

        //Here we add using UnityEngine to system, so everything in this namespace is accessible now
        engine.AddUsings("using SomeCustomNamespace;");

        engine.CompileCode("HeyPrinter hp = new HeyPrinter(); hp.PrintHey();").Execute();
    }
Exemplo n.º 6
0
    //-------------------------------------------------------------------------------------------------

    //Compile simple methodless code
    void CompileCode()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddUsings("using UnityEngine;");

        IScript result = engine.CompileCode(@"Debug.Log(""Hey!"");");

        result.Execute();
    }
Exemplo n.º 7
0
    //-------------------------------------------------------------------------------------------------

    //Here we determine which method is going to automatically be invoked when compilation ends without errors
    void AddCompilationSucceededHandler()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddOnCompilationSucceededHandler(OnCompilationSucceededAction);

        engine.CompileCode("string warningCauser = \"This will result in warning, but compillation succeeds\";");

        engine.RemoveOnCompilationSucceededHandler(OnCompilationSucceededAction);
    }
Exemplo n.º 8
0
    //-------------------------------------------------------------------------------------------------

    //Here we determine which method is going to automatically be invoked when compilation ends with errors
    void AddCompilationFailedHandler()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddOnCompilationFailedHandler(OnCompilationFailedAction);

        engine.CompileCode("This will result in error");

        engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction);
    }
Exemplo n.º 9
0
    void CompileCodeTest()
    {
        //Setup
        string code =
            @"
                GameObject gob = new GameObject(""DynamicallyCreatedGO"");
            ";

        _engine.AddUsings("using UnityEngine;");

        //Action
        _engine.CompileCode(code).Execute();

        //Assert
        GameObject go = GameObject.Find("DynamicallyCreatedGO");

        Assert.IsTrue(go != null);

        //TearDown
        Destroy(go);
    }
Exemplo n.º 10
0
    //-------------------------------------------------------------------------------------------------

    //Remove compiled type from system, now this type is inaccessible to dynamic code
    void RemoveType()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddUsings("using UnityEngine;");

        engine.AddOnCompilationFailedHandler(OnCompilationFailedAction);

        string typeCode = @"
                
                                public class SimpleType
                                {
                                    public void PrintHey()
                                    {
                                        Debug.Log(""Hey!"");
                                    }
                                }

                              ";

        engine.CompileType("SimpleType", typeCode);

        string anotherTypeCode = @"
                
                                public class AnotherSimpleType
                                {
                                    public void InvokeSimpleTypesPrintHey()
                                    {
                                        Debug.Log(""Greetings from AnotherSimpleType! Invoking SimpleTypes PrintHey method..."");
                                        SimpleType sm = new SimpleType(); sm.PrintHey();
                                    }
                                }

                              ";

        engine.CompileType("AnotherSimpleType", anotherTypeCode);

        engine.RemoveTypes("AnotherSimpleType");

        //This will cause a compilation error, beacause we removed AnotherSimpleType
        engine.CompileCode(@"
                                                 AnotherSimpleType sm = new AnotherSimpleType(); 
                                               ");

        engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction);
    }
Exemplo n.º 11
0
    //-------------------------------------------------------------------------------------------------

    //Compile custom type, and then compile another type, using previously compiled type in its code, and then
    //execute last compiled types method via CompileCode
    void CompileMultipleTypes()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddUsings("using UnityEngine;");

        string typeCode = @"
                
                                public class SimpleType
                                {
                                    public void PrintHey()
                                    {
                                        Debug.Log(""Hey!"");
                                    }
                                }

                              ";

        engine.CompileType("SimpleType", typeCode);

        string anotherTypeCode = @"
                
                                public class AnotherSimpleType
                                {
                                    public void InvokeSimpleTypesPrintHey()
                                    {
                                        Debug.Log(""Greetings from AnotherSimpleType! Invoking SimpleTypes PrintHey method..."");
                                        SimpleType sm = new SimpleType(); sm.PrintHey();
                                    }
                                }

                              ";

        engine.CompileType("AnotherSimpleType", anotherTypeCode);

        IScript result = engine.CompileCode(@"
                                                 AnotherSimpleType sm = new AnotherSimpleType();sm.InvokeSimpleTypesPrintHey();
                                               ");

        result.Execute();
    }
 public void OnCompileButton(CompilationEvent cmpEv)
 {
     if (!Remote)
     {
         if (_currentlyCompilingCode)
         {
             _lastCompiledScript = _engine.CompileCode(cmpEv.Code);
         }
         else if (_currentlyCompilingCoroutine)
         {
             _lastCompiledCoroutine = _engine.CompileCoroutine(cmpEv.Code);
         }
     }
     else
     {
         if (_currentlyCompilingCode)
         {
             _engineRemote.CompileCode(cmpEv.Code);
         }
     }
 }