Unity 捕获IronPython脚本错误
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脚本错误的更多相关文章
- Jenkins 无法捕获构建脚本错误问题
Jenkins 版本 2.121.1 编写构建脚本执行,发现脚本执行出错,不会中断构建过程,导致最后展现的构建结果是错误的. 原因:构建脚本头部加入 #!/bin/bash ,jenkins会将脚本放 ...
- UWP锁、解屏后无法响应操作
UWP的Unity项目,在PC上运行时,如果锁屏(手动或自动)再解锁,游戏画面和进度正常,但是无法进行鼠标.键盘或手柄的操作.这Bug在很多线上的Unity项目中存在. 原因:UWP App的系统事件 ...
- ASP.NET Core DevOps
一.本系列教程说明 源代码管理工具:Gogs 持续集成工具:Jenkins 容器:Docker 本教程选用轻量级的 Git 管理工具 Gogs,搭建简单. 三.教程目录 1.配置免费HTTPS证书 ( ...
- Jenkins高级用法 - Jenkinsfile 介绍及实战经验
系列目录 1.Jenkins 安装 2.Jenkins 集群 3.Jenkins 持续集成 - ASP.NET Core 持续集成(Docker&自由风格&Jenkinsfile) 4 ...
- ASP.NET Core & Docker & Jenkins 零基础持续集成实战
原文:ASP.NET Core & Docker & Jenkins 零基础持续集成实战 一.本系列教程说明 源代码管理工具:Gogs 持续集成工具:Jenkins 容器:Docker ...
- script标签crossorigin属性及同源策略和跨域方法
首先介绍(同源策略) 同源策略是浏览器最核心且基本的安全约定,要求协议.域名.端口都相同为同源,如果非同源时请求数据浏览器会在控制台抛出跨域异常错误,同源策略是浏览器的行为,即使客户端请求发送了,服务 ...
- Unity 3D 调用摄像头捕获照片 录像
1,要想调用摄像头首先要打开摄像头驱动,如果用户允许则可以使用. 2,定义WebCamTexture的变量用于捕获单张照片. 3,连续捕获须启用线程. 实现代码: using UnityEngine; ...
- Unity游戏开发中的内存管理_资料
内存是手游的硬伤——Unity游戏Mono内存管理及泄漏http://wetest.qq.com/lab/view/135.html 深入浅出再谈Unity内存泄漏http://wetest.qq.c ...
- 【转】Unity中的协同程序-使用Promise进行封装(二)
原文:http://gad.qq.com/program/translateview/7170970 译者:王磊(未来的未来) 审校:崔国军(飞扬971) 在上一篇文章中,我们的注意力主要是 ...
随机推荐
- 访何红辉:谈谈Android源码中的设计模式
最近Android 6.0版本的源代码开放下载,刚好分析Android源码的技术书籍<Android源码设计模式解析与实战>上市,我们邀请到它的作者何红辉,来谈谈Android源码中的设计 ...
- Java读取本地文件,并显示在JSP文件中
当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相对路径,还可以是网络上的图片路径.在存 ...
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...
- 分享毕业学生“ERP实施project联赛”总结,是肺腑之言——知识是人的价值的体现,每门课程是有意义的学校纪律
丁.这是我刚刚完成的实习报告,特别是给你一个.阿信,让你知道的真实想法研究生管,我希望你相信在教育管帮助.---雷管1102 刘弈福 以上是刚刚收到(20140427)生邮件,贻富不是我带的毕业设计学 ...
- 关于Platinum库的MediaRender具体C++代码实现探讨
接上篇博文 NDK下 将Platinum SDK 编译成so库 (android - upnp) 讲述了如何利用该代码库编译给android程序调用的so库,其中也提到了,在使用sample-upnp ...
- Nutch+HBase
Nutch+HBase 当我们为nutch的架构发愁的时候,nutch的开发人员送来了nutchbase.我一些简单的测试表明,在hadoop0.20.1和hbase0.20.2上,稍加修改可以运行起 ...
- Android利用Get、Post 获取网络数据
首先是Get: 布局非常easy.就一个button,主要看一下MainActivity吧: package com.francis.httpget; import android.app.Activ ...
- C:打印菱形(自己的方法)
//-------------------*打印菱形*--------------------- int i,j,k; int n; printf("请输入一个奇数n:"); sc ...
- Xcode6在10.9.4上面crash解决
具体请看我的evernote 这里: 在10.9.4系统上面直接安装xcode6的beta3.和平时一样, 1.将beta3拖拽到application文件夹中. 2.等待copy完毕,执行xcode ...
- 谷歌下解决Pop遮罩层无法遮挡滚动栏下问题
今天用pop的弹出窗体里,出现一个问题,当网页出现滚动栏里,不能遮挡住,解决Pop遮罩层无法遮挡滚动栏下问题. 可通过下载获取改动后的代码----->进入下载