JD2022-TU1/main/tools/framework/JD.Asset/AssetBase.cs

147 lines
No EOL
3.7 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Directory = System.IO.Directory;
namespace JD.Asset
{
public class AssetBase : IAsset, IEquatable<AssetBase>
{
private PathInfo _path;
private PathInfo _folder;
#region Constructor
public AssetBase()
{
// Parameterless constructor
}
public AssetBase(string path)
{
Path = new PathInfo(path);
}
#endregion
#region Properties
/// <summary>
/// The relative path to the asset
/// </summary>
[Browsable(true)]
[ReadOnly(true)]
[Description("Path of the asset")]
public PathInfo Path
{
get { return _path; }
set
{
_path = value;
Name = System.IO.Path.GetFileName(_path);
}
}
[Browsable(false)]
[ReadOnly(true)]
public ObservableCollection<IAsset> RelatedAssets { get; } = new ObservableCollection<IAsset>();
[Browsable(false)]
public AssetType AssetType { get; set; } = AssetType.File;
/// <summary>
/// The folder of the asset
/// </summary>
///
[Browsable(false)]
[ReadOnly(true)]
public PathInfo Folder
{
get
{
if (_folder != null)
{
return _folder;
}
if (IsFolder)
{
return _folder = Path;
}
return _folder = new PathInfo(Directory.GetParent(Path.Absolute).FullName, Path.DataRoot);
}
}
/// <summary>
/// The name of the asset
/// </summary>
[Browsable(true)]
[ReadOnly(true)]
[Description("Name of the asset")]
public string Name { get; set; }
/// <summary>
/// Indicates whether the asset is a folder or not
/// </summary>
[Browsable(false)]
public bool IsFolder => AssetType == AssetType.Directory;
/// <summary>
/// Indicated whether the asset is a file or not
/// </summary>
[Browsable(false)]
public bool IsFile => AssetType == AssetType.File;
/// <summary>
/// Indicates whether this asset is valid
/// </summary>
[Browsable(false)]
public virtual bool IsValid { get; } = true;
/// <summary>
/// The data root
/// </summary>
[Browsable(false)]
public string DataRoot => Path.DataRoot;
[Browsable(true), ReadOnly(true), Description("Dependencies on other assets")]
public AssetDependencyCollection Dependencies { get; } = new AssetDependencyCollection();
[Browsable(true), ReadOnly(true), Description("Collection of other assets referencing this asset")]
public AssetDependencyCollection ReferencedBy { get; } = new AssetDependencyCollection();
[Browsable(false), ReadOnly(true)]
public PathInfo MetadataPath => Path + ".meta";
#endregion
#region Methods
public override string ToString()
{
return Name;
}
#endregion
#region Comparison
public bool Equals(AssetBase other)
{
return other != null && string.Equals(Path, other.Path, StringComparison.OrdinalIgnoreCase);
}
public override bool Equals(object obj)
{
return Equals(obj as AssetBase);
}
public override int GetHashCode()
{
return StringComparer.OrdinalIgnoreCase.GetHashCode(Path);
}
#endregion
}
}