From 3fccfc74d743e7d5c258101e318829914b8d61cb Mon Sep 17 00:00:00 2001 From: Doprez <73259914+Doprez@users.noreply.github.com> Date: Thu, 16 May 2024 11:03:27 -0600 Subject: [PATCH 1/6] Add method to validate type from url --- .../Serialization/Contents/ContentManager.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs b/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs index c09318039d..4ee1bb03d1 100644 --- a/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs +++ b/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs @@ -402,6 +402,19 @@ private object DeserializeObject(string initialUrl, string newUrl, Type type, ob return result; } + + public Type GetType(string url) + { + if(LoadedAssetUrls.TryGetValue(url, out var reference)) + { + if (reference.Object != null) + { + return reference.Object.GetType(); + } + } + + return null; + } internal Reference FindDeserializedObject(string url, Type objType) { From 77d6c0a1ac9954bdb52a360613a6321c0e4282df Mon Sep 17 00:00:00 2001 From: Doprez <73259914+Doprez@users.noreply.github.com> Date: Thu, 16 May 2024 11:04:21 -0600 Subject: [PATCH 2/6] added type validation --- .../Stride.Graphics/Data/TextureContentSerializer.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs b/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs index 1102794016..6d8d62d1da 100644 --- a/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs +++ b/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs @@ -46,9 +46,13 @@ internal static void Serialize(ArchiveMode mode, SerializationStream stream, Tex { var assetManager = services.GetService(); assetManager.TryGetAssetUrl(graphicsResource, out var url); - var textureDataReloaded = assetManager.Load(url); - ((Texture)graphicsResource).Recreate(textureDataReloaded.ToDataBox()); - assetManager.Unload(textureDataReloaded); + + if (assetManager.GetType(url) == typeof(Image)) + { + var textureDataReloaded = assetManager.Load(url); + ((Texture)graphicsResource).Recreate(textureDataReloaded.ToDataBox()); + assetManager.Unload(textureDataReloaded); + } }; } } From dc0c7cf7e0e42133ae50d49d35ffc31a2ae15771 Mon Sep 17 00:00:00 2001 From: Doprez <73259914+Doprez@users.noreply.github.com> Date: Thu, 16 May 2024 13:44:44 -0600 Subject: [PATCH 3/6] added Texture reload --- .../engine/Stride.Graphics/Data/TextureContentSerializer.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs b/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs index 6d8d62d1da..163799767f 100644 --- a/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs +++ b/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs @@ -53,6 +53,12 @@ internal static void Serialize(ArchiveMode mode, SerializationStream stream, Tex ((Texture)graphicsResource).Recreate(textureDataReloaded.ToDataBox()); assetManager.Unload(textureDataReloaded); } + if(assetManager.GetType(url) == typeof(Texture)) + { + var textureDataReloaded = assetManager.Load(url); + ((Texture)graphicsResource).Recreate(); + assetManager.Unload(textureDataReloaded); + } }; } } From 29b5a7e7ee8462128406b7a42d6d040acd0771cb Mon Sep 17 00:00:00 2001 From: Doprez <73259914+Doprez@users.noreply.github.com> Date: Thu, 16 May 2024 17:29:41 -0600 Subject: [PATCH 4/6] fixed unneseccary nesting --- .../Serialization/Contents/ContentManager.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs b/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs index 4ee1bb03d1..85a49fcced 100644 --- a/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs +++ b/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs @@ -405,15 +405,8 @@ private object DeserializeObject(string initialUrl, string newUrl, Type type, ob public Type GetType(string url) { - if(LoadedAssetUrls.TryGetValue(url, out var reference)) - { - if (reference.Object != null) - { - return reference.Object.GetType(); - } - } - - return null; + _ = LoadedAssetUrls.TryGetValue(url, out var reference); + return reference.Object?.GetType(); } internal Reference FindDeserializedObject(string url, Type objType) From d2d3e97e3f4fc3f261f9ef5963ccafab8bdfc2a7 Mon Sep 17 00:00:00 2001 From: Doprez <73259914+Doprez@users.noreply.github.com> Date: Sat, 18 May 2024 12:54:30 -0600 Subject: [PATCH 5/6] eiderens suggestions --- .../Serialization/Contents/ContentManager.cs | 14 ++++++++++---- .../Data/TextureContentSerializer.cs | 9 ++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs b/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs index 85a49fcced..741b8bb855 100644 --- a/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs +++ b/sources/core/Stride.Core.Serialization/Serialization/Contents/ContentManager.cs @@ -402,11 +402,17 @@ private object DeserializeObject(string initialUrl, string newUrl, Type type, ob return result; } - - public Type GetType(string url) + + public bool TryGetLoadedAsset(string url, out object asset) { - _ = LoadedAssetUrls.TryGetValue(url, out var reference); - return reference.Object?.GetType(); + if (LoadedAssetUrls.TryGetValue(url, out var reference)) + { + asset = reference.Object; + return true; + } + + asset = null; + return false; } internal Reference FindDeserializedObject(string url, Type objType) diff --git a/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs b/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs index 163799767f..00c2cea2e3 100644 --- a/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs +++ b/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs @@ -46,16 +46,15 @@ internal static void Serialize(ArchiveMode mode, SerializationStream stream, Tex { var assetManager = services.GetService(); assetManager.TryGetAssetUrl(graphicsResource, out var url); + var textureDataReloaded = assetManager.Load(url); - if (assetManager.GetType(url) == typeof(Image)) + if (textureDataReloaded is Image image) { - var textureDataReloaded = assetManager.Load(url); - ((Texture)graphicsResource).Recreate(textureDataReloaded.ToDataBox()); + ((Texture)graphicsResource).Recreate(image.ToDataBox()); assetManager.Unload(textureDataReloaded); } - if(assetManager.GetType(url) == typeof(Texture)) + if (textureDataReloaded is Texture) { - var textureDataReloaded = assetManager.Load(url); ((Texture)graphicsResource).Recreate(); assetManager.Unload(textureDataReloaded); } From 9046c59fb5e039de2e62c6917760cdf16a16e738 Mon Sep 17 00:00:00 2001 From: Doprez <73259914+Doprez@users.noreply.github.com> Date: Sat, 18 May 2024 12:56:38 -0600 Subject: [PATCH 6/6] removed double check --- sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs b/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs index 00c2cea2e3..a9f70d9e19 100644 --- a/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs +++ b/sources/engine/Stride.Graphics/Data/TextureContentSerializer.cs @@ -53,7 +53,7 @@ internal static void Serialize(ArchiveMode mode, SerializationStream stream, Tex ((Texture)graphicsResource).Recreate(image.ToDataBox()); assetManager.Unload(textureDataReloaded); } - if (textureDataReloaded is Texture) + else if (textureDataReloaded is Texture) { ((Texture)graphicsResource).Recreate(); assetManager.Unload(textureDataReloaded);