少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
/**
 * \file
 * Error handling code
 *
 * Authors:
 *    Rodrigo Kumpera    (rkumpera@novell.com)
 * Copyright 2009 Novell, Inc (http://www.novell.com)
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */
#include <glib.h>
 
#include <config.h>
#include "mono-error.h"
#include "mono-error-internals.h"
 
#include <mono/metadata/exception.h>
#include <mono/metadata/exception-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/object-internals.h>
 
#define set_error_messagev() do { \
    if (!(error->full_message = g_strdup_vprintf (msg_format, args))) \
            error->flags |= MONO_ERROR_INCOMPLETE; \
} while (0)
 
#define set_error_message() do { \
    va_list args; \
    va_start (args, msg_format); \
    set_error_messagev();         \
    va_end (args); \
} while (0)
 
static void
mono_error_set_generic_errorv (MonoError *oerror, const char *name_space, const char *name, const char *msg_format, va_list args);
 
static gboolean
is_managed_exception (MonoErrorInternal *error)
{
    return (error->error_code == MONO_ERROR_EXCEPTION_INSTANCE);
}
 
static gboolean
is_boxed (MonoErrorInternal *error)
{
    return ((error->flags & MONO_ERROR_MEMPOOL_BOXED) != 0);
}
 
static void
mono_error_prepare (MonoErrorInternal *error)
{
    /* mono_error_set_* after a mono_error_cleanup without an intervening init */
    g_assert (error->error_code != MONO_ERROR_CLEANUP_CALLED_SENTINEL);
    if (error->error_code != MONO_ERROR_NONE)
        return;
 
    error->type_name = error->assembly_name = error->member_name = error->full_message = error->exception_name_space = error->exception_name = error->full_message_with_fields = error->first_argument = error->member_signature = NULL;
    error->exn.klass = NULL;
}
 
static MonoClass*
get_class (MonoErrorInternal *error)
{
#ifndef RUNTIME_IL2CPP
    MonoClass *klass = NULL;
    if (is_managed_exception (error))
        klass = mono_object_class (mono_gchandle_get_target (error->exn.instance_handle));
    else
        klass = error->exn.klass;
    return klass;
#else
    g_assert_not_reached ();
#endif
}
 
static const char*
get_type_name (MonoErrorInternal *error)
{
    if (error->type_name)
        return error->type_name;
    MonoClass *klass = get_class (error);
    if (klass)
        return klass->name;
    return "<unknown type>";
}
 
static const char*
get_assembly_name (MonoErrorInternal *error)
{
    if (error->assembly_name)
        return error->assembly_name;
    MonoClass *klass = get_class (error);
    if (klass && klass->image)
        return klass->image->name;
    return "<unknown assembly>";
}
 
void
mono_error_init_flags (MonoError *oerror, unsigned short flags)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    g_assert (sizeof (MonoError) == sizeof (MonoErrorInternal));
 
    error->error_code = MONO_ERROR_NONE;
    error->flags = flags;
}
 
/**
 * mono_error_init:
 * \param error Pointer to \c MonoError struct to initialize
 * Any function which takes a \c MonoError for purposes of reporting an error
 * is required to call either this or \c mono_error_init_flags on entry.
 */
void
mono_error_init (MonoError *error)
{
    mono_error_init_flags (error, 0);
}
 
void
mono_error_cleanup (MonoError *oerror)
{
#ifndef RUNTIME_IL2CPP
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    short int orig_error_code = error->error_code;
    gboolean free_strings = error->flags & MONO_ERROR_FREE_STRINGS;
    gboolean has_instance_handle = is_managed_exception (error);
 
    /* Two cleanups in a row without an intervening init. */
    g_assert (orig_error_code != MONO_ERROR_CLEANUP_CALLED_SENTINEL);
    /* Mempool stored error shouldn't be cleaned up */
    g_assert (!is_boxed (error));
 
    /* Mark it as cleaned up. */
    error->error_code = MONO_ERROR_CLEANUP_CALLED_SENTINEL;
    error->flags = 0;
 
    if (orig_error_code == MONO_ERROR_NONE)
        return;
 
 
    if (has_instance_handle)
        mono_gchandle_free (error->exn.instance_handle);
 
 
    g_free ((char*)error->full_message);
    g_free ((char*)error->full_message_with_fields);
    error->full_message = NULL;
    error->full_message_with_fields = NULL;
    if (!free_strings) //no memory was allocated
        return;
 
    g_free ((char*)error->type_name);
    g_free ((char*)error->assembly_name);
    g_free ((char*)error->member_name);
    g_free ((char*)error->exception_name_space);
    g_free ((char*)error->exception_name);
    g_free ((char*)error->first_argument);
    g_free ((char*)error->member_signature);
    error->type_name = error->assembly_name = error->member_name = error->exception_name_space = error->exception_name = error->first_argument = error->member_signature = NULL;
    error->exn.klass = NULL;
#else
    g_assert_not_reached ();
#endif
}
 
