editor_file_system.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/io/resource_importer.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/os/thread.h"
  36. #include "core/os/thread_safe.h"
  37. #include "core/templates/hash_set.h"
  38. #include "core/templates/safe_refcount.h"
  39. #include "scene/main/node.h"
  40. class FileAccess;
  41. struct EditorProgressBG;
  42. class EditorFileSystemDirectory : public Object {
  43. GDCLASS(EditorFileSystemDirectory, Object);
  44. String name;
  45. uint64_t modified_time;
  46. bool verified = false; //used for checking changes
  47. EditorFileSystemDirectory *parent = nullptr;
  48. Vector<EditorFileSystemDirectory *> subdirs;
  49. struct FileInfo {
  50. String file;
  51. StringName type;
  52. StringName resource_script_class; // If any resource has script with a global class name, its found here.
  53. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  54. uint64_t modified_time = 0;
  55. uint64_t import_modified_time = 0;
  56. String import_md5;
  57. Vector<String> import_dest_paths;
  58. bool import_valid = false;
  59. String import_group_file;
  60. Vector<String> deps;
  61. bool verified = false; //used for checking changes
  62. // These are for script resources only.
  63. String script_class_name;
  64. String script_class_extends;
  65. String script_class_icon_path;
  66. String icon_path;
  67. };
  68. Vector<FileInfo *> files;
  69. static void _bind_methods();
  70. friend class EditorFileSystem;
  71. public:
  72. String get_name();
  73. String get_path() const;
  74. int get_subdir_count() const;
  75. EditorFileSystemDirectory *get_subdir(int p_idx);
  76. int get_file_count() const;
  77. String get_file(int p_idx) const;
  78. String get_file_path(int p_idx) const;
  79. StringName get_file_type(int p_idx) const;
  80. StringName get_file_resource_script_class(int p_idx) const;
  81. Vector<String> get_file_deps(int p_idx) const;
  82. bool get_file_import_is_valid(int p_idx) const;
  83. uint64_t get_file_modified_time(int p_idx) const;
  84. uint64_t get_file_import_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. String get_file_icon_path(int p_idx) const; //used for FileSystemDock
  89. EditorFileSystemDirectory *get_parent();
  90. int find_file_index(const String &p_file) const;
  91. int find_dir_index(const String &p_dir) const;
  92. void force_update();
  93. EditorFileSystemDirectory();
  94. ~EditorFileSystemDirectory();
  95. };
  96. class EditorFileSystemImportFormatSupportQuery : public RefCounted {
  97. GDCLASS(EditorFileSystemImportFormatSupportQuery, RefCounted);
  98. protected:
  99. GDVIRTUAL0RC_REQUIRED(bool, _is_active)
  100. GDVIRTUAL0RC_REQUIRED(Vector<String>, _get_file_extensions)
  101. GDVIRTUAL0RC_REQUIRED(bool, _query)
  102. static void _bind_methods() {
  103. GDVIRTUAL_BIND(_is_active);
  104. GDVIRTUAL_BIND(_get_file_extensions);
  105. GDVIRTUAL_BIND(_query);
  106. }
  107. public:
  108. virtual bool is_active() const {
  109. bool ret = false;
  110. GDVIRTUAL_CALL(_is_active, ret);
  111. return ret;
  112. }
  113. virtual Vector<String> get_file_extensions() const {
  114. Vector<String> ret;
  115. GDVIRTUAL_CALL(_get_file_extensions, ret);
  116. return ret;
  117. }
  118. virtual bool query() {
  119. bool ret = false;
  120. GDVIRTUAL_CALL(_query, ret);
  121. return ret;
  122. }
  123. };
  124. class EditorFileSystem : public Node {
  125. GDCLASS(EditorFileSystem, Node);
  126. _THREAD_SAFE_CLASS_
  127. struct ItemAction {
  128. enum Action {
  129. ACTION_NONE,
  130. ACTION_DIR_ADD,
  131. ACTION_DIR_REMOVE,
  132. ACTION_FILE_ADD,
  133. ACTION_FILE_REMOVE,
  134. ACTION_FILE_TEST_REIMPORT,
  135. ACTION_FILE_RELOAD
  136. };
  137. Action action = ACTION_NONE;
  138. EditorFileSystemDirectory *dir = nullptr;
  139. String file;
  140. EditorFileSystemDirectory *new_dir = nullptr;
  141. EditorFileSystemDirectory::FileInfo *new_file = nullptr;
  142. };
  143. struct ScannedDirectory {
  144. String name;
  145. String full_path;
  146. Vector<ScannedDirectory *> subdirs;
  147. List<String> files;
  148. ~ScannedDirectory();
  149. };
  150. bool use_threads = false;
  151. Thread thread;
  152. static void _thread_func(void *_userdata);
  153. EditorFileSystemDirectory *new_filesystem = nullptr;
  154. ScannedDirectory *first_scan_root_dir = nullptr;
  155. bool filesystem_changed_queued = false;
  156. bool scanning = false;
  157. bool importing = false;
  158. bool first_scan = true;
  159. bool scan_changes_pending = false;
  160. float scan_total;
  161. String filesystem_settings_version_for_import;
  162. bool revalidate_import_files = false;
  163. int nb_files_total = 0;
  164. void _notify_filesystem_changed();
  165. void _scan_filesystem();
  166. void _first_scan_filesystem();
  167. void _first_scan_process_scripts(const ScannedDirectory *p_scan_dir, List<String> &p_gdextension_extensions, HashSet<String> &p_existing_class_names, HashSet<String> &p_extensions);
  168. HashSet<String> late_update_files;
  169. void _save_late_updated_files();
  170. EditorFileSystemDirectory *filesystem = nullptr;
  171. static EditorFileSystem *singleton;
  172. /* Used for reading the filesystem cache file */
  173. struct FileCache {
  174. String type;
  175. String resource_script_class;
  176. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  177. uint64_t modification_time = 0;
  178. uint64_t import_modification_time = 0;
  179. String import_md5;
  180. Vector<String> import_dest_paths;
  181. Vector<String> deps;
  182. bool import_valid = false;
  183. String import_group_file;
  184. String script_class_name;
  185. String script_class_extends;
  186. String script_class_icon_path;
  187. };
  188. HashMap<String, FileCache> file_cache;
  189. HashSet<String> dep_update_list;
  190. struct ScanProgress {
  191. float hi = 0;
  192. int current = 0;
  193. EditorProgressBG *progress = nullptr;
  194. void increment();
  195. };
  196. struct DirectoryComparator {
  197. bool operator()(const EditorFileSystemDirectory *p_a, const EditorFileSystemDirectory *p_b) const {
  198. return p_a->name.filenocasecmp_to(p_b->name) < 0;
  199. }
  200. };
  201. void _save_filesystem_cache();
  202. void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file);
  203. bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
  204. void _scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, bool p_recursive = true);
  205. void _delete_internal_files(const String &p_file);
  206. int _insert_actions_delete_files_directory(EditorFileSystemDirectory *p_dir);
  207. HashSet<String> textfile_extensions;
  208. HashSet<String> other_file_extensions;
  209. HashSet<String> valid_extensions;
  210. HashSet<String> import_extensions;
  211. int _scan_new_dir(ScannedDirectory *p_dir, Ref<DirAccess> &da);
  212. void _process_file_system(const ScannedDirectory *p_scan_dir, EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, HashSet<String> *p_processed_files);
  213. Thread thread_sources;
  214. bool scanning_changes = false;
  215. SafeFlag scanning_changes_done;
  216. static void _thread_func_sources(void *_userdata);
  217. List<String> sources_changed;
  218. List<ItemAction> scan_actions;
  219. bool _update_scan_actions();
  220. void _update_extensions();
  221. 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, bool p_update_file_system = true);
  222. Error _reimport_group(const String &p_group_file, const Vector<String> &p_files);
  223. bool _test_for_reimport(const String &p_path, const String &p_expected_import_md5);
  224. bool _is_test_for_reimport_needed(const String &p_path, uint64_t p_last_modification_time, uint64_t p_modification_time, uint64_t p_last_import_modification_time, uint64_t p_import_modification_time, const Vector<String> &p_import_dest_paths);
  225. Vector<String> _get_import_dest_paths(const String &p_path);
  226. bool reimport_on_missing_imported_files;
  227. Vector<String> _get_dependencies(const String &p_path);
  228. struct ImportFile {
  229. String path;
  230. String importer;
  231. bool threaded = false;
  232. int order = 0;
  233. bool operator<(const ImportFile &p_if) const {
  234. return order == p_if.order ? (importer < p_if.importer) : (order < p_if.order);
  235. }
  236. };
  237. struct ScriptInfo {
  238. String type;
  239. String script_class_name;
  240. String script_class_extends;
  241. String script_class_icon_path;
  242. };
  243. Mutex update_script_mutex;
  244. HashMap<String, ScriptInfo> update_script_paths;
  245. HashSet<String> update_script_paths_documentation;
  246. void _queue_update_script_class(const String &p_path, const String &p_type, const String &p_script_class_name, const String &p_script_class_extends, const String &p_script_class_icon_path);
  247. void _update_script_classes();
  248. void _update_script_documentation();
  249. void _process_update_pending();
  250. void _process_removed_files(const HashSet<String> &p_processed_files);
  251. bool _should_reload_script(const String &p_path);
  252. Mutex update_scene_mutex;
  253. HashSet<String> update_scene_paths;
  254. void _queue_update_scene_groups(const String &p_path);
  255. void _update_scene_groups();
  256. void _update_pending_scene_groups();
  257. void _get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet<String> &r_list);
  258. String _get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const;
  259. static Error _resource_import(const String &p_path);
  260. static Ref<Resource> _load_resource_on_startup(ResourceFormatImporter *p_importer, const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, ResourceFormatLoader::CacheMode p_cache_mode);
  261. bool using_fat32_or_exfat; // Workaround for projects in FAT32 or exFAT filesystem (pendrives, most of the time)
  262. void _find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport);
  263. void _move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location);
  264. HashSet<String> group_file_cache;
  265. HashMap<String, String> file_icon_cache;
  266. struct CopiedFile {
  267. String from;
  268. String to;
  269. };
  270. bool refresh_queued = false;
  271. HashSet<ObjectID> folders_to_sort;
  272. Error _copy_file(const String &p_from, const String &p_to);
  273. bool _copy_directory(const String &p_from, const String &p_to, List<CopiedFile> *p_files);
  274. void _queue_refresh_filesystem();
  275. void _refresh_filesystem();
  276. struct ImportThreadData {
  277. const ImportFile *reimport_files;
  278. int reimport_from;
  279. SafeNumeric<int> max_index;
  280. };
  281. void _reimport_thread(uint32_t p_index, ImportThreadData *p_import_data);
  282. static ResourceUID::ID _resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate);
  283. bool _scan_extensions();
  284. bool _scan_import_support(const Vector<String> &reimports);
  285. Vector<Ref<EditorFileSystemImportFormatSupportQuery>> import_support_queries;
  286. void _update_file_icon_path(EditorFileSystemDirectory::FileInfo *file_info);
  287. void _update_files_icon_path(EditorFileSystemDirectory *edp = nullptr);
  288. void _remove_invalid_global_class_names(const HashSet<String> &p_existing_class_names);
  289. String _get_file_by_class_name(EditorFileSystemDirectory *p_dir, const String &p_class_name, EditorFileSystemDirectory::FileInfo *&r_file_info);
  290. void _register_global_class_script(const String &p_search_path, const String &p_target_path, const String &p_type, const String &p_script_class_name, const String &p_script_class_extends, const String &p_script_class_icon_path);
  291. protected:
  292. void _notification(int p_what);
  293. static void _bind_methods();
  294. public:
  295. static EditorFileSystem *get_singleton() { return singleton; }
  296. EditorFileSystemDirectory *get_filesystem();
  297. bool is_scanning() const;
  298. bool is_importing() const { return importing; }
  299. bool doing_first_scan() const { return first_scan; }
  300. float get_scanning_progress() const;
  301. void scan();
  302. void scan_changes();
  303. void update_file(const String &p_file);
  304. void update_files(const Vector<String> &p_script_paths);
  305. HashSet<String> get_valid_extensions() const;
  306. void register_global_class_script(const String &p_search_path, const String &p_target_path);
  307. EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
  308. String get_file_type(const String &p_file) const;
  309. EditorFileSystemDirectory *find_file(const String &p_file, int *r_index) const;
  310. void reimport_files(const Vector<String> &p_files);
  311. Error reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters);
  312. void reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params);
  313. bool is_group_file(const String &p_path) const;
  314. void move_group_file(const String &p_path, const String &p_new_path);
  315. Error make_dir_recursive(const String &p_path, const String &p_base_path = String());
  316. Error copy_file(const String &p_from, const String &p_to);
  317. Error copy_directory(const String &p_from, const String &p_to);
  318. static bool _should_skip_directory(const String &p_path);
  319. void add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
  320. void remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
  321. EditorFileSystem();
  322. ~EditorFileSystem();
  323. };
  324. #endif // EDITOR_FILE_SYSTEM_H