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