Ejemplo n.º 1
0
        public static bool IsSupportingFile(this IAssetImporter importer, UFile file)
        {
            if (file == null) throw new ArgumentNullException("file");
            if (file.GetFileExtension() == null) return false;

            return FileUtility.GetFileExtensionsAsSet(importer.SupportedFileExtensions).Contains(file.GetFileExtension());
        }
Ejemplo n.º 2
0
        public virtual bool IsSupportingFile(string filePath)
        {
            if (filePath == null) throw new ArgumentNullException("filePath");
            var file = new UFile(filePath);
            if (file.GetFileExtension() == null) return false;

            return FileUtility.GetFileExtensionsAsSet(SupportedFileExtensions).Contains(file.GetFileExtension());
        }
        public object Load(Stream stream, UFile filePath, ILogger log, out bool aliasOccurred, out Dictionary<YamlAssetPath, OverrideType> overrides)
        {
            aliasOccurred = false;

            var assetFileExtension = filePath.GetFileExtension().ToLowerInvariant();
            var type = AssetRegistry.GetAssetTypeFromFileExtension(assetFileExtension);
            var asset = (SourceCodeAsset)Activator.CreateInstance(type);

            var textAccessor = asset.TextAccessor as SourceCodeAsset.DefaultTextAccessor;
            if (textAccessor != null)
            {
                // Don't load the file if we have the file path
                textAccessor.FilePath = filePath;

                // Set the assets text if it loaded from an in-memory version
                // TODO: Propagate dirtiness?
                if (stream is MemoryStream)
                {
                    var reader = new StreamReader(stream, Encoding.UTF8);
                    textAccessor.Set(reader.ReadToEnd());
                }
            }

            // No override in source code assets
            overrides = new Dictionary<YamlAssetPath, OverrideType>();

            return asset;
        }
