editor_file_system.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*************************************************************************/
  2. /* editor_file_system.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  51. uint64_t modified_time = 0;
  52. uint64_t import_modified_time = 0;
  53. bool import_valid = false;
  54. String import_group_file;
  55. Vector<String> deps;
  56. bool verified = false; //used for checking changes
  57. String script_class_name;
  58. String script_class_extends;
  59. String script_class_icon_path;
  60. };
  61. struct FileInfoSort {
  62. bool operator()(const FileInfo *p_a, const FileInfo *p_b) const {
  63. return p_a->file < p_b->file;
  64. }
  65. };
  66. void sort_files();
  67. Vector<FileInfo *> files;
  68. static void _bind_methods();
  69. friend class EditorFileSystem;
  70. public:
  71. String get_name();
  72. String get_path() const;
  73. int get_subdir_count() const;
  74. EditorFileSystemDirectory *get_subdir(int p_idx);
  75. int get_file_count() const;
  76. String get_file(int p_idx) const;
  77. String get_file_path(int p_idx) const;
  78. StringName get_file_type(int p_idx) const;
  79. Vector<String> get_file_deps(int p_idx) const;
  80. bool get_file_import_is_valid(int p_idx) const;
  81. uint64_t get_file_modified_time(int p_idx) const;
  82. String get_file_script_class_name(int p_idx) const; //used for scripts
  83. String get_file_script_class_extends(int p_idx) const; //used for scripts
  84. String get_file_script_class_icon_path(int p_idx) const; //used for scripts
  85. EditorFileSystemDirectory *get_parent();
  86. int find_file_index(const String &p_file) const;
  87. int find_dir_index(const String &p_dir) const;
  88. void force_update();
  89. EditorFileSystemDirectory();
  90. ~EditorFileSystemDirectory();
  91. };
  92. class EditorFileSystemImportFormatSupportQuery : public RefCounted {
  93. GDCLASS(EditorFileSystemImportFormatSupportQuery, RefCounted);
  94. protected:
  95. GDVIRTUAL0RC(bool, _is_active)
  96. GDVIRTUAL0RC(Vector<String>, _get_file_extensions)
  97. GDVIRTUAL0RC(bool, _query)
  98. static void _bind_methods() {
  99. GDVIRTUAL_BIND(_is_active);
  100. GDVIRTUAL_BIND(_get_file_extensions);
  101. GDVIRTUAL_BIND(_query);
  102. }
  103. public:
  104. virtual bool is_active() const {
  105. bool ret = false;
  106. GDVIRTUAL_REQUIRED_CALL(_is_active, ret);
  107. return ret;
  108. }
  109. virtual Vector<String> get_file_extensions() const {
  110. Vector<String> ret;
  111. GDVIRTUAL_REQUIRED_CALL(_get_file_extensions, ret);
  112. return ret;
  113. }
  114. virtual bool query() {
  115. bool ret = false;
  116. GDVIRTUAL_REQUIRED_CALL(_query, ret);
  117. return ret;
  118. }
  119. };
  120. class EditorFileSystem : public Node {
  121. GDCLASS(EditorFileSystem, Node);
  122. _THREAD_SAFE_CLASS_
  123. struct ItemAction {
  124. enum Action {
  125. ACTION_NONE,
  126. ACTION_DIR_ADD,
  127. ACTION_DIR_REMOVE,
  128. ACTION_FILE_ADD,
  129. ACTION_FILE_REMOVE,
  130. ACTION_FILE_TEST_REIMPORT,
  131. ACTION_FILE_RELOAD
  132. };
  133. Action action = ACTION_NONE;
  134. EditorFileSystemDirectory *dir = nullptr;
  135. String file;
  136. EditorFileSystemDirectory *new_dir = nullptr;
  137. EditorFileSystemDirectory::FileInfo *new_file = nullptr;
  138. };
  139. bool use_threads = true;
  140. Thread thread;
  141. static void _thread_func(void *_userdata);
  142. EditorFileSystemDirectory *new_filesystem = nullptr;
  143. bool abort_scan = false;
  144. bool scanning = false;
  145. bool importing = false;
  146. bool first_scan = true;
  147. bool scan_changes_pending = false;
  148. float scan_total;
  149. String filesystem_settings_version_for_import;
  150. bool revalidate_import_files = false;
  151. void _scan_filesystem();
  152. HashSet<String> late_update_files;
  153. void _save_late_updated_files();
  154. EditorFileSystemDirectory *filesystem = nullptr;
  155. static EditorFileSystem *singleton;
  156. /* Used for reading the filesystem cache file */
  157. struct FileCache {
  158. String type;
  159. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  160. uint64_t modification_time = 0;
  161. uint64_t import_modification_time = 0;
  162. Vector<String> deps;
  163. bool import_valid = false;
  164. String import_group_file;
  165. String script_class_name;
  166. String script_class_extends;
  167. String script_class_icon_path;
  168. };
  169. HashMap<String, FileCache> file_cache;
  170. struct ScanProgress {
  171. float low = 0;
  172. float hi = 0;
  173. mutable EditorProgressBG *progress = nullptr;
  174. void update(int p_current, int p_total) const;
  175. ScanProgress get_sub(int p_current, int p_total) const;
  176. };
  177. void _save_filesystem_cache();
  178. void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file);
  179. bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
  180. void _scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress);
  181. void _delete_internal_files(String p_file);
  182. HashSet<String> textfile_extensions;
  183. HashSet<String> valid_extensions;
  184. HashSet<String> import_extensions;
  185. void _scan_new_dir(EditorFileSystemDirectory *p_dir, Ref<DirAccess> &da, const ScanProgress &p_progress);
  186. Thread thread_sources;
  187. bool scanning_changes = false;
  188. bool scanning_changes_done = false;
  189. static void _thread_func_sources(void *_userdata);
  190. List<String> sources_changed;
  191. List<ItemAction> scan_actions;
  192. bool _update_scan_actions();
  193. void _update_extensions();
  194. void _reimport_file(const String &p_file, const HashMap<StringName, Variant> *p_custom_options = nullptr, const String &p_custom_importer = String());
  195. Error _reimport_group(const String &p_group_file, const Vector<String> &p_files);
  196. bool _test_for_reimport(const String &p_path, bool p_only_imported_files);
  197. bool reimport_on_missing_imported_files;
  198. Vector<String> _get_dependencies(const String &p_path);
  199. struct ImportFile {
  200. String path;
  201. String importer;
  202. bool threaded = false;
  203. int order = 0;
  204. bool operator<(const ImportFile &p_if) const {
  205. return order == p_if.order ? (importer < p_if.importer) : (order < p_if.order);
  206. }
  207. };
  208. void _scan_script_classes(EditorFileSystemDirectory *p_dir);
  209. SafeFlag update_script_classes_queued;
  210. void _queue_update_script_classes();
  211. String _get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const;
  212. static Error _resource_import(const String &p_path);
  213. bool using_fat32_or_exfat; // Workaround for projects in FAT32 or exFAT filesystem (pendrives, most of the time)
  214. void _find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport);
  215. void _move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location);
  216. HashSet<String> group_file_cache;
  217. struct ImportThreadData {
  218. const ImportFile *reimport_files;
  219. int reimport_from;
  220. int max_index = 0;
  221. };
  222. void _reimport_thread(uint32_t p_index, ImportThreadData *p_import_data);
  223. static ResourceUID::ID _resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate);
  224. bool _scan_extensions();
  225. bool _scan_import_support(Vector<String> reimports);
  226. Vector<Ref<EditorFileSystemImportFormatSupportQuery>> import_support_queries;
  227. protected:
  228. void _notification(int p_what);
  229. static void _bind_methods();
  230. public:
  231. static EditorFileSystem *get_singleton() { return singleton; }
  232. EditorFileSystemDirectory *get_filesystem();
  233. bool is_scanning() const;
  234. bool is_importing() const { return importing; }
  235. float get_scanning_progress() const;
  236. void scan();
  237. void scan_changes();
  238. void update_file(const String &p_file);
  239. HashSet<String> get_valid_extensions() const;
  240. EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
  241. String get_file_type(const String &p_file) const;
  242. EditorFileSystemDirectory *find_file(const String &p_file, int *r_index) const;
  243. void reimport_files(const Vector<String> &p_files);
  244. void reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params);
  245. void update_script_classes();
  246. bool is_group_file(const String &p_path) const;
  247. void move_group_file(const String &p_path, const String &p_new_path);
  248. static bool _should_skip_directory(const String &p_path);
  249. void add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
  250. void remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
  251. EditorFileSystem();
  252. ~EditorFileSystem();
  253. };
  254. #endif // EDITOR_FILE_SYSTEM_H