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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
/*************************************************************************/
/* gd_mono_field.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 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_field.h"
#include <mono/metadata/attrdefs.h>
#include "gd_mono_class.h"
#include "gd_mono_marshal.h"
void GDMonoField::set_value_raw(MonoObject *p_object, void *p_ptr) {
mono_field_set_value(p_object, mono_field, &p_ptr);
}
void GDMonoField::set_value_from_variant(MonoObject *p_object, const Variant &p_value) {
#define SET_FROM_STRUCT(m_type) \
{ \
GDMonoMarshal::M_##m_type from = MARSHALLED_OUT(m_type, p_value.operator ::m_type()); \
mono_field_set_value(p_object, mono_field, &from); \
}
#define SET_FROM_ARRAY(m_type) \
{ \
MonoArray *managed = GDMonoMarshal::m_type##_to_mono_array(p_value.operator ::m_type()); \
mono_field_set_value(p_object, mono_field, &managed); \
}
switch (type.type_encoding) {
case MONO_TYPE_BOOLEAN: {
MonoBoolean val = p_value.operator bool();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_CHAR: {
int16_t val = p_value.operator unsigned short();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_I1: {
int8_t val = p_value.operator signed char();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_I2: {
int16_t val = p_value.operator signed short();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_I4: {
int32_t val = p_value.operator signed int();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_I8: {
int64_t val = p_value.operator int64_t();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_U1: {
uint8_t val = p_value.operator unsigned char();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_U2: {
uint16_t val = p_value.operator unsigned short();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_U4: {
uint32_t val = p_value.operator unsigned int();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_U8: {
uint64_t val = p_value.operator uint64_t();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_R4: {
float val = p_value.operator float();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_R8: {
double val = p_value.operator double();
mono_field_set_value(p_object, mono_field, &val);
} break;
case MONO_TYPE_STRING: {
MonoString *mono_string = GDMonoMarshal::mono_string_from_godot(p_value);
mono_field_set_value(p_object, mono_field, mono_string);
} break;
case MONO_TYPE_VALUETYPE: {
GDMonoClass *tclass = type.type_class;
if (tclass == CACHED_CLASS(Vector2)) {
SET_FROM_STRUCT(Vector2);
break;
}
if (tclass == CACHED_CLASS(Rect2)) {
SET_FROM_STRUCT(Rect2);
break;
}
if (tclass == CACHED_CLASS(Transform2D)) {
SET_FROM_STRUCT(Transform2D);
break;
}
if (tclass == CACHED_CLASS(Vector3)) {
SET_FROM_STRUCT(Vector3);
break;
}
if (tclass == CACHED_CLASS(Basis)) {
SET_FROM_STRUCT(Basis);
break;
}
if (tclass == CACHED_CLASS(Quat)) {
SET_FROM_STRUCT(Quat);
break;
}
if (tclass == CACHED_CLASS(Transform)) {
SET_FROM_STRUCT(Transform);
break;
}
if (tclass == CACHED_CLASS(AABB)) {
SET_FROM_STRUCT(AABB);
break;
}
if (tclass == CACHED_CLASS(Color)) {
SET_FROM_STRUCT(Color);
break;
}
if (tclass == CACHED_CLASS(Plane)) {
SET_FROM_STRUCT(Plane);
break;
}
if (mono_class_is_enum(tclass->get_mono_ptr())) {
MonoType *enum_basetype = mono_class_enum_basetype(tclass->get_mono_ptr());
switch (mono_type_get_type(enum_basetype)) {
case MONO_TYPE_BOOLEAN: {
MonoBoolean val = p_value.operator bool();
mono_field_set_value(p_object, mono_field, &val);
break;
}
case MONO_TYPE_CHAR: {
uint16_t val = p_value.operator unsigned short();
mono_field_set_value(p_object, mono_field, &val);
break;
}
case MONO_TYPE_I1: {
int8_t val = p_value.operator signed char();
mono_field_set_value(p_object, mono_field, &val);
break;
}
case MONO_TYPE_I2: {
int16_t val = p_value.operator signed short();
mono_field_set_value(p_object, mono_field, &val);
break;
}
case MONO_TYPE_I4: {
int32_t val = p_value.operator signed int();
mono_field_set_value(p_object, mono_field, &val);
break;
}
case MONO_TYPE_I8: {
int64_t val = p_value.operator int64_t();
mono_field_set_value(p_object, mono_field, &val);
break;
}
case MONO_TYPE_U1: {
uint8_t val = p_value.operator unsigned char();
mono_field_set_value(p_object, mono_field, &val);
break;
}
case MONO_TYPE_U2: {
uint16_t val = p_value.operator unsigned short();
mono_field_set_value(p_object, mono_field, &val);
break;
}
case MONO_TYPE_U4: {
uint32_t val = p_value.operator unsigned int();
mono_field_set_value(p_object, mono_field, &val);
break;
}
case MONO_TYPE_U8: {
uint64_t val = p_value.operator uint64_t();
mono_field_set_value(p_object, mono_field, &val);
break;
}
default: {
ERR_EXPLAIN(String() + "Attempted to convert Variant to a managed enum value of unmarshallable base type.");
ERR_FAIL();
}
}
break;
}
ERR_EXPLAIN(String() + "Attempted to set the value of a field of unmarshallable type: " + tclass->get_name());
ERR_FAIL();
} break;
case MONO_TYPE_ARRAY:
case MONO_TYPE_SZARRAY: {
MonoArrayType *array_type = mono_type_get_array_type(type.type_class->get_mono_type());
if (array_type->eklass == CACHED_CLASS_RAW(MonoObject)) {
SET_FROM_ARRAY(Array);
break;
}
if (array_type->eklass == CACHED_CLASS_RAW(uint8_t)) {
SET_FROM_ARRAY(PoolByteArray);
break;
}
if (array_type->eklass == CACHED_CLASS_RAW(int32_t)) {
SET_FROM_ARRAY(PoolIntArray);
break;
}
if (array_type->eklass == REAL_T_MONOCLASS) {
SET_FROM_ARRAY(PoolRealArray);
break;
}
if (array_type->eklass == CACHED_CLASS_RAW(String)) {
SET_FROM_ARRAY(PoolStringArray);
break;
}
if (array_type->eklass == CACHED_CLASS_RAW(Vector2)) {
SET_FROM_ARRAY(PoolVector2Array);
break;
}
if (array_type->eklass == CACHED_CLASS_RAW(Vector3)) {
SET_FROM_ARRAY(PoolVector3Array);
break;
}
if (array_type->eklass == CACHED_CLASS_RAW(Color)) {
SET_FROM_ARRAY(PoolColorArray);
break;
}
ERR_EXPLAIN(String() + "Attempted to convert Variant to a managed array of unmarshallable element type.");
ERR_FAIL();
} break;
case MONO_TYPE_CLASS: {
GDMonoClass *type_class = type.type_class;
// GodotObject
if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
MonoObject *managed = GDMonoUtils::unmanaged_get_managed(p_value.operator Object *());
mono_field_set_value(p_object, mono_field, managed);
break;
}
if (CACHED_CLASS(NodePath) == type_class) {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator NodePath());
mono_field_set_value(p_object, mono_field, managed);
break;
}
if (CACHED_CLASS(RID) == type_class) {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator RID());
mono_field_set_value(p_object, mono_field, managed);
break;
}
if (CACHED_CLASS(Dictionary) == type_class) {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(), CACHED_CLASS(Dictionary));
mono_field_set_value(p_object, mono_field, managed);
break;
}
if (CACHED_CLASS(Array) == type_class) {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(), CACHED_CLASS(Array));
mono_field_set_value(p_object, mono_field, managed);
break;
}
ERR_EXPLAIN(String() + "Attempted to set the value of a field of unmarshallable type: " + type_class->get_name());
ERR_FAIL();
} break;
case MONO_TYPE_OBJECT: {
// Variant
switch (p_value.get_type()) {
case Variant::BOOL: {
MonoBoolean val = p_value.operator bool();
mono_field_set_value(p_object, mono_field, &val);
} break;
case Variant::INT: {
int32_t val = p_value.operator signed int();
mono_field_set_value(p_object, mono_field, &val);
} break;
case Variant::REAL: {
#ifdef REAL_T_IS_DOUBLE
double val = p_value.operator double();
mono_field_set_value(p_object, mono_field, &val);
#else
float val = p_value.operator float();
mono_field_set_value(p_object, mono_field, &val);
#endif
} break;
case Variant::STRING: {
MonoString *mono_string = GDMonoMarshal::mono_string_from_godot(p_value);
mono_field_set_value(p_object, mono_field, mono_string);
} break;
case Variant::VECTOR2: {
SET_FROM_STRUCT(Vector2);
} break;
case Variant::RECT2: {
SET_FROM_STRUCT(Rect2);
} break;
case Variant::VECTOR3: {
SET_FROM_STRUCT(Vector3);
} break;
case Variant::TRANSFORM2D: {
SET_FROM_STRUCT(Transform2D);
} break;
case Variant::PLANE: {
SET_FROM_STRUCT(Plane);
} break;
case Variant::QUAT: {
SET_FROM_STRUCT(Quat);
} break;
case Variant::AABB: {
SET_FROM_STRUCT(AABB);
} break;
case Variant::BASIS: {
SET_FROM_STRUCT(Basis);
} break;
case Variant::TRANSFORM: {
SET_FROM_STRUCT(Transform);
} break;
case Variant::COLOR: {
SET_FROM_STRUCT(Color);
} break;
case Variant::NODE_PATH: {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator NodePath());
mono_field_set_value(p_object, mono_field, managed);
} break;
case Variant::_RID: {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator RID());
mono_field_set_value(p_object, mono_field, managed);
} break;
case Variant::OBJECT: {
MonoObject *managed = GDMonoUtils::unmanaged_get_managed(p_value.operator Object *());
mono_field_set_value(p_object, mono_field, managed);
break;
}
case Variant::DICTIONARY: {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(), CACHED_CLASS(Dictionary));
mono_field_set_value(p_object, mono_field, managed);
} break;
case Variant::ARRAY: {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(), CACHED_CLASS(Array));
mono_field_set_value(p_object, mono_field, managed);
} break;
case Variant::POOL_BYTE_ARRAY: {
SET_FROM_ARRAY(PoolByteArray);
} break;
case Variant::POOL_INT_ARRAY: {
SET_FROM_ARRAY(PoolIntArray);
} break;
case Variant::POOL_REAL_ARRAY: {
SET_FROM_ARRAY(PoolRealArray);
} break;
case Variant::POOL_STRING_ARRAY: {
SET_FROM_ARRAY(PoolStringArray);
} break;
case Variant::POOL_VECTOR2_ARRAY: {
SET_FROM_ARRAY(PoolVector2Array);
} break;
case Variant::POOL_VECTOR3_ARRAY: {
SET_FROM_ARRAY(PoolVector3Array);
} break;
case Variant::POOL_COLOR_ARRAY: {
SET_FROM_ARRAY(PoolColorArray);
} break;
default: break;
}
} break;
case MONO_TYPE_GENERICINST: {
MonoReflectionType *reftype = mono_type_get_object(SCRIPTS_DOMAIN, type.type_class->get_mono_type());
MonoException *exc = NULL;
GDMonoUtils::IsDictionaryGenericType type_is_dict = CACHED_METHOD_THUNK(MarshalUtils, IsDictionaryGenericType);
MonoBoolean is_dict = invoke_method_thunk(type_is_dict, (MonoObject *)reftype, (MonoObject **)&exc);
UNLIKELY_UNHANDLED_EXCEPTION(exc);
if (is_dict) {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(), type.type_class);
mono_field_set_value(p_object, mono_field, managed);
break;
}
exc = NULL;
GDMonoUtils::IsArrayGenericType type_is_array = CACHED_METHOD_THUNK(MarshalUtils, IsArrayGenericType);
MonoBoolean is_array = invoke_method_thunk(type_is_array, (MonoObject *)reftype, (MonoObject **)&exc);
UNLIKELY_UNHANDLED_EXCEPTION(exc);
if (is_array) {
MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(), type.type_class);
mono_field_set_value(p_object, mono_field, managed);
break;
}
} break;
default: {
ERR_PRINTS(String() + "Attempted to set the value of a field of unexpected type encoding: " + itos(type.type_encoding));
} break;
}
#undef SET_FROM_ARRAY_AND_BREAK
#undef SET_FROM_STRUCT_AND_BREAK
}
MonoObject *GDMonoField::get_value(MonoObject *p_object) {
return mono_field_get_value_object(mono_domain_get(), mono_field, p_object);
}
bool GDMonoField::get_bool_value(MonoObject *p_object) {
return (bool)GDMonoMarshal::unbox<MonoBoolean>(get_value(p_object));
}
int GDMonoField::get_int_value(MonoObject *p_object) {
return GDMonoMarshal::unbox<int32_t>(get_value(p_object));
}
String GDMonoField::get_string_value(MonoObject *p_object) {
MonoObject *val = get_value(p_object);
return GDMonoMarshal::mono_string_to_godot((MonoString *)val);
}
bool GDMonoField::has_attribute(GDMonoClass *p_attr_class) {
ERR_FAIL_NULL_V(p_attr_class, false);
if (!attrs_fetched)
fetch_attributes();
if (!attributes)
return false;
return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
}
MonoObject *GDMonoField::get_attribute(GDMonoClass *p_attr_class) {
ERR_FAIL_NULL_V(p_attr_class, NULL);
if (!attrs_fetched)
fetch_attributes();
if (!attributes)
return NULL;
return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
}
void GDMonoField::fetch_attributes() {
ERR_FAIL_COND(attributes != NULL);
attributes = mono_custom_attrs_from_field(owner->get_mono_ptr(), mono_field);
attrs_fetched = true;
}
bool GDMonoField::is_static() {
return mono_field_get_flags(mono_field) & MONO_FIELD_ATTR_STATIC;
}
GDMonoClassMember::Visibility GDMonoField::get_visibility() {
switch (mono_field_get_flags(mono_field) & MONO_FIELD_ATTR_FIELD_ACCESS_MASK) {
case MONO_FIELD_ATTR_PRIVATE:
return GDMonoClassMember::PRIVATE;
case MONO_FIELD_ATTR_FAM_AND_ASSEM:
return GDMonoClassMember::PROTECTED_AND_INTERNAL;
case MONO_FIELD_ATTR_ASSEMBLY:
return GDMonoClassMember::INTERNAL;
case MONO_FIELD_ATTR_FAMILY:
return GDMonoClassMember::PROTECTED;
case MONO_FIELD_ATTR_PUBLIC:
return GDMonoClassMember::PUBLIC;
default:
ERR_FAIL_V(GDMonoClassMember::PRIVATE);
}
}
GDMonoField::GDMonoField(MonoClassField *p_mono_field, GDMonoClass *p_owner) {
owner = p_owner;
mono_field = p_mono_field;
name = mono_field_get_name(mono_field);
MonoType *field_type = mono_field_get_type(mono_field);
type.type_encoding = mono_type_get_type(field_type);
MonoClass *field_type_class = mono_class_from_mono_type(field_type);
type.type_class = GDMono::get_singleton()->get_class(field_type_class);
attrs_fetched = false;
attributes = NULL;
}
GDMonoField::~GDMonoField() {
if (attributes) {
mono_custom_attrs_free(attributes);
}
}
|