Unity 给Mono脚本添加Try Catch工具
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工具的更多相关文章
- Unity Mono脚本 加密
加密环境 引擎版本:Unity3D 5.3.4 及更高版本 (使用Mono而并非IL2CPP) 操作系统:CentOS 6.2(Final) 加密环境:Android.IOS(暂定) 加密对象:C#源 ...
- 编译UNITY的MONO模块记录
起因 接收到一个UNITY文件处理的任务(c#逻辑代码存放的Assembly-CSharp.dll可热更等需求) 需要重新编译UNITY的mono模块 用于安卓环境下对DLL的定制处理 上网查阅了一些 ...
- 优化Unity游戏项目的脚本(下)
金秋9月,我们祝所有的老师们:教师节快乐 ! 今天,我们继续分享来自捷克的开发工程师Ondřej Kofroň,分享C#脚本的一系列优化方法. 在优化Unity游戏项目的脚本(上)中,我们介绍了如何查 ...
- Unity游戏Mono内存管理及泄漏
UWA工具 https://yq.aliyun.com/articles/435553 Unity游戏Mono内存管理及泄漏 https://wetest.qq.com/lab/view/135.ht ...
- 如何在MVC_WebAPI项目中的APIController帮助页面添加Web测试工具测试
本文转载自:http://www.cnblogs.com/pmars/p/3673811.html 先看效果图: 以下是原文: 如何在帮助页面添加测试工具 上一篇我在ASP.NET里面添加了一个Hel ...
- Unity协程(Coroutine)管理类——TaskManager工具分享
博客分类: Unity3D插件学习,工具分享 源码分析 Unity协程(Coroutine)管理类——TaskManager工具分享 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处 ...
- unity中js脚本与c#脚本互相调用
unity中js脚本与c#脚本互相调用 test1.js function OnGUI() { if(GUI.Button(Rect(25,25,100,30),"JS Call CS& ...
- SetupFactory +添加frm2.0工具
网盘链接: http://pan.baidu.com/s/1c1DFRJM 带单独的添加frm2.0工具 原setupfactory文件下载地址:http://www.pc0359.cn/downin ...
- Eclipse用法和技巧九:自动添加try/catch块2
上一篇介绍了如何给未检查异常快速增加try/catch语句,这里在补充一点其他相关操作.有时候我们增加了try/catch之后还需要在加一个finally块,比如android上每次分配一个curso ...
随机推荐
- 用ndk-stack分析应用native程序异常crash掉
adb logcat | "/home/hxl/bin/android-ndk-r10d/ndk-stack" -sym "/home/hxl/plu/BadGame/p ...
- F800上的CPU有多少个core?
本来想看看F800的一个节点上,每个core的cpu的使用情况.使用下面的命令,可以进行查看: top –P 从命令输出来看,好像是有32个core嘛. 使用下面的命令可以看到具体的CPU的信息. d ...
- 问题解决java.lang.IllegalArgumentException at org.springframework.asm.ClassReader
手上拿到一个老的项目,使用的是spring3.2,启动的时候报错了: 查了一下,发现spring3.2不兼容jdk8,只能使用jdk8以下的版本,使用jdk6可以启动,但是maven构建的时候又提示不 ...
- 如何诊断windows性能问题
直接使用perfmon中的性能诊断工具
- 【Zookeeper】源码分析之Leader选举(二)之FastLeaderElection
一.前言 前面学习了Leader选举的总体框架,接着来学习Zookeeper中默认的选举策略,FastLeaderElection. 二.FastLeaderElection源码分析 2.1 类的继承 ...
- HTML中input type="text"和type="password" 显示的长度不一样
在CSS里边加上input {width:100px;}能把所有input标签的控件宽度改为相同! 加上这个属性 style="width:180px;"
- 关于PHP中的webshell
一.webshell简介 webshell就是以asp.php.jsp或者cgi等网页文件形式存在的一种命令执行环境,也可以将其称做为一种网页后门.黑客在入侵了一个网站后,通常会将asp或php后门文 ...
- 阿里云Logtail 快速诊断工具
当日志采集发生异常时,您可以通过Logtail自助检测工具查看客户端是否存在异常情况,根据工具提示快速定位并解决问题. 说明 本工具目前仅支持Linux系统的服务器. 准备工作 下载检测工具脚本. ...
- 【Windows】创建任务计划
任务计划,可以将任何脚本.程序或文档安排在某个时间运行. 可以按照如下的方式来启动:附件 -> 系统工具 -> 任务计划程序. 也可以在Win+R后,输入:taskschd.msc 命令来 ...
- WebStorm for Mac(Web 前端开发工具)破解版安装
1.软件简介 WebStorm 是 jetbrains 公司旗下一款 JavaScript 开发工具.目前已经被广大中国 JS 开发者誉为 "Web 前端开发神器".&quo ...