FileWatcher.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #pragma once
  9. #include "FileWatcherBase.h"
  10. #if !defined(Q_MOC_RUN)
  11. #include <AzCore/std/smart_ptr/unique_ptr.h>
  12. #include <AzCore/std/containers/vector.h>
  13. #include <AzCore/std/parallel/atomic.h>
  14. #include <AzCore/std/parallel/thread.h>
  15. #include <QString>
  16. #include <QObject>
  17. #endif
  18. //////////////////////////////////////////////////////////////////////////
  19. //! FileWatcher
  20. /*! Class that handles creation and deletion of FolderRootWatches based on
  21. *! the given FolderWatches, and forwards file change signals to them.
  22. * */
  23. class FileWatcher : public FileWatcherBase
  24. {
  25. Q_OBJECT
  26. public:
  27. FileWatcher();
  28. ~FileWatcher() override;
  29. //////////////////////////////////////////////////////////////////////////
  30. void AddFolderWatch(QString directory, bool recursive = true) override;
  31. bool HasWatchFolder(QString directory) const;
  32. void ClearFolderWatches() override;
  33. //////////////////////////////////////////////////////////////////////////
  34. //////////////////////////////////////////////////////////////////////////
  35. void StartWatching() override;
  36. void StopWatching() override;
  37. //////////////////////////////////////////////////////////////////////////
  38. //////////////////////////////////////////////////////////////////////////
  39. void AddExclusion(const AssetBuilderSDK::FilePatternMatcher& excludeMatch) override;
  40. bool IsExcluded(QString filePath) const override;
  41. //////////////////////////////////////////////////////////////////////////
  42. void InstallDefaultExclusionRules(QString cacheRootPath, QString projectRootPath) override;
  43. private:
  44. bool PlatformStart();
  45. void PlatformStop();
  46. void WatchFolderLoop();
  47. class PlatformImplementation;
  48. friend class PlatformImplementation;
  49. struct WatchRoot
  50. {
  51. QString m_directory;
  52. bool m_recursive;
  53. };
  54. static bool Filter(QString path, const WatchRoot& watchRoot);
  55. AZStd::unique_ptr<PlatformImplementation> m_platformImpl;
  56. AZStd::vector<WatchRoot> m_folderWatchRoots;
  57. AZStd::vector<AssetBuilderSDK::FilePatternMatcher> m_excludes;
  58. AZStd::thread m_thread;
  59. bool m_startedWatching = false;
  60. AZStd::atomic_bool m_shutdownThreadSignal = false;
  61. // platform implementations must signal this to indicate they have fully initialized
  62. // and will not be dropping events.
  63. AZStd::atomic_bool m_startedSignal = false;
  64. };