summaryrefslogtreecommitdiff
path: root/modules/mono/mono_gd/gd_mono_assembly.cpp
blob: a1556bace5b2ac07d21264d72f74d95beb9a4049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*************************************************************************/
/*  gd_mono_assembly.cpp                                                 */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "gd_mono_assembly.h"

#include <mono/metadata/mono-debug.h>
#include <mono/metadata/tokentype.h>

#include "core/config/project_settings.h"
#include "core/io/file_access_pack.h"
#include "core/os/file_access.h"
#include "core/os/os.h"
#include "core/templates/list.h"

#include "../godotsharp_dirs.h"
#include "gd_mono_cache.h"
#include "gd_mono_class.h"

Vector<String> GDMonoAssembly::search_dirs;

void GDMonoAssembly::fill_search_dirs(Vector<String> &r_search_dirs, const String &p_custom_config, const String &p_custom_bcl_dir) {
	String framework_dir;

	if (!p_custom_bcl_dir.is_empty()) {
		framework_dir = p_custom_bcl_dir;
	} else if (mono_assembly_getrootdir()) {
		framework_dir = String::utf8(mono_assembly_getrootdir()).plus_file("mono").plus_file("4.5");
	}

	if (!framework_dir.is_empty()) {
		r_search_dirs.push_back(framework_dir);
		r_search_dirs.push_back(framework_dir.plus_file("Facades"));
	}

#if !defined(TOOLS_ENABLED)
	String data_game_assemblies_dir = GodotSharpDirs::get_data_game_assemblies_dir();
	if (!data_game_assemblies_dir.is_empty()) {
		r_search_dirs.push_back(data_game_assemblies_dir);
	}
#endif

	if (p_custom_config.length()) {
		r_search_dirs.push_back(GodotSharpDirs::get_res_temp_assemblies_base_dir().plus_file(p_custom_config));
	} else {
		r_search_dirs.push_back(GodotSharpDirs::get_res_temp_assemblies_dir());
	}

	if (p_custom_config.is_empty()) {
		r_search_dirs.push_back(GodotSharpDirs::get_res_assemblies_dir());
	} else {
		String api_config = p_custom_config == "ExportRelease" ? "Release" : "Debug";
		r_search_dirs.push_back(GodotSharpDirs::get_res_assemblies_base_dir().plus_file(api_config));
	}

	r_search_dirs.push_back(GodotSharpDirs::get_res_assemblies_base_dir());
	r_search_dirs.push_back(OS::get_singleton()->get_resource_dir());
	r_search_dirs.push_back(OS::get_singleton()->get_executable_path().get_base_dir());

#ifdef TOOLS_ENABLED
	r_search_dirs.push_back(GodotSharpDirs::get_data_editor_tools_dir());

	// For GodotTools to find the api assemblies
	r_search_dirs.push_back(GodotSharpDirs::get_data_editor_prebuilt_api_dir().plus_file("Debug"));
#endif
}

// This is how these assembly loading hooks work:
//
// - The 'search' hook checks if the assembly has already been loaded, to avoid loading again.
// - The 'preload' hook does the actual loading and is only called if the
//   'search' hook didn't find the assembly in the list of loaded assemblies.
// - The 'load' hook is called after the assembly has been loaded. Its job is to add the
//   assembly to the list of loaded assemblies so that the 'search' hook can look it up.

void GDMonoAssembly::assembly_load_hook(MonoAssembly *assembly, [[maybe_unused]] void *user_data) {
	String name = String::utf8(mono_assembly_name_get_name(mono_assembly_get_name(assembly)));

	MonoImage *image = mono_assembly_get_image(assembly);

	GDMonoAssembly *gdassembly = memnew(GDMonoAssembly(name, image, assembly));

#ifdef GD_MONO_HOT_RELOAD
	String path = String::utf8(mono_image_get_filename(image));
	if (FileAccess::exists(path)) {
		gdassembly->modified_time = FileAccess::get_modified_time(path);
	}
#endif

	MonoDomain *domain = mono_domain_get();
	GDMono::get_singleton()->add_assembly(domain ? mono_domain_get_id(domain) : 0, gdassembly);
}

MonoAssembly *GDMonoAssembly::assembly_search_hook(MonoAssemblyName *aname, void *user_data) {
	return GDMonoAssembly::_search_hook(aname, user_data, false);
}

