Skip to content
Permalink
Browse files

[Assets] Allow some assets to be non-referenceable (asset name collis…

…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 14, 2018
1 parent c2b417c commit 772c37b0ebf0853cc0e121ed2c4538aaeba5ca06
@@ -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;
@@ -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;
}
@@ -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>
@@ -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;
@@ -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);
@@ -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);

@@ -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");
}
@@ -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;
@@ -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"));

@@ -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";

0 comments on commit 772c37b

Please sign in to comment.
You can’t perform that action at this time.