editor_file_system.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**************************************************************************/
  2. /* editor_file_system.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef EDITOR_FILE_SYSTEM_H
  31. #define EDITOR_FILE_SYSTEM_H
  32. #include "core/io/dir_access.h"
  33. #include "core/os/thread.h"
  34. #include "core/os/thread_safe.h"
  35. #include "core/templates/hash_set.h"
  36. #include "core/templates/safe_refcount.h"
  37. #include "scene/main/node.h"
  38. class FileAccess;
  39. struct EditorProgressBG;
  40. class EditorFileSystemDirectory : public Object {
  41. GDCLASS(EditorFileSystemDirectory, Object);
  42. String name;
  43. uint64_t modified_time;
  44. bool verified = false; //used for checking changes
  45. EditorFileSystemDirectory *parent = nullptr;
  46. Vector<EditorFileSystemDirectory *> subdirs;
  47. struct FileInfo {
  48. String file;
  49. StringName type;
  50. StringName resource_script_class; // If any resource has script with a global class name, its found here.
  51. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  52. uint64_t modified_time = 0;
  53. uint64_t import_modified_time = 0;
  54. bool import_valid = false;
  55. String import_group_file;
  56. Vector<String> deps;
  57. bool verified = false; //used for checking changes
  58. // These are for script resources only.
  59. String script_class_name;
  60. String script_class_extends;
  61. String script_class_icon_path;
  62. };
  63. struct FileInfoSort {
  64. bool operator()(const FileInfo *p_a, const FileInfo *p_b) const {
  65. return p_a->file < p_b->file;
  66. }
  67. };
  68. void sort_files();
  69. Vector<FileInfo *> files;
  70. static void _bind_methods();
  71. friend class EditorFileSystem;
  72. public:
  73. String get_name();
  74. String get_path() const;
  75. int get_subdir_count() const;
  76. EditorFileSystemDirectory *get_subdir(int p_idx);
  77. int get_file_count() const;
  78. String get_file(int p_idx) const;
  79. String get_file_path(int p_idx) const;
  80. StringName get_file_type(int p_idx) const;
  81. StringName get_file_resource_script_class(int p_idx) const;
  82. Vector<String> get_file_deps(int p_idx) const;
  83. bool get_file_import_is_valid(int p_idx) const;
  84. uint64_t get_file_modified_time(int p_idx) const;
  85. String get_file_script_class_name(int p_idx) const; //used for scripts
  86. String get_file_script_class_extends(int p_idx) const; //used for scripts
  87. String get_file_script_class_icon_path(int p_idx) const; //used for scripts
  88. EditorFileSystemDirectory *get_parent();
  89. int find_file_index(const String &p_file) const;
  90. int find_dir_index(const String &p_dir) const;
  91. void force_update();
  92. EditorFileSystemDirectory();
  93. ~EditorFileSystemDirectory();
  94. };
  95. class EditorFileSystemImportFormatSupportQuery : public RefCounted {
  96. GDCLASS(EditorFileSystemImportFormatSupportQuery, RefCounted);
  97. protected:
  98. GDVIRTUAL0RC(bool, _is_active)
  99. GDVIRTUAL0RC(Vector<String>, _get_file_extensions)
  100. GDVIRTUAL0RC(bool, _query)
  101. static void _bind_methods() {
  102. GDVIRTUAL_BIND(_is_active);
  103. GDVIRTUAL_BIND(_get_file_extensions);
  104. GDVIRTUAL_BIND(_query);
  105. }
  106. public:
  107. virtual bool is_active() const {
  108. bool ret = false;
  109. GDVIRTUAL_REQUIRED_CALL(_is_active, ret);
  110. return ret;
  111. }
  112. virtual Vector<String> get_file_extensions() const {
  113. Vector<String> ret;
  114. GDVIRTUAL_REQUIRED_CALL(_get_file_extensions, ret);
  115. return ret;
  116. }
  117. virtual bool query() {
  118. bool ret = false;
  119. GDVIRTUAL_REQUIRED_CALL(_query, ret);
  120. return ret;
  121. }
  122. };
  123. class EditorFileSystem : public Node {
  124. GDCLASS(EditorFileSystem, Node);
  125. _THREAD_SAFE_CLASS_
  126. struct ItemAction {
  127. enum Action {
  128. ACTION_NONE,
  129. ACTION_DIR_ADD,
  130. ACTION_DIR_REMOVE,
  131. ACTION_FILE_ADD,
  132. ACTION_FILE_REMOVE,
  133. ACTION_FILE_TEST_REIMPORT,
  134. ACTION_FILE_RELOAD
  135. };
  136. Action action = ACTION_NONE;
  137. EditorFileSystemDirectory *dir = nullptr;
  138. String file;
  139. EditorFileSystemDirectory *new_dir = nullptr;
  140. EditorFileSystemDirectory::FileInfo *new_file = nullptr;
  141. };
  142. bool use_threads = true;
  143. Thread thread;
  144. static void _thread_func(void *_userdata);
  145. EditorFileSystemDirectory *new_filesystem = nullptr;
  146. bool scanning = false;
  147. bool importing = false;
  148. bool first_scan = true;
  149. bool scan_changes_pending = false;
  150. float scan_total;
  151. String filesystem_settings_version_for_import;
  152. bool revalidate_import_files = false;
  153. void _scan_filesystem();
  154. HashSet<String> late_update_files;
  155. void _save_late_updated_files();
  156. EditorFileSystemDirectory *filesystem = nullptr;
  157. static EditorFileSystem *singleton;
  158. /* Used for reading the filesystem cache file */
  159. struct FileCache {
  160. String type;
  161. String resource_script_class;
  162. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  163. uint64_t modification_time = 0;
  164. uint64_t import_modification_time = 0;
  165. Vector<String> deps;
  166. bool import_valid = false;
  167. String import_group_file;
  168. String script_class_name;
  169. String script_class_extends;
  170. String script_class_icon_path;
  171. };
  172. HashMap<String, FileCache> file_cache;
  173. struct ScanProgress {
  174. float low = 0;
  175. float hi = 0;
  176. mutable EditorProgressBG *progress = nullptr;
  177. void update(int p_current, int p_total) const;
  178. ScanProgress get_sub(int p_current, int p_total) const;
  179. };
  180. void _save_filesystem_cache();
  181. void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file);
  182. bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
  183. void _scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress);
  184. void _delete_internal_files(String p_file);
  185. HashSet<String> textfile_extensions;
  186. HashSet<String> valid_extensions;
  187. HashSet<String> import_extensions;
  188. void _scan_new_dir(EditorFileSystemDirectory *p_dir, Ref<DirAccess> &da, const ScanProgress &p_progress);
  189. Thread thread_sources;
  190. bool scanning_changes = false;
  191. bool scanning_changes_done = false;
  192. static void _thread_func_sources(void *_userdata);
  193. List<String> sources_changed;
  194. List<ItemAction> scan_actions;
  195. bool _update_scan_actions();
  196. void _update_extensions();
  197. Error _reimport_file(const String &p_file, const HashMap<StringName, Variant> &p_custom_options = HashMap<StringName, Variant>(), const String &p_custom_importer = String(), Variant *generator_parameters = nullptr);
  198. Error _reimport_group(const String &p_group_file, const Vector<String> &p_files);
  199. bool _test_for_reimport(const String &p_path, bool p_only_imported_files);
  200. bool reimport_on_missing_imported_files;
  201. Vector<String> _get_dependencies(const String &p_path);
  202. struct ImportFile {
  203. String path;
  204. String importer;
  205. bool threaded = false;
  206. int order = 0;
  207. bool operator<(const ImportFile &p_if) const {
  208. return order == p_if.order ? (importer < p_if.importer) : (order < p_if.order);
  209. }
  210. };
  211. Mutex update_script_mutex;
  212. HashSet<String> update_script_paths;
  213. void _queue_update_script_class(const String &p_path);
  214. void _update_script_classes();
  215. void _update_pending_script_classes();
  216. String _get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const;
  217. static Error _resource_import(const String &p_path);
  218. bool using_fat32_or_exfat; // Workaround for projects in FAT32 or exFAT filesystem (pendrives, most of the time)
  219. void _find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport);
  220. void _move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location);
  221. HashSet<String> group_file_cache;
  222. struct ImportThreadData {
  223. const ImportFile *reimport_files;
  224. int reimport_from;
  225. int max_index = 0;
  226. };
  227. void _reimport_thread(uint32_t p_index, ImportThreadData *p_import_data);
  228. static ResourceUID::ID _resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate);
  229. bool _scan_extensions();
  230. bool _scan_import_support(Vector<String> reimports);
  231. Vector<Ref<EditorFileSystemImportFormatSupportQuery>> import_support_queries;
  232. protected:
  233. void _notification(int p_what);
  234. static void _bind_methods();
  235. public:
  236. static EditorFileSystem *get_singleton() { return singleton; }
  237. EditorFileSystemDirectory *get_filesystem();
  238. bool is_scanning() const;
  239. bool is_importing() const { return importing; }
  240. bool doing_first_scan() const { return first_scan; }
  241. float get_scanning_progress() const;
  242. void scan();
  243. void scan_changes();
  244. void update_file(const String &p_file);
  245. HashSet<String> get_valid_extensions() const;
  246. EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
  247. String get_file_type(const String &p_file) const;
  248. EditorFileSystemDirectory *find_file(const String &p_file, int *r_index) const;
  249. void reimport_files(const Vector<String> &p_files);
  250. Error reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters);
  251. void reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params);
  252. bool is_group_file(const String &p_path) const;
  253. void move_group_file(const String &p_path, const String &p_new_path);
  254. static bool _should_skip_directory(const String &p_path);
  255. void add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
  256. void remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
  257. EditorFileSystem();
  258. ~EditorFileSystem();
  259. };
  260. #endif // EDITOR_FILE_SYSTEM_H