editor_file_system.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. String icon_path;
  63. };
  64. Vector<FileInfo *> files;
  65. static void _bind_methods();
  66. friend class EditorFileSystem;
  67. public:
  68. String get_name();
  69. String get_path() const;
  70. int get_subdir_count() const;
  71. EditorFileSystemDirectory *get_subdir(int p_idx);
  72. int get_file_count() const;
  73. String get_file(int p_idx) const;
  74. String get_file_path(int p_idx) const;
  75. StringName get_file_type(int p_idx) const;
  76. StringName get_file_resource_script_class(int p_idx) const;
  77. Vector<String> get_file_deps(int p_idx) const;
  78. bool get_file_import_is_valid(int p_idx) const;
  79. uint64_t get_file_modified_time(int p_idx) const;
  80. uint64_t get_file_import_modified_time(int p_idx) const;
  81. String get_file_script_class_name(int p_idx) const; //used for scripts
  82. String get_file_script_class_extends(int p_idx) const; //used for scripts
  83. String get_file_script_class_icon_path(int p_idx) const; //used for scripts
  84. String get_file_icon_path(int p_idx) const; //used for FileSystemDock
  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. struct ScannedDirectory {
  140. String name;
  141. String full_path;
  142. Vector<ScannedDirectory *> subdirs;
  143. List<String> files;
  144. ~ScannedDirectory();
  145. };
  146. bool use_threads = false;
  147. Thread thread;
  148. static void _thread_func(void *_userdata);
  149. EditorFileSystemDirectory *new_filesystem = nullptr;
  150. ScannedDirectory *first_scan_root_dir = nullptr;
  151. bool scanning = false;
  152. bool importing = false;
  153. bool first_scan = true;
  154. bool scan_changes_pending = false;
  155. float scan_total;
  156. String filesystem_settings_version_for_import;
  157. bool revalidate_import_files = false;
  158. int nb_files_total = 0;
  159. void _scan_filesystem();
  160. void _first_scan_filesystem();
  161. void _first_scan_process_scripts(const ScannedDirectory *p_scan_dir, HashSet<String> &p_existing_class_names);
  162. HashSet<String> late_update_files;
  163. void _save_late_updated_files();
  164. EditorFileSystemDirectory *filesystem = nullptr;
  165. static EditorFileSystem *singleton;
  166. /* Used for reading the filesystem cache file */
  167. struct FileCache {
  168. String type;
  169. String resource_script_class;
  170. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  171. uint64_t modification_time = 0;
  172. uint64_t import_modification_time = 0;
  173. Vector<String> deps;
  174. bool import_valid = false;
  175. String import_group_file;
  176. String script_class_name;
  177. String script_class_extends;
  178. String script_class_icon_path;
  179. };
  180. HashMap<String, FileCache> file_cache;
  181. HashSet<String> dep_update_list;
  182. struct ScanProgress {
  183. float hi = 0;
  184. int current = 0;
  185. EditorProgressBG *progress = nullptr;
  186. void increment();
  187. };
  188. void _save_filesystem_cache();
  189. void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file);
  190. bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
  191. void _scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanProgress &p_progress);
  192. void _delete_internal_files(const String &p_file);
  193. int _insert_actions_delete_files_directory(EditorFileSystemDirectory *p_dir);
  194. HashSet<String> textfile_extensions;
  195. HashSet<String> valid_extensions;
  196. HashSet<String> import_extensions;
  197. int _scan_new_dir(ScannedDirectory *p_dir, Ref<DirAccess> &da);
  198. void _process_file_system(const ScannedDirectory *p_scan_dir, EditorFileSystemDirectory *p_dir, ScanProgress &p_progress);
  199. Thread thread_sources;
  200. bool scanning_changes = false;
  201. SafeFlag scanning_changes_done;
  202. static void _thread_func_sources(void *_userdata);
  203. List<String> sources_changed;
  204. List<ItemAction> scan_actions;
  205. bool _update_scan_actions();
  206. void _update_extensions();
  207. 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);
  208. Error _reimport_group(const String &p_group_file, const Vector<String> &p_files);
  209. bool _test_for_reimport(const String &p_path, bool p_only_imported_files);
  210. bool reimport_on_missing_imported_files;
  211. Vector<String> _get_dependencies(const String &p_path);
  212. struct ImportFile {
  213. String path;
  214. String importer;
  215. bool threaded = false;
  216. int order = 0;
  217. bool operator<(const ImportFile &p_if) const {
  218. return order == p_if.order ? (importer < p_if.importer) : (order < p_if.order);
  219. }
  220. };
  221. struct ScriptInfo {
  222. String type;
  223. String script_class_name;
  224. String script_class_extends;
  225. String script_class_icon_path;
  226. };
  227. Mutex update_script_mutex;
  228. HashMap<String, ScriptInfo> update_script_paths;
  229. HashSet<String> update_script_paths_documentation;
  230. 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);
  231. void _update_script_classes();
  232. void _update_script_documentation();
  233. void _process_update_pending();
  234. Mutex update_scene_mutex;
  235. HashSet<String> update_scene_paths;
  236. void _queue_update_scene_groups(const String &p_path);
  237. void _update_scene_groups();
  238. void _update_pending_scene_groups();
  239. void _get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet<String> &r_list);
  240. String _get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const;
  241. static Error _resource_import(const String &p_path);
  242. bool using_fat32_or_exfat; // Workaround for projects in FAT32 or exFAT filesystem (pendrives, most of the time)
  243. void _find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport);
  244. void _move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location);
  245. HashSet<String> group_file_cache;
  246. HashMap<String, String> file_icon_cache;
  247. struct ImportThreadData {
  248. const ImportFile *reimport_files;
  249. int reimport_from;
  250. SafeNumeric<int> max_index;
  251. };
  252. void _reimport_thread(uint32_t p_index, ImportThreadData *p_import_data);
  253. static ResourceUID::ID _resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate);
  254. bool _scan_extensions();
  255. bool _scan_import_support(const Vector<String> &reimports);
  256. Vector<Ref<EditorFileSystemImportFormatSupportQuery>> import_support_queries;
  257. void _update_file_icon_path(EditorFileSystemDirectory::FileInfo *file_info);
  258. void _update_files_icon_path(EditorFileSystemDirectory *edp = nullptr);
  259. void _remove_invalid_global_class_names(const HashSet<String> &p_existing_class_names);
  260. String _get_file_by_class_name(EditorFileSystemDirectory *p_dir, const String &p_class_name, EditorFileSystemDirectory::FileInfo *&r_file_info);
  261. 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);
  262. protected:
  263. void _notification(int p_what);
  264. static void _bind_methods();
  265. public:
  266. static EditorFileSystem *get_singleton() { return singleton; }
  267. EditorFileSystemDirectory *get_filesystem();
  268. bool is_scanning() const;
  269. bool is_importing() const { return importing; }
  270. bool doing_first_scan() const { return first_scan; }
  271. float get_scanning_progress() const;
  272. void scan();
  273. void scan_changes();
  274. void update_file(const String &p_file);
  275. void update_files(const Vector<String> &p_script_paths);
  276. HashSet<String> get_valid_extensions() const;
  277. void register_global_class_script(const String &p_search_path, const String &p_target_path);
  278. EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
  279. String get_file_type(const String &p_file) const;
  280. EditorFileSystemDirectory *find_file(const String &p_file, int *r_index) const;
  281. void reimport_files(const Vector<String> &p_files);
  282. Error reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters);
  283. void reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params);
  284. bool is_group_file(const String &p_path) const;
  285. void move_group_file(const String &p_path, const String &p_new_path);
  286. static bool _should_skip_directory(const String &p_path);
  287. void add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
  288. void remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
  289. EditorFileSystem();
  290. ~EditorFileSystem();
  291. };
  292. #endif // EDITOR_FILE_SYSTEM_H