MonoAssembly *GDMonoAssembly::assembly_refonly_search_hook(MonoAssemblyName *aname, void *user_data) {
	return GDMonoAssembly::_search_hook(aname, user_data, true);
}

MonoAssembly *GDMonoAssembly::assembly_preload_hook(MonoAssemblyName *aname, char **assemblies_path, void *user_data) {
	return GDMonoAssembly::_preload_hook(aname, assemblies_path, user_data, false);
}

MonoAssembly *GDMonoAssembly::assembly_refonly_preload_hook(MonoAssemblyName *aname, char **assemblies_path, void *user_data) {
	return GDMonoAssembly::_preload_hook(aname, assemblies_path, user_data, true);
}

MonoAssembly *GDMonoAssembly::_search_hook(MonoAssemblyName *aname, [[maybe_unused]] void *user_data, bool refonly) {
	String name = String::utf8(mono_assembly_name_get_name(aname));
	bool has_extension = name.ends_with(".dll") || name.ends_with(".exe");

	GDMonoAssembly *loaded_asm = GDMono::get_singleton()->get_loaded_assembly(has_extension ? name.get_basename() : name);
	if (loaded_asm) {
		return loaded_asm->get_assembly();
	}

	return nullptr;
}

MonoAssembly *GDMonoAssembly::_preload_hook(MonoAssemblyName *aname, char **, [[maybe_unused]] void *user_data, bool refonly) {
	String name = String::utf8(mono_assembly_name_get_name(aname));
	return _load_assembly_search(name, aname, refonly, search_dirs);
}

MonoAssembly *GDMonoAssembly::_load_assembly_search(const String &p_name, MonoAssemblyName *p_aname, bool p_refonly, const Vector<String> &p_search_dirs) {
	MonoAssembly *res = nullptr;
	String path;

	bool has_extension = p_name.ends_with(".dll") || p_name.ends_with(".exe");

	for (int i = 0; i < p_search_dirs.size(); i++) {
		const String &search_dir = p_search_dirs[i];

		if (has_extension) {
			path = search_dir.plus_file(p_name);
			if (FileAccess::exists(path)) {
				res = _real_load_assembly_from(path, p_refonly, p_aname);
				if (res != nullptr) {
					return res;
				}
			}
		} else {
			path = search_dir.plus_file(p_name + ".dll");
			if (FileAccess::exists(path)) {
				res = _real_load_assembly_from(path, p_refonly, p_aname);
				if (res != nullptr) {
					return res;
				}
			}

			path = search_dir.plus_file(p_name + ".exe");
			if (FileAccess::exists(path)) {
				res = _real_load_assembly_from(path, p_refonly, p_aname);
				if (res != nullptr) {
					return res;
				}
			}
		}
	}

	return nullptr;
}

String GDMonoAssembly::find_assembly(const String &p_name) {
	String path;

	bool has_extension = p_name.ends_with(".dll") || p_name.ends_with(".exe");

	for (int i = 0; i < search_dirs.size(); i++) {
		const String &search_dir = search_dirs[i];

		if (has_extension) {
			path = search_dir.plus_file(p_name);
			if (FileAccess::exists(path)) {
				return path;
			}
		} else {
			path = search_dir.plus_file(p_name + ".dll");
			if (FileAccess::exists(path)) {
				return path;
			}

			path = search_dir.plus_file(p_name + ".exe");
			if (FileAccess::exists(path)) {
				return path;
			}
		}
	}

	return String();
}

void GDMonoAssembly::initialize() {
	fill_search_dirs(search_dirs);

	mono_install_assembly_search_hook(&assembly_search_hook, nullptr);
	mono_install_assembly_refonly_search_hook(&assembly_refonly_search_hook, nullptr);
	mono_install_assembly_preload_hook(&assembly_preload_hook, nullptr);
	mono_install_assembly_refonly_preload_hook(&assembly_refonly_preload_hook, nullptr);
	mono_install_assembly_load_hook(&assembly_load_hook, nullptr);
}

