FileProcessor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <native/FileProcessor/FileProcessor.h>
  9. #include <native/utilities/PlatformConfiguration.h>
  10. #include <QDir>
  11. namespace FileProcessorPrivate
  12. {
  13. bool FinishedScanning(AssetProcessor::AssetScanningStatus status)
  14. {
  15. return status == AssetProcessor::AssetScanningStatus::Completed ||
  16. status == AssetProcessor::AssetScanningStatus::Stopped;
  17. }
  18. QString GenerateUniqueFileKey(AZ::s64 scanFolder, const char* fileName)
  19. {
  20. return QString("%1:%2").arg(scanFolder).arg(fileName);
  21. }
  22. }
  23. namespace AssetProcessor
  24. {
  25. using namespace FileProcessorPrivate;
  26. FileProcessor::FileProcessor(PlatformConfiguration* config)
  27. : m_platformConfig(config)
  28. {
  29. m_connection = AZStd::shared_ptr<AssetDatabaseConnection>(aznew AssetDatabaseConnection());
  30. m_connection->OpenDatabase();
  31. QDir cacheRootDir;
  32. if (!AssetUtilities::ComputeProjectCacheRoot(cacheRootDir))
  33. {
  34. AZ_Error(AssetProcessor::ConsoleChannel, false, "Failed to compute cache root folder");
  35. }
  36. m_normalizedCacheRootPath = AssetUtilities::NormalizeDirectoryPath(cacheRootDir.absolutePath());
  37. }
  38. FileProcessor::~FileProcessor() = default;
  39. void FileProcessor::OnAssetScannerStatusChange(AssetScanningStatus status)
  40. {
  41. //when AssetScanner finished processing, synchronize Files table
  42. if (FileProcessorPrivate::FinishedScanning(status))
  43. {
  44. QMetaObject::invokeMethod(this, "Sync", Qt::QueuedConnection);
  45. }
  46. }
  47. void FileProcessor::AssessFilesFromScanner(QSet<AssetFileInfo> files)
  48. {
  49. for (const AssetFileInfo& file : files)
  50. {
  51. m_filesInAssetScanner.append(file);
  52. }
  53. }
  54. void FileProcessor::AssessFoldersFromScanner(QSet<AssetFileInfo> folders)
  55. {
  56. for (const AssetFileInfo& folder : folders)
  57. {
  58. m_filesInAssetScanner.append(folder);
  59. }
  60. }
  61. void FileProcessor::AssessAddedFile(QString filePath)
  62. {
  63. using namespace AzToolsFramework;
  64. if (m_shutdownSignalled)
  65. {
  66. return;
  67. }
  68. QString relativeFileName;
  69. QString scanFolderPath;
  70. if (!GetRelativePath(filePath, relativeFileName, scanFolderPath))
  71. {
  72. return;
  73. }
  74. const ScanFolderInfo* scanFolderInfo = m_platformConfig->GetScanFolderByPath(scanFolderPath);
  75. if (!scanFolderInfo)
  76. {
  77. AZ_Error(AssetProcessor::ConsoleChannel, false, "Failed to find the scan folder for file %s", filePath.toUtf8().constData());
  78. return;
  79. }
  80. AssetDatabase::FileDatabaseEntry file;
  81. file.m_scanFolderPK = scanFolderInfo->ScanFolderID();
  82. file.m_fileName = relativeFileName.toUtf8().constData();
  83. file.m_isFolder = QFileInfo(filePath).isDir();
  84. bool entryAlreadyExists;
  85. if (m_connection->InsertFile(file, entryAlreadyExists) && !entryAlreadyExists)
  86. {
  87. AssetSystem::FileInfosNotificationMessage message;
  88. message.m_type = AssetSystem::FileInfosNotificationMessage::FileAdded;
  89. message.m_fileID = file.m_fileID;
  90. ConnectionBus::Broadcast(&ConnectionBusTraits::Send, 0, message);
  91. }
  92. if (file.m_isFolder)
  93. {
  94. QDir folder(filePath);
  95. for (const QFileInfo& subFile : folder.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot))
  96. {
  97. AssessAddedFile(subFile.absoluteFilePath());
  98. }
  99. }
  100. }
  101. void FileProcessor::AssessDeletedFile(QString filePath)
  102. {
  103. using namespace AzToolsFramework;
  104. if (m_shutdownSignalled)
  105. {
  106. return;
  107. }
  108. QString relativeFileName;
  109. QString scanFolderPath;
  110. if (!GetRelativePath(filePath, relativeFileName, scanFolderPath))
  111. {
  112. return;
  113. }
  114. const ScanFolderInfo* scanFolderInfo = m_platformConfig->GetScanFolderByPath(scanFolderPath);
  115. if (!scanFolderInfo)
  116. {
  117. AZ_Error(AssetProcessor::ConsoleChannel, false, "Failed to find the scan folder for file %s", filePath.toUtf8().constData());
  118. return;
  119. }
  120. AssetDatabase::FileDatabaseEntry file;
  121. if (m_connection->GetFileByFileNameAndScanFolderId(relativeFileName, scanFolderInfo->ScanFolderID(), file) && DeleteFileRecursive(file))
  122. {
  123. AssetSystem::FileInfosNotificationMessage message;
  124. message.m_type = AssetSystem::FileInfosNotificationMessage::FileRemoved;
  125. message.m_fileID = file.m_fileID;
  126. ConnectionBus::Broadcast(&ConnectionBusTraits::Send, 0, message);
  127. }
  128. }
  129. void FileProcessor::Sync()
  130. {
  131. using namespace AzToolsFramework;
  132. if (m_shutdownSignalled)
  133. {
  134. return;
  135. }
  136. QMap<QString, AZ::s64> filesInDatabase;
  137. //query all current files from Files table
  138. auto filesFunction = [&filesInDatabase](AzToolsFramework::AssetDatabase::FileDatabaseEntry& entry)
  139. {
  140. QString uniqueKey = GenerateUniqueFileKey(entry.m_scanFolderPK, entry.m_fileName.c_str());
  141. filesInDatabase[uniqueKey] = entry.m_fileID;
  142. return true;
  143. };
  144. m_connection->QueryFilesTable(filesFunction);
  145. //first collect all fileIDs in Files table
  146. QSet<AZ::s64> missingFileIDs;
  147. for (AZ::s64 fileID : filesInDatabase.values())
  148. {
  149. missingFileIDs.insert(fileID);
  150. }
  151. AzToolsFramework::AssetDatabase::FileDatabaseEntryContainer filesToInsert;
  152. for (const AssetFileInfo& fileInfo : m_filesInAssetScanner)
  153. {
  154. bool isDir = fileInfo.m_isDirectory;
  155. QString scanFolderPath;
  156. QString relativeFileName;
  157. if (!m_platformConfig->ConvertToRelativePath(fileInfo.m_filePath, relativeFileName, scanFolderPath))
  158. {
  159. AZ_Error(AssetProcessor::ConsoleChannel, false, "Failed to convert full path to relative for file %s", fileInfo.m_filePath.toUtf8().constData());
  160. continue;
  161. }
  162. const ScanFolderInfo* scanFolderInfo = m_platformConfig->GetScanFolderByPath(scanFolderPath);
  163. if (!scanFolderInfo)
  164. {
  165. AZ_Error(AssetProcessor::ConsoleChannel, false, "Failed to find the scan folder for file %s", fileInfo.m_filePath.toUtf8().constData());
  166. continue;
  167. }
  168. AssetDatabase::FileDatabaseEntry file;
  169. file.m_scanFolderPK = scanFolderInfo->ScanFolderID();
  170. file.m_fileName = relativeFileName.toUtf8().constData();
  171. file.m_isFolder = isDir;
  172. file.m_modTime = 0;
  173. //when file is found by AssetScanner, remove it from the "missing" set
  174. QString uniqueKey = GenerateUniqueFileKey(file.m_scanFolderPK, relativeFileName.toUtf8().constData());
  175. if (filesInDatabase.contains(uniqueKey))
  176. {
  177. // found it, its not missing anymore. (Its also already in the db)
  178. missingFileIDs.remove(filesInDatabase[uniqueKey]);
  179. }
  180. else
  181. {
  182. // its a new file we were previously unaware of.
  183. filesToInsert.push_back(AZStd::move(file));
  184. }
  185. }
  186. m_connection->InsertFiles(filesToInsert);
  187. // remove remaining files from the database as they no longer exist on hard drive
  188. for (AZ::s64 fileID : missingFileIDs)
  189. {
  190. m_connection->RemoveFile(fileID);
  191. }
  192. AssetSystem::FileInfosNotificationMessage message;
  193. ConnectionBus::Broadcast(&ConnectionBusTraits::Send, 0, message);
  194. // It's important to clear this out since rescanning will end up filling this up with duplicates otherwise
  195. QList<AssetFileInfo> emptyList;
  196. m_filesInAssetScanner.swap(emptyList);
  197. }
  198. // note that this function normalizes the path and also returns true only if the file is 'relevant'
  199. // meaning something we care about tracking (ignore list/ etc taken into account).
  200. bool FileProcessor::GetRelativePath(QString& filePath, QString& relativeFileName, QString& scanFolderPath) const
  201. {
  202. filePath = AssetUtilities::NormalizeFilePath(filePath);
  203. if (AssetUtilities::IsInCacheFolder(filePath.toUtf8().constData(), m_normalizedCacheRootPath.toUtf8().constData()))
  204. {
  205. // modifies/adds to the cache are irrelevant. Deletions are all we care about
  206. return false;
  207. }
  208. if (m_platformConfig->IsFileExcluded(filePath))
  209. {
  210. return false; // we don't care about this kind of file.
  211. }
  212. if (!m_platformConfig->ConvertToRelativePath(filePath, relativeFileName, scanFolderPath))
  213. {
  214. AZ_Error(AssetProcessor::ConsoleChannel, false, "Failed to convert full path to relative for file %s", filePath.toUtf8().constData());
  215. return false;
  216. }
  217. return true;
  218. }
  219. bool FileProcessor::DeleteFileRecursive(const AzToolsFramework::AssetDatabase::FileDatabaseEntry& file) const
  220. {
  221. using namespace AzToolsFramework;
  222. if (m_shutdownSignalled)
  223. {
  224. return false;
  225. }
  226. if (file.m_isFolder)
  227. {
  228. AssetDatabase::FileDatabaseEntryContainer container;
  229. AZStd::string searchStr = file.m_fileName + AZ_CORRECT_DATABASE_SEPARATOR;
  230. m_connection->GetFilesLikeFileNameScanFolderId(
  231. searchStr.c_str(),
  232. AssetDatabaseConnection::LikeType::StartsWith,
  233. file.m_scanFolderPK,
  234. container);
  235. for (const auto& subFile : container)
  236. {
  237. DeleteFileRecursive(subFile);
  238. }
  239. }
  240. return m_connection->RemoveFile(file.m_fileID);
  241. }
  242. void FileProcessor::QuitRequested()
  243. {
  244. m_shutdownSignalled = true;
  245. Q_EMIT ReadyToQuit(this);
  246. }
  247. } // namespace AssetProcessor
  248. #include "native/FileProcessor/moc_FileProcessor.cpp"