From d54bea84175a32900660c280bd8b564afae58bd3 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Tue, 10 Oct 2023 22:48:02 -0700 Subject: [PATCH] hactoolnet: Make some changes for building with NativeAOT --- src/hactoolnet/HomeFolder.cs | 2 +- src/hactoolnet/Program.cs | 4 +-- src/hactoolnet/ResultLogger.cs | 48 +++++++++++++++------------- src/hactoolnet/ResultNameResolver.cs | 6 ++-- src/hactoolnet/hactoolnet.csproj | 3 +- 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/hactoolnet/HomeFolder.cs b/src/hactoolnet/HomeFolder.cs index 0270aa70..fdbd1b26 100644 --- a/src/hactoolnet/HomeFolder.cs +++ b/src/hactoolnet/HomeFolder.cs @@ -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; diff --git a/src/hactoolnet/Program.cs b/src/hactoolnet/Program.cs index 9a540320..75cd16b3 100644 --- a/src/hactoolnet/Program.cs +++ b/src/hactoolnet/Program.cs @@ -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); diff --git a/src/hactoolnet/ResultLogger.cs b/src/hactoolnet/ResultLogger.cs index a073f948..e652ab34 100644 --- a/src/hactoolnet/ResultLogger.cs +++ b/src/hactoolnet/ResultLogger.cs @@ -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; diff --git a/src/hactoolnet/ResultNameResolver.cs b/src/hactoolnet/ResultNameResolver.cs index 650aaa38..56792b27 100644 --- a/src/hactoolnet/ResultNameResolver.cs +++ b/src/hactoolnet/ResultNameResolver.cs @@ -1,4 +1,5 @@ -using System; +#if !NATIVEAOT +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -36,4 +37,5 @@ internal class ResultNameResolver : Result.IResultNameResolver return dict; } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/hactoolnet/hactoolnet.csproj b/src/hactoolnet/hactoolnet.csproj index 0726f025..aa2e3102 100644 --- a/src/hactoolnet/hactoolnet.csproj +++ b/src/hactoolnet/hactoolnet.csproj @@ -25,6 +25,7 @@ true + NATIVEAOT;$(DefineConstants) @@ -37,7 +38,7 @@ true - CORERT_NO_REFLECTION;$(DefineConstants) + NATIVEAOT_NO_REFLECTION;$(DefineConstants)