| 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_project_data_dir() const { |
| 58 | return project_data_dir; |
| 59 | } |
| 60 | |
| 61 | bool EditorPaths::is_self_contained() const { |
| 62 | return self_contained; |
| 63 | } |
| 64 | |
| 65 | String EditorPaths::get_self_contained_file() const { |
| 66 | return self_contained_file; |
| 67 | } |
| 68 | |
| 69 | String EditorPaths::get_export_templates_dir() const { |
| 70 | return get_data_dir().path_join(export_templates_folder); |
| 71 | } |
| 72 | |
| 73 | String EditorPaths::get_project_settings_dir() const { |
| 74 | return get_project_data_dir().path_join("editor" ); |
| 75 | } |
| 76 | |
| 77 | String EditorPaths::get_text_editor_themes_dir() const { |
| 78 | return get_config_dir().path_join(text_editor_themes_folder); |
| 79 | } |
| 80 | |
| 81 | String EditorPaths::get_script_templates_dir() const { |
| 82 | return get_config_dir().path_join(script_templates_folder); |
| 83 | } |
| 84 | |
| 85 | String EditorPaths::get_project_script_templates_dir() const { |
| 86 | return GLOBAL_GET("editor/script/templates_search_path" ); |
| 87 | } |
| 88 | |
| 89 | String EditorPaths::get_feature_profiles_dir() const { |
| 90 | return get_config_dir().path_join(feature_profiles_folder); |
| 91 | } |
| 92 | |
| 93 | void EditorPaths::create() { |
| 94 | memnew(EditorPaths); |
| 95 | } |
| 96 | |
| 97 | void EditorPaths::free() { |
| 98 | ERR_FAIL_NULL(singleton); |
| 99 | memdelete(singleton); |
| 100 | } |
| 101 | |
| 102 | void EditorPaths::_bind_methods() { |
| 103 | ClassDB::bind_method(D_METHOD("get_data_dir" ), &EditorPaths::get_data_dir); |
| 104 | ClassDB::bind_method(D_METHOD("get_config_dir" ), &EditorPaths::get_config_dir); |
| 105 | ClassDB::bind_method(D_METHOD("get_cache_dir" ), &EditorPaths::get_cache_dir); |
| 106 | ClassDB::bind_method(D_METHOD("is_self_contained" ), &EditorPaths::is_self_contained); |
| 107 | ClassDB::bind_method(D_METHOD("get_self_contained_file" ), &EditorPaths::get_self_contained_file); |
| 108 | |
| 109 | ClassDB::bind_method(D_METHOD("get_project_settings_dir" ), &EditorPaths::get_project_settings_dir); |
| 110 | } |
| 111 | |
| 112 | EditorPaths::EditorPaths() { |
| 113 | ERR_FAIL_COND(singleton != nullptr); |
| 114 | singleton = this; |
| 115 | |
| 116 | project_data_dir = ProjectSettings::get_singleton()->get_project_data_path(); |
| 117 | |
| 118 | // Self-contained mode if a `._sc_` or `_sc_` file is present in executable dir. |
| 119 | String exe_path = OS::get_singleton()->get_executable_path().get_base_dir(); |
| 120 | Ref<DirAccess> d = DirAccess::create_for_path(exe_path); |
| 121 | if (d->file_exists(exe_path + "/._sc_" )) { |
| 122 | self_contained = true; |
| 123 | self_contained_file = exe_path + "/._sc_" ; |
| 124 | } else if (d->file_exists(exe_path + "/_sc_" )) { |
| 125 | self_contained = true; |
| 126 | self_contained_file = exe_path + "/_sc_" ; |
| 127 | } |
| 128 | |
| 129 | // On macOS, look outside .app bundle, since .app bundle is read-only. |
| 130 | // Note: This will not work if Gatekeeper path randomization is active. |
| 131 | if (OS::get_singleton()->has_feature("macos" ) && exe_path.ends_with("MacOS" ) && exe_path.path_join(".." ).simplify_path().ends_with("Contents" )) { |
| 132 | exe_path = exe_path.path_join("../../.." ).simplify_path(); |
| 133 | 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 | |
| 143 | String data_path; |
| 144 | String config_path; |
| 145 | String cache_path; |
| 146 | |
| 147 | if (self_contained) { |
| 148 | // editor is self contained, all in same folder |
| 149 | data_path = exe_path; |
| 150 | data_dir = data_path.path_join("editor_data" ); |
| 151 | config_path = exe_path; |
| 152 | config_dir = data_dir; |
| 153 | cache_path = exe_path; |
| 154 | cache_dir = data_dir.path_join("cache" ); |
| 155 | } else { |
| 156 | // Typically XDG_DATA_HOME or %APPDATA%. |
| 157 | data_path = OS::get_singleton()->get_data_path(); |
| 158 | data_dir = data_path.path_join(OS::get_singleton()->get_godot_dir_name()); |
| 159 | // Can be different from data_path e.g. on Linux or macOS. |
| 160 | config_path = OS::get_singleton()->get_config_path(); |
| 161 | config_dir = config_path.path_join(OS::get_singleton()->get_godot_dir_name()); |
| 162 | // Can be different from above paths, otherwise a subfolder of data_dir. |
| 163 | cache_path = OS::get_singleton()->get_cache_path(); |
| 164 | if (cache_path == data_path) { |
| 165 | cache_dir = data_dir.path_join("cache" ); |
| 166 | } else { |
| 167 | cache_dir = cache_path.path_join(OS::get_singleton()->get_godot_dir_name()); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | paths_valid = (!data_path.is_empty() && !config_path.is_empty() && !cache_path.is_empty()); |
| 172 | ERR_FAIL_COND_MSG(!paths_valid, "Editor data, config, or cache paths are invalid." ); |
| 173 | |
| 174 | // Validate or create each dir and its relevant subdirectories. |
| 175 | |
| 176 | Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 177 | |
| 178 | // Data dir. |
| 179 | { |
| 180 | if (dir->change_dir(data_dir) != OK) { |
| 181 | dir->make_dir_recursive(data_dir); |
| 182 | if (dir->change_dir(data_dir) != OK) { |
| 183 | ERR_PRINT("Could not create editor data directory: " + data_dir); |
| 184 | paths_valid = false; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (!dir->dir_exists(export_templates_folder)) { |
| 189 | dir->make_dir(export_templates_folder); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Config dir. |
| 194 | { |
| 195 | if (dir->change_dir(config_dir) != OK) { |
| 196 | dir->make_dir_recursive(config_dir); |
| 197 | if (dir->change_dir(config_dir) != OK) { |
| 198 | ERR_PRINT("Could not create editor config directory: " + config_dir); |
| 199 | paths_valid = false; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (!dir->dir_exists(text_editor_themes_folder)) { |
| 204 | dir->make_dir(text_editor_themes_folder); |
| 205 | } |
| 206 | if (!dir->dir_exists(script_templates_folder)) { |
| 207 | dir->make_dir(script_templates_folder); |
| 208 | } |
| 209 | if (!dir->dir_exists(feature_profiles_folder)) { |
| 210 | dir->make_dir(feature_profiles_folder); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Cache dir. |
| 215 | { |
| 216 | if (dir->change_dir(cache_dir) != OK) { |
| 217 | dir->make_dir_recursive(cache_dir); |
| 218 | if (dir->change_dir(cache_dir) != OK) { |
| 219 | ERR_PRINT("Could not create editor cache directory: " + cache_dir); |
| 220 | paths_valid = false; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // Validate or create project-specific editor data dir, |
| 226 | // including shader cache subdir. |
| 227 | if (Engine::get_singleton()->is_project_manager_hint() || (Main::is_cmdline_tool() && !ProjectSettings::get_singleton()->is_project_loaded())) { |
| 228 | // Nothing to create, use shared editor data dir for shader cache. |
| 229 | Engine::get_singleton()->set_shader_cache_path(data_dir); |
| 230 | } else { |
| 231 | Ref<DirAccess> dir_res = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 232 | if (dir_res->change_dir(project_data_dir) != OK) { |
| 233 | dir_res->make_dir_recursive(project_data_dir); |
| 234 | if (dir_res->change_dir(project_data_dir) != OK) { |
| 235 | ERR_PRINT("Could not create project data directory (" + project_data_dir + ") in: " + dir_res->get_current_dir()); |
| 236 | paths_valid = false; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Check that the project data directory '.gdignore' file exists |
| 241 | String project_data_gdignore_file_path = project_data_dir.path_join(".gdignore" ); |
| 242 | if (!FileAccess::exists(project_data_gdignore_file_path)) { |
| 243 | // Add an empty .gdignore file to avoid scan. |
| 244 | Ref<FileAccess> f = FileAccess::open(project_data_gdignore_file_path, FileAccess::WRITE); |
| 245 | if (f.is_valid()) { |
| 246 | f->store_line("" ); |
| 247 | } else { |
| 248 | ERR_PRINT("Failed to create file " + project_data_gdignore_file_path); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | Engine::get_singleton()->set_shader_cache_path(project_data_dir); |
| 253 | |
| 254 | // Editor metadata dir. |
| 255 | if (!dir_res->dir_exists("editor" )) { |
| 256 | dir_res->make_dir("editor" ); |
| 257 | } |
| 258 | // Imported assets dir. |
| 259 | String imported_files_path = ProjectSettings::get_singleton()->get_imported_files_path(); |
| 260 | if (!dir_res->dir_exists(imported_files_path)) { |
| 261 | dir_res->make_dir(imported_files_path); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |