editor_file_system.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/os/dir_access.h"
  33. #include "core/os/thread.h"
  34. #include "core/os/thread_safe.h"
  35. #include "core/safe_refcount.h"
  36. #include "core/set.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; //used for checking changes
  45. EditorFileSystemDirectory *parent;
  46. Vector<EditorFileSystemDirectory *> subdirs;
  47. struct FileInfo {
  48. String file;
  49. StringName type;
  50. uint64_t modified_time;
  51. uint64_t import_modified_time;
  52. bool import_valid;
  53. String import_group_file;
  54. Vector<String> deps;
  55. bool verified; //used for checking changes
  56. String script_class_name;
  57. String script_class_extends;
  58. String script_class_icon_path;
  59. };
  60. struct FileInfoSort {
  61. bool operator()(const FileInfo *p_a, const FileInfo *p_b) const {
  62. return p_a->file < p_b->file;
  63. }
  64. };
  65. void sort_files();
  66. Vector<FileInfo *> files;
  67. static void _bind_methods();
  68. friend class EditorFileSystem;
  69. public:
  70. String get_name();
  71. String get_path() const;
  72. int get_subdir_count() const;
  73. EditorFileSystemDirectory *get_subdir(int p_idx);
  74. int get_file_count() const;
  75. String get_file(int p_idx) const;
  76. String get_file_path(int p_idx) const;
  77. StringName get_file_type(int p_idx) const;
  78. Vector<String> get_file_deps(int p_idx) const;
  79. bool get_file_import_is_valid(int p_idx) const;
  80. uint64_t get_file_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. EditorFileSystemDirectory *get_parent();
  85. int find_file_index(const String &p_file) const;
  86. int find_dir_index(const String &p_dir) const;
  87. void force_update();
  88. EditorFileSystemDirectory();
  89. ~EditorFileSystemDirectory();
  90. };
  91. class EditorFileSystem : public Node {
  92. GDCLASS(EditorFileSystem, Node);
  93. _THREAD_SAFE_CLASS_
  94. struct ItemAction {
  95. enum Action {
  96. ACTION_NONE,
  97. ACTION_DIR_ADD,
  98. ACTION_DIR_REMOVE,
  99. ACTION_FILE_ADD,
  100. ACTION_FILE_REMOVE,
  101. ACTION_FILE_TEST_REIMPORT,
  102. ACTION_FILE_RELOAD
  103. };
  104. Action action;
  105. EditorFileSystemDirectory *dir;
  106. String file;
  107. EditorFileSystemDirectory *new_dir;
  108. EditorFileSystemDirectory::FileInfo *new_file;
  109. ItemAction() {
  110. action = ACTION_NONE;
  111. dir = nullptr;
  112. new_dir = nullptr;
  113. new_file = nullptr;
  114. }
  115. };
  116. bool use_threads;
  117. Thread thread;
  118. static void _thread_func(void *_userdata);
  119. EditorFileSystemDirectory *new_filesystem;
  120. bool abort_scan;
  121. bool scanning;
  122. bool importing;
  123. bool first_scan;
  124. bool scan_changes_pending;
  125. float scan_total;
  126. String filesystem_settings_version_for_import;
  127. bool revalidate_import_files;
  128. void _scan_filesystem();
  129. Set<String> late_added_files; //keep track of files that were added, these will be re-scanned
  130. Set<String> late_update_files;
  131. void _save_late_updated_files();
  132. EditorFileSystemDirectory *filesystem;
  133. static EditorFileSystem *singleton;
  134. /* Used for reading the filesystem cache file */
  135. struct FileCache {
  136. String type;
  137. uint64_t modification_time;
  138. uint64_t import_modification_time;
  139. Vector<String> deps;
  140. bool import_valid;
  141. String import_group_file;
  142. String script_class_name;
  143. String script_class_extends;
  144. String script_class_icon_path;
  145. };
  146. HashMap<String, FileCache> file_cache;
  147. struct ScanProgress {
  148. float low;
  149. float hi;
  150. mutable EditorProgressBG *progress;
  151. void update(int p_current, int p_total) const;
  152. ScanProgress get_sub(int p_current, int p_total) const;
  153. };
  154. void _save_filesystem_cache();
  155. void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file);
  156. bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
  157. void _scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress);
  158. void _create_project_data_dir_if_necessary();
  159. void _delete_internal_files(String p_file);
  160. Set<String> valid_extensions;
  161. Set<String> import_extensions;
  162. void _scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess *da, const ScanProgress &p_progress);
  163. Thread thread_sources;
  164. bool scanning_changes;
  165. bool scanning_changes_done;
  166. static void _thread_func_sources(void *_userdata);
  167. List<String> sources_changed;
  168. List<ItemAction> scan_actions;
  169. bool _update_scan_actions();
  170. void _update_extensions();
  171. void _reimport_file(const String &p_file);
  172. Error _reimport_group(const String &p_group_file, const Vector<String> &p_files);
  173. bool _test_for_reimport(const String &p_path, bool p_only_imported_files);
  174. bool reimport_on_missing_imported_files;
  175. Vector<String> _get_dependencies(const String &p_path);
  176. struct ImportFile {
  177. String path;
  178. int order;
  179. bool operator<(const ImportFile &p_if) const {
  180. return order < p_if.order;
  181. }
  182. };
  183. void _scan_script_classes(EditorFileSystemDirectory *p_dir);
  184. SafeFlag update_script_classes_queued;
  185. void _queue_update_script_classes();
  186. String _get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const;
  187. static Error _resource_import(const String &p_path);
  188. bool using_fat32_or_exfat; // Workaround for projects in FAT32 or exFAT filesystem (pendrives, most of the time)
  189. void _find_group_files(EditorFileSystemDirectory *efd, Map<String, Vector<String>> &group_files, Set<String> &groups_to_reimport);
  190. void _move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location);
  191. Set<String> group_file_cache;
  192. protected:
  193. void _notification(int p_what);
  194. static void _bind_methods();
  195. public:
  196. static EditorFileSystem *get_singleton() { return singleton; }
  197. EditorFileSystemDirectory *get_filesystem();
  198. bool is_scanning() const;
  199. bool is_importing() const { return importing; }
  200. float get_scanning_progress() const;
  201. void scan();
  202. void scan_changes();
  203. void get_changed_sources(List<String> *r_changed);
  204. void update_file(const String &p_file);
  205. Set<String> get_valid_extensions() const;
  206. EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
  207. String get_file_type(const String &p_file) const;
  208. EditorFileSystemDirectory *find_file(const String &p_file, int *r_index) const;
  209. void reimport_files(const Vector<String> &p_files);
  210. void update_script_classes();
  211. bool is_group_file(const String &p_path) const;
  212. void move_group_file(const String &p_path, const String &p_new_path);
  213. static bool _should_skip_directory(const String &p_path);
  214. EditorFileSystem();
  215. ~EditorFileSystem();
  216. };
  217. #endif // EDITOR_FILE_SYSTEM_H