Skip to content
This repository was archived by the owner on Jun 5, 2024. It is now read-only.

Use ordinal string comparison #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/CacheDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Apkd.Internal
{
internal sealed class CacheDictionary<TKey, TValue> where TKey : class where TValue : class
sealed class CacheDictionary<TKey, TValue> where TKey : class where TValue : class
{
readonly int cacheSize;
readonly Queue<object> objectReferenceQueue;
readonly System.Runtime.CompilerServices.ConditionalWeakTable<TKey, TValue> weakTable
= new System.Runtime.CompilerServices.ConditionalWeakTable<TKey, TValue>();

const int defaultCacheSize = 256;
const int DefaultCacheSize = 256;

public CacheDictionary(int cacheSize = 256)
public CacheDictionary(int cacheSize = DefaultCacheSize)
#if APKD_STACKTRACE_NOCACHE
{ }
#else
Expand Down
4 changes: 2 additions & 2 deletions src/Detour.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Reflection;
using System.Diagnostics;
using static System.Reflection.BindingFlags;
// ReSharper disable once RedundantUsingDirective
using static Apkd.Internal.UnityEditorOverrides;

namespace Apkd.Internal
Expand All @@ -11,7 +11,7 @@ static class Detour
{
// This is based on an interesting technique from the RimWorld ComunityCoreLibrary project, originally credited to RawCode:
// https://github.com/RimWorldCCLTeam/CommunityCoreLibrary/blob/master/DLL_Project/Classes/Static/Detours.cs
internal static unsafe void TryDetourFromTo(MethodInfo src, MethodInfo dst)
static unsafe void TryDetourFromTo(MethodInfo src, MethodInfo dst)
{
try
{
Expand Down
31 changes: 17 additions & 14 deletions src/EnhancedStackFrame.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Ben A Adams. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Reflection;

namespace Apkd.Internal
{
internal sealed class EnhancedStackFrame : StackFrame
sealed class EnhancedStackFrame : StackFrame
{
string _fileName;
int _lineNumber;
Expand Down Expand Up @@ -51,31 +52,33 @@ public override string GetFileName()
if (string.IsNullOrWhiteSpace(_fileName))
return null;

if (_fileName.StartsWith(@"C:\buildslave\unity", System.StringComparison.Ordinal))
return null;

return System.IO.Path.GetFileName(_fileName);
// return _fileName;
return !_fileName.StartsWith(@"C:\buildslave\unity", StringComparison.Ordinal)
? System.IO.Path.GetFileName(_fileName)
: null;
}

internal string GetFullFilename()
{
if (string.IsNullOrWhiteSpace(_fileName))
return null;
int index = _fileName.IndexOf("\\Assets\\");
if (index >= 0)
return _fileName.Substring(index + 1);
return _fileName;

int index = _fileName.IndexOf("\\Assets\\", StringComparison.Ordinal);

return index >= 0
? _fileName.Substring(index + 1)
: _fileName;
}

internal StringBuilder AppendFullFilename(StringBuilder sb)
{
if (string.IsNullOrWhiteSpace(_fileName))
return sb;
int index = _fileName.IndexOf("\\Assets\\");
if (index >= 0)
return sb.Append(_fileName, index + 1);
return sb.Append(_fileName);

int index = _fileName.IndexOf("\\Assets\\", StringComparison.Ordinal);

return index >= 0
? sb.Append(_fileName, index + 1)
: sb.Append(_fileName);
}

internal bool IsEmpty => MethodInfo == null;
Expand Down
100 changes: 53 additions & 47 deletions src/EnhancedStackTrace.Frames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Apkd.Internal
{
internal sealed partial class EnhancedStackTrace
sealed partial class EnhancedStackTrace
{
static readonly Type StackTraceHiddenAttibuteType = Type.GetType("System.Diagnostics.StackTraceHiddenAttribute", false);
static readonly MethodInfo UnityEditorInspectorWindowOnGuiMethod = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.InspectorWindow", false)?.GetMethod("OnGUI", NonPublic | Instance);
Expand Down Expand Up @@ -52,8 +52,8 @@ IEnumerable<EnhancedStackFrame> EnumerateEnhancedFrames(StackTrace st)
IEnumerable<StackFrame> EnumerateFrames(StackTrace st2)
{
foreach (var inner in GetInnerStackTraces(st2))
foreach (var frame in EnumerateFrames(inner))
yield return frame;
foreach (var frame in EnumerateFrames(inner))
yield return frame;

for (var i = 0; i < st2.FrameCount; i++)
yield return st2.GetFrame(i);
Expand All @@ -65,7 +65,8 @@ EnhancedStackFrame MakeEnhancedFrame(StackFrame frame, MethodBase method)
methodInfo: GetResolvedMethod(method),
fileName: frame.GetFileName(),
lineNumber: frame.GetFileLineNumber(),
colNumber: frame.GetFileColumnNumber());
colNumber: frame.GetFileColumnNumber()
);

bool collapseNext = false;
StackFrame current = null;
Expand All @@ -74,35 +75,35 @@ EnhancedStackFrame MakeEnhancedFrame(StackFrame frame, MethodBase method)
{
try
{
if (current != null)
{
var method = current.GetMethod();
if (current == null)
continue;

if (method == null) // TODO: remove this
continue;
var method = current.GetMethod();

bool shouldExcludeFromCollapse = ShouldExcludeFromCollapse(method);
if (shouldExcludeFromCollapse)
collapseNext = false;
if (method == null) // TODO: remove this
continue;

bool shouldExcludeFromCollapse = ShouldExcludeFromCollapse(method);
if (shouldExcludeFromCollapse)
collapseNext = false;

if ((collapseNext || ShouldCollapseStackFrames(method)) && !shouldExcludeFromCollapse)
if ((collapseNext || ShouldCollapseStackFrames(method)) && !shouldExcludeFromCollapse)
{
if (ShouldCollapseStackFrames(next.GetMethod()))
{
if (ShouldCollapseStackFrames(next.GetMethod()))
{
collapseNext = true;
continue;
}
else
{
collapseNext = false;
}
collapseNext = true;
continue;
}
else
{
collapseNext = false;
}
}

if (!ShouldShowInStackTrace(method))
continue;
if (!ShouldShowInStackTrace(method))
continue;

yield return MakeEnhancedFrame(current, method);
}
yield return MakeEnhancedFrame(current, method);
}
finally
{
Expand All @@ -125,6 +126,7 @@ static bool IsDefined<T>(MemberInfo member)
foreach (var attr in GetCustomAttributes(member))
if (attr is T)
return true;

return false;
}

Expand Down Expand Up @@ -251,7 +253,7 @@ internal static ResolvedMethod GetResolvedMethodInternal(MethodBase methodBase)
if (i > 0)
builder.Append(',').Append(' ');

TypeNameHelper.AppendTypeDisplayName(builder, genericArguments[i], fullName: false, includeGenericParameterNames: true);
builder.AppendTypeDisplayName(genericArguments[i], fullName: false, includeGenericParameterNames: true);
}

builder.Append('>');
Expand Down Expand Up @@ -317,14 +319,16 @@ static bool TryResolveGeneratedName(ref MethodBase method, out Type type, out st
var localNameStart = generatedName.IndexOf((char)kind, closeBracketOffset + 1);
if (localNameStart < 0)
break;

localNameStart += 3;

if (localNameStart < generatedName.Length)
{
var localNameEnd = generatedName.IndexOf("|", localNameStart);
var localNameEnd = generatedName.IndexOf("|", localNameStart, StringComparison.Ordinal);
if (localNameEnd > 0)
subMethodName = generatedName.Substring(localNameStart, localNameEnd - localNameStart);
}

break;
case GeneratedNameKind.LambdaMethod:
subMethodName = "";
Expand Down Expand Up @@ -401,6 +405,7 @@ static bool TryResolveSourceMethod(IEnumerable<MethodBase> candidateMethods, Gen
var rawIL = methodBody?.GetILAsByteArray();
if (rawIL == null)
continue;

var reader = new ILReader(rawIL);
while (reader.Read(candidateMethod))
{
Expand Down Expand Up @@ -475,15 +480,17 @@ static string GetMatchHint(GeneratedNameKind kind, MethodBase method)
switch (kind)
{
case GeneratedNameKind.LocalFunction:
var start = methodName.IndexOf("|");
var start = methodName.IndexOf("|", StringComparison.Ordinal);
if (start < 1)
return null;
var end = methodName.IndexOf("_", start) + 1;

var end = methodName.IndexOf("_", start, StringComparison.Ordinal) + 1;
if (end <= start)
return null;

return methodName.Substring(start, end - start);
}

return null;
}

Expand Down Expand Up @@ -593,14 +600,16 @@ static ResolvedParameter GetParameter(ParameterInfo parameter)
if (attr is Attribute tena && tena.IsTupleElementNameAttribute())
tupleNameAttribute = tena;

// ReSharper disable ExpressionIsAlwaysNull
#if APKD_STACKTRACE_FULLPARAMS
var tupleNames = tupleNameAttribute?.GetTransformNames();
#else
var tupleNames = null as IList<String>;
var tupleNames = null as IList<string>;
#endif

if (tupleNameAttribute != null)
return GetValueTupleParameter(tupleNames, prefix, parameter.Name, parameterType);
// ReSharper restore ExpressionIsAlwaysNull
}

if (parameterType.IsByRef)
Expand Down Expand Up @@ -635,8 +644,7 @@ static string GetValueTupleParameterName(IList<string> tupleNames, Type paramete
if (i > 0)
sb.Append(',').Append(' ');


TypeNameHelper.AppendTypeDisplayName(sb, args[i], fullName: false, includeGenericParameterNames: true);
sb.AppendTypeDisplayName(args[i], fullName: false, includeGenericParameterNames: true);

if (i >= tupleNames.Count)
continue;
Expand All @@ -657,23 +665,20 @@ static bool ShouldCollapseStackFrames(MethodBase method)
{
var comparison = StringComparison.Ordinal;
string typeName = method?.DeclaringType?.FullName;

if (string.IsNullOrWhiteSpace(typeName))
return false;

return typeName.StartsWith("UnityEditor.", comparison) ||
typeName.StartsWith("UnityEngine.", comparison) ||
typeName.StartsWith("System.", comparison) ||
typeName.StartsWith("UnityScript.Lang.", comparison) ||
typeName.StartsWith("Odin.Editor.", comparison) ||
typeName.StartsWith("Boo.Lang.", comparison);
typeName.StartsWith("UnityEngine.", comparison) ||
typeName.StartsWith("System.", comparison) ||
typeName.StartsWith("UnityScript.Lang.", comparison) ||
typeName.StartsWith("Odin.Editor.", comparison) ||
typeName.StartsWith("Boo.Lang.", comparison);
}

static bool ShouldExcludeFromCollapse(MethodBase method)
{
if (method == UnityEditorInspectorWindowOnGuiMethod)
return true;

return false;
}
=> method == UnityEditorInspectorWindowOnGuiMethod;

static bool ShouldShowInStackTrace(MethodBase method)
{
Expand Down Expand Up @@ -734,9 +739,9 @@ static bool ShouldShowInStackTrace(MethodBase method)
return false;
}
else if (type == typeof(TaskAwaiter) ||
type == typeof(TaskAwaiter<>) ||
type == typeof(ConfiguredTaskAwaitable.ConfiguredTaskAwaiter) ||
type == typeof(ConfiguredTaskAwaitable<>.ConfiguredTaskAwaiter))
type == typeof(TaskAwaiter<>) ||
type == typeof(ConfiguredTaskAwaitable.ConfiguredTaskAwaiter) ||
type == typeof(ConfiguredTaskAwaitable<>.ConfiguredTaskAwaiter))
{
switch (method.Name)
{
Expand Down Expand Up @@ -791,6 +796,7 @@ static bool IsStackTraceHidden(MemberInfo memberInfo)
{
if (attr.GetType() == StackTraceHiddenAttibuteType)
return true;

return false;
}
}
Expand Down
Loading