Universal AssetHandles. Serialize AssetHandle instead of AssetIdentifier

A bit unstable
This commit is contained in:
Simon Lübeß
2023-08-01 21:38:54 +02:00
parent 6d7f26b1f4
commit 60725c3588
45 changed files with 520 additions and 127 deletions
+38 -4
View File
@@ -50,16 +50,50 @@ abstract class Asset : RefCounter
static void AssetSerialize(BonWriter writer, ValueView value, BonEnvironment environment, SerializeValueState state)
{
Log.EngineLogger.Assert(value.type == typeof(Asset));
var handle = value.Get<Asset>()._handle;
Serialize.Value(writer, handle, environment);
let identifier = value.Get<Asset>().Identifier;
writer.String(identifier);
//AssetHandle.[Friend]AssetSerialize(writer, ValueView(typeof(AssetHandle), &handle), environment, state);
//writer.String(identifier);
}
static Result<void> AssetDeserialize(BonReader reader, ValueView value, BonEnvironment environment, DeserializeValueState state)
{
Log.EngineLogger.Assert(value.type == typeof(Asset));
String identifier = scope .();
Try!(Deserialize.Value<AssetHandle>(reader, let handle, environment));
//String identifier = scope .();
//Deserialize.String!(reader, ref identifier, environment);
AssetHandle loadedHandle = Content.LoadAsset(handle);
if (loadedHandle == .Invalid)
{
value.Assign<Asset>(null);
return .Ok;
}
Asset asset = Content.GetAsset<Asset>(handle);
if (asset != null)
{
Asset oldAsset = value.Get<Asset>();
oldAsset.ReleaseRef();
value.Assign(asset);
return .Ok;
}
else
{
Deserialize.Error!("Invalid resource path", reader, value.type);
}
/*String identifier = scope .();
Deserialize.String!(reader, ref identifier, environment);
@@ -84,7 +118,7 @@ abstract class Asset : RefCounter
else
{
Deserialize.Error!("Invalid resource path", reader, value.type);
}
}*/
}
//gBonEnv.typeHandlers.Add(typeof(Resource<>),
+17 -19
View File
@@ -50,6 +50,11 @@ struct AssetHandle : IHashable
{
Log.EngineLogger.Assert(value.type == typeof(AssetHandle));
var uuid = value.Get<AssetHandle>()._uuid;
Serialize.Value(writer, uuid, environment);
/*Log.EngineLogger.Assert(value.type == typeof(AssetHandle));
AssetHandle handle = value.Get<AssetHandle>();
if (handle.IsInvalid)
@@ -58,20 +63,24 @@ struct AssetHandle : IHashable
{
let identifier = handle.Get<Asset>().Identifier;
writer.String(identifier);
}
}*/
}
static Result<void> AssetDeserialize(BonReader reader, ValueView value, BonEnvironment environment, DeserializeValueState state)
{
Log.EngineLogger.Assert(value.type == typeof(AssetHandle));
String identifier = scope .();
Try!(Deserialize.Value<UUID>(reader, let uuid, environment));
value.Assign(AssetHandle(uuid));
/*String identifier = scope .();
Deserialize.String!(reader, ref identifier, environment);
AssetHandle handle = Content.LoadAsset(identifier);
value.Assign<AssetHandle>(handle);
value.Assign<AssetHandle>(handle);*/
return .Ok;
}
@@ -314,29 +323,18 @@ struct AssetHandle<T> where T : Asset
static void AssetSerialize(BonWriter writer, ValueView value, BonEnvironment environment, SerializeValueState state)
{
Log.EngineLogger.Assert(value.type == typeof(AssetHandle<T>));
AssetHandle handle = value.Get<AssetHandle<T>>();
if (handle.IsInvalid)
writer.String("");
else
{
let identifier = handle.Get<Asset>().Identifier;
writer.String(identifier);
}
Serialize.Value(writer, handle, environment);
}
static Result<void> AssetDeserialize(BonReader reader, ValueView value, BonEnvironment environment, DeserializeValueState state)
{
Log.EngineLogger.Assert(value.type == typeof(AssetHandle<T>));
String identifier = scope .();
Deserialize.String!(reader, ref identifier, environment);
AssetHandle<T> handle = Content.LoadAsset(identifier);
value.Assign<AssetHandle<T>>(handle);
Try!(Deserialize.Value<AssetHandle>(reader, let handle, environment));
value.Assign(AssetHandle<T>(handle));
return .Ok;
}
@@ -67,6 +67,19 @@ namespace GlitchyEngine.Content
static class Content
{
/// Loads the specified asset with the given contentManager or the current applications content manager.
public static AssetHandle LoadAsset(AssetHandle assetHandle, IContentManager contentManager = null, bool blocking = false)
{
var contentManager;
if (contentManager == null)
contentManager = Application.Get().ContentManager;
AssetHandle handle = contentManager.LoadAsset(assetHandle, blocking);
return handle;
}
/// Loads the specified asset with the given contentManager or the current applications content manager.
public static AssetHandle LoadAsset(StringView assetIdentifier, IContentManager contentManager = null, bool blocking = false)
{
@@ -119,6 +132,9 @@ namespace GlitchyEngine.Content
/// Loads the Asset with the given handle and returns the handle.
AssetHandle LoadAsset(StringView assetIdentifier, bool blocking = false);
/// Loads the Asset with the given handle and returns the handle. Returns .Invalid, if the asset handle doesn't exist.
AssetHandle LoadAsset(AssetHandle handle, bool blocking = false);
/// Returns the asset for the given handle or null, if it isn't loaded.
Asset GetAsset(AssetHandle handle)
{