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 ...
随机推荐
- ZegGraph属性含义
一.主要内容概念 属性名称 属性值.作用 MasterPane 一个类对象管理多个GraphPane来源于PaneBase.使用MasterPane类都是可选的,GraphPane类可以直接用于一个单 ...
- Socket编程的UDP与TCP,应用在哪些地方
随着网络技术飞速发展,网速已不再是传输的瓶颈,UDP协议以其简单.传输快的优势,在越来越多场景下取代了TCP,如网页浏览.流媒体.实时游戏.物联网. 1,网速的提升给UDP稳定性提供可靠网络保障 CD ...
- [BetterExplained]为什么你应该(从现在开始就)写博客
(一)为什么你应该(从现在开始就)写博客 用一句话来说就是,写一个博客有很多好处,却没有任何明显的坏处.(阿灵顿的情况属于例外,而非常态,就像不能拿抽烟活到一百岁的英国老太太的个例来反驳抽烟对健康的极 ...
- tmux手册中文翻译
man tmux可以看到最详细的tmux介绍,本文翻译自tmux手册. tmux全名叫"terminal multiplexer",终端多路复用器. tmux的命令格式为: tmu ...
- More than the maximum number of request parameters
前些时间,我们的的一个管理系统出现了点问题,原本运行的好好的功能,业务方突然讲不行了,那个应用已经运行了好多年了,并且对应的代码最近谁也没改动过,好奇怪的问题,为了解决此问题,我们查看了日志,发现请求 ...
- 我的Nginx配置文件
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- 有关volatile关键字和transient关键字
(1)volatile关键字['vɑlətl]的作用 让变量每次在使用的时候,都从主存中取,而不是从各个线程的“工作内存”. 也就是说,volatile变量对于每次使用,线程都能得到当前volatil ...
- xml解析、写入遇到的坑
前言 最近在看一个线上xml文件导出的问题,需求如下: 从我们平台导出一个后缀为tmx的术语语料数据(实际内容为xml文件),然后导入到其他第三方平台发现无法导入. 从其他平台导入的tmx文件无法导入 ...
- [svc]对称加密/非对称加密细枝末节-如何做到数据传输的authentication/data integrity/confidentiality(私密)
对称/非对称/混合加密的冷知识 数据在互联网上传输,要考虑安全性. 讲到安全,要从三方面考虑: 1.authentication 每一个IP包的认证,确保合法源的数据 2.data integrity ...
- 未能加载文件或程序集"CheckRegister"或它的某一个依赖项.参数错误. (异常来
报“未能加载文件或程序集“CheckRegister”或它的某一个依赖项.参数错误”的解决方法 问题如下所示: 未能加载文件或程序集“CheckRegister”或它的某一个依赖项.参数错误. (异常 ...