예제 #1
0
        internal static IEnumerable <Sample> FindByPackage(PackageInfo package, UpmCache upmCache, IOProxy ioProxy, AssetDatabaseProxy assetDatabaseProxy)
        {
            if (string.IsNullOrEmpty(package?.upmReserved) && string.IsNullOrEmpty(package.resolvedPath))
            {
                return(Enumerable.Empty <Sample>());
            }

            try
            {
                IEnumerable <IDictionary <string, object> > samples = null;
                var upmReserved = upmCache.ParseUpmReserved(package);
                if (upmReserved != null)
                {
                    samples = upmReserved.GetList <IDictionary <string, object> >("samples");
                }

                if (samples == null)
                {
                    var jsonPath = ioProxy.PathsCombine(package.resolvedPath, "package.json");
                    if (ioProxy.FileExists(jsonPath))
                    {
                        var packageJson = Json.Deserialize(ioProxy.FileReadAllText(jsonPath)) as Dictionary <string, object>;
                        samples = packageJson.GetList <IDictionary <string, object> >("samples");
                    }
                }

                return(samples?.Select(sample =>
                {
                    var displayName = sample.GetString("displayName");
                    var path = sample.GetString("path");
                    var description = sample.GetString("description");
                    var interactiveImport = sample.Get("interactiveImport", false);

                    var resolvedSamplePath = ioProxy.PathsCombine(package.resolvedPath, path);
                    var importPath = ioProxy.PathsCombine(
                        Application.dataPath,
                        "Samples",
                        IOUtils.SanitizeFileName(package.displayName),
                        package.version,
                        string.IsNullOrEmpty(displayName) ? string.Empty : IOUtils.SanitizeFileName(displayName)
                        );
                    return new Sample(ioProxy, assetDatabaseProxy, displayName, description, resolvedSamplePath, importPath, interactiveImport);
                }).ToArray() ?? Enumerable.Empty <Sample>());
            }
            catch (IOException e)
            {
                Debug.Log($"[Package Manager Window] Cannot find samples for package {package.displayName}: {e}");
                return(Enumerable.Empty <Sample>());
            }
            catch (InvalidCastException e)
            {
                Debug.Log($"[Package Manager Window] Invalid sample data for package {package.displayName}: {e}");
                return(Enumerable.Empty <Sample>());
            }
            catch (Exception)
            {
                return(Enumerable.Empty <Sample>());
            }
        }
예제 #2
0
        private static List <Sample> GetSamplesFromPackageInfo(IOProxy ioProxy, PackageInfo packageInfo)
        {
            if (string.IsNullOrEmpty(packageInfo?.resolvedPath))
            {
                return(null);
            }

            var jsonPath = Path.Combine(packageInfo.resolvedPath, "package.json");

            if (!ioProxy.FileExists(jsonPath))
            {
                return(null);
            }

            try
            {
                var packageJson = Json.Deserialize(ioProxy.FileReadAllText(jsonPath)) as Dictionary <string, object>;
                var samples     = packageJson.GetList <IDictionary <string, object> >("samples");
                return(samples?.Select(sample =>
                {
                    var displayName = sample.GetString("displayName");
                    var path = sample.GetString("path");
                    var description = sample.GetString("description");
                    var interactiveImport = sample.Get("interactiveImport", false);

                    var resolvedSamplePath = Path.Combine(packageInfo.resolvedPath, path);
                    var importPath = IOUtils.CombinePaths(
                        Application.dataPath,
                        "Samples",
                        IOUtils.SanitizeFileName(packageInfo.displayName),
                        packageInfo.version,
                        IOUtils.SanitizeFileName(displayName)
                        );
                    return new Sample(ioProxy, displayName, description, resolvedSamplePath, importPath, interactiveImport);
                }).ToList());
            }
            catch (Exception)
            {
                return(null);
            }
        }