示例#1
0
        public void ValidateAssets(bool alwaysGenerateNewAssetId = false)
        {
            if (TemporaryAssets.Count == 0)
            {
                return;
            }

            try
            {
                // Make sure we are suspending notifications before updating all assets
                Assets.SuspendCollectionChanged();

                Assets.Clear();

                // Get generated output items
                var outputItems = new AssetItemCollection();

                // Create a resolver from the package
                var resolver = AssetResolver.FromPackage(this);
                resolver.AlwaysCreateNewId = alwaysGenerateNewAssetId;

                // Clean assets
                AssetCollision.Clean(TemporaryAssets, outputItems, resolver, false);

                // Add them back to the package
                foreach (var item in outputItems)
                {
                    Assets.Add(item);
                }

                TemporaryAssets.Clear();
            }
            finally
            {
                // Restore notification on assets
                Assets.ResumeCollectionChanged();
            }
        }
示例#2
0
        /// <summary>
        /// Refreshes this package from the disk by loading or reloading all assets.
        /// </summary>
        /// <param name="log">The log.</param>
        /// <param name="cancelToken">The cancel token.</param>
        /// <returns>A logger that contains error messages while refreshing.</returns>
        /// <exception cref="System.InvalidOperationException">Package RootDirectory is null
        /// or
        /// Package RootDirectory [{0}] does not exist.ToFormat(RootDirectory)</exception>
        public void LoadTemporaryAssets(ILogger log, CancellationToken?cancelToken = null)
        {
            if (log == null)
            {
                throw new ArgumentNullException("log");
            }

            // If FullPath is null, then we can't load assets from disk, just return
            if (FullPath == null)
            {
                log.Warning("Fullpath not set on this package");
                return;
            }

            // Clears the assets already loaded and reload them
            TemporaryAssets.Clear();

            // List all package files on disk
            var listFiles = ListAssetFiles(log, this, cancelToken);

            var progressMessage = String.Format("Loading Assets from Package [{0}]", FullPath.GetFileNameWithExtension());

            // Display this message at least once if the logger does not log progress (And it shouldn't in this case)
            var loggerResult = log as LoggerResult;

            if (loggerResult == null || !loggerResult.IsLoggingProgressAsInfo)
            {
                log.Info(progressMessage);
            }

            // Update step counter for log progress
            for (int i = 0; i < listFiles.Count; i++)
            {
                var fileUPath    = listFiles[i].Item1;
                var sourceFolder = listFiles[i].Item2;
                if (cancelToken.HasValue && cancelToken.Value.IsCancellationRequested)
                {
                    log.Warning("Skipping loading assets. PackageSession.Load cancelled");
                    break;
                }

                // Update the loading progress
                if (loggerResult != null)
                {
                    loggerResult.Progress(progressMessage, i, listFiles.Count);
                }

                // Try to load only if asset is not already in the package or assetRef.Asset is null
                var assetPath = fileUPath.MakeRelative(sourceFolder).GetDirectoryAndFileName();
                try
                {
                    // An exception can occur here, so we make sure that loading a single asset is not going to break
                    // the loop
                    var assetFullPath = fileUPath.FullPath;
                    var asset         = LoadAsset(log, assetFullPath, assetPath, fileUPath);

                    // Create asset item
                    var assetItem = new AssetItem(assetPath, asset)
                    {
                        IsDirty      = false,
                        Package      = this,
                        SourceFolder = sourceFolder.MakeRelative(RootDirectory)
                    };
                    // Set the modified time to the time loaded from disk
                    assetItem.ModifiedTime = File.GetLastWriteTime(assetFullPath);

                    FixAssetImport(assetItem);

                    // Add to temporary assets
                    TemporaryAssets.Add(assetItem);
                }
                catch (Exception ex)
                {
                    int row           = 1;
                    int column        = 1;
                    var yamlException = ex as YamlException;
                    if (yamlException != null)
                    {
                        row    = yamlException.Start.Line + 1;
                        column = yamlException.Start.Column;
                    }

                    var module = log.Module;

                    var assetReference = new AssetReference <Asset>(Guid.Empty, fileUPath.FullPath);

                    // TODO: Change this instead of patching LoggerResult.Module, use a proper log message
                    if (loggerResult != null)
                    {
                        loggerResult.Module = "{0}({1},{2})".ToFormat(Path.GetFullPath(fileUPath.FullPath), row, column);
                    }

                    log.Error(this, assetReference, AssetMessageCode.AssetLoadingFailed, ex, fileUPath, ex.Message);

                    if (loggerResult != null)
                    {
                        loggerResult.Module = module;
                    }
                }
            }
        }
