editor_paths.cpp 10 KB

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