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) 在上一篇文章中,我们的注意力主要是 ...
随机推荐
- 灰度共生矩阵(GLCM) 及matlab代码实现
原地址:http://blog.csdn.net/bookwormno1/article/details/7962466 这几天学习灰度共生矩阵,现记录如下: 讲灰度共生矩阵比较好的一份百度文库文档: ...
- FZU2181+poj2942(点双连通+判奇圈)
分析:我们对于那些相互不憎恨的人连边,将每次参加会议的所有人(不一定是全部人,只需人数>=3且为奇数)看做一个点双联通分量,那么每个点都至少有两个点与他相邻.即需要保证双联通分量中存在奇圈.至于 ...
- Spring整合Jms学习(三)_MessageConverter介绍
1.4 消息转换器MessageConverter MessageConverter的作用主要有双方面,一方面它能够把我们的非标准化Message对象转换成我们的目标Message对象,这主要 ...
- poj1655(树形dp)
题目链接:http://poj.org/problem?id=1655 题目大意:给一个树,删除其中一个点就会形成一个森林,点的平衡度为删除了这个节点后,所形成多个树,其中组成树的节点最多,节点个数就 ...
- hdu4126(最小生成树+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4126 题意:给出一幅3000个点的图,有10000次操作: 求将某条边的权值变大后的最小生成树,最后输 ...
- Mutex(测量)
游标共享怎样使用Mutex kks 使用mutex以便保护对于下述基于parent cursor父游标和子游标child cursor的一系列操作 对于父游标parent cursor的操作: 基于发 ...
- NPC
这里的想说的NPC不是Non-Player-Controled,非玩家控制角色,而是Non-determinisitc Polynomial complete problem,它属于一类很特殊的问题, ...
- js实现页面重定位的几种方法
参考地址:http://www.cnblogs.com/super-d2/archive/2011/10/01/2197004.html js实现页面重定向 在现行的网站应用中URL重定向的应用有很多 ...
- openstack之nova-api服务流程分析
nova-api公布api服务没实用到一个些框架,基本都是从头写的.在不了解它时,以为它很复杂,难以掌握.花了两三天的时间把它分析一遍后,发现它本身的结构比較简单,主要难点在于对它所使用的一些类库不了 ...
- 安卓---项目中插入百度地图sdk
百度地图 应用里面 自带地图 搜房网 下载百度地图的sdk 熟悉api 注冊百度开发人员的账号 2.12 仅仅要有一个ak就能够 高版本号须要提供应用程序的包名和签名返回开发人员的序列号 使用百度地图 ...