editor_file_system.h 8.5 KB

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