Exemple #1
0
        /// <summary>
        /// Called by Unity.
        /// </summary>
        public void Start()
        {
            // Create domain
            domain = ScriptDomain.CreateDomain("Example Domain");


            // Compile and load code - Note that we use 'CompileAndLoadMainSource' which is the same as 'CompileAndLoadSource' but returns the main type in the compiled assembly
            ScriptType type = domain.CompileAndLoadMainSource(sourceCode, ScriptSecurityMode.UseSettings);


            // Get the property value from a property with the name 'ExampleProperty' and cast it to a string value
            // Note that only static properties can be accessed via a 'ScriptType' instance. A 'ScriptProxy' is required for instance properties
            string propertyString = (string)type.PropertiesStatic["ExampleProperty"];

            // Check that the string we read has the expected value
            Debug.Log(propertyString == "Hello World");

            // Set a property value with the name 'ExampleProperty'
            // Note that this will throw an 'TargetException' because the specified property does not define a 'set' accessor
            type.PropertiesStatic["ExampleProperty"] = "Goodbye World";


            // The safe version will handle any exceptions thrown when trying to access the property and return null if that is the case.
            propertyString = (string)type.SafePropertiesStatic["ExampleProperty"];

            type.SafePropertiesStatic["ExampleProperty"] = propertyString;
        }
    public void ExecuteCode()
    {
        /*try
         * {*/
        robotManagementCode = GetRobotManagementClass();
        ScriptDomain         domain = ScriptDomain.CreateDomain("MyDomain");
        ScriptType           type   = domain.CompileAndLoadMainSource(robotManagementCode);
        ScriptProxy          proxy  = type.CreateInstance(robot);
        Tuple <bool, string> result = (Tuple <bool, string>)proxy.Call("isTaskCompleted");

        if (result.Item1)
        {
            UI.ResultField.text = "<color=green>Задание выполнено!</color>";
            Canvas.GetComponent <TaskCompletingActions>().MakeActions(taskNumber);
            UI.CloseTaskButton.transform.localScale = new Vector3(0, 0, 0);
        }
        else
        {
            UI.ResultField.text = "Есть ошибки. Попробуй ещё раз!";
        }
        UI.OutputField.text = result.Item2;
        //}

        /*catch
         * {
         *  Debug.Log("Exception!!!");
         *  UI.ResultField.text = "Есть ошибки. Попробуй ещё раз!";
         * }*/
    }
        /// <summary>
        /// Called by Unity.
        /// </summary>
        public void Start()
        {
            // Create domain
            domain = ScriptDomain.CreateDomain("Example Domain");


            // Compile and load code - Note that we use 'CompileAndLoadMainSource' which is the same as 'CompileAndLoadSource' but returns the main type in the compiled assembly
            ScriptType type = domain.CompileAndLoadMainSource(sourceCode, ScriptSecurityMode.UseSettings);

            // Create an instance of 'Example'
            ScriptProxy proxy = type.CreateInstance();

            // Create an instance of 'Example' using the overload constructor
            proxy = type.CreateInstance();


            // Call the method called 'ExampleMethod' and pass the string argument 'World'
            // Note that any exceptions thrown by the target method will not be caught
            proxy.Call("ExampleMethod", "World");


            // Call the method called 'ExampleMethod' and pass the string argument 'Safe World'
            // Note that any exceptions thrown by the target method will handled as indicated by the 'Safe' name
            proxy.SafeCall("ExampleMethod", "Safe World");
        }
Exemple #4
0
        /// <summary>
        /// Called by Unity.
        /// </summary>
        public void Start()
        {
            // Create domain
            domain = ScriptDomain.CreateDomain("Example Domain");


            // Compile and load code - Note that we use 'CompileAndLoadMainSource' which is the same as 'CompileAndLoadSource' but returns the main type in the compiled assembly
            ScriptType type = domain.CompileAndLoadMainSource(sourceCode, ScriptSecurityMode.UseSettings);


            // Get the field value from a field with the name 'exampleField' and cast it to a string value
            // Note that only static fields can be accessed via a 'ScriptType' instance. A 'ScriptProxy' is required for instance fields
            string fieldString = (string)type.FieldsStatic["exampleField"];

            // Check that the string we read has the expected value
            Debug.Log(fieldString == "Hello World");

            // Set a field value with the name 'exampleField'
            // An exception may occur if the assigned type canot be implicitly converted to the actual field type
            type.FieldsStatic["exampleField"] = "Goodbye World";


            // The safe version will handle any exceptions thrown when trying to access the field and return null if that is the case.
            fieldString = (string)type.SafeFieldsStatic["exampleField"];

            type.SafeFieldsStatic["exampleField"] = fieldString;
        }
