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. // 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. // 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;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;

View file

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

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using LibHac; using LibHac;
@ -8,6 +9,7 @@ using LibHac.Fs.Fsa;
namespace hactoolnet; 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 internal class ResultLogger : Result.IResultLogger, IDisposable
{ {
private TextWriter Writer { get; } private TextWriter Writer { get; }
@ -40,17 +42,19 @@ internal class ResultLogger : Result.IResultLogger, IDisposable
StackTrace st = GetStackTrace(); StackTrace st = GetStackTrace();
MethodBase method = st.GetFrame(0)?.GetMethod(); MethodBase method = st.GetFrame(0)?.GetMethod();
if (method is null) 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(nameof(IAttributeFileSystem.GetFileAttributes)))
{ {
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)); AddLogEntry(new LogEntry(result, st));
@ -94,28 +98,28 @@ internal class ResultLogger : Result.IResultLogger, IDisposable
private void PrintLogEntry(LogEntry entry) 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; bool printStackTrace = PrintStackTrace && !entry.IsConvertedResult;
// Make sure there's a new line if printing a stack trace // 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 // 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 entryText = printStackTrace && !LastEntryPrintedNewLine ? Environment.NewLine : string.Empty;
string lineNumber = entry.LineNumber > 0 ? $":line{entry.LineNumber}" : string.Empty;
if (entry.IsConvertedResult) 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 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) if (entry.TimesCalled > 1)
@ -202,7 +206,7 @@ internal class ResultLogger : Result.IResultLogger, IDisposable
if (method is null) if (method is null)
{ {
CallingMethod = string.Empty; CallingMethod = string.Empty;
StackTraceText = string.Empty; StackTraceText = stackTrace.ToString();
LineNumber = 0; LineNumber = 0;
TimesCalled = 1; TimesCalled = 1;

View file

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

View file

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