Пример #1
0
        public void ResetEngine(bool debugmode = false)
        {
            if (_python != null)
            {
                _python.Runtime.Shutdown();
            }

            ScriptRuntimeSetup setup = new ScriptRuntimeSetup();

            setup.DebugMode = debugmode;
            setup.LanguageSetups.Add(Python.CreateLanguageSetup(null));
            ScriptRuntime runtime = new ScriptRuntime(setup);

            _python = runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);
            LoadAssembly(typeof(Prefab.Bitmap).Assembly);
            LoadAssembly(typeof(PrefabUtils.PathDescriptor).Assembly);

            if (_consoleOutput == null)
            {
                _consoleOutput = new MemoryStream();
            }
            _python.Runtime.IO.SetOutput(_consoleOutput, Encoding.Default);
            // _python.Runtime.GetClrModule().GetVariable("AddReference")("Prefab");
            //_python = runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);
            //Instance = new PythonScriptHost(false);
        }
Пример #2
0
        public virtual int Run(string[] args)
        {
            var runtimeSetup = CreateRuntimeSetup();
            var options      = new ConsoleHostOptions();

            _optionsParser = new ConsoleHostOptionsParser(options, runtimeSetup);

            try {
                ParseHostOptions(args);
            } catch (InvalidOptionException e) {
                Console.Error.WriteLine("Invalid argument: " + e.Message);
                return(_exitCode = 1);
            }

            SetEnvironment();

            string provider = GetLanguageProvider(runtimeSetup);

            LanguageSetup languageSetup = null;

            foreach (var language in runtimeSetup.LanguageSetups)
            {
                if (language.TypeName == provider)
                {
                    languageSetup = language;
                }
            }
            if (languageSetup == null)
            {
                // the language doesn't have a setup -> create a default one:
                languageSetup = new LanguageSetup(Provider.AssemblyQualifiedName, Provider.Name);
                runtimeSetup.LanguageSetups.Add(languageSetup);
            }

            // inserts search paths for all languages (/paths option):
            InsertSearchPaths(runtimeSetup.Options, Options.SourceUnitSearchPaths);

            _languageOptionsParser = CreateOptionsParser();

            try {
                _languageOptionsParser.Parse(Options.IgnoredArgs.ToArray(), runtimeSetup, languageSetup, PlatformAdaptationLayer);
            } catch (InvalidOptionException e) {
                Console.Error.WriteLine(e.Message);
                return(_exitCode = -1);
            }

            _runtime = new ScriptRuntime(runtimeSetup);

            try {
                _engine = _runtime.GetEngineByTypeName(provider);
            } catch (Exception e) {
                Console.Error.WriteLine(e.Message);
                return(_exitCode = 1);
            }

            Execute();
            return(_exitCode);
        }
Пример #3
0
        protected void SetupEngine()
        {
            var setup = Python.CreateRuntimeSetup(null);

            setup.HostType = typeof(SnekScriptHost);
            var runtime = new ScriptRuntime(setup);

            engine = runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);
            SnekImporter.OverrideImport(engine);

            defaultScope = new SnekScope(engine.CreateScope());

            SetupAssemblies();

            AddSearchPath(Application.streamingAssetsPath + "/");

            MlfProcessorManager.OnEngineInit(this);
        }
Пример #4
0
        public ScriptController(bool ADebug)
        {
            // _SearchPaths = new List<string>();

            _setup           = new ScriptRuntimeSetup();
            _setup.DebugMode = true;
            _setup.LanguageSetups.Add(Python.CreateLanguageSetup(null));
            _setup.DebugMode = ADebug;

            _runtime = new ScriptRuntime(_setup);
            _engine  = _runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);
            var bufOut = new BufferedStream(256, 100);

            _outputStream         = bufOut;
            bufOut.OnFlushBuffer += new FlushBufferEvent(bufOut_OnFlushBuffer);
            _engine.Runtime.IO.SetOutput(bufOut, ASCIIEncoding.ASCII);
            _engine.Runtime.IO.SetErrorOutput(bufOut, ASCIIEncoding.ASCII);

            SetSearchPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
        }
