editor_file_system.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/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. String get_file_script_class_name(int p_idx) const; //used for scripts
  81. String get_file_script_class_extends(int p_idx) const; //used for scripts
  82. String get_file_script_class_icon_path(int p_idx) const; //used for scripts
  83. EditorFileSystemDirectory *get_parent();
  84. int find_file_index(const String &p_file) const;
  85. int find_dir_index(const String &p_dir) const;
  86. EditorFileSystemDirectory();
  87. ~EditorFileSystemDirectory();
  88. };
  89. class EditorFileSystem : public Node {
  90. GDCLASS(EditorFileSystem, Node);
  91. _THREAD_SAFE_CLASS_
  92. struct ItemAction {
  93. enum Action {
  94. ACTION_NONE,
  95. ACTION_DIR_ADD,
  96. ACTION_DIR_REMOVE,
  97. ACTION_FILE_ADD,
  98. ACTION_FILE_REMOVE,
  99. ACTION_FILE_TEST_REIMPORT,
  100. ACTION_FILE_RELOAD
  101. };
  102. Action action;
  103. EditorFileSystemDirectory *dir;
  104. String file;
  105. EditorFileSystemDirectory *new_dir;
  106. EditorFileSystemDirectory::FileInfo *new_file;
  107. ItemAction() {
  108. action = ACTION_NONE;
  109. dir = NULL;
  110. new_dir = NULL;
  111. new_file = NULL;
  112. }
  113. };
  114. bool use_threads;
  115. Thread thread;
  116. static void _thread_func(void *_userdata);
  117. EditorFileSystemDirectory *new_filesystem;
  118. bool abort_scan;
  119. bool scanning;
  120. bool importing;
  121. bool first_scan;
  122. bool scan_changes_pending;
  123. float scan_total;
  124. String filesystem_settings_version_for_import;
  125. bool revalidate_import_files;
  126. void _scan_filesystem();
  127. Set<String> late_added_files; //keep track of files that were added, these will be re-scanned
  128. Set<String> late_update_files;
  129. void _save_late_updated_files();
  130. EditorFileSystemDirectory *filesystem;
  131. static EditorFileSystem *singleton;
  132. /* Used for reading the filesystem cache file */
  133. struct FileCache {
  134. String type;
  135. uint64_t modification_time;
  136. uint64_t import_modification_time;
  137. Vector<String> deps;
  138. bool import_valid;
  139. String import_group_file;
  140. String script_class_name;
  141. String script_class_extends;
  142. String script_class_icon_path;
  143. };
  144. HashMap<String, FileCache> file_cache;
  145. struct ScanProgress {
  146. float low;
  147. float hi;
  148. mutable EditorProgressBG *progress;
  149. void update(int p_current, int p_total) const;
  150. ScanProgress get_sub(int p_current, int p_total) const;
  151. };
  152. void _save_filesystem_cache();
  153. void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file);
  154. bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
  155. void _scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress);
  156. void _delete_internal_files(String p_file);
  157. Set<String> valid_extensions;
  158. Set<String> import_extensions;
  159. void _scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess *da, const ScanProgress &p_progress);
  160. Thread thread_sources;
  161. bool scanning_changes;
  162. bool scanning_changes_done;
  163. static void _thread_func_sources(void *_userdata);
  164. List<String> sources_changed;
  165. List<ItemAction> scan_actions;
  166. bool _update_scan_actions();
  167. void _update_extensions();
  168. void _reimport_file(const String &p_file);
  169. Error _reimport_group(const String &p_group_file, const Vector<String> &p_files);
  170. bool _test_for_reimport(const String &p_path, bool p_only_imported_files);
  171. bool reimport_on_missing_imported_files;
  172. Vector<String> _get_dependencies(const String &p_path);
  173. struct ImportFile {
  174. String path;
  175. int order;
  176. bool operator<(const ImportFile &p_if) const {
  177. return order < p_if.order;
  178. }
  179. };
  180. void _scan_script_classes(EditorFileSystemDirectory *p_dir);
  181. SafeFlag update_script_classes_queued;
  182. void _queue_update_script_classes();
  183. String _get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const;
  184. static Error _resource_import(const String &p_path);
  185. bool using_fat32_or_exfat; // Workaround for projects in FAT32 or exFAT filesystem (pendrives, most of the time)
  186. void _find_group_files(EditorFileSystemDirectory *efd, Map<String, Vector<String> > &group_files, Set<String> &groups_to_reimport);
  187. void _move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location);
  188. Set<String> group_file_cache;
  189. protected:
  190. void _notification(int p_what);
  191. static void _bind_methods();
  192. public:
  193. static EditorFileSystem *get_singleton() { return singleton; }
  194. EditorFileSystemDirectory *get_filesystem();
  195. bool is_scanning() const;
  196. bool is_importing() const { return importing; }
  197. float get_scanning_progress() const;
  198. void scan();
  199. void scan_changes();
  200. void get_changed_sources(List<String> *r_changed);
  201. void update_file(const String &p_file);
  202. EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
  203. String get_file_type(const String &p_file) const;
  204. EditorFileSystemDirectory *find_file(const String &p_file, int *r_index) const;
  205. void reimport_files(const Vector<String> &p_files);
  206. void update_script_classes();
  207. bool is_group_file(const String &p_path) const;
  208. void move_group_file(const String &p_path, const String &p_new_path);
  209. static bool _should_skip_directory(const String &p_path);
  210. EditorFileSystem();
  211. ~EditorFileSystem();
  212. };
  213. #endif // EDITOR_FILE_SYSTEM_H