MonoAssembly *GDMonoAssembly::_real_load_assembly_from(const String &p_path, bool p_refonly, MonoAssemblyName *p_aname) {
	Vector<uint8_t> data = FileAccess::get_file_as_array(p_path);
	ERR_FAIL_COND_V_MSG(data.is_empty(), nullptr, "Could read the assembly in the specified location");

	String image_filename;

#ifdef ANDROID_ENABLED
	if (p_path.begins_with("res://")) {
		image_filename = p_path.substr(6, p_path.length());
	} else {
		image_filename = ProjectSettings::get_singleton()->globalize_path(p_path);
	}
#else
	// FIXME: globalize_path does not work on exported games
	image_filename = ProjectSettings::get_singleton()->globalize_path(p_path);
#endif

	MonoImageOpenStatus status = MONO_IMAGE_OK;

	MonoImage *image = mono_image_open_from_data_with_name(
			(char *)&data[0], data.size(),
			true, &status, p_refonly,
			image_filename.utf8());

	ERR_FAIL_COND_V_MSG(status != MONO_IMAGE_OK || !image, nullptr, "Failed to open assembly image from memory: '" + p_path + "'.");

	if (p_aname != nullptr) {
		// Check assembly version
		const MonoTableInfo *table = mono_image_get_table_info(image, MONO_TABLE_ASSEMBLY);

		ERR_FAIL_NULL_V(table, nullptr);

		if (mono_table_info_get_rows(table)) {
			uint32_t cols[MONO_ASSEMBLY_SIZE];
			mono_metadata_decode_row(table, 0, cols, MONO_ASSEMBLY_SIZE);

			// Not sure about .NET's policy. We will only ensure major and minor are equal, and ignore build and revision.
			uint16_t major = cols[MONO_ASSEMBLY_MAJOR_VERSION];
			uint16_t minor = cols[MONO_ASSEMBLY_MINOR_VERSION];

			uint16_t required_minor;
			uint16_t required_major = mono_assembly_name_get_version(p_aname, &required_minor, nullptr, nullptr);

			if (required_major != 0) {
				if (major != required_major && minor != required_minor) {
					mono_image_close(image);
					return nullptr;
				}
			}
		}
	}

#ifdef DEBUG_ENABLED
	Vector<uint8_t> pdb_data;
	String pdb_path(p_path + ".pdb");

	if (!FileAccess::exists(pdb_path)) {
		pdb_path = p_path.get_basename() + ".pdb"; // without .dll

		if (!FileAccess::exists(pdb_path)) {
			goto no_pdb;
		}
	}

	pdb_data = FileAccess::get_file_as_array(pdb_path);

	// mono_debug_close_image doesn't seem to be needed
	mono_debug_open_image_from_memory(image, &pdb_data[0], pdb_data.size());

no_pdb:

#endif

	bool need_manual_load_hook = mono_image_get_assembly(image) != nullptr; // Re-using an existing image with an assembly loaded

	status = MONO_IMAGE_OK;

	MonoAssembly *assembly = mono_assembly_load_from_full(image, image_filename.utf8().get_data(), &status, p_refonly);

	ERR_FAIL_COND_V_MSG(status != MONO_IMAGE_OK || !assembly, nullptr, "Failed to load assembly for image");

	if (need_manual_load_hook) {
		// For some reason if an assembly survived domain reloading (maybe because it's referenced somewhere else),
		// the mono internal search hook don't detect it, yet mono_image_open_from_data_with_name re-uses the image
		// and assembly, and mono_assembly_load_from_full doesn't call the load hook. We need to call it manually.
		String name = String::utf8(mono_assembly_name_get_name(mono_assembly_get_name(assembly)));
		bool has_extension = name.ends_with(".dll") || name.ends_with(".exe");
		GDMonoAssembly *loaded_asm = GDMono::get_singleton()->get_loaded_assembly(has_extension ? name.get_basename() : name);
		if (!loaded_asm) {
			assembly_load_hook(assembly, nullptr);
		}
	}

	// Decrement refcount which was previously incremented by mono_image_open_from_data_with_name
	mono_image_close(image);

	return assembly;
}

void GDMonoAssembly::unload() {
	ERR_FAIL_NULL(image); // Should not be called if already unloaded

	for (Map<MonoClass *, GDMonoClass *>::Element *E = cached_raw.front(); E; E = E->next()) {
		memdelete(E->value());
	}

	cached_classes.clear();
	cached_raw.clear();

	assembly = nullptr;
	image = nullptr;
}

String GDMonoAssembly::get_path() const {
	return String::utf8(mono_image_get_filename(image));
}

bool GDMonoAssembly::has_attribute(GDMonoClass *p_attr_class) {
#ifdef DEBUG_ENABLED
	ERR_FAIL_NULL_V(p_attr_class, false);
#endif

	if (!attrs_fetched) {
		fetch_attributes();
	}

	if (!attributes) {
		return false;
	}

	return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
}

MonoObject *GDMonoAssembly::get_attribute(GDMonoClass *p_attr_class) {
#ifdef DEBUG_ENABLED
	ERR_FAIL_NULL_V(p_attr_class, nullptr);
#endif

	if (!attrs_fetched) {
		fetch_attributes();
	}

	if (!attributes) {
		return nullptr;
	}

	return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
}

void GDMonoAssembly::fetch_attributes() {
	ERR_FAIL_COND(attributes != nullptr);

	attributes = mono_custom_attrs_from_assembly(assembly);
	attrs_fetched = true;
}

GDMonoClass *GDMonoAssembly::get_class(const StringName &p_namespace, const StringName &p_name) {
	ERR_FAIL_NULL_V(image, nullptr);

	ClassKey key(p_namespace, p_name);

	GDMonoClass **match = cached_classes.getptr(key);

	if (match) {
		return *match;
	}

	MonoClass *mono_class = mono_class_from_name(image, String(p_namespace).utf8(), String(p_name).utf8());

	if (!mono_class) {
		return nullptr;
	}

	GDMonoClass *wrapped_class = memnew(GDMonoClass(p_namespace, p_name, mono_class, this));

	cached_classes[key] = wrapped_class;
	cached_raw[mono_class] = wrapped_class;

	return wrapped_class;
}

GDMonoClass *GDMonoAssembly::get_class(MonoClass *p_mono_class) {
	ERR_FAIL_NULL_V(image, nullptr);

	Map<MonoClass *, GDMonoClass *>::Element *match = cached_raw.find(p_mono_class);

	if (match) {
		return match->value();
	}

	StringName namespace_name = String::utf8(mono_class_get_namespace(p_mono_class));
	StringName class_name = String::utf8(mono_class_get_name(p_mono_class));

	GDMonoClass *wrapped_class = memnew(GDMonoClass(namespace_name, class_name, p_mono_class, this));

	cached_classes[ClassKey(namespace_name, class_name)] = wrapped_class;
	cached_raw[p_mono_class] = wrapped_class;

	return wrapped_class;
}

GDMonoAssembly *GDMonoAssembly::load(const String &p_name, MonoAssemblyName *p_aname, bool p_refonly, const Vector<String> &p_search_dirs) {
	if (GDMono::get_singleton()->get_corlib_assembly() && (p_name == "mscorlib" || p_name == "mscorlib.dll")) {
		return GDMono::get_singleton()->get_corlib_assembly();
	}

	// We need to manually call the search hook in this case, as it won't be called in the next step
	MonoAssembly *assembly = mono_assembly_invoke_search_hook(p_aname);

	if (!assembly) {
		assembly = _load_assembly_search(p_name, p_aname, p_refonly, p_search_dirs);
		if (!assembly) {
			return nullptr;
		}
	}

	GDMonoAssembly *loaded_asm = GDMono::get_singleton()->get_loaded_assembly(p_name);
	ERR_FAIL_NULL_V_MSG(loaded_asm, nullptr, "Loaded assembly missing from table. Did we not receive the load hook?");
	ERR_FAIL_COND_V(loaded_asm->get_assembly() != assembly, nullptr);

	return loaded_asm;
}

GDMonoAssembly *GDMonoAssembly::load_from(const String &p_name, const String &p_path, bool p_refonly) {
	if (p_name == "mscorlib" || p_name == "mscorlib.dll") {
		return GDMono::get_singleton()->get_corlib_assembly();
	}

	// We need to manually call the search hook in this case, as it won't be called in the next step
	MonoAssemblyName *aname = mono_assembly_name_new(p_name.utf8());
	MonoAssembly *assembly = mono_assembly_invoke_search_hook(aname);
	mono_assembly_name_free(aname);
	mono_free(aname);

	if (!assembly) {
		assembly = _real_load_assembly_from(p_path, p_refonly);
		if (!assembly) {
			return nullptr;
		}
	}

	GDMonoAssembly *loaded_asm = GDMono::get_singleton()->get_loaded_assembly(p_name);
	ERR_FAIL_NULL_V_MSG(loaded_asm, nullptr, "Loaded assembly missing from table. Did we not receive the load hook?");

	return loaded_asm;
}

GDMonoAssembly::~GDMonoAssembly() {
	if (image) {
		unload();
	}
}