Path: blob/master/editor/file_system/editor_paths.cpp
9896 views
/**************************************************************************/1/* editor_paths.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_paths.h"3132#include "core/config/engine.h"33#include "core/config/project_settings.h"34#include "core/io/dir_access.h"35#include "core/os/os.h"36#include "main/main.h"3738EditorPaths *EditorPaths::singleton = nullptr;3940bool EditorPaths::are_paths_valid() const {41return paths_valid;42}4344String EditorPaths::get_data_dir() const {45return data_dir;46}4748String EditorPaths::get_config_dir() const {49return config_dir;50}5152String EditorPaths::get_cache_dir() const {53return cache_dir;54}5556String EditorPaths::get_temp_dir() const {57return temp_dir;58}5960String EditorPaths::get_project_data_dir() const {61return project_data_dir;62}6364bool EditorPaths::is_self_contained() const {65return self_contained;66}6768String EditorPaths::get_self_contained_file() const {69return self_contained_file;70}7172String EditorPaths::get_export_templates_dir() const {73return get_data_dir().path_join(export_templates_folder);74}7576String EditorPaths::get_debug_keystore_path() const {77#ifdef ANDROID_ENABLED78return "assets://keystores/debug.keystore";79#else80return get_data_dir().path_join("keystores/debug.keystore");81#endif82}8384String EditorPaths::get_project_settings_dir() const {85return get_project_data_dir().path_join("editor");86}8788String EditorPaths::get_text_editor_themes_dir() const {89return get_config_dir().path_join(text_editor_themes_folder);90}9192String EditorPaths::get_script_templates_dir() const {93return get_config_dir().path_join(script_templates_folder);94}9596String EditorPaths::get_project_script_templates_dir() const {97return GLOBAL_GET("editor/script/templates_search_path");98}99100String EditorPaths::get_feature_profiles_dir() const {101return get_config_dir().path_join(feature_profiles_folder);102}103104void EditorPaths::create() {105memnew(EditorPaths);106}107108void EditorPaths::free() {109ERR_FAIL_NULL(singleton);110memdelete(singleton);111singleton = nullptr;112}113114void EditorPaths::_bind_methods() {115ClassDB::bind_method(D_METHOD("get_data_dir"), &EditorPaths::get_data_dir);116ClassDB::bind_method(D_METHOD("get_config_dir"), &EditorPaths::get_config_dir);117ClassDB::bind_method(D_METHOD("get_cache_dir"), &EditorPaths::get_cache_dir);118ClassDB::bind_method(D_METHOD("is_self_contained"), &EditorPaths::is_self_contained);119ClassDB::bind_method(D_METHOD("get_self_contained_file"), &EditorPaths::get_self_contained_file);120121ClassDB::bind_method(D_METHOD("get_project_settings_dir"), &EditorPaths::get_project_settings_dir);122}123124EditorPaths::EditorPaths() {125ERR_FAIL_COND(singleton != nullptr);126singleton = this;127128project_data_dir = ProjectSettings::get_singleton()->get_project_data_path();129130// Self-contained mode if a `._sc_` or `_sc_` file is present in executable dir.131String exe_path = OS::get_singleton()->get_executable_path().get_base_dir();132Ref<DirAccess> d = DirAccess::create_for_path(exe_path);133if (d->file_exists(exe_path + "/._sc_")) {134self_contained = true;135self_contained_file = exe_path + "/._sc_";136} else if (d->file_exists(exe_path + "/_sc_")) {137self_contained = true;138self_contained_file = exe_path + "/_sc_";139}140141// On macOS, look outside .app bundle, since .app bundle is read-only.142// Note: This will not work if Gatekeeper path randomization is active.143if (OS::get_singleton()->has_feature("macos") && exe_path.ends_with("MacOS") && exe_path.path_join("..").simplify_path().ends_with("Contents")) {144exe_path = exe_path.path_join("../../..").simplify_path();145d = DirAccess::create_for_path(exe_path);146if (d->file_exists(exe_path + "/._sc_")) {147self_contained = true;148self_contained_file = exe_path + "/._sc_";149} else if (d->file_exists(exe_path + "/_sc_")) {150self_contained = true;151self_contained_file = exe_path + "/_sc_";152}153}154155String data_path;156String config_path;157String cache_path;158159if (self_contained) {160// editor is self contained, all in same folder161data_path = exe_path;162data_dir = data_path.path_join("editor_data");163config_path = exe_path;164config_dir = data_dir;165cache_path = exe_path;166cache_dir = data_dir.path_join("cache");167temp_dir = data_dir.path_join("temp");168} else {169// Typically XDG_DATA_HOME or %APPDATA%.170data_path = OS::get_singleton()->get_data_path();171data_dir = data_path.path_join(OS::get_singleton()->get_godot_dir_name());172// Can be different from data_path e.g. on Linux or macOS.173config_path = OS::get_singleton()->get_config_path();174config_dir = config_path.path_join(OS::get_singleton()->get_godot_dir_name());175// Can be different from above paths, otherwise a subfolder of data_dir.176cache_path = OS::get_singleton()->get_cache_path();177if (cache_path == data_path) {178cache_dir = data_dir.path_join("cache");179} else {180cache_dir = cache_path.path_join(OS::get_singleton()->get_godot_dir_name());181}182temp_dir = OS::get_singleton()->get_temp_path();183}184185paths_valid = (!data_path.is_empty() && !config_path.is_empty() && !cache_path.is_empty());186ERR_FAIL_COND_MSG(!paths_valid, "Editor data, config, or cache paths are invalid.");187188// Validate or create each dir and its relevant subdirectories.189190Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);191192// Data dir.193{194if (dir->change_dir(data_dir) != OK) {195dir->make_dir_recursive(data_dir);196if (dir->change_dir(data_dir) != OK) {197ERR_PRINT("Could not create editor data directory: " + data_dir);198paths_valid = false;199}200}201202if (!dir->dir_exists(export_templates_folder)) {203dir->make_dir(export_templates_folder);204}205}206207// Config dir.208{209if (dir->change_dir(config_dir) != OK) {210dir->make_dir_recursive(config_dir);211if (dir->change_dir(config_dir) != OK) {212ERR_PRINT("Could not create editor config directory: " + config_dir);213paths_valid = false;214}215}216217if (!dir->dir_exists(text_editor_themes_folder)) {218dir->make_dir(text_editor_themes_folder);219}220if (!dir->dir_exists(script_templates_folder)) {221dir->make_dir(script_templates_folder);222}223if (!dir->dir_exists(feature_profiles_folder)) {224dir->make_dir(feature_profiles_folder);225}226}227228// Cache dir.229{230if (dir->change_dir(cache_dir) != OK) {231dir->make_dir_recursive(cache_dir);232if (dir->change_dir(cache_dir) != OK) {233ERR_PRINT("Could not create editor cache directory: " + cache_dir);234paths_valid = false;235}236}237}238239// Temporary dir.240{241if (dir->change_dir(temp_dir) != OK) {242dir->make_dir_recursive(temp_dir);243if (dir->change_dir(temp_dir) != OK) {244ERR_PRINT("Could not create editor temporary directory: " + temp_dir);245paths_valid = false;246}247}248}249250// Validate or create project-specific editor data dir,251// including shader cache subdir.252if (Engine::get_singleton()->is_project_manager_hint() || (Main::is_cmdline_tool() && !ProjectSettings::get_singleton()->is_project_loaded())) {253// Nothing to create, use shared editor data dir for shader cache.254Engine::get_singleton()->set_shader_cache_path(data_dir);255} else {256Ref<DirAccess> dir_res = DirAccess::create(DirAccess::ACCESS_RESOURCES);257if (dir_res->change_dir(project_data_dir) != OK) {258dir_res->make_dir_recursive(project_data_dir);259if (dir_res->change_dir(project_data_dir) != OK) {260ERR_PRINT("Could not create project data directory (" + project_data_dir + ") in: " + dir_res->get_current_dir());261paths_valid = false;262}263}264265// Check that the project data directory `.gdignore` file exists.266String project_data_gdignore_file_path = project_data_dir.path_join(".gdignore");267if (!FileAccess::exists(project_data_gdignore_file_path)) {268// Add an empty .gdignore file to avoid scan.269Ref<FileAccess> f = FileAccess::open(project_data_gdignore_file_path, FileAccess::WRITE);270if (f.is_valid()) {271f->store_line("");272} else {273ERR_PRINT("Failed to create file " + project_data_gdignore_file_path.quote() + ".");274}275}276277Engine::get_singleton()->set_shader_cache_path(project_data_dir);278279// Editor metadata dir.280if (!dir_res->dir_exists("editor")) {281dir_res->make_dir("editor");282}283// Imported assets dir.284String imported_files_path = ProjectSettings::get_singleton()->get_imported_files_path();285if (!dir_res->dir_exists(imported_files_path)) {286dir_res->make_dir(imported_files_path);287}288}289}290291292