summaryrefslogtreecommitdiff
path: root/tools/editor/editor_file_system.h
blob: 3f413292fe0d161aca79ebb013a85e32d0f7674b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*************************************************************************/
/*  editor_file_system.h                                                 */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#ifndef EDITOR_FILE_SYSTEM_H
#define EDITOR_FILE_SYSTEM_H

#include "scene/main/node.h"
#include "os/thread.h"
#include "os/dir_access.h"
#include "set.h"
#include "os/thread_safe.h"
class FileAccess;

class EditorProgressBG;
class EditorFileSystemDirectory : public Object {

	OBJ_TYPE( EditorFileSystemDirectory,Object );

	String name;

	EditorFileSystemDirectory *parent;
	Vector<EditorFileSystemDirectory*> subdirs;

	struct ImportMeta {

		struct Source {

			String path;
			String md5;
			uint64_t modified_time;
			bool missing;

		};

		Vector<Source> sources;
		String import_editor;
		bool enabled;

	};

	struct FileInfo {
		String file;
		String type;
		uint64_t modified_time;

		ImportMeta meta;
	};

	Vector<FileInfo> files;

	static void _bind_methods();


friend class EditorFileSystem;
public:

	String get_name();
	String get_path() const;

	int get_subdir_count() const;
	EditorFileSystemDirectory *get_subdir(int p_idx);
	int get_file_count() const;
	String get_file(int p_idx) const;
	String get_file_path(int p_idx) const;
	String get_file_type(int p_idx) const;
	bool get_file_meta(int p_idx) const;
	bool is_missing_sources(int p_idx) const;
	Vector<String> get_missing_sources(int p_idx) const;

	EditorFileSystemDirectory *get_parent();


	EditorFileSystemDirectory();
	~EditorFileSystemDirectory();
};

class EditorFileSystem : public Node {

	OBJ_TYPE( EditorFileSystem, Node );

	_THREAD_SAFE_CLASS_

	struct SceneItem {

		String file;
		String path;
		String type;
		uint64_t modified_time;
		EditorFileSystemDirectory::ImportMeta meta;
	};

	struct DirItem {

		uint64_t modified_time;
		String path;
		String name;
		Vector<DirItem*> dirs;
		Vector<SceneItem*> files;
		~DirItem();
	};

	float total;
	bool use_threads;
	Thread *thread;
	static void _thread_func(void *_userdata);

	DirItem *scandir;
	DirItem *rootdir;

	bool abort_scan;
	bool scanning;

	EditorFileSystemDirectory* _update_tree(DirItem *p_item);

	void _scan_scenes();
	void _load_type_cache();

	EditorFileSystemDirectory *filesystem;

	static EditorFileSystem *singleton;

	struct FileCache {

		String type;
		uint64_t modification_time;
		EditorFileSystemDirectory::ImportMeta meta;
	};

	struct DirCache {

		uint64_t modification_time;
		Set<String> files;
		Set<String> subdirs;
	};


	static EditorFileSystemDirectory::ImportMeta _get_meta(const String& p_path);

	bool _check_meta_sources(EditorFileSystemDirectory::ImportMeta & p_meta,EditorProgressBG *ep=NULL);

	DirItem* _scan_dir(DirAccess *da,Set<String> &extensions,String p_name,float p_from,float p_range,const String& p_path,HashMap<String,FileCache> &file_cache,HashMap<String,DirCache> &dir_cache,EditorProgressBG& p_prog);
	void _save_type_cache_fs(DirItem *p_dir,FileAccess *p_file);

	bool _find_file(const String& p_file,EditorFileSystemDirectory ** r_d, int &r_file_pos) const;

	void _scan_sources(EditorFileSystemDirectory *p_dir,EditorProgressBG *ep);

	int md_count;


	Thread *thread_sources;
	bool scanning_sources;
	bool scanning_sources_done;
	int ss_amount;
	static void _thread_func_sources(void *_userdata);
	List<String> sources_changed;

	static void _resource_saved(const String& p_path);
	String _find_first_from_source(EditorFileSystemDirectory* p_dir,const String &p_src) const;

protected:

	void _notification(int p_what);
	static void _bind_methods();
public:


	static EditorFileSystem* get_singleton() { return singleton; }

	EditorFileSystemDirectory *get_filesystem();
	bool is_scanning() const;
	float get_scanning_progress() const;
	void scan();
	void scan_sources();
	void get_changed_sources(List<String> *r_changed);
	void update_file(const String& p_file);
	String find_resource_from_source(const String& p_path) const;
	EditorFileSystemDirectory *get_path(const String& p_path);
	String get_file_type(const String& p_file) const;
	EditorFileSystem();
	~EditorFileSystem();
};

#endif // EDITOR_FILE_SYSTEM_H