Fixed AssetHandle serialization

- Serialize MeshComponents
- Fixed handling renamed files
This commit is contained in:
Simon Lübeß
2023-03-19 01:04:32 +01:00
parent c164aeecaa
commit edcec02e94
19 changed files with 324 additions and 321 deletions
+9 -1
View File
@@ -63,7 +63,15 @@ abstract class Asset : RefCounter
Deserialize.String!(reader, ref identifier, environment);
Asset asset = Content.GetAsset<Asset>(Content.LoadAsset(identifier));
AssetHandle handle = Content.LoadAsset(identifier);
if (handle == .Invalid)
{
value.Assign<Asset>(null);
return .Ok;
}
Asset asset = Content.GetAsset<Asset>(handle);
if (asset != null)
{
+75
View File
@@ -1,9 +1,12 @@
using Bon;
using Bon.Integrated;
using System;
using xxHash;
using System.Collections;
using System.Reflection;
using System.Diagnostics;
using GlitchyEngine.Core;
namespace GlitchyEngine.Content;
struct AssetHandle : IHashable
@@ -27,6 +30,12 @@ struct AssetHandle : IHashable
_uuid = uuid;
}
static this
{
gBonEnv.typeHandlers.Add(typeof(AssetHandle),
((.)new => AssetSerialize, new => AssetDeserialize));
}
[Inline]
public T Get<T>(IContentManager contentManager = null) where T : Asset
{
@@ -34,6 +43,36 @@ struct AssetHandle : IHashable
}
public int GetHashCode() => _uuid.GetHashCode();
static void AssetSerialize(BonWriter writer, ValueView value, BonEnvironment environment, SerializeValueState state)
{
Log.EngineLogger.Assert(value.type == typeof(AssetHandle));
AssetHandle handle = value.Get<AssetHandle>();
if (handle.IsInvalid)
writer.String("");
else
{
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 .();
Deserialize.String!(reader, ref identifier, environment);
AssetHandle handle = Content.LoadAsset(identifier);
value.Assign<AssetHandle>(handle);
return .Ok;
}
}
struct AssetHandle<T> where T : Asset
@@ -72,6 +111,12 @@ struct AssetHandle<T> where T : Asset
_asset = null;
}
static this
{
gBonEnv.typeHandlers.Add(typeof(AssetHandle<T>),
((.)new => AssetSerialize, new => AssetDeserialize));
}
public static implicit operator Self(AssetHandle handle)
{
return Self(handle);
@@ -258,4 +303,34 @@ struct AssetHandle<T> where T : Asset
{
return AssetHandle<NewT>(this._handle, this._contentManager);
}
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);
}
}
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);
return .Ok;
}
}