Skip to content

Commit 2cb4a66

Browse files
committed
Mark UdonSharpEditorCache as public and clean up API for getting info
- Requested by Orels to mark the class public
1 parent 1c8cbd1 commit 2cb4a66

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

Assets/UdonSharp/Editor/UdonSharpClassDebugInfo.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22

3+
using JetBrains.Annotations;
34
using Microsoft.CodeAnalysis;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -37,15 +38,15 @@ public class DebugLineSpan
3738
private List<DebugLineSpan> debugSpans;
3839
private bool includeInlineCode;
3940

40-
public ClassDebugInfo(string source, bool includeInlineCodeIn)
41+
internal ClassDebugInfo(string source, bool includeInlineCodeIn)
4142
{
4243
sourceText = source;
4344
mostRecentSpanStart = 0;
4445
debugSpans = new List<DebugLineSpan>();
4546
includeInlineCode = includeInlineCodeIn;
4647
}
4748

48-
public void UpdateSyntaxNode(SyntaxNode node)
49+
internal void UpdateSyntaxNode(SyntaxNode node)
4950
{
5051
if (debugSpans.Count == 0)
5152
debugSpans.Add(new DebugLineSpan());
@@ -102,7 +103,7 @@ public void UpdateSyntaxNode(SyntaxNode node)
102103
}
103104
}
104105

105-
public void FinalizeDebugInfo()
106+
internal void FinalizeDebugInfo()
106107
{
107108
serializedDebugSpans = new DebugLineSpan[debugSpans.Count];
108109

@@ -139,5 +140,24 @@ public void FinalizeDebugInfo()
139140
serializedDebugSpans[i].lineChar = lineChar;
140141
}
141142
}
143+
144+
/// <summary>
145+
/// Gets the debug line span from a given program counter
146+
/// </summary>
147+
/// <param name="programCounter"></param>
148+
/// <returns></returns>
149+
[PublicAPI]
150+
public DebugLineSpan GetLineFromProgramCounter(int programCounter)
151+
{
152+
int debugSpanIdx = System.Array.BinarySearch(DebugLineSpans.Select(e => e.endInstruction).ToArray(), programCounter);
153+
if (debugSpanIdx < 0)
154+
debugSpanIdx = ~debugSpanIdx;
155+
156+
debugSpanIdx = UnityEngine.Mathf.Clamp(debugSpanIdx, 0, DebugLineSpans.Length - 1);
157+
158+
ClassDebugInfo.DebugLineSpan debugLineSpan = DebugLineSpans[debugSpanIdx];
159+
160+
return debugLineSpan;
161+
}
142162
}
143163
}

Assets/UdonSharp/Editor/UdonSharpEditorCache.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
using JetBrains.Annotations;
23
using System.Collections.Generic;
34
using System.IO;
45
using UdonSharp.Compiler;
@@ -13,7 +14,7 @@ namespace UdonSharp
1314
/// 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.
1415
/// </summary>
1516
[InitializeOnLoad]
16-
internal class UdonSharpEditorCache
17+
public class UdonSharpEditorCache
1718
{
1819
#region Instance and serialization management
1920
[System.Serializable]
@@ -31,7 +32,7 @@ struct SourceHashLookupStorage
3132
public static UdonSharpEditorCache Instance => GetInstance();
3233

3334
static UdonSharpEditorCache _instance;
34-
static object instanceLock = new object();
35+
static readonly object instanceLock = new object();
3536

3637
private static UdonSharpEditorCache GetInstance()
3738
{
@@ -298,6 +299,13 @@ void SaveDebugInfo(UdonSharpProgramAsset sourceProgram, DebugInfoType debugInfoT
298299

299300
Dictionary<UdonSharpProgramAsset, Dictionary<DebugInfoType, ClassDebugInfo>> _classDebugInfoLookup = new Dictionary<UdonSharpProgramAsset, Dictionary<DebugInfoType, ClassDebugInfo>>();
300301

302+
/// <summary>
303+
/// 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.
304+
/// </summary>
305+
/// <param name="sourceProgram"></param>
306+
/// <param name="debugInfoType"></param>
307+
/// <returns></returns>
308+
[PublicAPI]
301309
public ClassDebugInfo GetDebugInfo(UdonSharpProgramAsset sourceProgram, DebugInfoType debugInfoType)
302310
{
303311
if (!_classDebugInfoLookup.TryGetValue(sourceProgram, out var debugInfo))
@@ -392,6 +400,12 @@ void FlushUasmCache()
392400
}
393401
}
394402

403+
/// <summary>
404+
/// Gets the uasm string for the last build of the given program asset
405+
/// </summary>
406+
/// <param name="programAsset"></param>
407+
/// <returns></returns>
408+
[PublicAPI]
395409
public string GetUASMStr(UdonSharpProgramAsset programAsset)
396410
{
397411
if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(programAsset, out string guid, out long _))

Assets/UdonSharp/Editor/UdonSharpRuntimeLogWatcher.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,7 @@ static void HandleLogError(string errorStr, string logPrefix, string prePrefix)
479479
if (debugInfo == null)
480480
return;
481481

482-
int debugSpanIdx = System.Array.BinarySearch(debugInfo.DebugLineSpans.Select(e => e.endInstruction).ToArray(), programCounter);
483-
if (debugSpanIdx < 0)
484-
debugSpanIdx = ~debugSpanIdx;
485-
486-
debugSpanIdx = Mathf.Clamp(debugSpanIdx, 0, debugInfo.DebugLineSpans.Length - 1);
487-
488-
ClassDebugInfo.DebugLineSpan debugLineSpan = debugInfo.DebugLineSpans[debugSpanIdx];
482+
ClassDebugInfo.DebugLineSpan debugLineSpan = debugInfo.GetLineFromProgramCounter(programCounter);
489483

490484
UdonSharpUtils.LogRuntimeError($"{logPrefix}\n{errorMessage}", prePrefix != null ? $"[<color=#575ff2>{prePrefix}</color>]" : "", assetInfo.Item1, debugLineSpan.line, debugLineSpan.lineChar);
491485
}

0 commit comments

Comments
 (0)