// // /*=============================================================================== // // Copyright (C) 2023 PhantomsXR Ltd. All Rights Reserved. // // // // This file is part of the UnityFusion.Runtime.CodeHook.Editor. // // // // The HandheldARPlatform cannot be copied, distributed, or made available to // // third-parties for commercial purposes without written permission of PhantomsXR Ltd. // // // // Contact info@phantomsxr.com for licensing requests. // // ===============================================================================*/ using System; using System.Reflection; using System.Text.RegularExpressions; using UnityEditor; using UnityEditor.Callbacks; using UnityEditorInternal; using UnityEngine; namespace Phantom.XRMOD.Runtime.Editor { public static class LogRedirection { private static readonly Regex CONST_REGEX = new Regex(@"\(at (.+)\:(\d+)\)\r?\n"); private static readonly Regex CONST_VIRT_REGEX = new Regex(@"IL_[0-9a-f]+: .*\r?\n.* \(at .+:\d+\)", RegexOptions.Multiline); [OnOpenAssetAttribute(0)] public static bool OpenLog(int _instanceID, int _line) { string tmp_SelectedStackTrace = GetSelectedStackTrace(); if (string.IsNullOrEmpty(tmp_SelectedStackTrace)) { return false; } if (!tmp_SelectedStackTrace.Contains("UnityFusion StackTrace")) { Match tmp_VirtMatch = CONST_VIRT_REGEX.Match(tmp_SelectedStackTrace); if (tmp_VirtMatch.Success) { var tmp_LineMatch = new Regex(@" \(at (.+)\:(\d+)\)"); var tmp_LineMatchResult = tmp_LineMatch.Match(tmp_VirtMatch.Groups[0].Value); if (tmp_LineMatchResult.Success) { InternalEditorUtility.OpenFileAtLineExternal(tmp_LineMatchResult.Groups[1].Value, int.Parse(tmp_LineMatchResult.Groups[2].Value)); return true; } } return false; } Match tmp_Match = CONST_REGEX.Match(tmp_SelectedStackTrace); Debug.Log($"{tmp_Match.Success}->{tmp_SelectedStackTrace}"); if (!tmp_Match.Success) return false; InternalEditorUtility.OpenFileAtLineExternal(tmp_Match.Groups[1].Value, int.Parse(tmp_Match.Groups[2].Value)); return true; } private static string GetSelectedStackTrace() { Assembly tmp_EditorWindowAssembly = typeof(EditorWindow).Assembly; Type tmp_ConsoleWindowType = tmp_EditorWindowAssembly.GetType("UnityEditor.ConsoleWindow"); if (tmp_ConsoleWindowType == null) return null; FieldInfo tmp_ConsoleWindowFieldInfo = tmp_ConsoleWindowType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic); if (tmp_ConsoleWindowFieldInfo == null) return null; EditorWindow tmp_ConsoleWindow = tmp_ConsoleWindowFieldInfo.GetValue(null) as EditorWindow; if (tmp_ConsoleWindow == null) return null; if (tmp_ConsoleWindow != EditorWindow.focusedWindow) return null; FieldInfo tmp_ActiveTextFieldInfo = tmp_ConsoleWindowType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic); if (tmp_ActiveTextFieldInfo == null) return null; return (string) tmp_ActiveTextFieldInfo.GetValue(tmp_ConsoleWindow); } } }