diff --git a/CHANGELOG.md b/CHANGELOG.md index eef4da02..c853527a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# Upcoming +* Android Resolver - Handle package paths that don't include a version hash, + which is no longer present with Unity 6. Fixes #697 +* Android Resolver - Handle packages referenced using local file paths. + Fixes #701 + # Version 1.2.182 - Aug 2, 2024 * General - Check for gradle version instead of Unity version when determining the template files to modify. diff --git a/source/AndroidResolver/src/GradleTemplateResolver.cs b/source/AndroidResolver/src/GradleTemplateResolver.cs index b371eabd..ff9f75e5 100644 --- a/source/AndroidResolver/src/GradleTemplateResolver.cs +++ b/source/AndroidResolver/src/GradleTemplateResolver.cs @@ -170,6 +170,22 @@ public static bool UnityChangeMavenRepoInSettingsTemplate { } } + /// + /// Modifies the given path such that the m2repository is placed into the + /// LocalMavenRepoDir/PrefixDirectory/m2repository/... + /// + /// The original path to modify. + /// A modified path if m2repository is found, the same path otherwise. + private static string ReplaceLocalFolderBasedOnM2repo(string path) { + string regexPattern = @"^(.*[/\\])([^/\\]+[/\\]m2repository.*)$"; + Match match = Regex.Match(path, regexPattern); + if (match.Success) { + path = Path.Combine(GooglePlayServices.SettingsDialog.LocalMavenRepoDir, + match.Groups[2].Value); + } + return path; + } + /// /// Copy srcaar files to aar files that are excluded from Unity's build process. /// @@ -198,6 +214,15 @@ private static bool CopySrcAars(ICollection dependencies) { var dir = FileUtils.ReplaceBaseAssetsOrPackagesFolder( Path.GetDirectoryName(aar), GooglePlayServices.SettingsDialog.LocalMavenRepoDir); + + if (!dir.StartsWith(GooglePlayServices.SettingsDialog.LocalMavenRepoDir)) { + // The directory replace logic failed, likely because the original aar + // is not located under the Assets or Packages folders. + // Try to come up with a sensible destination folder by searching for + // an m2repository within the path, and using that. + dir = ReplaceLocalFolderBasedOnM2repo(Path.GetDirectoryName(aar)); + } + var filename = Path.GetFileNameWithoutExtension(aarPath); var targetFilename = Path.Combine(dir, filename + ".aar"); @@ -700,15 +725,18 @@ internal static IList GradleMavenReposLinesFromDependencies( if (repoAndSources.Key.StartsWith(projectFileUri)) { var relativePath = repoAndSources.Key.Substring(projectFileUri.Length + 1); // Convert "Assets", "Packages/packageid", or - // "Library/PackageCache/packageid@version" prefix to local maven repo - // path. Note that local maven repo path only exists if the original repo - // path contains .srcaar. - var repoPath = FileUtils.PosixPathSeparators( - FileUtils.ReplaceBaseAssetsOrPackagesFolder( - relativePath, GooglePlayServices.SettingsDialog.LocalMavenRepoDir)); + // "Library/PackageCache/packageid@version" prefix (@version optional) to local + // maven repo path. Note that local maven repo path only exists if the original + // repo path contains .srcaar. + var repoPath = FileUtils.ReplaceBaseAssetsOrPackagesFolder( + relativePath, GooglePlayServices.SettingsDialog.LocalMavenRepoDir); + // We also want to just convert any prefixes before a directory/m2repository, since + // they are copied to the LocalMavenRepoDir as well. + repoPath = ReplaceLocalFolderBasedOnM2repo(repoPath); if (!Directory.Exists(repoPath)) { repoPath = relativePath; } + repoPath = FileUtils.PosixPathSeparators(repoPath); if (useFullPath) { // build.gradle expects file:/// URI so file separator will be "/" in anycase diff --git a/source/VersionHandlerImpl/src/FileUtils.cs b/source/VersionHandlerImpl/src/FileUtils.cs index 6f3b098f..75f6aae3 100644 --- a/source/VersionHandlerImpl/src/FileUtils.cs +++ b/source/VersionHandlerImpl/src/FileUtils.cs @@ -48,9 +48,10 @@ internal class FileUtils { /// /// Regex to match packages folder like "Library/PackageCache/com.company.pkg" + /// or "Library/PackageCache/com.company.pkg@version" /// private static Regex PACKAGES_PHYSICAL_PATH_REGEX = - new Regex(@"^(Library[/\\]PackageCache[/\\])([^/\\]+)(@[^/\\]+)[/\\](.*)?$"); + new Regex(@"^(Library[/\\]PackageCache[/\\])([^/\\]+)(@[^/\\]+)?[/\\](.*)?$"); /// /// Returns the project directory (e.g contains the Assets folder). @@ -448,7 +449,9 @@ public static string GetPackageDirectory( // work if the package is installed from a local tarball or from a registry // server. string absolutePath = Path.GetFullPath(packageDir); - packageDir = absolutePath.Substring(ProjectDirectory.Length + 1); + if (absolutePath.StartsWith(ProjectDirectory)) { + packageDir = absolutePath.Substring(ProjectDirectory.Length + 1); + } } } else { nameMatch = PACKAGES_PHYSICAL_PATH_REGEX.Match(path); @@ -640,42 +643,47 @@ internal static bool IsValidGuid(string guidStr) { /// Path to the file/directory that needs checking. /// True if all folders are created successfully. public static bool CreateFolder(string path, Google.Logger logger = null) { - if (AssetDatabase.IsValidFolder(path)) { - return true; - } - DirectoryInfo di = new DirectoryInfo(path); - var parentFolder = Path.GetDirectoryName(path); - if (!CreateFolder(parentFolder)) { - return false; - } + try { + if (AssetDatabase.IsValidFolder(path)) { + return true; + } + DirectoryInfo di = new DirectoryInfo(path); + var parentFolder = Path.GetDirectoryName(path); + if (!CreateFolder(parentFolder)) { + return false; + } - // Try to use Unity API to create folder. However, some versions of Unity has issue to - // create folders with version number in it like '9.0.0'. In this case, instead of - // returnig empty guid, it can return guids with all zeroes. - if (IsValidGuid(AssetDatabase.CreateFolder(parentFolder, di.Name))) { - return true; - } + // Try to use Unity API to create folder. However, some versions of Unity has issue to + // create folders with version number in it like '9.0.0'. In this case, instead of + // returning empty guid, it can return guids with all zeroes. + if (IsValidGuid(AssetDatabase.CreateFolder(parentFolder, di.Name))) { + return true; + } - if (logger != null) { - logger.Log( - String.Format( - "Please ignore Unity error messages similar to '{0}'.\n" + - "Unable to use Unity API `AssetDatabase.CreateFolder()` to " + - "create folder: '{1}'. Switch to use `Directory.CreateDirectory()` " + - "instead. \n\n" + - "See {2} for more information.", - "*** is not a valid directory name.", - path, - "https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-7046"), - LogLevel.Info); - } + if (logger != null) { + logger.Log( + String.Format( + "Please ignore Unity error messages similar to '{0}'.\n" + + "Unable to use Unity API `AssetDatabase.CreateFolder()` to " + + "create folder: '{1}'. Switch to use `Directory.CreateDirectory()` " + + "instead. \n\n" + + "See {2} for more information.", + "*** is not a valid directory name.", + path, + "https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-7046"), + LogLevel.Info); + } - return Directory.CreateDirectory(path) != null; + return Directory.CreateDirectory(path) != null; + } catch (Exception ex) { + logger.Log("Exception thrown trying to CreateFolder. " + ex, LogLevel.Error); + return false; + } } /// /// Replace "Assets/", "Packages/package-id", or "Library/PackageCache/package-id@version" - /// base in the path with the new base. + /// base (@version optional) in the path with the new base. /// /// Path to the file/directory to be modified. /// New base used to replace the given path.