hactoolnet: Make some changes for building with NativeAOT

This commit is contained in:
Alex Barney 2023-10-10 22:48:02 -07:00
parent 316a9302c9
commit d54bea8417
5 changed files with 35 additions and 28 deletions

View file

@ -6,7 +6,7 @@
// This code is copied from the .NET runtime with modifications to avoid that.
// The downside is that it won't work in Linux unless the HOME environmental variable is set.
#if CORERT_NO_REFLECTION
#if NATIVEAOT_NO_REFLECTION
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

View file

@ -45,7 +45,7 @@ public static class Program
Console.Error.WriteLine($"\nERROR: {ex.Message}\n");
Console.Error.WriteLine("Additional information:");
#if !CORERT_NO_REFLECTION
#if !NATIVEAOT_NO_REFLECTION
Console.Error.WriteLine(ex.GetType().FullName);
#endif
@ -189,7 +189,7 @@ public static class Program
private static void OpenKeySet(Context ctx)
{
#if CORERT_NO_REFLECTION
#if NATIVEAOT_NO_REFLECTION
string home = HomeFolder.GetFolderPath(Environment.SpecialFolder.UserProfile);
#else
string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

View file

@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using LibHac;
@ -8,6 +9,7 @@ using LibHac.Fs.Fsa;
namespace hactoolnet;
[UnconditionalSuppressMessage("Trimmer", "IL2026", Justification = "If MethodInfo from GetMethod is missing, the logger will simply not log the method info.")]
internal class ResultLogger : Result.IResultLogger, IDisposable
{
private TextWriter Writer { get; }
@ -40,17 +42,19 @@ internal class ResultLogger : Result.IResultLogger, IDisposable
StackTrace st = GetStackTrace();
MethodBase method = st.GetFrame(0)?.GetMethod();
if (method is null)
return;
// This result from these functions is usually noise because they
// are frequently used to detect if a file exists
if (ResultFs.PathNotFound.Includes(result) &&
typeof(IFileSystem).IsAssignableFrom(method.DeclaringType) &&
method.Name.StartsWith(nameof(IFileSystem.GetEntryType)) ||
method.Name.StartsWith(nameof(IAttributeFileSystem.GetFileAttributes)))
if (method is not null)
{
return;
// This result from these functions is usually noise because they
// are frequently used to detect if a file exists
if (ResultFs.PathNotFound.Includes(result) &&
typeof(IFileSystem).IsAssignableFrom(method.DeclaringType) &&
(method.Name.StartsWith(nameof(IFileSystem.GetEntryType)) ||
method.Name.StartsWith($"Do{nameof(IFileSystem.GetEntryType)}") ||
method.Name.StartsWith(nameof(IAttributeFileSystem.GetFileAttributes)) ||
method.Name.StartsWith($"Do{nameof(IAttributeFileSystem.GetFileAttributes)}")))
{
return;
}
}
AddLogEntry(new LogEntry(result, st));
@ -94,28 +98,28 @@ internal class ResultLogger : Result.IResultLogger, IDisposable
private void PrintLogEntry(LogEntry entry)
{
MethodBase method = entry.StackTrace.GetFrame(0)?.GetMethod();
if (method is null)
return;
string methodName = $"{method.DeclaringType?.FullName}.{method.Name}";
bool printStackTrace = PrintStackTrace && !entry.IsConvertedResult;
// Make sure there's a new line if printing a stack trace
// A stack trace includes a new line at the end of it, so add the new line only if needed
string entryText = printStackTrace && !LastEntryPrintedNewLine ? Environment.NewLine : string.Empty;
string lineNumber = entry.LineNumber > 0 ? $":line{entry.LineNumber}" : string.Empty;
if (entry.IsConvertedResult)
{
entryText += $"{entry.OriginalResult.ToStringWithName()} was converted to {entry.Result.ToStringWithName()} by {methodName}{lineNumber}";
entryText += $"{entry.OriginalResult.ToStringWithName()} was converted to {entry.Result.ToStringWithName()}";
}
else
{
entryText += $"{entry.Result.ToStringWithName()} was returned by {methodName}{lineNumber}";
entryText += $"{entry.Result.ToStringWithName()} was returned";
}
MethodBase method = entry.StackTrace.GetFrame(0)?.GetMethod();
if (method is not null)
{
string methodName = $"{method.DeclaringType?.FullName}.{method.Name}";
string lineNumber = entry.LineNumber > 0 ? $":line{entry.LineNumber}" : string.Empty;
entryText += $" by {methodName}{lineNumber}";
}
if (entry.TimesCalled > 1)
@ -202,7 +206,7 @@ internal class ResultLogger : Result.IResultLogger, IDisposable
if (method is null)
{
CallingMethod = string.Empty;
StackTraceText = string.Empty;
StackTraceText = stackTrace.ToString();
LineNumber = 0;
TimesCalled = 1;

View file

@ -1,4 +1,5 @@
using System;
#if !NATIVEAOT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@ -37,3 +38,4 @@ internal class ResultNameResolver : Result.IResultNameResolver
return dict;
}
}
#endif

View file

@ -25,6 +25,7 @@
<PropertyGroup Condition=" $(BuildType.StartsWith('native')) ">
<PublishAot>true</PublishAot>
<DefineConstants>NATIVEAOT;$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" $(BuildType.StartsWith('native')) and '$(BuildType)' != 'native-untrimmed' ">
@ -37,7 +38,7 @@
<!-- No-reflection mode might not work on Linux if the HOME environment variable is not set -->
<PropertyGroup Condition=" '$(BuildType)' == 'native-noreflection' ">
<IlcDisableReflection>true</IlcDisableReflection>
<DefineConstants>CORERT_NO_REFLECTION;$(DefineConstants)</DefineConstants>
<DefineConstants>NATIVEAOT_NO_REFLECTION;$(DefineConstants)</DefineConstants>
</PropertyGroup>
</Project>