gboolean
mono_error_ok (MonoError *error)
{
    return error->error_code == MONO_ERROR_NONE;
}
 
void
mono_error_assert_ok_pos (MonoError *error, const char* filename, int lineno)
{
    if (mono_error_ok (error))
        return;
 
    g_error ("%s:%d: %s\n", filename, lineno, mono_error_get_message (error));
}
 
unsigned short
mono_error_get_error_code (MonoError *error)
{
    return error->error_code;
}
 
const char*
mono_error_get_exception_name (MonoError *oerror)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    if (error->error_code == MONO_ERROR_NONE)
        return NULL;
 
    return error->exception_name;
}
 
/*Return a pointer to the internal error message, might be NULL.
Caller should not release it.*/
const char*
mono_error_get_message (MonoError *oerror)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    if (error->error_code == MONO_ERROR_NONE)
        return NULL;
    if (error->full_message_with_fields)
        return error->full_message_with_fields;
 
    error->full_message_with_fields = g_strdup_printf ("%s assembly:%s type:%s member:%s signature:%s",
        error->full_message,
        get_assembly_name (error),
        get_type_name (error),
        error->member_signature,
        error->member_name ? error->member_name : "<none>");
 
    return error->full_message_with_fields ? error->full_message_with_fields : error->full_message;
}
 
/*
 * Inform that this error has heap allocated strings.
 * The strings will be duplicated if @dup_strings is TRUE
 * otherwise they will just be free'd in mono_error_cleanup.
 */
void
mono_error_dup_strings (MonoError *oerror, gboolean dup_strings)
{
#define DUP_STR(field) do { if (error->field) {\
    if (!(error->field = g_strdup (error->field))) \
        error->flags |= MONO_ERROR_INCOMPLETE; \
    }} while (0);
 
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    error->flags |= MONO_ERROR_FREE_STRINGS;
 
    if (dup_strings) {
        DUP_STR (type_name);
        DUP_STR (assembly_name);
        DUP_STR (member_name);
        DUP_STR (exception_name_space);
        DUP_STR (exception_name);
        DUP_STR (first_argument);
        DUP_STR (member_signature);
    }
#undef DUP_STR
}
 
void
mono_error_set_error (MonoError *oerror, int error_code, const char *msg_format, ...)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = error_code;
    set_error_message ();
}
 
static void
mono_error_set_assembly_name (MonoError *oerror, const char *assembly_name)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    g_assert (error->error_code != MONO_ERROR_NONE);
 
    error->assembly_name = assembly_name;
}
 
static void
mono_error_set_member_name (MonoError *oerror, const char *member_name)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    error->member_name = member_name;
}
 
static void
mono_error_set_member_signature (MonoError *oerror, const char *member_signature)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    error->member_signature = member_signature;
}
 
static void
mono_error_set_type_name (MonoError *oerror, const char *type_name)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    error->type_name = type_name;
}
 
static void
mono_error_set_class (MonoError *oerror, MonoClass *klass)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    if (is_managed_exception (error))
        return;
    error->exn.klass = klass;    
}
 
