editor_paths.cpp 9.8 KB

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