editor_file_system.h 7.7 KB

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