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. Android 吸入动画效果详解

    1,背景 吸入(Inhale)效果,最初我是在iOS上面看到的,它是在Note程序中,用户可能添加了一页记录,在做删除时,它的删除效果是:这一页内容吸入到一个垃圾框的图标里面.请看下图所示: ==== ...

  2. sqlplus登录、连接命令

    经常使用: sqlplus username/password  如:普通用户登录  sqlplus scott/tiger sqlplus username/password@net_service ...

  3. 无需安装Mono的Jexus

    ASP.NET跨平台实践:无需安装Mono的Jexus“独立版”   在Linux上运行ASP.NET网站或WebApi的传统步骤是,先安装libgdiplus,再安装mono,然后安装Jexus.在 ...

  4. java Process在windows的使用汇总(转)

    最常用的是ant(java工程中流行),maven,及通用的exec(只要有shell脚本如.sh,.bat,.exe,.cmd等).而其实前两者不容易出错,后者却遇到了以下问题:Caused by: ...

  5. java访问mysql方法数据库

    1.下载界面包mysql-connector-java-5.0.8-bin.jar 下载链接 2.程序 (1)载入驱动 (2)编程连接操作 (3)返回结果处理 编程演示样例 import java.s ...

  6. Linux从用户层到内核层系列 - GNU系列之glibc介绍

    题记:本系列文章的目的是抛开书本从源代码和使用的角度分析Linux内核和相关源代码,byhankswang和你一起玩转linux开发 轻松搞定TCP/IP协议栈,原创文章欢迎交流, byhankswa ...

  7. spring-security3.2.5实现中国式安全管理(转)

    最近公司要做开发平台,对安全要求比较高:SPRING SECURTIY框架刚好对所有安全问题都有涉及,框架的作者最近还做了spring-session项目实现分布式会话管理,还有他的另一个开源项目sp ...

  8. Android 检測网络是否连接

    权限: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  <u ...

  9. Centos 5.5 更新网卡驱动 bnx2 version: 2.0.2

    操作系统:CentOS release 5.5 (Final) 故障现象:网卡无故自动down掉,使用service  network restart 重启后没多久又会自动down , 连接数大概在2 ...

  10. 关于在ios7之后改变状态栏颜色

    看到网上都说 在ios7之后要这样设置 首先,须要在Info.plist配置文件里,添加键:UIViewControllerBasedStatusBarAppearance,并设置为YES: 然后,在 ...