public void TestMoveAssetWithUFile() { var projectDir = new UFile(Path.Combine(Environment.CurrentDirectory, "testxk")); var rawAssetPath = new UFile("../image.png"); var assetPath = new UFile("sub1/sub2/test"); // Create a project with an asset reference a raw file var project = new Package { FullPath = projectDir }; project.AssetFolders.Clear(); project.AssetFolders.Add(new AssetFolder(".")); var asset = new AssetObjectTest() { RawAsset = new UFile(rawAssetPath) }; var assetItem = new AssetItem(assetPath, asset); project.Assets.Add(assetItem); // Run an asset reference analysis on this project var analysis = new PackageAnalysis(project, new PackageAnalysisParameters() { ConvertUPathTo = UPathType.Absolute, IsProcessingUPaths = true }); var result = analysis.Run(); Assert.False(result.HasErrors); Assert.Equal(UPath.Combine(project.RootDirectory, new UFile("sub1/image.png")), asset.RawAsset); project.Assets.Remove(assetItem); assetItem = new AssetItem("sub1/test", asset); project.Assets.Add(assetItem); result = analysis.Run(); Assert.False(result.HasErrors); Assert.Equal(UPath.Combine(project.RootDirectory, new UFile("sub1/image.png")), asset.RawAsset); project.Assets.Remove(assetItem); assetItem = new AssetItem("test", asset); project.Assets.Add(assetItem); result = analysis.Run(); Assert.False(result.HasErrors); Assert.Equal(UPath.Combine(project.RootDirectory, new UFile("sub1/image.png")), asset.RawAsset); analysis.Parameters.ConvertUPathTo = UPathType.Relative; result = analysis.Run(); Assert.False(result.HasErrors); Assert.Equal(new UFile("sub1/image.png"), asset.RawAsset); }
/// <summary> /// Adds an existing package to the current session. /// </summary> /// <param name="packagePath">The package path.</param> /// <param name="logger">The session result.</param> /// <param name="loadParametersArg">The load parameters argument.</param> /// <exception cref="System.ArgumentNullException">packagePath</exception> /// <exception cref="System.ArgumentException">Invalid relative path. Expecting an absolute package path;packagePath</exception> /// <exception cref="System.IO.FileNotFoundException">Unable to find package</exception> public Package AddExistingPackage(UFile packagePath, ILogger logger, PackageLoadParameters loadParametersArg = null) { if (packagePath == null) { throw new ArgumentNullException("packagePath"); } if (logger == null) { throw new ArgumentNullException("logger"); } if (!packagePath.IsAbsolute) { throw new ArgumentException("Invalid relative path. Expecting an absolute package path", "packagePath"); } if (!File.Exists(packagePath)) { throw new FileNotFoundException("Unable to find package", packagePath); } var loadParameters = loadParametersArg ?? PackageLoadParameters.Default(); Package package; try { // Enable reference analysis caching during loading AssetReferenceAnalysis.EnableCaching = true; var packagesLoaded = new PackageCollection(); package = PreLoadPackage(this, logger, packagePath, false, packagesLoaded, loadParameters); // Load all missing references/dependencies LoadMissingReferences(logger, loadParameters); // Load assets TryLoadAssets(this, logger, package, loadParameters); // Run analysis after foreach (var packageToAdd in packagesLoaded) { var analysis = new PackageAnalysis(packageToAdd, GetPackageAnalysisParametersForLoad()); analysis.Run(logger); } } finally { // Disable reference analysis caching after loading AssetReferenceAnalysis.EnableCaching = false; } return(package); }
/// <summary> /// Second part of the package loading process, when references, assets and package analysis is done. /// </summary> /// <param name="package">The package.</param> /// <param name="log">The log.</param> /// <param name="loadParametersArg">The load parameters argument.</param> /// <returns></returns> internal bool LoadAssembliesAndAssets(ILogger log, PackageLoadParameters loadParametersArg) { var loadParameters = loadParametersArg ?? PackageLoadParameters.Default(); try { // Load assembly references if (loadParameters.LoadAssemblyReferences) { LoadAssemblyReferencesForPackage(log, loadParameters); } // Load assets if (loadParameters.AutoLoadTemporaryAssets) { LoadTemporaryAssets(log, loadParameters.AssetFiles, loadParameters.CancelToken); } // Convert UPath to absolute if (loadParameters.ConvertUPathToAbsolute) { var analysis = new PackageAnalysis(this, new PackageAnalysisParameters() { ConvertUPathTo = UPathType.Absolute, IsProcessingUPaths = true, // This is done already by Package.Load SetDirtyFlagOnAssetWhenFixingAbsoluteUFile = true // When loading tag attributes that have an absolute file }); analysis.Run(log); } // Load templates LoadTemplates(log); return(true); } catch (Exception ex) { log.Error("Error while pre-loading package [{0}]", ex, FullPath); return(false); } }
public void TestUpdateAssetUrl() { var projectDir = new UFile(Path.Combine(Environment.CurrentDirectory, "testxk")); // Create a project with an asset reference a raw file var project = new Package { FullPath = projectDir }; var assetItem = new AssetItem("test", new AssetObjectTest() { Reference = new AssetReference(AssetId.Empty, "good/location") }); project.Assets.Add(assetItem); var goodAsset = new AssetObjectTest(); project.Assets.Add(new AssetItem("good/location", goodAsset)); // Add the project to the session to make sure analysis will run correctly var session = new PackageSession(project); // Create a session with this project var analysis = new PackageAnalysis(project, new PackageAnalysisParameters() { IsProcessingAssetReferences = true, ConvertUPathTo = UPathType.Absolute, IsProcessingUPaths = true }); var result = analysis.Run(); Assert.False(result.HasErrors); Assert.Single(result.Messages); Assert.Contains("changed", result.Messages[0].ToString()); var asset = (AssetObjectTest)assetItem.Asset; Assert.Equal(goodAsset.Id, asset.Reference.Id); Assert.Equal("good/location", asset.Reference.Location); }
/// <summary> /// Loads only the package description but not assets or plugins. /// </summary> /// <param name="log">The log to receive error messages.</param> /// <param name="filePath">The file path.</param> /// <param name="loadParametersArg">The load parameters argument.</param> /// <returns>A package.</returns> /// <exception cref="System.ArgumentNullException">log /// or /// filePath</exception> public static Package Load(ILogger log, string filePath, PackageLoadParameters loadParametersArg = null) { if (log == null) { throw new ArgumentNullException("log"); } if (filePath == null) { throw new ArgumentNullException("filePath"); } filePath = FileUtility.GetAbsolutePath(filePath); if (!File.Exists(filePath)) { log.Error("Package file [{0}] was not found", filePath); return(null); } var loadParameters = loadParametersArg ?? PackageLoadParameters.Default(); try { var package = AssetSerializer.Load <Package>(filePath); package.FullPath = filePath; package.IsDirty = false; // Load assembly references if (loadParameters.LoadAssemblyReferences) { package.LoadAssemblyReferencesForPackage(log, loadParameters); } // Load assets if (loadParameters.AutoLoadTemporaryAssets) { package.LoadTemporaryAssets(log, loadParameters.CancelToken); } // Convert UPath to absolute if (loadParameters.ConvertUPathToAbsolute) { var analysis = new PackageAnalysis(package, new PackageAnalysisParameters() { ConvertUPathTo = UPathType.Absolute, IsProcessingUPaths = true, // This is done already by Package.Load SetDirtyFlagOnAssetWhenFixingAbsoluteUFile = true // When loading tag attributes that have an absolute file }); analysis.Run(log); } // Load templates package.LoadTemplates(log); return(package); } catch (Exception ex) { log.Error("Error while pre-loading package [{0}]", ex, filePath); } return(null); }
/// <summary> /// Saves this package and all dirty assets. See remarks. /// </summary> /// <param name="log">The log.</param> /// <exception cref="System.ArgumentNullException">log</exception> /// <remarks>When calling this method directly, it does not handle moving assets between packages. /// Call <see cref="PackageSession.Save" /> instead.</remarks> public void Save(ILogger log) { if (log == null) { throw new ArgumentNullException("log"); } if (FullPath == null) { log.Error(this, null, AssetMessageCode.PackageCannotSave, "null"); return; } // Use relative paths when saving var analysis = new PackageAnalysis(this, new PackageAnalysisParameters() { SetDirtyFlagOnAssetWhenFixingUFile = false, ConvertUPathTo = UPathType.Relative, IsProcessingUPaths = true }); analysis.Run(log); try { // Update source folders UpdateSourceFolders(); if (IsDirty) { try { // Notifies the dependency manager that a package with the specified path is being saved if (session != null && session.HasDependencyManager) { session.DependencyManager.AddFileBeingSaveDuringSessionSave(FullPath); } AssetSerializer.Save(FullPath, this); IsDirty = false; } catch (Exception ex) { log.Error(this, null, AssetMessageCode.PackageCannotSave, ex, FullPath); return; } } foreach (var asset in Assets) { if (asset.IsDirty) { var assetPath = asset.FullPath; try { // Notifies the dependency manager that an asset with the specified path is being saved if (session != null && session.HasDependencyManager) { session.DependencyManager.AddFileBeingSaveDuringSessionSave(assetPath); } // Incject a copy of the base into the current asset when saving var assetBase = asset.Asset.Base; if (assetBase != null && !assetBase.IsRootImport) { var assetBaseItem = session != null?session.FindAsset(assetBase.Id) : Assets.Find(assetBase.Id); if (assetBaseItem != null) { var newBase = (Asset)AssetCloner.Clone(assetBaseItem.Asset); newBase.Base = null; asset.Asset.Base = new AssetBase(asset.Asset.Base.Location, newBase); } } AssetSerializer.Save(assetPath, asset.Asset); asset.IsDirty = false; } catch (Exception ex) { log.Error(this, asset.ToReference(), AssetMessageCode.AssetCannotSave, ex, assetPath); } } } Assets.IsDirty = false; // Save properties like the Paradox version used PackageSessionHelper.SaveProperties(this); } finally { // Rollback all relative UFile to absolute paths analysis.Parameters.ConvertUPathTo = UPathType.Absolute; analysis.Run(); } }