godotsharp_dirs.cpp 8.9 KB

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