• 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[ 	
    
    ]+System[ 	
    
    ]*;"))
                            {
                                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} : 
    {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} : 
    {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] == '	' || file[i] == '
    ' || file[i] == '
    '))
                {
                    i--;
                }
    
                return i;
            }
    
            private static int Skip(string file, int index)
            {
                var i = index;
                while (i < file.Length && (file[i] == ' ' || file[i] == '	' || file[i] == '
    ' || file[i] == '
    '))
                {
                    i++;
                }
    
                return i;
            }
    
            static string GetWord(string file, int index)
            {
                var i = index;
                while (i < file.Length && !(file[i] == ' ' || file[i] == '	' || file[i] == '
    ' || file[i] == '
    '))
                {
                    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;
            }
    
        }
    }
  • 相关阅读:
    省选模拟24 题解
    省选模拟23 题解
    省选模拟22 题解
    省选模拟21 题解
    省选模拟20 题解
    省选模拟19 题解
    省选模拟18 题解
    源码分析工具
    深入理解js的变量提升和函数提升
    python并发编程之IO模型
  • 原文地址:https://www.cnblogs.com/mrblue/p/7827063.html
Copyright © 2020-2023  润新知