Unity 捕获IronPython脚本错误
2016-04-03 11:57
447 查看
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); } 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 { ScriptSource 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 = String.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); } } } }
相关文章推荐
- Unity3D中使用Profiler精确定位性能热点的优化技巧
- surface shader
- FIX SHADER
- 在editor模式下遍历unity3d builtsetting中的场景
- [Unity] Shader(着色器)之纹理贴图
- [Unity] Shader(着色器)之固定管线
- 翻译了一下Unity新的UI的画布渲染模式,记录一下,希望有人能用得上。
- Learn Unity(0)——basic C# for Unity
- ARToolKit for Unity
- [Unity热更新]tolua# & LuaFramework(三):lua使用list与事件委托
- Unity3D外包团队——技术分享U3D全景漫游(三)
- 全景视频外包团队:技术分享Unity3D全景漫游
- Unity3D外包
- 【Unity】3.2 利用预设(Prefab)制作可复用的组件
- 【Unity】3.1 利用内置的3D对象创建三维模型
- 【Unity】3.0 第3章 创建和导入3D模型
- unity发布自定义分辨率
- Unity3D运行报DllNotFoundException
- Unity3D中C#调用iOS的静态库(*.a)
- Unity Flow distort of screen