3
0

BuilderSettingManager.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <BuilderSettings/BuilderSettings.h>
  10. #include <AzCore/base.h>
  11. #include <AzCore/IO/Path/Path.h>
  12. #include <AzCore/std/containers/set.h>
  13. #include <Atom/ImageProcessing/ImageObject.h>
  14. #include <Atom/ImageProcessing/ImageProcessingDefines.h>
  15. #include <QDateTime>
  16. #include <QFileSystemWatcher>
  17. #include <QScopedPointer>
  18. class QSettings;
  19. class QString;
  20. namespace AZ
  21. {
  22. template<class T>
  23. class EnvironmentVariable;
  24. class SerializeContext;
  25. }
  26. namespace ImageProcessingAtom
  27. {
  28. /**
  29. * BuilderSettingManager is a singleton which responses to manage preset settings and some global settings for image builder.
  30. * It loads all presets from *.settings file. The settings file need to be loaded before builder or editor starting process any image.
  31. * Preset is a collection of some parameters which used for convert and export image to formats used in runtime. It's used as template to provide
  32. * default settings when process certain type of texture
  33. * When process an image, a texture setting will be loaded (from legacy .exportsettings or new .imagesettings) or
  34. * generated for this image. The texture setting will need to reference to a preset which is used to initialize the texture setting.
  35. * Each preset setting may have different values on different platform, but they are using same uuid.
  36. */
  37. class BuilderSettingManager
  38. : public QObject // required for using QFileSystemWatcher
  39. {
  40. friend class ImageProcessingTest;
  41. public:
  42. AZ_TYPE_INFO(BuilderSettingManager, "{8E95726D-4E3A-446C-99A6-D02863640EAC}");
  43. AZ_CLASS_ALLOCATOR(BuilderSettingManager, AZ::SystemAllocator);
  44. static BuilderSettingManager* Instance();
  45. // life cycle management:
  46. static void CreateInstance();
  47. static void DestroyInstance();
  48. static void Reflect(AZ::ReflectContext* context);
  49. const PresetSettings* GetPreset(const PresetName& presetName, const PlatformName& platform = "", AZStd::string_view* settingsFilePathOut = nullptr) const;
  50. AZStd::vector<AZStd::string> GetFileMasksForPreset(const PresetName& presetName) const;
  51. const BuilderSettings* GetBuilderSetting(const PlatformName& platform) const;
  52. //! Return A list of platform supported
  53. const PlatformNameList GetPlatformList() const;
  54. //! Return A map of preset settings based on their filemasks.
  55. //! @key filemask string, empty string means no filemask
  56. //! @value set of preset setting names supporting the specified filemask
  57. const AZStd::map<FileMask, AZStd::unordered_set<PresetName>>& GetPresetFilterMap() const;
  58. const AZStd::unordered_set<PresetName>& GetFullPresetList() const;
  59. //! Find preset name based on the preset id.
  60. const PresetName GetPresetNameFromId(const AZ::Uuid& presetId);
  61. //! Load configurations files from both project and gem default config folders
  62. StringOutcome LoadConfig();
  63. //! Load configurations files from a folder which includes builder settings and presets
  64. //! Note: this is only used for unit test. Use LoadConfig() for editor or game launcher
  65. StringOutcome LoadConfigFromFolder(AZStd::string_view configFolder);
  66. //! Reload preset from config folders
  67. void ReloadPreset(const PresetName& presetName);
  68. const AZStd::string& GetAnalysisFingerprint() const;
  69. //! Provides a full path to the adjacent metafile of a given texture/image file.
  70. //! @param imagePath File path string to the texture/image file.
  71. //! @param Output metafilePath File path string to the adjacent texture/image metafile.
  72. void MetafilePathFromImagePath(AZStd::string_view imagePath, AZStd::string& metafilePath);
  73. //! Find a suitable preset a given image file.
  74. //! @param imageFilePath: Filepath string of the image file. The function may load the image from the path for better detection
  75. //! @param image: an optional image object which can be used for preset selection if there is no match based file mask.
  76. //! @return suggested preset name.
  77. PresetName GetSuggestedPreset(AZStd::string_view imageFilePath) const;
  78. //! Get the possible preset config's full file paths
  79. //! This function is only used for setting up image's source dependency if a preset file is missing
  80. //! Otherwise, the preset's file path can be retrieved in GetPreset() function
  81. AZStd::vector<AZStd::string> GetPossiblePresetPaths(const PresetName& presetName) const;
  82. //! Extract the file mask of a file path
  83. AZStd::string GetFileMask(AZStd::string_view imageFilePath) const;
  84. //! Return the default opaque preset name
  85. PresetName GetDefaultPreset() const;
  86. //! Return the default alpha preset name
  87. PresetName GetDefaultAlphaPreset() const;
  88. //! Return all preset names associated with this file mask
  89. AZStd::vector<PresetName> GetPresetsForFileMask(const FileMask& fileMask) const;
  90. //! Return true if the preset name is valid
  91. bool IsValidPreset(PresetName presetName) const;
  92. bool DoesSupportPlatform(AZStd::string_view platformId);
  93. static const char* s_environmentVariableName;
  94. static AZ::EnvironmentVariable<BuilderSettingManager*> s_globalInstance;
  95. static AZStd::mutex s_instanceMutex;
  96. static const PlatformName s_defaultPlatform;
  97. // The relative folder where the default image builder configuration files (builder setting, presets) are.
  98. static const char* s_defaultConfigFolder;
  99. // The relative folder where project's image builder configuration files are
  100. static const char* s_projectConfigRelativeFolder;
  101. // Builder setting file name
  102. static const char* s_builderSettingFileName;
  103. static const char* s_presetFileExtension;
  104. BuilderSettingManager() = default;
  105. private: // functions
  106. AZ_DISABLE_COPY_MOVE(BuilderSettingManager);
  107. // Write image builder setting to the file specified by filepath
  108. StringOutcome WriteSettings(AZStd::string_view filepath);
  109. // Load image builder settings from the file specified by filepath
  110. StringOutcome LoadSettings(AZStd::string_view filepath);
  111. // Load merge image builder settings (project and default)
  112. StringOutcome LoadSettings();
  113. // report warnings for the deprecated properties in image builder setting data
  114. void ReportDeprecatedSettings();
  115. // Clear Builder Settings and any cached maps/lists
  116. void ClearSettings();
  117. // collect file masks
  118. void CollectFileMasksFromPresets();
  119. // Functions to save/load preset from a folder
  120. void SavePresets(AZStd::string_view outputFolder);
  121. void LoadPresets(AZStd::string_view presetFolder);
  122. // Load a preset to m_presets and return true if success
  123. bool LoadPreset(const AZStd::string& filePath);
  124. // handle preset files changes
  125. void OnFileChanged(const QString &path);
  126. void OnFolderChanged(const QString &path);
  127. private: // variables
  128. struct PresetEntry
  129. {
  130. MultiplatformPresetSettings m_multiPreset;
  131. AZStd::string m_presetFilePath; // Can be used for debug output
  132. QDateTime m_lastModifiedTime;
  133. };
  134. // Builder settings for each platform
  135. AZStd::map <PlatformName, BuilderSettings> m_builderSettings;
  136. AZStd::unordered_map<PresetName, PresetEntry> m_presets;
  137. // a list of presets mapped by their file masks.
  138. // @Key file mask, use empty string to indicate all presets without filtering
  139. // @Value set of preset names that matches the file mask
  140. AZStd::map <FileMask, AZStd::unordered_set<PresetName>> m_presetFilterMap;
  141. // A mutex to protect when modifying any map in this manager
  142. mutable AZStd::recursive_mutex m_presetMapLock;
  143. // Default presets for certain file masks
  144. AZStd::map <FileMask, PresetName > m_defaultPresetByFileMask;
  145. // Default preset for none power of two image
  146. PresetName m_defaultPresetNonePOT;
  147. // Default preset for power of two
  148. PresetName m_defaultPreset;
  149. // Default preset for power of two with alpha
  150. PresetName m_defaultPresetAlpha;
  151. // Image builder's version
  152. AZStd::string m_analysisFingerprint;
  153. // default config folder
  154. AZ::IO::FixedMaxPath m_defaultConfigFolder;
  155. // project config folder
  156. AZ::IO::FixedMaxPath m_projectConfigFolder;
  157. // File system watcher to detect preset file changes
  158. QScopedPointer<QFileSystemWatcher> m_fileWatcher;
  159. };
  160. } // namespace ImageProcessingAtom