Overte C++ Documentation
AssetServer.h
1 //
2 // AssetServer.h
3 // assignment-client/src/assets
4 //
5 // Created by Ryan Huffman on 2015/07/21
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_AssetServer_h
13 #define hifi_AssetServer_h
14 
15 #include <QtCore/QDir>
16 #include <QtCore/QSharedPointer>
17 #include <QtCore/QThreadPool>
18 #include <QRunnable>
19 
20 #include <ThreadedAssignment.h>
21 
22 #include "AssetUtils.h"
23 #include "ReceivedMessage.h"
24 
25 #include "RegisteredMetaTypes.h"
26 
27 using BakeVersion = int;
28 static const BakeVersion INITIAL_BAKE_VERSION = 0;
29 static const BakeVersion NEEDS_BAKING_BAKE_VERSION = -1;
30 
31 enum class BakedAssetType : int {
32  Model = 0,
33  Texture,
34  Script,
35 
36  NUM_ASSET_TYPES,
37  Undefined
38 };
39 
40 // ATTENTION! Do not remove baking versions, and do not reorder them. If you add
41 // a new value, it will immediately become the "current" version.
42 enum class ModelBakeVersion : BakeVersion {
43  Initial = INITIAL_BAKE_VERSION,
44  MetaTextureJson,
45 
46  COUNT
47 };
48 
49 // ATTENTION! See above.
50 enum class TextureBakeVersion : BakeVersion {
51  Initial = INITIAL_BAKE_VERSION,
52  MetaTextureJson,
53 
54  COUNT
55 };
56 
57 // ATTENTION! See above.
58 enum class ScriptBakeVersion : BakeVersion {
59  Initial = INITIAL_BAKE_VERSION,
60  FixEmptyScripts,
61 
62  COUNT
63 };
64 
65 struct AssetMeta {
66  BakeVersion bakeVersion { INITIAL_BAKE_VERSION };
67  bool failedLastBake { false };
68  QString lastBakeErrors;
69  QString redirectTarget;
70 };
71 
72 class BakeAssetTask;
73 
74 class AssetServer : public ThreadedAssignment {
75  Q_OBJECT
76 public:
77  AssetServer(ReceivedMessage& message);
78 
79  void aboutToFinish() override;
80 
81 public slots:
82  void run() override;
83 
84 private slots:
85  void completeSetup();
86 
87  void queueRequests(QSharedPointer<ReceivedMessage> packet, SharedNodePointer senderNode);
88  void handleAssetGetInfo(QSharedPointer<ReceivedMessage> packet, SharedNodePointer senderNode);
89  void handleAssetGet(QSharedPointer<ReceivedMessage> packet, SharedNodePointer senderNode);
90  void handleAssetUpload(QSharedPointer<ReceivedMessage> packetList, SharedNodePointer senderNode);
91  void handleAssetMappingOperation(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
92 
93  void sendStatsPacket() override;
94 
95 private:
96  void replayRequests();
97 
98  void handleGetMappingOperation(ReceivedMessage& message, NLPacketList& replyPacket);
99  void handleGetAllMappingOperation(NLPacketList& replyPacket);
100  void handleSetMappingOperation(ReceivedMessage& message, bool hasWriteAccess, NLPacketList& replyPacket);
101  void handleDeleteMappingsOperation(ReceivedMessage& message, bool hasWriteAccess, NLPacketList& replyPacket);
102  void handleRenameMappingOperation(ReceivedMessage& message, bool hasWriteAccess, NLPacketList& replyPacket);
103  void handleSetBakingEnabledOperation(ReceivedMessage& message, bool hasWriteAccess, NLPacketList& replyPacket);
104 
105  void handleAssetServerBackup(ReceivedMessage& message, NLPacketList& replyPacket);
106  void handleAssetServerRestore(ReceivedMessage& message, NLPacketList& replyPacket);
107 
108  // Mapping file operations must be called from main assignment thread only
109  bool loadMappingsFromFile();
110  bool writeMappingsToFile();
111 
113  bool setMapping(AssetUtils::AssetPath path, AssetUtils::AssetHash hash);
114 
116  bool deleteMappings(const AssetUtils::AssetPathList& paths);
117 
119  bool renameMapping(AssetUtils::AssetPath oldPath, AssetUtils::AssetPath newPath);
120 
121  bool setBakingEnabled(const AssetUtils::AssetPathList& paths, bool enabled);
122 
124  void cleanupUnmappedFiles();
125 
127  void cleanupBakedFilesForDeletedAssets();
128 
129  QString getPathToAssetHash(const AssetUtils::AssetHash& assetHash);
130 
131  std::pair<AssetUtils::BakingStatus, QString> getAssetStatus(const AssetUtils::AssetPath& path, const AssetUtils::AssetHash& hash);
132 
133  void bakeAssets();
134  void maybeBake(const AssetUtils::AssetPath& path, const AssetUtils::AssetHash& hash);
135  void createEmptyMetaFile(const AssetUtils::AssetHash& hash);
136  bool hasMetaFile(const AssetUtils::AssetHash& hash);
137  bool needsToBeBaked(const AssetUtils::AssetPath& path, const AssetUtils::AssetHash& assetHash);
138  void bakeAsset(const AssetUtils::AssetHash& assetHash, const AssetUtils::AssetPath& assetPath, const QString& filePath);
139 
141  void handleCompletedBake(QString originalAssetHash, QString assetPath, QString bakedTempOutputDir);
142  void handleFailedBake(QString originalAssetHash, QString assetPath, QString errors);
143  void handleAbortedBake(QString originalAssetHash, QString assetPath);
144 
146  std::pair<bool, AssetMeta> readMetaFile(AssetUtils::AssetHash hash);
147  bool writeMetaFile(AssetUtils::AssetHash originalAssetHash, const AssetMeta& meta = AssetMeta());
148 
150  void removeBakedPathsForDeletedAsset(AssetUtils::AssetHash originalAssetHash);
151 
152  AssetUtils::Mappings _fileMappings;
153 
154  QDir _resourcesDirectory;
155  QDir _filesDirectory;
156 
158  QThreadPool _transferTaskPool;
159 
160  QHash<AssetUtils::AssetHash, std::shared_ptr<BakeAssetTask>> _pendingBakes;
161  QThreadPool _bakingTaskPool;
162 
163  QMutex _queuedRequestsMutex;
164  bool _isQueueingRequests { true };
165  using RequestQueue = QVector<QPair<QSharedPointer<ReceivedMessage>, SharedNodePointer>>;
166  RequestQueue _queuedRequests;
167 
168  uint64_t _filesizeLimit;
169 };
170 
171 #endif
A generic 3D model displaying geometry loaded from a URL.
Definition: Model.h:84
A simple object wrapper for an OpenGL texture.
Definition: material-networking/src/material-networking/TextureCache.h:39