aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/XMREngine/XMREngine.cs
blob: 7447f2f55e720bc7c1db6d85f70cd6cd7b9f3973 (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
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSimulator Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// based on XMREngine from Mike Rieker (Dreamnation) and Melanie Thielker
// but with several changes to be more cross platform.


using log4net;
using Mono.Addins;
using Nini.Config;
using OpenSim.Framework;
using OpenSim.Framework.Console;
using OpenSim.Framework.Monitoring;
using OpenSim.Region.ClientStack.Linden;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
using OpenSim.Region.ScriptEngine.Interfaces;
using OpenSim.Region.ScriptEngine.Shared;
using OpenSim.Region.ScriptEngine.Shared.Api;
using OpenMetaverse;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Timers;
using System.Xml;

using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;

[assembly: Addin("XMREngine", OpenSim.VersionInfo.VersionNumber)]
[assembly: AddinDependency("OpenSim.Region.Framework", OpenSim.VersionInfo.VersionNumber)]

namespace OpenSim.Region.ScriptEngine.XMREngine
{
    [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XMREngine")]
    public partial class XMREngine : INonSharedRegionModule, IScriptEngine,
            IScriptModule
    {
        public  static readonly DetectParams[] zeroDetectParams = new DetectParams[0];
        private static ArrayList noScriptErrors = new ArrayList();
        public  static readonly ILog m_log =
            LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static readonly string[] scriptReferencedAssemblies = new string[0];

        private bool m_LateInit;
        private bool m_TraceCalls;
        public  bool m_Verbose;
        public  bool m_ScriptDebug;
        public  Scene m_Scene;
        private IConfigSource m_ConfigSource;
        private IConfig m_Config;
        private string m_ScriptBasePath;
        private bool m_Enabled = false;
        public  bool m_StartProcessing = false;
        public  bool m_UseSourceHashCode = false;
        public  ConstructorInfo uThreadCtor;
        private Dictionary<UUID, ArrayList> m_ScriptErrors =
                new Dictionary<UUID, ArrayList>();
        private Dictionary<UUID, List<UUID>> m_ObjectItemList =
                new Dictionary<UUID, List<UUID>>();
        private Dictionary<UUID, XMRInstance[]> m_ObjectInstArray =
                new Dictionary<UUID, XMRInstance[]>();
        public  Dictionary<string,FieldInfo> m_XMRInstanceApiCtxFieldInfos =
                new Dictionary<string,FieldInfo> ();
        private int m_StackSize;
        private int m_HeapSize;

        private XMRScriptThread[] m_ScriptThreads;
        private int    m_WakeUpOne  = 0;
        public  object m_WakeUpLock = new object();
        private Dictionary<Thread,XMRScriptThread> m_AllThreads = new Dictionary<Thread,XMRScriptThread> ();

        private bool m_SuspendScriptThreadFlag = false;
        /**
         * @brief Something was just added to the Start or Yield queue so
         *        wake one of the XMRScriptThread instances to run it.
         */

        private Thread m_SleepThread = null;
        private bool m_Exiting = false;

        private int m_MaintenanceInterval = 10;
        private System.Timers.Timer m_MaintenanceTimer;
        public  int numThreadScriptWorkers;

        private object m_FrameUpdateLock = new object ();
        private event ThreadStart m_FrameUpdateList = null;

         // Various instance lists:
         //   m_InstancesDict = all known instances
         //                     find an instance given its itemID
         //   m_StartQueue = instances that have just had event queued to them
         //   m_YieldQueue = instances that are ready to run right now
         //   m_SleepQueue = instances that have m_SleepUntil valid
         //                  sorted by ascending m_SleepUntil

        private Dictionary<UUID, XMRInstance> m_InstancesDict =
                new Dictionary<UUID, XMRInstance>();
        public  Queue<ThreadStart> m_ThunkQueue = new Queue<ThreadStart> ();
        public  XMRInstQueue m_StartQueue = new XMRInstQueue();
        public  XMRInstQueue m_YieldQueue = new XMRInstQueue();
        public  XMRInstQueue m_SleepQueue = new XMRInstQueue();
        private string m_LockedDict = "nobody";

        public XMREngine()
        {
        }

        public string Name
        {
            get { return "XMREngine"; }
        }

        public Type ReplaceableInterface
        {
            get { return null; }
        }

        public string ScriptEnginePath
        {
            get { return m_ScriptBasePath; }
        }

        public string ScriptClassName
        {
            get { return "XMREngineScript"; }
        }

        public string ScriptBaseClassName
        {
            get { return typeof (XMRInstance).FullName; }
        }

        public ParameterInfo[] ScriptBaseClassParameters
        {
            get { return typeof(XMRInstance).GetConstructor (new Type[] { typeof (WaitHandle) }).GetParameters (); }
        }

        public string[] ScriptReferencedAssemblies
        {
            get { return scriptReferencedAssemblies; }
        }

        public void WakeUpOne()
        {
            lock (m_WakeUpLock)
            {
                m_WakeUpOne++;
                Monitor.Pulse(m_WakeUpLock);
            }
        }

        public void AddThread(Thread thd, XMRScriptThread xthd)
        {
            lock(m_AllThreads)
                m_AllThreads.Add(thd, xthd);
        }

        public void RemoveThread(Thread thd)
        {
            lock(m_AllThreads)
                m_AllThreads.Remove(thd);
        }

        public XMRScriptThread CurrentScriptThread ()
        {
            XMRScriptThread st;
            lock (m_AllThreads)
                m_AllThreads.TryGetValue (Thread.CurrentThread, out st);

            return st;
        }

        public void Initialise(IConfigSource config)
        {
            TraceCalls("[XMREngine]: Initialize entry");
            m_ConfigSource = config;

            ////foreach (IConfig icfg in config.Configs) {
            ////    m_log.Debug("[XMREngine]: Initialise: configs[" + icfg.Name + "]");
            ////    foreach (string key in icfg.GetKeys ()) {
            ////        m_log.Debug("[XMREngine]: Initialise:     " + key + "=" + icfg.GetExpanded (key));
            ////    }
            ////}

            m_Enabled = false;
            m_Config = config.Configs["XMREngine"];
            if (m_Config == null)
            {
                m_log.Info("[XMREngine]: no config, assuming disabled");
                return;
            }

            m_Enabled = m_Config.GetBoolean("Enabled", false);
            m_log.InfoFormat("[XMREngine]: config enabled={0}", m_Enabled);
            if (!m_Enabled)
                return;

            Type uThreadType = null;
            uThreadType = typeof (ScriptUThread_Nul);
            uThreadCtor = uThreadType.GetConstructor (new Type[] { typeof (XMRInstance) });

            m_UseSourceHashCode    = m_Config.GetBoolean("UseSourceHashCode", false);
            numThreadScriptWorkers = m_Config.GetInt("NumThreadScriptWorkers", 3);
            m_ScriptThreads        = new XMRScriptThread[numThreadScriptWorkers];

            m_TraceCalls           = m_Config.GetBoolean("TraceCalls", false);
            m_Verbose              =  m_Config.GetBoolean("Verbose", false);
            m_ScriptDebug          = m_Config.GetBoolean("ScriptDebug", false);

             // Verify that our ScriptEventCode's match OpenSim's scriptEvent's.
            bool err = false;
            for (int i = 0; i < 32; i ++)
            {
                string mycode = "undefined";
                string oscode = "undefined";
                try
                {
                    mycode = ((ScriptEventCode)i).ToString();
                    Convert.ToInt32(mycode);
                    mycode = "undefined";
                }
                catch { }
                try
                {
                    oscode = ((OpenSim.Region.Framework.Scenes.scriptEvents)(1 << i)).ToString();
                    Convert.ToInt32(oscode);
                    oscode = "undefined";
                }
                catch { }
                if (mycode != oscode)
                {
                    m_log.ErrorFormat("[XMREngine]: {0} mycode={1}, oscode={2}", i, mycode, oscode);
                    err = true;
                }
            }
            if (err)
            {
                m_Enabled = false;
                return;
            }

            for (int i = 0; i < numThreadScriptWorkers; i ++)
            {
                m_ScriptThreads[i] = new XMRScriptThread(this, i);;
            }


            m_SleepThread = StartMyThread(RunSleepThread, "xmrengine sleep", ThreadPriority.Normal);

            m_StackSize = m_Config.GetInt("ScriptStackSize", 2048) << 10;
            m_HeapSize  = m_Config.GetInt("ScriptHeapSize",  1024) << 10;

            m_log.InfoFormat("[XMREngine]: Enabled, {0}.{1} Meg (0x{2}) stacks",
                    (m_StackSize >> 20).ToString (),
                    (((m_StackSize % 0x100000) * 1000) 
                            >> 20).ToString ("D3"),
                    m_StackSize.ToString ("X"));

            m_log.InfoFormat("[XMREngine]:  ... {0}.{1} Meg (0x{2}) heaps",
                    (m_HeapSize >> 20).ToString (),
                    (((m_HeapSize % 0x100000) * 1000) 
                            >> 20).ToString ("D3"),
                    m_HeapSize.ToString ("X"));

            m_MaintenanceInterval = m_Config.GetInt("MaintenanceInterval", 10);

            if (m_MaintenanceInterval > 0)
            {
                m_MaintenanceTimer = new System.Timers.Timer(m_MaintenanceInterval * 60000);
                m_MaintenanceTimer.Elapsed += DoMaintenance;
                m_MaintenanceTimer.Start();
            }

            MainConsole.Instance.Commands.AddCommand("xmr", false,
                    "xmr",
                    "xmr [...|help|...] ...",
                    "Run xmr script engine commands",
                    RunTest);

            TraceCalls("[XMREngine]: Initialize successful");
        }

        public void AddRegion(Scene scene)
        {
            if (!m_Enabled)
                return;

            TraceCalls("[XMREngine]: XMREngine.AddRegion({0})", scene.RegionInfo.RegionName);

            m_Scene = scene;

            m_Scene.RegisterModuleInterface<IScriptModule>(this);

            m_ScriptBasePath = m_Config.GetString ("ScriptBasePath", "ScriptData");
            m_ScriptBasePath = Path.Combine (m_ScriptBasePath, scene.RegionInfo.RegionID.ToString());

            Directory.CreateDirectory(m_ScriptBasePath);

            m_Scene.EventManager.OnRezScript += OnRezScript;

            m_Scene.StackModuleInterface<IScriptModule>(this);
        }

        private void OneTimeLateInitialization ()
        {
             // Build list of defined APIs and their 'this' types and define a field in XMRInstanceSuperType.
            ApiManager am = new ApiManager ();
            Dictionary<string,Type> apiCtxTypes = new Dictionary<string,Type> ();
            foreach (string api in am.GetApis ())
            {
                m_log.Debug ("[XMREngine]: adding api " + api);
                IScriptApi scriptApi = am.CreateApi (api);
                Type apiCtxType = scriptApi.GetType ();
                if (api == "LSL") apiCtxType = typeof (XMRLSL_Api);
                apiCtxTypes[api] = apiCtxType;
            }

            if (ScriptCodeGen.xmrInstSuperType == null) // Only create type once!
            {
                 // Start creating type XMRInstanceSuperType that contains a field
                 // m_ApiManager_<APINAME> that points to the per-instance context
                 // struct for that API, ie, the 'this' value passed to all methods
                 // in that API.  It is in essence:

                 //  public class XMRInstanceSuperType : XMRInstance {
                 //      public XMRLSL_Api m_ApiManager_LSL;   // 'this' value for all ll...() functions
                 //      public MOD_Api    m_ApiManager_MOD;   // 'this' value for all mod...() functions
                 //      public OSSL_Api   m_ApiManager_OSSL;  // 'this' value for all os...() functions
                 //              ....
                 //  }

                AssemblyName assemblyName = new AssemblyName ();
                assemblyName.Name = "XMRInstanceSuperAssembly";
                AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
#if DEBUG
                Type daType = typeof(DebuggableAttribute);
                ConstructorInfo daCtor = daType.GetConstructor(new Type[] { typeof(DebuggableAttribute.DebuggingModes) });

                CustomAttributeBuilder daBuilder = new CustomAttributeBuilder(daCtor, new object[] {
                    DebuggableAttribute.DebuggingModes.DisableOptimizations |
                    DebuggableAttribute.DebuggingModes.EnableEditAndContinue |
                    DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints |
                    DebuggableAttribute.DebuggingModes.Default });

                assemblyBuilder.SetCustomAttribute(daBuilder);
#endif
                ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("XMRInstanceSuperModule");
                TypeBuilder typeBuilder = moduleBuilder.DefineType("XMRInstanceSuperType", TypeAttributes.Public | TypeAttributes.Class);
                typeBuilder.SetParent(typeof (XMRInstance));

                foreach (string apiname in apiCtxTypes.Keys)
                {
                    string fieldName = "m_ApiManager_" + apiname;
                    typeBuilder.DefineField (fieldName, apiCtxTypes[apiname], FieldAttributes.Public);
                }

                 // Finalize definition of XMRInstanceSuperType.
                 // Give the compiler a short name to reference it by,
                 // otherwise it will try to use the AssemblyQualifiedName
                 // and fail miserably.
                ScriptCodeGen.xmrInstSuperType = typeBuilder.CreateType ();
                ScriptObjWriter.DefineInternalType ("xmrsuper", ScriptCodeGen.xmrInstSuperType);
            }

             // Tell the compiler about all the constants and methods for each API.
             // We also tell the compiler how to get the per-instance context for each API
             // by reading the corresponding m_ApiManager_<APINAME> field of XMRInstanceSuperType.

            foreach (KeyValuePair<string,Type> kvp in apiCtxTypes)
            {
                // get API name and the corresponding per-instance context type
                string api = kvp.Key;
                Type apiCtxType = kvp.Value;

                // give script compiler an abbreviated name for the API context type
                ScriptObjWriter.DefineInternalType ("apimanager_" + api, apiCtxType);

                // this field tells the compiled code where the per-instance API context object is
                // eg, for the OSSL API, it is in ((XMRInstanceSuperType)inst).m_ApiManager_OSSL
                string fieldName = "m_ApiManager_" + api;
                FieldInfo fieldInfo = ScriptCodeGen.xmrInstSuperType.GetField (fieldName);
                m_XMRInstanceApiCtxFieldInfos[api] = fieldInfo;

                // now tell the compiler about the constants and methods for the API
                ScriptConst.AddInterfaceConstants   (null, apiCtxType.GetFields ());
                TokenDeclInline.AddInterfaceMethods (null, apiCtxType.GetMethods (), fieldInfo);
            }

             // Add sim-specific APIs to the compiler.

            IScriptModuleComms comms = m_Scene.RequestModuleInterface<IScriptModuleComms> ();
            if (comms != null)
            {
                 // Add methods to list of built-in functions.
                Delegate[] methods = comms.GetScriptInvocationList ();
                foreach (Delegate m in methods)
                {
                    MethodInfo mi = m.Method;
                    try
                    {
                        CommsCallCodeGen cccg = new CommsCallCodeGen (mi, comms, m_XMRInstanceApiCtxFieldInfos["MOD"]);
                        Verbose ("[XMREngine]: added comms function " + cccg.fullName);
                    }
                    catch (Exception e)
                    {
                        m_log.Error ("[XMREngine]: failed to add comms function " + mi.Name);
                        m_log.Error ("[XMREngine]: - " + e.ToString ());
                    }
                }

                 // Add constants to list of built-in constants.

                Dictionary<string,object> consts = comms.GetConstants ();
                foreach (KeyValuePair<string,object> kvp in consts)
                {
                    try
                    {
                        ScriptConst sc = ScriptConst.AddConstant (kvp.Key, kvp.Value);
                        Verbose ("[XMREngine]: added comms constant " + sc.name);
                    }
                    catch (Exception e)
                    {
                        m_log.Error ("[XMREngine]: failed to add comms constant " + kvp.Key);
                        m_log.Error ("[XMREngine]: - " + e.Message);
                    }
                }
            }
            else
            {
                Verbose ("[XMREngine]: comms not enabled");
            }
        }

        /**
         * @brief Generate code for the calls to the comms functions.
         *        It is a tRUlY EvIL interface.
         *        To call the function we must call an XMRInstanceSuperType.m_ApiManager_MOD.modInvoker?()
         *        method passing it the name of the function as a string and the script
         *        argument list wrapped up in an object[] array.  The modInvoker?() methods 
         *        do some sick type conversions (with corresponding mallocs) so we can't 
         *        call the methods directly.
         */
        private class CommsCallCodeGen : TokenDeclInline
        {
            private static Type[] modInvokerArgTypes = new Type[] { typeof (string), typeof (object[]) };
            public  static FieldInfo xmrInstModApiCtxField;

            private MethodInfo modInvokerMeth;
            private string methName;

            /**
             * @brief Constructor
             * @param mi = method to make available to scripts
             *        mi.Name = name that is used by scripts
             *        mi.GetParameters() = parameter list as defined by module
             *                             includes the 'UUID host','UUID script' parameters that script does not see
             *                             allowed types for script-visible parameters are as follows:
             *                                    Single -> float
             *                                     Int32 -> integer
             *                        OpenMetaverse.UUID -> key
             *                                  Object[] -> list
             *                  OpenMetaverse.Quaternion -> rotation
             *                                    String -> string
             *                     OpenMetaverse.Vector3 -> vector
             *        mi.ReturnType = return type as defined by module
             *                        types are same as allowed for parameters
             * @param comms = comms module the method came from
             * @param apictxfi = what field in XMRInstanceSuperType the 'this' value is for this method
             */
            public CommsCallCodeGen (MethodInfo mi, IScriptModuleComms comms, FieldInfo apictxfi)
                    : base (null, false, NameArgSig (mi), RetType (mi))
            {
                methName = mi.Name;
                string modInvokerName = comms.LookupModInvocation (methName);
                if (modInvokerName == null)
                    throw new Exception("cannot find comms method " + methName);
                modInvokerMeth = typeof(MOD_Api).GetMethod(modInvokerName, modInvokerArgTypes);
                xmrInstModApiCtxField = apictxfi;
            }

            // script-visible name(argtype,...) signature string
            private static string NameArgSig (MethodInfo mi)
            {
                StringBuilder sb = new StringBuilder ();
                sb.Append (mi.Name);
                sb.Append ('(');
                ParameterInfo[] mps = mi.GetParameters ();
                for (int i = 2; i < mps.Length; i ++)
                {
                    ParameterInfo pi = mps[i];
                    if (i > 2) sb.Append (',');
                    sb.Append (ParamType (pi.ParameterType));
                }
                sb.Append (')');
                return sb.ToString ();
            }

            // script-visible return type
            // note that although we support void, the comms stuff does not
            private static TokenType RetType (MethodInfo mi)
            {
                Type rt = mi.ReturnType;
                if (rt == typeof (float))                    return new TokenTypeFloat (null);
                if (rt == typeof (int))                      return new TokenTypeInt   (null);
                if (rt == typeof (object[]))                 return new TokenTypeList  (null);
                if (rt == typeof (OpenMetaverse.UUID))       return new TokenTypeKey   (null);
                if (rt == typeof (OpenMetaverse.Quaternion)) return new TokenTypeRot   (null);
                if (rt == typeof (string))                   return new TokenTypeStr   (null);
                if (rt == typeof (OpenMetaverse.Vector3))    return new TokenTypeVec   (null);
                if (rt == null || rt == typeof (void))       return new TokenTypeVoid  (null);
                throw new Exception ("unsupported return type " + rt.Name);
            }

            // script-visible parameter type
            private static string ParamType (Type t)
            {
                if (t == typeof (float))                    return "float";
                if (t == typeof (int))                      return "integer";
                if (t == typeof (OpenMetaverse.UUID))       return "key";
                if (t == typeof (object[]))                 return "list";
                if (t == typeof (OpenMetaverse.Quaternion)) return "rotation";
                if (t == typeof (string))                   return "string";
                if (t == typeof (OpenMetaverse.Vector3))    return "vector";
                throw new Exception ("unsupported parameter type " + t.Name);
            }

            /**
             * @brief Called by the compiler to generate a call to the comms function.
             * @param scg = which script is being compiled
             * @param errorAt = where in the source code the call is being made (for error messages)
             * @param result = a temp location to put the return value in if any
             * @param args = array of script-visible arguments being passed to the function
             */
            public override void CodeGen (ScriptCodeGen scg, Token errorAt, CompValuTemp result, CompValu[] args)
            {
                 // Set up 'this' pointer for modInvoker?() = value from ApiManager.CreateApi("MOD").
                scg.PushXMRInst ();
                scg.ilGen.Emit (errorAt, OpCodes.Castclass, xmrInstModApiCtxField.DeclaringType);
                scg.ilGen.Emit (errorAt, OpCodes.Ldfld, xmrInstModApiCtxField);

                 // Set up 'fname' argument to modInvoker?() = name of the function to be called.
                scg.ilGen.Emit (errorAt, OpCodes.Ldstr, methName);

                 // Set up 'parms' argument to modInvoker?() = object[] of the script-visible parameters,
                 // in their LSL-wrapped form.  Of course, the modInvoker?() method will malloc yet another 
                 // object[] and type-convert these parameters one-by-one with another round of unwrapping 
                 // and wrapping.
                 // Types allowed in this object[]:
                 //    LSL_Float, LSL_Integer, LSL_Key, LSL_List, LSL_Rotation, LSL_String, LSL_Vector

                int nargs = args.Length;
                scg.ilGen.Emit (errorAt, OpCodes.Ldc_I4, nargs);
                scg.ilGen.Emit (errorAt, OpCodes.Newarr, typeof (object));

                for (int i = 0; i < nargs; i ++)
                {
                    scg.ilGen.Emit (errorAt, OpCodes.Dup);
                    scg.ilGen.Emit (errorAt, OpCodes.Ldc_I4, i);

                    // get location and type of argument
                    CompValu arg = args[i];
                    TokenType argtype = arg.type;

                    // if already in a form acceptable to modInvoker?(),
                    // just push it to the stack and convert to object
                    // by boxing it if necessary

                    // but if something like a double, int, string, etc
                    // push to stack converting to the LSL-wrapped type
                    // then convert to object by boxing if necessary

                    Type boxit = null;
                    if (argtype is TokenTypeLSLFloat)
                    {
                        args[i].PushVal (scg, errorAt);
                        boxit = typeof (LSL_Float);
                    }
                    else if (argtype is TokenTypeLSLInt)
                    {
                        args[i].PushVal (scg, errorAt);
                        boxit = typeof (LSL_Integer);
                    }
                    else if (argtype is TokenTypeLSLKey)
                    {
                        args[i].PushVal (scg, errorAt);
                        boxit = typeof (LSL_Key);
                    }
                    else if (argtype is TokenTypeList)
                    {
                        args[i].PushVal (scg, errorAt);
                        boxit = typeof (LSL_List);
                    }
                    else if (argtype is TokenTypeRot)
                    {
                        args[i].PushVal (scg, errorAt);
                        boxit = typeof (LSL_Rotation);
                    }
                    else if (argtype is TokenTypeLSLString)
                    {
                        args[i].PushVal (scg, errorAt);
                        boxit = typeof (LSL_String);
                    }
                    else if (argtype is TokenTypeVec)
                    {
                        args[i].PushVal (scg, errorAt);
                        boxit = typeof (LSL_Vector);
                    }
                    else if (argtype is TokenTypeFloat)
                    {
                        args[i].PushVal (scg, errorAt, new TokenTypeLSLFloat (argtype));
                        boxit = typeof (LSL_Float);
                    }
                    else if (argtype is TokenTypeInt)
                    {
                        args[i].PushVal (scg, errorAt, new TokenTypeLSLInt (argtype));
                        boxit = typeof (LSL_Integer);
                    }
                    else if (argtype is TokenTypeKey)
                    {
                        args[i].PushVal (scg, errorAt, new TokenTypeLSLKey (argtype));
                        boxit = typeof (LSL_Key);
                    }
                    else if (argtype is TokenTypeStr)
                    {
                        args[i].PushVal (scg, errorAt, new TokenTypeLSLString (argtype));
                        boxit = typeof (LSL_String);
                    }
                    else
                        throw new Exception ("unsupported arg type " + argtype.GetType ().Name);

                    if (boxit.IsValueType)
                        scg.ilGen.Emit (errorAt, OpCodes.Box, boxit);

                    // pop the object into the object[]
                    scg.ilGen.Emit (errorAt, OpCodes.Stelem, typeof (object));
                }

                 // Call the modInvoker?() method.
                 // It leaves an LSL-wrapped type on the stack.
                if (modInvokerMeth.IsVirtual)
                    scg.ilGen.Emit (errorAt, OpCodes.Callvirt, modInvokerMeth);
                else
                    scg.ilGen.Emit (errorAt, OpCodes.Call, modInvokerMeth);

                 // The 3rd arg to Pop() is the type on the stack, 
                 // ie, what modInvoker?() actually returns.
                 // The Pop() method will wrap/unwrap as needed.
                Type retSysType = modInvokerMeth.ReturnType;
                if (retSysType == null)
                    retSysType = typeof (void);
                TokenType retTokType = TokenType.FromSysType (errorAt, retSysType);
                result.Pop (scg, errorAt, retTokType);
            }
        }

        /**
         * @brief Called late in shutdown procedure,
         *        after the 'Shutting down..." message.
         */
        public void RemoveRegion(Scene scene)
        {
            if (!m_Enabled)
                return;

            TraceCalls("[XMREngine]: XMREngine.RemoveRegion({0})", scene.RegionInfo.RegionName);

             // Write script states out to .state files so it will be
             // available when the region is restarted.
            DoMaintenance(null, null);

             // Stop executing script threads and wait for final
             // one to finish (ie, script gets to CheckRun() call).
            m_Exiting = true;

            m_Scene.EventManager.OnFrame -= OnFrame;
            m_Scene.EventManager.OnRezScript -= OnRezScript;
            m_Scene.EventManager.OnRemoveScript -= OnRemoveScript;
            m_Scene.EventManager.OnScriptReset -= OnScriptReset;
            m_Scene.EventManager.OnStartScript -= OnStartScript;
            m_Scene.EventManager.OnStopScript -= OnStopScript;
            m_Scene.EventManager.OnGetScriptRunning -= OnGetScriptRunning;
            m_Scene.EventManager.OnShutdown -= OnShutdown;

            for (int i = 0; i < numThreadScriptWorkers; i ++)
            {
                XMRScriptThread scriptThread = m_ScriptThreads[i];
                if (scriptThread != null)
                {
                    scriptThread.WakeUpScriptThread();
                    Monitor.PulseAll (m_WakeUpLock);
                    scriptThread.Terminate();
                    m_ScriptThreads[i] = null;
                }
            }

            if (m_SleepThread != null)
            {
                lock (m_SleepQueue)
                {
                    Monitor.PulseAll (m_SleepQueue);
                }
                if(!m_SleepThread.Join(250))
                    m_SleepThread.Abort();
                m_SleepThread = null;
            }

            m_Enabled = false;
            m_Scene = null;
        }

        public void RegionLoaded(Scene scene)
        {
            if (!m_Enabled)
                return;

            TraceCalls("[XMREngine]: XMREngine.RegionLoaded({0})", scene.RegionInfo.RegionName);

            m_Scene.EventManager.OnFrame += OnFrame;
            m_Scene.EventManager.OnRemoveScript += OnRemoveScript;
            m_Scene.EventManager.OnScriptReset += OnScriptReset;
            m_Scene.EventManager.OnStartScript += OnStartScript;
            m_Scene.EventManager.OnStopScript += OnStopScript;
            m_Scene.EventManager.OnGetScriptRunning += OnGetScriptRunning;
            m_Scene.EventManager.OnShutdown += OnShutdown;

            InitEvents();
        }

        public void StartProcessing()
        {
            m_log.Debug ("[XMREngine]: StartProcessing entry");
            m_Scene.EventManager.TriggerEmptyScriptCompileQueue (0, "");
            m_StartProcessing = true;
            for (int i = 0; i < numThreadScriptWorkers; i ++) {
                WakeUpOne();
            }
            m_log.Debug ("[XMREngine]: StartProcessing return");
        }

        public void Close()
        {
            TraceCalls("[XMREngine]: XMREngine.Close()");
        }

        private void RunTest (string module, string[] args)
        {
            if (args.Length < 2)
            {
                m_log.Info ("[XMREngine]: missing command, try 'xmr help'");
                return;
            }

            switch (args[1])
            {
                case "cvv":
                    switch (args.Length)
                    {
                        case 2:
                            m_log.InfoFormat ("[XMREngine]: compiled version value = {0}", 
                                    ScriptCodeGen.COMPILED_VERSION_VALUE);
                            break;

                        case 3:
                            try
                            {
                                ScriptCodeGen.COMPILED_VERSION_VALUE = Convert.ToInt32 (args[2]);
                            }
                            catch
                            {
                                m_log.Error ("[XMREngine]: bad/missing version number");
                            }
                            break;

                        default:
                            m_log.Error ("[XMREngine]: xmr cvv [<new_compiled_version_value>]");
                            break;
                    }
                    break;

                case "echo":
                    for (int i = 0; i < args.Length; i ++)
                        m_log.Info ("[XMREngine]: echo[" + i + "]=<" + args[i] + ">");

                    break;

                case "gc":
                    GC.Collect();
                    break;

                case "help":
                case "?": 
                    m_log.Info ("[XMREngine]: xmr cvv [<newvalue>] - show/set compiled version value");
                    m_log.Info ("[XMREngine]: xmr gc");
                    m_log.Info ("[XMREngine]: xmr ls [-help ...]");
                    m_log.Info ("[XMREngine]: xmr mvv [<newvalue>] - show/set migration version value");
                    m_log.Info ("[XMREngine]: xmr pev [-help ...] - post event");
                    m_log.Info ("[XMREngine]: xmr reset [-help ...]");
                    m_log.Info ("[XMREngine]: xmr resume - resume script processing");
                    m_log.Info ("[XMREngine]: xmr suspend - suspend script processing");
                    m_log.Info ("[XMREngine]: xmr tracecalls [yes | no]");
                    m_log.Info ("[XMREngine]: xmr verbose [yes | no]");
                    break;

                case "ls":
                    XmrTestLs (args, 2);
                    break;

                case "mvv":
                    switch (args.Length)
                    {
                        case 2:
                            m_log.InfoFormat ("[XMREngine]: migration version value = {0}", 
                                    XMRInstance.migrationVersion);
                            break;

                        case 3:
                            try
                            {
                                int mvv = Convert.ToInt32 (args[2]);
                                if ((mvv < 0) || (mvv > 255)) throw new Exception ("out of range");
                                XMRInstance.migrationVersion = (byte) mvv;
                            }
                            catch (Exception e)
                            {
                                m_log.Error ("[XMREngine]: bad/missing version number (" + e.Message + ")");
                            }
                            break;

                        default:
                            m_log.Error ("[XMREngine]: xmr mvv [<new_migration_version_value>]");
                            break;
                    }
                    break;

                case "pev":
                    XmrTestPev (args, 2);
                    break;

                case "reset":
                    XmrTestReset (args, 2);
                    break;

                case "resume":
                    m_log.Info ("[XMREngine]: resuming scripts");
                    m_SuspendScriptThreadFlag = false;
                    for (int i = 0; i < numThreadScriptWorkers; i ++)
                         m_ScriptThreads[i].WakeUpScriptThread();
                    Monitor.PulseAll(m_WakeUpLock);
                    break;

                case "suspend":
                    m_log.Info ("[XMREngine]: suspending scripts");
                    m_SuspendScriptThreadFlag = true;
                    for (int i = 0; i < numThreadScriptWorkers; i ++)
                        m_ScriptThreads[i].WakeUpScriptThread();
                    Monitor.PulseAll(m_WakeUpLock);
                    break;

                case "tracecalls":
                    if (args.Length > 2)
                        m_TraceCalls = (args[2][0] & 1) != 0;
                    m_log.Info ("[XMREngine]: tracecalls " + (m_TraceCalls ? "yes" : "no"));
                    break;

                case "verbose":
                    if (args.Length > 2)
                        m_Verbose = (args[2][0] & 1) != 0;
                    m_log.Info ("[XMREngine]: verbose " + (m_Verbose ? "yes" : "no"));
                    break;

                default:
                    m_log.Error ("[XMREngine]: unknown command " + args[1] + ", try 'xmr help'");
                    break;
            }
        }

        // Not required when not using IScriptInstance
        //
        public IScriptWorkItem QueueEventHandler(object parms)
        {
            return null;
        }

        public Scene World
        {
            get { return m_Scene; }
        }

        public IScriptModule ScriptModule
        {
            get { return this; }
        }

        public void SaveAllState()
        {
            m_log.Error("[XMREngine]: XMREngine.SaveAllState() called!!");
        }

#pragma warning disable 0067
        public event ScriptRemoved OnScriptRemoved;
        public event ObjectRemoved OnObjectRemoved;
#pragma warning restore 0067

        // Events targeted at a specific script
        // ... like listen() for an llListen() call
        //
        public bool PostScriptEvent(UUID itemID, EventParams parms)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance == null) return false;

            TraceCalls("[XMREngine]: XMREngine.PostScriptEvent({0},{1})", itemID.ToString(), parms.EventName);

            instance.PostEvent(parms);
            return true;
        }

        // Events targeted at all scripts in the given prim.
        //   localID = which prim
        //   parms   = event to post
        //
        public bool PostObjectEvent (uint localID, EventParams parms)
        {
            SceneObjectPart part = m_Scene.GetSceneObjectPart(localID);

            if (part == null)
                return false;

            TraceCalls("[XMREngine]: XMREngine.PostObjectEvent({0},{1})", localID.ToString(), parms.EventName);

             // In SecondLife, attach events go to all scripts of all prims
             // in a linked object.  So here we duplicate that functionality,
             // as all we ever get is a single attach event for the whole
             // object.
            if (parms.EventName == "attach")
            {
                bool posted = false;
                foreach (SceneObjectPart primpart in part.ParentGroup.Parts)
                {
                    posted |= PostPrimEvent (primpart, parms);
                }
                return posted;
            }

             // Other events go to just the scripts in that prim.
            return PostPrimEvent (part, parms);
        }

        private bool PostPrimEvent (SceneObjectPart part, EventParams parms)
        {
            UUID partUUID = part.UUID;

             // Get list of script instances running in the object.
            XMRInstance[] objInstArray;
            lock (m_InstancesDict)
            {
                if (!m_ObjectInstArray.TryGetValue (partUUID, out objInstArray))
                    return false;

                if (objInstArray == null)
                {
                    objInstArray = RebuildObjectInstArray (partUUID);
                    m_ObjectInstArray[partUUID] = objInstArray;
                }
            }

             // Post event to all script instances in the object.
            if (objInstArray.Length <= 0) return false;
            foreach (XMRInstance inst in objInstArray)
                inst.PostEvent (parms);

            return true;
        }

        public DetectParams GetDetectParams(UUID itemID, int number)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance == null)
                return null;
            return instance.GetDetectParams(number);
        }

        public void SetMinEventDelay(UUID itemID, double delay)
        {
        }

        public int GetStartParameter(UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance == null)
                return 0;
            return instance.StartParam;
        }

        // This is the "set running" method
        //
        public void SetScriptState(UUID itemID, bool state, bool self)
        {
            SetScriptState (itemID, state);
        }
        public void SetScriptState(UUID itemID, bool state)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
                instance.Running = state;
        }

        // Control display of the "running" checkbox
        //
        public bool GetScriptState(UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance == null)
                return false;
            return instance.Running;
        }

        public void SetState(UUID itemID, string newState)
        {
            TraceCalls("[XMREngine]: XMREngine.SetState({0},{1})", itemID.ToString(), newState);
        }

        public void ApiResetScript(UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
                instance.ApiReset();
        }

        public void ResetScript(UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
            {
                IUrlModule urlModule = m_Scene.RequestModuleInterface<IUrlModule>();
                if (urlModule != null)
                    urlModule.ScriptRemoved(itemID);

                instance.Reset();
            }
        }

        public IConfig Config
        {
            get { return m_Config; }
        }

        public IConfigSource ConfigSource
        {
            get { return m_ConfigSource; }
        }

        public string ScriptEngineName
        {
            get { return "XMREngine"; }
        }

        public IScriptApi GetApi(UUID itemID, string name)
        {
            FieldInfo fi;
            if (!m_XMRInstanceApiCtxFieldInfos.TryGetValue (name, out fi))
                return null;
            XMRInstance inst = GetInstance (itemID);
            if (inst == null) return null;
            return (IScriptApi)fi.GetValue (inst);
        }

        /**
         * @brief Get script's current state as an XML string
         *        - called by "Take", "Take Copy" and when object deleted (ie, moved to Trash)
         *        This includes the .state file
         */
        public string GetXMLState(UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance == null)
                return String.Empty;

            TraceCalls("[XMREngine]: XMREngine.GetXMLState({0})", itemID.ToString());

            if (!instance.m_HasRun)
                return String.Empty;

            XmlDocument doc = new XmlDocument();

            /*
             * Set up <State Engine="XMREngine" UUID="itemID" Asset="assetID"> tag.
             */
            XmlElement stateN = doc.CreateElement("", "State", "");
            doc.AppendChild(stateN);

            XmlAttribute engineA = doc.CreateAttribute("", "Engine", "");
            engineA.Value = ScriptEngineName;
            stateN.Attributes.Append(engineA);

            XmlAttribute uuidA = doc.CreateAttribute("", "UUID", "");
            uuidA.Value = itemID.ToString();
            stateN.Attributes.Append(uuidA);

            XmlAttribute assetA = doc.CreateAttribute("", "Asset", "");
            string assetID = instance.AssetID.ToString();
            assetA.Value = assetID;
            stateN.Attributes.Append(assetA);

             // Get <ScriptState>...</ScriptState> item that hold's script's state.
             // This suspends the script if necessary then takes a snapshot.
            XmlElement scriptStateN = instance.GetExecutionState(doc);
            stateN.AppendChild(scriptStateN);

            return doc.OuterXml;
        }

        // Set script's current state from an XML string
        // - called just before a script is instantiated
        // So we write the .state file so the .state file will be seen when 
        // the script is instantiated.
        public bool SetXMLState(UUID itemID, string xml)
        {
            XmlDocument doc = new XmlDocument();

            try
            {
                doc.LoadXml(xml);
            }
            catch
            {
                return false;
            }
            TraceCalls("[XMREngine]: XMREngine.SetXMLState({0})", itemID.ToString());

            // Make sure <State Engine="XMREngine"> so we know it is in our
            // format.
            XmlElement stateN = (XmlElement)doc.SelectSingleNode("State");
            if (stateN == null)
                return false;

            if (stateN.GetAttribute("Engine") != ScriptEngineName)
                return false;

            // <ScriptState>...</ScriptState> contains contents of .state file.
            XmlElement scriptStateN = (XmlElement)stateN.SelectSingleNode("ScriptState");
            if (scriptStateN == null)
                return false;

            string sen = stateN.GetAttribute("Engine");
            if ((sen == null) || (sen != ScriptEngineName))
                return false;

            XmlAttribute assetA = doc.CreateAttribute("", "Asset", "");
            assetA.Value = stateN.GetAttribute("Asset");
            scriptStateN.Attributes.Append(assetA);

            // Write out the .state file with the <ScriptState ...>...</ScriptState> XML text
            string statePath = XMRInstance.GetStateFileName(m_ScriptBasePath, itemID);
            FileStream ss = File.Create(statePath);
            StreamWriter sw = new StreamWriter(ss);
            sw.Write(scriptStateN.OuterXml);
            sw.Close();
            ss.Close();

            return true;
        }

        public bool PostScriptEvent(UUID itemID, string name, Object[] p)
        {
            if (!m_Enabled)
                return false;

            TraceCalls("[XMREngine]: XMREngine.PostScriptEvent({0},{1})", itemID.ToString(), name);

            return PostScriptEvent(itemID, new EventParams(name, p, zeroDetectParams));
        }

        public bool PostObjectEvent(UUID itemID, string name, Object[] p)
        {
            if (!m_Enabled)
                return false;

            TraceCalls("[XMREngine]: XMREngine.PostObjectEvent({0},{1})", itemID.ToString(), name);

            SceneObjectPart part = m_Scene.GetSceneObjectPart(itemID);
            if (part == null)
                return false;

            return PostObjectEvent(part.LocalId, new EventParams(name, p, zeroDetectParams));
        }

        // about the 3523rd entrypoint for a script to put itself to sleep
        public void SleepScript(UUID itemID, int delay)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
                instance.Sleep (delay);
        }

        // Get a script instance loaded, compiling it if necessary
        //
        //  localID     = the object as a whole, may contain many scripts
        //  itemID      = this instance of the script in this object
        //  script      = script source code
        //  startParam  = value passed to 'on_rez' event handler
        //  postOnRez   = true to post an 'on_rez' event to script on load
        //  defEngine   = default script engine
        //  stateSource = post this event to script on load

        public void OnRezScript(uint localID, UUID itemID, string script,
                int startParam, bool postOnRez, string defEngine, int stateSource)
        {
            SceneObjectPart part = m_Scene.GetSceneObjectPart(localID);
            TaskInventoryItem item = part.Inventory.GetInventoryItem(itemID);

            if (!m_LateInit)
            {
                m_LateInit = true;
                OneTimeLateInitialization ();
            }

            TraceCalls("[XMREngine]: OnRezScript(...,{0},...)", itemID.ToString());

             // Assume script uses the default engine, whatever that is.
            string engineName = defEngine;

             // Very first line might contain "//" scriptengine ":".
            string firstline = "";
            if (script.StartsWith("//")) {
                int lineEnd = script.IndexOf('\n');
                if (lineEnd > 1) firstline = script.Substring(0, lineEnd).Trim();
                int colon = firstline.IndexOf(':');
                if (colon >= 2) {
                    engineName = firstline.Substring(2, colon - 2).Trim();
                    if (engineName == "") engineName = defEngine;
                }
            }

             // Make sure the default or requested engine is us.
            if (engineName != ScriptEngineName) {

                 // Not us, if requested engine exists, silently ignore script and let
                 // requested engine handle it.
                IScriptModule[] engines = m_Scene.RequestModuleInterfaces<IScriptModule> ();
                foreach (IScriptModule eng in engines)
                {
                    if (eng.ScriptEngineName == engineName)
                        return;
                }

                 // Requested engine not defined, warn on console.
                 // Then we try to handle it if we're the default engine, else we ignore it.
                m_log.Warn ("[XMREngine]: " + itemID.ToString() + " requests undefined/disabled engine " + engineName);
                m_log.Info ("[XMREngine]: - " + part.GetWorldPosition ());
                m_log.Info ("[XMREngine]: first line: " + firstline);
                if (defEngine != ScriptEngineName)
                {
                    m_log.Info ("[XMREngine]: leaving it to the default script engine (" + defEngine + ") to process it");
                    return;
                }
                m_log.Info ("[XMREngine]: will attempt to processing it anyway as default script engine");
            }

             // Put on object/instance lists.
            XMRInstance instance   = (XMRInstance)Activator.CreateInstance (ScriptCodeGen.xmrInstSuperType);
            instance.m_LocalID     = localID;
            instance.m_ItemID      = itemID;
            instance.m_SourceCode  = script;
            instance.m_StartParam  = startParam;
            instance.m_PostOnRez   = postOnRez;
            instance.m_StateSource = (StateSource)stateSource;
            instance.m_Part        = part;
            instance.m_PartUUID    = part.UUID;
            instance.m_Item        = item;
            instance.m_DescName    = part.Name + ":" + item.Name;
            instance.m_IState      = XMRInstState.CONSTRUCT;

            lock (m_InstancesDict)
            {
                m_LockedDict = "RegisterInstance";

                // Insert on internal list of all scripts being handled by this engine instance.
                m_InstancesDict[instance.m_ItemID] = instance;

                // Insert on internal list of all scripts being handled by this engine instance
                // that are part of the object.
                List<UUID> itemIDList;
                if (!m_ObjectItemList.TryGetValue(instance.m_PartUUID, out itemIDList))
                {
                    itemIDList = new List<UUID>();
                    m_ObjectItemList[instance.m_PartUUID] = itemIDList;
                }
                if (!itemIDList.Contains(instance.m_ItemID))
                {
                    itemIDList.Add(instance.m_ItemID);
                    m_ObjectInstArray[instance.m_PartUUID] = null;
                }

                m_LockedDict = "~RegisterInstance";
            }

             // Compile and load it.
            lock (m_ScriptErrors)
                m_ScriptErrors.Remove (instance.m_ItemID);

            LoadThreadWork (instance);
        }

        /**
         * @brief This routine instantiates one script.
         */
        private void LoadThreadWork (XMRInstance instance)
        {
             // Compile and load the script in memory.

            ArrayList errors = new ArrayList();
            Exception initerr = null;
            try
            {
                instance.Initialize(this, m_ScriptBasePath, m_StackSize, m_HeapSize, errors);
            }
            catch (Exception e1)
            {
                initerr = e1;
            }
            if ((initerr != null) && !instance.m_ForceRecomp)
            {
                UUID itemID = instance.m_ItemID;
                Verbose ("[XMREngine]: {0}/{2} first load failed ({1}), retrying after recompile", 
                        itemID.ToString(), initerr.Message, instance.m_Item.AssetID.ToString());
                Verbose ("[XMREngine]:\n{0}", initerr.ToString ());
                initerr = null;
                errors = new ArrayList();
                instance.m_ForceRecomp = true;
                try
                {
                    instance.Initialize(this, m_ScriptBasePath, m_StackSize, m_HeapSize, errors);
                }
                catch (Exception e2)
                {
                    initerr = e2;
                }
            }
            if (initerr != null)
            {
                UUID itemID = instance.m_ItemID;
                Verbose ("[XMREngine]: Error starting script {0}/{2}: {1}",
                                  itemID.ToString(), initerr.Message, instance.m_Item.AssetID.ToString());
                if (initerr.Message != "compilation errors")
                {
                    Verbose ("[XMREngine]: - " + instance.m_Part.GetWorldPosition () + " " + instance.m_DescName);
                    Verbose ("[XMREngine]:   exception:\n{0}", initerr.ToString());
                }

                OnRemoveScript (0, itemID);

                 // Post errors where GetScriptErrors() can see them.

                if (errors.Count == 0)
                    errors.Add(initerr.Message);
                else
                {
                    foreach (Object err in errors)
                    {
                        if (m_ScriptDebug)
                            m_log.DebugFormat ("[XMREngine]:   {0}", err.ToString());
                    }
                }

                lock (m_ScriptErrors)
                    m_ScriptErrors[instance.m_ItemID] = errors;

                return;
            }

             // Tell GetScriptErrors() that we have finished compiling/loading
             // successfully (by posting a 0 element array).
            lock (m_ScriptErrors)
            {
                if (instance.m_IState != XMRInstState.CONSTRUCT) throw new Exception("bad state");
                m_ScriptErrors[instance.m_ItemID] = noScriptErrors;
            }

             // Transition from CONSTRUCT->ONSTARTQ and give to RunScriptThread().
             // Put it on the start queue so it will run any queued event handlers,
             // such as state_entry() or on_rez().  If there aren't any queued, it
             // will just go to idle state when RunOne() tries to dequeue an event.
            lock (instance.m_QueueLock)
            {
                if (instance.m_IState != XMRInstState.CONSTRUCT)
                    throw new Exception("bad state");
                instance.m_IState = XMRInstState.ONSTARTQ;
                if (!instance.m_Running)
                    instance.EmptyEventQueues ();
            }
            QueueToStart(instance);
        }

        public void OnRemoveScript(uint localID, UUID itemID)
        {
            TraceCalls("[XMREngine]: OnRemoveScript(...,{0})", itemID.ToString());

             // Remove from our list of known scripts.
             // After this, no more events can queue because we won't be
             // able to translate the itemID to an XMRInstance pointer.

            XMRInstance instance = null;
            lock (m_InstancesDict)
            {
                m_LockedDict = "OnRemoveScript:" + itemID.ToString();

                 // Tell the instance to free off everything it can.

                if (!m_InstancesDict.TryGetValue(itemID, out instance))
                {
                    m_LockedDict = "~OnRemoveScript";
                    return;
                }

                 // Tell it to stop executing anything.
                instance.suspendOnCheckRunHold = true;

                 // Remove it from our list of known script instances
                 // mostly so no more events can queue to it.
                m_InstancesDict.Remove(itemID);

                List<UUID> itemIDList;
                if (m_ObjectItemList.TryGetValue (instance.m_PartUUID, out itemIDList))
                {
                    itemIDList.Remove(itemID);
                    if (itemIDList.Count == 0)
                    {
                        m_ObjectItemList.Remove(instance.m_PartUUID);
                        m_ObjectInstArray.Remove(instance.m_PartUUID);
                    }
                    else
                        m_ObjectInstArray[instance.m_PartUUID] = null;
                }

                 // Delete the .state file as any needed contents were fetched with GetXMLState()
                 // and stored on the database server.
                string stateFileName = XMRInstance.GetStateFileName(m_ScriptBasePath, itemID);
                File.Delete(stateFileName);

                ScriptRemoved handlerScriptRemoved = OnScriptRemoved;
                if (handlerScriptRemoved != null)
                    handlerScriptRemoved(itemID);

                m_LockedDict = "~~OnRemoveScript";
            }

             // Free off its stack and fun things like that.
             // If it is running, abort it.
            instance.Dispose ();
        }

        public void OnScriptReset(uint localID, UUID itemID)
        {
            TraceCalls("[XMREngine]: XMREngine.OnScriptReset({0},{1})", localID.ToString(), itemID.ToString());
            ResetScript(itemID);
        }

        public void OnStartScript(uint localID, UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
                instance.Running = true;
        }

        public void OnStopScript(uint localID, UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
                instance.Running = false;
        }

        public void OnGetScriptRunning(IClientAPI controllingClient,
                UUID objectID, UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
            {
                TraceCalls("[XMREngine]: XMREngine.OnGetScriptRunning({0},{1})", objectID.ToString(), itemID.ToString());

                IEventQueue eq = World.RequestModuleInterface<IEventQueue>();
                if (eq == null)
                {
                    controllingClient.SendScriptRunningReply(objectID, itemID,
                            instance.Running);
                }
                else
                {
                    eq.Enqueue(EventQueueHelper.ScriptRunningReplyEvent(objectID,
                            itemID, instance.Running, true),
                            controllingClient.AgentId);
                }
            }
        }

        public bool HasScript(UUID itemID, out bool running)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance == null)
            {
                running = true;
                return false;
            }
            running = instance.Running;
            return true;
        }

        /**
         * @brief Called once per frame update to see if scripts have
         *        any such work to do.
         */
        private void OnFrame ()
        {
            if (m_FrameUpdateList != null)
            {
                ThreadStart frameupdates;
                lock (m_FrameUpdateLock)
                {
                    frameupdates = m_FrameUpdateList;
                    m_FrameUpdateList = null;
                }
                frameupdates ();
            }
        }

        /**
         * @brief Add a one-shot delegate to list of things to do
         *        synchronized with frame updates.
         */
        public void AddOnFrameUpdate (ThreadStart thunk)
        {
            lock (m_FrameUpdateLock)
                m_FrameUpdateList += thunk;
        }

        /**
         * @brief Gets called early as part of shutdown,
         *        right after "Persisting changed objects" message.
         */
        public void OnShutdown()
        {
            TraceCalls("[XMREngine]: XMREngine.OnShutdown()");
        }

        /**
         * @brief Queue an instance to the StartQueue so it will run.
         *        This queue is used for instances that have just had
         *        an event queued to them when they were previously
         *        idle.  It must only be called by the thread that
         *        transitioned the thread to XMRInstState.ONSTARTQ so
         *        we don't get two threads trying to queue the same
         *        instance to the m_StartQueue at the same time.
         */
        public void QueueToStart(XMRInstance inst)
        {
            lock (m_StartQueue)
            {
                if (inst.m_IState != XMRInstState.ONSTARTQ) throw new Exception("bad state");
                m_StartQueue.InsertTail(inst);
            }
            WakeUpOne();
        }

        public void QueueToTrunk(ThreadStart thds)
        {
            lock (m_WakeUpLock)
                    m_ThunkQueue.Enqueue (thds);
            WakeUpOne();
        }

        /**
         * @brief A script may be sleeping, in which case we wake it.
         */
        public void WakeFromSleep(XMRInstance inst)
        {
             // Remove from sleep queue unless someone else already woke it.
            lock (m_SleepQueue)
            {
                if (inst.m_IState != XMRInstState.ONSLEEPQ)
                    return;

                m_SleepQueue.Remove(inst);
                inst.m_IState = XMRInstState.REMDFROMSLPQ;
            }

             // Put on end of list of scripts that are ready to run.
            lock (m_YieldQueue)
            {
                inst.m_IState = XMRInstState.ONYIELDQ;
                m_YieldQueue.InsertTail(inst);
            }


             // Make sure the OS thread is running so it will see the script.
            WakeUpOne();
        }

        /**
         * @brief An instance has just finished running for now,
         *        figure out what to do with it next.
         * @param inst = instance in question, not on any queue at the moment
         * @param newIState = its new state
         * @returns with instance inserted onto proper queue (if any)
         */
        public void HandleNewIState(XMRInstance inst, XMRInstState newIState)
        {

             // RunOne() should have left the instance in RUNNING state.
            if (inst.m_IState != XMRInstState.RUNNING)
                throw new Exception("bad state");


             // Now see what RunOne() wants us to do with the instance next.
            switch (newIState)
            {

                 // Instance has set m_SleepUntil to when it wants to sleep until.
                 // So insert instance in sleep queue by ascending wake time.
                 // Then wake the timer thread if this is the new first entry
                 // so it will reset its timer.
                case XMRInstState.ONSLEEPQ:
                    lock (m_SleepQueue)
                     {
                        XMRInstance after;

                        inst.m_IState = XMRInstState.ONSLEEPQ;
                        for (after = m_SleepQueue.PeekHead(); after != null; after = after.m_NextInst)
                        {
                            if (after.m_SleepUntil > inst.m_SleepUntil)
                                break;
                        }
                        m_SleepQueue.InsertBefore(inst, after);
                        if (m_SleepQueue.PeekHead() == inst)
                        {
                            Monitor.Pulse (m_SleepQueue);
                        }
                    }
                    break;

                 // Instance just took a long time to run and got wacked by the
                 // slicer.  So put on end of yield queue to let someone else
                 // run.  If there is no one else, it will run again right away.
                case XMRInstState.ONYIELDQ:
                    lock (m_YieldQueue)
                    {
                        inst.m_IState = XMRInstState.ONYIELDQ;
                        m_YieldQueue.InsertTail(inst);
                    }
                    break;

                 // Instance finished executing an event handler.  So if there is
                 // another event queued for it, put it on the start queue so it
                 // will process the new event.  Otherwise, mark it idle and the
                 // next event to queue to it will start it up.
                case XMRInstState.FINISHED:
                    Monitor.Enter(inst.m_QueueLock);
                    if (!inst.m_Suspended && (inst.m_EventQueue.Count > 0))
                    {
                        Monitor.Exit(inst.m_QueueLock);
                        lock (m_StartQueue)
                        {
                            inst.m_IState = XMRInstState.ONSTARTQ;
                            m_StartQueue.InsertTail (inst);
                        }
                    }
                    else
                    {
                        inst.m_IState = XMRInstState.IDLE;
                        Monitor.Exit(inst.m_QueueLock);
                    }
                    break;

                 // Its m_SuspendCount > 0.
                 // Don't put it on any queue and it won't run.
                 // Since it's not IDLE, even queuing an event won't start it.
                case XMRInstState.SUSPENDED:
                    inst.m_IState = XMRInstState.SUSPENDED;
                    break;

                 // It has been disposed of.
                 // Just set the new state and all refs should theoretically drop off
                 // as the instance is no longer in any list.
                case XMRInstState.DISPOSED:
                    inst.m_IState = XMRInstState.DISPOSED;
                    break;

                 // RunOne returned something bad.
                default:
                     throw new Exception("bad new state");
            }
        }

        /**
         * @brief Thread that moves instances from the Sleep queue to the Yield queue.
         */
        private void RunSleepThread()
        {
            double deltaTS;
            int deltaMS;
            XMRInstance inst;

            while (true)
            {
                lock (m_SleepQueue)
                {

                     // Wait here until there is a script on the timer queue that has expired.
                    while (true)
                    {
                        UpdateMyThread ();
                        if (m_Exiting)
                        {
                            MyThreadExiting ();
                            return;
                        }
                        inst = m_SleepQueue.PeekHead();
                        if (inst == null)
                        {
                            Monitor.Wait (m_SleepQueue, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS / 2);
                            continue;
                        }
                        if (inst.m_IState != XMRInstState.ONSLEEPQ) throw new Exception("bad state");
                        deltaTS = (inst.m_SleepUntil - DateTime.UtcNow).TotalMilliseconds;
                        if (deltaTS <= 0.0)
                            break;
                        deltaMS = Int32.MaxValue;
                        if (deltaTS < Int32.MaxValue)
                            deltaMS = (int)deltaTS;
                        if (deltaMS > Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS / 2)
                        {
                            deltaMS = Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS / 2;
                        }
                        Monitor.Wait (m_SleepQueue, deltaMS);
                    }

                     // Remove the expired entry from the timer queue.
                    m_SleepQueue.RemoveHead();
                    inst.m_IState = XMRInstState.REMDFROMSLPQ;
                }

                 // Post the script to the yield queue so it will run and wake a script thread to run it.
                lock (m_YieldQueue)
                {
                    inst.m_IState = XMRInstState.ONYIELDQ;
                    m_YieldQueue.InsertTail(inst);
                }
                WakeUpOne ();
            }
        }

        /**
         * @brief Thread that runs a time slicer.
         */
        public void Suspend(UUID itemID, int ms)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
                instance.Sleep(ms);
        }

        public void Die(UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
            {
                TraceCalls("[XMREngine]: XMREngine.Die({0})", itemID.ToString());
                instance.Die();
            }
        }

        /**
         * @brief Get specific script instance for which OnRezScript()
         *        has been called for an XMREngine script, and that
         *        OnRemoveScript() has not been called since.
         * @param itemID = as passed to OnRezScript() identifying a specific script instance
         * @returns null: not one of our scripts (maybe XEngine etc)
         *          else: points to the script instance
         */
        public XMRInstance GetInstance(UUID itemID)
        {
            XMRInstance instance;
            lock (m_InstancesDict)
            {
                if (!m_InstancesDict.TryGetValue(itemID, out instance))
                    instance = null;
            }
            return instance;
        }

        // Called occasionally to write script state to .state file so the
        // script will restart from its last known state if the region crashes
        // and gets restarted.
        private void DoMaintenance(object source, ElapsedEventArgs e)
        {
            XMRInstance[] instanceArray;

            lock (m_InstancesDict)
                instanceArray = System.Linq.Enumerable.ToArray(m_InstancesDict.Values);

            foreach (XMRInstance ins in instanceArray)
            {
                // Don't save attachments
                if (ins.m_Part.ParentGroup.IsAttachment)
                    continue;
                ins.GetExecutionState(new XmlDocument());
            }
        }

        /**
         * @brief Retrieve errors generated by a previous call to OnRezScript().
         *        We are guaranteed this routine will not be called before the
         *        corresponding OnRezScript() has returned.  It blocks until the
         *        compile has completed.
         */
        public ArrayList GetScriptErrors(UUID itemID)
        {
            ArrayList errors;

            lock (m_ScriptErrors)
            {
                while (!m_ScriptErrors.TryGetValue (itemID, out errors))
                {
                    Monitor.Wait (m_ScriptErrors);
                }
                m_ScriptErrors.Remove (itemID);
            }
            return errors;
        }

        /**
         * @brief Return a list of all script execution times.
         */
        public Dictionary<uint, float> GetObjectScriptsExecutionTimes ()
        {
            Dictionary<uint, float> topScripts = new Dictionary<uint, float> ();
            lock (m_InstancesDict)
            {
                foreach (XMRInstance instance in m_InstancesDict.Values)
                {
                    uint rootLocalID = instance.m_Part.ParentGroup.LocalId;
                    float oldTotal;
                    if (!topScripts.TryGetValue (rootLocalID, out oldTotal))
                        oldTotal = 0;

                    topScripts[rootLocalID] = (float)instance.m_CPUTime + oldTotal;
                }
            }
            return topScripts;
        }

        /**
         * @brief A float the value is a representative execution time in
         *        milliseconds of all scripts in the link set.
         * @param itemIDs = list of scripts in the link set
         * @returns milliseconds for all those scripts
         */
        public float GetScriptExecutionTime (List<UUID> itemIDs)
        {
            if ((itemIDs == null) || (itemIDs.Count == 0))
                return 0;

            float time = 0;
            foreach (UUID itemID in itemIDs)
            {
                XMRInstance instance = GetInstance (itemID);
                if ((instance != null) && instance.Running)
                    time += (float) instance.m_CPUTime;
            }
            return time;
        }

        /**
         * @brief Block script from dequeuing events.
         */
        public void SuspendScript(UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
            {
                TraceCalls("[XMREngine]: XMREngine.SuspendScript({0})", itemID.ToString());
                instance.SuspendIt();
            }
        }

        /**
         * @brief Allow script to dequeue events.
         */
        public void ResumeScript(UUID itemID)
        {
            XMRInstance instance = GetInstance (itemID);
            if (instance != null)
            {
                TraceCalls("[XMREngine]: XMREngine.ResumeScript({0})", itemID.ToString());
                instance.ResumeIt();
            }
            else
            {
                // probably an XEngine script
            }
        }

        /**
         * @brief Rebuild m_ObjectInstArray[partUUID] from m_ObjectItemList[partUUID]
         * @param partUUID = which object in scene to rebuild for
         */
        private XMRInstance[] RebuildObjectInstArray (UUID partUUID)
        {
            List<UUID> itemIDList = m_ObjectItemList[partUUID];
            int n = 0;
            foreach (UUID itemID in itemIDList)
            {
                if (m_InstancesDict.ContainsKey(itemID))
                    n ++;
            }
            XMRInstance[] a = new XMRInstance[n];
            n = 0;
            foreach (UUID itemID in itemIDList)
            {
                if (m_InstancesDict.TryGetValue (itemID, out a[n]))
                    n ++;
            }
            m_ObjectInstArray[partUUID] = a;
            return a;
        }

        public void TraceCalls (string format, params object[] args)
        {
            if (m_TraceCalls)
                m_log.DebugFormat (format, args);
        }
        public void Verbose (string format, params object[] args)
        {
            if (m_Verbose)
                m_log.DebugFormat (format, args);
        }

        /**
         * @brief Manage our threads.
         */
        public static Thread StartMyThread (ThreadStart start, string name, ThreadPriority priority)
        {
            m_log.Debug ("[XMREngine]: starting thread " + name);
            Thread thread   = new Thread (start);
            thread.Name     = name;
            thread.Priority = priority;
            thread.IsBackground = true;
            thread.Start ();

            Watchdog.ThreadWatchdogInfo info = new Watchdog.ThreadWatchdogInfo (thread, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS, name);
            Watchdog.AddThread (info, name, true);

            return thread;
        }

        public static void UpdateMyThread ()
        {
            Watchdog.UpdateThread();
        }

        public static void MyThreadExiting ()
        {
            Watchdog.RemoveThread(true);
        }

        public void RunScriptThread(XMRScriptThread xthd)
        {
            XMRInstance inst;
            while (!m_Exiting)
            {
                Watchdog.UpdateThread();

                /*
                 * Handle 'xmr resume/suspend' commands.
                 */
                if (m_SuspendScriptThreadFlag)
                {
                    lock (m_WakeUpLock)
                    {
                        while (m_SuspendScriptThreadFlag &&
                               !m_Exiting &&
                               (m_ThunkQueue.Count == 0))
                        {
                            Monitor.Wait (m_WakeUpLock, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS / 2);
                            XMREngine.UpdateMyThread ();
                        }
                    }
                }

                /*
                 * Maybe there are some scripts waiting to be migrated in or out.
                 */
                ThreadStart thunk = null;
                lock (m_WakeUpLock)
                {
                    if (m_ThunkQueue.Count > 0)
                        thunk = m_ThunkQueue.Dequeue ();
                }
                if (thunk != null)
                {
                    inst = (XMRInstance)thunk.Target;
                    thunk ();
                    if (m_Exiting || m_SuspendScriptThreadFlag)
                        continue;
                }

                if (m_StartProcessing)
                {
                     // If event just queued to any idle scripts
                     // start them right away.  But only start so
                     // many so we can make some progress on yield
                     // queue.

                    int numStarts;
                    for (numStarts = 5; -- numStarts >= 0;)
                    {
                        lock (m_StartQueue)
                        {
                            inst = m_StartQueue.RemoveHead();
                        }
                        if (inst == null) break;
                        if (inst.m_IState != XMRInstState.ONSTARTQ) throw new Exception("bad state");
                        xthd.RunInstance (inst);
                        if (m_Exiting || m_SuspendScriptThreadFlag)
                            continue;
                    }

                     // If there is something to run, run it
                     // then rescan from the beginning in case
                     // a lot of things have changed meanwhile.
                     //
                     // These are considered lower priority than
                     // m_StartQueue as they have been taking at
                     // least one quantum of CPU time and event
                     // handlers are supposed to be quick.

                    lock (m_YieldQueue)
                    {
                        inst = m_YieldQueue.RemoveHead();
                    }
                    if (inst != null)
                    {
                        if (inst.m_IState != XMRInstState.ONYIELDQ) throw new Exception("bad state");
                        xthd.RunInstance(inst);
                        numStarts = -1;
                    }

                     // If we left something dangling in the m_StartQueue or m_YieldQueue, go back to check it.
                    if (m_Exiting || numStarts < 0)
                        continue;
                }

                 // Nothing to do, sleep.
                lock (m_WakeUpLock)
                {
                    if (!xthd.m_WakeUpThis && (m_WakeUpOne <= 0) && !m_Exiting)
                        Monitor.Wait(m_WakeUpLock, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS / 2);

                    xthd.m_WakeUpThis = false;
                    if ((m_WakeUpOne > 0) && (--m_WakeUpOne > 0))
                        Monitor.Pulse (m_WakeUpLock);
                }
            }
            Watchdog.RemoveThread(true);
        }
    }
}