summaryrefslogtreecommitdiff
path: root/Runtime/Mono/MonoBehaviour.cpp
blob: 3c799018576dfe80199f0698f2cffe3b5e1fa27b (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
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
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
#include "UnityPrefix.h"
#if ENABLE_SCRIPTING
#include "MonoBehaviour.h"
#include "MonoBehaviourAnimationBinding.h"
#include "Runtime/Mono/Coroutine.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Runtime/Serialize/TransferFunctions/TransferNameConversions.h"
#include "Runtime/Serialize/FileCache.h"
#include "Runtime/Scripting/ScriptingObjectWithIntPtrField.h"
#include "MonoScript.h"
#include "MonoScriptCache.h"
#include "MonoTypeSignatures.h"
#include "MonoManager.h"
#include "Runtime/BaseClasses/IsPlaying.h"
#include "Runtime/BaseClasses/MessageIdentifiers.h"
#include "Runtime/GameCode/CallDelayed.h"
#include "Runtime/BaseClasses/MessageHandler.h"
#include "tabledefs.h"
#include "Runtime/Utilities/Word.h"
#include "Runtime/IMGUI/GUIState.h"
#include "Runtime/Serialize/IterateTypeTree.h"
#include "Runtime/BaseClasses/SupportedMessageOptimization.h"
#include "Runtime/Misc/MessageParameters.h"
#include "Runtime/Camera/Camera.h"
#include "Runtime/Camera/Renderable.h"
#include "Runtime/Math/AnimationCurve.h"
#include "Runtime/Misc/AsyncOperation.h"
#include "Runtime/Camera/RenderManager.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#include "Runtime/Threads/ThreadSpecificValue.h"
#include "Runtime/Audio/AudioCustomFilter.h"
#include "Runtime/Misc/BuildSettings.h"
#include "Runtime/Scripting/Backend/ScriptingArguments.h"
#include "Runtime/Scripting/Backend/ScriptingMethodRegistry.h"
#include "Runtime/Physics2D/CollisionListener2D.h"
#include "Runtime/Interfaces/IPhysics.h"

#if ENABLE_WWW
#include "Runtime/Export/WWW.h"
#endif
#include "Runtime/Serialize/TransferUtility.h"
#include "Runtime/BaseClasses/RefCounted.h"
#include "Runtime/Misc/InputEvent.h"
#include "Runtime/IMGUI/GUIManager.h"
#include "Runtime/Profiler/ExternalGraphicsProfiler.h"
#if UNITY_EDITOR
#include "Runtime/Serialize/PersistentManager.h"
#include "Runtime/Graphics/Transform.h"
#include "Editor/Src/Gizmos/GizmoRenderer.h"
#include "Editor/Src/Gizmos/GizmoUtil.h"
#include "Editor/Src/Application.h"
#include "Editor/Src/AssetPipeline/LogicGraphCompilationPipeline.h"
#endif

#include "Runtime/Scripting/Backend/ScriptingBackendApi.h"
#include "Runtime/Scripting/Scripting.h"

#include "Runtime/Interfaces/IAudio.h"

IMPLEMENT_CLASS_HAS_INIT (MonoBehaviour)

using namespace std;
#define DEBUG_COROUTINE 0
#define DEBUG_COROUTINE_LEAK 0
#if DEBUG_COROUTINE_LEAK
int gCoroutineCounter = 0;
#endif


PROFILER_INFORMATION(gMonoImageFxProfile, "Camera.ImageEffect", kProfilerRender);


bool IsInstanceValid (ScriptingObjectPtr target);

#if UNITY_EDITOR
BackupState::BackupState ()
:	yamlState(NULL)
,	inYamlFormat (false)
,	loadedFromDisk (false)
{}

BackupState::~BackupState ()
{
	delete yamlState;
}

#endif

MonoBehaviour::MonoBehaviour (MemLabelId label, ObjectCreationMode mode)
:	Super(label, mode)
,	m_UpdateNode(this)
,	m_FixedUpdateNode(this)
,	m_LateUpdateNode(this)
,	m_GUINode(this)
,	m_OnRenderObjectNode(this)
{
	m_Methods = NULL;
	m_ScriptCache = NULL;
	m_DidAwake = m_DidStart = m_IsDestroying = false;
	m_UseGUILayout = true;
	m_GUIState = NULL;
	#if UNITY_EDITOR
	m_EditorHideFlags = 0;
	m_Backup = NULL;
	#endif
	#if ENABLE_AUDIO_FMOD 
	m_AudioCustomFilter = NULL;
	#endif
}

MonoBehaviour::~MonoBehaviour()
{
	Assert(m_ActiveCoroutines.empty());

	LockObjectCreation();
	ReleaseMonoInstance ();
	StopAllCoroutines ();
	UnlockObjectCreation();
	
#if UNITY_EDITOR
	delete m_Backup;
#endif
#if ENABLE_UNITYGUI
	delete m_GUIState;
#endif

#if ENABLE_AUDIO_FMOD
	delete m_AudioCustomFilter;
#endif
}

bool MonoBehaviour::IsScriptableObject()
{
	if (m_ScriptCache != NULL)
		return m_ScriptCache->scriptType == kScriptTypeScriptableObjectDerived || m_ScriptCache->scriptType == kScriptTypeEditorScriptableObjectDerived;
	else
		return false;
}

#if ENABLE_UNITYGUI
ObjectGUIState& MonoBehaviour::GetObjectGUIState()
{
	if (m_GUIState)
		return *m_GUIState;
	else
	{
		m_GUIState = new ObjectGUIState ();
		return *m_GUIState;
	}
}
#endif

bool MonoBehaviour::IsPlayingOrAllowExecuteInEditMode() const
{
	#if UNITY_EDITOR
	return IsWorldPlaying () || GetRunInEditMode();
	#else
	return true;
	#endif
}

bool MonoBehaviour::WillUnloadScriptableObject ()
{
	MonoScript* script = m_Script;
	if (GetInstance() == SCRIPTING_NULL || script == NULL)
		return true;
	
	///@TODO: SHould this check for m_DidAwake? Can this happen in any way?
	
	ScriptingObjectPtr instance = GetInstance();
	if (IsScriptableObject())
	{
		ScriptingMethodPtr method = m_Methods[MonoScriptCache::kRemoveFromManager];
		if (method != SCRIPTING_NULL)
			CallMethodInactive(method);
		
		if (IsInstanceValid(instance))
		{
			method = m_Methods[MonoScriptCache::kRemoveFromManagerInternal];
      		if (method != SCRIPTING_NULL)
				CallMethodInactive(method);
		}
	}
	return IsInstanceValid(instance);
}


// IsInstanceValid is used to determine if the C# side destroyed the object during the script callback using DestroyImmediate.
// For example Awake and OnEnable are called from the same C++ code. If the object is destroyed by Awake, we must not call OnEnable and not touch MonoBehaviour member variables.
bool IsInstanceValid (ScriptingObjectPtr target)
{
	if (target == SCRIPTING_NULL)
		return false;
	ScriptingObjectOfType<Object> wrapper(target);
	return wrapper.GetCachedPtr() != NULL;
}

void MonoBehaviour::DeprecatedAddToManager ()
{
	ScriptingObjectPtr instance = GetInstance();
	if (instance && IsPlayingOrAllowExecuteInEditMode ())
	{
		MonoScript* script = m_Script;
		int executionOrder = script ? script->GetExecutionOrder() : 0;
		///Try removing this
		if ((IsInstanceValid (instance) && m_Methods[MonoScriptCache::kCoroutineStart]) || m_Methods[MonoScriptCache::kCoroutineMain])
			CallDelayed (DelayedStartCall, this, -10, NULL, 0.0F, NULL, DelayedCallManager::kRunDynamicFrameRate | DelayedCallManager::kRunFixedFrameRate | DelayedCallManager::kRunStartupFrame);
		if (IsInstanceValid (instance) && m_Methods[MonoScriptCache::kUpdate])
			GetBehaviourManager().AddBehaviour (m_UpdateNode, executionOrder);
		if (IsInstanceValid (instance) && m_Methods[MonoScriptCache::kFixedUpdate])
			GetFixedBehaviourManager().AddBehaviour (m_FixedUpdateNode, executionOrder);
		if (IsInstanceValid (instance) && m_Methods[MonoScriptCache::kLateUpdate])
			GetLateBehaviourManager().AddBehaviour (m_LateUpdateNode, executionOrder);
		
		if (IsInstanceValid (instance) && m_Methods[MonoScriptCache::kRenderObject])
			GetRenderManager().AddOnRenderObject (m_OnRenderObjectNode);
		
		if (IsInstanceValid (instance) && m_Methods[MonoScriptCache::kAddToManager])
		{
			// @TODO: This is only for backwards compatiblity
			SetupAwake ();
			
			// If we got disabled or destroyed from a call to Awake, do not proceed calling OnEnable and so on.
			// Just quit, we are removed from all managers by a call to RemoveFromManager already.
			if( !IsInstanceValid (instance) ||  !GetEnabled() )
				return;
			CallMethodIfAvailable (MonoScriptCache::kAddToManager);
		}
		
#if ENABLE_IMAGEEFFECTS
		if (IsInstanceValid (instance) && m_Methods[MonoScriptCache::kRenderImageFilter])
		{
			Camera *camera = QueryComponent (Camera);
			if (camera)
			{
				bool afterOpaque = scripting_method_has_attribute(m_Methods[MonoScriptCache::kRenderImageFilter], MONO_COMMON.imageEffectOpaque);
				bool transformsToLDR = scripting_method_has_attribute(m_Methods[MonoScriptCache::kRenderImageFilter], MONO_COMMON.imageEffectTransformsToLDR);
				
				ImageFilter filter (this, &RenderImageFilter, transformsToLDR, afterOpaque);
				camera->AddImageFilter (filter);
			}
		}
#endif
		
#if ENABLE_UNITYGUI
		if (IsInstanceValid (instance) && m_Methods[MonoScriptCache::kGUI])
			GetGUIManager().AddGUIScript (m_GUINode);
#endif
		
		if ( IsInstanceValid (instance) )
			SetByPassOnDSP(false);
	}
}

void MonoBehaviour::WillDestroyComponent ()
{
	Super::WillDestroyComponent();

	if (m_IsDestroying)
	{
		ErrorString("DestroyImmediate should not be called on the same game object when destroying a MonoBehaviour");
		return;
	}
	
	m_IsDestroying = true;
	ScriptingMethodPtr method;
	
	ScriptingObjectPtr instance = GetInstance();
	if (instance && m_DidAwake)
	{
		// OnDisable must be called for scriptable objects since we never get a Deactivate call there.
		if (IsScriptableObject())
		{
			method = m_Methods[MonoScriptCache::kRemoveFromManager];
			if (method)
				CallMethodInactive (method);
			
			if (IsInstanceValid(instance))
			{
				method = m_Methods[MonoScriptCache::kRemoveFromManagerInternal];
				if (method)
					CallMethodInactive (method);
			}
		}

		// OnDestroy
		if (IsInstanceValid(instance))
		{
			method = m_Methods[MonoScriptCache::kOnDestroy];
			if (method != SCRIPTING_NULL)
				CallMethodInactive(method);
		}
	}
}


void MonoBehaviour::RemoveFromManager ()
{
	m_UpdateNode.RemoveFromList();
	m_FixedUpdateNode.RemoveFromList();
	m_LateUpdateNode.RemoveFromList();
	m_GUINode.RemoveFromList();
	m_OnRenderObjectNode.RemoveFromList();
	
#if ENABLE_IMAGEEFFECTS
	if (GetInstance() && m_Methods[MonoScriptCache::kRenderImageFilter])
	{
		Camera *camera = QueryComponent (Camera);
		if (camera)
		{
			ImageFilter filter (this, &RenderImageFilter, false, false);
			camera->RemoveImageFilter (filter);
		}
	}
#endif
	
	if (IsPlayingOrAllowExecuteInEditMode() && GetCachedScriptingObject()) 
	{
		ScriptingObjectPtr instance = GetInstance();
		
		if (IsInstanceValid(instance) && m_Methods[MonoScriptCache::kRemoveFromManager] && m_DidAwake)
			CallMethodInactive(m_Methods[MonoScriptCache::kRemoveFromManager]);
		if (IsInstanceValid(instance) && m_Methods[MonoScriptCache::kRemoveFromManagerInternal] && m_DidAwake)
			CallMethodInactive(m_Methods[MonoScriptCache::kRemoveFromManagerInternal]);
		//@TODO: Try removing this
		if (IsInstanceValid(instance) && (m_Methods[MonoScriptCache::kCoroutineStart] || m_Methods[MonoScriptCache::kCoroutineMain]))
			GetDelayedCallManager().CancelCallDelayed (this, DelayedStartCall, NULL, NULL);
		
		if ( IsInstanceValid(instance) )
			SetByPassOnDSP(true);
	}	
}	

bool MonoBehaviour::CallMethodInactive (ScriptingMethodPtr method)
{
	AssertIf (GetInstance() == SCRIPTING_NULL);
	AssertIf (method == SCRIPTING_NULL);

	ScriptingObjectPtr instance = GetInstance();
	ScriptingInvocation invocation(method);
	invocation.object = instance;
	invocation.logException = true;
	invocation.objectInstanceIDContextForException = Scripting::GetInstanceIDFromScriptingWrapper(instance);
	invocation.AdjustArgumentsToMatchMethod();
	invocation.InvokeChecked();
	return (NULL == invocation.exception);
}

bool MonoBehaviour::CallMethodInactive (const char* methodName)
{
	ScriptingMethodPtr method = GetScriptingMethodRegistry().GetMethod(GetClass(),methodName,ScriptingMethodRegistry::kWithoutArguments);

	if (!method)
		return false;

	CallMethodInactive (method);
	
	return true;
}

bool MonoBehaviour::DoGUI (MonoBehaviour::GUILayoutType layoutType, int skin)
{
	if (GetInstance () == SCRIPTING_NULL)
		return false;
	ScriptingMethodPtr method = GetMethod (MonoScriptCache::kGUI);
	if (method == SCRIPTING_NULL)
		return false;
	Start ();

	PPtr<MonoBehaviour> behaviourPPtr = this;
	ScriptingObjectPtr monoObject = GetInstance ();
	int instanceID = GetInstanceID();
	GUIState &guiState = GetGUIState ();
	guiState.m_CanvasGUIState.m_GUIClipState.BeginOnGUI (*guiState.m_CurrentEvent);

	guiState.BeginOnGUI(GetObjectGUIState());

	int allowGUILayoutParam = layoutType;
	
	ScriptingInvocation invocation;
	invocation.AddInt(skin);
	invocation.AddInt(instanceID);
	invocation.AddInt(allowGUILayoutParam);
	invocation.method = MONO_COMMON.beginGUI;
	invocation.Invoke();

#if UNITY_EDITOR
	ScriptingInvocation invocation2;
	invocation2.method = MONO_COMMON.clearUndoSnapshotTarget;
	invocation2.Invoke();
#endif
	
	ScriptingExceptionPtr exception = NULL;
	ScriptingInvocationNoArgs userOnGUIInvocation;
	userOnGUIInvocation.method = method;
	userOnGUIInvocation.object = monoObject;
	userOnGUIInvocation.logException = false;
	userOnGUIInvocation.Invoke(&exception);

	// Display exception (except for ExitGUIException which exists only to successfully exit out of a GUI loop)
	if (exception)
	{
#if ENABLE_MONO
		void* excparams[] = {exception}; 
		MonoObject* res = CallStaticMonoMethod("GUIUtility", "EndGUIFromException", excparams);
		guiState.m_CanvasGUIState.m_GUIClipState.EndThroughException();
		if (MonoObjectToBool(res))
		{
			guiState.EndOnGUI ();
			return guiState.m_CurrentEvent->type == InputEvent::kUsed;
		}
		else
		{
			guiState.EndOnGUI ();
			::Scripting::LogException(exception, behaviourPPtr.GetInstanceID());
			return false;
		}
#endif
	}
	else
	{	
		// Mono is not happy about receiving bools from internal calls,
		// especially since bools are not exactly specified on how many bytes they contain on the C++ side
		
		ScriptingInvocation endGUIInvocation;
		endGUIInvocation.method = MONO_COMMON.endGUI;
		endGUIInvocation.AddInt((int)layoutType);
		endGUIInvocation.Invoke();

		guiState.EndOnGUI ();
		guiState.m_CanvasGUIState.m_GUIClipState.EndOnGUI (*guiState.m_CurrentEvent);
	}
	return guiState.m_CurrentEvent->type == InputEvent::kUsed;
}

#if 0
// IM GUI in retained GUI callbacks. not used right now.
bool MonoBehaviour::DoCustomGUI (Rectf &position, const ColorRGBAf &guiColor)
{
	MonoObject *instance = GetInstance ();
	MonoMethod* method = GetMethod(MonoScriptCache::kCustomGUI);
	if (instance == NULL || method == NULL)
		return false;
	Start ();
	
	GUIState &guiState = GetGUIState ();
	guiState.SetObjectGUIState (GetObjectGUIState());
	guiState.BeginOnGUI();
	guiState.m_OnGUIState.m_Color = guiColor;
	
	MonoException* exception;
	
	void* params[] = { &position };	
	bool retval = mono_runtime_invoke_profiled(method, instance, params, &exception);

	// TODO: Handle GUI exceptions
	AssertIf (exception);
	
	guiState.EndOnGUI ();
	return retval;
}
#endif

void MonoBehaviour::SetUseGUILayout (bool use)
{
	m_UseGUILayout = use;
}

bool MonoBehaviour::GetUseGUILayout ()
{
	return m_UseGUILayout;
}

#if ENABLE_AUDIO_FMOD

FMOD::DSP* MonoBehaviour::GetOrCreateDSP() 
{	
	IAudio* audio = GetIAudio();
	if (!audio)
		return NULL;

	if (!m_AudioCustomFilter)
	{
	if (m_Methods && m_Methods[MonoScriptCache::kAudioFilterRead] && IsActive())
			m_AudioCustomFilter = audio->CreateAudioCustomFilter(this);
	else
		return NULL;
}

	return audio->GetOrCreateDSPFromCustomFilter(m_AudioCustomFilter);
}

FMOD::DSP* MonoBehaviour::GetDSP() const
{	
	IAudio* audio = GetIAudio();
	if (!audio)
		return NULL;

	if (m_AudioCustomFilter)
		return audio->GetDSPFromAudioCustomFilter(m_AudioCustomFilter);
	else
		return NULL;
}
#endif

bool MonoBehaviour::HaveAudioCallback() const
{ 
	return m_Methods ? m_Methods[MonoScriptCache::kAudioFilterRead] != SCRIPTING_NULL : false; 
}

inline void MonoBehaviour::CallMethodIfAvailable (int methodIndex)
{
	AssertIf (methodIndex < 0 || methodIndex >= MonoScriptCache::kMethodCount);
	ScriptingMethodPtr method = m_Methods[methodIndex];
	if (method == SCRIPTING_NULL)
	{
		return;
	}

	AssertIf (GetInstance() == SCRIPTING_NULL);
	AssertIf (!m_DidAwake);

	if (!IsActive ())
		return;
	
	ScriptingInvocationNoArgs invocation(method);
	invocation.objectInstanceIDContextForException = GetInstanceID();
	invocation.object = GetInstance();
	invocation.Invoke();
}

inline void MonoBehaviour::Start ()
{
	DebugAssertIf (!m_DidAwake);

	
	#if UNITY_EDITOR
	AssertIf (!IsActive () && !GetRunInEditMode());
	#else
	AssertIf (!IsActive ());
	#endif
	
	if (m_DidStart)
		return;

	AssertIf (GetInstance() == SCRIPTING_NULL);
	
	m_DidStart = true;
	
	ScriptingMethodPtr method;
	method = m_Methods[MonoScriptCache::kCoroutineMain];
	
	if (method)
		InvokeMethodOrCoroutineChecked (method, SCRIPTING_NULL);
	
	method = m_Methods[MonoScriptCache::kCoroutineStart];
	if (method)
		InvokeMethodOrCoroutineChecked (method, SCRIPTING_NULL);
}


#if UNITY_EDITOR
void MonoBehaviour::RestartExecuteInEditModeScripts ()
{
	std::vector<SInt32> behaviours;
	Object::FindAllDerivedObjects (ClassID(MonoBehaviour), &behaviours);
	
	for (std::vector<SInt32>::iterator it = behaviours.begin(), itEnd = behaviours.end(); it != itEnd; ++it)
	{
		MonoBehaviour* beh = dynamic_pptr_cast<MonoBehaviour*> (Object::IDToPointer(*it));
		if (beh == NULL || !beh->GetRunInEditMode() || !beh->IsActive() || !beh->GetEnabled())
			continue;
		
		// disable and enable the script (careful to not call SetDirty)
		beh->SetEnabledNoDirty (false);
		beh->SetEnabledNoDirty (true);
		// and Start it again
		beh->m_DidStart = false;
		beh->Start ();
	}
}

bool MonoBehaviour::GetRunInEditMode () const
{
	if (m_ScriptCache)
		return m_ScriptCache->runInEditMode;
	else
		return false;
}

#endif


void MonoBehaviour::CallUpdateMethod(int methodIndex)
{
	AssertIf (!IsPlayingOrAllowExecuteInEditMode ());
	AssertIf (!IsActive ());
	AssertIf (!GetEnabled ());
	ScriptingObjectPtr instance = GetInstance();
	
	if (instance == SCRIPTING_NULL)
		return;
	// Ensure Start has been called
	Start ();
	
	// We might be getting destroyed in Start
	if (!IsInstanceValid(instance))
		return;

	// Call Update
	CallMethodIfAvailable (methodIndex);
}

void MonoBehaviour::Update ()
{
	CallUpdateMethod (MonoScriptCache::kUpdate);
}

void MonoBehaviour::LateUpdate ()
{
	CallUpdateMethod (MonoScriptCache::kLateUpdate);
}

void MonoBehaviour::FixedUpdate ()
{
	CallUpdateMethod (MonoScriptCache::kFixedUpdate);
}

ScriptingMethodPtr MonoBehaviour::FindMethod (const char* name)
{
	if (GetInstance() == SCRIPTING_NULL)
		return SCRIPTING_NULL;
	
	const MethodCache* cache = GetMethodCache();
	
	MethodCache::const_iterator i = cache->find (name);
	if (i != cache->end ())
		return i->second;
	else
		return SCRIPTING_NULL;
}


#if ENABLE_MONO || UNITY_WINRT
void MonoBehaviour::InvokeOnRenderObject ()
{
	DebugAssertIf (!IsPlayingOrAllowExecuteInEditMode());
	AssertIf (!GetEnabled ());
	AssertIf (!IsActive ());
	if (GetInstance())
	{
		Start ();
		CallMethodIfAvailable (MonoScriptCache::kRenderObject);
	}
}
#endif //ENABLE_MONO || UNITY_WINRT

#if ENABLE_IMAGEEFFECTS
void MonoBehaviour::RenderImageFilter (Unity::Component* component, RenderTexture *source, RenderTexture *destination)
{
	MonoBehaviour* self = static_cast<MonoBehaviour*>(component);

	AssertIf (!self->GetEnabled ());
	AssertIf (!self->IsActive ());
	if (self->IsPlayingOrAllowExecuteInEditMode () && self->GetInstance())
	{
		self->Start ();

		ScriptingMethodPtr method = self->m_Methods[MonoScriptCache::kRenderImageFilter];
		// Early out message not supported
		if (method == SCRIPTING_NULL)
			return;
		
		PROFILER_AUTO_GFX(gMonoImageFxProfile, self)

		ScriptingObjectPtr instance = self->GetInstance();
		ScriptingInvocation invocation(method);
		invocation.object = instance;
		invocation.AddObject(Scripting::ScriptingWrapperFor (source));
		invocation.AddObject(Scripting::ScriptingWrapperFor (destination));
		invocation.objectInstanceIDContextForException = self->GetInstanceID();
		invocation.Invoke();
	}
}
#endif // ENABLE_IMAGEEFFECTS



#if ENABLE_MONO
static MonoObject* ConvertBuiltinMonoValue( MonoObject* value, int wantedType )
{
	#define SUPPORT_CONVERSION(srctype,dsttype,srccode,dstclass) \
		case srccode: \
			res = mono_object_new( domain, classes.dstclass ); \
			ExtractMonoObjectData<dsttype>(res) = static_cast<dsttype>( ExtractMonoObjectData<srctype>(value) ); \
			return res
	
	MonoClass* valueClass = mono_object_get_class (value);
	int type = mono_type_get_type( mono_class_get_type (valueClass) );
	// types match exactly - return input object
	if( type == wantedType )
		return value;
	
	MonoDomain* domain = mono_domain_get();
	const CommonScriptingClasses& classes = GetMonoManager().GetCommonClasses();
	MonoObject* res;
	
	if( wantedType == MONO_TYPE_I4 )
	{
		switch( type ) {
			SUPPORT_CONVERSION(float, int,MONO_TYPE_R4,int_32);
			SUPPORT_CONVERSION(double,int,MONO_TYPE_R8,int_32);
		}
	}
	else if( wantedType == MONO_TYPE_R4 )
	{
		switch( type ) {
			SUPPORT_CONVERSION(int,   float,MONO_TYPE_I4,floatSingle);
			SUPPORT_CONVERSION(double,float,MONO_TYPE_R8,floatSingle);
		}
	}
	else if( wantedType == MONO_TYPE_R8 )
	{
		switch( type ) {
			SUPPORT_CONVERSION(int,  double,MONO_TYPE_I4,floatDouble);
			SUPPORT_CONVERSION(float,double,MONO_TYPE_R4,floatDouble);
		}
	}
		
	#undef SUPPORT_CONVERSION
	
	// can't convert
	return NULL;
}
#endif //ENABLE_MONO


static bool CompareCoroutine (void* callBackUserData, void* cancelUserdata)
{
	return callBackUserData == cancelUserdata;
}

Coroutine* MonoBehaviour::CreateCoroutine(ScriptingObjectPtr userCoroutine, ScriptingMethodPtr method)
{
	ScriptingMethodPtr moveNext = scripting_object_get_virtual_method(userCoroutine, MONO_COMMON.IEnumerator_MoveNext, GetScriptingMethodRegistry());

#if !UNITY_FLASH
	ScriptingMethodPtr current = scripting_object_get_virtual_method(userCoroutine, MONO_COMMON.IEnumerator_Current, GetScriptingMethodRegistry());
#else
	//todo: make flash use generic path. set a bogus value for flash here right now so it passes current != NULL check,  flash path will never use this value for now.
	ScriptingMethodPtr current = (ScriptingMethodPtr)1;
#endif

	if (current == SCRIPTING_NULL || moveNext == SCRIPTING_NULL)
	{
		std::string message = (method != SCRIPTING_NULL) ? Format ("Coroutine '%s' couldn't be started!", scripting_method_get_name(method)) : "Coroutine couldn't be started!";
		LogStringObject (message, this);
		return NULL;
	}
	
	Coroutine* coroutine = new Coroutine ();
	
	coroutine->m_CoroutineEnumeratorGCHandle = scripting_gchandle_new (userCoroutine);
	coroutine->m_CoroutineEnumerator = userCoroutine;
	coroutine->m_CoroutineMethod = method;
	coroutine->SetMoveNextMethod(moveNext);
	coroutine->SetCurrentMethod(current);
	coroutine->m_Behaviour = this;
	coroutine->m_ContinueWhenFinished = NULL;
	coroutine->m_WaitingFor = NULL;
	coroutine->m_AsyncOperation = NULL;
	coroutine->m_RefCount = 1;
	coroutine->m_IsReferencedByMono = 0;
	#if DEBUG_COROUTINE
	printf_console ("Allocate coroutine %d\n", coroutine);
	AssertIf(GetDelayedCallManager().HasDelayedCall(coroutine->m_Behaviour, Coroutine::ContinueCoroutine, CompareCoroutine, coroutine));
	#endif

	#if DEBUG_COROUTINE_LEAK			
	printf_console ("Active coroutines %d\n", gCoroutineCounter);
	gCoroutineCounter++;
	#endif

	m_ActiveCoroutines.push_back (*coroutine);
	AssertIf(&m_ActiveCoroutines.back() != coroutine);
	m_ActiveCoroutines.back ().Run ();

	AssertIf(coroutine->m_RefCount == 0);
	if (coroutine->m_RefCount <= 1)
	{
		Coroutine::CleanupCoroutine(coroutine);
		return NULL;
	}
	
	Coroutine::CleanupCoroutine(coroutine);
	return coroutine;				
}


static ScriptingObjectPtr CreateManagedWrapperForCoroutine(Coroutine* coroutine)
{
	if (coroutine == NULL) return SCRIPTING_NULL;
	Assert(!coroutine->m_IsReferencedByMono);
	coroutine->m_IsReferencedByMono = true;
	ScriptingObjectWithIntPtrField<Coroutine> wrapper = scripting_object_new(GetMonoManager ().GetCommonClasses ().coroutine);
	wrapper.SetPtr(coroutine, Coroutine::CleanupCoroutineGC);
	return wrapper.object;
}

ScriptingObjectPtr MonoBehaviour::StartCoroutineManaged (const char* name, ScriptingObjectPtr value)
{
	Coroutine* coroutine = StartCoroutine (name, value);
	return CreateManagedWrapperForCoroutine(coroutine);
}

ScriptingObjectPtr MonoBehaviour::StartCoroutineManaged2 (ScriptingObjectPtr enumerator)
{
	if (!IsActive ())
	{
		ErrorStringObject (Format ("Coroutine couldn't be started because the the game object '%s' is inactive!", GetName()), this);
		return SCRIPTING_NULL;
	}
	
	Coroutine* coroutine = CreateCoroutine(enumerator, SCRIPTING_NULL);
	return CreateManagedWrapperForCoroutine(coroutine);
}

static bool DoesMethodHaveIEnumeratorReturnType(ScriptingMethodPtr method)
{
	ScriptingClassPtr iEnumerator = GetMonoManager ().GetCommonClasses ().iEnumerator;
	ScriptingClassPtr returnType = scripting_method_get_returntype(method, GetScriptingTypeRegistry());
	return returnType == iEnumerator;
}

Coroutine* MonoBehaviour::HandleCoroutineReturnValue (ScriptingMethodPtr method, ScriptingObjectPtr returnValue)
{
	if (!DoesMethodHaveIEnumeratorReturnType(method))
		return NULL;

	return CreateCoroutine(returnValue, method);
}

ScriptingObjectPtr MonoBehaviour::InvokeMethodOrCoroutineChecked(ScriptingMethodPtr scriptingMethod, ScriptingObjectPtr value, ScriptingExceptionPtr* exception)
{
	#define PARAMETER_ERROR(x) \
	{ \
		string error = Format ("Failed to call function %s of class %s\n", scripting_method_get_name (scriptingMethod), GetScript ()->GetScriptClassName ().c_str ()); \
		error += x; \
		ErrorStringObject (error, this); \
		return SCRIPTING_NULL; \
	}
#if ENABLE_MONO


	MonoObject* instance = GetInstance();
	int argCount = scripting_method_get_argument_count (scriptingMethod, GetScriptingTypeRegistry());

	// Fast path - method takes no arguments
	if (argCount == 0)
		return mono_runtime_invoke_profiled_fast(scriptingMethod, instance, exception, NULL);

	// One argument, one value
	if (argCount != 1 || value == SCRIPTING_NULL)
	{
		if (value == NULL)
		{
			PARAMETER_ERROR (Format ("Calling function %s with no parameters but the function requires %d.", scripting_method_get_name (scriptingMethod), argCount));
		}
		else
		{
			PARAMETER_ERROR (Format ("Calling function %s with 1 parameter but the function requires %d.", scripting_method_get_name (scriptingMethod), argCount));
		}

		return NULL;
	}

	MonoClass* inParamClass = mono_object_get_class (value);

	void* iterator = NULL;

	MonoMethodSignature* sig = mono_method_signature (scriptingMethod->monoMethod);
	MonoType* methodType = mono_signature_get_params (sig, &iterator);
	MonoClass* methodClass = mono_class_from_mono_type (methodType);
	int methodTypeType = mono_type_get_type (methodType);

	void* argumentList[1] = { NULL };
		
	// Pass builtin type
	if (IsMonoBuiltinType (methodTypeType))
	{
		MonoObject* converted = ConvertBuiltinMonoValue(value, methodTypeType);
		if( converted )
		{
			argumentList[0] = ExtractMonoObjectDataPtr<void> (converted);
		}
	} 
	// Pass by value
	else if (methodTypeType == MONO_TYPE_VALUETYPE)
	{
		if (inParamClass == methodClass)
		{
			argumentList[0] = ExtractMonoObjectDataPtr<void> (value);
		}
	}
	// Pass by class
	else if (methodTypeType == MONO_TYPE_CLASS)
	{
		if (mono_class_is_subclass_of (inParamClass, methodClass, false))
		{
			argumentList[0] = value;
		}
	}
	// Passing string
	else if (methodTypeType == MONO_TYPE_STRING && methodTypeType == mono_type_get_type (mono_class_get_type (inParamClass)))
	{
		argumentList[0] = value;
	}
	else if (methodTypeType == MONO_TYPE_OBJECT)
	{
		argumentList[0] = value;
	}

	if (argumentList[0])
		return mono_runtime_invoke_profiled (scriptingMethod->monoMethod, instance, argumentList, exception);

	void* invokeargs[3] = { instance, mono_string_new_wrapper (scripting_method_get_name (scriptingMethod)), value };
	return mono_runtime_invoke_profiled (GetMonoManager().GetCommonClasses().invokeMember->monoMethod, NULL, invokeargs, exception);
	
#else
	// ToDo: make full params type check, like we do it with mono
	int argCount = scripting_method_get_argument_count(scriptingMethod, GetScriptingTypeRegistry());
	ScriptingInvocation invocation(scriptingMethod);
	invocation.object = GetInstance();

	if (argCount == 0)
		return invocation.Invoke(exception);

	if (argCount != 1 || value == SCRIPTING_NULL)
	{
		if (value == SCRIPTING_NULL)
		{
			PARAMETER_ERROR (Format ("Calling function %s with no parameters but the function requires %d.", scripting_method_get_name (scriptingMethod), argCount));
		}
		else
		{
			PARAMETER_ERROR (Format ("Calling function %s with 1 parameter but the function requires %d.", scripting_method_get_name (scriptingMethod), argCount));
		}
		return SCRIPTING_NULL;
	}

#if UNITY_WP8
	ScriptingInvocation invokeMember(MONO_COMMON.invokeMember);
	invokeMember.AddObject(GetInstance());
	invokeMember.AddString(scripting_method_get_name(scriptingMethod));
	invokeMember.AddObject(value);
	return invokeMember.Invoke(exception);
#else
	invocation.AddObject(value);
	return invocation.Invoke(exception, true);
#endif

#endif
	#undef PARAMETER_ERROR
}

Coroutine* MonoBehaviour::InvokeMethodOrCoroutineChecked (ScriptingMethodPtr method, ScriptingObjectPtr value)
{
	
	AssertIf (!IsPlayingOrAllowExecuteInEditMode ());
	
	ScriptingObjectPtr instance = GetInstance();
	AssertIf (instance == SCRIPTING_NULL);
	
	ScriptingExceptionPtr exception = NULL;
	ScriptingObjectPtr returnValue = InvokeMethodOrCoroutineChecked(method,value,&exception);
	
	if (returnValue != SCRIPTING_NULL && exception == NULL)
		return HandleCoroutineReturnValue (method, returnValue);
	
	if (exception != NULL)
		Scripting::LogException(exception, Scripting::GetInstanceIDFromScriptingWrapper(instance));
	
	return NULL;
}

Coroutine* MonoBehaviour::StartCoroutine (const char* name, ScriptingObjectPtr value)
{
	AssertIf (!IsPlayingOrAllowExecuteInEditMode ());
	AssertIf (GetInstance() == SCRIPTING_NULL);

	if (!IsActive ())
	{
		ErrorStringObject (Format ("Coroutine '%s' couldn't be started because the the game object '%s' is inactive!", name, GetName()), this);
		return NULL;
	}
	
	ScriptingMethodPtr method = FindMethod (name);
	if (method == NULL)
	{
		ErrorStringObject (Format ("Coroutine '%s' couldn't be started!", name), this);
		return NULL;
	}

	return InvokeMethodOrCoroutineChecked (method, value);
}

static void DoStopCoroutine (Coroutine *coroutine)
{
	coroutine->RemoveFromList ();

	// We clear the behaviour reference prior to cleaning up the coroutine because otherwise CleanupCoroutine might remove
	// it from underneath m_ActiveCoroutines
	coroutine->m_Behaviour = NULL;

	if (coroutine->m_WaitingFor)
	{
		AssertIf (coroutine->m_WaitingFor->m_ContinueWhenFinished != coroutine);
		coroutine->m_WaitingFor->m_ContinueWhenFinished = NULL;
		coroutine->m_WaitingFor = NULL;
		Coroutine::CleanupCoroutine (coroutine);
	}
	else if (coroutine->m_AsyncOperation)
		Coroutine::CleanupCoroutine (coroutine);
}

void MonoBehaviour::StopCoroutine (const char* name)
{
	#if DEBUG_COROUTINE
	printf_console("StopCoroutine %s. %d - %s", name, this, GetName());
	#endif
	GetDelayedCallManager ().CancelCallDelayed (this, Coroutine::ContinueCoroutine, Coroutine::CompareCoroutineMethodName, (void*)name);
	for (List<Coroutine>::iterator i = m_ActiveCoroutines.begin ();
	     i != m_ActiveCoroutines.end (); ++i) {
		if (i->m_CoroutineMethod != NULL && !strcmp (name, scripting_method_get_name (i->m_CoroutineMethod))) {
			DoStopCoroutine (&(*i));
			break;
		}
	}
}

void MonoBehaviour::StopAllCoroutines ()
{
	if(m_ActiveCoroutines.empty ())
	{
		return;
	}

	#if DEBUG_COROUTINE
	printf_console("StopAllCoroutines! %d - %s", this, GetName());
	#endif
	
	DelayedCall* wwwCallback = NULL;
	#if ENABLE_WWW
	wwwCallback = WWWDelayCall::Callback;
	#endif
	
	// Remove all coroutines in this behaviour from delayed call/
	// This will call CleanupCoroutine and decrease the retain count.
	// In turn this might remove coroutines from the active coroutine list, if no one else is referencing them.
	GetDelayedCallManager ().CancelCallDelayed2 (this, Coroutine::ContinueCoroutine, wwwCallback);

	/// 1. We need to go through the remaining coroutines (Those that are still referenced from mono or other coroutines are yielding to it from another game object)
	
	while (!m_ActiveCoroutines.empty())
	{
		Coroutine *coroutine = &m_ActiveCoroutines.front();
		DoStopCoroutine (coroutine);
		#if DEBUG_COROUTINE
		printf_console ("Cleaning up Stop ALL %d\n", coroutine);
		#endif
	}
	
	// This assert is useful when you think we might leak coroutines.
	Assert (m_ActiveCoroutines.empty ());
}

#if UNITY_EDITOR
void MonoBehaviour::CheckConsistency ()
{
	Super::CheckConsistency();

	if (!GetInstance())
		return;

	bool canDestroy = GetDisableImmediateDestruction ();
	SetDisableImmediateDestruction (true);

	// Validate properties

	ScriptingMethodPtr method = m_Methods[MonoScriptCache::kValidateProperties];
	if (method && !(m_ScriptCache->scriptType == kScriptTypeMonoBehaviourDerived && GetGameObjectPtr() == NULL))
	{
		CallMethodInactive (method);
	}

	SetDisableImmediateDestruction (canDestroy);
}
#endif

void MonoBehaviour::SmartReset ()
{
	Super::SmartReset ();
	// Only in edit mode for more speed
	// and to be nice to scripters we call Reset only if we are active
	if (GetInstance() && !IsWorldPlaying () )
		CallMethodInactive("Reset");
}

void MonoBehaviour::InitializeClass ()
{
	GameObject::RegisterAllMessagesHandler (ClassID (MonoBehaviour), &MonoBehaviour::HandleNotifications, &MonoBehaviour::CanHandleNotifications);
	
	RegisterAllowNameConversion ("GUISkin", "customStyles", "m_CustomStyles");

	InitializeMonoBehaviourAnimationBindingInterface ();
}

void MonoBehaviour::CleanupClass ()
{
	CleanupMonoBehaviourAnimationBindingInterface ();
}

UInt32 MonoBehaviour::CalculateSupportedMessages ()
{
	if (!GetInstance())
		return 0;

	int mask = 0;
	if (m_Methods[MonoScriptCache::kMethodCount + kEnterContact.messageID])
		mask |= kHasCollisionEnterExit;
	if (m_Methods[MonoScriptCache::kMethodCount + kExitContact.messageID])
		mask |= kHasCollisionEnterExit;
	if (m_Methods[MonoScriptCache::kMethodCount + kStayContact.messageID])
		mask |= kHasCollisionStay;
	if (m_Methods[MonoScriptCache::kMethodCount + kOnWillRenderObject.messageID])
		mask |= kHasOnWillRenderObject;
	if (m_Methods[MonoScriptCache::kMethodCount + kAnimatorMove.messageID])
		mask |= kHasOnAnimatorMove;
	if (m_Methods[MonoScriptCache::kMethodCount + kAnimatorIK.messageID])
		mask |= kHasOnAnimatorIK;
	if (m_Methods[MonoScriptCache::kMethodCount + kCollisionEnter2D.messageID])
		mask |= kHasCollision2D;
	if (m_Methods[MonoScriptCache::kMethodCount + kCollisionExit2D.messageID])
		mask |= kHasCollision2D;
	if (m_Methods[MonoScriptCache::kMethodCount + kCollisionStay2D.messageID])
		mask |= kHasCollision2D;
	return mask;
}

bool MonoBehaviour::CanHandleNotifications (void* receiver, int messageIndex, MessageData& data) {
	DebugAssertIf (dynamic_pptr_cast<MonoBehaviour*> (static_cast<MonoBehaviour*> (receiver)) == NULL);
	MonoBehaviour* behaviour = static_cast<MonoBehaviour*> (receiver);
	return behaviour->GetInstance() && behaviour->m_Methods[MonoScriptCache::kMethodCount + messageIndex];
}

static bool SetupArgumentsForMessageInvocation(ScriptingArguments& arguments, MessageData& data, ScriptingMethodPtr receivingMethod, MonoBehaviour& behaviour)
{
	switch (data.type)
	{
		case 0:
			return true;
		case ClassID(int):
			arguments.AddInt((int)data.GetGenericDataRef());
			return true;
		case ClassID(float):
			arguments.AddFloat((float)data.GetGenericDataRef());
			return true;
		case ClassID(bool):
			arguments.AddBoolean((int)data.GetGenericDataRef() != 0);
			return true;
		case ClassID(Collision):
			arguments.AddObject(GetIPhysics()->ConvertContactToMono (data.GetData<Collision*> ()));
			return true;
	#if ENABLE_2D_PHYSICS
		case ClassID(Collision2D):
			arguments.AddObject(ConvertCollision2DToScripting (data.GetData<Collision2D*> ()));
			return true;
	#endif
		case ClassID(MonoObject):
			{
				ScriptingObjectPtr parameter = data.GetScriptingObjectData ();
				arguments.AddObject(parameter);
				
				if (!parameter)
					return true;

				ScriptingClassPtr klass_of_receivingmethodargument = scripting_method_get_nth_argumenttype(receivingMethod,0,GetScriptingTypeRegistry());
				if (klass_of_receivingmethodargument == NULL)
					return true;
				ScriptingClassPtr klass_of_payload = scripting_object_get_class(parameter,GetScriptingTypeRegistry());

				if (!scripting_class_is_subclass_of(klass_of_payload, klass_of_receivingmethodargument))
				{
					std::string message = Format("%s couldn't be called because the expected parameter %s doesn't match %s.", scripting_method_get_name(receivingMethod), scripting_class_get_name(klass_of_receivingmethodargument),scripting_class_get_name(klass_of_payload));
					ErrorStringObject(message, &behaviour);
					return false;
				}

				return true;
			}
		default:
			// Otherwise its a Object derived class (The heavy typechecking is done in MonoScript)
			arguments.AddObject(Scripting::ScriptingWrapperFor (data.GetData<Object*> ()));
			return true;
	}
}

static bool ShouldMessageBeSentToDisabledGameObjects(int messageIndex)
{
	if( !IS_CONTENT_NEWER_OR_SAME(kUnityVersion4_2_a1) && (messageIndex == kAnimatorMove.messageID || messageIndex == kAnimatorIK.messageID))
		return true;

	MessageIdentifier msg = GameObject::GetMessageHandler ().MessageIDToMessageIdentifier (messageIndex);
	
	return !(msg.options & MessageIdentifier::kDontSendToDisabled);
}

void MonoBehaviour::HandleNotifications (void* receiver, int messageIndex, MessageData& data) {
	DebugAssertIf (dynamic_pptr_cast<MonoBehaviour*> (static_cast<MonoBehaviour*> (receiver)) == NULL);
	MonoBehaviour* behaviour = static_cast<MonoBehaviour*> (receiver);

	#if UNITY_FLASH
	if(behaviour->m_Methods == NULL)
		return;
	#endif

	if (!behaviour->IsPlayingOrAllowExecuteInEditMode ())
		return;

	if (!behaviour->GetInstance())
		return;

	ScriptingMethodPtr method = behaviour->m_Methods[MonoScriptCache::kMethodCount + messageIndex];

	if (method == SCRIPTING_NULL)
		return;

	if (!behaviour->GetEnabled () && !ShouldMessageBeSentToDisabledGameObjects(messageIndex))
		return;

	ScriptingInvocation invocation(method);
	invocation.object = behaviour->GetInstance();
	invocation.objectInstanceIDContextForException = behaviour->GetInstanceID();

	if (!SetupArgumentsForMessageInvocation(invocation.Arguments(),data,method,*behaviour))
		return;	

	ScriptingExceptionPtr exception = NULL;
	ScriptingObjectPtr returnValue = invocation.Invoke(&exception);

	if (exception == NULL && returnValue)
		behaviour->HandleCoroutineReturnValue (method, returnValue);	
}

void MonoBehaviour::SetupAwake ()
{
	if (IsPlayingOrAllowExecuteInEditMode () && !m_DidAwake && GetInstance() && IsActive ())
	{
		m_DidAwake = true;
		CallMethodIfAvailable (MonoScriptCache::kAwake);
	}
}

void MonoBehaviour::DeprecatedDelayedAwakeCall (Object* o, void* userData)
{
	static_cast<MonoBehaviour*> (o)->SetupAwake ();
}

void MonoBehaviour::DelayedStartCall (Object* o, void* userData)
{
	static_cast<MonoBehaviour*> (o)->Start ();
}

void MonoBehaviour::DeprecatedAwakeFromLoadCodePath (AwakeFromLoadMode awakeMode)
{
	// We must cache the reference to other objects here
	// since Behaviour::AwakeFromLoad might destroy the object itself
	ScriptingObjectPtr instance = GetInstance();

	// Potential Call to AddToManager / RemoveFromManager with various C# callbacks
	Super::AwakeFromLoad (awakeMode);
	
	if (IsPlayingOrAllowExecuteInEditMode () && IsInstanceValid(instance) && !m_DidAwake && IsActive ())
	{
		if (awakeMode & (kInstantiateOrCreateFromCodeAwakeFromLoad | kActivateAwakeFromLoad))
			SetupAwake();
		else
			// @TODO: Do we really need this? Build this into Proper AwakeFromLoadQueue or whatever
			CallDelayed (DeprecatedDelayedAwakeCall, this, -1000, NULL, 0.0F, NULL, DelayedCallManager::kRunDynamicFrameRate | DelayedCallManager::kRunFixedFrameRate | DelayedCallManager::kRunStartupFrame);
	}
	else if (!m_DidAwake && IsInstanceValid(instance) && IsScriptableObject())
	{
		m_DidAwake = true;
		
		ScriptingMethodPtr method = m_Methods[MonoScriptCache::kAddToManager];
		if (method)
			CallMethodInactive (method);
		
		method = m_Methods[MonoScriptCache::kAwake];
		if (method)
			CallMethodInactive (method);
	}
	
	if ((awakeMode & kDidLoadFromDisk) == 0 && IsInstanceValid(instance))
	{
		ScriptingMethodPtr method = m_Methods[MonoScriptCache::kValidateProperties];
		if (method && !(m_ScriptCache->scriptType == kScriptTypeMonoBehaviourDerived && GetGameObjectPtr() == NULL))
			CallMethodInactive (method);
	}
}


#define USE_DEPRECATED_AWAKE !IS_CONTENT_NEWER_OR_SAME(kUnityVersion4_0_a1)
//#define USE_DEPRECATED_AWAKE 1



// * Awake
// * OnEnable
// * ----- For all scripts
// * Start for all scripts
void MonoBehaviour::AwakeFromLoad (AwakeFromLoadMode awakeMode)
{
#if UNITY_EDITOR && UNITY_LOGIC_GRAPH
	LoadLogicGraphInEditor ();
#endif

	if (GetGameObjectPtr())
		GetGameObjectPtr()->SetSupportedMessagesDirty ();

	// Deprecated 3.x codepath
	if (USE_DEPRECATED_AWAKE)
	{
		DeprecatedAwakeFromLoadCodePath (awakeMode);
		return;
	}
	
	// We must cache the reference to other objects here
	// since Behaviour::AwakeFromLoad might destroy the object itself
	// and we need a way to check that this happened.
	ScriptingObjectPtr instance = GetInstance();

	// The managed MonoBehaviour could not be created. Give control to the Behaviour and move on.
	if (instance == SCRIPTING_NULL)
	{
		Super::AwakeFromLoad (awakeMode);
		return;
	}

	// CallDelayed->Start must be called before Awake otherwise objects created inside of Awake will get their Start call
	// before the Start of the Behaviour that created them.
	bool willCallAddToManager = IsPlayingOrAllowExecuteInEditMode() && GetEnabled() && IsActive();
	if (willCallAddToManager)
	{
		Super::AwakeFromLoad (awakeMode);
		return;
	}
	
	#define RETURN_IF_INSTANCE_DESTROYED 	if (!IsInstanceValid(instance)) return;
	
	bool isMonoBehaviourRequiringAwake = IsPlayingOrAllowExecuteInEditMode () && !m_DidAwake && IsActive ();
	bool isScriptableObjectRequiringAwakeAndEnable = !m_DidAwake && IsScriptableObject();

	// Awake for monobehaviours and scriptable objects
	if (isMonoBehaviourRequiringAwake || isScriptableObjectRequiringAwakeAndEnable)
	{
		CallAwake();
		RETURN_IF_INSTANCE_DESTROYED
	}

	// OnEnable for scriptable object
	if (isScriptableObjectRequiringAwakeAndEnable)
	{
		ScriptingMethodPtr enableMethod = m_Methods[MonoScriptCache::kAddToManager];
		if (enableMethod)
		{
			CallMethodInactive (enableMethod);
			RETURN_IF_INSTANCE_DESTROYED
		}
	}
	
	// Potential Call to AddToManager / RemoveFromManager with various C# callbacks
	Super::AwakeFromLoad (awakeMode);

	#undef RETURN_IF_INSTANCE_DESTROYED
}

void MonoBehaviour::CallAwake ()
{
	m_DidAwake = true;
	ScriptingMethodPtr awakeMethod = m_Methods[MonoScriptCache::kAwake];
	if (awakeMethod)
		if (!CallMethodInactive (awakeMethod))
			SetEnabled (false);
}



void MonoBehaviour::AddExternalDependencyCallbacksToManagers ()
{
#if ENABLE_IMAGEEFFECTS
	///@TODO: It doesn't look like the order of image effects is determined by the component order.
	//        Instead by the rather random from the users perspective Awake order.
	if (m_Methods[MonoScriptCache::kRenderImageFilter])
	{
		Camera *camera = QueryComponent (Camera);
		if (camera)
		{
			bool afterOpaque = scripting_method_has_attribute(m_Methods[MonoScriptCache::kRenderImageFilter], MONO_COMMON.imageEffectOpaque);
			bool transformsToLDR = scripting_method_has_attribute(m_Methods[MonoScriptCache::kRenderImageFilter], MONO_COMMON.imageEffectTransformsToLDR);
			
			ImageFilter filter (this, &RenderImageFilter, transformsToLDR, afterOpaque);
			camera->AddImageFilter (filter);
		}
	}
#endif
	
	SetByPassOnDSP(false);
}

void MonoBehaviour::SetByPassOnDSP(bool state)
{
#if ENABLE_AUDIO_FMOD
	IAudio* audio = GetIAudio();
	if (!audio) return;

	FMOD::DSP* dsp = GetOrCreateDSP();
	if (dsp)
		audio->SetBypassOnDSP(dsp,state);
#endif
}

void MonoBehaviour::AddBehaviourCallbacksToManagers ()
{
	MonoScript* script = m_Script;
	int executionOrder = script ? script->GetExecutionOrder() : 0;
	
	if (m_Methods[MonoScriptCache::kUpdate])
		GetBehaviourManager().AddBehaviour (m_UpdateNode, executionOrder);
	if (m_Methods[MonoScriptCache::kFixedUpdate])
		GetFixedBehaviourManager().AddBehaviour (m_FixedUpdateNode, executionOrder);
	if (m_Methods[MonoScriptCache::kLateUpdate])
		GetLateBehaviourManager().AddBehaviour (m_LateUpdateNode, executionOrder);
	
	if (m_Methods[MonoScriptCache::kRenderObject])
		GetRenderManager().AddOnRenderObject (m_OnRenderObjectNode);
	
#if ENABLE_UNITYGUI
	if (m_Methods[MonoScriptCache::kGUI])
		GetGUIManager().AddGUIScript (m_GUINode);
#endif
}


/* TODO: Tests
 * Destruction during OnEnable / Awake / Start (Maybe integration tests cover this already??)
 * Ensure that image effects & image filters are setup if their camera is created in OnEnable...
*/

#define RETURN_IF_DESTROYED_OR_DISABLED if (!IsInstanceValid(instance) || !GetEnabled()) return;

void MonoBehaviour::AddToManager ()
{
	if (USE_DEPRECATED_AWAKE)
	{
		DeprecatedAddToManager ();
		return;
	}

	
	ScriptingObjectPtr instance = GetInstance();
	if (instance == SCRIPTING_NULL || !IsPlayingOrAllowExecuteInEditMode ())
		return;
	
	if (m_Methods[MonoScriptCache::kCoroutineStart] || m_Methods[MonoScriptCache::kCoroutineMain])
		CallDelayed (DelayedStartCall, this, -10, NULL, 0.0F, NULL, DelayedCallManager::kRunDynamicFrameRate | DelayedCallManager::kRunFixedFrameRate | DelayedCallManager::kRunStartupFrame);
	

	// Behaviour callbacks are registered before OnEnable.
	// If an object is created in OnEnable, the order will be this script, then the created script.
	AddBehaviourCallbacksToManagers ();

	// We must call Awake here. 
	// CallDelayed->Start must be called before Awake otherwise objects created inside of Awake will get their Start call
	// before the Start of the Behaviour that created them.
	if (!m_DidAwake)
	{
		CallAwake ();
		RETURN_IF_DESTROYED_OR_DISABLED
	}

	if (m_Methods[MonoScriptCache::kAddToManager])
	{
		CallMethodIfAvailable (MonoScriptCache::kAddToManager);
		RETURN_IF_DESTROYED_OR_DISABLED
	}
	
	// External dependencies might get created by OnEnable.
	// Thus we hook them up after OnEnable
	AddExternalDependencyCallbacksToManagers ();
}

void MonoBehaviour::Deactivate (DeactivateOperation operation)
{
	// When loading a new level we don't want coroutines to stop running, so just ignore it.
	if (operation != kDeprecatedDeactivateToggleForLevelLoad)
		StopAllCoroutines ();
	
	Super::Deactivate (operation);
}


void MonoBehaviour::ReleaseMonoInstance ()
{
	Assert(m_ActiveCoroutines.empty());
	
	ScriptingObjectPtr instance = GetCachedScriptingObject();
	if (instance)
	{
		// In the player we protect against null reference exceptions by setting the instance id of the mono representation to 0.
		// This is necessary because mono classes might stick around for a bit longer until the GC cleans it up.
		// By setting the id to 0 it is impossible for another mono behaviour instance to load it from disk or other weird things.

		// In the editor we don't want to do this as it will cause references to scripts to
		// get lost when importing packages containing the referenced script.
		// ReleaseMonoInstance is called in that case because the script is getting deleted.


		#if !UNITY_EDITOR
		ScriptingObjectOfType<Object> wrapper(instance);
		wrapper.SetInstanceID(0);
		wrapper.SetCachedPtr(NULL);
		#endif

		SetCachedScriptingObject(SCRIPTING_NULL);
	}

	Assert(GetCachedScriptingObject() == SCRIPTING_NULL);
	
	m_Methods = NULL;
	if (m_ScriptCache != NULL)
	{
		const_cast<MonoScriptCache*> (m_ScriptCache)->Release();
		m_ScriptCache = NULL;
	}
}

#if UNITY_EDITOR
#define LogScriptError(x,script) DebugStringToFile (x, 0, __FILE__, __LINE__, kLog | kScriptCompileError, script, GetMonoManager().GetInstanceID ());
#endif

static UNITY_TLS_VALUE(int) s_MonoBehaviourInConstructorCounter;
int GetMonoBehaviourInConstructor()
{
	return s_MonoBehaviourInConstructorCounter;
}

static std::string SafeGetScriptFileName(MonoScript* script, ScriptingClassPtr klass)
{
	if (script)
		return script->GetName();
	
	if (klass)
		return scripting_class_get_name(klass);

	return "";
}

/////@TODO: THIS IS NOT REALLY THREAD SAFE. ALL MONO SCRIPT FUNCTIONS ARE NOT THREAD SAFE???
//// MONO SCRIPT LOADING SHOULD NOT BE DONE IN A DIFFERENT THREAD
/// ALL FUNCTIONS MODIFYING SCRIPT CONTENT MUST BE THREAD SAFE
void MonoBehaviour::RebuildMonoInstance (ScriptingObjectPtr instance)
{
	ReleaseMonoInstance ();

	MonoScript* script = 0;
	MonoScriptType type = kScriptTypeNotInitialized;

#if UNITY_EDITOR
	if (m_ScriptCache == NULL && !m_EditorClassIdentifier.empty())
	{
		std::string assembly;
		std::string ns;
		std::string klass;
		GetScriptClassIdComponents(m_EditorClassIdentifier, assembly, ns, klass);

		ScriptingClass* sc = GetMonoManager().GetMonoClassWithAssemblyName(klass, ns, assembly);
		m_ScriptCache = CreateMonoScriptCache(sc, true, this);
		if (m_ScriptCache != NULL)
		{
			m_ScriptCache->Retain();
			type = m_ScriptCache->scriptType;
		}
	}
#endif

	if (m_ScriptCache == NULL)
	{
		script = dynamic_pptr_cast<MonoScript*> (InstanceIDToObjectThreadSafe(m_Script.GetInstanceID()));
		if (script)
		{
			m_ScriptCache = script->GetScriptCache ();
			if (m_ScriptCache != NULL)
			{
				m_ScriptCache->Retain();
				type = m_ScriptCache->scriptType;
			}
		}
		else
			type = kScriptTypeScriptMissing;
	}

	// We want a warning to be printed only once, that is when entering play mode and not when returning back
	if (IsWorldPlaying() && !IsValidScriptType(type))
		WarningStringObject (FormatScriptTypeError(type, SafeGetScriptFileName(script, GetClass())), this);

	if (!IsValidScriptType(type))
		return;
	
	AssertIf(GetCachedScriptingObject() != SCRIPTING_NULL);
	Assert(m_ScriptCache != NULL && m_ScriptCache->klass != NULL);

	if (instance == SCRIPTING_NULL)
	{
		ScriptingObjectPtr newInstance;
#if ENABLE_MONO
		SET_ALLOC_OWNER(s_MonoDomainContainer);
#endif
		// Instantiate Mono class, handle exception		
		newInstance = scripting_object_new (m_ScriptCache->klass);
		if (newInstance == SCRIPTING_NULL)
		{
			if (IsWorldPlaying())
				WarningStringObject (Format("The script behaviour '%s' could not be instantiated!", script->GetScriptClassName().c_str()), this);
			return;
		}

		/// Setup Mono object pointers
		Scripting::ConnectScriptingWrapperToObject(newInstance, this);

		// Prevent certain functions inside constructor
		int constructorCount = s_MonoBehaviourInConstructorCounter;
		constructorCount++;
		s_MonoBehaviourInConstructorCounter = constructorCount;
		
		ScriptingExceptionPtr exception = NULL;

		scripting_object_invoke_default_constructor(GetInstance(), &exception);
		
		DebugAssertIf(constructorCount != s_MonoBehaviourInConstructorCounter);
		constructorCount--;
		s_MonoBehaviourInConstructorCounter = constructorCount;

		if (exception)
			Scripting::LogException(exception, Scripting::GetInstanceIDFromScriptingWrapper(newInstance));
	}
	else
	{
		Scripting::ConnectScriptingWrapperToObject (instance, this);
	}
	
	// Get methods array from script
	m_Methods = m_ScriptCache->methods.begin();
	
	#if UNITY_EDITOR
	m_EditorHideFlags |= (script && script->IsBuiltinScript()) ? kHideScriptPPtr : 0;
	#endif
}

void MonoBehaviour::RebuildMonoInstanceFromScriptChange (ScriptingObjectPtr instance)
{
	if (IsAddedToManager ())
		RemoveFromManager ();
	
	RebuildMonoInstance (instance);
#if UNITY_EDITOR
	if (GetInstance() != NULL)
		SetBackup(NULL);
#endif
	
	if (IsAddedToManager ())
		AddToManager ();
}

void MonoBehaviour::SetScript (const PPtr<MonoScript>& newScript, ScriptingObjectPtr instance)
{
	if (m_Script != newScript)
	{
		//LockPlayerLoop();
		
		m_Script = newScript;
		
		RebuildMonoInstanceFromScriptChange(instance);

		//UnlockPlayerLoop();
	}
	else if (IsWorldPlaying () && newScript == PPtr<MonoScript> (0))
	{
		WarningStringObject ("The referenced script on this Behaviour is missing!", this);
	}
}

#if UNITY_EDITOR

void MonoBehaviour::SetClassIdentifier (const std::string& id)
{
	if (m_EditorClassIdentifier != id)
	{
		//LockPlayerLoop();

		m_EditorClassIdentifier = id;
			
		RebuildMonoInstanceFromScriptChange(NULL);

		//UnlockPlayerLoop();
	}
	//m_EditorClassIdentifier = id;
}

bool MonoBehaviour::CanAssignMonoVariable (const char* propertyType, Object* object)
{
	return CanAssignMonoVariableStatic(propertyType, object);
}

bool MonoBehaviour::CanAssignMonoVariableStatic (const char* propertyType, Object* object)
{
	MonoObject* instance = Scripting::ScriptingWrapperFor(object);
	if (!instance)
		return false;
	
	MonoClass* assignmentClass = mono_object_get_class(instance);
	while (assignmentClass != NULL)
	{
		if (strcmp(mono_class_get_name(assignmentClass), propertyType) == 0)
			return true;
	
		assignmentClass = mono_class_get_parent(assignmentClass);
	}
	
	return false;
}

#if UNITY_LOGIC_GRAPH
void MonoBehaviour::LoadLogicGraphInEditor ()
{
	// At the moment we compile LogicGraphs just in time when loading scenes.
	// MonoBehaviour instances are currently created on the loading thread -> Before logic graphs are compiled
	// Thus we need this hack in the editor to ensure that the logic graph is active using the latest graph data
	MonoScript* script = m_Script;
	if (script != NULL && script->GetEditorGraphData() != NULL && IsWorldPlaying())
	{
		script->Rebuild(CompileAndLoadLogicGraph(*script, false));
		RebuildMonoInstance(NULL);
	}
}
#endif


void MonoBehaviour::DidReloadDomain ()
{
#if ENABLE_AUDIO_FMOD
	// release custom filter
	delete m_AudioCustomFilter;
	m_AudioCustomFilter = NULL;
#endif
	
	if (IsAddedToManager())
		AddToManager();
	else
	{
		MonoScript* script = m_Script;
		if (GetInstance() == NULL || script == NULL)
			return;
		
		if (!IsScriptableObject())
			return;
		
		ScriptingMethodPtr method = m_Methods[MonoScriptCache::kAddToManager];
		if (method != NULL)
			CallMethodInactive(method);
	}
}

bool MonoBehaviour::ShouldDisplayEnabled ()
{
	if (m_EditorHideFlags & kHideEnabled)
		return false;
	if (GetInstance())
	{
		return m_Methods[MonoScriptCache::kUpdate] || m_Methods[MonoScriptCache::kFixedUpdate] || 
		       m_Methods[MonoScriptCache::kLateUpdate] ||
		       m_Methods[MonoScriptCache::kRenderImageFilter] ||
		       m_Methods[MonoScriptCache::kCoroutineStart] || m_Methods[MonoScriptCache::kCoroutineMain] ||
			   m_Methods[MonoScriptCache::kGUI] ||
			   m_Methods[MonoScriptCache::kAddToManager] || m_Methods[MonoScriptCache::kRemoveFromManager] ||
			   m_Methods[MonoScriptCache::kAudioFilterRead];
	}
	else
		return true;
}



#endif // UNITY_EDITOR

char const* MonoBehaviour::GetName () const
{
	const GameObject* go = GetGameObjectPtr();
	if (go)
		return go->GetName();
	else
		return m_Name.c_str ();
}

void MonoBehaviour::SetName (char const* name)
{
	GameObject* go = GetGameObjectPtr();
	if (go)
		return go->SetName(name);
	else
	{
		m_Name = name;
		SetDirty();
	}
} 

ScriptingClassPtr MonoBehaviour::GetClass ()
{
	if (m_ScriptCache != NULL)
		return m_ScriptCache->klass;
	else
		return NULL;
}

const MonoBehaviour::MethodCache* MonoBehaviour::GetMethodCache ()
{
	if (m_ScriptCache != NULL)
		return &m_ScriptCache->methodCache;
	else
		return NULL;
}


std::string MonoBehaviour::GetScriptClassName ()
{
	MonoScript* script = m_Script;
	if (script)
		return script->GetScriptClassName();
	else
		return string();
}

std::string MonoBehaviour::GetScriptFullClassName ()
{
	MonoScript* script = m_Script;
	if (script)
		return script->GetScriptFullClassName();
	else
		return string();
}

#if ENABLE_MONO || UNITY_WINRT
ScriptingArrayPtr RequiredComponentsOf(ScriptingClassPtr klass)
{
	// Extract component requirements
	ScriptingObjectPtr typeObject = scripting_class_get_object(klass);
	ScriptingInvocation invocation(GetMonoManager().GetCommonClasses().extractRequiredComponents);
	invocation.AddObject(typeObject);
	return scripting_cast_object_to_array(invocation.Invoke());
}

ScriptingArrayPtr RequiredComponentsOf(MonoBehaviour* script)
{
	return RequiredComponentsOf(script->GetClass());
}
#endif

void ResetAndApplyDefaultReferencesOnNewMonoBehaviour(MonoBehaviour& behaviour)
{

#if UNITY_EDITOR
	MonoScript* script = behaviour.GetScript();
	if (script && (script->GetScriptType() == kScriptTypeEditorScriptableObjectDerived || !IsWorldPlaying()))
		ApplyDefaultReferences(behaviour, script->GetDefaultReferences());
#endif
	
	behaviour.Reset();
	behaviour.SmartReset();
	
	behaviour.AwakeFromLoad(kInstantiateOrCreateFromCodeAwakeFromLoad);
}

#undef LogScriptError
#endif