aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Application/OpenSim.cs
blob: fcc87177a6f6ff989095b214f247c7625dc9ffce (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
/*
 * 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.
 */

using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Text;
using System.Text.RegularExpressions;
using System.Timers;
using log4net;
using NDesk.Options;
using Nini.Config;
using OpenMetaverse;
using OpenSim.Framework;
using OpenSim.Framework.Console;
using OpenSim.Framework.Servers;
using OpenSim.Framework.Monitoring;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
using OpenSim.Services.Interfaces;

namespace OpenSim
{
    /// <summary>
    /// Interactive OpenSim region server
    /// </summary>
    public class OpenSim : OpenSimBase
    {
        private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        protected string m_startupCommandsFile;
        protected string m_shutdownCommandsFile;
        protected bool m_gui = false;
        protected string m_consoleType = "local";
        protected uint m_consolePort = 0;

        /// <summary>
        /// Prompt to use for simulator command line.
        /// </summary>
        private string m_consolePrompt;

        /// <summary>
        /// Regex for parsing out special characters in the prompt.
        /// </summary>
        private Regex m_consolePromptRegex = new Regex(@"([^\\])\\(\w)", RegexOptions.Compiled);

        private string m_timedScript = "disabled";
        private int m_timeInterval = 1200;
        private System.Timers.Timer m_scriptTimer;

        public OpenSim(IConfigSource configSource) : base(configSource)
        {
        }

        protected override void ReadExtraConfigSettings()
        {
            base.ReadExtraConfigSettings();

            IConfig startupConfig = Config.Configs["Startup"];
            IConfig networkConfig = Config.Configs["Network"];

            int stpMinThreads = 2;
            int stpMaxThreads = 15;

            if (startupConfig != null)
            {
                m_startupCommandsFile = startupConfig.GetString("startup_console_commands_file", "startup_commands.txt");
                m_shutdownCommandsFile = startupConfig.GetString("shutdown_console_commands_file", "shutdown_commands.txt");

                if (startupConfig.GetString("console", String.Empty) == String.Empty)
                    m_gui = startupConfig.GetBoolean("gui", false);
                else
                    m_consoleType= startupConfig.GetString("console", String.Empty);

                if (networkConfig != null)
                    m_consolePort = (uint)networkConfig.GetInt("console_port", 0);

                m_timedScript = startupConfig.GetString("timer_Script", "disabled");
                if (m_timedScript != "disabled")
                {
                    m_timeInterval = startupConfig.GetInt("timer_Interval", 1200);
                }

                string asyncCallMethodStr = startupConfig.GetString("async_call_method", String.Empty);
                FireAndForgetMethod asyncCallMethod;
                if (!String.IsNullOrEmpty(asyncCallMethodStr) && Utils.EnumTryParse<FireAndForgetMethod>(asyncCallMethodStr, out asyncCallMethod))
                    Util.FireAndForgetMethod = asyncCallMethod;

                stpMinThreads = startupConfig.GetInt("MinPoolThreads", 2 );
                stpMaxThreads = startupConfig.GetInt("MaxPoolThreads", 25);
                m_consolePrompt = startupConfig.GetString("ConsolePrompt", @"Region (\R) ");
            }

            if (Util.FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool)
                Util.InitThreadPool(stpMinThreads, stpMaxThreads);

            m_log.Info("[OPENSIM MAIN]: Using async_call_method " + Util.FireAndForgetMethod);

            m_log.InfoFormat("[OPENSIM MAIN] Running GC in {0} mode", GCSettings.IsServerGC ? "server":"workstation");
        }

#if (_MONO)
        private static Mono.Unix.UnixSignal[] signals;


        private Thread signal_thread = new Thread (delegate ()
        {
            while (true)
            {
                // Wait for a signal to be delivered
                int index = Mono.Unix.UnixSignal.WaitAny (signals, -1);

                //Mono.Unix.Native.Signum signal = signals [index].Signum;
                MainConsole.Instance.RunCommand("shutdown");
            }
        });       
#endif

        /// <summary>
        /// Performs initialisation of the scene, such as loading configuration from disk.
        /// </summary>
        protected override void StartupSpecific()
        {
            m_log.Info("====================================================================");
            m_log.Info("========================= STARTING OPENSIM =========================");
            m_log.Info("====================================================================");

#if (_MONO)
            if(!Util.IsWindows())
            {
                try
                {
                    // linux mac os specifics
                    signals = new Mono.Unix.UnixSignal[]
                    {
                        new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGTERM)
                    };
                    signal_thread.IsBackground = true;
                    signal_thread.Start();
                }
                catch (Exception e)
                {
                    m_log.Info("Could not set up UNIX signal handlers. SIGTERM will not");
                    m_log.InfoFormat("shut down gracefully: {0}", e.Message);
                    m_log.Debug("Exception was: ", e);
                }
            }
#endif
            //m_log.InfoFormat("[OPENSIM MAIN]: GC Is Server GC: {0}", GCSettings.IsServerGC.ToString());
            // http://msdn.microsoft.com/en-us/library/bb384202.aspx
            //GCSettings.LatencyMode = GCLatencyMode.Batch;
            //m_log.InfoFormat("[OPENSIM MAIN]: GC Latency Mode: {0}", GCSettings.LatencyMode.ToString());

            if (m_gui) // Driven by external GUI
            {
                m_console = new CommandConsole("Region");
            }
            else
            {
                switch (m_consoleType)
                {
                case "basic":
                    m_console = new CommandConsole("Region");
                    break;
                case "rest":
                    m_console = new RemoteConsole("Region");
                    ((RemoteConsole)m_console).ReadConfig(Config);
                    break;
                default:
                    m_console = new LocalConsole("Region", Config.Configs["Startup"]);
                    break;
                }
            }

            MainConsole.Instance = m_console;

            LogEnvironmentInformation();
            RegisterCommonAppenders(Config.Configs["Startup"]);
            RegisterConsoleCommands();

            base.StartupSpecific();

            MainServer.Instance.AddStreamHandler(new OpenSim.SimStatusHandler());
            MainServer.Instance.AddStreamHandler(new OpenSim.XSimStatusHandler(this));
            if (userStatsURI != String.Empty)
                MainServer.Instance.AddStreamHandler(new OpenSim.UXSimStatusHandler(this));
            MainServer.Instance.AddStreamHandler(new OpenSim.SimRobotsHandler());

            if (managedStatsURI != String.Empty)
            {
                string urlBase = String.Format("/{0}/", managedStatsURI);
                StatsManager.StatsPassword = managedStatsPassword;
                MainServer.Instance.AddHTTPHandler(urlBase, StatsManager.HandleStatsRequest);
                m_log.InfoFormat("[OPENSIM] Enabling remote managed stats fetch. URL = {0}", urlBase);
            }

            if (m_console is RemoteConsole)
            {
                if (m_consolePort == 0)
                {
                    ((RemoteConsole)m_console).SetServer(m_httpServer);
                }
                else
                {
                    ((RemoteConsole)m_console).SetServer(MainServer.GetHttpServer(m_consolePort));
                }
            }

            // Hook up to the watchdog timer
            Watchdog.OnWatchdogTimeout += WatchdogTimeoutHandler;

            PrintFileToConsole("startuplogo.txt");

            // For now, start at the 'root' level by default
            if (SceneManager.Scenes.Count == 1) // If there is only one region, select it
                ChangeSelectedRegion("region",
                                     new string[] {"change", "region", SceneManager.Scenes[0].RegionInfo.RegionName});
            else
                ChangeSelectedRegion("region", new string[] {"change", "region", "root"});

            //Run Startup Commands
            if (String.IsNullOrEmpty(m_startupCommandsFile))
            {
                m_log.Info("[STARTUP]: No startup command script specified. Moving on...");
            }
            else
            {
                RunCommandScript(m_startupCommandsFile);
            }

            // Start timer script (run a script every xx seconds)
            if (m_timedScript != "disabled")
            {
                m_scriptTimer = new System.Timers.Timer();
                m_scriptTimer.Enabled = true;
                m_scriptTimer.Interval = m_timeInterval*1000;
                m_scriptTimer.Elapsed += RunAutoTimerScript;
            }
        }

        /// <summary>
        /// Register standard set of region console commands
        /// </summary>
        private void RegisterConsoleCommands()
        {
            MainServer.RegisterHttpConsoleCommands(m_console);

            m_console.Commands.AddCommand("Objects", false, "force update",
                                          "force update",
                                          "Force the update of all objects on clients",
                                          HandleForceUpdate);

            m_console.Commands.AddCommand("General", false, "change region",
                                          "change region <region name>",
                                          "Change current console region",
                                          ChangeSelectedRegion);

            m_console.Commands.AddCommand("Archiving", false, "save xml",
                                          "save xml [<file name>]",
                                          "Save a region's data in XML format",
                                          SaveXml);

            m_console.Commands.AddCommand("Archiving", false, "save xml2",
                                          "save xml2 [<file name>]",
                                          "Save a region's data in XML2 format",
                                          SaveXml2);

            m_console.Commands.AddCommand("Archiving", false, "load xml",
                                          "load xml [<file name> [-newUID [<x> <y> <z>]]]",
                                          "Load a region's data from XML format",
                                          LoadXml);

            m_console.Commands.AddCommand("Archiving", false, "load xml2",
                                          "load xml2 [<file name>]",
                                          "Load a region's data from XML2 format",
                                          LoadXml2);

            m_console.Commands.AddCommand("Archiving", false, "save prims xml2",
                                          "save prims xml2 [<prim name> <file name>]",
                                          "Save named prim to XML2",
                                          SavePrimsXml2);

            m_console.Commands.AddCommand("Archiving", false, "load oar",
                                          "load oar [-m|--merge] [-s|--skip-assets]"
                                             + " [--default-user \"User Name\"]"
                                             + " [--force-terrain] [--force-parcels]"
                                             + " [--no-objects]"
                                             + " [--rotation degrees]"
                                             + " [--bounding-origin \"<x,y,z>\"]"
                                             + " [--bounding-size \"<x,y,z>\"]"
                                             + " [--displacement \"<x,y,z>\"]"
                                             + " [-d|--debug]"
                                             + " [<OAR path>]",
                                          "Load a region's data from an OAR archive.",
                                          "--merge will merge the OAR with the existing scene (suppresses terrain and parcel info loading).\n"
                                            + "--skip-assets will load the OAR but ignore the assets it contains.\n"
                                            + "--default-user will use this user for any objects with an owner whose UUID is not found in the grid.\n"
                                            + "--force-terrain forces the loading of terrain from the oar (undoes suppression done by --merge).\n"
                                            + "--force-parcels forces the loading of parcels from the oar (undoes suppression done by --merge).\n"
                                            + "--no-objects suppresses the addition of any objects (good for loading only the terrain).\n"
                                            + "--rotation specified rotation to be applied to the oar. Specified in degrees.\n"
                                            + "--bounding-origin will only place objects that after displacement and rotation fall within the bounding cube who's position starts at <x,y,z>. Defaults to <0,0,0>.\n"
                                            + "--bounding-size specifies the size of the bounding cube. The default is the size of the destination region and cannot be larger than this.\n"
                                            + "--displacement will add this value to the position of every object loaded.\n"
                                            + "--debug forces the archiver to display messages about where each object is being placed.\n\n"
                                            + "The path can be either a filesystem location or a URI.\n"
                                            + "  If this is not given then the command looks for an OAR named region.oar in the current directory."
                                            + "  [--rotation-center \"<x,y,z>\"] used to be an option, now it does nothing and will be removed soon."
                                            + "When an OAR is being loaded, operations are applied in this order:\n"
                                            + "1: Rotation (around the incoming OARs region center)\n"
                                            + "2: Cropping (a bounding cube with origin and size)\n"
                                            + "3: Displacement (setting offset coordinates within the destination region)",
                                          LoadOar); ;

            m_console.Commands.AddCommand("Archiving", false, "save oar",
                                          //"save oar [-v|--version=<N>] [-p|--profile=<url>] [<OAR path>]",
                                          "save oar [-h|--home=<url>] [--noassets] [--publish] [--perm=<permissions>] [--all] [<OAR path>]",
                                          "Save a region's data to an OAR archive.",
//                                          "-v|--version=<N> generates scene objects as per older versions of the serialization (e.g. -v=0)" + Environment.NewLine
                                          "-h|--home=<url> adds the url of the profile service to the saved user information.\n"
                                          + "--noassets stops assets being saved to the OAR.\n"
                                          + "--publish saves an OAR stripped of owner and last owner information.\n"
                                          + "   on reload, the estate owner will be the owner of all objects\n"
                                          + "   this is useful if you're making oars generally available that might be reloaded to the same grid from which you published\n"
                                          + "--perm=<permissions> stops objects with insufficient permissions from being saved to the OAR.\n"
                                          + "   <permissions> can contain one or more of these characters: \"C\" = Copy, \"T\" = Transfer\n"
                                          + "--all saves all the regions in the simulator, instead of just the current region.\n"
                                          + "The OAR path must be a filesystem path."
                                          + " If this is not given then the oar is saved to region.oar in the current directory.",
                                          SaveOar);

            m_console.Commands.AddCommand("Objects", false, "edit scale",
                                          "edit scale <name> <x> <y> <z>",
                                          "Change the scale of a named prim",
                                          HandleEditScale);

            m_console.Commands.AddCommand("Objects", false, "rotate scene",
                                          "rotate scene <degrees> [centerX, centerY]",
                                          "Rotates all scene objects around centerX, centerY (default 128, 128) (please back up your region before using)",
                                          HandleRotateScene);

            m_console.Commands.AddCommand("Objects", false, "scale scene",
                                          "scale scene <factor>",
                                          "Scales the scene objects (please back up your region before using)",
                                          HandleScaleScene);

            m_console.Commands.AddCommand("Objects", false, "translate scene",
                                          "translate scene xOffset yOffset zOffset",
                                          "translates the scene objects (please back up your region before using)",
                                          HandleTranslateScene);

            m_console.Commands.AddCommand("Users", false, "kick user",
                                          "kick user <first> <last> [--force] [message]",
                                          "Kick a user off the simulator",
                                          "The --force option will kick the user without any checks to see whether it's already in the process of closing\n"
                                          + "Only use this option if you are sure the avatar is inactive and a normal kick user operation does not removed them",
                                          KickUserCommand);

            m_console.Commands.AddCommand("Users", false, "show users",
                                          "show users [full]",
                                          "Show user data for users currently on the region",
                                          "Without the 'full' option, only users actually on the region are shown."
                                            + "  With the 'full' option child agents of users in neighbouring regions are also shown.",
                                          HandleShow);

            m_console.Commands.AddCommand("Comms", false, "show connections",
                                          "show connections",
                                          "Show connection data",
                                          HandleShow);

            m_console.Commands.AddCommand("Comms", false, "show circuits",
                                          "show circuits",
                                          "Show agent circuit data",
                                          HandleShow);

            m_console.Commands.AddCommand("Comms", false, "show pending-objects",
                                          "show pending-objects",
                                          "Show # of objects on the pending queues of all scene viewers",
                                          HandleShow);

            m_console.Commands.AddCommand("General", false, "show modules",
                                          "show modules",
                                          "Show module data",
                                          HandleShow);

            m_console.Commands.AddCommand("Regions", false, "show regions",
                                          "show regions",
                                          "Show region data",
                                          HandleShow);

            m_console.Commands.AddCommand("Regions", false, "show ratings",
                                          "show ratings",
                                          "Show rating data",
                                          HandleShow);

            m_console.Commands.AddCommand("Objects", false, "backup",
                                          "backup",
                                          "Persist currently unsaved object changes immediately instead of waiting for the normal persistence call.",
                                          RunCommand);

            m_console.Commands.AddCommand("Regions", false, "create region",
                                          "create region [\"region name\"] <region_file.ini>",
                                          "Create a new region.",
                                          "The settings for \"region name\" are read from <region_file.ini>. Paths specified with <region_file.ini> are relative to your Regions directory, unless an absolute path is given."
                                          + " If \"region name\" does not exist in <region_file.ini>, it will be added." + Environment.NewLine
                                          + "Without \"region name\", the first region found in <region_file.ini> will be created." + Environment.NewLine
                                          + "If <region_file.ini> does not exist, it will be created.",
                                          HandleCreateRegion);

            m_console.Commands.AddCommand("Regions", false, "restart",
                                          "restart",
                                          "Restart the currently selected region(s) in this instance",
                                          RunCommand);

            m_console.Commands.AddCommand("General", false, "command-script",
                                          "command-script <script>",
                                          "Run a command script from file",
                                          RunCommand);

            m_console.Commands.AddCommand("Regions", false, "remove-region",
                                          "remove-region <name>",
                                          "Remove a region from this simulator",
                                          RunCommand);

            m_console.Commands.AddCommand("Regions", false, "delete-region",
                                          "delete-region <name>",
                                          "Delete a region from disk",
                                          RunCommand);

            m_console.Commands.AddCommand("Estates", false, "estate create",
                                          "estate create <owner UUID> <estate name>",
                                          "Creates a new estate with the specified name, owned by the specified user."
                                          + " Estate name must be unique.",
                                          CreateEstateCommand);

            m_console.Commands.AddCommand("Estates", false, "estate set owner",
                                          "estate set owner <estate-id>[ <UUID> | <Firstname> <Lastname> ]",
                                          "Sets the owner of the specified estate to the specified UUID or user. ",
                                          SetEstateOwnerCommand);

            m_console.Commands.AddCommand("Estates", false, "estate set name",
                                          "estate set name <estate-id> <new name>",
                                          "Sets the name of the specified estate to the specified value. New name must be unique.",
                                          SetEstateNameCommand);

            m_console.Commands.AddCommand("Estates", false, "estate link region",
                                          "estate link region <estate ID> <region ID>",
                                          "Attaches the specified region to the specified estate.",
                                          EstateLinkRegionCommand);
        }

        protected override void ShutdownSpecific()
        {
            if (m_shutdownCommandsFile != String.Empty)
            {
                RunCommandScript(m_shutdownCommandsFile);
            }

            if (m_timedScript != "disabled")
            {
                m_scriptTimer.Dispose();
                m_timedScript = "disabled";
            }

            base.ShutdownSpecific();
        }

        /// <summary>
        /// Timer to run a specific text file as console commands.  Configured in in the main ini file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RunAutoTimerScript(object sender, EventArgs e)
        {
            if (m_timedScript != "disabled")
            {
                RunCommandScript(m_timedScript);
            }
        }

        private void WatchdogTimeoutHandler(Watchdog.ThreadWatchdogInfo twi)
        {
            int now = Environment.TickCount & Int32.MaxValue;
            m_log.ErrorFormat(
                "[WATCHDOG]: Timeout detected for thread \"{0}\". ThreadState={1}. Last tick was {2}ms ago.  {3}",
                twi.Thread.Name,
                twi.Thread.ThreadState,
                now - twi.LastTick,
                twi.AlarmMethod != null ? string.Format("Data: {0}", twi.AlarmMethod()) : "");
        }

        #region Console Commands

        /// <summary>
        /// Kicks users off the region
        /// </summary>
        /// <param name="module"></param>
        /// <param name="cmdparams">name of avatar to kick</param>
        private void KickUserCommand(string module, string[] cmdparams)
        {
            bool force = false;

            OptionSet options = new OptionSet().Add("f|force", delegate (string v) { force = v != null; });

            List<string> mainParams = options.Parse(cmdparams);

            if (mainParams.Count < 4)
                return;

            string alert = null;
            if (mainParams.Count > 4)
                alert = String.Format("\n{0}\n", String.Join(" ", cmdparams, 4, cmdparams.Length - 4));

            IList agents = SceneManager.GetCurrentSceneAvatars();

            foreach (ScenePresence presence in agents)
            {
                RegionInfo regionInfo = presence.Scene.RegionInfo;

                if (presence.Firstname.ToLower().Equals(mainParams[2].ToLower()) &&
                    presence.Lastname.ToLower().Equals(mainParams[3].ToLower()))
                {
                    MainConsole.Instance.Output(
                        String.Format(
                            "Kicking user: {0,-16} {1,-16} {2,-37} in region: {3,-16}",
                            presence.Firstname, presence.Lastname, presence.UUID, regionInfo.RegionName));

                    // kick client...
                    if (alert != null)
                        presence.ControllingClient.Kick(alert);
                    else
                        presence.ControllingClient.Kick("\nYou have been logged out by an administrator.\n");

                    presence.Scene.CloseAgent(presence.UUID, force);
                    break;
                }
            }

            MainConsole.Instance.Output("");
        }

        /// <summary>
        /// Opens a file and uses it as input to the console command parser.
        /// </summary>
        /// <param name="fileName">name of file to use as input to the console</param>
        private static void PrintFileToConsole(string fileName)
        {
            if (File.Exists(fileName))
            {
                StreamReader readFile = File.OpenText(fileName);
                string currentLine;
                while ((currentLine = readFile.ReadLine()) != null)
                {
                    m_log.Info("[!]" + currentLine);
                }
            }
        }

        /// <summary>
        /// Force resending of all updates to all clients in active region(s)
        /// </summary>
        /// <param name="module"></param>
        /// <param name="args"></param>
        private void HandleForceUpdate(string module, string[] args)
        {
            MainConsole.Instance.Output("Updating all clients");
            SceneManager.ForceCurrentSceneClientUpdate();
        }

        /// <summary>
        /// Edits the scale of a primative with the name specified
        /// </summary>
        /// <param name="module"></param>
        /// <param name="args">0,1, name, x, y, z</param>
        private void HandleEditScale(string module, string[] args)
        {
            if (args.Length == 6)
            {
                SceneManager.HandleEditCommandOnCurrentScene(args);
            }
            else
            {
                MainConsole.Instance.Output("Argument error: edit scale <prim name> <x> <y> <z>");
            }
        }

        private void HandleRotateScene(string module, string[] args)
        {
            string usage = "Usage: rotate scene <angle in degrees> [centerX centerY] (centerX and centerY are optional and default to Constants.RegionSize / 2";

            float centerX = Constants.RegionSize * 0.5f;
            float centerY = Constants.RegionSize * 0.5f;

            if (args.Length < 3 || args.Length == 4)
            {
                MainConsole.Instance.Output(usage);
                return;
            }

            float angle = (float)(Convert.ToSingle(args[2]) / 180.0 * Math.PI);
            OpenMetaverse.Quaternion rot = OpenMetaverse.Quaternion.CreateFromAxisAngle(0, 0, 1, angle);

            if (args.Length > 4)
            {
                centerX = Convert.ToSingle(args[3]);
                centerY = Convert.ToSingle(args[4]);
            }

            Vector3 center = new Vector3(centerX, centerY, 0.0f);

            SceneManager.ForEachSelectedScene(delegate(Scene scene)
            {
                scene.ForEachSOG(delegate(SceneObjectGroup sog)
                {
                    if (!sog.IsAttachment)
                    {
                        sog.RootPart.UpdateRotation(rot * sog.GroupRotation);
                        Vector3 offset = sog.AbsolutePosition - center;
                        offset *= rot;
                        sog.UpdateGroupPosition(center + offset);
                    }
                });
            });
        }

        private void HandleScaleScene(string module, string[] args)
        {
            string usage = "Usage: scale scene <factor>";

            if (args.Length < 3)
            {
                MainConsole.Instance.Output(usage);
                return;
            }

            float factor = (float)(Convert.ToSingle(args[2]));

            float minZ = float.MaxValue;

            SceneManager.ForEachSelectedScene(delegate(Scene scene)
            {
                scene.ForEachSOG(delegate(SceneObjectGroup sog)
                {
                    if (!sog.IsAttachment)
                    {
                        if (sog.RootPart.AbsolutePosition.Z < minZ)
                            minZ = sog.RootPart.AbsolutePosition.Z;
                    }
                });
            });

            SceneManager.ForEachSelectedScene(delegate(Scene scene)
            {
                scene.ForEachSOG(delegate(SceneObjectGroup sog)
                {
                    if (!sog.IsAttachment)
                    {
                        Vector3 tmpRootPos = sog.RootPart.AbsolutePosition;
                        tmpRootPos.Z -= minZ;
                        tmpRootPos *= factor;
                        tmpRootPos.Z += minZ;

                        foreach (SceneObjectPart sop in sog.Parts)
                        {
                            if (sop.ParentID != 0)
                                sop.OffsetPosition *= factor;
                            sop.Scale *= factor;
                        }

                        sog.UpdateGroupPosition(tmpRootPos);
                    }
                });
            });
        }

        private void HandleTranslateScene(string module, string[] args)
        {
            string usage = "Usage: translate scene <xOffset, yOffset, zOffset>";

            if (args.Length < 5)
            {
                MainConsole.Instance.Output(usage);
                return;
            }

            float xOFfset = (float)Convert.ToSingle(args[2]);
            float yOffset = (float)Convert.ToSingle(args[3]);
            float zOffset = (float)Convert.ToSingle(args[4]);

            Vector3 offset = new Vector3(xOFfset, yOffset, zOffset);

            SceneManager.ForEachSelectedScene(delegate(Scene scene)
            {
                scene.ForEachSOG(delegate(SceneObjectGroup sog)
                {
                    if (!sog.IsAttachment)
                        sog.UpdateGroupPosition(sog.AbsolutePosition + offset);
                });
            });
        }

        /// <summary>
        /// Creates a new region based on the parameters specified.   This will ask the user questions on the console
        /// </summary>
        /// <param name="module"></param>
        /// <param name="cmd">0,1,region name, region ini or XML file</param>
        private void HandleCreateRegion(string module, string[] cmd)
        {
            string regionName = string.Empty;
            string regionFile = string.Empty;

            if (cmd.Length == 3)
            {
                regionFile = cmd[2];
            }
            else if (cmd.Length > 3)
            {
                regionName = cmd[2];
                regionFile = cmd[3];
            }

            string extension = Path.GetExtension(regionFile).ToLower();
            bool isXml = extension.Equals(".xml");
            bool isIni = extension.Equals(".ini");

            if (!isXml && !isIni)
            {
                MainConsole.Instance.Output("Usage: create region [\"region name\"] <region_file.ini>");
                return;
            }

            if (!Path.IsPathRooted(regionFile))
            {
                string regionsDir = ConfigSource.Source.Configs["Startup"].GetString("regionload_regionsdir", "Regions").Trim();
                regionFile = Path.Combine(regionsDir, regionFile);
            }

            RegionInfo regInfo;
            if (isXml)
            {
                regInfo = new RegionInfo(regionName, regionFile, false, ConfigSource.Source);
            }
            else
            {
                regInfo = new RegionInfo(regionName, regionFile, false, ConfigSource.Source, regionName);
            }

            Scene existingScene;
            if (SceneManager.TryGetScene(regInfo.RegionID, out existingScene))
            {
                MainConsole.Instance.OutputFormat(
                    "ERROR: Cannot create region {0} with ID {1}, this ID is already assigned to region {2}",
                    regInfo.RegionName, regInfo.RegionID, existingScene.RegionInfo.RegionName);

                return;
            }

            bool changed = PopulateRegionEstateInfo(regInfo);
            IScene scene;
            CreateRegion(regInfo, true, out scene);

            if (changed)
                m_estateDataService.StoreEstateSettings(regInfo.EstateSettings);

            scene.Start();
        }

        /// <summary>
        /// Runs commands issued by the server console from the operator
        /// </summary>
        /// <param name="command">The first argument of the parameter (the command)</param>
        /// <param name="cmdparams">Additional arguments passed to the command</param>
        public void RunCommand(string module, string[] cmdparams)
        {
            List<string> args = new List<string>(cmdparams);
            if (args.Count < 1)
                return;

            string command = args[0];
            args.RemoveAt(0);

            cmdparams = args.ToArray();

            switch (command)
            {
                case "backup":
                    MainConsole.Instance.Output("Triggering save of pending object updates to persistent store");
                    SceneManager.BackupCurrentScene();
                    break;

                case "remove-region":
                    string regRemoveName = CombineParams(cmdparams, 0);

                    Scene removeScene;
                    if (SceneManager.TryGetScene(regRemoveName, out removeScene))
                        RemoveRegion(removeScene, false);
                    else
                        MainConsole.Instance.Output("No region with that name");
                    break;

                case "delete-region":
                    string regDeleteName = CombineParams(cmdparams, 0);

                    Scene killScene;
                    if (SceneManager.TryGetScene(regDeleteName, out killScene))
                        RemoveRegion(killScene, true);
                    else
                        MainConsole.Instance.Output("no region with that name");
                    break;

                case "restart":
                    SceneManager.RestartCurrentScene();
                    break;
            }
        }

        /// <summary>
        /// Change the currently selected region.  The selected region is that operated upon by single region commands.
        /// </summary>
        /// <param name="cmdParams"></param>
        protected void ChangeSelectedRegion(string module, string[] cmdparams)
        {
            if (cmdparams.Length > 2)
            {
                string newRegionName = CombineParams(cmdparams, 2);

                if (!SceneManager.TrySetCurrentScene(newRegionName))
                    MainConsole.Instance.Output(String.Format("Couldn't select region {0}", newRegionName));
                else
                    RefreshPrompt();
            }
            else
            {
                MainConsole.Instance.Output("Usage: change region <region name>");
            }
        }

        /// <summary>
        /// Refreshs prompt with the current selection details.
        /// </summary>
        private void RefreshPrompt()
        {
            string regionName = (SceneManager.CurrentScene == null ? "root" : SceneManager.CurrentScene.RegionInfo.RegionName);
            MainConsole.Instance.Output(String.Format("Currently selected region is {0}", regionName));

//            m_log.DebugFormat("Original prompt is {0}", m_consolePrompt);
            string prompt = m_consolePrompt;

            // Replace "\R" with the region name
            // Replace "\\" with "\"
            prompt = m_consolePromptRegex.Replace(prompt, m =>
            {
//                m_log.DebugFormat("Matched {0}", m.Groups[2].Value);
                if (m.Groups[2].Value == "R")
                    return m.Groups[1].Value + regionName;
                else
                    return m.Groups[0].Value;
            });

            m_console.DefaultPrompt = prompt;
            m_console.ConsoleScene = SceneManager.CurrentScene;
        }

        protected override void HandleRestartRegion(RegionInfo whichRegion)
        {
            base.HandleRestartRegion(whichRegion);

            // Where we are restarting multiple scenes at once, a previous call to RefreshPrompt may have set the
            // m_console.ConsoleScene to null (indicating all scenes).
            if (m_console.ConsoleScene != null && whichRegion.RegionName == ((Scene)m_console.ConsoleScene).Name)
                SceneManager.TrySetCurrentScene(whichRegion.RegionName);

            RefreshPrompt();
        }

        // see BaseOpenSimServer
        /// <summary>
        /// Many commands list objects for debugging.  Some of the types are listed  here
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="cmd"></param>
        public override void HandleShow(string mod, string[] cmd)
        {
            base.HandleShow(mod, cmd);

            List<string> args = new List<string>(cmd);
            args.RemoveAt(0);
            string[] showParams = args.ToArray();

            switch (showParams[0])
            {
                case "users":
                    IList agents;
                    if (showParams.Length > 1 && showParams[1] == "full")
                    {
                        agents = SceneManager.GetCurrentScenePresences();
                    } else
                    {
                        agents = SceneManager.GetCurrentSceneAvatars();
                    }

                    MainConsole.Instance.Output(String.Format("\nAgents connected: {0}\n", agents.Count));

                    MainConsole.Instance.Output(
                        String.Format("{0,-16} {1,-16} {2,-37} {3,-11} {4,-16} {5,-30}", "Firstname", "Lastname",
                                      "Agent ID", "Root/Child", "Region", "Position")
                    );

                    foreach (ScenePresence presence in agents)
                    {
                        RegionInfo regionInfo = presence.Scene.RegionInfo;
                        string regionName;

                        if (regionInfo == null)
                        {
                            regionName = "Unresolvable";
                        } else
                        {
                            regionName = regionInfo.RegionName;
                        }

                        MainConsole.Instance.Output(
                            String.Format(
                                "{0,-16} {1,-16} {2,-37} {3,-11} {4,-16} {5,-30}",
                                presence.Firstname,
                                presence.Lastname,
                                presence.UUID,
                                presence.IsChildAgent ? "Child" : "Root",
                                regionName,
                                presence.AbsolutePosition.ToString())
                        );
                    }

                    MainConsole.Instance.Output(String.Empty);
                    break;

                case "connections":
                    HandleShowConnections();
                    break;

                case "circuits":
                    HandleShowCircuits();
                    break;

                case "modules":
                    SceneManager.ForEachSelectedScene(
                        scene =>
                        {
                            MainConsole.Instance.OutputFormat("Loaded region modules in {0} are:", scene.Name);

                            List<IRegionModuleBase> sharedModules = new List<IRegionModuleBase>();
                            List<IRegionModuleBase> nonSharedModules = new List<IRegionModuleBase>();

                            foreach (IRegionModuleBase module in scene.RegionModules.Values)
                            {
                                if (module.GetType().GetInterface("ISharedRegionModule") == null)
                                    nonSharedModules.Add(module);
                                else
                                    sharedModules.Add(module);
                            }

                            foreach (IRegionModuleBase module in sharedModules.OrderBy(m => m.Name))
                                MainConsole.Instance.OutputFormat("New Region Module (Shared): {0}", module.Name);

                            foreach (IRegionModuleBase module in nonSharedModules.OrderBy(m => m.Name))
                                MainConsole.Instance.OutputFormat("New Region Module (Non-Shared): {0}", module.Name);
                        }
                    );

                    MainConsole.Instance.Output("");
                    break;

                case "regions":
                    ConsoleDisplayTable cdt = new ConsoleDisplayTable();
                    cdt.AddColumn("Name", ConsoleDisplayUtil.RegionNameSize);
                    cdt.AddColumn("ID", ConsoleDisplayUtil.UuidSize);
                    cdt.AddColumn("Position", ConsoleDisplayUtil.CoordTupleSize);
                    cdt.AddColumn("Size", 11);
                    cdt.AddColumn("Port", ConsoleDisplayUtil.PortSize);
                    cdt.AddColumn("Ready?", 6);
                    cdt.AddColumn("Estate", ConsoleDisplayUtil.EstateNameSize);
                    SceneManager.ForEachScene(
                        scene =>
                        {
                            RegionInfo ri = scene.RegionInfo;
                            cdt.AddRow(
                                ri.RegionName,
                                ri.RegionID,
                                string.Format("{0},{1}", ri.RegionLocX, ri.RegionLocY),
                                string.Format("{0}x{1}", ri.RegionSizeX, ri.RegionSizeY),
                                ri.InternalEndPoint.Port,
                                scene.Ready ? "Yes" : "No",
                                ri.EstateSettings.EstateName);
                        }
                    );

                    MainConsole.Instance.Output(cdt.ToString());
                    break;

                case "ratings":
                    SceneManager.ForEachScene(
                    delegate(Scene scene)
                    {
                        string rating = "";
                        if (scene.RegionInfo.RegionSettings.Maturity == 1)
                        {
                            rating = "MATURE";
                        }
                        else if (scene.RegionInfo.RegionSettings.Maturity == 2)
                        {
                            rating = "ADULT";
                        }
                        else
                        {
                            rating = "PG";
                        }
                        MainConsole.Instance.Output(String.Format(
                                   "Region Name: {0}, Region Rating {1}",
                                   scene.RegionInfo.RegionName,
                                   rating));
                    });
                    break;
            }
        }

        private void HandleShowCircuits()
        {
            ConsoleDisplayTable cdt = new ConsoleDisplayTable();
            cdt.AddColumn("Region", 20);
            cdt.AddColumn("Avatar name", 24);
            cdt.AddColumn("Type", 5);
            cdt.AddColumn("Code", 10);
            cdt.AddColumn("IP", 16);
            cdt.AddColumn("Viewer Name", 24);

            SceneManager.ForEachScene(
                s =>
                {
                    foreach (AgentCircuitData aCircuit in s.AuthenticateHandler.GetAgentCircuits().Values)
                        cdt.AddRow(
                            s.Name,
                            aCircuit.Name,
                            aCircuit.child ? "child" : "root",
                            aCircuit.circuitcode.ToString(),
                            aCircuit.IPAddress != null ? aCircuit.IPAddress.ToString() : "not set",
                            Util.GetViewerName(aCircuit));
                });

            MainConsole.Instance.Output(cdt.ToString());
        }

        private void HandleShowConnections()
        {
            ConsoleDisplayTable cdt = new ConsoleDisplayTable();
            cdt.AddColumn("Region", 20);
            cdt.AddColumn("Avatar name", 24);
            cdt.AddColumn("Circuit code", 12);
            cdt.AddColumn("Endpoint", 23);
            cdt.AddColumn("Active?", 7);
            cdt.AddColumn("ChildAgent?", 7);
            cdt.AddColumn("ping(ms)", 8);

            SceneManager.ForEachScene(
                s => s.ForEachClient(
                    c =>
                        {
                            bool child = false;
                            if(c.SceneAgent != null && c.SceneAgent.IsChildAgent)
                                child = true;
                            cdt.AddRow(
                            s.Name,
                            c.Name,
                            c.CircuitCode.ToString(),
                            c.RemoteEndPoint.ToString(),
                            c.IsActive.ToString(),
                            child.ToString(),
                            c.PingTimeMS);
                        }));

            MainConsole.Instance.Output(cdt.ToString());
        }

        /// <summary>
        /// Use XML2 format to serialize data to a file
        /// </summary>
        /// <param name="module"></param>
        /// <param name="cmdparams"></param>
        protected void SavePrimsXml2(string module, string[] cmdparams)
        {
            if (cmdparams.Length > 4)
            {
                SceneManager.SaveNamedPrimsToXml2(cmdparams[3], cmdparams[4]);
            }
            else
            {
                SceneManager.SaveNamedPrimsToXml2("Primitive", DEFAULT_PRIM_BACKUP_FILENAME);
            }
        }

        /// <summary>
        /// Use XML format to serialize data to a file
        /// </summary>
        /// <param name="module"></param>
        /// <param name="cmdparams"></param>
        protected void SaveXml(string module, string[] cmdparams)
        {
            MainConsole.Instance.Output("PLEASE NOTE, save-xml is DEPRECATED and may be REMOVED soon.  If you are using this and there is some reason you can't use save-xml2, please file a mantis detailing the reason.");

            if (cmdparams.Length > 2)
            {
                SceneManager.SaveCurrentSceneToXml(cmdparams[2]);
            }
            else
            {
                SceneManager.SaveCurrentSceneToXml(DEFAULT_PRIM_BACKUP_FILENAME);
            }
        }

        /// <summary>
        /// Loads data and region objects from XML format.
        /// </summary>
        /// <param name="module"></param>
        /// <param name="cmdparams"></param>
        protected void LoadXml(string module, string[] cmdparams)
        {
            MainConsole.Instance.Output("PLEASE NOTE, load-xml is DEPRECATED and may be REMOVED soon.  If you are using this and there is some reason you can't use load-xml2, please file a mantis detailing the reason.");

            Vector3 loadOffset = new Vector3(0, 0, 0);
            if (cmdparams.Length > 2)
            {
                bool generateNewIDS = false;
                if (cmdparams.Length > 3)
                {
                    if (cmdparams[3] == "-newUID")
                    {
                        generateNewIDS = true;
                    }
                    if (cmdparams.Length > 4)
                    {
                        loadOffset.X = (float)Convert.ToDecimal(cmdparams[4], Culture.NumberFormatInfo);
                        if (cmdparams.Length > 5)
                        {
                            loadOffset.Y = (float)Convert.ToDecimal(cmdparams[5], Culture.NumberFormatInfo);
                        }
                        if (cmdparams.Length > 6)
                        {
                            loadOffset.Z = (float)Convert.ToDecimal(cmdparams[6], Culture.NumberFormatInfo);
                        }
                        MainConsole.Instance.Output(String.Format("loadOffsets <X,Y,Z> = <{0},{1},{2}>",loadOffset.X,loadOffset.Y,loadOffset.Z));
                    }
                }
                SceneManager.LoadCurrentSceneFromXml(cmdparams[2], generateNewIDS, loadOffset);
            }
            else
            {
                try
                {
                    SceneManager.LoadCurrentSceneFromXml(DEFAULT_PRIM_BACKUP_FILENAME, false, loadOffset);
                }
                catch (FileNotFoundException)
                {
                    MainConsole.Instance.Output("Default xml not found. Usage: load-xml <filename>");
                }
            }
        }
        /// <summary>
        /// Serialize region data to XML2Format
        /// </summary>
        /// <param name="module"></param>
        /// <param name="cmdparams"></param>
        protected void SaveXml2(string module, string[] cmdparams)
        {
            if (cmdparams.Length > 2)
            {
                SceneManager.SaveCurrentSceneToXml2(cmdparams[2]);
            }
            else
            {
                SceneManager.SaveCurrentSceneToXml2(DEFAULT_PRIM_BACKUP_FILENAME);
            }
        }

        /// <summary>
        /// Load region data from Xml2Format
        /// </summary>
        /// <param name="module"></param>
        /// <param name="cmdparams"></param>
        protected void LoadXml2(string module, string[] cmdparams)
        {
            if (cmdparams.Length > 2)
            {
                try
                {
                    SceneManager.LoadCurrentSceneFromXml2(cmdparams[2]);
                }
                catch (FileNotFoundException)
                {
                    MainConsole.Instance.Output("Specified xml not found. Usage: load xml2 <filename>");
                }
            }
            else
            {
                try
                {
                    SceneManager.LoadCurrentSceneFromXml2(DEFAULT_PRIM_BACKUP_FILENAME);
                }
                catch (FileNotFoundException)
                {
                    MainConsole.Instance.Output("Default xml not found. Usage: load xml2 <filename>");
                }
            }
        }

        /// <summary>
        /// Load a whole region from an opensimulator archive.
        /// </summary>
        /// <param name="cmdparams"></param>
        protected void LoadOar(string module, string[] cmdparams)
        {
            try
            {
                SceneManager.LoadArchiveToCurrentScene(cmdparams);
            }
            catch (Exception e)
            {
                MainConsole.Instance.Output(e.Message);
            }
        }

        /// <summary>
        /// Save a region to a file, including all the assets needed to restore it.
        /// </summary>
        /// <param name="cmdparams"></param>
        protected void SaveOar(string module, string[] cmdparams)
        {
            SceneManager.SaveCurrentSceneToArchive(cmdparams);
        }

        protected void CreateEstateCommand(string module, string[] args)
        {
            string response = null;
            UUID userID;

            if (args.Length == 2)
            {
                response = "No user specified.";
            }
            else if (!UUID.TryParse(args[2], out userID))
            {
                response = String.Format("{0} is not a valid UUID", args[2]);
            }
            else if (args.Length == 3)
            {
                response = "No estate name specified.";
            }
            else
            {
                Scene scene = SceneManager.CurrentOrFirstScene;

                // TODO: Is there a better choice here?
                UUID scopeID = UUID.Zero;
                UserAccount account = scene.UserAccountService.GetUserAccount(scopeID, userID);
                if (account == null)
                {
                    response = String.Format("Could not find user {0}", userID);
                }
                else
                {
                    // concatenate it all to "name"
                    StringBuilder sb = new StringBuilder(args[3]);
                    for (int i = 4; i < args.Length; i++)
                        sb.Append (" " + args[i]);
                    string estateName = sb.ToString().Trim();

                    // send it off for processing.
                    IEstateModule estateModule = scene.RequestModuleInterface<IEstateModule>();
                    response = estateModule.CreateEstate(estateName, userID);
                    if (response == String.Empty)
                    {
                        List<int> estates = scene.EstateDataService.GetEstates(estateName);
                        response = String.Format("Estate {0} created as \"{1}\"", estates.ElementAt(0), estateName);
                    }
                }
            }

            // give the user some feedback
            if (response != null)
                MainConsole.Instance.Output(response);
        }

        protected void SetEstateOwnerCommand(string module, string[] args)
        {
            string response = null;

            Scene scene = SceneManager.CurrentOrFirstScene;
            IEstateModule estateModule = scene.RequestModuleInterface<IEstateModule>();

            if (args.Length == 3)
            {
                response = "No estate specified.";
            }
            else
            {
                int estateId;
                if (!int.TryParse(args[3], out estateId))
                {
                    response = String.Format("\"{0}\" is not a valid ID for an Estate", args[3]);
                }
                else
                {
                    if (args.Length == 4)
                    {
                        response = "No user specified.";
                    }
                    else
                    {
                        UserAccount account = null;

                        // TODO: Is there a better choice here?
                        UUID scopeID = UUID.Zero;

                        string s1 = args[4];
                        if (args.Length == 5)
                        {
                            // attempt to get account by UUID
                            UUID u;
                            if (UUID.TryParse(s1, out u))
                            {
                                account = scene.UserAccountService.GetUserAccount(scopeID, u);
                                if (account == null)
                                    response = String.Format("Could not find user {0}", s1);
                            }
                            else
                            {
                                response = String.Format("Invalid UUID {0}", s1);
                            }
                        }
                        else
                        {
                            // attempt to get account by Firstname, Lastname
                            string s2 = args[5];
                            account = scene.UserAccountService.GetUserAccount(scopeID, s1, s2);
                            if (account == null)
                                response = String.Format("Could not find user {0} {1}", s1, s2);
                        }

                        // If it's valid, send it off for processing.
                        if (account != null)
                            response = estateModule.SetEstateOwner(estateId, account);

                        if (response == String.Empty)
                        {
                            response = String.Format("Estate owner changed to {0} ({1} {2})", account.PrincipalID, account.FirstName, account.LastName);
                        }
                    }
                }
            }

            // give the user some feedback
            if (response != null)
                MainConsole.Instance.Output(response);
        }

        protected void SetEstateNameCommand(string module, string[] args)
        {
            string response = null;

            Scene scene = SceneManager.CurrentOrFirstScene;
            IEstateModule estateModule = scene.RequestModuleInterface<IEstateModule>();

            if (args.Length == 3)
            {
                response = "No estate specified.";
            }
            else
            {
                int estateId;
                if (!int.TryParse(args[3], out estateId))
                {
                    response = String.Format("\"{0}\" is not a valid ID for an Estate", args[3]);
                }
                else
                {
                    if (args.Length == 4)
                    {
                        response = "No name specified.";
                    }
                    else
                    {
                        // everything after the estate ID is "name"
                        StringBuilder sb = new StringBuilder(args[4]);
                        for (int i = 5; i < args.Length; i++)
                            sb.Append (" " + args[i]);

                        string estateName = sb.ToString();

                        // send it off for processing.
                        response = estateModule.SetEstateName(estateId, estateName);

                        if (response == String.Empty)
                        {
                            response = String.Format("Estate {0} renamed to \"{1}\"", estateId, estateName);
                        }
                    }
                }
            }

            // give the user some feedback
            if (response != null)
                MainConsole.Instance.Output(response);
        }

        private void EstateLinkRegionCommand(string module, string[] args)
        {
            int estateId =-1;
            UUID regionId = UUID.Zero;
            Scene scene = null;
            string response = null;

            if (args.Length == 3)
            {
                response = "No estate specified.";
            }
            else if (!int.TryParse(args [3], out estateId))
            {
                response = String.Format("\"{0}\" is not a valid ID for an Estate", args [3]);
            }
            else if (args.Length == 4)
            {
                response = "No region specified.";
            }
            else if (!UUID.TryParse(args[4], out regionId))
            {
                response = String.Format("\"{0}\" is not a valid UUID for a Region", args [4]);
            }
            else if (!SceneManager.TryGetScene(regionId, out scene))
            {
                // region may exist, but on a different sim.
                response = String.Format("No access to Region \"{0}\"", args [4]);
            }

            if (response != null)
            {
                MainConsole.Instance.Output(response);
                return;
            }

            // send it off for processing.
            IEstateModule estateModule = scene.RequestModuleInterface<IEstateModule>();
            response = estateModule.SetRegionEstate(scene.RegionInfo, estateId);
            if (response == String.Empty)
            {
                estateModule.TriggerRegionInfoChange();
                estateModule.sendRegionHandshakeToAll();
                response = String.Format ("Region {0} is now attached to estate {1}", regionId, estateId);
            }

            // give the user some feedback
            if (response != null)
                MainConsole.Instance.Output (response);
        }

        #endregion

        private static string CombineParams(string[] commandParams, int pos)
        {
            string result = String.Empty;
            for (int i = pos; i < commandParams.Length; i++)
            {
                result += commandParams[i] + " ";
            }
            result = result.TrimEnd(' ');
            return result;
        }
    }
}