static void
mono_error_set_corlib_exception (MonoError *oerror, const char *name_space, const char *name)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    error->exception_name_space = name_space;
    error->exception_name = name;
}
 
 
void
mono_error_set_assembly_load (MonoError *oerror, const char *assembly_name, const char *msg_format, ...)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_FILE_NOT_FOUND;
    mono_error_set_assembly_name (oerror, assembly_name);
 
    set_error_message ();
}
 
 
void
mono_error_set_assembly_load_simple (MonoError *oerror, const char *assembly_name, gboolean refection_only)
{
    if (refection_only)
        mono_error_set_assembly_load (oerror, assembly_name, "Cannot resolve dependency to assembly because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.");
    else
        mono_error_set_assembly_load (oerror, assembly_name, "Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
}
 
void
mono_error_set_type_load_class (MonoError *oerror, MonoClass *klass, const char *msg_format, ...)
{
    va_list args;
    va_start (args, msg_format);
    mono_error_vset_type_load_class (oerror, klass, msg_format, args);
    va_end (args);
}
 
void
mono_error_vset_type_load_class (MonoError *oerror, MonoClass *klass, const char *msg_format, va_list args)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_TYPE_LOAD;
    mono_error_set_class (oerror, klass);
    set_error_messagev ();
}
 
/*
 * Different than other functions, this one here assumes that type_name and assembly_name to have been allocated just for us.
 * Which means mono_error_cleanup will free them.
 */
void
mono_error_set_type_load_name (MonoError *oerror, const char *type_name, const char *assembly_name, const char *msg_format, ...)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_TYPE_LOAD;
    mono_error_set_type_name (oerror, type_name);
    mono_error_set_assembly_name (oerror, assembly_name);
    mono_error_dup_strings (oerror, FALSE);
    set_error_message ();
}
 
void
mono_error_set_method_load (MonoError *oerror, MonoClass *klass, const char *method_name, const char *signature, const char *msg_format, ...)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_MISSING_METHOD;
    mono_error_set_class (oerror, klass);
    mono_error_set_member_name (oerror, method_name);
    mono_error_set_member_signature (oerror, signature);
    set_error_message ();
}
 
void
mono_error_set_field_load (MonoError *oerror, MonoClass *klass, const char *field_name, const char *msg_format, ...)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_MISSING_FIELD;
    mono_error_set_class (oerror, klass);
    mono_error_set_member_name (oerror, field_name);
    set_error_message ();    
}
 
void
mono_error_set_bad_image_name (MonoError *oerror, const char *assembly_name, const char *msg_format, ...)
{
#ifndef RUNTIME_IL2CPP
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_BAD_IMAGE;
    mono_error_set_assembly_name (oerror, assembly_name);
    set_error_message ();
#else
    g_assert_not_reached ();
#endif
}
 
void
mono_error_set_bad_image (MonoError *oerror, MonoImage *image, const char *msg_format, ...)
{
#ifndef RUNTIME_IL2CPP
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_BAD_IMAGE;
    error->assembly_name = image ? mono_image_get_name (image) : "<no_image>";
    set_error_message ();
#else
    g_assert_not_reached ();
#endif
}
 
void
mono_error_set_generic_errorv (MonoError *oerror, const char *name_space, const char *name, const char *msg_format, va_list args)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_GENERIC;
    mono_error_set_corlib_exception (oerror, name_space, name);
    set_error_messagev ();
}
 
void
mono_error_set_generic_error (MonoError *oerror, const char * name_space, const char *name, const char *msg_format, ...)
{
    va_list args;
    va_start (args, msg_format);
    mono_error_set_generic_errorv (oerror, name_space, name, msg_format, args);
    va_end (args);
}
 
/**
 * mono_error_set_not_implemented:
 *
 * System.NotImplementedException
 */
void
mono_error_set_not_implemented (MonoError *oerror, const char *msg_format, ...)
{
    va_list args;
    va_start (args, msg_format);
    mono_error_set_generic_errorv (oerror, "System", "NotImplementedException", msg_format, args);
    va_end (args);
}
 
/**
 * mono_error_set_execution_engine:
 *
 * System.ExecutionEngineException
 */
void
mono_error_set_execution_engine (MonoError *oerror, const char *msg_format, ...)
{
    va_list args;
    va_start (args, msg_format);
    mono_error_set_generic_errorv (oerror, "System", "ExecutionEngineException", msg_format, args);
    va_end (args);
}
 
