From 233de2d4a4951280f423e2ce69f79f5b1fb1c787 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 20 Dec 2021 18:47:19 +0100 Subject: [PATCH] Fix Animation timeline load/save with the latest format --- Source/Engine/Content/Assets/Animation.cpp | 183 +++++++++++---------- 1 file changed, 93 insertions(+), 90 deletions(-) diff --git a/Source/Engine/Content/Assets/Animation.cpp b/Source/Engine/Content/Assets/Animation.cpp index 468669a5c..1ef3add14 100644 --- a/Source/Engine/Content/Assets/Animation.cpp +++ b/Source/Engine/Content/Assets/Animation.cpp @@ -120,7 +120,7 @@ void Animation::LoadTimeline(BytesContainer& result) const MemoryWriteStream stream(4096); // Version - stream.WriteInt32(3); + stream.WriteInt32(4); // Meta float fps = (float)Data.FramesPerSecond; @@ -230,108 +230,111 @@ bool Animation::SaveTimeline(BytesContainer& data) LOG(Error, "Asset loading failed. Cannot save it."); return true; } - ScopeLock lock(Locker); - MemoryReadStream stream(data.Get(), data.Length()); // Version int32 version; stream.ReadInt32(&version); - if (version != 3) + switch (version) { - LOG(Error, "Unknown timeline data version {0}.", version); - return true; - } - - // Meta - float fps; - stream.ReadFloat(&fps); - Data.FramesPerSecond = static_cast(fps); - int32 duration; - stream.ReadInt32(&duration); - Data.Duration = static_cast(duration); - int32 tracksCount; - stream.ReadInt32(&tracksCount); - - // Tracks - Data.Channels.Clear(); - Dictionary animationChannelTrackIndexToChannelIndex; - animationChannelTrackIndexToChannelIndex.EnsureCapacity(tracksCount * 3); - for (int32 trackIndex = 0; trackIndex < tracksCount; trackIndex++) + case 3: // [Deprecated on 03.09.2021 expires on 03.09.2023] + case 4: { - const byte trackType = stream.ReadByte(); - const byte trackFlags = stream.ReadByte(); - int32 parentIndex, childrenCount; - stream.ReadInt32(&parentIndex); - stream.ReadInt32(&childrenCount); - String name; - stream.ReadString(&name, -13); - Color32 color; - stream.Read(&color); - switch (trackType) + // Meta + float fps; + stream.ReadFloat(&fps); + Data.FramesPerSecond = static_cast(fps); + int32 duration; + stream.ReadInt32(&duration); + Data.Duration = static_cast(duration); + int32 tracksCount; + stream.ReadInt32(&tracksCount); + + // Tracks + Data.Channels.Clear(); + Dictionary animationChannelTrackIndexToChannelIndex; + animationChannelTrackIndexToChannelIndex.EnsureCapacity(tracksCount * 3); + for (int32 trackIndex = 0; trackIndex < tracksCount; trackIndex++) { - case 17: - { - // Animation Channel track - const int32 channelIndex = Data.Channels.Count(); - animationChannelTrackIndexToChannelIndex[trackIndex] = channelIndex; - auto& channel = Data.Channels.AddOne(); - channel.NodeName = name; - break; - } - case 18: - { - // Animation Channel Data track - const byte type = stream.ReadByte(); - int32 keyframesCount; - stream.ReadInt32(&keyframesCount); - int32 channelIndex; - if (!animationChannelTrackIndexToChannelIndex.TryGet(parentIndex, channelIndex)) + const byte trackType = stream.ReadByte(); + const byte trackFlags = stream.ReadByte(); + int32 parentIndex, childrenCount; + stream.ReadInt32(&parentIndex); + stream.ReadInt32(&childrenCount); + String name; + stream.ReadString(&name, -13); + Color32 color; + stream.Read(&color); + switch (trackType) { - LOG(Error, "Invalid animation channel data track parent linkage."); + case 17: + { + // Animation Channel track + const int32 channelIndex = Data.Channels.Count(); + animationChannelTrackIndexToChannelIndex[trackIndex] = channelIndex; + auto& channel = Data.Channels.AddOne(); + channel.NodeName = name; + break; + } + case 18: + { + // Animation Channel Data track + const byte type = stream.ReadByte(); + int32 keyframesCount; + stream.ReadInt32(&keyframesCount); + int32 channelIndex; + if (!animationChannelTrackIndexToChannelIndex.TryGet(parentIndex, channelIndex)) + { + LOG(Error, "Invalid animation channel data track parent linkage."); + return true; + } + auto& channel = Data.Channels[channelIndex]; + switch (type) + { + case 0: + channel.Position.Resize(keyframesCount); + for (int32 i = 0; i < keyframesCount; i++) + { + LinearCurveKeyframe& k = channel.Position.GetKeyframes()[i]; + stream.ReadFloat(&k.Time); + k.Time *= fps; + stream.Read(&k.Value); + } + break; + case 1: + channel.Rotation.Resize(keyframesCount); + for (int32 i = 0; i < keyframesCount; i++) + { + LinearCurveKeyframe& k = channel.Rotation.GetKeyframes()[i]; + stream.ReadFloat(&k.Time); + k.Time *= fps; + stream.Read(&k.Value); + } + break; + case 2: + channel.Scale.Resize(keyframesCount); + for (int32 i = 0; i < keyframesCount; i++) + { + LinearCurveKeyframe& k = channel.Scale.GetKeyframes()[i]; + stream.ReadFloat(&k.Time); + k.Time *= fps; + stream.Read(&k.Value); + } + break; + } + break; + } + default: + LOG(Error, "Unsupported track type {0} for animation.", trackType); return true; } - auto& channel = Data.Channels[channelIndex]; - switch (type) - { - case 0: - channel.Position.Resize(keyframesCount); - for (int32 i = 0; i < keyframesCount; i++) - { - LinearCurveKeyframe& k = channel.Position.GetKeyframes()[i]; - stream.ReadFloat(&k.Time); - k.Time *= fps; - stream.Read(&k.Value); - } - break; - case 1: - channel.Rotation.Resize(keyframesCount); - for (int32 i = 0; i < keyframesCount; i++) - { - LinearCurveKeyframe& k = channel.Rotation.GetKeyframes()[i]; - stream.ReadFloat(&k.Time); - k.Time *= fps; - stream.Read(&k.Value); - } - break; - case 2: - channel.Scale.Resize(keyframesCount); - for (int32 i = 0; i < keyframesCount; i++) - { - LinearCurveKeyframe& k = channel.Scale.GetKeyframes()[i]; - stream.ReadFloat(&k.Time); - k.Time *= fps; - stream.Read(&k.Value); - } - break; - } - break; - } - default: - LOG(Error, "Unsupported track type {0} for animation.", trackType); - return true; } + break; + } + default: + LOG(Warning, "Unknown timeline version {0}.", version); + return true; } if (stream.GetLength() != stream.GetPosition()) {