Fix Json asset cooking to properly serialize whole asset data even if modified at runtime

This commit is contained in:
Wojtek Figat
2023-03-13 16:00:27 +01:00
parent 086c2f155d
commit dadb9207b9
3 changed files with 36 additions and 15 deletions

View File

@@ -327,8 +327,8 @@ bool CookAssetsStep::ProcessDefaultAsset(AssetCookData& options)
{
// Use compact json
rapidjson_flax::StringBuffer buffer;
rapidjson_flax::Writer<rapidjson_flax::StringBuffer> writer(buffer);
asJsonAsset->Document.Accept(writer);
CompactJsonWriter writerObj(buffer);
asJsonAsset->Save(writerObj);
// Store json data in the first chunk
auto chunk = New<FlaxChunk>();

View File

@@ -146,7 +146,7 @@ void JsonAssetBase::GetReferences(const StringAnsiView& json, Array<Guid>& outpu
FindIds(document, output);
}
bool JsonAssetBase::Save(const StringView& path)
bool JsonAssetBase::Save(const StringView& path) const
{
// Validate state
if (WaitForLoaded())
@@ -161,10 +161,31 @@ bool JsonAssetBase::Save(const StringView& path)
}
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;
}

View File

@@ -32,7 +32,7 @@ public:
ISerializable::SerializeDocument Document;
/// <summary>
/// The data node (reference from Document).
/// The data node (reference from Document or Document itself).
/// </summary>
ISerializable::DeserializeStream* Data;
@@ -78,7 +78,14 @@ public:
/// </summary>
/// <param name="path">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.</param>
/// <returns>True if cannot save data, otherwise false.</returns>
API_FUNCTION() bool Save(const StringView& path = StringView::Empty);
API_FUNCTION() bool Save(const StringView& path = StringView::Empty) const;
/// <summary>
/// Saves this asset to the Json Writer buffer (both ID, Typename header and Data contents). Supported only in Editor.
/// </summary>
/// <param name="writer">The output Json Writer to write asset.</param>
/// <returns>True if cannot save data, otherwise false.</returns>
bool Save(JsonWriter& writer) const;
#endif
protected: