Debian Codegraph @

modules/mono/godotsharp_dirs.cpp

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