godotsharp_dirs.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/os/dir_access.h"
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "core/version.h"
  36. #include "editor/editor_settings.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. #include "utils/path_utils.h"
  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 (EditorSettings::get_singleton()) {
  58. return EditorSettings::get_singleton()->get_data_dir().plus_file("mono");
  59. } else {
  60. String settings_path;
  61. String exe_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  62. DirAccessRef 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.plus_file("editor_data");
  66. } else {
  67. settings_path = OS::get_singleton()->get_data_path().plus_file(OS::get_singleton()->get_godot_dir_name());
  68. }
  69. return settings_path.plus_file("mono");
  70. }
  71. #else
  72. return OS::get_singleton()->get_user_data_dir().plus_file("mono");
  73. #endif
  74. }
  75. class _GodotSharpDirs {
  76. public:
  77. String res_data_dir;
  78. String res_metadata_dir;
  79. String res_assemblies_base_dir;
  80. String res_assemblies_dir;
  81. String res_config_dir;
  82. String res_temp_dir;
  83. String res_temp_assemblies_base_dir;
  84. String res_temp_assemblies_dir;
  85. String mono_user_dir;
  86. String mono_logs_dir;
  87. #ifdef TOOLS_ENABLED
  88. String mono_solutions_dir;
  89. String build_logs_dir;
  90. String project_assembly_name;
  91. String sln_filepath;
  92. String csproj_filepath;
  93. String data_editor_tools_dir;
  94. String data_editor_prebuilt_api_dir;
  95. #else
  96. // Equivalent of res_assemblies_dir, but in the data directory rather than in 'res://'.
  97. // Only defined on export templates. Used when exporting assemblies outside of PCKs.
  98. String data_game_assemblies_dir;
  99. #endif
  100. String data_mono_etc_dir;
  101. String data_mono_lib_dir;
  102. #ifdef WINDOWS_ENABLED
  103. String data_mono_bin_dir;
  104. #endif
  105. private:
  106. _GodotSharpDirs() {
  107. res_data_dir = "res://.mono";
  108. res_metadata_dir = res_data_dir.plus_file("metadata");
  109. res_assemblies_base_dir = res_data_dir.plus_file("assemblies");
  110. res_assemblies_dir = res_assemblies_base_dir.plus_file(GDMono::get_expected_api_build_config());
  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. #ifdef JAVASCRIPT_ENABLED
  117. mono_user_dir = "user://";
  118. #else
  119. mono_user_dir = _get_mono_user_dir();
  120. #endif
  121. mono_logs_dir = mono_user_dir.plus_file("mono_logs");
  122. #ifdef TOOLS_ENABLED
  123. mono_solutions_dir = mono_user_dir.plus_file("solutions");
  124. build_logs_dir = mono_user_dir.plus_file("build_logs");
  125. GLOBAL_DEF_RST("mono/project/assembly_name", "");
  126. GLOBAL_DEF_RST("mono/project/solution_directory", "");
  127. GLOBAL_DEF_RST("mono/project/c#_project_directory", "");
  128. project_assembly_name = ProjectSettings::get_singleton()->get("mono/project/assembly_name");
  129. if (project_assembly_name.empty()) {
  130. project_assembly_name = path::get_csharp_project_name();
  131. ProjectSettings::get_singleton()->set("mono/project/assembly_name", project_assembly_name);
  132. }
  133. String sln_parent_dir = ProjectSettings::get_singleton()->get("mono/project/solution_directory");
  134. if (sln_parent_dir.empty()) {
  135. sln_parent_dir = "res://";
  136. }
  137. String csproj_parent_dir = ProjectSettings::get_singleton()->get("mono/project/c#_project_directory");
  138. if (csproj_parent_dir.empty()) {
  139. csproj_parent_dir = "res://";
  140. }
  141. sln_filepath = ProjectSettings::get_singleton()->globalize_path(sln_parent_dir).plus_file(project_assembly_name + ".sln");
  142. csproj_filepath = ProjectSettings::get_singleton()->globalize_path(csproj_parent_dir).plus_file(project_assembly_name + ".csproj");
  143. #endif
  144. String exe_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  145. #ifdef TOOLS_ENABLED
  146. String data_dir_root = exe_dir.plus_file("GodotSharp");
  147. data_editor_tools_dir = data_dir_root.plus_file("Tools");
  148. data_editor_prebuilt_api_dir = data_dir_root.plus_file("Api");
  149. String data_mono_root_dir = data_dir_root.plus_file("Mono");
  150. data_mono_etc_dir = data_mono_root_dir.plus_file("etc");
  151. #ifdef ANDROID_ENABLED
  152. data_mono_lib_dir = gdmono::android::support::get_app_native_lib_dir();
  153. #else
  154. data_mono_lib_dir = data_mono_root_dir.plus_file("lib");
  155. #endif
  156. #ifdef WINDOWS_ENABLED
  157. data_mono_bin_dir = data_mono_root_dir.plus_file("bin");
  158. #endif
  159. #ifdef OSX_ENABLED
  160. if (!DirAccess::exists(data_editor_tools_dir)) {
  161. data_editor_tools_dir = exe_dir.plus_file("../Resources/GodotSharp/Tools");
  162. }
  163. if (!DirAccess::exists(data_editor_prebuilt_api_dir)) {
  164. data_editor_prebuilt_api_dir = exe_dir.plus_file("../Resources/GodotSharp/Api");
  165. }
  166. if (!DirAccess::exists(data_mono_root_dir)) {
  167. data_mono_etc_dir = exe_dir.plus_file("../Resources/GodotSharp/Mono/etc");
  168. data_mono_lib_dir = exe_dir.plus_file("../Resources/GodotSharp/Mono/lib");
  169. }
  170. #endif
  171. #else
  172. String appname = ProjectSettings::get_singleton()->get("application/config/name");
  173. String appname_safe = OS::get_singleton()->get_safe_dir_name(appname);
  174. String data_dir_root = exe_dir.plus_file("data_" + appname_safe);
  175. if (!DirAccess::exists(data_dir_root)) {
  176. data_dir_root = exe_dir.plus_file("data_Godot");
  177. }
  178. String data_mono_root_dir = data_dir_root.plus_file("Mono");
  179. data_mono_etc_dir = data_mono_root_dir.plus_file("etc");
  180. #ifdef ANDROID_ENABLED
  181. data_mono_lib_dir = gdmono::android::support::get_app_native_lib_dir();
  182. #else
  183. data_mono_lib_dir = data_mono_root_dir.plus_file("lib");
  184. data_game_assemblies_dir = data_dir_root.plus_file("Assemblies");
  185. #endif
  186. #ifdef WINDOWS_ENABLED
  187. data_mono_bin_dir = data_mono_root_dir.plus_file("bin");
  188. #endif
  189. #ifdef OSX_ENABLED
  190. if (!DirAccess::exists(data_mono_root_dir)) {
  191. data_mono_etc_dir = exe_dir.plus_file("../Resources/GodotSharp/Mono/etc");
  192. data_mono_lib_dir = exe_dir.plus_file("../Resources/GodotSharp/Mono/lib");
  193. }
  194. if (!DirAccess::exists(data_game_assemblies_dir)) {
  195. data_game_assemblies_dir = exe_dir.plus_file("../Resources/GodotSharp/Assemblies");
  196. }
  197. #endif
  198. #endif
  199. }
  200. _GodotSharpDirs(const _GodotSharpDirs &);
  201. _GodotSharpDirs &operator=(const _GodotSharpDirs &);
  202. public:
  203. static _GodotSharpDirs &get_singleton() {
  204. static _GodotSharpDirs singleton;
  205. return singleton;
  206. }
  207. };
  208. String get_res_data_dir() {
  209. return _GodotSharpDirs::get_singleton().res_data_dir;
  210. }
  211. String get_res_metadata_dir() {
  212. return _GodotSharpDirs::get_singleton().res_metadata_dir;
  213. }
  214. String get_res_assemblies_base_dir() {
  215. return _GodotSharpDirs::get_singleton().res_assemblies_base_dir;
  216. }
  217. String get_res_assemblies_dir() {
  218. return _GodotSharpDirs::get_singleton().res_assemblies_dir;
  219. }
  220. String get_res_config_dir() {
  221. return _GodotSharpDirs::get_singleton().res_config_dir;
  222. }
  223. String get_res_temp_dir() {
  224. return _GodotSharpDirs::get_singleton().res_temp_dir;
  225. }
  226. String get_res_temp_assemblies_base_dir() {
  227. return _GodotSharpDirs::get_singleton().res_temp_assemblies_base_dir;
  228. }
  229. String get_res_temp_assemblies_dir() {
  230. return _GodotSharpDirs::get_singleton().res_temp_assemblies_dir;
  231. }
  232. String get_mono_user_dir() {
  233. return _GodotSharpDirs::get_singleton().mono_user_dir;
  234. }
  235. String get_mono_logs_dir() {
  236. return _GodotSharpDirs::get_singleton().mono_logs_dir;
  237. }
  238. #ifdef TOOLS_ENABLED
  239. String get_mono_solutions_dir() {
  240. return _GodotSharpDirs::get_singleton().mono_solutions_dir;
  241. }
  242. String get_build_logs_dir() {
  243. return _GodotSharpDirs::get_singleton().build_logs_dir;
  244. }
  245. String get_project_assembly_name() {
  246. return _GodotSharpDirs::get_singleton().project_assembly_name;
  247. }
  248. String get_project_sln_path() {
  249. return _GodotSharpDirs::get_singleton().sln_filepath;
  250. }
  251. String get_project_csproj_path() {
  252. return _GodotSharpDirs::get_singleton().csproj_filepath;
  253. }
  254. String get_data_editor_tools_dir() {
  255. return _GodotSharpDirs::get_singleton().data_editor_tools_dir;
  256. }
  257. String get_data_editor_prebuilt_api_dir() {
  258. return _GodotSharpDirs::get_singleton().data_editor_prebuilt_api_dir;
  259. }
  260. #else
  261. String get_data_game_assemblies_dir() {
  262. return _GodotSharpDirs::get_singleton().data_game_assemblies_dir;
  263. }
  264. #endif
  265. String get_data_mono_etc_dir() {
  266. return _GodotSharpDirs::get_singleton().data_mono_etc_dir;
  267. }
  268. String get_data_mono_lib_dir() {
  269. return _GodotSharpDirs::get_singleton().data_mono_lib_dir;
  270. }
  271. #ifdef WINDOWS_ENABLED
  272. String get_data_mono_bin_dir() {
  273. return _GodotSharpDirs::get_singleton().data_mono_bin_dir;
  274. }
  275. #endif
  276. } // namespace GodotSharpDirs