Ejemplo n.º 4
0
        private static List<Tuple<UFile, UDirectory>> ListAssetFiles(ILogger log, Package package, CancellationToken? cancelToken)
        {
            var listFiles = new List<Tuple<UFile, UDirectory>>();

            // TODO Check how to handle refresh correctly as a public API
            if (package.RootDirectory == null)
            {
                throw new InvalidOperationException("Package RootDirectory is null");
            }

            if (!Directory.Exists(package.RootDirectory))
            {
                throw new InvalidOperationException("Package RootDirectory [{0}] does not exist".ToFormat(package.RootDirectory));
            }

            // Iterate on each source folders
            foreach (var sourceFolder in package.GetDistinctAssetFolderPaths())
            {
                // Lookup all files
                foreach (var directory in FileUtility.EnumerateDirectories(sourceFolder, SearchDirection.Down))
                {
                    var files = directory.GetFiles();

                    foreach (var filePath in files)
                    {
                        // Don't load package via this method
                        if (filePath.FullName.EndsWith(PackageFileExtension))
                        {
                            continue;
                        }

                        // Make an absolute path from the root of this package
                        var fileUPath = new UFile(filePath.FullName);
                        if (fileUPath.GetFileExtension() == null)
                        {
                            continue;
                        }

                        // If this kind of file an asset file?
                        if (!AssetRegistry.IsAssetFileExtension(fileUPath.GetFileExtension()))
                        {
                            continue;
                        }

                        listFiles.Add(new Tuple<UFile, UDirectory>(fileUPath, sourceFolder));
                    }
                }
            }

            return listFiles;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// This method is running in a separate thread and process file events received from <see cref="Core.IO.DirectoryWatcher"/>
        /// in order to generate the appropriate list of <see cref="AssetFileChangedEvent"/>.
        /// </summary>
        private void RunChangeWatcher()
        {
            // Only check every minute
            while (true)
            {
                if (threadWatcherEvent.WaitOne(TrackingSleepTime))
                    break;

                // Use a working copy in order to limit the locking
                fileEventsWorkingCopy.Clear();
                lock (fileEvents)
                {
                    fileEventsWorkingCopy.AddRange(fileEvents);
                    fileEvents.Clear();
                }

                if (fileEventsWorkingCopy.Count == 0 || isTrackingPaused)
                    continue;

                var assetEvents = new List<AssetFileChangedEvent>();

                // If this an asset belonging to a package
                lock (ThisLock)
                {
                    var packages = Packages;

                    // File event
                    foreach (var fileEvent in fileEventsWorkingCopy)
                    {
                        var file = new UFile(fileEvent.FullPath);

                        // When the session is being saved, we should not process events are they are false-positive alerts
                        // So we just skip the file
                        if (assetsBeingSaved.Contains(file.FullPath))
                        {
                            continue;
                        }

                        // 1) Check if this is related to imported assets
                        // Asset imports are handled slightly differently as we need to compute the
                        // hash of the source file
                        if (mapInputDependencyToAssets.ContainsKey(file.FullPath))
                        {
                            // Prepare the hash of the import file in advance for later re-import
                            FileVersionManager.Instance.ComputeFileHashAsync(file.FullPath, SourceImportFileHashCallback, tokenSourceForImportHash.Token);
                            continue;
                        }

                        // 2) else check that the file is a supported extension
                        if (!AssetRegistry.IsAssetFileExtension(file.GetFileExtension()))
                        {
                            continue;
                        }

                        // Find the parent package of the file that has been updated
                        UDirectory parentPackagePath = null;
                        Package parentPackage = null;
                        foreach (var package in packages)
                        {
                            var rootDirectory = package.RootDirectory;
                            if (rootDirectory == null)
                                continue;

                            if (rootDirectory.Contains(file) && (parentPackagePath == null || parentPackagePath.FullPath.Length < rootDirectory.FullPath.Length))
                            {
                                parentPackagePath = rootDirectory;
                                parentPackage = package;
                            }
                        }

                        // If we found a parent package, create an associated asset event
                        if (parentPackage != null)
                        {
                            var relativeLocation = file.MakeRelative(parentPackagePath);

                            var item = parentPackage.Assets.Find(relativeLocation);
                            AssetFileChangedEvent evt = null;
                            switch (fileEvent.ChangeType)
                            {
                                case FileEventChangeType.Created:
                                    evt = new AssetFileChangedEvent(parentPackage, AssetFileChangedType.Added, relativeLocation);
                                    break;
                                case FileEventChangeType.Deleted:
                                    evt = new AssetFileChangedEvent(parentPackage, AssetFileChangedType.Deleted, relativeLocation);
                                    break;
                                case FileEventChangeType.Changed:
                                    evt = new AssetFileChangedEvent(parentPackage, AssetFileChangedType.Updated, relativeLocation);
                                    break;
                            }
                            if (evt != null)
                            {
                                if (item != null)
                                {
                                    evt.AssetId = item.Id;
                                }
                                assetEvents.Add(evt);
                            }
                        }
                    }


                    // After all events have been processed, we 
                    // remove the file assetBeingSaved
                    foreach (var fileEvent in fileEventsWorkingCopy)
                    {
                        var file = new UFile(fileEvent.FullPath);

                        // When the session is being saved, we should not process events are they are false-positive alerts
                        // So we just skip the file
                        if (assetsBeingSaved.Remove(file))
                        {
                            if (assetsBeingSaved.Count == 0)
                            {
                                break;
                            }
                        }
                    }


                    // If we have any new events, copy them back
                    if (assetEvents.Count > 0 && !isTrackingPaused)
                    {
                        currentAssetFileChangedEvents.AddRange(assetEvents);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public static List<PackageLoadingAssetFile> ListAssetFiles(ILogger log, Package package, CancellationToken? cancelToken)
        {
            var listFiles = new List<PackageLoadingAssetFile>();

            // TODO Check how to handle refresh correctly as a public API
            if (package.RootDirectory == null)
            {
                throw new InvalidOperationException("Package RootDirectory is null");
            }

            if (!Directory.Exists(package.RootDirectory))
            {
                return listFiles;
            }

            var sharedProfile = package.Profiles.FindSharedProfile();
            var hasProject = sharedProfile != null && sharedProfile.ProjectReferences.Count > 0;

            // Iterate on each source folders
            foreach (var sourceFolder in package.GetDistinctAssetFolderPaths())
            {
                // Lookup all files
                foreach (var directory in FileUtility.EnumerateDirectories(sourceFolder, SearchDirection.Down))
                {
                    var files = directory.GetFiles();

                    foreach (var filePath in files)
                    {
                        // Don't load package via this method
                        if (filePath.FullName.EndsWith(PackageFileExtension))
                        {
                            continue;
                        }

                        // Make an absolute path from the root of this package
                        var fileUPath = new UFile(filePath.FullName);
                        if (fileUPath.GetFileExtension() == null)
                        {
                            continue;
                        }

                        // If this kind of file an asset file?
                        var ext = fileUPath.GetFileExtension();

                        //make sure to add default shaders in this case, since we don't have a csproj for them
                        if (AssetRegistry.IsProjectCodeGeneratorAssetFileExtension(ext) && !hasProject)
                        {
                            listFiles.Add(new PackageLoadingAssetFile(fileUPath, sourceFolder));
                            continue;
                        }

                        if (!AssetRegistry.IsAssetFileExtension(ext) || AssetRegistry.IsProjectSourceCodeAssetFileExtension(ext)) //project source code assets follow the csproj pipeline
                        {
                            continue;
                        }

                        listFiles.Add(new PackageLoadingAssetFile(fileUPath, sourceFolder));
                    }
                }
            }

            //find also assets in the csproj
            FindCodeAssetsInProject(listFiles, package);

            return listFiles;
        }
Ejemplo n.º 7
0
        public void TestWithNormalization()
        {
            var assetPath = new UFile("/a/b/.././././//c.txt");
            Assert.AreEqual("/a", assetPath.GetDirectory());
            Assert.AreEqual("c", assetPath.GetFileName());
            Assert.AreEqual(".txt", assetPath.GetFileExtension());
            Assert.AreEqual("/a/c", assetPath.GetDirectoryAndFileName());
            Assert.AreEqual("/a/c.txt", assetPath.FullPath);

            assetPath = new UFile("../.././././//c.txt");
            Assert.AreEqual("../..", assetPath.GetDirectory());
            Assert.AreEqual("c", assetPath.GetFileName());
            Assert.AreEqual(".txt", assetPath.GetFileExtension());
            Assert.AreEqual("../../c", assetPath.GetDirectoryAndFileName());
            Assert.AreEqual("../../c.txt", assetPath.FullPath);

            assetPath = new UFile("a/../../../c.txt");
            Assert.AreEqual("../../c.txt", assetPath.FullPath);
        }
Ejemplo n.º 8
0
 public void TestWithSimplePathWithExtension()
 {
     var assetPath = new UFile("/a/b/c.txt");
     Assert.AreEqual("/a/b", assetPath.GetDirectory());
     Assert.AreEqual("c", assetPath.GetFileName());
     Assert.AreEqual(".txt", assetPath.GetFileExtension());
     Assert.AreEqual("/a/b/c", assetPath.GetDirectoryAndFileName());
     Assert.AreEqual("/a/b/c.txt", assetPath.FullPath);
 }