godotsharp_dirs.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**************************************************************************/
  2. /* godotsharp_dirs.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 "godotsharp_dirs.h"
  31. #include "mono_gd/gd_mono.h"
  32. #include "utils/path_utils.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/os/os.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "core/version.h"
  38. #include "editor/editor_paths.h"
  39. #endif
  40. namespace GodotSharpDirs {
  41. String _get_expected_build_config() {
  42. #ifdef TOOLS_ENABLED
  43. return "Debug";
  44. #else
  45. #ifdef DEBUG_ENABLED
  46. return "ExportDebug";
  47. #else
  48. return "ExportRelease";
  49. #endif
  50. #endif
  51. }
  52. String _get_mono_user_dir() {
  53. #ifdef TOOLS_ENABLED
  54. if (EditorPaths::get_singleton()) {
  55. return EditorPaths::get_singleton()->get_data_dir().path_join("mono");
  56. } else {
  57. String settings_path = OS::get_singleton()->get_data_path().path_join(OS::get_singleton()->get_godot_dir_name());
  58. // Self-contained mode if a `._sc_` or `_sc_` file is present in executable dir.
  59. String exe_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  60. Ref<DirAccess> d = DirAccess::create_for_path(exe_dir);
  61. if (d->file_exists("._sc_") || d->file_exists("_sc_")) {
  62. // contain yourself
  63. settings_path = exe_dir.path_join("editor_data");
  64. }
  65. // On macOS, look outside .app bundle, since .app bundle is read-only.
  66. // Note: This will not work if Gatekeeper path randomization is active.
  67. if (OS::get_singleton()->has_feature("macos") && exe_dir.ends_with("MacOS") && exe_dir.path_join("..").simplify_path().ends_with("Contents")) {
  68. exe_dir = exe_dir.path_join("../../..").simplify_path();
  69. d = DirAccess::create_for_path(exe_dir);
  70. if (d->file_exists("._sc_") || d->file_exists("_sc_")) {
  71. // contain yourself
  72. settings_path = exe_dir.path_join("editor_data");
  73. }
  74. }
  75. return settings_path.path_join("mono");
  76. }
  77. #else
  78. return OS::get_singleton()->get_user_data_dir().path_join("mono");
  79. #endif
  80. }
  81. #if !TOOLS_ENABLED
  82. // This should be the equivalent of GodotTools.Utils.OS.PlatformNameMap.
  83. static const char *platform_name_map[][2] = {
  84. { "Windows", "windows" },
  85. { "macOS", "macos" },
  86. { "Linux", "linuxbsd" },
  87. { "FreeBSD", "linuxbsd" },
  88. { "NetBSD", "linuxbsd" },
  89. { "BSD", "linuxbsd" },
  90. { "Android", "android" },
  91. { "iOS", "ios" },
  92. { "Web", "web" },
  93. { nullptr, nullptr }
  94. };
  95. String _get_platform_name() {
  96. String platform_name = OS::get_singleton()->get_name();
  97. int idx = 0;
  98. while (platform_name_map[idx][0] != nullptr) {
  99. if (platform_name_map[idx][0] == platform_name) {
  100. return platform_name_map[idx][1];
  101. }
  102. idx++;
  103. }
  104. return "";
  105. }
  106. #endif
  107. class _GodotSharpDirs {
  108. public:
  109. String res_metadata_dir;
  110. String res_temp_assemblies_dir;
  111. String mono_user_dir;
  112. String api_assemblies_dir;
  113. #ifdef TOOLS_ENABLED
  114. String build_logs_dir;
  115. String data_editor_tools_dir;
  116. #endif
  117. private:
  118. _GodotSharpDirs() {
  119. String res_data_dir = ProjectSettings::get_singleton()->get_project_data_path().path_join("mono");
  120. res_metadata_dir = res_data_dir.path_join("metadata");
  121. // TODO use paths from csproj
  122. res_temp_assemblies_dir = res_data_dir.path_join("temp").path_join("bin").path_join(_get_expected_build_config());
  123. #ifdef WEB_ENABLED
  124. mono_user_dir = "user://";
  125. #else
  126. mono_user_dir = _get_mono_user_dir();
  127. #endif
  128. String exe_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  129. String res_dir = OS::get_singleton()->get_bundle_resource_dir();
  130. #ifdef TOOLS_ENABLED
  131. String data_dir_root = exe_dir.path_join("GodotSharp");
  132. data_editor_tools_dir = data_dir_root.path_join("Tools");
  133. String api_assemblies_base_dir = data_dir_root.path_join("Api");
  134. build_logs_dir = mono_user_dir.path_join("build_logs");
  135. #ifdef MACOS_ENABLED
  136. if (!DirAccess::exists(data_editor_tools_dir)) {
  137. data_editor_tools_dir = res_dir.path_join("GodotSharp").path_join("Tools");
  138. }
  139. if (!DirAccess::exists(api_assemblies_base_dir)) {
  140. api_assemblies_base_dir = res_dir.path_join("GodotSharp").path_join("Api");
  141. }
  142. #endif
  143. api_assemblies_dir = api_assemblies_base_dir.path_join(GDMono::get_expected_api_build_config());
  144. #else // TOOLS_ENABLED
  145. String platform = _get_platform_name();
  146. String arch = Engine::get_singleton()->get_architecture_name();
  147. String appname_safe = path::get_csharp_project_name();
  148. String packed_path = "res://.godot/mono/publish/" + arch;
  149. if (DirAccess::exists(packed_path)) {
  150. // The dotnet publish data is packed in the pck/zip.
  151. String data_dir_root = OS::get_singleton()->get_cache_path().path_join("data_" + appname_safe + "_" + platform + "_" + arch);
  152. bool has_data = false;
  153. if (!has_data) {
  154. // 1. Try to access the data directly.
  155. String global_packed = ProjectSettings::get_singleton()->globalize_path(packed_path);
  156. if (global_packed.is_absolute_path() && FileAccess::exists(global_packed.path_join(".dotnet-publish-manifest"))) {
  157. data_dir_root = global_packed;
  158. has_data = true;
  159. }
  160. }
  161. if (!has_data) {
  162. // 2. Check if the data was extracted before and is up-to-date.
  163. String packed_manifest = packed_path.path_join(".dotnet-publish-manifest");
  164. String extracted_manifest = data_dir_root.path_join(".dotnet-publish-manifest");
  165. if (FileAccess::exists(packed_manifest) && FileAccess::exists(extracted_manifest)) {
  166. if (FileAccess::get_file_as_bytes(packed_manifest) == FileAccess::get_file_as_bytes(extracted_manifest)) {
  167. has_data = true;
  168. }
  169. }
  170. }
  171. if (!has_data) {
  172. // 3. Extract the data to a temporary location to load from there, delete old data if it exists but is not up-to-date.
  173. Ref<DirAccess> da;
  174. if (DirAccess::exists(data_dir_root)) {
  175. da = DirAccess::open(data_dir_root);
  176. ERR_FAIL_COND(da.is_null());
  177. ERR_FAIL_COND(da->erase_contents_recursive() != OK);
  178. }
  179. da = DirAccess::create_for_path(packed_path);
  180. ERR_FAIL_COND(da.is_null());
  181. ERR_FAIL_COND(da->copy_dir(packed_path, data_dir_root) != OK);
  182. }
  183. api_assemblies_dir = data_dir_root;
  184. } else {
  185. // The dotnet publish data is in a directory next to the executable.
  186. String data_dir_root = exe_dir.path_join("data_" + appname_safe + "_" + platform + "_" + arch);
  187. #ifdef MACOS_ENABLED
  188. if (!DirAccess::exists(data_dir_root)) {
  189. data_dir_root = res_dir.path_join("data_" + appname_safe + "_" + platform + "_" + arch);
  190. }
  191. #endif
  192. api_assemblies_dir = data_dir_root;
  193. }
  194. #endif
  195. }
  196. public:
  197. static _GodotSharpDirs &get_singleton() {
  198. static _GodotSharpDirs singleton;
  199. return singleton;
  200. }
  201. };
  202. String get_res_metadata_dir() {
  203. return _GodotSharpDirs::get_singleton().res_metadata_dir;
  204. }
  205. String get_res_temp_assemblies_dir() {
  206. return _GodotSharpDirs::get_singleton().res_temp_assemblies_dir;
  207. }
  208. String get_api_assemblies_dir() {
  209. return _GodotSharpDirs::get_singleton().api_assemblies_dir;
  210. }
  211. String get_mono_user_dir() {
  212. return _GodotSharpDirs::get_singleton().mono_user_dir;
  213. }
  214. #ifdef TOOLS_ENABLED
  215. String get_build_logs_dir() {
  216. return _GodotSharpDirs::get_singleton().build_logs_dir;
  217. }
  218. String get_data_editor_tools_dir() {
  219. return _GodotSharpDirs::get_singleton().data_editor_tools_dir;
  220. }
  221. #endif
  222. } // namespace GodotSharpDirs