godotsharp_dirs.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/os/os.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "core/version.h"
  36. #include "editor/editor_paths.h"
  37. #endif
  38. #ifdef ANDROID_ENABLED
  39. #include "mono_gd/support/android_support.h"
  40. #endif
  41. #include "mono_gd/gd_mono.h"
  42. namespace GodotSharpDirs {
  43. String _get_expected_build_config() {
  44. #ifdef TOOLS_ENABLED
  45. return "Debug";
  46. #else
  47. #ifdef DEBUG_ENABLED
  48. return "ExportDebug";
  49. #else
  50. return "ExportRelease";
  51. #endif
  52. #endif
  53. }
  54. String _get_mono_user_dir() {
  55. #ifdef TOOLS_ENABLED
  56. if (EditorPaths::get_singleton()) {
  57. return EditorPaths::get_singleton()->get_data_dir().path_join("mono");
  58. } else {
  59. String settings_path = OS::get_singleton()->get_data_path().path_join(OS::get_singleton()->get_godot_dir_name());
  60. // Self-contained mode if a `._sc_` or `_sc_` file is present in executable dir.
  61. String exe_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  62. Ref<DirAccess> d = DirAccess::create_for_path(exe_dir);
  63. if (d->file_exists("._sc_") || d->file_exists("_sc_")) {
  64. // contain yourself
  65. settings_path = exe_dir.path_join("editor_data");
  66. }
  67. // On macOS, look outside .app bundle, since .app bundle is read-only.
  68. // Note: This will not work if Gatekeeper path randomization is active.
  69. if (OS::get_singleton()->has_feature("macos") && exe_dir.ends_with("MacOS") && exe_dir.path_join("..").simplify_path().ends_with("Contents")) {
  70. exe_dir = exe_dir.path_join("../../..").simplify_path();
  71. d = DirAccess::create_for_path(exe_dir);
  72. if (d->file_exists("._sc_") || d->file_exists("_sc_")) {
  73. // contain yourself
  74. settings_path = exe_dir.path_join("editor_data");
  75. }
  76. }
  77. return settings_path.path_join("mono");
  78. }
  79. #else
  80. return OS::get_singleton()->get_user_data_dir().path_join("mono");
  81. #endif
  82. }
  83. class _GodotSharpDirs {
  84. public:
  85. String res_metadata_dir;
  86. String res_temp_assemblies_dir;
  87. String mono_user_dir;
  88. String api_assemblies_dir;
  89. #ifdef TOOLS_ENABLED
  90. String build_logs_dir;
  91. String data_editor_tools_dir;
  92. #endif
  93. private:
  94. _GodotSharpDirs() {
  95. String res_data_dir = ProjectSettings::get_singleton()->get_project_data_path().path_join("mono");
  96. res_metadata_dir = res_data_dir.path_join("metadata");
  97. // TODO use paths from csproj
  98. res_temp_assemblies_dir = res_data_dir.path_join("temp").path_join("bin").path_join(_get_expected_build_config());
  99. #ifdef WEB_ENABLED
  100. mono_user_dir = "user://";
  101. #else
  102. mono_user_dir = _get_mono_user_dir();
  103. #endif
  104. String exe_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  105. String res_dir = OS::get_singleton()->get_bundle_resource_dir();
  106. #ifdef TOOLS_ENABLED
  107. String data_dir_root = exe_dir.path_join("GodotSharp");
  108. data_editor_tools_dir = data_dir_root.path_join("Tools");
  109. String api_assemblies_base_dir = data_dir_root.path_join("Api");
  110. build_logs_dir = mono_user_dir.path_join("build_logs");
  111. #ifdef MACOS_ENABLED
  112. if (!DirAccess::exists(data_editor_tools_dir)) {
  113. data_editor_tools_dir = res_dir.path_join("GodotSharp").path_join("Tools");
  114. }
  115. if (!DirAccess::exists(api_assemblies_base_dir)) {
  116. api_assemblies_base_dir = res_dir.path_join("GodotSharp").path_join("Api");
  117. }
  118. #endif
  119. api_assemblies_dir = api_assemblies_base_dir.path_join(GDMono::get_expected_api_build_config());
  120. #else // TOOLS_ENABLED
  121. String arch = Engine::get_singleton()->get_architecture_name();
  122. String appname = GLOBAL_GET("application/config/name");
  123. String appname_safe = OS::get_singleton()->get_safe_dir_name(appname);
  124. String data_dir_root = exe_dir.path_join("data_" + appname_safe + "_" + arch);
  125. if (!DirAccess::exists(data_dir_root)) {
  126. data_dir_root = exe_dir.path_join("data_Godot_" + arch);
  127. }
  128. #ifdef MACOS_ENABLED
  129. if (!DirAccess::exists(data_dir_root)) {
  130. data_dir_root = res_dir.path_join("data_" + appname_safe + "_" + arch);
  131. }
  132. if (!DirAccess::exists(data_dir_root)) {
  133. data_dir_root = res_dir.path_join("data_Godot_" + arch);
  134. }
  135. #endif
  136. api_assemblies_dir = data_dir_root;
  137. #endif
  138. }
  139. public:
  140. static _GodotSharpDirs &get_singleton() {
  141. static _GodotSharpDirs singleton;
  142. return singleton;
  143. }
  144. };
  145. String get_res_metadata_dir() {
  146. return _GodotSharpDirs::get_singleton().res_metadata_dir;
  147. }
  148. String get_res_temp_assemblies_dir() {
  149. return _GodotSharpDirs::get_singleton().res_temp_assemblies_dir;
  150. }
  151. String get_api_assemblies_dir() {
  152. return _GodotSharpDirs::get_singleton().api_assemblies_dir;
  153. }
  154. String get_mono_user_dir() {
  155. return _GodotSharpDirs::get_singleton().mono_user_dir;
  156. }
  157. #ifdef TOOLS_ENABLED
  158. String get_build_logs_dir() {
  159. return _GodotSharpDirs::get_singleton().build_logs_dir;
  160. }
  161. String get_data_editor_tools_dir() {
  162. return _GodotSharpDirs::get_singleton().data_editor_tools_dir;
  163. }
  164. #endif
  165. } // namespace GodotSharpDirs