Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/file_system/editor_paths.cpp
9896 views
1
/**************************************************************************/
2
/* editor_paths.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31
#include "editor_paths.h"
32
33
#include "core/config/engine.h"
34
#include "core/config/project_settings.h"
35
#include "core/io/dir_access.h"
36
#include "core/os/os.h"
37
#include "main/main.h"
38
39
EditorPaths *EditorPaths::singleton = nullptr;
40
41
bool EditorPaths::are_paths_valid() const {
42
return paths_valid;
43
}
44
45
String EditorPaths::get_data_dir() const {
46
return data_dir;
47
}
48
49
String EditorPaths::get_config_dir() const {
50
return config_dir;
51
}
52
53
String EditorPaths::get_cache_dir() const {
54
return cache_dir;
55
}
56
57
String EditorPaths::get_temp_dir() const {
58
return temp_dir;
59
}
60
61
String EditorPaths::get_project_data_dir() const {
62
return project_data_dir;
63
}
64
65
bool EditorPaths::is_self_contained() const {
66
return self_contained;
67
}
68
69
String EditorPaths::get_self_contained_file() const {
70
return self_contained_file;
71
}
72
73
String EditorPaths::get_export_templates_dir() const {
74
return get_data_dir().path_join(export_templates_folder);
75
}
76
77
String EditorPaths::get_debug_keystore_path() const {
78
#ifdef ANDROID_ENABLED
79
return "assets://keystores/debug.keystore";
80
#else
81
return get_data_dir().path_join("keystores/debug.keystore");
82
#endif
83
}
84
85
String EditorPaths::get_project_settings_dir() const {
86
return get_project_data_dir().path_join("editor");
87
}
88
89
String EditorPaths::get_text_editor_themes_dir() const {
90
return get_config_dir().path_join(text_editor_themes_folder);
91
}
92
93
String EditorPaths::get_script_templates_dir() const {
94
return get_config_dir().path_join(script_templates_folder);
95
}
96
97
String EditorPaths::get_project_script_templates_dir() const {
98
return GLOBAL_GET("editor/script/templates_search_path");
99
}
100
101
String EditorPaths::get_feature_profiles_dir() const {
102
return get_config_dir().path_join(feature_profiles_folder);
103
}
104
105
void EditorPaths::create() {
106
memnew(EditorPaths);
107
}
108
109
void EditorPaths::free() {
110
ERR_FAIL_NULL(singleton);
111
memdelete(singleton);
112
singleton = nullptr;
113
}
114
115
void EditorPaths::_bind_methods() {
116
ClassDB::bind_method(D_METHOD("get_data_dir"), &EditorPaths::get_data_dir);
117
ClassDB::bind_method(D_METHOD("get_config_dir"), &EditorPaths::get_config_dir);
118
ClassDB::bind_method(D_METHOD("get_cache_dir"), &EditorPaths::get_cache_dir);
119
ClassDB::bind_method(D_METHOD("is_self_contained"), &EditorPaths::is_self_contained);
120
ClassDB::bind_method(D_METHOD("get_self_contained_file"), &EditorPaths::get_self_contained_file);
121
122
ClassDB::bind_method(D_METHOD("get_project_settings_dir"), &EditorPaths::get_project_settings_dir);
123
}
124
125
EditorPaths::EditorPaths() {
126
ERR_FAIL_COND(singleton != nullptr);
127
singleton = this;
128
129
project_data_dir = ProjectSettings::get_singleton()->get_project_data_path();
130
131
// Self-contained mode if a `._sc_` or `_sc_` file is present in executable dir.
132
String exe_path = OS::get_singleton()->get_executable_path().get_base_dir();
133
Ref<DirAccess> d = DirAccess::create_for_path(exe_path);
134
if (d->file_exists(exe_path + "/._sc_")) {
135
self_contained = true;
136
self_contained_file = exe_path + "/._sc_";
137
} else if (d->file_exists(exe_path + "/_sc_")) {
138
self_contained = true;
139
self_contained_file = exe_path + "/_sc_";
140
}
141
142
// On macOS, look outside .app bundle, since .app bundle is read-only.
143
// Note: This will not work if Gatekeeper path randomization is active.
144
if (OS::get_singleton()->has_feature("macos") && exe_path.ends_with("MacOS") && exe_path.path_join("..").simplify_path().ends_with("Contents")) {
145
exe_path = exe_path.path_join("../../..").simplify_path();
146
d = DirAccess::create_for_path(exe_path);
147
if (d->file_exists(exe_path + "/._sc_")) {
148
self_contained = true;
149
self_contained_file = exe_path + "/._sc_";
150
} else if (d->file_exists(exe_path + "/_sc_")) {
151
self_contained = true;
152
self_contained_file = exe_path + "/_sc_";
153
}
154
}
155
156
String data_path;
157
String config_path;
158
String cache_path;
159
160
if (self_contained) {
161
// editor is self contained, all in same folder
162
data_path = exe_path;
163
data_dir = data_path.path_join("editor_data");
164
config_path = exe_path;
165
config_dir = data_dir;
166
cache_path = exe_path;
167
cache_dir = data_dir.path_join("cache");
168
temp_dir = data_dir.path_join("temp");
169
} else {
170
// Typically XDG_DATA_HOME or %APPDATA%.
171
data_path = OS::get_singleton()->get_data_path();
172
data_dir = data_path.path_join(OS::get_singleton()->get_godot_dir_name());
173
// Can be different from data_path e.g. on Linux or macOS.
174
config_path = OS::get_singleton()->get_config_path();
175
config_dir = config_path.path_join(OS::get_singleton()->get_godot_dir_name());
176
// Can be different from above paths, otherwise a subfolder of data_dir.
177
cache_path = OS::get_singleton()->get_cache_path();
178
if (cache_path == data_path) {
179
cache_dir = data_dir.path_join("cache");
180
} else {
181
cache_dir = cache_path.path_join(OS::get_singleton()->get_godot_dir_name());
182
}
183
temp_dir = OS::get_singleton()->get_temp_path();
184
}
185
186
paths_valid = (!data_path.is_empty() && !config_path.is_empty() && !cache_path.is_empty());
187
ERR_FAIL_COND_MSG(!paths_valid, "Editor data, config, or cache paths are invalid.");
188
189
// Validate or create each dir and its relevant subdirectories.
190
191
Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
192
193
// Data dir.
194
{
195
if (dir->change_dir(data_dir) != OK) {
196
dir->make_dir_recursive(data_dir);
197
if (dir->change_dir(data_dir) != OK) {
198
ERR_PRINT("Could not create editor data directory: " + data_dir);
199
paths_valid = false;
200
}
201
}
202
203
if (!dir->dir_exists(export_templates_folder)) {
204
dir->make_dir(export_templates_folder);
205
}
206
}
207
208
// Config dir.
209
{
210
if (dir->change_dir(config_dir) != OK) {
211
dir->make_dir_recursive(config_dir);
212
if (dir->change_dir(config_dir) != OK) {
213
ERR_PRINT("Could not create editor config directory: " + config_dir);
214
paths_valid = false;
215
}
216
}
217
218
if (!dir->dir_exists(text_editor_themes_folder)) {
219
dir->make_dir(text_editor_themes_folder);
220
}
221
if (!dir->dir_exists(script_templates_folder)) {
222
dir->make_dir(script_templates_folder);
223
}
224
if (!dir->dir_exists(feature_profiles_folder)) {
225
dir->make_dir(feature_profiles_folder);
226
}
227
}
228
229
// Cache dir.
230
{
231
if (dir->change_dir(cache_dir) != OK) {
232
dir->make_dir_recursive(cache_dir);
233
if (dir->change_dir(cache_dir) != OK) {
234
ERR_PRINT("Could not create editor cache directory: " + cache_dir);
235
paths_valid = false;
236
}
237
}
238
}
239
240
// Temporary dir.
241
{
242
if (dir->change_dir(temp_dir) != OK) {
243
dir->make_dir_recursive(temp_dir);
244
if (dir->change_dir(temp_dir) != OK) {
245
ERR_PRINT("Could not create editor temporary directory: " + temp_dir);
246
paths_valid = false;
247
}
248
}
249
}
250
251
// Validate or create project-specific editor data dir,
252
// including shader cache subdir.
253
if (Engine::get_singleton()->is_project_manager_hint() || (Main::is_cmdline_tool() && !ProjectSettings::get_singleton()->is_project_loaded())) {
254
// Nothing to create, use shared editor data dir for shader cache.
255
Engine::get_singleton()->set_shader_cache_path(data_dir);
256
} else {
257
Ref<DirAccess> dir_res = DirAccess::create(DirAccess::ACCESS_RESOURCES);
258
if (dir_res->change_dir(project_data_dir) != OK) {
259
dir_res->make_dir_recursive(project_data_dir);
260
if (dir_res->change_dir(project_data_dir) != OK) {
261
ERR_PRINT("Could not create project data directory (" + project_data_dir + ") in: " + dir_res->get_current_dir());
262
paths_valid = false;
263
}
264
}
265
266
// Check that the project data directory `.gdignore` file exists.
267
String project_data_gdignore_file_path = project_data_dir.path_join(".gdignore");
268
if (!FileAccess::exists(project_data_gdignore_file_path)) {
269
// Add an empty .gdignore file to avoid scan.
270
Ref<FileAccess> f = FileAccess::open(project_data_gdignore_file_path, FileAccess::WRITE);
271
if (f.is_valid()) {
272
f->store_line("");
273
} else {
274
ERR_PRINT("Failed to create file " + project_data_gdignore_file_path.quote() + ".");
275
}
276
}
277
278
Engine::get_singleton()->set_shader_cache_path(project_data_dir);
279
280
// Editor metadata dir.
281
if (!dir_res->dir_exists("editor")) {
282
dir_res->make_dir("editor");
283
}
284
// Imported assets dir.
285
String imported_files_path = ProjectSettings::get_singleton()->get_imported_files_path();
286
if (!dir_res->dir_exists(imported_files_path)) {
287
dir_res->make_dir(imported_files_path);
288
}
289
}
290
}
291
292