[AssetProcessor] Refactor the FileWatcher to use only one watch thread
This change reworks the AssetProcessor's FileWatcher so that it only uses one thread. This is motivated by getting better support for inotify on Linux. The previous architecture required calling `inotify_init` once for each directory that was being watched, and using separate inotify instances for each watched tree. In addition, having separate threads per watched tree is not necessary, and just consumes system resources. Each platform supports watching multiple directories with the same platform-specific watcher API, so each platform has been updated accordingly. The interface to the FileWatcher class is greatly simplified. Previously, it supported client-supplied filtering of the paths that would generate notifications. This was done by subclassing `FolderWatchBase` and implementing `OnFileChange`. However, only one filter was ever used, so that filter is now hard-coded in the FileWatcher class, and the classes driving the old filtering mechanism are removed. Users of the interface now have a much easier time, they just call `AddFolderWatch` with the path to watch, and only have to connect to one set of signals, instead of separate signals per watched directory. Signed-off-by: Chris Burel <burelc@amazon.com>
This commit is contained in:
@@ -6,154 +6,122 @@
|
||||
*
|
||||
*/
|
||||
#include "FileWatcher.h"
|
||||
#include "AzCore/std/containers/vector.h"
|
||||
#include <native/assetprocessor.h>
|
||||
#include <native/FileWatcher/FileWatcher_platform.h>
|
||||
#include <QFileInfo>
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
/// FolderWatchRoot
|
||||
void FolderRootWatch::ProcessNewFileEvent(const QString& file)
|
||||
//! IsSubfolder(folderA, folderB)
|
||||
//! returns whether folderA is a subfolder of folderB
|
||||
//! assumptions: absolute paths, case insensitive
|
||||
static bool IsSubfolder(const QString& folderA, const QString& folderB)
|
||||
{
|
||||
FileChangeInfo info;
|
||||
info.m_action = FileAction::FileAction_Added;
|
||||
info.m_filePath = file;
|
||||
const bool invoked = QMetaObject::invokeMethod(m_fileWatcher, "AnyFileChange", Qt::QueuedConnection, Q_ARG(FileChangeInfo, info));
|
||||
Q_ASSERT(invoked);
|
||||
}
|
||||
// lets avoid allocating or messing with memory - this is a MAJOR hotspot as it is called for any file change even in the cache!
|
||||
int sizeB = folderB.length();
|
||||
int sizeA = folderA.length();
|
||||
|
||||
void FolderRootWatch::ProcessDeleteFileEvent(const QString& file)
|
||||
{
|
||||
FileChangeInfo info;
|
||||
info.m_action = FileAction::FileAction_Removed;
|
||||
info.m_filePath = file;
|
||||
const bool invoked = QMetaObject::invokeMethod(m_fileWatcher, "AnyFileChange", Qt::QueuedConnection, Q_ARG(FileChangeInfo, info));
|
||||
Q_ASSERT(invoked);
|
||||
}
|
||||
if (sizeA <= sizeB)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void FolderRootWatch::ProcessModifyFileEvent(const QString& file)
|
||||
{
|
||||
FileChangeInfo info;
|
||||
info.m_action = FileAction::FileAction_Modified;
|
||||
info.m_filePath = file;
|
||||
const bool invoked = QMetaObject::invokeMethod(m_fileWatcher, "AnyFileChange", Qt::QueuedConnection, Q_ARG(FileChangeInfo, info));
|
||||
Q_ASSERT(invoked);
|
||||
QChar slash1 = QChar('\\');
|
||||
QChar slash2 = QChar('/');
|
||||
int posA = 0;
|
||||
|
||||
// A is going to be the longer one, so use B:
|
||||
for (int idx = 0; idx < sizeB; ++idx)
|
||||
{
|
||||
QChar charAtA = folderA.at(posA);
|
||||
QChar charAtB = folderB.at(idx);
|
||||
|
||||
if ((charAtB == slash1) || (charAtB == slash2))
|
||||
{
|
||||
if ((charAtA != slash1) && (charAtA != slash2))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
++posA;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (charAtA.toLower() != charAtB.toLower())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
++posA;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
/// FileWatcher
|
||||
FileWatcher::FileWatcher()
|
||||
: m_nextHandle(0)
|
||||
: m_platformImpl(AZStd::make_unique<PlatformImplementation>())
|
||||
{
|
||||
qRegisterMetaType<FileChangeInfo>("FileChangeInfo");
|
||||
auto makeFilter = [this](auto signal)
|
||||
{
|
||||
return [this, signal](QString path)
|
||||
{
|
||||
const auto foundWatchRoot = AZStd::find_if(begin(m_folderWatchRoots), end(m_folderWatchRoots), [path](const WatchRoot& watchRoot)
|
||||
{
|
||||
return Filter(path, watchRoot);
|
||||
});
|
||||
if (foundWatchRoot == end(m_folderWatchRoots))
|
||||
{
|
||||
return;
|
||||
}
|
||||
AZStd::invoke(signal, this, path);
|
||||
};
|
||||
};
|
||||
|
||||
// The rawFileAdded signals are emitted by the watcher thread. Use a queued
|
||||
// connection so that the consumers of the notification process the
|
||||
// notification on the main thread.
|
||||
connect(this, &FileWatcher::rawFileAdded, this, makeFilter(&FileWatcher::fileAdded), Qt::QueuedConnection);
|
||||
connect(this, &FileWatcher::rawFileRemoved, this, makeFilter(&FileWatcher::fileRemoved), Qt::QueuedConnection);
|
||||
connect(this, &FileWatcher::rawFileModified, this, makeFilter(&FileWatcher::fileModified), Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
FileWatcher::~FileWatcher()
|
||||
{
|
||||
disconnect();
|
||||
StopWatching();
|
||||
}
|
||||
|
||||
int FileWatcher::AddFolderWatch(FolderWatchBase* pFolderWatch, bool recursive)
|
||||
void FileWatcher::AddFolderWatch(QString directory, bool recursive)
|
||||
{
|
||||
if (!pFolderWatch)
|
||||
// Search for an already monitored root that is a parent of `directory`,
|
||||
// that is already watching subdirectories recursively
|
||||
const auto found = AZStd::find_if(begin(m_folderWatchRoots), end(m_folderWatchRoots), [directory](const WatchRoot& root)
|
||||
{
|
||||
return -1;
|
||||
return root.m_recursive && IsSubfolder(directory, root.m_directory);
|
||||
});
|
||||
|
||||
if (found != end(m_folderWatchRoots))
|
||||
{
|
||||
// This directory is already watched
|
||||
return;
|
||||
}
|
||||
|
||||
FolderRootWatch* pFolderRootWatch = nullptr;
|
||||
//create a new root and start listening for changes
|
||||
m_folderWatchRoots.push_back({directory, recursive});
|
||||
|
||||
//see if this a sub folder of an already watched root
|
||||
for (auto rootsIter = m_folderWatchRoots.begin(); !pFolderRootWatch && rootsIter != m_folderWatchRoots.end(); ++rootsIter)
|
||||
//since we created a new root, see if the new root is a super folder
|
||||
//of other roots, if it is then then fold those roots into the new super root
|
||||
if (recursive)
|
||||
{
|
||||
if (FolderWatchBase::IsSubfolder(pFolderWatch->m_folder, (*rootsIter)->m_root))
|
||||
AZStd::erase_if(m_folderWatchRoots, [directory](const WatchRoot& root)
|
||||
{
|
||||
pFolderRootWatch = *rootsIter;
|
||||
}
|
||||
return IsSubfolder(root.m_directory, directory);
|
||||
});
|
||||
}
|
||||
|
||||
bool bCreatedNewRoot = false;
|
||||
//if its not a sub folder
|
||||
if (!pFolderRootWatch)
|
||||
{
|
||||
//create a new root and start listening for changes
|
||||
pFolderRootWatch = new FolderRootWatch(pFolderWatch->m_folder, recursive);
|
||||
|
||||
//make sure the folder watcher(s) get deleted before this
|
||||
pFolderRootWatch->setParent(this);
|
||||
bCreatedNewRoot = true;
|
||||
}
|
||||
|
||||
pFolderRootWatch->m_fileWatcher = this;
|
||||
QObject::connect(this, &FileWatcher::AnyFileChange, pFolderWatch, &FolderWatchBase::OnAnyFileChange);
|
||||
|
||||
if (bCreatedNewRoot)
|
||||
{
|
||||
if (m_startedWatching)
|
||||
{
|
||||
pFolderRootWatch->Start();
|
||||
}
|
||||
|
||||
//since we created a new root, see if the new root is a super folder
|
||||
//of other roots, if it is then then fold those roots into the new super root
|
||||
for (auto rootsIter = m_folderWatchRoots.begin(); rootsIter != m_folderWatchRoots.end(); )
|
||||
{
|
||||
if (pFolderWatch->m_watchSubtree && FolderWatchBase::IsSubfolder((*rootsIter)->m_root, pFolderWatch->m_folder))
|
||||
{
|
||||
//union the sub folder map over to the new root
|
||||
pFolderRootWatch->m_subFolderWatchesMap.insert((*rootsIter)->m_subFolderWatchesMap);
|
||||
|
||||
//clear the old root sub folders map so they don't get deleted when we
|
||||
//delete the old root as they are now pointed to by the new root
|
||||
(*rootsIter)->m_subFolderWatchesMap.clear();
|
||||
|
||||
//delete the empty old root, deleting a root will call Stop()
|
||||
//automatically which kills the thread
|
||||
delete *rootsIter;
|
||||
|
||||
//remove the old root pointer form the watched list
|
||||
rootsIter = m_folderWatchRoots.erase(rootsIter);
|
||||
}
|
||||
else
|
||||
{
|
||||
++rootsIter;
|
||||
}
|
||||
}
|
||||
|
||||
//add the new root to the watched roots
|
||||
m_folderWatchRoots.push_back(pFolderRootWatch);
|
||||
}
|
||||
|
||||
//add to the root
|
||||
pFolderRootWatch->m_subFolderWatchesMap.insert(m_nextHandle, pFolderWatch);
|
||||
|
||||
m_nextHandle++;
|
||||
|
||||
return m_nextHandle - 1;
|
||||
}
|
||||
|
||||
void FileWatcher::RemoveFolderWatch(int handle)
|
||||
void FileWatcher::ClearFolderWatches()
|
||||
{
|
||||
for (auto rootsIter = m_folderWatchRoots.begin(); rootsIter != m_folderWatchRoots.end(); )
|
||||
{
|
||||
//find an element by the handle
|
||||
auto foundIter = (*rootsIter)->m_subFolderWatchesMap.find(handle);
|
||||
if (foundIter != (*rootsIter)->m_subFolderWatchesMap.end())
|
||||
{
|
||||
//remove the element
|
||||
(*rootsIter)->m_subFolderWatchesMap.erase(foundIter);
|
||||
|
||||
//we removed a folder watch, if it's empty then there is no reason to keep watching it.
|
||||
if ((*rootsIter)->m_subFolderWatchesMap.empty())
|
||||
{
|
||||
delete(*rootsIter);
|
||||
rootsIter = m_folderWatchRoots.erase(rootsIter);
|
||||
}
|
||||
else
|
||||
{
|
||||
++rootsIter;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
++rootsIter;
|
||||
}
|
||||
}
|
||||
m_folderWatchRoots.clear();
|
||||
}
|
||||
|
||||
void FileWatcher::StartWatching()
|
||||
@@ -164,12 +132,18 @@ void FileWatcher::StartWatching()
|
||||
return;
|
||||
}
|
||||
|
||||
for (FolderRootWatch* root : m_folderWatchRoots)
|
||||
if (PlatformStart())
|
||||
{
|
||||
root->Start();
|
||||
m_thread = AZStd::thread({/*.name=*/ "AssetProcessor FileWatcher thread"}, [this]{
|
||||
WatchFolderLoop();
|
||||
});
|
||||
AZ_TracePrintf(AssetProcessor::ConsoleChannel, "File Change Monitoring started.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
AZ_TracePrintf(AssetProcessor::ConsoleChannel, "File Change Monitoring failed to start.\n");
|
||||
}
|
||||
|
||||
AZ_TracePrintf(AssetProcessor::ConsoleChannel, "File Change Monitoring started.\n");
|
||||
m_startedWatching = true;
|
||||
}
|
||||
|
||||
@@ -177,17 +151,35 @@ void FileWatcher::StopWatching()
|
||||
{
|
||||
if (!m_startedWatching)
|
||||
{
|
||||
AZ_Warning("FileWatcher", false, "StartWatching() called when is not watching for file changes.");
|
||||
AZ_Warning("FileWatcher", false, "StopWatching() called when is not watching for file changes.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (FolderRootWatch* root : m_folderWatchRoots)
|
||||
{
|
||||
root->Stop();
|
||||
}
|
||||
PlatformStop();
|
||||
|
||||
m_startedWatching = false;
|
||||
}
|
||||
|
||||
#include "native/FileWatcher/moc_FileWatcher.cpp"
|
||||
#include "native/FileWatcher/moc_FileWatcherAPI.cpp"
|
||||
bool FileWatcher::Filter(QString path, const WatchRoot& watchRoot)
|
||||
{
|
||||
if (!IsSubfolder(path, watchRoot.m_directory))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!watchRoot.m_recursive)
|
||||
{
|
||||
// filter out subtrees too.
|
||||
QStringRef subRef = path.rightRef(path.length() - watchRoot.m_directory.length());
|
||||
if ((subRef.indexOf('/') != -1) || (subRef.indexOf('\\') != -1))
|
||||
{
|
||||
return false; // filter this out.
|
||||
}
|
||||
|
||||
// we don't care about subdirs. IsDir is more expensive so we do it after the above filter.
|
||||
if (QFileInfo(path).isDir())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -5,63 +5,21 @@
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
#ifndef FILEWATCHER_COMPONENT_H
|
||||
#define FILEWATCHER_COMPONENT_H
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(Q_MOC_RUN)
|
||||
#include "FileWatcherAPI.h"
|
||||
|
||||
#include <AzCore/std/smart_ptr/unique_ptr.h>
|
||||
#include <AzCore/std/containers/vector.h>
|
||||
#include <AzCore/std/parallel/atomic.h>
|
||||
#include <AzCore/std/parallel/thread.h>
|
||||
#include <QMap>
|
||||
#include <QVector>
|
||||
#include <QString>
|
||||
#include <QObject>
|
||||
|
||||
#include <thread>
|
||||
#endif
|
||||
|
||||
class FileWatcher;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
//! FolderRootWatch
|
||||
/*! Class used for holding a point in the files system from which file changes are tracked.
|
||||
* */
|
||||
class FolderRootWatch
|
||||
: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
friend class FileWatcher;
|
||||
public:
|
||||
FolderRootWatch(const QString rootFolder, bool recursive = true);
|
||||
virtual ~FolderRootWatch();
|
||||
|
||||
void ProcessNewFileEvent(const QString& file);
|
||||
void ProcessDeleteFileEvent(const QString& file);
|
||||
void ProcessModifyFileEvent(const QString& file);
|
||||
void ProcessRenameFileEvent(const QString& fileOld, const QString& fileNew);
|
||||
|
||||
public Q_SLOTS:
|
||||
bool Start();
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
void WatchFolderLoop();
|
||||
|
||||
private:
|
||||
std::thread m_thread;
|
||||
QString m_root;
|
||||
QMap<int, FolderWatchBase*> m_subFolderWatchesMap;
|
||||
volatile bool m_shutdownThreadSignal;
|
||||
FileWatcher* m_fileWatcher;
|
||||
bool m_recursive;
|
||||
|
||||
// Can't use unique_ptr because this is a QObject and Qt's magic sauce is
|
||||
// unable to determine the size of the unique_ptr and so fails to compile
|
||||
struct PlatformImplementation;
|
||||
PlatformImplementation* m_platformImpl;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
//! FileWatcher
|
||||
/*! Class that handles creation and deletion of FolderRootWatches based on
|
||||
@@ -74,23 +32,47 @@ class FileWatcher
|
||||
|
||||
public:
|
||||
FileWatcher();
|
||||
virtual ~FileWatcher();
|
||||
~FileWatcher() override;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
virtual int AddFolderWatch(FolderWatchBase* pFolderWatch, bool recursive = true);
|
||||
virtual void RemoveFolderWatch(int handle);
|
||||
void AddFolderWatch(QString directory, bool recursive = true);
|
||||
void ClearFolderWatches();
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
void StartWatching();
|
||||
void StopWatching();
|
||||
|
||||
Q_SIGNALS:
|
||||
void AnyFileChange(FileChangeInfo info);
|
||||
// These signals are emitted when a file under a watched path changes
|
||||
void fileAdded(QString filePath);
|
||||
void fileRemoved(QString filePath);
|
||||
void fileModified(QString filePath);
|
||||
|
||||
// These signals are emitted by the platform implementations when files
|
||||
// change. Some platforms' file watch APIs do not support non-recursive
|
||||
// watches, so the signals are filtered before being forwarded to the
|
||||
// non-"raw" fileAdded/Removed/Modified signals above.
|
||||
void rawFileAdded(QString filePath, QPrivateSignal);
|
||||
void rawFileRemoved(QString filePath, QPrivateSignal);
|
||||
void rawFileModified(QString filePath, QPrivateSignal);
|
||||
|
||||
private:
|
||||
int m_nextHandle;
|
||||
AZStd::vector<FolderRootWatch*> m_folderWatchRoots;
|
||||
bool m_startedWatching = false;
|
||||
};
|
||||
bool PlatformStart();
|
||||
void PlatformStop();
|
||||
void WatchFolderLoop();
|
||||
|
||||
#endif//FILEWATCHER_COMPONENT_H
|
||||
class PlatformImplementation;
|
||||
friend class PlatformImplementation;
|
||||
struct WatchRoot
|
||||
{
|
||||
QString m_directory;
|
||||
bool m_recursive;
|
||||
};
|
||||
static bool Filter(QString path, const WatchRoot& watchRoot);
|
||||
|
||||
AZStd::unique_ptr<PlatformImplementation> m_platformImpl;
|
||||
AZStd::vector<WatchRoot> m_folderWatchRoots;
|
||||
AZStd::thread m_thread;
|
||||
bool m_startedWatching = false;
|
||||
AZStd::atomic_bool m_shutdownThreadSignal = false;
|
||||
};
|
||||
|
||||
@@ -1,222 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
#ifndef FILEWATCHERAPI_H
|
||||
#define FILEWATCHERAPI_H
|
||||
|
||||
#include <QString>
|
||||
#include <QObject>
|
||||
#include <QDir>
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
//! FileAction
|
||||
/*! Enum for which file changes are tracked.
|
||||
* */
|
||||
enum FileAction
|
||||
{
|
||||
FileAction_None = 0x00,
|
||||
FileAction_Added = 0x01,
|
||||
FileAction_Removed = 0x02,
|
||||
FileAction_Modified = 0x04,
|
||||
FileAction_Any = 0xFF,
|
||||
};
|
||||
inline FileAction operator | (FileAction a, FileAction b)
|
||||
{
|
||||
return static_cast<FileAction>(static_cast<int>(a) | static_cast<int>(b));
|
||||
}
|
||||
inline FileAction operator & (FileAction a, FileAction b)
|
||||
{
|
||||
return static_cast<FileAction>(static_cast<int>(a) & static_cast<int>(b));
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
//! FileChangeInfo
|
||||
/*! Struct for passing along information about file changes.
|
||||
* */
|
||||
struct FileChangeInfo
|
||||
{
|
||||
FileChangeInfo()
|
||||
: m_action(FileAction::FileAction_None)
|
||||
{}
|
||||
FileChangeInfo(const FileChangeInfo& rhs)
|
||||
: m_action(rhs.m_action)
|
||||
, m_filePath(rhs.m_filePath)
|
||||
, m_filePathOld(rhs.m_filePathOld)
|
||||
{
|
||||
}
|
||||
|
||||
FileAction m_action;
|
||||
QString m_filePath;
|
||||
QString m_filePathOld;
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(FileChangeInfo)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
//! FolderWatchBase
|
||||
/*! Class for filtering file changes generated from a root watch. Define your own
|
||||
*! custom filtering by deriving from this base class and implement your own
|
||||
*! custom code for what to do when receiving a file change notification.
|
||||
* */
|
||||
class FolderWatchBase
|
||||
: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FolderWatchBase(const QString strFolder, bool bWatchSubtree = true, FileAction fileAction = FileAction::FileAction_Any)
|
||||
: m_folder(strFolder)
|
||||
, m_watchSubtree(bWatchSubtree)
|
||||
, m_fileAction(fileAction)
|
||||
{
|
||||
m_folder = QDir::toNativeSeparators(QDir::cleanPath(m_folder) + "/");
|
||||
}
|
||||
|
||||
//! IsSubfolder(folderA, folderB)
|
||||
//! returns whether folderA is a subfolder of folderB
|
||||
//! assumptions: absolute paths, case insensitive
|
||||
static bool IsSubfolder(const QString& folderA, const QString& folderB)
|
||||
{
|
||||
// lets avoid allocating or messing with memory - this is a MAJOR hotspot as it is called for any file change even in the cache!
|
||||
int sizeB = folderB.length();
|
||||
int sizeA = folderA.length();
|
||||
|
||||
if (sizeA <= sizeB)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
QChar slash1 = QChar('\\');
|
||||
QChar slash2 = QChar('/');
|
||||
int posA = 0;
|
||||
|
||||
// A is going to be the longer one, so use B:
|
||||
for (int idx = 0; idx < sizeB; ++idx)
|
||||
{
|
||||
QChar charAtA = folderA.at(posA);
|
||||
QChar charAtB = folderB.at(idx);
|
||||
|
||||
if ((charAtB == slash1) || (charAtB == slash2))
|
||||
{
|
||||
if ((charAtA != slash1) && (charAtA != slash2))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
++posA;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (charAtA.toLower() != charAtB.toLower())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
++posA;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QString m_folder;
|
||||
bool m_watchSubtree;
|
||||
FileAction m_fileAction;
|
||||
|
||||
public Q_SLOTS:
|
||||
void OnAnyFileChange(FileChangeInfo info)
|
||||
{
|
||||
//if they set a file action then respect it by rejecting non matching file actions
|
||||
if (info.m_action & m_fileAction)
|
||||
{
|
||||
//is the file is in the folder or subtree (if specified) then call OnFileChange
|
||||
|
||||
if (FolderWatchBase::IsSubfolder(info.m_filePath, m_folder))
|
||||
{
|
||||
OnFileChange(info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual void OnFileChange(const FileChangeInfo& info) = 0;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
//! FolderWatchCallbackEx
|
||||
/*! Class implements a more complex filtering that can optionally filter for file
|
||||
*! extension and call different callback for different kinds of file changes
|
||||
*! generated from a root watch.
|
||||
*! Notes:
|
||||
*! - empty extension "" catches all file changes
|
||||
*! - extension should not include the leading "."
|
||||
* */
|
||||
class FolderWatchCallbackEx
|
||||
: public FolderWatchBase
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FolderWatchCallbackEx(const QString strFolder, const QString extension, bool bWatchSubtree)
|
||||
: FolderWatchBase(strFolder, bWatchSubtree)
|
||||
, m_extension(extension)
|
||||
{
|
||||
}
|
||||
|
||||
QString m_extension;
|
||||
|
||||
//on file change call the change callback if passes extension then route
|
||||
//to specific file action type callback
|
||||
virtual void OnFileChange(const FileChangeInfo& info)
|
||||
{
|
||||
//if they set an extension to watch for only let matching extensions through
|
||||
QFileInfo fileInfo(info.m_filePath);
|
||||
|
||||
if (!m_watchSubtree)
|
||||
{
|
||||
// filter out subtrees too.
|
||||
QStringRef subRef = info.m_filePath.rightRef(info.m_filePath.length() - m_folder.length());
|
||||
if ((subRef.indexOf('/') != -1) || (subRef.indexOf('\\') != -1))
|
||||
{
|
||||
return; // filter this out.
|
||||
}
|
||||
|
||||
// we don't care about subdirs. IsDir is more expensive so we do it after the above filter.
|
||||
if (fileInfo.isDir())
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_extension.isEmpty() || fileInfo.completeSuffix().compare(m_extension, Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
if (info.m_action & FileAction::FileAction_Any)
|
||||
{
|
||||
Q_EMIT fileChange(info);
|
||||
}
|
||||
|
||||
if (info.m_action & FileAction::FileAction_Added)
|
||||
{
|
||||
Q_EMIT fileAdded(info.m_filePath);
|
||||
}
|
||||
|
||||
if (info.m_action & FileAction::FileAction_Removed)
|
||||
{
|
||||
Q_EMIT fileRemoved(info.m_filePath);
|
||||
}
|
||||
|
||||
if (info.m_action & FileAction::FileAction_Modified)
|
||||
{
|
||||
Q_EMIT fileModified(info.m_filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Q_SIGNALS:
|
||||
void fileChange(FileChangeInfo info);
|
||||
void fileAdded(QString filePath);
|
||||
void fileRemoved(QString filePath);
|
||||
void fileModified(QString filePath);
|
||||
};
|
||||
|
||||
#endif//FILEWATCHERAPI_H
|
||||
@@ -24,14 +24,12 @@ void FileWatcherUnitTestRunner::StartTest()
|
||||
|
||||
FileWatcher fileWatcher;
|
||||
|
||||
FolderWatchCallbackEx folderWatch(tempPath, "", true);
|
||||
|
||||
fileWatcher.AddFolderWatch(&folderWatch);
|
||||
fileWatcher.AddFolderWatch(tempPath);
|
||||
fileWatcher.StartWatching();
|
||||
|
||||
{ // test a single file create/write
|
||||
bool foundFile = false;
|
||||
auto connection = QObject::connect(&folderWatch, &FolderWatchCallbackEx::fileAdded, this, [&](QString filename)
|
||||
auto connection = QObject::connect(&fileWatcher, &FileWatcher::fileAdded, this, [&](QString filename)
|
||||
{
|
||||
AZ_TracePrintf(AssetProcessor::DebugChannel, "Single file test Found asset: %s.\n", filename.toUtf8().data());
|
||||
foundFile = true;
|
||||
@@ -66,7 +64,7 @@ void FileWatcherUnitTestRunner::StartTest()
|
||||
const unsigned long maxFiles = 10000;
|
||||
QSet<QString> outstandingFiles;
|
||||
|
||||
auto connection = QObject::connect(&folderWatch, &FolderWatchCallbackEx::fileAdded, this, [&](QString filename)
|
||||
auto connection = QObject::connect(&fileWatcher, &FileWatcher::fileAdded, this, [&](QString filename)
|
||||
{
|
||||
outstandingFiles.remove(filename);
|
||||
});
|
||||
@@ -122,7 +120,7 @@ void FileWatcherUnitTestRunner::StartTest()
|
||||
|
||||
{ // test deletion
|
||||
bool foundFile = false;
|
||||
auto connection = QObject::connect(&folderWatch, &FolderWatchCallbackEx::fileRemoved, this, [&](QString filename)
|
||||
auto connection = QObject::connect(&fileWatcher, &FileWatcher::fileRemoved, this, [&](QString filename)
|
||||
{
|
||||
AZ_TracePrintf(AssetProcessor::DebugChannel, "Deleted asset: %s...\n", filename.toUtf8().data());
|
||||
foundFile = true;
|
||||
@@ -155,7 +153,7 @@ void FileWatcherUnitTestRunner::StartTest()
|
||||
{
|
||||
bool fileAddCalled = false;
|
||||
QString fileAddName;
|
||||
auto connectionAdd = QObject::connect(&folderWatch, &FolderWatchCallbackEx::fileAdded, this, [&](QString filename)
|
||||
auto connectionAdd = QObject::connect(&fileWatcher, &FileWatcher::fileAdded, this, [&](QString filename)
|
||||
{
|
||||
fileAddCalled = true;
|
||||
fileAddName = filename;
|
||||
@@ -163,7 +161,7 @@ void FileWatcherUnitTestRunner::StartTest()
|
||||
|
||||
bool fileRemoveCalled = false;
|
||||
QString fileRemoveName;
|
||||
auto connectionRemove = QObject::connect(&folderWatch, &FolderWatchCallbackEx::fileRemoved, this, [&](QString filename)
|
||||
auto connectionRemove = QObject::connect(&fileWatcher, &FileWatcher::fileRemoved, this, [&](QString filename)
|
||||
{
|
||||
fileRemoveCalled = true;
|
||||
fileRemoveName = filename;
|
||||
@@ -171,7 +169,7 @@ void FileWatcherUnitTestRunner::StartTest()
|
||||
|
||||
QStringList fileModifiedNames;
|
||||
bool fileModifiedCalled = false;
|
||||
auto connectionModified = QObject::connect(&folderWatch, &FolderWatchCallbackEx::fileModified, this, [&](QString filename)
|
||||
auto connectionModified = QObject::connect(&fileWatcher, &FileWatcher::fileModified, this, [&](QString filename)
|
||||
{
|
||||
fileModifiedCalled = true;
|
||||
fileModifiedNames.append(filename);
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
#include "native/assetprocessor.h"
|
||||
#endif
|
||||
|
||||
class FolderWatchCallbackEx;
|
||||
class QCoreApplication;
|
||||
|
||||
namespace AZ
|
||||
|
||||
@@ -433,63 +433,77 @@ void ApplicationManagerBase::DestroyPlatformConfiguration()
|
||||
|
||||
void ApplicationManagerBase::InitFileMonitor()
|
||||
{
|
||||
m_folderWatches.reserve(m_platformConfiguration->GetScanFolderCount());
|
||||
m_watchHandles.reserve(m_platformConfiguration->GetScanFolderCount());
|
||||
for (int folderIdx = 0; folderIdx < m_platformConfiguration->GetScanFolderCount(); ++folderIdx)
|
||||
{
|
||||
const AssetProcessor::ScanFolderInfo& info = m_platformConfiguration->GetScanFolderAt(folderIdx);
|
||||
|
||||
FolderWatchCallbackEx* newFolderWatch = new FolderWatchCallbackEx(info.ScanPath(), "", info.RecurseSubFolders());
|
||||
// hook folder watcher to assess files on add/modify
|
||||
// relevant files will be sent to resource compiler
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileAdded,
|
||||
m_assetProcessorManager, &AssetProcessor::AssetProcessorManager::AssessAddedFile);
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileModified,
|
||||
m_assetProcessorManager, &AssetProcessor::AssetProcessorManager::AssessModifiedFile);
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileRemoved,
|
||||
m_assetProcessorManager, &AssetProcessor::AssetProcessorManager::AssessDeletedFile);
|
||||
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileAdded, [this](QString path) { m_fileStateCache->AddFile(path); });
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileModified, [this](QString path) { m_fileStateCache->UpdateFile(path); });
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileRemoved, [this](QString path) { m_fileStateCache->RemoveFile(path); });
|
||||
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileAdded, [](QString path) { AZ::Interface<AssetProcessor::ExcludedFolderCacheInterface>::Get()->FileAdded(path); });
|
||||
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileAdded,
|
||||
m_fileProcessor.get(), &AssetProcessor::FileProcessor::AssessAddedFile);
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileRemoved,
|
||||
m_fileProcessor.get(), &AssetProcessor::FileProcessor::AssessDeletedFile);
|
||||
|
||||
m_folderWatches.push_back(AZStd::unique_ptr<FolderWatchCallbackEx>(newFolderWatch));
|
||||
m_watchHandles.push_back(m_fileWatcher.AddFolderWatch(newFolderWatch, info.RecurseSubFolders()));
|
||||
m_fileWatcher.AddFolderWatch(info.ScanPath(), info.RecurseSubFolders());
|
||||
}
|
||||
|
||||
// also hookup monitoring for the cache (output directory)
|
||||
QDir cacheRoot;
|
||||
if (AssetUtilities::ComputeProjectCacheRoot(cacheRoot))
|
||||
{
|
||||
FolderWatchCallbackEx* newFolderWatch = new FolderWatchCallbackEx(cacheRoot.absolutePath(), "", true);
|
||||
m_fileWatcher.AddFolderWatch(cacheRoot.absolutePath(), true);
|
||||
}
|
||||
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileAdded, [this](QString path) { m_fileStateCache->AddFile(path); });
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileModified, [this](QString path) { m_fileStateCache->UpdateFile(path); });
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileRemoved, [this](QString path) { m_fileStateCache->RemoveFile(path); });
|
||||
if (m_platformConfiguration->GetScanFolderCount() || !cacheRoot.path().isEmpty())
|
||||
{
|
||||
const auto cachePath = QDir::toNativeSeparators(cacheRoot.absolutePath());
|
||||
|
||||
// we only care about cache root deletions.
|
||||
QObject::connect(newFolderWatch, &FolderWatchCallbackEx::fileRemoved,
|
||||
m_assetProcessorManager, &AssetProcessor::AssetProcessorManager::AssessDeletedFile);
|
||||
const auto OnFileAdded = [this, cachePath](QString path)
|
||||
{
|
||||
const bool isCacheRoot = path.startsWith(cachePath);
|
||||
if (isCacheRoot)
|
||||
{
|
||||
m_fileStateCache->AddFile(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_assetProcessorManager->AssessAddedFile(path);
|
||||
m_fileStateCache->AddFile(path);
|
||||
AZ::Interface<AssetProcessor::ExcludedFolderCacheInterface>::Get()->FileAdded(path);
|
||||
m_fileProcessor->AssetProcessor::FileProcessor::AssessAddedFile(path);
|
||||
}
|
||||
};
|
||||
|
||||
m_folderWatches.push_back(AZStd::unique_ptr<FolderWatchCallbackEx>(newFolderWatch));
|
||||
m_watchHandles.push_back(m_fileWatcher.AddFolderWatch(newFolderWatch));
|
||||
const auto OnFileModified = [this, cachePath](QString path)
|
||||
{
|
||||
const bool isCacheRoot = path.startsWith(cachePath);
|
||||
if (isCacheRoot)
|
||||
{
|
||||
m_assetProcessorManager->AssessModifiedFile(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_assetProcessorManager->AssessModifiedFile(path);
|
||||
m_fileStateCache->UpdateFile(path);
|
||||
}
|
||||
};
|
||||
|
||||
const auto OnFileRemoved = [this, cachePath](QString path)
|
||||
{
|
||||
const bool isCacheRoot = path.startsWith(cachePath);
|
||||
if (isCacheRoot)
|
||||
{
|
||||
m_fileStateCache->RemoveFile(path);
|
||||
m_assetProcessorManager->AssessDeletedFile(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_assetProcessorManager->AssessDeletedFile(path);
|
||||
m_fileStateCache->RemoveFile(path);
|
||||
m_fileProcessor->AssessDeletedFile(path);
|
||||
}
|
||||
};
|
||||
|
||||
connect(&m_fileWatcher, &FileWatcher::fileAdded, OnFileAdded);
|
||||
connect(&m_fileWatcher, &FileWatcher::fileModified, OnFileModified);
|
||||
connect(&m_fileWatcher, &FileWatcher::fileRemoved, OnFileRemoved);
|
||||
}
|
||||
}
|
||||
|
||||
void ApplicationManagerBase::DestroyFileMonitor()
|
||||
{
|
||||
for (int watchHandle : m_watchHandles)
|
||||
{
|
||||
m_fileWatcher.RemoveFolderWatch(watchHandle);
|
||||
}
|
||||
m_folderWatches.resize(0);
|
||||
m_fileWatcher.ClearFolderWatches();
|
||||
}
|
||||
|
||||
void ApplicationManagerBase::DestroyApplicationServer()
|
||||
@@ -798,8 +812,6 @@ ApplicationManager::BeforeRunStatus ApplicationManagerBase::BeforeRun()
|
||||
qRegisterMetaType<AzFramework::AssetSystem::AssetStatus>("AzFramework::AssetSystem::AssetStatus");
|
||||
qRegisterMetaType<AzFramework::AssetSystem::AssetStatus>("AssetStatus");
|
||||
|
||||
qRegisterMetaType<FileChangeInfo>("FileChangeInfo");
|
||||
|
||||
qRegisterMetaType<AssetProcessor::AssetScanningStatus>("AssetScanningStatus");
|
||||
|
||||
qRegisterMetaType<AssetProcessor::NetworkRequestID>("NetworkRequestID");
|
||||
|
||||
@@ -47,7 +47,6 @@ namespace AssetProcessor
|
||||
|
||||
class ApplicationServer;
|
||||
class ConnectionManager;
|
||||
class FolderWatchCallbackEx;
|
||||
class ControlRequestHandler;
|
||||
|
||||
class ApplicationManagerBase
|
||||
@@ -192,9 +191,7 @@ protected:
|
||||
bool m_sourceControlReady = false;
|
||||
bool m_fullIdle = false;
|
||||
|
||||
AZStd::vector<AZStd::unique_ptr<FolderWatchCallbackEx> > m_folderWatches;
|
||||
FileWatcher m_fileWatcher;
|
||||
AZStd::vector<int> m_watchHandles;
|
||||
AssetProcessor::PlatformConfiguration* m_platformConfiguration = nullptr;
|
||||
AssetProcessor::AssetProcessorManager* m_assetProcessorManager = nullptr;
|
||||
AssetProcessor::AssetCatalog* m_assetCatalog = nullptr;
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include <native/resourcecompiler/RCBuilder.h>
|
||||
#include <AssetBuilder/AssetBuilderInfo.h>
|
||||
|
||||
class FolderWatchCallbackEx;
|
||||
class QCoreApplication;
|
||||
|
||||
namespace AssetProcessor
|
||||
|
||||
Reference in New Issue
Block a user