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(0, "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 != -1) { Console.WriteLine("Find a function {0} : \n{1}", func, file.Substring(index > 10 ? index - 10 : 0, file.Length - index > 100 ? 100 : file.Length - index)); var skip = SkipBackward(file, index - 1); var before = file[skip] != '.' && skip != index - 1; var after = file[Skip(file, index + func.Length)] == '('; if (before && after) { Console.WriteLine("Find a function {0} : \n{1}", func, file.Substring(index > 10 ? index - 10 : 0, file.Length - index > 100 ? 100 : 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 + 1); 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 + 1, 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 >= 0 && (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 == 0) { break; } start++; } return start; } } }