Exemplo n.º 1
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;
                    }
                }
            }
        }
Exemplo n.º 2
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;
                }
            }
        }