示例#3
0
        private void LoadAsset(ILogger log, PackageLoadingAssetFile assetFile, LoggerResult loggerResult)
        {
            var fileUPath    = assetFile.FilePath;
            var sourceFolder = assetFile.SourceFolder;

            // Check if asset has been deleted by an upgrader
            if (assetFile.Deleted)
            {
                IsDirty = true;
                filesToDelete.Add(assetFile.FilePath);
            }

            // An exception can occur here, so we make sure that loading a single asset is not going to break
            // the loop
            try
            {
                AssetMigration.MigrateAssetIfNeeded(log, assetFile);

                // Try to load only if asset is not already in the package or assetRef.Asset is null
                var assetPath = fileUPath.MakeRelative(sourceFolder).GetDirectoryAndFileName();

                var assetFullPath = fileUPath.FullPath;
                var assetContent  = assetFile.AssetContent;

                var asset = LoadAsset(log, assetFullPath, assetPath, fileUPath, assetContent);

                // Create asset item
                var assetItem = new AssetItem(assetPath, asset, this)
                {
                    IsDirty      = assetContent != null,
                    SourceFolder = sourceFolder.MakeRelative(RootDirectory)
                };
                // Set the modified time to the time loaded from disk
                if (!assetItem.IsDirty)
                {
                    assetItem.ModifiedTime = File.GetLastWriteTime(assetFullPath);
                }

                // TODO: Let's review that when we rework import process
                // Not fixing asset import anymore, as it was only meant for upgrade
                // However, it started to make asset dirty, for ex. when we create a new texture, choose a file and reload the scene later
                // since there was no importer id and base.
                //FixAssetImport(assetItem);

                // Add to temporary assets
                lock (TemporaryAssets)
                {
                    TemporaryAssets.Add(assetItem);
                }
            }
            catch (Exception ex)
            {
                int row           = 1;
                int column        = 1;
                var yamlException = ex as YamlException;
                if (yamlException != null)
                {
                    row    = yamlException.Start.Line + 1;
                    column = yamlException.Start.Column;
                }

                var module = log.Module;

                var assetReference = new AssetReference <Asset>(Guid.Empty, fileUPath.FullPath);

                // TODO: Change this instead of patching LoggerResult.Module, use a proper log message
                if (loggerResult != null)
                {
                    loggerResult.Module = "{0}({1},{2})".ToFormat(Path.GetFullPath(fileUPath.FullPath), row, column);
                }

                log.Error(this, assetReference, AssetMessageCode.AssetLoadingFailed, ex, fileUPath, ex.Message);

                if (loggerResult != null)
                {
                    loggerResult.Module = module;
                }
            }
        }
示例#4
0
        /// <summary>
        /// Refreshes this package from the disk by loading or reloading all assets.
        /// </summary>
        /// <param name="log">The log.</param>
        /// <param name="assetFiles">The asset files (loaded from <see cref="ListAssetFiles"/> if null).</param>
        /// <param name="cancelToken">The cancel token.</param>
        /// <returns>A logger that contains error messages while refreshing.</returns>
        /// <exception cref="System.InvalidOperationException">Package RootDirectory is null
        /// or
        /// Package RootDirectory [{0}] does not exist.ToFormat(RootDirectory)</exception>
        public void LoadTemporaryAssets(ILogger log, IList <PackageLoadingAssetFile> assetFiles = null, CancellationToken?cancelToken = null)
        {
            if (log == null)
            {
                throw new ArgumentNullException("log");
            }

            // If FullPath is null, then we can't load assets from disk, just return
            if (FullPath == null)
            {
                log.Warning("Fullpath not set on this package");
                return;
            }

            // Clears the assets already loaded and reload them
            TemporaryAssets.Clear();

            // List all package files on disk
            if (assetFiles == null)
            {
                assetFiles = ListAssetFiles(log, this, cancelToken);
            }

            var progressMessage = String.Format("Loading Assets from Package [{0}]", FullPath.GetFileNameWithExtension());

            // Display this message at least once if the logger does not log progress (And it shouldn't in this case)
            var loggerResult = log as LoggerResult;

            if (loggerResult == null || !loggerResult.IsLoggingProgressAsInfo)
            {
                log.Info(progressMessage);
            }

            // Update step counter for log progress
            var tasks = new List <System.Threading.Tasks.Task>();

            for (int i = 0; i < assetFiles.Count; i++)
            {
                var assetFile = assetFiles[i];
                // Update the loading progress
                if (loggerResult != null)
                {
                    loggerResult.Progress(progressMessage, i, assetFiles.Count);
                }

                var task = cancelToken.HasValue ?
                           System.Threading.Tasks.Task.Factory.StartNew(() => LoadAsset(log, assetFile, loggerResult), cancelToken.Value) :
                           System.Threading.Tasks.Task.Factory.StartNew(() => LoadAsset(log, assetFile, loggerResult));

                tasks.Add(task);
            }

            if (cancelToken.HasValue)
            {
                System.Threading.Tasks.Task.WaitAll(tasks.ToArray(), cancelToken.Value);
            }
            else
            {
                System.Threading.Tasks.Task.WaitAll(tasks.ToArray());
            }

            // DEBUG
            // StaticLog.Info("[{0}] Assets files loaded in {1}", assetFiles.Count, clock.ElapsedMilliseconds);

            if (cancelToken.HasValue && cancelToken.Value.IsCancellationRequested)
            {
                log.Warning("Skipping loading assets. PackageSession.Load cancelled");
            }
        }