godotsharp_dirs.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*************************************************************************/
  2. /* godotsharp_dirs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "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().plus_file("mono");
  58. } else {
  59. String settings_path;
  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. // On macOS, look outside .app bundle, since .app bundle is read-only.
  63. if (OS::get_singleton()->has_feature("macos") && exe_dir.ends_with("MacOS") && exe_dir.plus_file("..").simplify_path().ends_with("Contents")) {
  64. exe_dir = exe_dir.plus_file("../../..").simplify_path();
  65. }
  66. Ref<DirAccess> d = DirAccess::create_for_path(exe_dir);
  67. if (d->file_exists("._sc_") || d->file_exists("_sc_")) {
  68. // contain yourself
  69. settings_path = exe_dir.plus_file("editor_data");
  70. } else {
  71. settings_path = OS::get_singleton()->get_data_path().plus_file(OS::get_singleton()->get_godot_dir_name());
  72. }
  73. return settings_path.plus_file("mono");
  74. }
  75. #else
  76. return OS::get_singleton()->get_user_data_dir().plus_file("mono");
  77. #endif
  78. }
  79. class _GodotSharpDirs {
  80. public:
  81. String res_data_dir;
  82. String res_metadata_dir;
  83. String res_config_dir;
  84. String res_temp_dir;
  85. String res_temp_assemblies_base_dir;
  86. String res_temp_assemblies_dir;
  87. String mono_user_dir;
  88. String mono_logs_dir;
  89. String api_assemblies_base_dir;
  90. String api_assemblies_dir;
  91. #ifdef TOOLS_ENABLED
  92. String mono_solutions_dir;
  93. String build_logs_dir;
  94. String sln_filepath;
  95. String csproj_filepath;
  96. String data_editor_tools_dir;
  97. #else
  98. // Equivalent of res_assemblies_dir, but in the data directory rather than in 'res://'.
  99. // Only defined on export templates. Used when exporting assemblies outside of PCKs.
  100. String data_game_assemblies_dir;
  101. #endif
  102. String data_mono_etc_dir;
  103. String data_mono_lib_dir;
  104. #ifdef WINDOWS_ENABLED
  105. String data_mono_bin_dir;
  106. #endif
  107. private:
  108. _GodotSharpDirs() {
  109. res_data_dir = ProjectSettings::get_singleton()->get_project_data_path().plus_file("mono");
  110. res_metadata_dir = res_data_dir.plus_file("metadata");
  111. res_config_dir = res_data_dir.plus_file("etc").plus_file("mono");
  112. // TODO use paths from csproj
  113. res_temp_dir = res_data_dir.plus_file("temp");
  114. res_temp_assemblies_base_dir = res_temp_dir.plus_file("bin");
  115. res_temp_assemblies_dir = res_temp_assemblies_base_dir.plus_file(_get_expected_build_config());
  116. api_assemblies_base_dir = res_data_dir.plus_file("assemblies");
  117. #ifdef JAVASCRIPT_ENABLED
  118. mono_user_dir = "user://";
  119. #else
  120. mono_user_dir = _get_mono_user_dir();
  121. #endif
  122. mono_logs_dir = mono_user_dir.plus_file("mono_logs");
  123. #ifdef TOOLS_ENABLED
  124. mono_solutions_dir = mono_user_dir.plus_file("solutions");
  125. build_logs_dir = mono_user_dir.plus_file("build_logs");
  126. String appname = ProjectSettings::get_singleton()->get("application/config/name");
  127. String appname_safe = OS::get_singleton()->get_safe_dir_name(appname);
  128. if (appname_safe.is_empty()) {
  129. appname_safe = "UnnamedProject";
  130. }
  131. String base_path = ProjectSettings::get_singleton()->globalize_path("res://");
  132. sln_filepath = base_path.plus_file(appname_safe + ".sln");
  133. csproj_filepath = base_path.plus_file(appname_safe + ".csproj");
  134. #endif
  135. String exe_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  136. #ifdef TOOLS_ENABLED
  137. String data_dir_root = exe_dir.plus_file("GodotSharp");
  138. data_editor_tools_dir = data_dir_root.plus_file("Tools");
  139. api_assemblies_base_dir = data_dir_root.plus_file("Api");
  140. String data_mono_root_dir = data_dir_root.plus_file("Mono");
  141. data_mono_etc_dir = data_mono_root_dir.plus_file("etc");
  142. #ifdef ANDROID_ENABLED
  143. data_mono_lib_dir = gdmono::android::support::get_app_native_lib_dir();
  144. #else
  145. data_mono_lib_dir = data_mono_root_dir.plus_file("lib");
  146. #endif
  147. #ifdef WINDOWS_ENABLED
  148. data_mono_bin_dir = data_mono_root_dir.plus_file("bin");
  149. #endif
  150. #ifdef MACOS_ENABLED
  151. if (!DirAccess::exists(data_editor_tools_dir)) {
  152. data_editor_tools_dir = exe_dir.plus_file("../Resources/GodotSharp/Tools");
  153. }
  154. if (!DirAccess::exists(api_assemblies_base_dir)) {
  155. api_assemblies_base_dir = exe_dir.plus_file("../Resources/GodotSharp/Api");
  156. }
  157. if (!DirAccess::exists(data_mono_root_dir)) {
  158. data_mono_etc_dir = exe_dir.plus_file("../Resources/GodotSharp/Mono/etc");
  159. data_mono_lib_dir = exe_dir.plus_file("../Resources/GodotSharp/Mono/lib");
  160. }
  161. #endif
  162. #else
  163. String appname = ProjectSettings::get_singleton()->get("application/config/name");
  164. String appname_safe = OS::get_singleton()->get_safe_dir_name(appname);
  165. String data_dir_root = exe_dir.plus_file("data_" + appname_safe);
  166. if (!DirAccess::exists(data_dir_root)) {
  167. data_dir_root = exe_dir.plus_file("data_Godot");
  168. }
  169. String data_mono_root_dir = data_dir_root.plus_file("Mono");
  170. data_mono_etc_dir = data_mono_root_dir.plus_file("etc");
  171. #ifdef ANDROID_ENABLED
  172. data_mono_lib_dir = gdmono::android::support::get_app_native_lib_dir();
  173. #else
  174. data_mono_lib_dir = data_mono_root_dir.plus_file("lib");
  175. data_game_assemblies_dir = data_dir_root.plus_file("Assemblies");
  176. #endif
  177. #ifdef WINDOWS_ENABLED
  178. data_mono_bin_dir = data_mono_root_dir.plus_file("bin");
  179. #endif
  180. #ifdef MACOS_ENABLED
  181. if (!DirAccess::exists(data_mono_root_dir)) {
  182. data_mono_etc_dir = exe_dir.plus_file("../Resources/GodotSharp/Mono/etc");
  183. data_mono_lib_dir = exe_dir.plus_file("../Resources/GodotSharp/Mono/lib");
  184. }
  185. if (!DirAccess::exists(data_game_assemblies_dir)) {
  186. data_game_assemblies_dir = exe_dir.plus_file("../Resources/GodotSharp/Assemblies");
  187. }
  188. #endif
  189. #endif
  190. api_assemblies_dir = api_assemblies_base_dir.plus_file(GDMono::get_expected_api_build_config());
  191. }
  192. public:
  193. static _GodotSharpDirs &get_singleton() {
  194. static _GodotSharpDirs singleton;
  195. return singleton;
  196. }
  197. };
  198. String get_res_data_dir() {
  199. return _GodotSharpDirs::get_singleton().res_data_dir;
  200. }
  201. String get_res_metadata_dir() {
  202. return _GodotSharpDirs::get_singleton().res_metadata_dir;
  203. }
  204. String get_res_config_dir() {
  205. return _GodotSharpDirs::get_singleton().res_config_dir;
  206. }
  207. String get_res_temp_dir() {
  208. return _GodotSharpDirs::get_singleton().res_temp_dir;
  209. }
  210. String get_res_temp_assemblies_base_dir() {
  211. return _GodotSharpDirs::get_singleton().res_temp_assemblies_base_dir;
  212. }
  213. String get_res_temp_assemblies_dir() {
  214. return _GodotSharpDirs::get_singleton().res_temp_assemblies_dir;
  215. }
  216. String get_api_assemblies_dir() {
  217. return _GodotSharpDirs::get_singleton().api_assemblies_dir;
  218. }
  219. String get_api_assemblies_base_dir() {
  220. return _GodotSharpDirs::get_singleton().api_assemblies_base_dir;
  221. }
  222. String get_mono_user_dir() {
  223. return _GodotSharpDirs::get_singleton().mono_user_dir;
  224. }
  225. String get_mono_logs_dir() {
  226. return _GodotSharpDirs::get_singleton().mono_logs_dir;
  227. }
  228. #ifdef TOOLS_ENABLED
  229. String get_mono_solutions_dir() {
  230. return _GodotSharpDirs::get_singleton().mono_solutions_dir;
  231. }
  232. String get_build_logs_dir() {
  233. return _GodotSharpDirs::get_singleton().build_logs_dir;
  234. }
  235. String get_project_sln_path() {
  236. return _GodotSharpDirs::get_singleton().sln_filepath;
  237. }
  238. String get_project_csproj_path() {
  239. return _GodotSharpDirs::get_singleton().csproj_filepath;
  240. }
  241. String get_data_editor_tools_dir() {
  242. return _GodotSharpDirs::get_singleton().data_editor_tools_dir;
  243. }
  244. #else
  245. String get_data_game_assemblies_dir() {
  246. return _GodotSharpDirs::get_singleton().data_game_assemblies_dir;
  247. }
  248. #endif
  249. String get_data_mono_etc_dir() {
  250. return _GodotSharpDirs::get_singleton().data_mono_etc_dir;
  251. }
  252. String get_data_mono_lib_dir() {
  253. return _GodotSharpDirs::get_singleton().data_mono_lib_dir;
  254. }
  255. #ifdef WINDOWS_ENABLED
  256. String get_data_mono_bin_dir() {
  257. return _GodotSharpDirs::get_singleton().data_mono_bin_dir;
  258. }
  259. #endif
  260. } // namespace GodotSharpDirs