From 2caa3e3def539ff6324d18b7e11cd32d6adaf05b Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Thu, 2 May 2019 11:23:32 +0400
Subject: [PATCH] QtLottie: Add precomp assets parsing.

---
 .../SourceFiles/lottie/lottie_animation.cpp   | 11 ++-
 .../SourceFiles/lottie/lottie_animation.h     | 11 +--
 Telegram/ThirdParty/qtlottie                  |  2 +-
 .../QtBodymovin/private/bmasset_p.h           | 72 +++++++++++++++++++
 .../QtBodymovin/private/bmprecompasset_p.h    | 67 +++++++++++++++++
 Telegram/gyp/lib_lottie.gyp                   |  4 ++
 6 files changed, 160 insertions(+), 7 deletions(-)
 create mode 100644 Telegram/ThirdParty/qtlottie_helper/QtBodymovin/private/bmasset_p.h
 create mode 100644 Telegram/ThirdParty/qtlottie_helper/QtBodymovin/private/bmprecompasset_p.h

diff --git a/Telegram/SourceFiles/lottie/lottie_animation.cpp b/Telegram/SourceFiles/lottie/lottie_animation.cpp
index dd9b01228..945a87127 100644
--- a/Telegram/SourceFiles/lottie/lottie_animation.cpp
+++ b/Telegram/SourceFiles/lottie/lottie_animation.cpp
@@ -28,6 +28,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 #include <QtBodymovin/private/bmbase_p.h>
 #include <QtBodymovin/private/bmlayer_p.h>
+#include <QtBodymovin/private/bmasset_p.h>
 
 #include "rasterrenderer/lottierasterrenderer.h"
 
@@ -138,8 +139,14 @@ void Animation::parse(const QByteArray &content) {
 		}
 	}
 
-	if (root.value(qstr("assets")).toArray().count()) {
-		_unsupported = true;
+	const auto assets = root.value(qstr("assets")).toArray();
+	for (const auto &entry : assets) {
+		if (const auto asset = BMAsset::construct(entry.toObject())) {
+			_assetIndexById.emplace(asset->id(), _assets.size());
+			_assets.emplace_back(asset);
+		} else {
+			_unsupported = true;
+		}
 	}
 
 	if (root.value(qstr("chars")).toArray().count()) {
diff --git a/Telegram/SourceFiles/lottie/lottie_animation.h b/Telegram/SourceFiles/lottie/lottie_animation.h
index c4040f4df..48bae857a 100644
--- a/Telegram/SourceFiles/lottie/lottie_animation.h
+++ b/Telegram/SourceFiles/lottie/lottie_animation.h
@@ -11,12 +11,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "base/flat_map.h"
 
 #include <crl/crl_time.h>
-#include <QString>
-#include <QHash>
-#include <QPainter>
+
+class QImage;
+class QString;
+class QByteArray;
 
 class BMBase;
-class BMLayer;
+class BMAsset;
 
 namespace Lottie {
 
@@ -77,6 +78,8 @@ private:
 	PlaybackOptions _options;
 
 	std::unique_ptr<BMBase> _treeBlueprint;
+	std::vector<std::unique_ptr<BMAsset>> _assets;
+	base::flat_map<QString, int> _assetIndexById;
 
 };
 
diff --git a/Telegram/ThirdParty/qtlottie b/Telegram/ThirdParty/qtlottie
index 270cd6f31..44fa23063 160000
--- a/Telegram/ThirdParty/qtlottie
+++ b/Telegram/ThirdParty/qtlottie
@@ -1 +1 @@
-Subproject commit 270cd6f31b9c67bdf2ff7cd961df8e5bb86e137c
+Subproject commit 44fa230637dafe2fe01ffc6e93c3e3f5841dcc5a
diff --git a/Telegram/ThirdParty/qtlottie_helper/QtBodymovin/private/bmasset_p.h b/Telegram/ThirdParty/qtlottie_helper/QtBodymovin/private/bmasset_p.h
new file mode 100644
index 000000000..a60d1b030
--- /dev/null
+++ b/Telegram/ThirdParty/qtlottie_helper/QtBodymovin/private/bmasset_p.h
@@ -0,0 +1,72 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the lottie-qt module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef BMASSET_P_H
+#define BMASSET_P_H
+
+//
+//  W A R N I N G
+//  -------------
+//
+// This file is not part of the Qt API.  It exists purely as an
+// implementation detail.  This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <QtBodymovin/private/bmbase_p.h>
+
+QT_BEGIN_NAMESPACE
+
+class LottieRenderer;
+
+class BODYMOVIN_EXPORT BMAsset : public BMBase
+{
+public:
+	BMAsset() = default;
+	explicit BMAsset (const BMAsset &other) = default;
+    ~BMAsset() = default;
+
+    BMBase *clone() const override;
+
+    static BMAsset *construct(QJsonObject definition);
+
+    void parse(const QJsonObject &definition) override;
+
+	QString id() const;
+
+private:
+	QString m_id;
+
+};
+
+QT_END_NAMESPACE
+
+#endif // BMASSET_P_H
diff --git a/Telegram/ThirdParty/qtlottie_helper/QtBodymovin/private/bmprecompasset_p.h b/Telegram/ThirdParty/qtlottie_helper/QtBodymovin/private/bmprecompasset_p.h
new file mode 100644
index 000000000..6fb19b4e9
--- /dev/null
+++ b/Telegram/ThirdParty/qtlottie_helper/QtBodymovin/private/bmprecompasset_p.h
@@ -0,0 +1,67 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the lottie-qt module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef BMPRECOMPASSET_P_H
+#define BMPRECOMPASSET_P_H
+
+//
+//  W A R N I N G
+//  -------------
+//
+// This file is not part of the Qt API.  It exists purely as an
+// implementation detail.  This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <QtBodymovin/private/bmasset_p.h>
+
+QT_BEGIN_NAMESPACE
+
+class LottieRenderer;
+
+class BODYMOVIN_EXPORT BMPreCompAsset : public BMAsset
+{
+public:
+	BMPreCompAsset() = default;
+	explicit BMPreCompAsset (const BMPreCompAsset &other) = default;
+	BMPreCompAsset(const QJsonObject &definition);
+	~BMPreCompAsset() = default;
+
+    BMBase *clone() const override;
+
+private:
+	void parseLayers(const QJsonArray &definition);
+
+};
+
+QT_END_NAMESPACE
+
+#endif // BMPRECOMPASSET_P_H
diff --git a/Telegram/gyp/lib_lottie.gyp b/Telegram/gyp/lib_lottie.gyp
index 1f2287e41..a8927bf07 100644
--- a/Telegram/gyp/lib_lottie.gyp
+++ b/Telegram/gyp/lib_lottie.gyp
@@ -123,8 +123,12 @@
       '<(lottie_loc)/imports/rasterrenderer/lottierasterrenderer.h',
 
       # added to qtlottie/src/bodymovin/bodymovin.pro
+      '<(lottie_loc)/bodymovin/bmasset.cpp',
+      '<(lottie_loc)/bodymovin/bmprecompasset.cpp',
       '<(lottie_loc)/bodymovin/bmnulllayer.cpp',
 
+      '<(lottie_loc)/bodymovin/bmasset_p.h',
+      '<(lottie_loc)/bodymovin/bmprecompasset_p.h',
       '<(lottie_loc)/bodymovin/bmnulllayer_p.h',
     ],
     'conditions': [[ 'build_macold', {