/**
 * mono_error_set_not_supported:
 *
 * System.NotSupportedException
 */
void
mono_error_set_not_supported (MonoError *oerror, const char *msg_format, ...)
{
    va_list args;
    va_start (args, msg_format);
    mono_error_set_generic_errorv (oerror, "System", "NotSupportedException", msg_format, args);
    va_end (args);
}
 
/**
 * mono_error_set_invalid_operation:
 *
 * System.InvalidOperationException
 */
void
mono_error_set_invalid_operation (MonoError *oerror, const char *msg_format, ...)
{
    va_list args;
    va_start (args, msg_format);
    mono_error_set_generic_errorv (oerror, "System", "InvalidOperationException", msg_format, args);
    va_end (args);
}
 
/**
 * mono_error_set_file_not_found:
 *
 * System.IO.FileNotFoundException
 */
void
mono_error_set_file_not_found (MonoError *oerror, const char *msg_format, ...)
{
    va_list args;
    va_start (args, msg_format);
    mono_error_set_generic_errorv (oerror, "System.IO", "FileNotFoundException", msg_format, args);
    va_end (args);
}
 
void
mono_error_set_invalid_program (MonoError *oerror, const char *msg_format, ...)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    mono_error_prepare (error);
    error->error_code = MONO_ERROR_INVALID_PROGRAM;
 
    set_error_message ();
}
 
/**
 * mono_error_set_invalid_cast:
 *
 * System.InvalidCastException
 */
void
mono_error_set_invalid_cast (MonoError *oerror)
{
        mono_error_set_generic_error (oerror, "System", "InvalidCastException", "");
}
 
void
mono_error_set_exception_instance (MonoError *oerror, MonoException *exc)
{
#ifndef RUNTIME_IL2CPP
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    mono_error_prepare (error);
    error->error_code = MONO_ERROR_EXCEPTION_INSTANCE;
    error->exn.instance_handle = mono_gchandle_new (exc ? &exc->object : NULL, FALSE);
#else
    g_assert_not_reached ();
#endif
}
 
void
mono_error_set_exception_handle (MonoError *oerror, MonoExceptionHandle exc)
{
#ifndef RUNTIME_IL2CPP
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    mono_error_prepare (error);
    error->error_code = MONO_ERROR_EXCEPTION_INSTANCE;
    error->exn.instance_handle = mono_gchandle_from_handle (MONO_HANDLE_CAST(MonoObject, exc), FALSE);
#else
    g_assert_not_reached ();
#endif
}
 
void
mono_error_set_out_of_memory (MonoError *oerror, const char *msg_format, ...)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_OUT_OF_MEMORY;
 
    set_error_message ();
}
 
void
mono_error_set_argument (MonoError *oerror, const char *argument, const char *msg_format, ...)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_ARGUMENT;
    error->first_argument = argument;
 
    set_error_message ();
}
 
void
mono_error_set_argument_null (MonoError *oerror, const char *argument, const char *msg_format, ...)
{
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_ARGUMENT_NULL;
    error->first_argument = argument;
 
    set_error_message ();
}
 
void
mono_error_set_not_verifiable (MonoError *oerror, MonoMethod *method, const char *msg_format, ...)
{
#ifndef RUNTIME_IL2CPP
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
    mono_error_prepare (error);
 
    error->error_code = MONO_ERROR_NOT_VERIFIABLE;
    if (method) {
        mono_error_set_class (oerror, method->klass);
        mono_error_set_member_name (oerror, mono_method_full_name (method, 1));
    }
 
    set_error_message ();
#else
    g_assert_not_reached ();
#endif
}
 
 
/* Used by mono_error_prepare_exception - it sets its own error on mono_string_new_checked failure. */
static MonoString*
string_new_cleanup (MonoDomain *domain, const char *text)
{
    MonoError ignored_err;
    MonoString *result = mono_string_new_checked (domain, text, &ignored_err);
    mono_error_cleanup (&ignored_err);
    return result;
}
 
