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 ...
随机推荐
- Git -- 本地 一个相同的新的分支 并 推送到远程仓库
(一).创建本地分支 git checkout -b 新分支名 执行该指令后,会在本地创建一个新分支,该分支是从当前分支上检出的,所以所有文件内容都和当前分支一模一样,这是正常的.创建成功后,将自动切 ...
- .Net AppDomain详解(二)
AppDomain 类 表示应用程序域,它是一个应用程序在其中执行的独立环境. 此类不能被继承. 命名空间: System程序集: mscorlib(位于 mscorlib.dll) 继承层次结 ...
- linux C++ 获取服务器外网IP地址(使用系统调用system)
废话不多说,直接贴代码: #include<string.h> #include<stdlib.h> #include<stdio.h> #include<s ...
- 如何用C#动态编译、执行代码
在开始之前,先熟悉几个类及部分属性.方法:CSharpCodeProvider.ICodeCompiler.CompilerParameters.CompilerResults.Assembly. 一 ...
- Android批量图片加载经典系列——使用xutil框架缓存、异步加载网络图片
一.问题描述 为提高图片加载的效率,需要对图片的采用缓存和异步加载策略,编码相对比较复杂,实际上有一些优秀的框架提供了解决方案,比如近期在git上比较活跃的xutil框架 Xutil框架提供了四大模块 ...
- windows server 2012 浏览器IE10无法下载。
cannot download in IE 10 of window server 2012 中文版解决办法: 1.打开IE,按F12,选择兼容浏览器为IE 9 2.选择IE的Internet选项菜单 ...
- Django中使用Celery,定制应用程序中定义的shared_task未在定期任务管理页面的注册任务中显示
解决办法: 在项目 proj/proj/celery.py文件中,看到下面这行配置: celery_app.config_from_object('django.conf:settings', nam ...
- 【asp.net core】Publish to a Linux-Ubuntu 14.04 Server Production Environment
Submary 又升级了,目录结构有变化了 . project.json and Visual Studio 2015 with .NET Core On March 7, 2017, the .NE ...
- Seaslog安装和参数配置
详细文档访问:https://github.com/Neeke/SeasLog/blob/master/README_zh.md 源码安装步骤: 1.先下载Seaslog源码,下载地址:http:// ...
- 【Windows】查看Windows上运行程序的异常日志
任何在windows系统上运行的程序,只要发生异常导致程序异常终止,windows都会在日志中详细记录这个异常.可以在计算机管理中查看,如图:也可以在操作中心查看,如图: