From 78ba05b60c27beb72ecfcab32ae1e87150c34c1b Mon Sep 17 00:00:00 2001 From: "jie.chen" Date: Fri, 21 Jun 2024 15:54:39 +0800 Subject: [PATCH 1/3] fix duplicate content --- src/NuGetForUnity/Editor/NugetPackageInstaller.cs | 15 +++++++++++++-- src/NuGetForUnity/Editor/PackageContentManager.cs | 12 +++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/NuGetForUnity/Editor/NugetPackageInstaller.cs b/src/NuGetForUnity/Editor/NugetPackageInstaller.cs index 9fd20529..9f8b994c 100644 --- a/src/NuGetForUnity/Editor/NugetPackageInstaller.cs +++ b/src/NuGetForUnity/Editor/NugetPackageInstaller.cs @@ -166,6 +166,18 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets, // unzip the package using (var zip = ZipFile.OpenRead(cachedPackagePath)) { + // check if nuget package has contentFiles folder + const string contentFilesDirectoryName = "contentFiles/"; + var hasContentFilesFolder = false; + foreach (var entry in zip.Entries) + { + if (entry.FullName.EndsWith("/") && entry.FullName.Equals(contentFilesDirectoryName, StringComparison.OrdinalIgnoreCase)) + { + hasContentFilesFolder = true; + break; + } + } + var libs = new Dictionary>(); var csFiles = new Dictionary>(); var anyFiles = new Dictionary>(); @@ -179,7 +191,7 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets, continue; } - if (PackageContentManager.ShouldSkipUnpackingOnPath(entryFullName)) + if (PackageContentManager.ShouldSkipUnpackingOnPath(entryFullName, hasContentFilesFolder)) { continue; } @@ -206,7 +218,6 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets, // in case this is a source code package, we want to collect all its entries that have 'cs' or 'any' set as language // and their frameworks so we can get the best framework later - const string contentFilesDirectoryName = "contentFiles/"; if (entryFullName.StartsWith(contentFilesDirectoryName, StringComparison.Ordinal)) { // Folder structure for source code packages: diff --git a/src/NuGetForUnity/Editor/PackageContentManager.cs b/src/NuGetForUnity/Editor/PackageContentManager.cs index 56c460a2..61cdd4f6 100644 --- a/src/NuGetForUnity/Editor/PackageContentManager.cs +++ b/src/NuGetForUnity/Editor/PackageContentManager.cs @@ -109,8 +109,12 @@ internal static void CleanInstallationDirectory([NotNull] INugetPackageIdentifie /// The path of the file inside the .nupkg it is relative starting from the package route. It always uses '/' as a slash on all /// platforms. /// + /// + /// The package has contentfiles folder, we need to omit content folder it exist. Because some nuget package has both folder to + /// to support old version and new version of nuget. + /// /// True if the file can be skipped, is not needed. - internal static bool ShouldSkipUnpackingOnPath([NotNull] string path) + internal static bool ShouldSkipUnpackingOnPath([NotNull] string path, bool hasContentFilesFolder) { if (path.EndsWith("/")) { @@ -120,6 +124,12 @@ internal static bool ShouldSkipUnpackingOnPath([NotNull] string path) return true; } + // skip content folder when it has contentFiles folder + if (hasContentFilesFolder && (path.StartsWith("content/", StringComparison.Ordinal) || path.Contains("/content/"))) + { + return true; + } + // skip directories & files that NuGet normally deletes if (path.StartsWith("_rels/", StringComparison.Ordinal) || path.Contains("/_rels/")) { From 0cb7859ece6856803bfa1f9c0463c9341b876b25 Mon Sep 17 00:00:00 2001 From: "jie.chen" Date: Fri, 21 Jun 2024 16:13:16 +0800 Subject: [PATCH 2/3] fix check if it has contentfiles --- src/NuGetForUnity/Editor/NugetPackageInstaller.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGetForUnity/Editor/NugetPackageInstaller.cs b/src/NuGetForUnity/Editor/NugetPackageInstaller.cs index 9f8b994c..b3155105 100644 --- a/src/NuGetForUnity/Editor/NugetPackageInstaller.cs +++ b/src/NuGetForUnity/Editor/NugetPackageInstaller.cs @@ -171,7 +171,7 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets, var hasContentFilesFolder = false; foreach (var entry in zip.Entries) { - if (entry.FullName.EndsWith("/") && entry.FullName.Equals(contentFilesDirectoryName, StringComparison.OrdinalIgnoreCase)) + if (entry.FullName.StartsWith(contentFilesDirectoryName, StringComparison.Ordinal)) { hasContentFilesFolder = true; break; From 63d8c041c137fd9d6b6af622f6153c8899a6e820 Mon Sep 17 00:00:00 2001 From: JoC0de <53140583+JoC0de@users.noreply.github.com> Date: Sun, 21 Jul 2024 23:11:07 +0200 Subject: [PATCH 3/3] Use Linq instead of 'foreach' --- src/NuGetForUnity/Editor/NugetPackageInstaller.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/NuGetForUnity/Editor/NugetPackageInstaller.cs b/src/NuGetForUnity/Editor/NugetPackageInstaller.cs index b3155105..687d2641 100644 --- a/src/NuGetForUnity/Editor/NugetPackageInstaller.cs +++ b/src/NuGetForUnity/Editor/NugetPackageInstaller.cs @@ -168,15 +168,7 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets, { // check if nuget package has contentFiles folder const string contentFilesDirectoryName = "contentFiles/"; - var hasContentFilesFolder = false; - foreach (var entry in zip.Entries) - { - if (entry.FullName.StartsWith(contentFilesDirectoryName, StringComparison.Ordinal)) - { - hasContentFilesFolder = true; - break; - } - } + var hasContentFilesFolder = zip.Entries.Any(entry => entry.FullName.StartsWith(contentFilesDirectoryName, StringComparison.Ordinal)); var libs = new Dictionary>(); var csFiles = new Dictionary>();