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. 慕尼黑大学公开课 Competitive Strategy(竞争策略)总结

    第一章博弈 同时的博弈:双方同时定制策略 如果有显著的次优策略总是不如另一个,则剔除它. 如果一个策略组合中没有一方可以单独改变其策略以提高回报,则称为Nash均衡.一个游戏可能没有也可能有多个Nas ...

  2. 解决ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (2)

    在Mac和XAMPP环境,假设终端打字mysql,现这样的问题: ERROR 2002 (HY000): Can't connect to local MySQL server through soc ...

  3. C#同步SQL Server数据库中的数据--数据库同步工具[同步新数据]

    C#同步SQL Server数据库中的数据 1. 先写个sql处理类: using System; using System.Collections.Generic; using System.Dat ...

  4. 如何搭建DNS服务(转)

    继NTP时间服务器后,继续搭建DNS服务,鉴于昨晚撰写时间超过预期,这次改变策略,先把自己需要用到的部分写出来(主要是基于RAC的搭建,只涉及正向和反向DNS解析),后面再添加必要的说明和阐述. 试验 ...

  5. TI推出SimpleLink低能耗蓝牙CC2541

    TI推出SimpleLink低能耗蓝牙CC2541 日前,德州仪器 (TI) 宣布推出 SimpleLink™ 低能耗蓝牙 (Bluetooth®Low Energy) CC2541-Q1, 这是一款 ...

  6. Python基础 - 内建函数

    什么是内建函数 在Python的手册中,名叫build-in Functions,中文可以称为内建函数. 内建函数就像dos系统的内部命令,他不依赖于外部模块,也就是说: 内建函数就是:安装好Pyth ...

  7. keil提示:missing ';' before 'XXX'但又找不到是哪里少了分号——已解决!

    今天写一个51程序,keil在编译时总提示丢失了一个分号,但怎么都找不到是哪里丢失了分号,搞了一下午才解决问题,现在把经验在此分享,以求让他人不要犯同样的错误!提示信息为: LCD12864.H(20 ...

  8. FZU2176(二维线段树+dfs)

    传送门:easy problem 题意:给定一棵n个节点以1为根的树,初始每个节点的值为0,现在我们要在树上进行一些操作,操作有两种类型. 1 x val 表示对以x为根的子树的每个点进行加权操作(我 ...

  9. JSP路径出现故障

    1.错误叙述性说明 2.错误原因 <%@ page language="java" import="java.util.*" pageEncoding=& ...

  10. 开发指南专题4:JEECG高速微云开发平台--JEECG开发环境的搭建

    开发指南专题4:JEECG微云高速开发平台开发环境搭建 1. JEECG开发环境搭建 JEECG推荐的开发环境为Myeclipse8.5/Eclipse3.7+JDK1.6+Tomcat6.0 1.1 ...