editor_file_system.h 11 KB

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