Start of async asset loading

- Refactored content manager and asset loaders
- Asset placeholder
- Error asset
This commit is contained in:
Simon Lübeß
2023-03-18 18:59:30 +01:00
parent 7b6662cbc4
commit 50714976e9
11 changed files with 329 additions and 321 deletions
+5 -8
View File
@@ -8,7 +8,7 @@ using System.IO;
namespace GlitchyEngine.Content;
[BonTarget]
class Asset : RefCounter
abstract class Asset : RefCounter
{
internal AssetHandle _handle = .Invalid;
@@ -22,15 +22,12 @@ class Asset : RefCounter
public StringView Identifier
{
get => _identifier;
set
{
_contentManager?.UpdateAssetIdentifier(this, _identifier, value);
_identifier.Set(value);
// TODO: do we need to tell the content manager, that the name changed?
}
internal set => _identifier.Set(value);
}
/// If true the asset is completely loaded. If false it is only partially loaded (if at all).
public bool Complete { get; internal set; }
// TODO: do we need unmanaged assets? Probably not...
/// Gets the content manager that manages this asset; or null if this asset isn't managed.
public IContentManager ContentManager => _contentManager;
+5 -5
View File
@@ -11,10 +11,10 @@ struct AssetHandle : IHashable
private UUID _uuid;
/// Defines an asset that is invalid.
public const AssetHandle Invalid = .(UUID(0xAAAA'AAAA'AAAA'AAAA));
public const AssetHandle Invalid = .(UUID(0));
public bool IsValid => this == .Invalid;
public bool IsInvalid => !IsValid;
public bool IsValid => this != .Invalid;
public bool IsInvalid => this == .Invalid;
/// Create a new random AssetHandle
public this()
@@ -49,8 +49,8 @@ struct AssetHandle<T> where T : Asset
public const Self Invalid = .();
public bool IsValid => this == .Invalid;
public bool IsInvalid => !IsValid;
public bool IsValid => this != .Invalid;
public bool IsInvalid => this == .Invalid;
public this(AssetHandle handle, IContentManager contentManager = null)
{
+10 -13
View File
@@ -57,19 +57,25 @@ namespace GlitchyEngine.Content
/// @param contentManager The content manager used to load the asset.
/// @returns The loaded asset.
Asset LoadAsset(Stream file, AssetLoaderConfig config, StringView assetIdentifier, StringView? subAsset, IContentManager contentManager);
/// Returns the placeholder asset.
Asset GetPlaceholderAsset(Type assetType);
/// Returns the error asset.
Asset GetErrorAsset(Type assetType);
}
static class Content
{
/// Loads the specified asset with the given contentManager or the current applications content manager.
public static AssetHandle LoadAsset(StringView assetIdentifier, IContentManager contentManager = null)
public static AssetHandle LoadAsset(StringView assetIdentifier, IContentManager contentManager = null, bool blocking = false)
{
var contentManager;
if (contentManager == null)
contentManager = Application.Get().ContentManager;
AssetHandle handle = contentManager.LoadAsset(assetIdentifier);
AssetHandle handle = contentManager.LoadAsset(assetIdentifier, blocking);
return handle;
}
@@ -111,7 +117,7 @@ namespace GlitchyEngine.Content
interface IContentManager
{
/// Loads the Asset with the given handle and returns the handle.
AssetHandle LoadAsset(StringView assetIdentifier);
AssetHandle LoadAsset(StringView assetIdentifier, bool blocking = false);
/// Returns the asset for the given handle or null, if it isn't loaded.
Asset GetAsset(AssetHandle handle)
@@ -128,10 +134,6 @@ namespace GlitchyEngine.Content
/// The content manager will no longer manage the asset.
void UnmanageAsset(AssetHandle asset);
// TODO: Maybe calling UnmanageAsset -> ManageAsset is enough....
/// Provides a method for the asset to tell its content manager that the identifer changed.
void UpdateAssetIdentifier(Asset asset, StringView oldIdentifier, StringView newIdentifier);
/// Returns a data stream for the given asset.
Stream GetStream(StringView assetIdentifier);
@@ -149,7 +151,7 @@ namespace GlitchyEngine.Content
Runtime.NotImplemented();
}
public AssetHandle LoadAsset(StringView assetIdentifier)
public AssetHandle LoadAsset(StringView assetIdentifier, bool blocking = false)
{
Runtime.NotImplemented();
}
@@ -169,11 +171,6 @@ namespace GlitchyEngine.Content
Runtime.NotImplemented();
}
public void UpdateAssetIdentifier(Asset asset, StringView oldIdentifier, StringView newIdentifier)
{
Runtime.NotImplemented();
}
public Stream GetStream(StringView assetIdentifier)
{
Runtime.NotImplemented();