static MonoString*
get_type_name_as_mono_string (MonoErrorInternal *error, MonoDomain *domain, MonoError *error_out)
{
    MonoString* res = NULL;
 
    if (error->type_name) {
        res = string_new_cleanup (domain, error->type_name);
        
    } else {
        MonoClass *klass = get_class (error);
        if (klass) {
            char *name = mono_type_full_name (&klass->byval_arg);
            if (name) {
                res = string_new_cleanup (domain, name);
                g_free (name);
            }
        }
    }
    if (!res)
        mono_error_set_out_of_memory (error_out, "Could not allocate type name");
    return res;
}
 
static void
set_message_on_exception (MonoException *exception, MonoErrorInternal *error, MonoError *error_out)
{
    MonoString *msg = string_new_cleanup (mono_domain_get (), error->full_message);
    if (msg)
        MONO_OBJECT_SETREF (exception, message, msg);
    else
        mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
}
 
/*Can fail with out-of-memory*/
MonoException*
mono_error_prepare_exception (MonoError *oerror, MonoError *error_out)
{
#ifndef RUNTIME_IL2CPP
    MonoErrorInternal *error = (MonoErrorInternal*)oerror;
 
    MonoException* exception = NULL;
    MonoString *assembly_name = NULL, *type_name = NULL, *method_name = NULL, *field_name = NULL, *msg = NULL;
    MonoDomain *domain = mono_domain_get ();
 
    error_init (error_out);
 
    switch (error->error_code) {
    case MONO_ERROR_NONE:
        return NULL;
 
    case MONO_ERROR_MISSING_METHOD:
        if ((error->type_name || error->exn.klass) && error->member_name) {
            type_name = get_type_name_as_mono_string (error, domain, error_out);
            if (!mono_error_ok (error_out))
                break;
 
            method_name = string_new_cleanup (domain, error->member_name);
            if (!method_name) {
                mono_error_set_out_of_memory (error_out, "Could not allocate method name");
                break;
            }
 
            MonoString *signature = NULL;
            if (error->member_signature) {
                signature = string_new_cleanup (domain, error->member_signature);
                if (!signature) {
                    mono_error_set_out_of_memory (error_out, "Could not allocate signature");
                    break;
                }
            }
 
            MonoString *message = NULL;
            if (error->full_message && strlen (error->full_message) > 0) {
                message = string_new_cleanup (domain, error->full_message);
                if (!message) {
                    mono_error_set_out_of_memory (error_out, "Could not allocate message");
                    break;
                }
            }
            exception = mono_exception_from_name_four_strings_checked (mono_get_corlib (), "System", "MissingMethodException", type_name, method_name, signature, message, error_out);
            if (exception)
                set_message_on_exception (exception, error, error_out);
        } else {
            exception = mono_exception_from_name_msg (mono_get_corlib (), "System", "MissingMethodException", error->full_message);
        }
        break;
 
    case MONO_ERROR_MISSING_FIELD:
        if ((error->type_name || error->exn.klass) && error->member_name) {
            type_name = get_type_name_as_mono_string (error, domain, error_out);
            if (!mono_error_ok (error_out))
                break;
            
            field_name = string_new_cleanup (domain, error->member_name);
            if (!field_name) {
                mono_error_set_out_of_memory (error_out, "Could not allocate field name");
                break;
            }
            
            exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "MissingFieldException", type_name, field_name, error_out);
            if (exception)
                set_message_on_exception (exception, error, error_out);
        } else {
            exception = mono_exception_from_name_msg (mono_get_corlib (), "System", "MissingFieldException", error->full_message);
        }
        break;
 
    case MONO_ERROR_TYPE_LOAD:
        if ((error->type_name && error->assembly_name) || error->exn.klass) {
            type_name = get_type_name_as_mono_string (error, domain, error_out);
            if (!mono_error_ok (error_out))
                break;
 
            if (error->assembly_name) {
                assembly_name = string_new_cleanup (domain, error->assembly_name);
                if (!assembly_name) {
                    mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
                    break;
                }
            } else {
                assembly_name = mono_string_empty (domain);
            }
 
            exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "TypeLoadException", type_name, assembly_name, error_out);
            if (exception && error->full_message != NULL && strcmp (error->full_message, ""))
                set_message_on_exception (exception, error, error_out);
        } else {
            exception = mono_exception_from_name_msg (mono_get_corlib (), "System", "TypeLoadException", error->full_message);
        }
        break;
 
    case MONO_ERROR_FILE_NOT_FOUND:
    case MONO_ERROR_BAD_IMAGE:
        if (error->assembly_name) {
            msg = string_new_cleanup (domain, error->full_message);
            if (!msg) {
                mono_error_set_out_of_memory (error_out, "Could not allocate message");
                break;
            }
 
            if (error->assembly_name) {
                assembly_name = string_new_cleanup (domain, error->assembly_name);
                if (!assembly_name) {
                    mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
                    break;
                }
            }
 
            if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
                exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System.IO", "FileNotFoundException", msg, assembly_name, error_out);
            else
                exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "BadImageFormatException", msg, assembly_name, error_out);
        } else {
            if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
                exception = mono_exception_from_name_msg (mono_get_corlib (), "System.IO", "FileNotFoundException", error->full_message);
            else
                exception = mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", error->full_message);
        }
        break;
 
    case MONO_ERROR_OUT_OF_MEMORY:
        exception = mono_get_exception_out_of_memory ();
        break;
 
    case MONO_ERROR_ARGUMENT:
        exception = mono_get_exception_argument (error->first_argument, error->full_message);
        break;
 
    case MONO_ERROR_ARGUMENT_NULL:
        exception = mono_get_exception_argument_null (error->first_argument);
        break;
 
    case MONO_ERROR_NOT_VERIFIABLE: {
        char *type_name = NULL, *message;
        if (error->exn.klass) {
            type_name = mono_type_get_full_name (error->exn.klass);
            if (!type_name) {
                mono_error_set_out_of_memory (error_out, "Could not allocate message");
                break;
            }
        }
        message = g_strdup_printf ("Error in %s:%s %s", type_name, error->member_name, error->full_message);
        if (!message) {
            g_free (type_name);
            mono_error_set_out_of_memory (error_out, "Could not allocate message");
            break;    
        }
        exception = mono_exception_from_name_msg (mono_get_corlib (), "System.Security", "VerificationException", message);
        g_free (message);
        g_free (type_name);
        break;
    }
    case MONO_ERROR_GENERIC:
        if (!error->exception_name_space || !error->exception_name)
            mono_error_set_execution_engine (error_out, "MonoError with generic error but no exception name was supplied");
        else
            exception = mono_exception_from_name_msg (mono_get_corlib (), error->exception_name_space, error->exception_name, error->full_message);
        break;
 
    case MONO_ERROR_EXCEPTION_INSTANCE:
        exception = (MonoException*) mono_gchandle_get_target (error->exn.instance_handle);
        break;
 
    case MONO_ERROR_CLEANUP_CALLED_SENTINEL:
        mono_error_set_execution_engine (error_out, "MonoError reused after mono_error_cleanup");
        break;
 
    case MONO_ERROR_INVALID_PROGRAM: {
        gboolean lacks_message = error->flags & MONO_ERROR_INCOMPLETE;
        if (lacks_message)
            return mono_exception_from_name_msg (mono_get_corlib (), "System", "InvalidProgramException", "");
        else
            return mono_exception_from_name_msg (mono_get_corlib (), "System", "InvalidProgramException", error->full_message);
    }
    default:
        mono_error_set_execution_engine (error_out, "Invalid error-code %d", error->error_code);
    }
 
    if (!mono_error_ok (error_out))
        return NULL;
    if (!exception)
        mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
    return exception;
