editor_file_system.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "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. OBJ_TYPE(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 ImportMeta {
  47. struct Source {
  48. String path;
  49. String md5;
  50. uint64_t modified_time;
  51. bool missing;
  52. };
  53. Vector<Source> sources;
  54. String import_editor;
  55. Vector<String> deps;
  56. bool enabled;
  57. bool sources_changed;
  58. };
  59. struct FileInfo {
  60. String file;
  61. StringName type;
  62. uint64_t modified_time;
  63. ImportMeta meta;
  64. bool verified; //used for checking changes
  65. };
  66. struct FileInfoSort {
  67. bool operator()(const FileInfo *p_a, const FileInfo *p_b) const {
  68. return p_a->file < p_b->file;
  69. }
  70. };
  71. void sort_files();
  72. Vector<FileInfo *> files;
  73. static void _bind_methods();
  74. friend class EditorFileSystem;
  75. public:
  76. String get_name();
  77. String get_path() const;
  78. int get_subdir_count() const;
  79. EditorFileSystemDirectory *get_subdir(int p_idx);
  80. int get_file_count() const;
  81. String get_file(int p_idx) const;
  82. String get_file_path(int p_idx) const;
  83. StringName get_file_type(int p_idx) const;
  84. bool get_file_meta(int p_idx) const;
  85. bool is_missing_sources(int p_idx) const;
  86. bool have_sources_changed(int p_idx) const;
  87. Vector<String> get_missing_sources(int p_idx) const;
  88. Vector<String> get_file_deps(int p_idx) const;
  89. int get_source_count(int p_idx) const;
  90. String get_source_file(int p_idx, int p_source) const;
  91. bool is_source_file_missing(int p_idx, int p_source) const;
  92. EditorFileSystemDirectory *get_parent();
  93. int find_file_index(const String &p_file) const;
  94. int find_dir_index(const String &p_dir) const;
  95. EditorFileSystemDirectory();
  96. ~EditorFileSystemDirectory();
  97. };
  98. class EditorFileSystem : public Node {
  99. OBJ_TYPE(EditorFileSystem, Node);
  100. _THREAD_SAFE_CLASS_
  101. struct ItemAction {
  102. enum Action {
  103. ACTION_NONE,
  104. ACTION_DIR_ADD,
  105. ACTION_DIR_REMOVE,
  106. ACTION_FILE_ADD,
  107. ACTION_FILE_REMOVE,
  108. ACTION_FILE_SOURCES_CHANGED
  109. };
  110. Action action;
  111. EditorFileSystemDirectory *dir;
  112. String file;
  113. EditorFileSystemDirectory *new_dir;
  114. EditorFileSystemDirectory::FileInfo *new_file;
  115. ItemAction() {
  116. action = ACTION_NONE;
  117. dir = NULL;
  118. new_dir = NULL;
  119. new_file = NULL;
  120. }
  121. };
  122. bool use_threads;
  123. Thread *thread;
  124. static void _thread_func(void *_userdata);
  125. EditorFileSystemDirectory *new_filesystem;
  126. bool abort_scan;
  127. bool scanning;
  128. float scan_total;
  129. void _scan_filesystem();
  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. EditorFileSystemDirectory::ImportMeta meta;
  137. Vector<String> deps;
  138. };
  139. HashMap<String, FileCache> file_cache;
  140. struct ScanProgress {
  141. float low;
  142. float hi;
  143. mutable EditorProgressBG *progress;
  144. void update(int p_current, int p_total) const;
  145. ScanProgress get_sub(int p_current, int p_total) const;
  146. };
  147. static EditorFileSystemDirectory::ImportMeta _get_meta(const String &p_path);
  148. bool _check_meta_sources(EditorFileSystemDirectory::ImportMeta &p_meta);
  149. void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file);
  150. bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
  151. void _scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress);
  152. Set<String> valid_extensions;
  153. void _scan_new_dir(EditorFileSystemDirectory *p_dir, DirAccess *da, const ScanProgress &p_progress);
  154. Thread *thread_sources;
  155. bool scanning_sources;
  156. bool scanning_sources_done;
  157. static void _thread_func_sources(void *_userdata);
  158. List<String> sources_changed;
  159. List<ItemAction> scan_actions;
  160. bool _update_scan_actions();
  161. static void _resource_saved(const String &p_path);
  162. String _find_first_from_source(EditorFileSystemDirectory *p_dir, const String &p_src) const;
  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. float get_scanning_progress() const;
  171. void scan();
  172. void scan_sources();
  173. void get_changed_sources(List<String> *r_changed);
  174. void update_file(const String &p_file);
  175. String find_resource_from_source(const String &p_path) const;
  176. EditorFileSystemDirectory *get_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. EditorFileSystem();
  180. ~EditorFileSystem();
  181. };
  182. #endif // EDITOR_FILE_SYSTEM_H