Path: blob/master/editor/file_system/editor_paths.cpp
20892 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/io/file_access.h"36#include "core/os/os.h"37#include "main/main.h"3839EditorPaths *EditorPaths::singleton = nullptr;4041bool EditorPaths::are_paths_valid() const {42return paths_valid;43}4445String EditorPaths::get_data_dir() const {46return data_dir;47}4849String EditorPaths::get_config_dir() const {50return config_dir;51}5253String EditorPaths::get_cache_dir() const {54return cache_dir;55}5657String EditorPaths::get_temp_dir() const {58return temp_dir;59}6061String EditorPaths::get_project_data_dir() const {62return project_data_dir;63}6465bool EditorPaths::is_self_contained() const {66return self_contained;67}6869String EditorPaths::get_self_contained_file() const {70return self_contained_file;71}7273String EditorPaths::get_export_templates_dir() const {74return get_data_dir().path_join(export_templates_folder);75}7677String EditorPaths::get_debug_keystore_path() const {78#ifdef ANDROID_ENABLED79return "assets://keystores/debug.keystore";80#else81return get_data_dir().path_join("keystores/debug.keystore");82#endif83}8485// This returns paths like "res://.godot/editor".86String EditorPaths::get_project_settings_dir() const {87return get_project_data_dir().path_join("editor");88}8990String EditorPaths::get_text_editor_themes_dir() const {91return get_config_dir().path_join(text_editor_themes_folder);92}9394String EditorPaths::get_script_templates_dir() const {95return get_config_dir().path_join(script_templates_folder);96}9798String EditorPaths::get_project_script_templates_dir() const {99return GLOBAL_GET("editor/script/templates_search_path");100}101102String EditorPaths::get_feature_profiles_dir() const {103return get_config_dir().path_join(feature_profiles_folder);104}105106void EditorPaths::create() {107memnew(EditorPaths);108}109110void EditorPaths::free() {111ERR_FAIL_NULL(singleton);112memdelete(singleton);113singleton = nullptr;114}115116void EditorPaths::_bind_methods() {117ClassDB::bind_method(D_METHOD("get_data_dir"), &EditorPaths::get_data_dir);118ClassDB::bind_method(D_METHOD("get_config_dir"), &EditorPaths::get_config_dir);119ClassDB::bind_method(D_METHOD("get_cache_dir"), &EditorPaths::get_cache_dir);120ClassDB::bind_method(D_METHOD("is_self_contained"), &EditorPaths::is_self_contained);121ClassDB::bind_method(D_METHOD("get_self_contained_file"), &EditorPaths::get_self_contained_file);122123ClassDB::bind_method(D_METHOD("get_project_settings_dir"), &EditorPaths::get_project_settings_dir);124}125126EditorPaths::EditorPaths() {127ERR_FAIL_COND(singleton != nullptr);128singleton = this;129130project_data_dir = ProjectSettings::get_singleton()->get_project_data_path();131132// Self-contained mode if a `._sc_` or `_sc_` file is present in executable dir.133String exe_path = OS::get_singleton()->get_executable_path().get_base_dir();134Ref<DirAccess> d = DirAccess::create_for_path(exe_path);135if (d->file_exists(exe_path + "/._sc_")) {136self_contained = true;137self_contained_file = exe_path + "/._sc_";138} else if (d->file_exists(exe_path + "/_sc_")) {139self_contained = true;140self_contained_file = exe_path + "/_sc_";141}142143// On macOS, look outside .app bundle, since .app bundle is read-only.144// Note: This will not work if Gatekeeper path randomization is active.145if (OS::get_singleton()->has_feature("macos") && exe_path.ends_with("MacOS") && exe_path.path_join("..").simplify_path().ends_with("Contents")) {146exe_path = exe_path.path_join("../../..").simplify_path();147d = DirAccess::create_for_path(exe_path);148if (d->file_exists(exe_path + "/._sc_")) {149self_contained = true;150self_contained_file = exe_path + "/._sc_";151} else if (d->file_exists(exe_path + "/_sc_")) {152self_contained = true;153self_contained_file = exe_path + "/_sc_";154}155}156157String data_path;158String config_path;159String cache_path;160161if (self_contained) {162// editor is self contained, all in same folder163data_path = exe_path;164data_dir = data_path.path_join("editor_data");165config_path = exe_path;166config_dir = data_dir;167cache_path = exe_path;168cache_dir = data_dir.path_join("cache");169temp_dir = data_dir.path_join("temp");170} else {171// Typically XDG_DATA_HOME or %APPDATA%.172data_path = OS::get_singleton()->get_data_path();173data_dir = data_path.path_join(OS::get_singleton()->get_godot_dir_name());174// Can be different from data_path e.g. on Linux or macOS.175config_path = OS::get_singleton()->get_config_path();176config_dir = config_path.path_join(OS::get_singleton()->get_godot_dir_name());177// Can be different from above paths, otherwise a subfolder of data_dir.178cache_path = OS::get_singleton()->get_cache_path();179if (cache_path == data_path) {180cache_dir = data_dir.path_join("cache");181} else {182cache_dir = cache_path.path_join(OS::get_singleton()->get_godot_dir_name());183}184temp_dir = OS::get_singleton()->get_temp_path();185}186187paths_valid = (!data_path.is_empty() && !config_path.is_empty() && !cache_path.is_empty());188ERR_FAIL_COND_MSG(!paths_valid, "Editor data, config, or cache paths are invalid.");189190// Validate or create each dir and its relevant subdirectories.191192Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);193194// Data dir.195{196if (dir->change_dir(data_dir) != OK) {197dir->make_dir_recursive(data_dir);198if (dir->change_dir(data_dir) != OK) {199ERR_PRINT("Could not create editor data directory: " + data_dir);200paths_valid = false;201}202}203204if (!dir->dir_exists(export_templates_folder)) {205dir->make_dir(export_templates_folder);206}207}208209// Config dir.210{211if (dir->change_dir(config_dir) != OK) {212dir->make_dir_recursive(config_dir);213if (dir->change_dir(config_dir) != OK) {214ERR_PRINT("Could not create editor config directory: " + config_dir);215paths_valid = false;216}217}218219if (!dir->dir_exists(text_editor_themes_folder)) {220dir->make_dir(text_editor_themes_folder);221}222if (!dir->dir_exists(script_templates_folder)) {223dir->make_dir(script_templates_folder);224}225if (!dir->dir_exists(feature_profiles_folder)) {226dir->make_dir(feature_profiles_folder);227}228}229230// Cache dir.231{232if (dir->change_dir(cache_dir) != OK) {233dir->make_dir_recursive(cache_dir);234if (dir->change_dir(cache_dir) != OK) {235ERR_PRINT("Could not create editor cache directory: " + cache_dir);236paths_valid = false;237}238}239}240241// Temporary dir.242{243if (dir->change_dir(temp_dir) != OK) {244dir->make_dir_recursive(temp_dir);245if (dir->change_dir(temp_dir) != OK) {246ERR_PRINT("Could not create editor temporary directory: " + temp_dir);247paths_valid = false;248}249}250}251252// Validate or create project-specific editor data dir,253// including shader cache subdir.254if (Engine::get_singleton()->is_project_manager_hint() || (Main::is_cmdline_tool() && !ProjectSettings::get_singleton()->is_project_loaded())) {255// Nothing to create, use shared editor data dir for shader cache.256Engine::get_singleton()->set_shader_cache_path(data_dir);257} else {258Ref<DirAccess> dir_res = DirAccess::create(DirAccess::ACCESS_RESOURCES);259if (dir_res->change_dir(project_data_dir) != OK) {260dir_res->make_dir_recursive(project_data_dir);261if (dir_res->change_dir(project_data_dir) != OK) {262ERR_PRINT("Could not create project data directory (" + project_data_dir + ") in: " + dir_res->get_current_dir());263paths_valid = false;264}265}266267// Check that the project data directory `.gdignore` file exists.268String project_data_gdignore_file_path = project_data_dir.path_join(".gdignore");269if (!FileAccess::exists(project_data_gdignore_file_path)) {270// Add an empty .gdignore file to avoid scan.271Ref<FileAccess> f = FileAccess::open(project_data_gdignore_file_path, FileAccess::WRITE);272if (f.is_valid()) {273f->store_line("");274} else {275ERR_PRINT("Failed to create file " + project_data_gdignore_file_path.quote() + ".");276}277}278279Engine::get_singleton()->set_shader_cache_path(project_data_dir);280281// Editor metadata dir.282if (!dir_res->dir_exists("editor")) {283dir_res->make_dir("editor");284}285// Imported assets dir.286String imported_files_path = ProjectSettings::get_singleton()->get_imported_files_path();287if (!dir_res->dir_exists(imported_files_path)) {288dir_res->make_dir(imported_files_path);289}290}291}292293294