#else
    g_assert_not_reached ();
    return NULL;
#endif
}
 
/*
Convert this MonoError to an exception if it's faulty or return NULL.
The error object is cleant after.
*/
 
MonoException*
mono_error_convert_to_exception (MonoError *target_error)
{
    MonoError error;
    MonoException *ex;
 
    /* Mempool stored error shouldn't be cleaned up */
    g_assert (!is_boxed ((MonoErrorInternal*)target_error));
 
    if (mono_error_ok (target_error))
        return NULL;
 
    ex = mono_error_prepare_exception (target_error, &error);
    if (!mono_error_ok (&error)) {
        MonoError second_chance;
        /*Try to produce the exception for the second error. FIXME maybe we should log about the original one*/
        ex = mono_error_prepare_exception (&error, &second_chance);
 
        g_assert (mono_error_ok (&second_chance)); /*We can't reasonable handle double faults, maybe later.*/
        mono_error_cleanup (&error);
    }
    mono_error_cleanup (target_error);
    return ex;
}
 
void
mono_error_move (MonoError *dest, MonoError *src)
{
    memcpy (dest, src, sizeof (MonoErrorInternal));
    error_init (src);
}
 
/**
 * mono_error_box:
 * \param ierror The input error that will be boxed.
 * \param image The mempool of this image will hold the boxed error.
 * Creates a new boxed error in the given mempool from \c MonoError.
 * It does not alter \p ierror, so you still have to clean it up with
 * \c mono_error_cleanup or \c mono_error_convert_to_exception or another such function.
 * \returns the boxed error, or NULL if the mempool could not allocate.
 */
