Using system;using system.collections.generic;using system.io;using system.reflection;using System.Text;using System.windows.forms;using ironpython.hosting;using ironpython.runtime;using ironpython.runtime.exceptions;using Microsoft.scripting;using microsoft.scripting.runtime;using microsoft.scripting.hosting;using Plugins;namespace embeddingplugin{internal class Pythonstream:memorystream {TextBox _output; Public Pythonstream (textbox textbox) {_output = textbox; public override void Write (byte[] buffer, int offset, int count) {_output. AppendText (Encoding.UTF8.GetString (buffer, offset, count)); }} internal class Engine {ScriptEngine _engine; Scriptruntime _runtime; TextBox _box; Public list<pluginbase> Plugins {get {return pluginstore.plugins;} Public Engine (textbox textbox) {_engine = Python.createengine(); _runtime = _engine. Runtime; _box = textbox; Setstreams (); String rootdir = Addassemblies (); Loadplugins (RootDir); } public void Setstreams () {Pythonstream stream = new Pythonstream (_box); _runtime. Io. Setoutput (stream, Encoding.UTF8); _runtime. Io. Seterroroutput (stream, Encoding.UTF8); The public string addassemblies () {Assembly mainassembly = assembly.getexecutingassembly (); String RootDir = Directory.getparent (mainassembly.location). FullName; String pluginspath = Path.Combine (RootDir, "Plugins.dll"); Assembly pluginsassembly = Assembly.loadfile (Pluginspath); _runtime. LoadAssembly (mainassembly); _runtime. LoadAssembly (pluginsassembly); _runtime. LoadAssembly (typeof (String). Assembly); _runtime. LoadAssembly (typeof (Uri). Assembly); return rootdir; }public void Loadplugins (string rootdir) {string pluginsdir = Path.Combine (RootDir, "plugins"); foreach (String path in Directory.GetFiles (Pluginsdir)) {if (path. ToLower (). EndsWith (". Py")) {Createplugin (path); }}} public void Createplugin (string path) {try {S Criptsource script = _engine. Createscriptsourcefromfile (path); Compiledcode code = script.compile (); Scriptscope scope = _engine. Createscope (); Script. Execute (scope); } catch (Syntaxerrorexception e) {string msg = "Syntax error in \" {0}\ ""; ShowError (msg, Path.getfilename (Path), E); } catch (Systemexitexception e) {string msg = "Systemexit in \" {0}\ ""; ShowError (MSG, Path.getfilename (PAth), E); } catch (Exception e) {string msg = "Error loading plugin \" {0}\ ""; ShowError (msg, Path.getfilename (Path), E); }} public void ShowError (string title, string name, Exception e) {String caption = Strin G.format (title, name); Exceptionoperations eo = _engine. Getservice<exceptionoperations> (); String error = EO. FormatException (e); MessageBox.Show (Error, caption, MessageBoxButtons.OK, messageboxicon.error); } public void Executepluginatindex (int index) {pluginbase plugin = Plugins[index]; try {plugin. Execute (_box); } catch (Exception e) {string msg = "Error executing plugin \" {0}\ ""; ShowError (msg, plugin. Name, E); } } }}
Unity captures IronPython script error