editor_file_system.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/dir_access.h"
  33. #include "os/thread.h"
  34. #include "os/thread_safe.h"
  35. #include "scene/main/node.h"
  36. #include "set.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. };
  55. struct FileInfoSort {
  56. bool operator()(const FileInfo *p_a, const FileInfo *p_b) const {
  57. return p_a->file < p_b->file;
  58. }
  59. };
  60. void sort_files();
  61. Vector<FileInfo *> files;
  62. static void _bind_methods();
  63. friend class EditorFileSystem;
  64. public:
  65. String get_name();
  66. String get_path() const;
  67. int get_subdir_count() const;
  68. EditorFileSystemDirectory *get_subdir(int p_idx);
  69. int get_file_count() const;
  70. String get_file(int p_idx) const;
  71. String get_file_path(int p_idx) const;
  72. StringName get_file_type(int p_idx) const;
  73. Vector<String> get_file_deps(int p_idx) const;
  74. bool get_file_import_is_valid(int p_idx) const;
  75. EditorFileSystemDirectory *get_parent();
  76. int find_file_index(const String &p_file) const;
  77. int find_dir_index(const String &p_dir) const;
  78. EditorFileSystemDirectory();
  79. ~EditorFileSystemDirectory();
  80. };
  81. class EditorFileSystem : public Node {
  82. GDCLASS(EditorFileSystem, Node);
  83. _THREAD_SAFE_CLASS_
  84. struct ItemAction {
  85. enum Action {
  86. ACTION_NONE,
  87. ACTION_DIR_ADD,
  88. ACTION_DIR_REMOVE,
  89. ACTION_FILE_ADD,
  90. ACTION_FILE_REMOVE,
  91. ACTION_FILE_REIMPORT
  92. };
  93. Action action;
  94. EditorFileSystemDirectory *dir;
  95. String file;
  96. EditorFileSystemDirectory *new_dir;
  97. EditorFileSystemDirectory::FileInfo *new_file;
  98. ItemAction() {
  99. action = ACTION_NONE;
  100. dir = NULL;
  101. new_dir = NULL;
  102. new_file = NULL;
  103. }
  104. };
  105. bool use_threads;
  106. Thread *thread;
  107. static void _thread_func(void *_userdata);
  108. EditorFileSystemDirectory *new_filesystem;
  109. bool abort_scan;
  110. bool scanning;
  111. bool importing;
  112. float scan_total;
  113. void _scan_filesystem();
  114. Set<String> late_added_files; //keep track of files that were added, these will be re-scanned
  115. Set<String> late_update_files;
  116. void _save_late_updated_files();
  117. EditorFileSystemDirectory *filesystem;
  118. static EditorFileSystem *singleton;
  119. /* Used for reading the filesystem cache file */
  120. struct FileCache {
  121. String type;
  122. uint64_t modification_time;
  123. uint64_t import_modification_time;
  124. Vector<String> deps;
  125. bool import_valid;
  126. };
  127. HashMap<String, FileCache> file_cache;
  128. struct ScanProgress {
  129. float low;
  130. float hi;
  131. mutable EditorProgressBG *progress;
  132. void update(int p_current, int p_total) const;
  133. ScanProgress get_sub(int p_current, int p_total) const;
  134. };
  135. void _save_filesystem_cache();
  136. void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file);
  137. bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
  138. void _scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress);
  139. void _delete_internal_files(String p_file);
  140. Set<String> valid_extensions;
  141. Set<String> import_extensions;
  142. void _scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess *da, const ScanProgress &p_progress);
  143. Thread *thread_sources;
  144. bool scanning_changes;
  145. bool scanning_changes_done;
  146. static void _thread_func_sources(void *_userdata);
  147. List<String> sources_changed;
  148. List<ItemAction> scan_actions;
  149. bool _update_scan_actions();
  150. static void _resource_saved(const String &p_path);
  151. void _update_extensions();
  152. void _reimport_file(const String &p_file);
  153. bool _check_missing_imported_files(const String &p_path);
  154. bool reimport_on_missing_imported_files;
  155. Vector<String> _get_dependencies(const String &p_path);
  156. struct ImportFile {
  157. String path;
  158. int order;
  159. bool operator<(const ImportFile &p_if) const {
  160. return order < p_if.order;
  161. }
  162. };
  163. protected:
  164. void _notification(int p_what);
  165. static void _bind_methods();
  166. public:
  167. static EditorFileSystem *get_singleton() { return singleton; }
  168. EditorFileSystemDirectory *get_filesystem();
  169. bool is_scanning() const;
  170. bool is_importing() const { return importing; }
  171. float get_scanning_progress() const;
  172. void scan();
  173. void scan_changes();
  174. void get_changed_sources(List<String> *r_changed);
  175. void update_file(const String &p_file);
  176. EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
  177. String get_file_type(const String &p_file) const;
  178. EditorFileSystemDirectory *find_file(const String &p_file, int *r_index) const;
  179. void reimport_files(const Vector<String> &p_files);
  180. EditorFileSystem();
  181. ~EditorFileSystem();
  182. };
  183. #endif // EDITOR_FILE_SYSTEM_H