MonoErrorBoxed*
mono_error_box (const MonoError *ierror, MonoImage *image)
{
#ifndef RUNTIME_IL2CPP
    MonoErrorInternal *from = (MonoErrorInternal*)ierror;
    /* Don't know how to box a gchandle */
    g_assert (!is_managed_exception (from));
    MonoErrorBoxed* box = mono_image_alloc (image, sizeof (MonoErrorBoxed));
    box->image = image;
    mono_error_init_flags (&box->error, MONO_ERROR_MEMPOOL_BOXED);
    MonoErrorInternal *to = (MonoErrorInternal*)&box->error;
 
#define DUP_STR(field) do {                        \
        if (from->field) {                    \
            if (!(to->field = mono_image_strdup (image, from->field))) \
                to->flags |= MONO_ERROR_INCOMPLETE;    \
        } else {                        \
            to->field = NULL;                \
        }                            \
    } while (0)
 
    to->error_code = from->error_code;
    DUP_STR (type_name);
    DUP_STR (assembly_name);
    DUP_STR (member_name);
    DUP_STR (exception_name_space);
    DUP_STR (exception_name);
    DUP_STR (full_message);
    DUP_STR (full_message_with_fields);
    DUP_STR (first_argument);
    DUP_STR (member_signature);
    to->exn.klass = from->exn.klass;
 
#undef DUP_STR
    
    return box;
#else
    g_assert_not_reached ();
    return NULL;
#endif
}
 
 
/**
 * mono_error_set_from_boxed:
 * \param oerror The error that will be set to the contents of the box.
 * \param box A mempool-allocated error.
 * Sets the error condition in the oerror from the contents of the
 * given boxed error.  Does not alter the boxed error, so it can be
 * used in a future call to \c mono_error_set_from_boxed as needed.  The
 * \p oerror should've been previously initialized with \c mono_error_init,
 * as usual.
 * \returns TRUE on success or FALSE on failure.
 */
gboolean
mono_error_set_from_boxed (MonoError *oerror, const MonoErrorBoxed *box)
{
    MonoErrorInternal* to = (MonoErrorInternal*)oerror;
    MonoErrorInternal* from = (MonoErrorInternal*)&box->error;
    g_assert (!is_managed_exception (from));
 
    mono_error_prepare (to);
    to->flags |= MONO_ERROR_FREE_STRINGS;
#define DUP_STR(field)    do {                        \
        if (from->field) {                    \
            if (!(to->field = g_strdup (from->field)))    \
                to->flags |= MONO_ERROR_INCOMPLETE;    \
        } else {                        \
            to->field = NULL;                \
        }                            \
    } while (0)
 
    to->error_code = from->error_code;
    DUP_STR (type_name);
    DUP_STR (assembly_name);
    DUP_STR (member_name);
    DUP_STR (exception_name_space);
    DUP_STR (exception_name);
    DUP_STR (full_message);
    DUP_STR (full_message_with_fields);
    DUP_STR (first_argument);
    DUP_STR (member_signature);
    to->exn.klass = from->exn.klass;
          
#undef DUP_STR
    return (to->flags & MONO_ERROR_INCOMPLETE) == 0 ;
}