editor_paths.cpp 9.7 KB

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