Skip to content

Commit

Permalink
[Assets] Allow some assets to be non-referenceable (asset name collis…
Browse files Browse the repository at this point in the history
…ion is allowed in this case because they exist only at compile-time). This is used for scripts, otherwise they can't have same name as an asset.
  • Loading branch information
xen2 committed Dec 20, 2018
1 parent c2b417c commit 772c37b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
8 changes: 6 additions & 2 deletions sources/assets/Xenko.Core.Assets/Analysis/AssetCollision.cs
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using Xenko.Core.IO;
using Xenko.Core.Serialization.Contents;
Expand Down Expand Up @@ -66,8 +67,11 @@ public static void Clean(Package package, ICollection<AssetItem> inputItems, ICo
changed = true;
}

UFile newLocation;
if (assetResolver.RegisterLocation(item.Location, out newLocation))
// Note: we ignore name collisions if asset is not referenceable
var referenceable = item.Asset.GetType().GetCustomAttribute<AssetDescriptionAttribute>()?.Referenceable ?? true;

UFile newLocation = null;
if (referenceable && assetResolver.RegisterLocation(item.Location, out newLocation))
{
changed = true;
}
Expand Down
6 changes: 6 additions & 0 deletions sources/assets/Xenko.Core.Assets/AssetDescriptionAttribute.cs
Expand Up @@ -30,6 +30,12 @@ public AssetDescriptionAttribute(string fileExtensions)
/// </summary>
public bool AllowArchetype { get; set; } = true;

/// <summary>
/// Defines if an asset is referenceable through an <see cref="AssetReference"/>.
/// Asset name collision is allowed in this case because they exist only at compile-time.
/// </summary>
public bool Referenceable { get; set; } = true;

/// <summary>
/// Always mark this asset type as root.
/// </summary>
Expand Down
18 changes: 15 additions & 3 deletions sources/assets/Xenko.Core.Assets/PackageAssetCollection.cs
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using Xenko.Core;
using Xenko.Core.Diagnostics;
Expand Down Expand Up @@ -132,8 +133,12 @@ public void Add(AssetItem item)
var asset = item.Asset;
asset.IsIdLocked = true;

// Note: we ignore name collisions if asset is not referenceable
var referenceable = item.Asset.GetType().GetCustomAttribute<AssetDescriptionAttribute>()?.Referenceable ?? true;

// Maintain all internal maps
mapPathToId.Add(item.Location, item.Id);
if (referenceable)
mapPathToId.Add(item.Location, item.Id);
mapIdToPath.Add(item.Id, item.Location);
mapIdToAsset.Add(item.Id, item);
registeredItems.Add(item);
Expand Down Expand Up @@ -204,7 +209,11 @@ public bool Remove(AssetItem item)
registeredItems.Remove(item);
mapIdToAsset.Remove(item.Id);
mapIdToPath.Remove(item.Id);
mapPathToId.Remove(item.Location);

// Note: we ignore name collisions if asset is not referenceable
var referenceable = item.Asset.GetType().GetCustomAttribute<AssetDescriptionAttribute>()?.Referenceable ?? true;
if (referenceable)
mapPathToId.Remove(item.Location);

RemoveInternal(item);

Expand Down Expand Up @@ -328,8 +337,11 @@ public void CheckCanAdd(AssetItem item)
throw new ArgumentException("Cannot add an asset that is already added to another package", "item");
}

// Note: we ignore name collisions if asset is not referenceable
var referenceable = item.Asset.GetType().GetCustomAttribute<AssetDescriptionAttribute>()?.Referenceable ?? true;

var location = item.Location;
if (mapPathToId.ContainsKey(location))
if (referenceable && mapPathToId.ContainsKey(location))
{
throw new ArgumentException("An asset [{0}] with the same location [{1}] is already registered ".ToFormat(mapPathToId[location], location.GetDirectoryAndFileName()), "item");
}
Expand Down
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xenko.Core.Assets.Analysis;
Expand Down Expand Up @@ -399,7 +400,7 @@ public void Delete()

public void CheckConsistency()
{
var assetList = Package.Assets.ToDictionary(x => x.Location.FullPath);
var assetList = Package.Assets.Where(item => item.Asset.GetType().GetCustomAttribute<AssetDescriptionAttribute>()?.Referenceable ?? true).ToDictionary(x => x.Location.FullPath);
var assetViewModels = Assets.ToList();
var logger = Session.AssetLog.GetLogger(LogKey.Get("Consistency"));

Expand Down
Expand Up @@ -7,7 +7,7 @@
namespace Xenko.Assets.Scripts
{
[DataContract("ScriptSourceFileAsset")]
[AssetDescription(Extension, AlwaysMarkAsRoot = true, AllowArchetype = false)]
[AssetDescription(Extension, AlwaysMarkAsRoot = true, AllowArchetype = false, Referenceable = false)]
public sealed partial class ScriptSourceFileAsset : ProjectSourceCodeAsset
{
public const string Extension = ".cs";
Expand Down

0 comments on commit 772c37b

Please sign in to comment.