Пример #5
0
        /// <summary>
        /// Create new scripting engine
        /// </summary>
        /// <param name="runtime">Instance of the current runtime enviroement</param>
        /// <returns>Instance of IP script engine</returns>
        public ScriptEngine CreateEngine(ScriptRuntime runtime)
        {
            scriptEngine = runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);

            // Override import functionality
            ScriptScope scope = IronPython.Hosting.Python.GetBuiltinModule(scriptEngine);

            scope.SetVariable("__import__", new ImportDelegate(ResolveImport));

            var sysScope = scriptEngine.GetSysModule();

            List path_hooks = sysScope.GetVariable("path_hooks");

            // Disable zipimporter if needed
            for (int i = 0; i < path_hooks.Count; i++)
            {
                if (path_hooks.ElementAt(i) != null)
                {
                    PythonType type = path_hooks.ElementAt(i) as PythonType;
                    string     name = PythonType.Get__name__(type);

                    if (name == "zipimporter" && EnableZipImporter == false)
                    {
                        path_hooks.RemoveAt(i);
                    }
                }
            }

            PythonType genericimporter = DynamicHelpers.GetPythonType(new GenericImportModule.genericimporter());

            path_hooks.Add(genericimporter);

            sysScope.SetVariable("path_hooks", path_hooks);

            return(scriptEngine);
        }
Пример #6
0
        public virtual int Run(string[] args) {
            var runtimeSetup = CreateRuntimeSetup();
            var options = new ConsoleHostOptions();
            _optionsParser = new ConsoleHostOptionsParser(options, runtimeSetup);

            try {
                ParseHostOptions(args);
            } catch (InvalidOptionException e) {
                Console.Error.WriteLine("Invalid argument: " + e.Message);
                return _exitCode = 1;
            }

            SetEnvironment();

            string provider = GetLanguageProvider(runtimeSetup);

            LanguageSetup languageSetup = null;
            foreach (var language in runtimeSetup.LanguageSetups) {
                if (language.TypeName == provider) {
                    languageSetup = language;
                }
            }
            if (languageSetup == null) {
                // the language doesn't have a setup -> create a default one:
                languageSetup = new LanguageSetup(Provider.AssemblyQualifiedName, Provider.Name);
                runtimeSetup.LanguageSetups.Add(languageSetup);
            }

            // inserts search paths for all languages (/paths option):
            InsertSearchPaths(runtimeSetup.Options, Options.SourceUnitSearchPaths);

            _consoleOptions = ParseOptions(Options.IgnoredArgs.ToArray(), runtimeSetup, languageSetup);
            if (_consoleOptions == null) {
                return _exitCode = 1;
            }

            _runtime = new ScriptRuntime(runtimeSetup);

            try {
                _engine = _runtime.GetEngineByTypeName(provider);
            } catch (Exception e) {
                Console.Error.WriteLine(e.Message);
                return _exitCode = 1;
            }

            Execute();
            return _exitCode;
        }
Пример #7
0
 /// <summary>
 /// Given a ScriptRuntime gets the ScriptEngine for IronPython.
 /// </summary>
 public static ScriptEngine /*!*/ GetEngine(ScriptRuntime /*!*/ runtime)
 {
     return(runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName));
 }
 public ScriptEngine GetDefaultEngine()
 {
     return(_scriptingRuntime.GetEngineByTypeName(_defaultLanguageSetup.TypeName));
 }
