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. Android ANR Waiting because no window has focus问题分析

    转自:https://www.cnblogs.com/MMLoveMeMM/articles/4849667.html 这种问题主要是发生在两个应用页面之间切换的时候,这个临界点的时候,一个页面正在起 ...

  2. Java ThreadLocal的使用

    Java中的ThreadLocal类允许我们创建只能被同一个线程读写的变量.因此,如果一段代码含有一个ThreadLocal变量的引用,即使两个线程同时执行这段代码,它们也无法访问到对方的Thread ...

  3. Morris图表使用小记

    挺好用的,碰到几个问题,有的是瞎试解决了的: 1.我想折线图能够响应单击事件,即点击某个节点后,就能加载进一步的信息,帮助没找到,参照另外一个地方的写法,居然支持事件 Morris.Line({ el ...

  4. 单反毁三代,kindle富一生

    盖kindle 面更香 人皆知有用之用,而莫知无用之用   一个存留记忆的地方:http://i-remember.fr/en   文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论

  5. PL/SQL学习笔记之函数

    一:函数 函数与过程的最大不同就是,函数有返回值.适用于需要返回结果的场景. 二:创建函数 CREATE [OR REPLACE] FUNCTION function_name [(parameter ...

  6. (原)GAN之pix2pix

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9175281.html 论文: Image-to-Image Translation with Con ...

  7. Effective Java 第三版——49. 检查参数有效性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx EF 6 Code-Firs ...

  9. cmd命令中截取日期字符

    %date:~0,10% 表示年份    

  10. HTTP Status 500 - Unable to create directory

    分析原因: 例如:java web项目 上传图片创建文件夹cd /data/apps/static-web/sjk/driver/attachment/编号/文件名称.jpg 在创建文件目录 /dat ...