private async void runScriptButton_ClickAsync(object sender, EventArgs e)
        {
            var sb = new ScriptSandbox(Data, FilePath, true);

            label1.Text = awaitingScriptCompleteMsg;
            try
            {
                var loader = new InteractiveAssemblyLoader();
                loader.RegisterDependency(typeof(UndertaleObject).GetTypeInfo().Assembly);
                var sf     = new FileStream(scriptFileTB.Text, FileMode.Open);
                var sr     = new StreamReader(sf);
                var script = CSharpScript.Create <object>(sr.ReadToEnd(), ScriptOptions.Default
                                                          .WithImports("UndertaleModLib", "UndertaleModLib.Models", "UndertaleModLib.Decompiler", "UndertaleModLib.Scripting", "System", "System.IO", "System.Collections.Generic")
                                                          .WithReferences(typeof(UndertaleObject).GetTypeInfo().Assembly),
                                                          typeof(IScriptInterface), loader);
                await script.RunAsync(sb);

                label1.Text = boxCompleteMsg;
            }catch (CompilationErrorException eanother)
            {
                label1.Text = eanother.ToString();
                MessageBox.Show(eanother.Message, "Script compiling error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ea)
            {
                label1.Text = e.ToString();
                MessageBox.Show(ea.Message, "Script error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 2
0
        private static async void RunMain()
        {
            var engine = new ScriptingEngine();

            Console.Write("Basic Arithmetic...");
            var addResult = await engine.EvaluateAsync <int>("1 + 2");

            TestUtil.WriteResult(TestUtil.Match(3, addResult));

            Console.Write("Testing variables...");
            engine.Globals["X"] = 1;
            engine.Globals["Y"] = 2;
            var variableAddResult = await engine.EvaluateAsync <int>("Globals[\"X\"] + Globals[\"Y\"]");

            TestUtil.WriteResult(TestUtil.Match(engine.Globals["X"] + engine.Globals["Y"], variableAddResult));

            Console.Write("Testing precompiled script basic arithmetic...");
            var precompiledAddScript = engine.CompileScript("1 + 2");
            var precompiledAddResult = await precompiledAddScript.RunAsync <int>();

            TestUtil.WriteResult(TestUtil.Match(3, precompiledAddResult));

            Console.WriteLine("Testing passing objects to script...");
            engine.Globals["Creature"] = new Dog();
            await engine.RunAsync("Globals[\"Creature\"].Bark()");

            Console.WriteLine("Completed Scripting Engine tests");

            Console.Write("Testing Sandboxing...");

            var securityParams = new SandboxSecurityParameters();

            //securityParams.UseZoneSecurity = true; //This is very weak

            securityParams.AllowScripting();

            var scriptSandbox = new ScriptSandbox(securityParams, Guid.NewGuid().ToString("N"));

            Console.WriteLine("Sandbox Created.");

            Console.Write("Basic arithmetic precompiled script in sandbox...");
            var sandboxedPrecompileAddScript       = scriptSandbox.SandboxedEngine.CompileScript("1 + 2");
            var sandboxedPrecompileAddScriptResult = sandboxedPrecompileAddScript.RunSync <int>();

            TestUtil.WriteResult(TestUtil.Match(3, sandboxedPrecompileAddScriptResult));

            /*
             * Console.Write("Basic arithmetic in sandbox...");
             * var sandboxedAddResult = scriptSandbox.SandboxedEngine.EvaluateSync<int>("1 + 2");
             * TestUtil.WriteResult(TestUtil.Match(3, sandboxedAddResult));
             */

            Console.WriteLine("All tests completed");
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static ScriptWindow_CodeEditor()
        {
            var names_ignore = typeof(object).GetMembers().Select(obj => obj.Name);

            ACLIST_API = ScriptSandbox.GetApiNames().Where(name => !names_ignore.Contains(name)).OrderBy(name => name);
        }