editor_file_system.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/io/dir_access.h"
  33. #include "core/os/thread.h"
  34. #include "core/os/thread_safe.h"
  35. #include "core/templates/safe_refcount.h"
  36. #include "core/templates/set.h"
  37. #include "core/templates/thread_work_pool.h"
  38. #include "scene/main/node.h"
  39. class FileAccess;
  40. struct EditorProgressBG;
  41. class EditorFileSystemDirectory : public Object {
  42. GDCLASS(EditorFileSystemDirectory, Object);
  43. String name;
  44. uint64_t modified_time;
  45. bool verified; //used for checking changes
  46. EditorFileSystemDirectory *parent;
  47. Vector<EditorFileSystemDirectory *> subdirs;
  48. struct FileInfo {
  49. String file;
  50. StringName type;
  51. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  52. uint64_t modified_time = 0;
  53. uint64_t import_modified_time = 0;
  54. bool import_valid = false;
  55. String import_group_file;
  56. Vector<String> deps;
  57. bool verified = false; //used for checking changes
  58. String script_class_name;
  59. String script_class_extends;
  60. String script_class_icon_path;
  61. };
  62. struct FileInfoSort {
  63. bool operator()(const FileInfo *p_a, const FileInfo *p_b) const {
  64. return p_a->file < p_b->file;
  65. }
  66. };
  67. void sort_files();
  68. Vector<FileInfo *> files;
  69. static void _bind_methods();
  70. friend class EditorFileSystem;
  71. public:
  72. String get_name();
  73. String get_path() const;
  74. int get_subdir_count() const;
  75. EditorFileSystemDirectory *get_subdir(int p_idx);
  76. int get_file_count() const;
  77. String get_file(int p_idx) const;
  78. String get_file_path(int p_idx) const;
  79. StringName get_file_type(int p_idx) const;
  80. Vector<String> get_file_deps(int p_idx) const;
  81. bool get_file_import_is_valid(int p_idx) const;
  82. uint64_t get_file_modified_time(int p_idx) const;
  83. String get_file_script_class_name(int p_idx) const; //used for scripts
  84. String get_file_script_class_extends(int p_idx) const; //used for scripts
  85. String get_file_script_class_icon_path(int p_idx) const; //used for scripts
  86. EditorFileSystemDirectory *get_parent();
  87. int find_file_index(const String &p_file) const;
  88. int find_dir_index(const String &p_dir) const;
  89. void force_update();
  90. EditorFileSystemDirectory();
  91. ~EditorFileSystemDirectory();
  92. };
  93. class EditorFileSystem : public Node {
  94. GDCLASS(EditorFileSystem, Node);
  95. _THREAD_SAFE_CLASS_
  96. struct ItemAction {
  97. enum Action {
  98. ACTION_NONE,
  99. ACTION_DIR_ADD,
  100. ACTION_DIR_REMOVE,
  101. ACTION_FILE_ADD,
  102. ACTION_FILE_REMOVE,
  103. ACTION_FILE_TEST_REIMPORT,
  104. ACTION_FILE_RELOAD
  105. };
  106. Action action = ACTION_NONE;
  107. EditorFileSystemDirectory *dir = nullptr;
  108. String file;
  109. EditorFileSystemDirectory *new_dir = nullptr;
  110. EditorFileSystemDirectory::FileInfo *new_file = nullptr;
  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. bool first_scan;
  120. bool scan_changes_pending;
  121. float scan_total;
  122. String filesystem_settings_version_for_import;
  123. bool revalidate_import_files;
  124. void _scan_filesystem();
  125. Set<String> late_update_files;
  126. void _save_late_updated_files();
  127. EditorFileSystemDirectory *filesystem;
  128. static EditorFileSystem *singleton;
  129. /* Used for reading the filesystem cache file */
  130. struct FileCache {
  131. String type;
  132. ResourceUID::ID uid = ResourceUID::INVALID_ID;
  133. uint64_t modification_time = 0;
  134. uint64_t import_modification_time = 0;
  135. Vector<String> deps;
  136. bool import_valid = false;
  137. String import_group_file;
  138. String script_class_name;
  139. String script_class_extends;
  140. String script_class_icon_path;
  141. };
  142. HashMap<String, FileCache> file_cache;
  143. struct ScanProgress {
  144. float low = 0;
  145. float hi = 0;
  146. mutable EditorProgressBG *progress = nullptr;
  147. void update(int p_current, int p_total) const;
  148. ScanProgress get_sub(int p_current, int p_total) const;
  149. };
  150. void _save_filesystem_cache();
  151. void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, FileAccess *p_file);
  152. bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
  153. void _scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress);
  154. void _delete_internal_files(String p_file);
  155. Set<String> textfile_extensions;
  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, const Map<StringName, Variant> *p_custom_options = nullptr, const String &p_custom_importer = String());
  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. String importer;
  175. bool threaded = false;
  176. int order = 0;
  177. bool operator<(const ImportFile &p_if) const {
  178. return order == p_if.order ? (importer < p_if.importer) : (order < p_if.order);
  179. }
  180. };
  181. void _scan_script_classes(EditorFileSystemDirectory *p_dir);
  182. SafeFlag update_script_classes_queued;
  183. void _queue_update_script_classes();
  184. String _get_global_script_class(const String &p_type, const String &p_path, String *r_extends, String *r_icon_path) const;
  185. static Error _resource_import(const String &p_path);
  186. bool using_fat32_or_exfat; // Workaround for projects in FAT32 or exFAT filesystem (pendrives, most of the time)
  187. void _find_group_files(EditorFileSystemDirectory *efd, Map<String, Vector<String>> &group_files, Set<String> &groups_to_reimport);
  188. void _move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location);
  189. Set<String> group_file_cache;
  190. ThreadWorkPool import_threads;
  191. struct ImportThreadData {
  192. const ImportFile *reimport_files;
  193. int reimport_from;
  194. int max_index = 0;
  195. };
  196. void _reimport_thread(uint32_t p_index, ImportThreadData *p_import_data);
  197. static ResourceUID::ID _resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate);
  198. bool _scan_extensions();
  199. protected:
  200. void _notification(int p_what);
  201. static void _bind_methods();
  202. public:
  203. static EditorFileSystem *get_singleton() { return singleton; }
  204. EditorFileSystemDirectory *get_filesystem();
  205. bool is_scanning() const;
  206. bool is_importing() const { return importing; }
  207. float get_scanning_progress() const;
  208. void scan();
  209. void scan_changes();
  210. void update_file(const String &p_file);
  211. EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
  212. String get_file_type(const String &p_file) const;
  213. EditorFileSystemDirectory *find_file(const String &p_file, int *r_index) const;
  214. void reimport_files(const Vector<String> &p_files);
  215. void reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const Map<StringName, Variant> &p_custom_params);
  216. void update_script_classes();
  217. bool is_group_file(const String &p_path) const;
  218. void move_group_file(const String &p_path, const String &p_new_path);
  219. static bool _should_skip_directory(const String &p_path);
  220. EditorFileSystem();
  221. ~EditorFileSystem();
  222. };
  223. #endif // EDITOR_FILE_SYSTEM_H