editor_paths.cpp 10 KB

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