Skip to content

Commit

Permalink
[Assets] Removed Package.Id
Browse files Browse the repository at this point in the history
  • Loading branch information
xen2 committed Sep 19, 2018
1 parent 0b5ba02 commit e59316a
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 309 deletions.
23 changes: 9 additions & 14 deletions sources/assets/Xenko.Core.Assets.Tests/TestPackage.cs
Expand Up @@ -26,7 +26,7 @@ public void TestBasicPackageCreateSaveLoad()

// Force the PackageId to be the same each time we run the test
// Usually the PackageId is unique and generated each time we create a new project
var project = new Package { Id = Guid.Empty, FullPath = testGenerated1 };
var project = new Package { FullPath = testGenerated1 };
project.Profile.AssetFolders.Clear();
project.Profile.AssetFolders.Add(new AssetFolder("."));

Expand Down Expand Up @@ -56,7 +56,6 @@ public void TestBasicPackageCreateSaveLoad()
AssertResult(project2Result);
var project2 = project2Result.Session.LocalPackages.FirstOrDefault();
Assert.NotNull(project2);
Assert.Equal(project.Id, project2.Id);
Assert.True(project2.Profile.AssetFolders.Count > 0);
Assert.Equal(project2.Container, project2Result.Session.CurrentProject); // Check that the current package is setup when loading a single package
var sourceFolder = project.Profile.AssetFolders.First().Path;
Expand All @@ -69,7 +68,6 @@ public void TestBasicPackageCreateSaveLoad()
var sessionReload = sessionResult.Session;
Assert.Single(sessionReload.LocalPackages);
var reloadPackage = sessionReload.LocalPackages.First();
Assert.Equal(project.Id, reloadPackage.Id);
}

[Fact]
Expand Down Expand Up @@ -99,9 +97,7 @@ public void TestPackageLoadingWithAssets()
AssertResult(sessionResult);
var session = sessionResult.Session;

var rootPackageId = new Guid("4102BF96-796D-4800-9983-9C227FAB7BBD");

var project = session.Packages.Find(rootPackageId);
var project = session.Packages.Single(x => x.FullPath.GetFileNameWithoutExtension() == "TestPackageLoadingWithAssets");
Assert.NotNull(project);
Assert.True(3 == project.Assets.Count, "Invalid number of assets loaded");

Expand All @@ -119,14 +115,14 @@ public void TestPackageLoadingWithAssets()

// Save project back to disk on a different location
project.FullPath = Path.Combine(DirectoryTestBase, @"TestPackage2\TestPackage2.xkpkg");
var subPackage = session.Packages.Find(Guid.Parse("281321F0-7664-4523-B1DC-3CFC26F80F77"));
var subPackage = session.Packages.Single(x => x.FullPath.GetFileNameWithoutExtension() == "SubPackage");
subPackage.FullPath = Path.Combine(DirectoryTestBase, @"TestPackage2\SubPackage\SubPackage.xkpkg");
var result = new LoggerResult();
session.Save(result);

var project2Result = PackageSession.Load(DirectoryTestBase + @"TestPackage2\TestPackage2.xkpkg");
AssertResult(project2Result);
var project2 = project2Result.Session.Packages.Find(rootPackageId);
var project2 = project2Result.Session.Packages.Single(x => x.FullPath.GetFileNameWithoutExtension() == "TestPackage2");
Assert.NotNull(project2);
Assert.Equal(3, project2.Assets.Count);
}
Expand All @@ -137,15 +133,14 @@ public void TestMovingAssets()
var basePath = Path.Combine(DirectoryTestBase, @"TestPackage");
var projectPath = Path.Combine(basePath, "TestPackageLoadingWithAssets.xkpkg");

var rootPackageId = new Guid("4102BF96-796D-4800-9983-9C227FAB7BBD");
var testAssetId = new AssetId("C2D80EF9-2160-43B2-9FEE-A19A903A0BE0");

