Skip to content

Commit

Permalink
Mark UdonSharpEditorCache as public and clean up API for getting info
Browse files Browse the repository at this point in the history
- Requested by Orels to mark the class public
  • Loading branch information
MerlinVR committed Mar 28, 2021
1 parent 1c8cbd1 commit 2cb4a66
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
26 changes: 23 additions & 3 deletions Assets/UdonSharp/Editor/UdonSharpClassDebugInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@


using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -37,15 +38,15 @@ public class DebugLineSpan
private List<DebugLineSpan> debugSpans;
private bool includeInlineCode;

public ClassDebugInfo(string source, bool includeInlineCodeIn)
internal ClassDebugInfo(string source, bool includeInlineCodeIn)
{
sourceText = source;
mostRecentSpanStart = 0;
debugSpans = new List<DebugLineSpan>();
includeInlineCode = includeInlineCodeIn;
}

public void UpdateSyntaxNode(SyntaxNode node)
internal void UpdateSyntaxNode(SyntaxNode node)
{
if (debugSpans.Count == 0)
debugSpans.Add(new DebugLineSpan());
Expand Down Expand Up @@ -102,7 +103,7 @@ public void UpdateSyntaxNode(SyntaxNode node)
}
}

public void FinalizeDebugInfo()
internal void FinalizeDebugInfo()
{
serializedDebugSpans = new DebugLineSpan[debugSpans.Count];

Expand Down Expand Up @@ -139,5 +140,24 @@ public void FinalizeDebugInfo()
serializedDebugSpans[i].lineChar = lineChar;
}
}

/// <summary>
/// Gets the debug line span from a given program counter
/// </summary>
/// <param name="programCounter"></param>
/// <returns></returns>
[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;
}
}
}
18 changes: 16 additions & 2 deletions Assets/UdonSharp/Editor/UdonSharpEditorCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using JetBrains.Annotations;
using System.Collections.Generic;
using System.IO;
using UdonSharp.Compiler;
Expand All @@ -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.
/// </summary>
[InitializeOnLoad]
internal class UdonSharpEditorCache
public class UdonSharpEditorCache
{
#region Instance and serialization management
[System.Serializable]
Expand All @@ -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()
{
Expand Down Expand Up @@ -298,6 +299,13 @@ void SaveDebugInfo(UdonSharpProgramAsset sourceProgram, DebugInfoType debugInfoT

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

/// <summary>
/// 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.
/// </summary>
/// <param name="sourceProgram"></param>
/// <param name="debugInfoType"></param>
/// <returns></returns>
[PublicAPI]
public ClassDebugInfo GetDebugInfo(UdonSharpProgramAsset sourceProgram, DebugInfoType debugInfoType)
{
if (!_classDebugInfoLookup.TryGetValue(sourceProgram, out var debugInfo))
Expand Down Expand Up @@ -392,6 +400,12 @@ void FlushUasmCache()
}
}

/// <summary>
/// Gets the uasm string for the last build of the given program asset
/// </summary>
/// <param name="programAsset"></param>
/// <returns></returns>
[PublicAPI]
public string GetUASMStr(UdonSharpProgramAsset programAsset)
{
if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(programAsset, out string guid, out long _))
Expand Down
8 changes: 1 addition & 7 deletions Assets/UdonSharp/Editor/UdonSharpRuntimeLogWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? $"[<color=#575ff2>{prePrefix}</color>]" : "", assetInfo.Item1, debugLineSpan.line, debugLineSpan.lineChar);
}
Expand Down

0 comments on commit 2cb4a66

Please sign in to comment.