using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; namespace TryCatchCreator
{
class Program
{
private static void Main(string[] args)
{
string[] ignore = {};
if (File.Exists("./trycatch.ignore"))
{
ignore = File.ReadAllLines("./trycatch.ignore");
}
var files = Directory.GetFiles("./", "*.cs", SearchOption.AllDirectories);
foreach (var s in files)
{
try
{
Console.WriteLine("Processing [{0}]", s); bool _ignore = false;
foreach (var s1 in ignore)
{
if (s.Contains(s1))
{
_ignore = true;
}
} if (_ignore)
{
Console.WriteLine("Ignore [{0}]", s);
continue;
} var f = File.ReadAllText(s);
var file = f;
file = AddTryCatchToFunction(file, "Start");
file = AddTryCatchToFunction(file, "Awake");
file = AddTryCatchToFunction(file, "Update");
file = AddTryCatchToFunction(file, "OnEnable");
file = AddTryCatchToFunction(file, "OnDisable");
file = AddTryCatchToFunction(file, "FixedUpdate");
file = AddTryCatchToFunction(file, "LateUpdate");
file = AddTryCatchToFunction(file, "OnDestroy"); if (file.Contains("catch (Exception ex)"))
{
if (!Regex.IsMatch(file, "using[ \t\n\r]+System[ \t\n\r]*;"))
{
Console.WriteLine("Insert 'using System;' in front of the file.");
file = file.Insert(, "using System;" + Environment.NewLine);
}
} if (file != f)
{
File.WriteAllText(s, file);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
} Console.ReadKey();
} static string AddTryCatchToFunction(string file, string func)
{
var index = file.IndexOf(func);
while (index != -)
{
Console.WriteLine("Find a function {0} : \n{1}", func,
file.Substring(index > ? index - : ,
file.Length - index > ? : file.Length - index)); var skip = SkipBackward(file, index - );
var before = file[skip] != '.' && skip != index - ;
var after = file[Skip(file, index + func.Length)] == '('; if (before && after)
{
Console.WriteLine("Find a function {0} : \n{1}", func,
file.Substring(index > ? index - : ,
file.Length - index > ? : file.Length - index)); var leftParenthese = file.IndexOf('(', index);
var rightPraceparenthese = FindNextMatchedSymbol(file, leftParenthese, '(', ')'); var leftBrace = file.IndexOf('{', rightPraceparenthese);
var rightBrace = FindNextMatchedSymbol(file, leftBrace, '{', '}'); var a = Skip(file, leftBrace + );
var macro = GetWord(file, a);
var b = Skip(file, a + macro.Length);
var content = GetWord(file, b);
var c = Skip(file, b + content.Length);
var t = GetWord(file, c);
if (macro == "#if" && content == "!UNITY_EDITOR" && t == "try")
{
index = file.IndexOf(func, index + func.Length);
continue;
} // insert code before right brace
var catchBlock = Environment.NewLine +
"#if !UNITY_EDITOR" + Environment.NewLine +
"}" + Environment.NewLine +
"catch (Exception ex)" + Environment.NewLine +
"{" + Environment.NewLine +
" Logger.Error(ex.ToString());" + Environment.NewLine +
"}" + Environment.NewLine +
"#endif" + Environment.NewLine;
file = file.Insert(rightBrace, catchBlock); // insert code before left brace
var tryBlock = Environment.NewLine +
"#if !UNITY_EDITOR" + Environment.NewLine +
"try" + Environment.NewLine +
"{" + Environment.NewLine +
"#endif" + Environment.NewLine;
file = file.Insert(leftBrace + , tryBlock); index = rightBrace + tryBlock.Length + catchBlock.Length;
index = file.IndexOf(func, index);
}
else
{
index = file.IndexOf(func, index + func.Length);
}
} return file;
} private static int SkipBackward(string file, int index)
{
var i = index;
while (i >= && (file[i] == ' ' || file[i] == '\t' || file[i] == '\n' || file[i] == '\r'))
{
i--;
} return i;
} private static int Skip(string file, int index)
{
var i = index;
while (i < file.Length && (file[i] == ' ' || file[i] == '\t' || file[i] == '\n' || file[i] == '\r'))
{
i++;
} return i;
} static string GetWord(string file, int index)
{
var i = index;
while (i < file.Length && !(file[i] == ' ' || file[i] == '\t' || file[i] == '\n' || file[i] == '\r'))
{
i++;
} return file.Substring(index, i - index);
} static int FindNextMatchedSymbol(string file, int start, char leftSymbol, char rightSymbol)
{
Stack<char> stack = new Stack<char>();
while (start < file.Length)
{
if (file[start] == leftSymbol)
{
stack.Push(leftSymbol);
}
else if (file[start] == rightSymbol)
{
stack.Pop();
} if (stack.Count == )
{
break;
} start++;
} return start;
} }
}

Unity 给Mono脚本添加Try Catch工具的更多相关文章

  1. Unity Mono脚本 加密

    加密环境 引擎版本:Unity3D 5.3.4 及更高版本 (使用Mono而并非IL2CPP) 操作系统:CentOS 6.2(Final) 加密环境:Android.IOS(暂定) 加密对象:C#源 ...

  2. 编译UNITY的MONO模块记录

    起因 接收到一个UNITY文件处理的任务(c#逻辑代码存放的Assembly-CSharp.dll可热更等需求) 需要重新编译UNITY的mono模块 用于安卓环境下对DLL的定制处理 上网查阅了一些 ...

  3. 优化Unity游戏项目的脚本(下)

    金秋9月,我们祝所有的老师们:教师节快乐 ! 今天,我们继续分享来自捷克的开发工程师Ondřej Kofroň,分享C#脚本的一系列优化方法. 在优化Unity游戏项目的脚本(上)中,我们介绍了如何查 ...

  4. Unity游戏Mono内存管理及泄漏

    UWA工具 https://yq.aliyun.com/articles/435553 Unity游戏Mono内存管理及泄漏 https://wetest.qq.com/lab/view/135.ht ...

  5. 如何在MVC_WebAPI项目中的APIController帮助页面添加Web测试工具测试

    本文转载自:http://www.cnblogs.com/pmars/p/3673811.html 先看效果图: 以下是原文: 如何在帮助页面添加测试工具 上一篇我在ASP.NET里面添加了一个Hel ...

  6. Unity协程(Coroutine)管理类——TaskManager工具分享

    博客分类: Unity3D插件学习,工具分享 源码分析   Unity协程(Coroutine)管理类——TaskManager工具分享 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处 ...

  7. unity中js脚本与c#脚本互相调用

    unity中js脚本与c#脚本互相调用   test1.js function OnGUI() { if(GUI.Button(Rect(25,25,100,30),"JS Call CS& ...

  8. SetupFactory +添加frm2.0工具

    网盘链接: http://pan.baidu.com/s/1c1DFRJM 带单独的添加frm2.0工具 原setupfactory文件下载地址:http://www.pc0359.cn/downin ...

  9. Eclipse用法和技巧九:自动添加try/catch块2

    上一篇介绍了如何给未检查异常快速增加try/catch语句,这里在补充一点其他相关操作.有时候我们增加了try/catch之后还需要在加一个finally块,比如android上每次分配一个curso ...

随机推荐

  1. MyEclipse部署WebLogic

    ====================================================================================

  2. C#模拟HTTP请求Post JSON

    前言 因为接口是http的,我们站点是https的,不能https直接ajax请求http,所以需要在SharePoint中开发一个模拟请求Ajax的Service,分享一下. var httpWeb ...

  3. VS2010链接TFS遇见错误:TF204017,没有访问工作区域,需要一个或者多个必须权限

    最近刚刚搭建好服务器,然后准备将VSS源代码迁移到TFS源代码管理服务器上面.在我本机先用的服务器帐号来上传初始化源代码数据库,然后我又用自己的帐号进行迁出代码的时候发生的异常. 造成上述错误,主要是 ...

  4. iOS:针对固定数据源,更好的封装cell

    一.介绍 在iOS开发中,tableView非常常用,能将其展示出来,它的数据源必不可少.当然数据源有动态下发的,有固定写死的,这里我只探讨固定写死的情况.对于死数据,我们在项目中经常遇到的场景就是我 ...

  5. 微信小程序wxss的background本地图片问题

    在web 或者webapp开发中我们习惯了直接饮用本地图片做背景,例如 .aaa { background: url('img/1.png'); } 但是这种引用方式在微信小程序中是无法使用的,控制台 ...

  6. RabbitMQ使用技巧

    一. net客户端介绍    http://www.cnblogs.com/hsyzero/p/6297644.html 二. RabbitMQ消息应答 执行一个任务可能需要花费几秒钟,你可能会担心如 ...

  7. Win10 设置窗口背景色

    Win10 的窗口背景色不能像Win7那样通过修改Windows的"窗口"配置来生效,只能是通过修改注册表的信息来修改Win10的窗口色. 1. 通过注册表来修改默认的窗口背景色( ...

  8. 关于Installation error: INSTALL_FAILED_NO_MATCHING_ABIS的解决方法

    遇到过好几次这种错误提示,工程代码没有任何错误,但是连安装都安装不上模拟器,console控制台就报出上面的错误: [2015-11-25 15:15:37 - Em4.x] Installation ...

  9. c 链表之 快慢指针 查找循环节点

    参考:http://blog.csdn.net/wenqian1991/article/details/17452715 上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上 ...

  10. intellij idea (Android studio )外部程序 打开某扩展名(格式)

    最近在为项目开发写思维导图, 为了留下思考的过程和业务逻辑. 本人使用的工具是很可怜的freemind,所以“脑图”的扩展名是“.mm” 情景: 在intellij idea (Android stu ...