OpenSim.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Timers;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Console;
  38. using OpenSim.Framework.Statistics;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. namespace OpenSim
  42. {
  43. /// <summary>
  44. /// Interactive OpenSim region server
  45. /// </summary>
  46. public class OpenSim : OpenSimBase
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. protected string m_startupCommandsFile;
  50. protected string m_shutdownCommandsFile;
  51. protected bool m_gui = false;
  52. protected string m_consoleType = "local";
  53. protected uint m_consolePort = 0;
  54. private string m_timedScript = "disabled";
  55. private Timer m_scriptTimer;
  56. public OpenSim(IConfigSource configSource) : base(configSource)
  57. {
  58. }
  59. protected override void ReadExtraConfigSettings()
  60. {
  61. base.ReadExtraConfigSettings();
  62. IConfig startupConfig = m_config.Source.Configs["Startup"];
  63. IConfig networkConfig = m_config.Source.Configs["Network"];
  64. int stpMaxThreads = 15;
  65. if (startupConfig != null)
  66. {
  67. m_startupCommandsFile = startupConfig.GetString("startup_console_commands_file", "startup_commands.txt");
  68. m_shutdownCommandsFile = startupConfig.GetString("shutdown_console_commands_file", "shutdown_commands.txt");
  69. if (startupConfig.GetString("console", String.Empty) == String.Empty)
  70. m_gui = startupConfig.GetBoolean("gui", false);
  71. else
  72. m_consoleType= startupConfig.GetString("console", String.Empty);
  73. if (networkConfig != null)
  74. m_consolePort = (uint)networkConfig.GetInt("console_port", 0);
  75. m_timedScript = startupConfig.GetString("timer_Script", "disabled");
  76. if (m_logFileAppender != null)
  77. {
  78. if (m_logFileAppender is log4net.Appender.FileAppender)
  79. {
  80. log4net.Appender.FileAppender appender =
  81. (log4net.Appender.FileAppender)m_logFileAppender;
  82. string fileName = startupConfig.GetString("LogFile", String.Empty);
  83. if (fileName != String.Empty)
  84. {
  85. appender.File = fileName;
  86. appender.ActivateOptions();
  87. }
  88. m_log.InfoFormat("[LOGGING]: Logging started to file {0}", appender.File);
  89. }
  90. }
  91. string asyncCallMethodStr = startupConfig.GetString("async_call_method", String.Empty);
  92. FireAndForgetMethod asyncCallMethod;
  93. if (!String.IsNullOrEmpty(asyncCallMethodStr) && Utils.EnumTryParse<FireAndForgetMethod>(asyncCallMethodStr, out asyncCallMethod))
  94. Util.FireAndForgetMethod = asyncCallMethod;
  95. stpMaxThreads = startupConfig.GetInt("MaxPoolThreads", 15);
  96. }
  97. if (Util.FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool)
  98. Util.InitThreadPool(stpMaxThreads);
  99. m_log.Info("[OPENSIM MAIN]: Using async_call_method " + Util.FireAndForgetMethod);
  100. }
  101. /// <summary>
  102. /// Performs initialisation of the scene, such as loading configuration from disk.
  103. /// </summary>
  104. protected override void StartupSpecific()
  105. {
  106. m_log.Info("====================================================================");
  107. m_log.Info("========================= STARTING OPENSIM =========================");
  108. m_log.Info("====================================================================");
  109. m_log.InfoFormat("[OPENSIM MAIN]: Running ");
  110. //m_log.InfoFormat("[OPENSIM MAIN]: GC Is Server GC: {0}", GCSettings.IsServerGC.ToString());
  111. // http://msdn.microsoft.com/en-us/library/bb384202.aspx
  112. //GCSettings.LatencyMode = GCLatencyMode.Batch;
  113. //m_log.InfoFormat("[OPENSIM MAIN]: GC Latency Mode: {0}", GCSettings.LatencyMode.ToString());
  114. if (m_gui) // Driven by external GUI
  115. m_console = new CommandConsole("Region");
  116. else
  117. {
  118. switch (m_consoleType)
  119. {
  120. case "basic":
  121. m_console = new CommandConsole("Region");
  122. break;
  123. case "rest":
  124. m_console = new RemoteConsole("Region");
  125. ((RemoteConsole)m_console).ReadConfig(m_config.Source);
  126. break;
  127. default:
  128. m_console = new LocalConsole("Region");
  129. break;
  130. }
  131. }
  132. MainConsole.Instance = m_console;
  133. RegisterConsoleCommands();
  134. base.StartupSpecific();
  135. MainServer.Instance.AddStreamHandler(new OpenSim.SimStatusHandler());
  136. MainServer.Instance.AddStreamHandler(new OpenSim.XSimStatusHandler(this));
  137. if (userStatsURI != String.Empty)
  138. MainServer.Instance.AddStreamHandler(new OpenSim.UXSimStatusHandler(this));
  139. if (m_console is RemoteConsole)
  140. {
  141. if (m_consolePort == 0)
  142. {
  143. ((RemoteConsole)m_console).SetServer(m_httpServer);
  144. }
  145. else
  146. {
  147. ((RemoteConsole)m_console).SetServer(MainServer.GetHttpServer(m_consolePort));
  148. }
  149. }
  150. //Run Startup Commands
  151. if (String.IsNullOrEmpty(m_startupCommandsFile))
  152. {
  153. m_log.Info("[STARTUP]: No startup command script specified. Moving on...");
  154. }
  155. else
  156. {
  157. RunCommandScript(m_startupCommandsFile);
  158. }
  159. // Start timer script (run a script every xx seconds)
  160. if (m_timedScript != "disabled")
  161. {
  162. m_scriptTimer = new Timer();
  163. m_scriptTimer.Enabled = true;
  164. m_scriptTimer.Interval = 1200*1000;
  165. m_scriptTimer.Elapsed += RunAutoTimerScript;
  166. }
  167. // Hook up to the watchdog timer
  168. Watchdog.OnWatchdogTimeout += WatchdogTimeoutHandler;
  169. PrintFileToConsole("startuplogo.txt");
  170. // For now, start at the 'root' level by default
  171. if (m_sceneManager.Scenes.Count == 1) // If there is only one region, select it
  172. ChangeSelectedRegion("region",
  173. new string[] {"change", "region", m_sceneManager.Scenes[0].RegionInfo.RegionName});
  174. else
  175. ChangeSelectedRegion("region", new string[] {"change", "region", "root"});
  176. }
  177. /// <summary>
  178. /// Register standard set of region console commands
  179. /// </summary>
  180. private void RegisterConsoleCommands()
  181. {
  182. m_console.Commands.AddCommand("region", false, "clear assets",
  183. "clear assets",
  184. "Clear the asset cache", HandleClearAssets);
  185. m_console.Commands.AddCommand("region", false, "force update",
  186. "force update",
  187. "Force the update of all objects on clients",
  188. HandleForceUpdate);
  189. m_console.Commands.AddCommand("region", false, "debug packet",
  190. "debug packet <level>",
  191. "Turn on packet debugging",
  192. "If level > 255 then all incoming and outgoing packets are logged.\n"
  193. + "If level <= 255 then incoming AgentUpdate and outgoing SimStats and SimulatorViewerTimeMessage packets are not logged.\n"
  194. + "If level <= 200 then incoming RequestImage and outgoing ImagePacket, ImageData, LayerData and CoarseLocationUpdate packets are not logged.\n"
  195. + "If level <= 100 then incoming ViewerEffect and AgentAnimation and outgoing ViewerEffect and AvatarAnimation packets are not logged.\n"
  196. + "If level <= 0 then no packets are logged.",
  197. Debug);
  198. m_console.Commands.AddCommand("region", false, "debug scene",
  199. "debug scene <cripting> <collisions> <physics>",
  200. "Turn on scene debugging", Debug);
  201. m_console.Commands.AddCommand("region", false, "change region",
  202. "change region <region name>",
  203. "Change current console region", ChangeSelectedRegion);
  204. m_console.Commands.AddCommand("region", false, "save xml",
  205. "save xml",
  206. "Save a region's data in XML format", SaveXml);
  207. m_console.Commands.AddCommand("region", false, "save xml2",
  208. "save xml2",
  209. "Save a region's data in XML2 format", SaveXml2);
  210. m_console.Commands.AddCommand("region", false, "load xml",
  211. "load xml [-newIDs [<x> <y> <z>]]",
  212. "Load a region's data from XML format", LoadXml);
  213. m_console.Commands.AddCommand("region", false, "load xml2",
  214. "load xml2",
  215. "Load a region's data from XML2 format", LoadXml2);
  216. m_console.Commands.AddCommand("region", false, "save prims xml2",
  217. "save prims xml2 [<prim name> <file name>]",
  218. "Save named prim to XML2", SavePrimsXml2);
  219. m_console.Commands.AddCommand("region", false, "load oar",
  220. "load oar [--merge] [--skip-assets] [<OAR path>]",
  221. "Load a region's data from an OAR archive.",
  222. "--merge will merge the OAR with the existing scene." + Environment.NewLine
  223. + "--skip-assets will load the OAR but ignore the assets it contains." + Environment.NewLine
  224. + "The path can be either a filesystem location or a URI."
  225. + " If this is not given then the command looks for an OAR named region.oar in the current directory.",
  226. LoadOar);
  227. m_console.Commands.AddCommand("region", false, "save oar",
  228. "save oar [<OAR path>]",
  229. "Save a region's data to an OAR archive.",
  230. "The OAR path must be a filesystem path."
  231. + " If this is not given then the oar is saved to region.oar in the current directory.",
  232. SaveOar);
  233. m_console.Commands.AddCommand("region", false, "edit scale",
  234. "edit scale <name> <x> <y> <z>",
  235. "Change the scale of a named prim", HandleEditScale);
  236. m_console.Commands.AddCommand("region", false, "kick user",
  237. "kick user <first> <last> [message]",
  238. "Kick a user off the simulator", KickUserCommand);
  239. m_console.Commands.AddCommand("region", false, "show assets",
  240. "show assets",
  241. "Show asset data", HandleShow);
  242. m_console.Commands.AddCommand("region", false, "show users",
  243. "show users [full]",
  244. "Show user data", HandleShow);
  245. m_console.Commands.AddCommand("region", false, "show connections",
  246. "show connections",
  247. "Show connection data", HandleShow);
  248. m_console.Commands.AddCommand("region", false, "show users full",
  249. "show users full",
  250. String.Empty, HandleShow);
  251. m_console.Commands.AddCommand("region", false, "show modules",
  252. "show modules",
  253. "Show module data", HandleShow);
  254. m_console.Commands.AddCommand("region", false, "show regions",
  255. "show regions",
  256. "Show region data", HandleShow);
  257. m_console.Commands.AddCommand("region", false, "show queues",
  258. "show queues",
  259. "Show queue data", HandleShow);
  260. m_console.Commands.AddCommand("region", false, "show ratings",
  261. "show ratings",
  262. "Show rating data", HandleShow);
  263. m_console.Commands.AddCommand("region", false, "backup",
  264. "backup",
  265. "Persist objects to the database now", RunCommand);
  266. m_console.Commands.AddCommand("region", false, "create region",
  267. "create region [\"region name\"] <region_file.ini>",
  268. "Create a new region.",
  269. "The settings for \"region name\" are read from <region_file.ini> in your Regions directory."
  270. + " If \"region name\" does not exist in <region_file.ini>, it will be added." + Environment.NewLine
  271. + "Without \"region name\", the first region found in <region_file.ini> will be created." + Environment.NewLine
  272. + "If <region_file.ini> does not exist, it will be created.",
  273. HandleCreateRegion);
  274. m_console.Commands.AddCommand("region", false, "restart",
  275. "restart",
  276. "Restart all sims in this instance", RunCommand);
  277. m_console.Commands.AddCommand("region", false, "config set",
  278. "config set <section> <field> <value>",
  279. "Set a config option", HandleConfig);
  280. m_console.Commands.AddCommand("region", false, "config get",
  281. "config get <section> <field>",
  282. "Read a config option", HandleConfig);
  283. m_console.Commands.AddCommand("region", false, "config save",
  284. "config save",
  285. "Save current configuration", HandleConfig);
  286. m_console.Commands.AddCommand("region", false, "command-script",
  287. "command-script <script>",
  288. "Run a command script from file", RunCommand);
  289. m_console.Commands.AddCommand("region", false, "remove-region",
  290. "remove-region <name>",
  291. "Remove a region from this simulator", RunCommand);
  292. m_console.Commands.AddCommand("region", false, "delete-region",
  293. "delete-region <name>",
  294. "Delete a region from disk", RunCommand);
  295. m_console.Commands.AddCommand("region", false, "modules list",
  296. "modules list",
  297. "List modules", HandleModules);
  298. m_console.Commands.AddCommand("region", false, "modules load",
  299. "modules load <name>",
  300. "Load a module", HandleModules);
  301. m_console.Commands.AddCommand("region", false, "modules unload",
  302. "modules unload <name>",
  303. "Unload a module", HandleModules);
  304. m_console.Commands.AddCommand("region", false, "Add-InventoryHost",
  305. "Add-InventoryHost <host>",
  306. String.Empty, RunCommand);
  307. m_console.Commands.AddCommand("region", false, "kill uuid",
  308. "kill uuid <UUID>",
  309. "Kill an object by UUID", KillUUID);
  310. }
  311. public override void ShutdownSpecific()
  312. {
  313. if (m_shutdownCommandsFile != String.Empty)
  314. {
  315. RunCommandScript(m_shutdownCommandsFile);
  316. }
  317. base.ShutdownSpecific();
  318. }
  319. /// <summary>
  320. /// Timer to run a specific text file as console commands. Configured in in the main ini file
  321. /// </summary>
  322. /// <param name="sender"></param>
  323. /// <param name="e"></param>
  324. private void RunAutoTimerScript(object sender, EventArgs e)
  325. {
  326. if (m_timedScript != "disabled")
  327. {
  328. RunCommandScript(m_timedScript);
  329. }
  330. }
  331. private void WatchdogTimeoutHandler(System.Threading.Thread thread, int lastTick)
  332. {
  333. int now = Environment.TickCount & Int32.MaxValue;
  334. m_log.ErrorFormat("[WATCHDOG]: Timeout detected for thread \"{0}\". ThreadState={1}. Last tick was {2}ms ago",
  335. thread.Name, thread.ThreadState, now - lastTick);
  336. }
  337. #region Console Commands
  338. /// <summary>
  339. /// Kicks users off the region
  340. /// </summary>
  341. /// <param name="module"></param>
  342. /// <param name="cmdparams">name of avatar to kick</param>
  343. private void KickUserCommand(string module, string[] cmdparams)
  344. {
  345. if (cmdparams.Length < 4)
  346. return;
  347. string alert = null;
  348. if (cmdparams.Length > 4)
  349. alert = String.Format("\n{0}\n", String.Join(" ", cmdparams, 4, cmdparams.Length - 4));
  350. IList agents = m_sceneManager.GetCurrentSceneAvatars();
  351. foreach (ScenePresence presence in agents)
  352. {
  353. RegionInfo regionInfo = presence.Scene.RegionInfo;
  354. if (presence.Firstname.ToLower().Contains(cmdparams[2].ToLower()) &&
  355. presence.Lastname.ToLower().Contains(cmdparams[3].ToLower()))
  356. {
  357. MainConsole.Instance.Output(
  358. String.Format(
  359. "Kicking user: {0,-16} {1,-16} {2,-37} in region: {3,-16}",
  360. presence.Firstname, presence.Lastname, presence.UUID, regionInfo.RegionName));
  361. // kick client...
  362. if (alert != null)
  363. presence.ControllingClient.Kick(alert);
  364. else
  365. presence.ControllingClient.Kick("\nThe OpenSim manager kicked you out.\n");
  366. // ...and close on our side
  367. presence.Scene.IncomingCloseAgent(presence.UUID);
  368. }
  369. }
  370. MainConsole.Instance.Output("");
  371. }
  372. /// <summary>
  373. /// Run an optional startup list of commands
  374. /// </summary>
  375. /// <param name="fileName"></param>
  376. private void RunCommandScript(string fileName)
  377. {
  378. if (File.Exists(fileName))
  379. {
  380. m_log.Info("[COMMANDFILE]: Running " + fileName);
  381. using (StreamReader readFile = File.OpenText(fileName))
  382. {
  383. string currentCommand;
  384. while ((currentCommand = readFile.ReadLine()) != null)
  385. {
  386. if (currentCommand != String.Empty)
  387. {
  388. m_log.Info("[COMMANDFILE]: Running '" + currentCommand + "'");
  389. m_console.RunCommand(currentCommand);
  390. }
  391. }
  392. }
  393. }
  394. }
  395. /// <summary>
  396. /// Opens a file and uses it as input to the console command parser.
  397. /// </summary>
  398. /// <param name="fileName">name of file to use as input to the console</param>
  399. private static void PrintFileToConsole(string fileName)
  400. {
  401. if (File.Exists(fileName))
  402. {
  403. StreamReader readFile = File.OpenText(fileName);
  404. string currentLine;
  405. while ((currentLine = readFile.ReadLine()) != null)
  406. {
  407. m_log.Info("[!]" + currentLine);
  408. }
  409. }
  410. }
  411. private void HandleClearAssets(string module, string[] args)
  412. {
  413. MainConsole.Instance.Output("Not implemented.");
  414. }
  415. /// <summary>
  416. /// Force resending of all updates to all clients in active region(s)
  417. /// </summary>
  418. /// <param name="module"></param>
  419. /// <param name="args"></param>
  420. private void HandleForceUpdate(string module, string[] args)
  421. {
  422. MainConsole.Instance.Output("Updating all clients");
  423. m_sceneManager.ForceCurrentSceneClientUpdate();
  424. }
  425. /// <summary>
  426. /// Edits the scale of a primative with the name specified
  427. /// </summary>
  428. /// <param name="module"></param>
  429. /// <param name="args">0,1, name, x, y, z</param>
  430. private void HandleEditScale(string module, string[] args)
  431. {
  432. if (args.Length == 6)
  433. {
  434. m_sceneManager.HandleEditCommandOnCurrentScene(args);
  435. }
  436. else
  437. {
  438. MainConsole.Instance.Output("Argument error: edit scale <prim name> <x> <y> <z>");
  439. }
  440. }
  441. /// <summary>
  442. /// Creates a new region based on the parameters specified. This will ask the user questions on the console
  443. /// </summary>
  444. /// <param name="module"></param>
  445. /// <param name="cmd">0,1,region name, region ini or XML file</param>
  446. private void HandleCreateRegion(string module, string[] cmd)
  447. {
  448. string regionName = string.Empty;
  449. string regionFile = string.Empty;
  450. if (cmd.Length == 3)
  451. {
  452. regionFile = cmd[2];
  453. }
  454. else if (cmd.Length > 3)
  455. {
  456. regionName = cmd[2];
  457. regionFile = cmd[3];
  458. }
  459. string extension = Path.GetExtension(regionFile).ToLower();
  460. bool isXml = extension.Equals(".xml");
  461. bool isIni = extension.Equals(".ini");
  462. if (!isXml && !isIni)
  463. {
  464. MainConsole.Instance.Output("Usage: create region [\"region name\"] <region_file.ini>");
  465. return;
  466. }
  467. if (!Path.IsPathRooted(regionFile))
  468. {
  469. string regionsDir = ConfigSource.Source.Configs["Startup"].GetString("regionload_regionsdir", "Regions").Trim();
  470. regionFile = Path.Combine(regionsDir, regionFile);
  471. }
  472. RegionInfo regInfo;
  473. if (isXml)
  474. {
  475. regInfo = new RegionInfo(regionName, regionFile, false, ConfigSource.Source);
  476. }
  477. else
  478. {
  479. regInfo = new RegionInfo(regionName, regionFile, false, ConfigSource.Source, regionName);
  480. }
  481. IScene scene;
  482. PopulateRegionEstateInfo(regInfo);
  483. CreateRegion(regInfo, true, out scene);
  484. regInfo.EstateSettings.Save();
  485. }
  486. /// <summary>
  487. /// Change and load configuration file data.
  488. /// </summary>
  489. /// <param name="module"></param>
  490. /// <param name="cmd"></param>
  491. private void HandleConfig(string module, string[] cmd)
  492. {
  493. List<string> args = new List<string>(cmd);
  494. args.RemoveAt(0);
  495. string[] cmdparams = args.ToArray();
  496. string n = "CONFIG";
  497. if (cmdparams.Length > 0)
  498. {
  499. switch (cmdparams[0].ToLower())
  500. {
  501. case "set":
  502. if (cmdparams.Length < 4)
  503. {
  504. MainConsole.Instance.Output(String.Format("SYNTAX: {0} SET SECTION KEY VALUE",n));
  505. MainConsole.Instance.Output(String.Format("EXAMPLE: {0} SET ScriptEngine.DotNetEngine NumberOfScriptThreads 5",n));
  506. }
  507. else
  508. {
  509. IConfig c;
  510. IConfigSource source = new IniConfigSource();
  511. c = source.AddConfig(cmdparams[1]);
  512. if (c != null)
  513. {
  514. string _value = String.Join(" ", cmdparams, 3, cmdparams.Length - 3);
  515. c.Set(cmdparams[2], _value);
  516. m_config.Source.Merge(source);
  517. MainConsole.Instance.Output(String.Format("{0} {0} {1} {2} {3}",n,cmdparams[1],cmdparams[2],_value));
  518. }
  519. }
  520. break;
  521. case "get":
  522. if (cmdparams.Length < 3)
  523. {
  524. MainConsole.Instance.Output(String.Format("SYNTAX: {0} GET SECTION KEY",n));
  525. MainConsole.Instance.Output(String.Format("EXAMPLE: {0} GET ScriptEngine.DotNetEngine NumberOfScriptThreads",n));
  526. }
  527. else
  528. {
  529. IConfig c = m_config.Source.Configs[cmdparams[1]];
  530. if (c == null)
  531. {
  532. MainConsole.Instance.Output(String.Format("Section \"{0}\" does not exist.",cmdparams[1]));
  533. break;
  534. }
  535. else
  536. {
  537. MainConsole.Instance.Output(String.Format("{0} GET {1} {2} : {3}",n,cmdparams[1],cmdparams[2],
  538. c.GetString(cmdparams[2])));
  539. }
  540. }
  541. break;
  542. case "save":
  543. if (cmdparams.Length < 2)
  544. {
  545. MainConsole.Instance.Output("SYNTAX: " + n + " SAVE FILE");
  546. return;
  547. }
  548. if (Application.iniFilePath == cmdparams[1])
  549. {
  550. MainConsole.Instance.Output("FILE can not be " + Application.iniFilePath);
  551. return;
  552. }
  553. MainConsole.Instance.Output("Saving configuration file: " + cmdparams[1]);
  554. m_config.Save(cmdparams[1]);
  555. break;
  556. }
  557. }
  558. }
  559. /// <summary>
  560. /// Load, Unload, and list Region modules in use
  561. /// </summary>
  562. /// <param name="module"></param>
  563. /// <param name="cmd"></param>
  564. private void HandleModules(string module, string[] cmd)
  565. {
  566. List<string> args = new List<string>(cmd);
  567. args.RemoveAt(0);
  568. string[] cmdparams = args.ToArray();
  569. if (cmdparams.Length > 0)
  570. {
  571. switch (cmdparams[0].ToLower())
  572. {
  573. case "list":
  574. foreach (IRegionModule irm in m_moduleLoader.GetLoadedSharedModules)
  575. {
  576. MainConsole.Instance.Output(String.Format("Shared region module: {0}", irm.Name));
  577. }
  578. break;
  579. case "unload":
  580. if (cmdparams.Length > 1)
  581. {
  582. foreach (IRegionModule rm in new ArrayList(m_moduleLoader.GetLoadedSharedModules))
  583. {
  584. if (rm.Name.ToLower() == cmdparams[1].ToLower())
  585. {
  586. MainConsole.Instance.Output(String.Format("Unloading module: {0}", rm.Name));
  587. m_moduleLoader.UnloadModule(rm);
  588. }
  589. }
  590. }
  591. break;
  592. case "load":
  593. if (cmdparams.Length > 1)
  594. {
  595. foreach (Scene s in new ArrayList(m_sceneManager.Scenes))
  596. {
  597. MainConsole.Instance.Output(String.Format("Loading module: {0}", cmdparams[1]));
  598. m_moduleLoader.LoadRegionModules(cmdparams[1], s);
  599. }
  600. }
  601. break;
  602. }
  603. }
  604. }
  605. /// <summary>
  606. /// Runs commands issued by the server console from the operator
  607. /// </summary>
  608. /// <param name="command">The first argument of the parameter (the command)</param>
  609. /// <param name="cmdparams">Additional arguments passed to the command</param>
  610. public void RunCommand(string module, string[] cmdparams)
  611. {
  612. List<string> args = new List<string>(cmdparams);
  613. if (args.Count < 1)
  614. return;
  615. string command = args[0];
  616. args.RemoveAt(0);
  617. cmdparams = args.ToArray();
  618. switch (command)
  619. {
  620. case "command-script":
  621. if (cmdparams.Length > 0)
  622. {
  623. RunCommandScript(cmdparams[0]);
  624. }
  625. break;
  626. case "backup":
  627. m_sceneManager.BackupCurrentScene();
  628. break;
  629. case "remove-region":
  630. string regRemoveName = CombineParams(cmdparams, 0);
  631. Scene removeScene;
  632. if (m_sceneManager.TryGetScene(regRemoveName, out removeScene))
  633. RemoveRegion(removeScene, false);
  634. else
  635. MainConsole.Instance.Output("no region with that name");
  636. break;
  637. case "delete-region":
  638. string regDeleteName = CombineParams(cmdparams, 0);
  639. Scene killScene;
  640. if (m_sceneManager.TryGetScene(regDeleteName, out killScene))
  641. RemoveRegion(killScene, true);
  642. else
  643. MainConsole.Instance.Output("no region with that name");
  644. break;
  645. case "restart":
  646. m_sceneManager.RestartCurrentScene();
  647. break;
  648. case "Add-InventoryHost":
  649. if (cmdparams.Length > 0)
  650. {
  651. MainConsole.Instance.Output("Not implemented.");
  652. }
  653. break;
  654. }
  655. }
  656. /// <summary>
  657. /// Change the currently selected region. The selected region is that operated upon by single region commands.
  658. /// </summary>
  659. /// <param name="cmdParams"></param>
  660. protected void ChangeSelectedRegion(string module, string[] cmdparams)
  661. {
  662. if (cmdparams.Length > 2)
  663. {
  664. string newRegionName = CombineParams(cmdparams, 2);
  665. if (!m_sceneManager.TrySetCurrentScene(newRegionName))
  666. MainConsole.Instance.Output(String.Format("Couldn't select region {0}", newRegionName));
  667. }
  668. else
  669. {
  670. MainConsole.Instance.Output("Usage: change region <region name>");
  671. }
  672. string regionName = (m_sceneManager.CurrentScene == null ? "root" : m_sceneManager.CurrentScene.RegionInfo.RegionName);
  673. MainConsole.Instance.Output(String.Format("Currently selected region is {0}", regionName));
  674. m_console.DefaultPrompt = String.Format("Region ({0}) ", regionName);
  675. m_console.ConsoleScene = m_sceneManager.CurrentScene;
  676. }
  677. /// <summary>
  678. /// Turn on some debugging values for OpenSim.
  679. /// </summary>
  680. /// <param name="args"></param>
  681. protected void Debug(string module, string[] args)
  682. {
  683. if (args.Length == 1)
  684. return;
  685. switch (args[1])
  686. {
  687. case "packet":
  688. if (args.Length > 2)
  689. {
  690. int newDebug;
  691. if (int.TryParse(args[2], out newDebug))
  692. {
  693. m_sceneManager.SetDebugPacketLevelOnCurrentScene(newDebug);
  694. }
  695. else
  696. {
  697. MainConsole.Instance.Output("packet debug should be 0..255");
  698. }
  699. MainConsole.Instance.Output(String.Format("New packet debug: {0}", newDebug));
  700. }
  701. break;
  702. case "scene":
  703. if (args.Length == 5)
  704. {
  705. if (m_sceneManager.CurrentScene == null)
  706. {
  707. MainConsole.Instance.Output("Please use 'change region <regioname>' first");
  708. }
  709. else
  710. {
  711. bool scriptingOn = !Convert.ToBoolean(args[2]);
  712. bool collisionsOn = !Convert.ToBoolean(args[3]);
  713. bool physicsOn = !Convert.ToBoolean(args[4]);
  714. m_sceneManager.CurrentScene.SetSceneCoreDebug(scriptingOn, collisionsOn, physicsOn);
  715. MainConsole.Instance.Output(
  716. String.Format(
  717. "Set debug scene scripting = {0}, collisions = {1}, physics = {2}",
  718. !scriptingOn, !collisionsOn, !physicsOn));
  719. }
  720. }
  721. else
  722. {
  723. MainConsole.Instance.Output("debug scene <scripting> <collisions> <physics> (where inside <> is true/false)");
  724. }
  725. break;
  726. default:
  727. MainConsole.Instance.Output("Unknown debug");
  728. break;
  729. }
  730. }
  731. // see BaseOpenSimServer
  732. /// <summary>
  733. /// Many commands list objects for debugging. Some of the types are listed here
  734. /// </summary>
  735. /// <param name="mod"></param>
  736. /// <param name="cmd"></param>
  737. public override void HandleShow(string mod, string[] cmd)
  738. {
  739. base.HandleShow(mod, cmd);
  740. List<string> args = new List<string>(cmd);
  741. args.RemoveAt(0);
  742. string[] showParams = args.ToArray();
  743. switch (showParams[0])
  744. {
  745. case "assets":
  746. MainConsole.Instance.Output("Not implemented.");
  747. break;
  748. case "users":
  749. IList agents;
  750. if (showParams.Length > 1 && showParams[1] == "full")
  751. {
  752. agents = m_sceneManager.GetCurrentScenePresences();
  753. }
  754. else
  755. {
  756. agents = m_sceneManager.GetCurrentSceneAvatars();
  757. }
  758. MainConsole.Instance.Output(String.Format("\nAgents connected: {0}\n", agents.Count));
  759. MainConsole.Instance.Output(
  760. String.Format("{0,-16} {1,-16} {2,-37} {3,-11} {4,-16} {5,-30}", "Firstname", "Lastname",
  761. "Agent ID", "Root/Child", "Region", "Position"));
  762. foreach (ScenePresence presence in agents)
  763. {
  764. RegionInfo regionInfo = presence.Scene.RegionInfo;
  765. string regionName;
  766. if (regionInfo == null)
  767. {
  768. regionName = "Unresolvable";
  769. }
  770. else
  771. {
  772. regionName = regionInfo.RegionName;
  773. }
  774. MainConsole.Instance.Output(
  775. String.Format(
  776. "{0,-16} {1,-16} {2,-37} {3,-11} {4,-16} {5,-30}",
  777. presence.Firstname,
  778. presence.Lastname,
  779. presence.UUID,
  780. presence.IsChildAgent ? "Child" : "Root",
  781. regionName,
  782. presence.AbsolutePosition.ToString()));
  783. }
  784. MainConsole.Instance.Output(String.Empty);
  785. break;
  786. case "connections":
  787. System.Text.StringBuilder connections = new System.Text.StringBuilder("Connections:\n");
  788. m_sceneManager.ForEachScene(
  789. delegate(Scene scene)
  790. {
  791. scene.ForEachClient(
  792. delegate(IClientAPI client)
  793. {
  794. connections.AppendFormat("{0}: {1} ({2}) from {3} on circuit {4}\n",
  795. scene.RegionInfo.RegionName, client.Name, client.AgentId, client.RemoteEndPoint, client.CircuitCode);
  796. }
  797. );
  798. }
  799. );
  800. MainConsole.Instance.Output(connections.ToString());
  801. break;
  802. case "modules":
  803. MainConsole.Instance.Output("The currently loaded shared modules are:");
  804. foreach (IRegionModule module in m_moduleLoader.GetLoadedSharedModules)
  805. {
  806. MainConsole.Instance.Output("Shared Module: " + module.Name);
  807. }
  808. MainConsole.Instance.Output("");
  809. break;
  810. case "regions":
  811. m_sceneManager.ForEachScene(
  812. delegate(Scene scene)
  813. {
  814. MainConsole.Instance.Output(String.Format(
  815. "Region Name: {0}, Region XLoc: {1}, Region YLoc: {2}, Region Port: {3}",
  816. scene.RegionInfo.RegionName,
  817. scene.RegionInfo.RegionLocX,
  818. scene.RegionInfo.RegionLocY,
  819. scene.RegionInfo.InternalEndPoint.Port));
  820. });
  821. break;
  822. case "queues":
  823. Notice(GetQueuesReport());
  824. break;
  825. case "ratings":
  826. m_sceneManager.ForEachScene(
  827. delegate(Scene scene)
  828. {
  829. string rating = "";
  830. if (scene.RegionInfo.RegionSettings.Maturity == 1)
  831. {
  832. rating = "MATURE";
  833. }
  834. else if (scene.RegionInfo.RegionSettings.Maturity == 2)
  835. {
  836. rating = "ADULT";
  837. }
  838. else
  839. {
  840. rating = "PG";
  841. }
  842. MainConsole.Instance.Output(String.Format(
  843. "Region Name: {0}, Region Rating {1}",
  844. scene.RegionInfo.RegionName,
  845. rating));
  846. });
  847. break;
  848. }
  849. }
  850. /// <summary>
  851. /// print UDP Queue data for each client
  852. /// </summary>
  853. /// <returns></returns>
  854. private string GetQueuesReport()
  855. {
  856. string report = String.Empty;
  857. m_sceneManager.ForEachScene(delegate(Scene scene)
  858. {
  859. scene.ForEachClient(delegate(IClientAPI client)
  860. {
  861. if (client is IStatsCollector)
  862. {
  863. report = report + client.FirstName +
  864. " " + client.LastName;
  865. IStatsCollector stats =
  866. (IStatsCollector) client;
  867. report = report + string.Format("{0,7} {1,7} {2,7} {3,7} {4,7} {5,7} {6,7} {7,7} {8,7} {9,7}\n",
  868. "Send",
  869. "In",
  870. "Out",
  871. "Resend",
  872. "Land",
  873. "Wind",
  874. "Cloud",
  875. "Task",
  876. "Texture",
  877. "Asset");
  878. report = report + stats.Report() +
  879. "\n";
  880. }
  881. });
  882. });
  883. return report;
  884. }
  885. /// <summary>
  886. /// Use XML2 format to serialize data to a file
  887. /// </summary>
  888. /// <param name="module"></param>
  889. /// <param name="cmdparams"></param>
  890. protected void SavePrimsXml2(string module, string[] cmdparams)
  891. {
  892. if (cmdparams.Length > 5)
  893. {
  894. m_sceneManager.SaveNamedPrimsToXml2(cmdparams[3], cmdparams[4]);
  895. }
  896. else
  897. {
  898. m_sceneManager.SaveNamedPrimsToXml2("Primitive", DEFAULT_PRIM_BACKUP_FILENAME);
  899. }
  900. }
  901. /// <summary>
  902. /// Use XML format to serialize data to a file
  903. /// </summary>
  904. /// <param name="module"></param>
  905. /// <param name="cmdparams"></param>
  906. protected void SaveXml(string module, string[] cmdparams)
  907. {
  908. 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.");
  909. if (cmdparams.Length > 0)
  910. {
  911. m_sceneManager.SaveCurrentSceneToXml(cmdparams[2]);
  912. }
  913. else
  914. {
  915. m_sceneManager.SaveCurrentSceneToXml(DEFAULT_PRIM_BACKUP_FILENAME);
  916. }
  917. }
  918. /// <summary>
  919. /// Loads data and region objects from XML format.
  920. /// </summary>
  921. /// <param name="module"></param>
  922. /// <param name="cmdparams"></param>
  923. protected void LoadXml(string module, string[] cmdparams)
  924. {
  925. 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.");
  926. Vector3 loadOffset = new Vector3(0, 0, 0);
  927. if (cmdparams.Length > 2)
  928. {
  929. bool generateNewIDS = false;
  930. if (cmdparams.Length > 3)
  931. {
  932. if (cmdparams[3] == "-newUID")
  933. {
  934. generateNewIDS = true;
  935. }
  936. if (cmdparams.Length > 4)
  937. {
  938. loadOffset.X = (float)Convert.ToDecimal(cmdparams[4], Culture.NumberFormatInfo);
  939. if (cmdparams.Length > 5)
  940. {
  941. loadOffset.Y = (float)Convert.ToDecimal(cmdparams[5], Culture.NumberFormatInfo);
  942. }
  943. if (cmdparams.Length > 6)
  944. {
  945. loadOffset.Z = (float)Convert.ToDecimal(cmdparams[6], Culture.NumberFormatInfo);
  946. }
  947. MainConsole.Instance.Output(String.Format("loadOffsets <X,Y,Z> = <{0},{1},{2}>",loadOffset.X,loadOffset.Y,loadOffset.Z));
  948. }
  949. }
  950. m_sceneManager.LoadCurrentSceneFromXml(cmdparams[0], generateNewIDS, loadOffset);
  951. }
  952. else
  953. {
  954. try
  955. {
  956. m_sceneManager.LoadCurrentSceneFromXml(DEFAULT_PRIM_BACKUP_FILENAME, false, loadOffset);
  957. }
  958. catch (FileNotFoundException)
  959. {
  960. MainConsole.Instance.Output("Default xml not found. Usage: load-xml <filename>");
  961. }
  962. }
  963. }
  964. /// <summary>
  965. /// Serialize region data to XML2Format
  966. /// </summary>
  967. /// <param name="module"></param>
  968. /// <param name="cmdparams"></param>
  969. protected void SaveXml2(string module, string[] cmdparams)
  970. {
  971. if (cmdparams.Length > 2)
  972. {
  973. m_sceneManager.SaveCurrentSceneToXml2(cmdparams[2]);
  974. }
  975. else
  976. {
  977. m_sceneManager.SaveCurrentSceneToXml2(DEFAULT_PRIM_BACKUP_FILENAME);
  978. }
  979. }
  980. /// <summary>
  981. /// Load region data from Xml2Format
  982. /// </summary>
  983. /// <param name="module"></param>
  984. /// <param name="cmdparams"></param>
  985. protected void LoadXml2(string module, string[] cmdparams)
  986. {
  987. if (cmdparams.Length > 2)
  988. {
  989. try
  990. {
  991. m_sceneManager.LoadCurrentSceneFromXml2(cmdparams[2]);
  992. }
  993. catch (FileNotFoundException)
  994. {
  995. MainConsole.Instance.Output("Specified xml not found. Usage: load xml2 <filename>");
  996. }
  997. }
  998. else
  999. {
  1000. try
  1001. {
  1002. m_sceneManager.LoadCurrentSceneFromXml2(DEFAULT_PRIM_BACKUP_FILENAME);
  1003. }
  1004. catch (FileNotFoundException)
  1005. {
  1006. MainConsole.Instance.Output("Default xml not found. Usage: load xml2 <filename>");
  1007. }
  1008. }
  1009. }
  1010. /// <summary>
  1011. /// Load a whole region from an opensimulator archive.
  1012. /// </summary>
  1013. /// <param name="cmdparams"></param>
  1014. protected void LoadOar(string module, string[] cmdparams)
  1015. {
  1016. try
  1017. {
  1018. m_sceneManager.LoadArchiveToCurrentScene(cmdparams);
  1019. }
  1020. catch (Exception e)
  1021. {
  1022. MainConsole.Instance.Output(e.Message);
  1023. }
  1024. }
  1025. /// <summary>
  1026. /// Save a region to a file, including all the assets needed to restore it.
  1027. /// </summary>
  1028. /// <param name="cmdparams"></param>
  1029. protected void SaveOar(string module, string[] cmdparams)
  1030. {
  1031. m_sceneManager.SaveCurrentSceneToArchive(cmdparams);
  1032. }
  1033. private static string CombineParams(string[] commandParams, int pos)
  1034. {
  1035. string result = String.Empty;
  1036. for (int i = pos; i < commandParams.Length; i++)
  1037. {
  1038. result += commandParams[i] + " ";
  1039. }
  1040. result = result.TrimEnd(' ');
  1041. return result;
  1042. }
  1043. /// <summary>
  1044. /// Kill an object given its UUID.
  1045. /// </summary>
  1046. /// <param name="cmdparams"></param>
  1047. protected void KillUUID(string module, string[] cmdparams)
  1048. {
  1049. if (cmdparams.Length > 2)
  1050. {
  1051. UUID id = UUID.Zero;
  1052. SceneObjectGroup grp = null;
  1053. Scene sc = null;
  1054. if (!UUID.TryParse(cmdparams[2], out id))
  1055. {
  1056. MainConsole.Instance.Output("[KillUUID]: Error bad UUID format!");
  1057. return;
  1058. }
  1059. m_sceneManager.ForEachScene(
  1060. delegate(Scene scene)
  1061. {
  1062. SceneObjectPart part = scene.GetSceneObjectPart(id);
  1063. if (part == null)
  1064. return;
  1065. grp = part.ParentGroup;
  1066. sc = scene;
  1067. });
  1068. if (grp == null)
  1069. {
  1070. MainConsole.Instance.Output(String.Format("[KillUUID]: Given UUID {0} not found!", id));
  1071. }
  1072. else
  1073. {
  1074. MainConsole.Instance.Output(String.Format("[KillUUID]: Found UUID {0} in scene {1}", id, sc.RegionInfo.RegionName));
  1075. try
  1076. {
  1077. sc.DeleteSceneObject(grp, false);
  1078. }
  1079. catch (Exception e)
  1080. {
  1081. m_log.ErrorFormat("[KillUUID]: Error while removing objects from scene: " + e);
  1082. }
  1083. }
  1084. }
  1085. else
  1086. {
  1087. MainConsole.Instance.Output("[KillUUID]: Usage: kill uuid <UUID>");
  1088. }
  1089. }
  1090. #endregion
  1091. }
  1092. }