Exemple #5
0
    public bool UpdateScript(Ship ship, StarSystem starSystem, string code)
    {
        if (domain != null)
        {
            domain.Dispose();
        }

        domain = ScriptDomain.CreateDomain("ShipDomain", true);

        //domain.RoslynCompilerService.ReferenceAssemblies.Add(typeof(UnityEngine.Object).Assembly);
        //domain.RoslynCompilerService.ReferenceAssemblies.Add(typeof(Ship).Assembly);
        //domain.RoslynCompilerService.ReferenceAssemblies.Add(typeof(StarSystem).Assembly);
        //domain.RoslynCompilerService.ReferenceAssemblies.Add(typeof(LandableObject).Assembly);

        type = domain.CompileAndLoadMainSource(@code);

        if (proxy != null && !proxy.IsDisposed)
        {
            Debug.Log(proxy.IsDisposed);
            proxy.Dispose();
        }

        Debug.Log(gameObject.name);
        Debug.Log(type.Name);

        proxy = type.CreateInstance(gameObject);
        Debug.Log(gameObject.name);

        if (type != null)
        {
            proxy.Fields["ship"]       = ship;
            proxy.Fields["starSystem"] = starSystem;
            return(true);
        }
        else
        {
            return(false);
        }
    }
Exemple #6
0
    // Start is called before the first frame update
    IEnumerator Start()
    {
        ScriptDomain domain    = ScriptDomain.CreateDomain("MyDomain", true);
        string       source    = ConnectionManager.ToCode(begin.GetComponent <RectTransform> ()) + "}}}";
        string       reference = "UnityEngine.dll";

        using (UnityWebRequest uwr = UnityWebRequest.Get(Application.streamingAssetsPath + "/bin/Data/Managed" + "/" + reference)) {
            yield return(uwr.SendWebRequest());

            if (uwr.isNetworkError || uwr.isHttpError)
            {
                Debug.Log("www error:" + uwr.error + " " + ("jar:file://" + Application.dataPath + "!/assets/" + reference));
            }
            else
            {
                Debug.Log("1");
                domain.RoslynCompilerService.ReferenceAssemblies.Add(AssemblyReference.FromImage(uwr.downloadHandler.data));
                Debug.Log(uwr.downloadHandler.data);
                ScriptType type = domain.CompileAndLoadMainSource(source);
            }
        }
    }
Exemple #7
0
        /// <summary>
        /// Main run method.
        /// This causes any modified code to be recompiled and executed on the mouse crawler.
        /// </summary>
        public void RunCrawler()
        {
            // Get the C# code from the input field
            string cSharpSource = runCrawlerInput.text;

            // Dont recompile the same code
            if (activeCSharpSource != cSharpSource || activeCrawlerScript == null)
            {
                // Remove any other scripts
                StopCrawler();

                try
                {
                    // Compile code
                    ScriptType type = domain.CompileAndLoadMainSource(cSharpSource, ScriptSecurityMode.UseSettings, assemblyReferences);

                    // Check for null
                    if (type == null)
                    {
                        if (domain.RoslynCompilerService.LastCompileResult.Success == false)
                        {
                            throw new Exception("Maze crawler code contained errors. Please fix and try again");
                        }
                        else if (domain.SecurityResult.IsSecurityVerified == false)
                        {
                            throw new Exception("Maze crawler code failed code security verification");
                        }
                        else
                        {
                            throw new Exception("Maze crawler code does not define a class. You must include one class definition of any name that inherits from 'RoslynCSharp.Example.MazeCrawler'");
                        }
                    }

                    // Check for base class
                    if (type.IsSubTypeOf <MazeCrawler>() == false)
                    {
                        throw new Exception("Maze crawler code must define a single type that inherits from 'RoslynCSharp.Example.MazeCrawler'");
                    }



                    // Create an instance
                    activeCrawlerScript = type.CreateInstance(mazeMouse);
                    activeCSharpSource  = cSharpSource;

                    // Set speed value
                    activeCrawlerScript.Fields["breadcrumbPrefab"] = breadcrumbPrefab;
                    activeCrawlerScript.Fields["moveSpeed"]        = mouseSpeed;
                }
                catch (Exception e)
                {
                    // Show the code editor window
                    codeEditorWindow.SetActive(true);
                    throw e;
                }
            }
            else
            {
                // Get the maze crawler instance
                MazeCrawler mazeCrawler = activeCrawlerScript.GetInstanceAs <MazeCrawler>(false);

                // Call the restart method
                mazeCrawler.Restart();
            }
        }