From adc2a2ba916597f0edfb590a149d7aaf0c6a33cc Mon Sep 17 00:00:00 2001 From: alkee Date: Sun, 31 Dec 2023 15:18:45 +0900 Subject: [PATCH 1/2] fix: invalid arch infos from native runtimes --- .gitignore | 9 +++++- .../Editor/DataClasses/NativePlatform.cs | 32 ++++++++++++++----- .../Editor/NuGetNativeImportSetting.cs | 3 +- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index a82d729..ec9b340 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /NuGetImporterForUnity/[Bb]uilds/ /NuGetImporterForUnity/[Ll]ogs/ /NuGetImporterForUnity/[Mm]emoryCaptures/ +/NuGetImporterForUnity/[Uu]ser[Ss]ettings/ /Packager/[Ll]ibrary/ /Packager/[Tt]emp/ /Packager/[Oo]bj/ @@ -89,4 +90,10 @@ crashlytics-build.properties /NuGetImporterForUnity/[Nn]u[Gg]et/ /NuGetImporterForUnity/[Pp]ackages/*/ -!/NuGetImporterForUnity/[Pp]ackages/[Nn]u[Gg]et [Ii]mporter/ \ No newline at end of file +!/NuGetImporterForUnity/[Pp]ackages/[Nn]u[Gg]et [Ii]mporter/ + +# MacOS attribute file +.DS_Store + +# VS Code meta files +.vscode diff --git a/NuGetImporterForUnity/Packages/NuGet Importer/Editor/DataClasses/NativePlatform.cs b/NuGetImporterForUnity/Packages/NuGet Importer/Editor/DataClasses/NativePlatform.cs index 916abb4..26a7ae0 100644 --- a/NuGetImporterForUnity/Packages/NuGet Importer/Editor/DataClasses/NativePlatform.cs +++ b/NuGetImporterForUnity/Packages/NuGet Importer/Editor/DataClasses/NativePlatform.cs @@ -54,25 +54,41 @@ public class NativePlatform public NativePlatform(string directoryPath) { Path = directoryPath; - var directoryName = System.IO.Path.GetFileName(directoryPath).ToLowerInvariant(); - (OS, OSPriority) = GetOSInfo(directoryName); - Architecture = GetArchInfo(directoryName); + // https://learn.microsoft.com/en-us/dotnet/core/rid-catalog + var rid = GetRidFrom(directoryPath); + (OS, OSPriority) = GetOSInfo(rid); + Architecture = GetArchInfo(rid); } - private (string os, int priority) GetOSInfo(string directoryName) + private static string GetRidFrom(string path) + { // https://learn.microsoft.com/en-us/nuget/create-packages/supporting-multiple-target-frameworks#architecture-specific-folders + // assumes right path is like "Packages/project/runtimes/RID" or +"/native/..." + path = path.Replace('\\', '/'); // in case of '\'(windows) + var pattern = "/?runtimes/(.+?)/?"; + var matched = System.Text.RegularExpressions.Regex.Match( + path, + pattern, + System.Text.RegularExpressions.RegexOptions.RightToLeft | // to choose last + System.Text.RegularExpressions.RegexOptions.IgnoreCase); + if (!matched.Success) + return string.Empty; // failed to find + return matched.Groups[1].Value.Split('/')[0]; + } + + private (string os, int priority) GetOSInfo(string rid) { - var matchedPriority = PriorityTable.Where(table => directoryName.StartsWith(table.Item1.ToLowerInvariant())); + var matchedPriority = PriorityTable.Where(table => rid.StartsWith(table.Item1.ToLowerInvariant())); if (matchedPriority.Any()) { return matchedPriority.First(); } - return (directoryName.Split('-')[0], int.MaxValue); + return (rid.Split('-')[0], int.MaxValue); } - private string GetArchInfo(string directoryName) + private string GetArchInfo(string rid) { - var matchedArch = Enum.GetNames(typeof(ArchitectureType)).Where(table => directoryName.EndsWith(table.ToLowerInvariant())); + var matchedArch = Enum.GetNames(typeof(ArchitectureType)).Where(table => rid.EndsWith(table.ToLowerInvariant())); if (matchedArch.Any()) { return matchedArch.First(); diff --git a/NuGetImporterForUnity/Packages/NuGet Importer/Editor/NuGetNativeImportSetting.cs b/NuGetImporterForUnity/Packages/NuGet Importer/Editor/NuGetNativeImportSetting.cs index 386c3fa..4272960 100644 --- a/NuGetImporterForUnity/Packages/NuGet Importer/Editor/NuGetNativeImportSetting.cs +++ b/NuGetImporterForUnity/Packages/NuGet Importer/Editor/NuGetNativeImportSetting.cs @@ -133,7 +133,8 @@ string pluginPath platformPath = Path.GetDirectoryName(platformPath); } - var platform = new NativePlatform(platformPath); + var directoryPath = System.IO.Path.GetDirectoryName(pluginPath); + var platform = new NativePlatform(directoryPath); architecture = platform.Architecture switch { nameof(ArchitectureType.X64) => platform.OS == nameof(OSType.IOS) ? "X64" : "x86_64", From e133224a5712ea9418f2f00578887af611e5066a Mon Sep 17 00:00:00 2001 From: alkee Date: Tue, 2 Jan 2024 17:25:16 +0900 Subject: [PATCH 2/2] Clean up unnecessaries. --- .../NuGet Importer/Editor/DataClasses/NativePlatform.cs | 9 +++++---- .../NuGet Importer/Editor/NuGetNativeImportSetting.cs | 7 +------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/NuGetImporterForUnity/Packages/NuGet Importer/Editor/DataClasses/NativePlatform.cs b/NuGetImporterForUnity/Packages/NuGet Importer/Editor/DataClasses/NativePlatform.cs index 26a7ae0..fedd935 100644 --- a/NuGetImporterForUnity/Packages/NuGet Importer/Editor/DataClasses/NativePlatform.cs +++ b/NuGetImporterForUnity/Packages/NuGet Importer/Editor/DataClasses/NativePlatform.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; namespace kumaS.NuGetImporter.Editor.DataClasses { @@ -62,14 +63,14 @@ public NativePlatform(string directoryPath) private static string GetRidFrom(string path) { // https://learn.microsoft.com/en-us/nuget/create-packages/supporting-multiple-target-frameworks#architecture-specific-folders - // assumes right path is like "Packages/project/runtimes/RID" or +"/native/..." + // assumes right path is like "Packages/project/runtimes/RID/native/..." or somthing path = path.Replace('\\', '/'); // in case of '\'(windows) var pattern = "/?runtimes/(.+?)/?"; - var matched = System.Text.RegularExpressions.Regex.Match( + var matched = Regex.Match( path, pattern, - System.Text.RegularExpressions.RegexOptions.RightToLeft | // to choose last - System.Text.RegularExpressions.RegexOptions.IgnoreCase); + RegexOptions.RightToLeft | // to choose last + RegexOptions.IgnoreCase); if (!matched.Success) return string.Empty; // failed to find return matched.Groups[1].Value.Split('/')[0]; diff --git a/NuGetImporterForUnity/Packages/NuGet Importer/Editor/NuGetNativeImportSetting.cs b/NuGetImporterForUnity/Packages/NuGet Importer/Editor/NuGetNativeImportSetting.cs index 4272960..de6c16b 100644 --- a/NuGetImporterForUnity/Packages/NuGet Importer/Editor/NuGetNativeImportSetting.cs +++ b/NuGetImporterForUnity/Packages/NuGet Importer/Editor/NuGetNativeImportSetting.cs @@ -127,13 +127,8 @@ string pluginPath BuildTarget target = BuildTarget.NoTarget; var architecture = ""; var enableOnEditor = false; - var platformPath = pluginPath; - while (platformPath!.Contains("native")) - { - platformPath = Path.GetDirectoryName(platformPath); - } - var directoryPath = System.IO.Path.GetDirectoryName(pluginPath); + var directoryPath = Path.GetDirectoryName(pluginPath); var platform = new NativePlatform(directoryPath); architecture = platform.Architecture switch {