Procházet zdrojové kódy

同步8.0日志重定向修改 (#547)

* 解决结构体内私有字段加了BsonElement还不能序列化的问题 (#527)

* Bson序列化结构体时忽略没加BsonElement的私有字段 (#528)

* 基本重构日志重定向,解决单击堆栈详细信息行号结果定位错误的问题 (#530)

* 基本重构日志重定向,解决单击堆栈详细信息行号结果定位错误的问题,

* 简单优化一下日志重定向的代码

* 修复编译错误

* 日志重定向之前的思路就有问题,肯定会有个倒霉蛋碰到行号恰好跟跳过的那三个文件之一行号一样的情况,这次避免了这种情况 (#531)

---------

Co-authored-by: ET6 <54055724+752636090@users.noreply.github.com>
Co-authored-by: tanghai <egametang@gmail.com>
EP-Toushirou před 2 roky
rodič
revize
5c028a191c

+ 8 - 2
Unity/Assets/Scripts/Core/Serialize/StructBsonSerialize.cs

@@ -3,6 +3,7 @@ using System.Reflection;
 using MongoDB.Bson.IO;
 using MongoDB.Bson.Serialization;
 using MongoDB.Bson.Serialization.Serializers;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace ET
 {
@@ -19,8 +20,13 @@ namespace ET
             FieldInfo[] fields = nominalType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
             foreach (FieldInfo field in fields)
             {
+                BsonElementAttribute bsonElement = field.GetCustomAttribute<BsonElementAttribute>();
+                if (bsonElement == null && !field.IsPublic)
+                {
+                    continue;
+                }
                 bsonWriter.WriteName(field.Name);
-                BsonSerializer.Serialize(bsonWriter, field.FieldType, field.GetValue(value));
+                BsonSerializer.Serialize(bsonWriter, field.FieldType, field.GetValue(value)); 
             }
 
             bsonWriter.WriteEndDocument();
@@ -42,7 +48,7 @@ namespace ET
                     case BsonReaderState.Name:
                     {
                         string name = bsonReader.ReadName(Utf8NameDecoder.Instance);
-                        FieldInfo field = actualType.GetField(name);
+                        FieldInfo field = actualType.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                         if (field != null)
                         {
                             object value = BsonSerializer.Deserialize(bsonReader, field.FieldType);

+ 20 - 37
Unity/Assets/Scripts/Editor/LogRedirection/LogRedirection.cs

@@ -1,4 +1,6 @@
 using System;
+using System.Diagnostics;
+using System.IO;
 using System.Reflection;
 using System.Text.RegularExpressions;
 using UnityEditor;
@@ -19,55 +21,36 @@ namespace ET
             {
                 return false;
             }
-            var stackTrace = GetStackTrace();
-            if (!string.IsNullOrEmpty(stackTrace))
+
+            Regex logFileRegex = new(@"((Log\.cs)|(UnityLogger\.cs)|(YooLogger\.cs))");
+            string codePath = AssetDatabase.GetAssetPath(instanceID);
+            if (logFileRegex.IsMatch(codePath))
             {
-                // 使用正则表达式匹配at的哪个脚本的哪一行
-                Match logMatches = Regex.Match(stackTrace, @"\(at (.+)\)",
-                    RegexOptions.IgnoreCase);
-                if (!logMatches.Success)
+                Match stackLineMatch = Regex.Match(GetStackTrace(), @"\(at (.+):([0-9]+)\)");
+                while (stackLineMatch.Success)
                 {
-                    Match compileErrorMatch = Regex.Match(stackTrace, @"(.*?)\(([0-9]+),([0-9]+)\): error");
-                    if (compileErrorMatch.Success)
+                    codePath = stackLineMatch.Groups[1].Value;
+                    if (!logFileRegex.IsMatch(codePath))
                     {
-                        OpenIDE(compileErrorMatch.Groups[1].Value, Convert.ToInt32(compileErrorMatch.Groups[2].Value), Convert.ToInt32(compileErrorMatch.Groups[3].Value));
+                        int matchLine = int.Parse(stackLineMatch.Groups[2].Value);
+                        OpenIDE(codePath, matchLine);
+                        return true;
                     }
+                    stackLineMatch = stackLineMatch.NextMatch();
                 }
-                while (logMatches.Success)
-                {
-                    var pathLine = logMatches.Groups[1].Value;
-
-                    if (!pathLine.Contains("Log.cs") && 
-                        !pathLine.Contains("UnityLogger.cs") &&
-                        !pathLine.Contains("YooLogger.cs:"))
-                    {
-                        var splitIndex = pathLine.LastIndexOf(":", StringComparison.Ordinal);
-                        // 脚本路径
-                        var path = pathLine.Substring(0, splitIndex);
-                        // 行号
-                        line = Convert.ToInt32(pathLine.Substring(splitIndex + 1));
-                        OpenIDE(path, line);
-                        break;
-                    }
-
-                    logMatches = logMatches.NextMatch();
-                }
-
-                return true;
             }
-
+            
             return false;
         }
 
         private static void OpenIDE(string path, int line, int column = 0)
         {
-            var fullPath = UnityEngine.Application.dataPath.Substring(0, UnityEngine.Application.dataPath.LastIndexOf("Assets", StringComparison.Ordinal));
-            fullPath = $"{fullPath}{path}";
-#if UNITY_STANDALONE_WIN
-                        fullPath = fullPath.Replace('/', '\\');
-#endif
+            if (!Path.IsPathFullyQualified(path))
+            {
+                path = Path.GetFullPath(path);
+            }
             // 跳转到目标代码的特定行
-            InternalEditorUtility.OpenFileAtLineExternal(fullPath, line, column);
+            InternalEditorUtility.OpenFileAtLineExternal(path, line, column);
         }
 
         /// <summary>