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);
}
}
}
}

  

Unity 捕获IronPython脚本错误的更多相关文章

  1. Jenkins 无法捕获构建脚本错误问题

    Jenkins 版本 2.121.1 编写构建脚本执行,发现脚本执行出错,不会中断构建过程,导致最后展现的构建结果是错误的. 原因:构建脚本头部加入 #!/bin/bash ,jenkins会将脚本放 ...

  2. UWP锁、解屏后无法响应操作

    UWP的Unity项目,在PC上运行时,如果锁屏(手动或自动)再解锁,游戏画面和进度正常,但是无法进行鼠标.键盘或手柄的操作.这Bug在很多线上的Unity项目中存在. 原因:UWP App的系统事件 ...

  3. ASP.NET Core DevOps

    一.本系列教程说明 源代码管理工具:Gogs 持续集成工具:Jenkins 容器:Docker 本教程选用轻量级的 Git 管理工具 Gogs,搭建简单. 三.教程目录 1.配置免费HTTPS证书 ( ...

  4. Jenkins高级用法 - Jenkinsfile 介绍及实战经验

    系列目录 1.Jenkins 安装 2.Jenkins 集群 3.Jenkins 持续集成 - ASP.NET Core 持续集成(Docker&自由风格&Jenkinsfile) 4 ...

  5. ASP.NET Core & Docker & Jenkins 零基础持续集成实战

    原文:ASP.NET Core & Docker & Jenkins 零基础持续集成实战 一.本系列教程说明 源代码管理工具:Gogs 持续集成工具:Jenkins 容器:Docker ...

  6. script标签crossorigin属性及同源策略和跨域方法

    首先介绍(同源策略) 同源策略是浏览器最核心且基本的安全约定,要求协议.域名.端口都相同为同源,如果非同源时请求数据浏览器会在控制台抛出跨域异常错误,同源策略是浏览器的行为,即使客户端请求发送了,服务 ...

  7. Unity 3D 调用摄像头捕获照片 录像

    1,要想调用摄像头首先要打开摄像头驱动,如果用户允许则可以使用. 2,定义WebCamTexture的变量用于捕获单张照片. 3,连续捕获须启用线程. 实现代码: using UnityEngine; ...

  8. Unity游戏开发中的内存管理_资料

    内存是手游的硬伤——Unity游戏Mono内存管理及泄漏http://wetest.qq.com/lab/view/135.html 深入浅出再谈Unity内存泄漏http://wetest.qq.c ...

  9. 【转】Unity中的协同程序-使用Promise进行封装(二)

    原文:http://gad.qq.com/program/translateview/7170970 译者:王磊(未来的未来)    审校:崔国军(飞扬971)   在上一篇文章中,我们的注意力主要是 ...

随机推荐

  1. 关于hibernate中多对多关系

    关于多对多关系 数据库:在使用多对多的关系时,我们能够使用复合主键.也能够不使用,直接引入外键相同能够实现. 在数据库中使用多对多关系时,须要一个中间表. 多对多关系中的数据库结构例如以下: 表:Or ...

  2. [Network]Wireless and Mobile

    Wireless 1 Introduction 1.1 Elements 1. Wireless Hosts Wireless does not mean mobility. 2. Base Stat ...

  3. ZXing拍摄代码扫描之后以区分一维码、二维码、其他代码

    我怎么有二维码没有联系,最近遇到一个问题,,如何推断条码扫描到一维代码或者二维代码,辛苦了一个下午下班后自己,加上网上跟踪信息. 总结出两种方式能够解决该问题(推荐採用另外一种方式): 1.改动源代码 ...

  4. STM32 + RT Thread OS 学习笔记[四]

    1.  补注 a)      硬件,打通通讯通道 若学习者购买了学习板,通常可以在学习板提供的示例代码中找到LCD的相关驱动代码,基本上,这里的驱动的所有代码都可以从里面找到. 从上面的示意图可见,M ...

  5. linux shell学习记录

    1.shell脚本开始以 #! /usr/bin 这个叫做Shebang 这个指定解释器的路径 2.shell 一些配置在~/.bashrc中,运行的历史shell命令在~/.bash_history ...

  6. VSTO之旅系列(四):创建Word解决方案

    原文:VSTO之旅系列(四):创建Word解决方案 本专题概要 引言 Word对象模型 创建Word外接程序 小结 一.引言 在上一个专题中主要为大家介绍如何自定义我们的Excel 界面的,然而在这个 ...

  7. DecimalFormat

    public class TestDemo { public static void main(String[] args) { String format = new DecimalFormat(& ...

  8. fs学习笔记之输出格式

    接触fs那么久,有必要再记录一下. 上一篇介绍了fs拓扑描写叙述文件dot的格式,今天要介绍fs输出文件的格式. 举个样例,下面是d节点输出文件的一行记录,也就是一条流经过d的记录. textexpo ...

  9. cocos2dx-3.0(1)------win7 32位android环境搭建

    參照链接http://blog.csdn.net/wonengxing/article/details/23601359 ----我的生活,我的点点滴滴!! 一. Android工具安装 1. 安装J ...

  10. ConditonHelper

    在网上其实已经有很多类似这种拼接sql条件的类,但是没有看到一个让我感觉完全满意的这样的类.最近看到 http://www.cnblogs.com/xtdhb/p/3811956.html 这博客,觉 ...