// Load the project from the original location
var sessionResult1 = PackageSession.Load(projectPath);
{
AssertResult(sessionResult1);
var session = sessionResult1.Session;
var project = session.Packages.Find(rootPackageId);
var project = session.Packages.Single(x => x.FullPath.GetFileNameWithoutExtension() == "TestPackageLoadingWithAssets");
Assert.NotNull(project);

Assert.True(3 == project.Assets.Count, "Invalid number of assets loaded");
Expand All @@ -159,7 +154,7 @@ public void TestMovingAssets()

// First save a copy of the project to TestPackageMovingAssets1
project.FullPath = Path.Combine(DirectoryTestBase, @"TestPackageMovingAssets1\TestPackage2.xkpkg");
var subPackage = session.Packages.Find(Guid.Parse("281321F0-7664-4523-B1DC-3CFC26F80F77"));
var subPackage = session.Packages.Single(x => x.FullPath.GetFileNameWithoutExtension() == "SubPackage");
subPackage.FullPath = Path.Combine(DirectoryTestBase, @"TestPackageMovingAssets1\SubPackage\SubPackage.xkpkg");
var result = new LoggerResult();
session.Save(result);
Expand All @@ -170,7 +165,7 @@ public void TestMovingAssets()
{
AssertResult(sessionResult2);
var session = sessionResult2.Session;
var project = session.Packages.Find(rootPackageId);
var project = session.Packages.Single(x => x.FullPath.GetFileNameWithoutExtension() == "TestPackage2");
Assert.NotNull(project);
Assert.True(3 == project.Assets.Count, "Invalid number of assets loaded");

Expand All @@ -184,7 +179,7 @@ public void TestMovingAssets()

// Save the whole project to a different location
project.FullPath = Path.Combine(DirectoryTestBase, @"TestPackageMovingAssets2\TestPackage2.xkpkg");
var subPackage = session.Packages.Find(Guid.Parse("281321F0-7664-4523-B1DC-3CFC26F80F77"));
var subPackage = session.Packages.Single(x => x.FullPath.GetFileNameWithoutExtension() == "TestPackage2");
subPackage.FullPath = Path.Combine(DirectoryTestBase, @"TestPackageMovingAssets2\SubPackage\SubPackage.xkpkg");
var result = new LoggerResult();
session.Save(result);
Expand All @@ -195,7 +190,7 @@ public void TestMovingAssets()
{
AssertResult(sessionResult3);
var session = sessionResult3.Session;
var project = session.Packages.Find(rootPackageId);
var project = session.Packages.Single(x => x.FullPath.GetFileNameWithoutExtension() == "TestPackage2");
Assert.NotNull(project);
Assert.True(3 == project.Assets.Count, "Invalid number of assets loaded");

Expand Down
48 changes: 8 additions & 40 deletions sources/assets/Xenko.Core.Assets/Package.cs
Expand Up @@ -51,21 +51,13 @@ public enum PackageState
[DataContract("Package")]
[NonIdentifiableCollectionItems]
[AssetDescription(PackageFileExtension)]
[DebuggerDisplay("Id: {Id}, Name: {Meta.Name}, Version: {Meta.Version}, Assets [{Assets.Count}]")]
[DebuggerDisplay("Name: {Meta.Name}, Version: {Meta.Version}, Assets [{Assets.Count}]")]
[AssetFormatVersion("Assets", PackageFileVersion, "0.0.0.4")]
[AssetUpgrader("Assets", "0.0.0.4", "3.1.0.0", typeof(MovePackageInsideProject))]
public sealed partial class Package : IIdentifiable, IFileSynchronizable, IAssetFinder
public sealed partial class Package : IFileSynchronizable, IAssetFinder
{
private const string PackageFileVersion = "3.1.0.0";

private Guid id;

// Note: Please keep this code in sync with Asset class
/// <summary>
/// Locks the unique identifier for further changes.
/// </summary>
internal bool IsIdLocked;

private readonly List<UFile> filesToDelete = new List<UFile>();

private PackageSession session;
Expand All @@ -90,7 +82,6 @@ public sealed partial class Package : IIdentifiable, IFileSynchronizable, IAsset
/// </summary>
public Package()
{
Id = Guid.NewGuid();
// Initializse package with default versions (same code as in Asset..ctor())
var defaultPackageVersion = AssetRegistry.GetCurrentFormatVersions(GetType());
if (defaultPackageVersion != null)
Expand All @@ -104,29 +95,6 @@ public Package()
settings = new Lazy<PackageUserSettings>(() => new PackageUserSettings(this));
}

// Note: Please keep this code in sync with Asset class
/// <summary>
/// Gets or sets the unique identifier of this package.
/// </summary>
/// <value>The identifier.</value>
[DataMember(-10000)]
[NonOverridable]
[Display(Browsable = false)]
public Guid Id
{
get
{
return id;
}
set
{
if (value != id && IsIdLocked)
throw new InvalidOperationException("Cannot change an Asset Object Id once it is locked by a package");

id = value;
}
}

// Note: Please keep this code in sync with Asset class
/// <summary>
/// Gets or sets the version number for this asset, used internally when migrating assets.
Expand Down Expand Up @@ -768,7 +736,7 @@ public static PackageContainer LoadProject(ILogger log, string filePath)
var projectPath = filePath;
var packagePath = Path.ChangeExtension(filePath, Package.PackageFileExtension);
var package = LoadRaw(log, packagePath);
return new SolutionProject(package, projectPath);
return new SolutionProject(package, Guid.NewGuid(), projectPath);
}
else
{
Expand All @@ -779,7 +747,7 @@ public static PackageContainer LoadProject(ILogger log, string filePath)
var projectPath = Path.ChangeExtension(package.FullPath, ".csproj");
if (File.Exists(projectPath))
{
return new SolutionProject(package, projectPath);
return new SolutionProject(package, Guid.NewGuid(), projectPath);
}
else
{
Expand Down Expand Up @@ -1051,7 +1019,7 @@ private void LoadAsset(AssetMigrationContext context, PackageLoadingAssetFile as

bool aliasOccurred;
AttachedYamlAssetMetadata yamlMetadata;
var asset = LoadAsset(context.Log, ref id, assetFullPath, assetPath.ToWindowsPath(), assetContent, out aliasOccurred, out yamlMetadata);
var asset = LoadAsset(context.Log, Meta.Name, assetFullPath, assetPath.ToWindowsPath(), assetContent, out aliasOccurred, out yamlMetadata);

// Create asset item
var assetItem = new AssetItem(assetPath, asset, this)
Expand Down Expand Up @@ -1117,7 +1085,7 @@ public void RestoreNugetPackages(ILogger log)
}
}

private static Asset LoadAsset(ILogger log, ref Guid packageId, string assetFullPath, string assetPath, byte[] assetContent, out bool assetDirty, out AttachedYamlAssetMetadata yamlMetadata)
private static Asset LoadAsset(ILogger log, string packageName, string assetFullPath, string assetPath, byte[] assetContent, out bool assetDirty, out AttachedYamlAssetMetadata yamlMetadata)
{
var loadResult = assetContent != null
? AssetFileSerializer.Load<Asset>(new MemoryStream(assetContent), assetFullPath, log)
Expand All @@ -1131,7 +1099,7 @@ private static Asset LoadAsset(ILogger log, ref Guid packageId, string assetFull
if (sourceCodeAsset != null)
{
// Use an id generated from the location instead of the default id
sourceCodeAsset.Id = SourceCodeAsset.GenerateIdFromLocation(packageId, assetPath);
sourceCodeAsset.Id = SourceCodeAsset.GenerateIdFromLocation(packageName, assetPath);
}

return loadResult.Asset;
Expand All @@ -1149,7 +1117,7 @@ private void LoadAssemblyReferencesForPackage(ILogger log, PackageLoadParameters

// Check if already loaded
// TODO: More advanced cases: unload removed references, etc...
var projectReference = new ProjectReference(Id, project.FullPath, Core.Assets.ProjectType.Library);
var projectReference = new ProjectReference(project.Id, project.FullPath, Core.Assets.ProjectType.Library);
if (LoadedAssemblies.Any(x => x.ProjectReference == projectReference))
return;

Expand Down
2 changes: 1 addition & 1 deletion sources/assets/Xenko.Core.Assets/PackageAssetCollection.cs
Expand Up @@ -363,7 +363,7 @@ public void CheckCanAdd(AssetItem item)
{
if (otherPackage.Assets.ContainsById(item.Id))
{
throw new ArgumentException("Cannot add the asset [{0}] that is already in different package [{1}] in the current session".ToFormat(item.Id, otherPackage.Id));
throw new ArgumentException("Cannot add the asset [{0}] that is already in different package [{1}] in the current session".ToFormat(item.Id, otherPackage.Meta.Name));
}
}
}
Expand Down
46 changes: 4 additions & 42 deletions sources/assets/Xenko.Core.Assets/PackageCollection.cs
Expand Up @@ -14,8 +14,6 @@ public interface IReadOnlyPackageCollection : IReadOnlyCollection<Package>, INot
{
Package Find(Dependency dependency);

Package Find(Guid packageGuid);

Package Find(PackageDependency packageDependency);

Package Find(string name, PackageVersionRange versionRange);
Expand Down Expand Up @@ -81,16 +79,6 @@ public Package Find(Dependency dependency)
return Find(dependency.Name, new PackageVersionRange(dependency.Version));
}

/// <summary>
/// Finds the specified package by its unique identifier.
/// </summary>
/// <param name="packageGuid">The package unique identifier.</param>
/// <returns>Package.</returns>
public Package Find(Guid packageGuid)
{
return packages.FirstOrDefault(package => package.Id == packageGuid);
}

/// <summary>
/// Finds the a package already in this collection from the specified dependency.
/// </summary>
Expand Down Expand Up @@ -119,11 +107,8 @@ public Package Find(string name, PackageVersionRange versionRange)
public void Add(Package item)
{
if (item == null) throw new ArgumentNullException("item");
if (Find(item.Id) == null)
{
packages.Add(item);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}
packages.Add(item);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}

public void Clear()
Expand All @@ -133,44 +118,21 @@ public void Clear()
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, packages, oldPackages));
}

/// <summary>
/// Determines whether this collection contains a package with the specified package unique identifier.
/// </summary>
/// <param name="packageGuid">The package unique identifier.</param>
/// <returns><c>true</c> if this collection contains a package with the specified package unique identifier; otherwise, <c>false</c>.</returns>
public bool ContainsById(Guid packageGuid)
{
return packages.Any(package => package.Id == packageGuid);
}

public bool Contains(Package item)
{
if (item == null) throw new ArgumentNullException("item");
return ContainsById(item.Id);
return packages.Contains(item);
}

public void CopyTo(Package[] array, int arrayIndex)
{
packages.CopyTo(array, arrayIndex);
}

public bool RemoveById(Guid packageGuid)
{
var item = Find(packageGuid);
if (item != null)
{

packages.Remove(item);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
return true;
}
return false;
}

public bool Remove(Package item)
{
if (item == null) throw new ArgumentNullException("item");
return RemoveById(item.Id);
return packages.Remove(item);
}

private void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
Expand Down

0 comments on commit e59316a

Please sign in to comment.