Пример #9
0
        public virtual int Run(string[] args) {
            var runtimeSetup = CreateRuntimeSetup();
            var options = new ConsoleHostOptions();
            _optionsParser = new ConsoleHostOptionsParser(options, runtimeSetup);

            try {
                ParseHostOptions(args);
            } catch (InvalidOptionException e) {
                Console.Error.WriteLine("Invalid argument: " + e.Message);
                return _exitCode = 1;
            }

            SetEnvironment();

            string provider = GetLanguageProvider(runtimeSetup);

            LanguageSetup languageSetup = null;
            foreach (var language in runtimeSetup.LanguageSetups) {
                if (language.TypeName == provider) {
                    languageSetup = language;
                }
            }
            if (languageSetup == null) {
                // the language doesn't have a setup -> create a default one:
                languageSetup = new LanguageSetup(Provider.AssemblyQualifiedName, Provider.Name);
                runtimeSetup.LanguageSetups.Add(languageSetup);
            }

            // inserts search paths for all languages (/paths option):
            InsertSearchPaths(runtimeSetup.Options, Options.SourceUnitSearchPaths);

            _languageOptionsParser = CreateOptionsParser();

            try {
                _languageOptionsParser.Parse(Options.IgnoredArgs.ToArray(), runtimeSetup, languageSetup, PlatformAdaptationLayer);
            } catch (InvalidOptionException e) {
                Console.Error.WriteLine(e.Message);
                return _exitCode = -1;
            }

#if !SILVERLIGHT
            if (typeof(DynamicMethod).GetConstructor(new Type[] { typeof(string), typeof(Type), typeof(Type[]), typeof(bool) }) == null) {
                Console.WriteLine(string.Format("{0} requires .NET 2.0 SP1 or later to run.", languageSetup.DisplayName));
                Environment.Exit(1);
            }
#endif

            _runtime = new ScriptRuntime(runtimeSetup);

            try {
                _engine = _runtime.GetEngineByTypeName(provider);
            } catch (Exception e) {
                Console.Error.WriteLine(e.Message);
                return _exitCode = 1;
            }

            Execute();
            return _exitCode;
        }
Пример #10
0
 public static ScriptEngine /*!*/ GetEngine(ScriptRuntime /*!*/ runtime)
 {
     ContractUtils.RequiresNotNull(runtime, "runtime");
     return(runtime.GetEngineByTypeName(typeof(RubyContext).AssemblyQualifiedName));
 }
Пример #11
0
        public virtual int Run(string[] args)
        {
            var runtimeSetup = CreateRuntimeSetup();
            var options      = new ConsoleHostOptions();

            _optionsParser = new ConsoleHostOptionsParser(options, runtimeSetup);

            try {
                ParseHostOptions(args);
            } catch (InvalidOptionException e) {
                Console.Error.WriteLine("Invalid argument: " + e.Message);
                return(_exitCode = 1);
            }

            SetEnvironment();

            string provider = GetLanguageProvider(runtimeSetup);

            LanguageSetup languageSetup = null;

            foreach (var language in runtimeSetup.LanguageSetups)
            {
                if (language.TypeName == provider)
                {
                    languageSetup = language;
                }
            }
            if (languageSetup == null)
            {
                // the language doesn't have a setup -> create a default one:
                languageSetup = new LanguageSetup(Provider.AssemblyQualifiedName, Provider.Name);
                runtimeSetup.LanguageSetups.Add(languageSetup);
            }

            // inserts search paths for all languages (/paths option):
            InsertSearchPaths(runtimeSetup.Options, Options.SourceUnitSearchPaths);

            _languageOptionsParser = CreateOptionsParser();

            try {
                _languageOptionsParser.Parse(Options.IgnoredArgs.ToArray(), runtimeSetup, languageSetup, PlatformAdaptationLayer);
            } catch (InvalidOptionException e) {
                Console.Error.WriteLine(e.Message);
                return(_exitCode = -1);
            }

#if !SILVERLIGHT
            if (typeof(DynamicMethod).GetConstructor(new Type[] { typeof(string), typeof(Type), typeof(Type[]), typeof(bool) }) == null)
            {
                Console.WriteLine(string.Format("{0} requires .NET 2.0 SP1 or later to run.", languageSetup.DisplayName));
                Environment.Exit(1);
            }
#endif

            _runtime = new ScriptRuntime(runtimeSetup);

            try {
                _engine = _runtime.GetEngineByTypeName(provider);
            } catch (Exception e) {
                Console.Error.WriteLine(e.Message);
                return(_exitCode = 1);
            }

            Execute();
            return(_exitCode);
        }
Пример #12
0
 /// <summary>
 /// Given a ScriptRuntime gets the ScriptEngine for Essence.
 /// </summary>
 public static ScriptEngine GetEngine(ScriptRuntime runtime)
 {
     return(runtime.GetEngineByTypeName(typeof(EssenceSharpContext).AssemblyQualifiedName));
 }