editor_paths.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*************************************************************************/
  2. /* editor_paths.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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" // For `is_project_manager`.
  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. void EditorPaths::create() {
  59. ERR_FAIL_COND(singleton != nullptr);
  60. memnew(EditorPaths());
  61. }
  62. void EditorPaths::free() {
  63. ERR_FAIL_COND(singleton == nullptr);
  64. memdelete(singleton);
  65. }
  66. void EditorPaths::_bind_methods() {
  67. ClassDB::bind_method(D_METHOD("get_data_dir"), &EditorPaths::get_data_dir);
  68. ClassDB::bind_method(D_METHOD("get_config_dir"), &EditorPaths::get_config_dir);
  69. ClassDB::bind_method(D_METHOD("get_cache_dir"), &EditorPaths::get_cache_dir);
  70. ClassDB::bind_method(D_METHOD("is_self_contained"), &EditorPaths::is_self_contained);
  71. ClassDB::bind_method(D_METHOD("get_self_contained_file"), &EditorPaths::get_self_contained_file);
  72. }
  73. EditorPaths::EditorPaths() {
  74. singleton = this;
  75. project_data_dir = ProjectSettings::get_singleton()->get_project_data_path();
  76. // Self-contained mode if a `._sc_` or `_sc_` file is present in executable dir.
  77. String exe_path = OS::get_singleton()->get_executable_path().get_base_dir();
  78. {
  79. DirAccessRef d = DirAccess::create_for_path(exe_path);
  80. if (d->file_exists(exe_path + "/._sc_")) {
  81. self_contained = true;
  82. self_contained_file = exe_path + "/._sc_";
  83. } else if (d->file_exists(exe_path + "/_sc_")) {
  84. self_contained = true;
  85. self_contained_file = exe_path + "/_sc_";
  86. }
  87. }
  88. String data_path;
  89. String config_path;
  90. String cache_path;
  91. if (self_contained) {
  92. // editor is self contained, all in same folder
  93. data_path = exe_path;
  94. data_dir = data_path.plus_file("editor_data");
  95. config_path = exe_path;
  96. config_dir = data_dir;
  97. cache_path = exe_path;
  98. cache_dir = data_dir.plus_file("cache");
  99. } else {
  100. // Typically XDG_DATA_HOME or %APPDATA%.
  101. data_path = OS::get_singleton()->get_data_path();
  102. data_dir = data_path.plus_file(OS::get_singleton()->get_godot_dir_name());
  103. // Can be different from data_path e.g. on Linux or macOS.
  104. config_path = OS::get_singleton()->get_config_path();
  105. config_dir = config_path.plus_file(OS::get_singleton()->get_godot_dir_name());
  106. // Can be different from above paths, otherwise a subfolder of data_dir.
  107. cache_path = OS::get_singleton()->get_cache_path();
  108. if (cache_path == data_path) {
  109. cache_dir = data_dir.plus_file("cache");
  110. } else {
  111. cache_dir = cache_path.plus_file(OS::get_singleton()->get_godot_dir_name());
  112. }
  113. }
  114. paths_valid = (data_path != "" && config_path != "" && cache_path != "");
  115. ERR_FAIL_COND_MSG(!paths_valid, "Editor data, config, or cache paths are invalid.");
  116. // Validate or create each dir and its relevant subdirectories.
  117. DirAccessRef dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  118. // Data dir.
  119. {
  120. if (dir->change_dir(data_dir) != OK) {
  121. dir->make_dir_recursive(data_dir);
  122. if (dir->change_dir(data_dir) != OK) {
  123. ERR_PRINT("Could not create editor data directory: " + data_dir);
  124. paths_valid = false;
  125. }
  126. }
  127. if (!dir->dir_exists("templates")) {
  128. dir->make_dir("templates");
  129. }
  130. }
  131. // Config dir.
  132. {
  133. if (dir->change_dir(config_dir) != OK) {
  134. dir->make_dir_recursive(config_dir);
  135. if (dir->change_dir(config_dir) != OK) {
  136. ERR_PRINT("Could not create editor config directory: " + config_dir);
  137. paths_valid = false;
  138. }
  139. }
  140. if (!dir->dir_exists("text_editor_themes")) {
  141. dir->make_dir("text_editor_themes");
  142. }
  143. if (!dir->dir_exists("script_templates")) {
  144. dir->make_dir("script_templates");
  145. }
  146. if (!dir->dir_exists("feature_profiles")) {
  147. dir->make_dir("feature_profiles");
  148. }
  149. }
  150. // Cache dir.
  151. {
  152. if (dir->change_dir(cache_dir) != OK) {
  153. dir->make_dir_recursive(cache_dir);
  154. if (dir->change_dir(cache_dir) != OK) {
  155. ERR_PRINT("Could not create editor cache directory: " + cache_dir);
  156. paths_valid = false;
  157. }
  158. }
  159. }
  160. // Validate or create project-specific editor data dir,
  161. // including shader cache subdir.
  162. if (Main::is_project_manager() || Main::is_cmdline_tool()) {
  163. // Nothing to create, use shared editor data dir for shader cache.
  164. Engine::get_singleton()->set_shader_cache_path(data_dir);
  165. } else {
  166. DirAccessRef dir_res = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  167. if (dir_res->change_dir(project_data_dir) != OK) {
  168. dir_res->make_dir_recursive(project_data_dir);
  169. if (dir_res->change_dir(project_data_dir) != OK) {
  170. ERR_PRINT("Could not create project data directory (" + project_data_dir + ") in: " + dir_res->get_current_dir());
  171. paths_valid = false;
  172. }
  173. }
  174. // Check that the project data directory '.gdignore' file exists
  175. String project_data_gdignore_file_path = project_data_dir.plus_file(".gdignore");
  176. if (!FileAccess::exists(project_data_gdignore_file_path)) {
  177. // Add an empty .gdignore file to avoid scan.
  178. FileAccessRef f = FileAccess::open(project_data_gdignore_file_path, FileAccess::WRITE);
  179. if (f) {
  180. f->store_line("");
  181. f->close();
  182. } else {
  183. ERR_PRINT("Failed to create file " + project_data_gdignore_file_path);
  184. }
  185. }
  186. Engine::get_singleton()->set_shader_cache_path(project_data_dir);
  187. // Editor metadata dir.
  188. if (!dir_res->dir_exists("editor")) {
  189. dir_res->make_dir("editor");
  190. }
  191. // Imported assets dir.
  192. String imported_files_path = ProjectSettings::get_singleton()->get_imported_files_path();
  193. if (!dir_res->dir_exists(imported_files_path)) {
  194. dir_res->make_dir(imported_files_path);
  195. }
  196. }
  197. }