From 2cb4a66707406a7779c0d26f78b56bb5cfc8a7dc Mon Sep 17 00:00:00 2001 From: Merlin <36685500+MerlinVR@users.noreply.github.com> Date: Sat, 27 Mar 2021 21:05:19 -0700 Subject: [PATCH] Mark UdonSharpEditorCache as public and clean up API for getting info - Requested by Orels to mark the class public --- .../Editor/UdonSharpClassDebugInfo.cs | 26 ++++++++++++++++--- .../UdonSharp/Editor/UdonSharpEditorCache.cs | 18 +++++++++++-- .../Editor/UdonSharpRuntimeLogWatcher.cs | 8 +----- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/Assets/UdonSharp/Editor/UdonSharpClassDebugInfo.cs b/Assets/UdonSharp/Editor/UdonSharpClassDebugInfo.cs index 5cf33df0..c0fce688 100644 --- a/Assets/UdonSharp/Editor/UdonSharpClassDebugInfo.cs +++ b/Assets/UdonSharp/Editor/UdonSharpClassDebugInfo.cs @@ -1,5 +1,6 @@  +using JetBrains.Annotations; using Microsoft.CodeAnalysis; using System.Collections.Generic; using System.Linq; @@ -37,7 +38,7 @@ public class DebugLineSpan private List debugSpans; private bool includeInlineCode; - public ClassDebugInfo(string source, bool includeInlineCodeIn) + internal ClassDebugInfo(string source, bool includeInlineCodeIn) { sourceText = source; mostRecentSpanStart = 0; @@ -45,7 +46,7 @@ public ClassDebugInfo(string source, bool includeInlineCodeIn) includeInlineCode = includeInlineCodeIn; } - public void UpdateSyntaxNode(SyntaxNode node) + internal void UpdateSyntaxNode(SyntaxNode node) { if (debugSpans.Count == 0) debugSpans.Add(new DebugLineSpan()); @@ -102,7 +103,7 @@ public void UpdateSyntaxNode(SyntaxNode node) } } - public void FinalizeDebugInfo() + internal void FinalizeDebugInfo() { serializedDebugSpans = new DebugLineSpan[debugSpans.Count]; @@ -139,5 +140,24 @@ public void FinalizeDebugInfo() serializedDebugSpans[i].lineChar = lineChar; } } + + /// + /// Gets the debug line span from a given program counter + /// + /// + /// + [PublicAPI] + public DebugLineSpan GetLineFromProgramCounter(int programCounter) + { + int debugSpanIdx = System.Array.BinarySearch(DebugLineSpans.Select(e => e.endInstruction).ToArray(), programCounter); + if (debugSpanIdx < 0) + debugSpanIdx = ~debugSpanIdx; + + debugSpanIdx = UnityEngine.Mathf.Clamp(debugSpanIdx, 0, DebugLineSpans.Length - 1); + + ClassDebugInfo.DebugLineSpan debugLineSpan = DebugLineSpans[debugSpanIdx]; + + return debugLineSpan; + } } } diff --git a/Assets/UdonSharp/Editor/UdonSharpEditorCache.cs b/Assets/UdonSharp/Editor/UdonSharpEditorCache.cs index 4eb01785..150da8c7 100644 --- a/Assets/UdonSharp/Editor/UdonSharpEditorCache.cs +++ b/Assets/UdonSharp/Editor/UdonSharpEditorCache.cs @@ -1,4 +1,5 @@  +using JetBrains.Annotations; using System.Collections.Generic; using System.IO; using UdonSharp.Compiler; @@ -13,7 +14,7 @@ namespace UdonSharp /// Handles cache data for U# that gets saved to the Library. All data this uses is intermediate generated data that is not required and can be regenerated from the source files. /// [InitializeOnLoad] - internal class UdonSharpEditorCache + public class UdonSharpEditorCache { #region Instance and serialization management [System.Serializable] @@ -31,7 +32,7 @@ struct SourceHashLookupStorage public static UdonSharpEditorCache Instance => GetInstance(); static UdonSharpEditorCache _instance; - static object instanceLock = new object(); + static readonly object instanceLock = new object(); private static UdonSharpEditorCache GetInstance() { @@ -298,6 +299,13 @@ void SaveDebugInfo(UdonSharpProgramAsset sourceProgram, DebugInfoType debugInfoT Dictionary> _classDebugInfoLookup = new Dictionary>(); + /// + /// Gets the debug info for a given program asset. If debug info type for Client is specified when there is no client debug info, will fall back to Editor debug info. + /// + /// + /// + /// + [PublicAPI] public ClassDebugInfo GetDebugInfo(UdonSharpProgramAsset sourceProgram, DebugInfoType debugInfoType) { if (!_classDebugInfoLookup.TryGetValue(sourceProgram, out var debugInfo)) @@ -392,6 +400,12 @@ void FlushUasmCache() } } + /// + /// Gets the uasm string for the last build of the given program asset + /// + /// + /// + [PublicAPI] public string GetUASMStr(UdonSharpProgramAsset programAsset) { if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(programAsset, out string guid, out long _)) diff --git a/Assets/UdonSharp/Editor/UdonSharpRuntimeLogWatcher.cs b/Assets/UdonSharp/Editor/UdonSharpRuntimeLogWatcher.cs index 7d584f86..0f451ba9 100644 --- a/Assets/UdonSharp/Editor/UdonSharpRuntimeLogWatcher.cs +++ b/Assets/UdonSharp/Editor/UdonSharpRuntimeLogWatcher.cs @@ -479,13 +479,7 @@ static void HandleLogError(string errorStr, string logPrefix, string prePrefix) if (debugInfo == null) return; - int debugSpanIdx = System.Array.BinarySearch(debugInfo.DebugLineSpans.Select(e => e.endInstruction).ToArray(), programCounter); - if (debugSpanIdx < 0) - debugSpanIdx = ~debugSpanIdx; - - debugSpanIdx = Mathf.Clamp(debugSpanIdx, 0, debugInfo.DebugLineSpans.Length - 1); - - ClassDebugInfo.DebugLineSpan debugLineSpan = debugInfo.DebugLineSpans[debugSpanIdx]; + ClassDebugInfo.DebugLineSpan debugLineSpan = debugInfo.GetLineFromProgramCounter(programCounter); UdonSharpUtils.LogRuntimeError($"{logPrefix}\n{errorMessage}", prePrefix != null ? $"[{prePrefix}]" : "", assetInfo.Item1, debugLineSpan.line, debugLineSpan.lineChar); }