From dadb9207b9b4763422c7a5db4311be00f812e13e Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 13 Mar 2023 16:00:27 +0100 Subject: [PATCH] Fix Json asset cooking to properly serialize whole asset data even if modified at runtime --- Source/Editor/Cooker/Steps/CookAssetsStep.cpp | 4 +-- Source/Engine/Content/JsonAsset.cpp | 36 +++++++++++++------ Source/Engine/Content/JsonAsset.h | 11 ++++-- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Source/Editor/Cooker/Steps/CookAssetsStep.cpp b/Source/Editor/Cooker/Steps/CookAssetsStep.cpp index 92ebf39f6..85f4a4126 100644 --- a/Source/Editor/Cooker/Steps/CookAssetsStep.cpp +++ b/Source/Editor/Cooker/Steps/CookAssetsStep.cpp @@ -327,8 +327,8 @@ bool CookAssetsStep::ProcessDefaultAsset(AssetCookData& options) { // Use compact json rapidjson_flax::StringBuffer buffer; - rapidjson_flax::Writer writer(buffer); - asJsonAsset->Document.Accept(writer); + CompactJsonWriter writerObj(buffer); + asJsonAsset->Save(writerObj); // Store json data in the first chunk auto chunk = New(); diff --git a/Source/Engine/Content/JsonAsset.cpp b/Source/Engine/Content/JsonAsset.cpp index 1f0e914f2..86880e56b 100644 --- a/Source/Engine/Content/JsonAsset.cpp +++ b/Source/Engine/Content/JsonAsset.cpp @@ -146,7 +146,7 @@ void JsonAssetBase::GetReferences(const StringAnsiView& json, Array& outpu FindIds(document, output); } -bool JsonAssetBase::Save(const StringView& path) +bool JsonAssetBase::Save(const StringView& path) const { // Validate state if (WaitForLoaded()) @@ -160,11 +160,32 @@ bool JsonAssetBase::Save(const StringView& path) return true; } ScopeLock lock(Locker); - - // Serialize to json file + + // Serialize to json to the buffer rapidjson_flax::StringBuffer buffer; PrettyJsonWriter writerObj(buffer); - JsonWriter& writer = writerObj; + Save(writerObj); + + // Save json to file + if (File::WriteAllBytes(path.HasChars() ? path : StringView(GetPath()), (byte*)buffer.GetString(), (int32)buffer.GetSize())) + { + LOG(Error, "Cannot save \'{0}\'", ToString()); + return true; + } + + return false; +} + +bool JsonAssetBase::Save(JsonWriter& writer) const +{ + // Validate state + if (WaitForLoaded()) + { + LOG(Error, "Asset loading failed. Cannot save it."); + return true; + } + ScopeLock lock(Locker); + writer.StartObject(); { // Json resource header @@ -183,13 +204,6 @@ bool JsonAssetBase::Save(const StringView& path) } writer.EndObject(); - // Save json to file - if (File::WriteAllBytes(path.HasChars() ? path : StringView(GetPath()), (byte*)buffer.GetString(), (int32)buffer.GetSize())) - { - LOG(Error, "Cannot save \'{0}\'", ToString()); - return true; - } - return false; } diff --git a/Source/Engine/Content/JsonAsset.h b/Source/Engine/Content/JsonAsset.h index ca52ac86b..c53649d33 100644 --- a/Source/Engine/Content/JsonAsset.h +++ b/Source/Engine/Content/JsonAsset.h @@ -32,7 +32,7 @@ public: ISerializable::SerializeDocument Document; /// - /// The data node (reference from Document). + /// The data node (reference from Document or Document itself). /// ISerializable::DeserializeStream* Data; @@ -78,7 +78,14 @@ public: /// /// The custom asset path to use for the saving. Use empty value to save this asset to its own storage location. Can be used to duplicate asset. Must be specified when saving virtual asset. /// True if cannot save data, otherwise false. - API_FUNCTION() bool Save(const StringView& path = StringView::Empty); + API_FUNCTION() bool Save(const StringView& path = StringView::Empty) const; + + /// + /// Saves this asset to the Json Writer buffer (both ID, Typename header and Data contents). Supported only in Editor. + /// + /// The output Json Writer to write asset. + /// True if cannot save data, otherwise false. + bool Save(JsonWriter& writer) const; #endif protected: