OpenSim.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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", Debug);
  192. m_console.Commands.AddCommand("region", false, "debug scene",
  193. "debug scene <cripting> <collisions> <physics>",
  194. "Turn on scene debugging", Debug);
  195. m_console.Commands.AddCommand("region", false, "change region",
  196. "change region <region name>",
  197. "Change current console region", ChangeSelectedRegion);
  198. m_console.Commands.AddCommand("region", false, "save xml",
  199. "save xml",
  200. "Save a region's data in XML format", SaveXml);
  201. m_console.Commands.AddCommand("region", false, "save xml2",
  202. "save xml2",
  203. "Save a region's data in XML2 format", SaveXml2);
  204. m_console.Commands.AddCommand("region", false, "load xml",
  205. "load xml [-newIDs [<x> <y> <z>]]",
  206. "Load a region's data from XML format", LoadXml);
  207. m_console.Commands.AddCommand("region", false, "load xml2",
  208. "load xml2",
  209. "Load a region's data from XML2 format", LoadXml2);
  210. m_console.Commands.AddCommand("region", false, "save prims xml2",
  211. "save prims xml2 [<prim name> <file name>]",
  212. "Save named prim to XML2", SavePrimsXml2);
  213. m_console.Commands.AddCommand("region", false, "load oar",
  214. "load oar [--merge] [--skip-assets] [<OAR path>]",
  215. "Load a region's data from an OAR archive.",
  216. "--merge will merge the OAR with the existing scene." + Environment.NewLine
  217. + "--skip-assets will load the OAR but ignore the assets it contains." + Environment.NewLine
  218. + "The path can be either a filesystem location or a URI."
  219. + " If this is not given then the command looks for an OAR named region.oar in the current directory.",
  220. LoadOar);
  221. m_console.Commands.AddCommand("region", false, "save oar",
  222. "save oar [<OAR path>]",
  223. "Save a region's data to an OAR archive.",
  224. "The OAR path must be a filesystem path."
  225. + " If this is not given then the oar is saved to region.oar in the current directory.",
  226. SaveOar);
  227. m_console.Commands.AddCommand("region", false, "edit scale",
  228. "edit scale <name> <x> <y> <z>",
  229. "Change the scale of a named prim", HandleEditScale);
  230. m_console.Commands.AddCommand("region", false, "kick user",
  231. "kick user <first> <last> [message]",
  232. "Kick a user off the simulator", KickUserCommand);
  233. m_console.Commands.AddCommand("region", false, "show assets",
  234. "show assets",
  235. "Show asset data", HandleShow);
  236. m_console.Commands.AddCommand("region", false, "show users",
  237. "show users [full]",
  238. "Show user data", HandleShow);
  239. m_console.Commands.AddCommand("region", false, "show connections",
  240. "show connections",
  241. "Show connection data", HandleShow);
  242. m_console.Commands.AddCommand("region", false, "show users full",
  243. "show users full",
  244. String.Empty, HandleShow);
  245. m_console.Commands.AddCommand("region", false, "show modules",
  246. "show modules",
  247. "Show module data", HandleShow);
  248. m_console.Commands.AddCommand("region", false, "show regions",
  249. "show regions",
  250. "Show region data", HandleShow);
  251. m_console.Commands.AddCommand("region", false, "show queues",
  252. "show queues",
  253. "Show queue data", HandleShow);
  254. m_console.Commands.AddCommand("region", false, "show ratings",
  255. "show ratings",
  256. "Show rating data", HandleShow);
  257. m_console.Commands.AddCommand("region", false, "backup",
  258. "backup",
  259. "Persist objects to the database now", RunCommand);
  260. m_console.Commands.AddCommand("region", false, "create region",
  261. "create region",
  262. "Create a new region", HandleCreateRegion);
  263. m_console.Commands.AddCommand("region", false, "restart",
  264. "restart",
  265. "Restart all sims in this instance", RunCommand);
  266. m_console.Commands.AddCommand("region", false, "config set",
  267. "config set <section> <field> <value>",
  268. "Set a config option", HandleConfig);
  269. m_console.Commands.AddCommand("region", false, "config get",
  270. "config get <section> <field>",
  271. "Read a config option", HandleConfig);
  272. m_console.Commands.AddCommand("region", false, "config save",
  273. "config save",
  274. "Save current configuration", HandleConfig);
  275. m_console.Commands.AddCommand("region", false, "command-script",
  276. "command-script <script>",
  277. "Run a command script from file", RunCommand);
  278. m_console.Commands.AddCommand("region", false, "remove-region",
  279. "remove-region <name>",
  280. "Remove a region from this simulator", RunCommand);
  281. m_console.Commands.AddCommand("region", false, "delete-region",
  282. "delete-region <name>",
  283. "Delete a region from disk", RunCommand);
  284. m_console.Commands.AddCommand("region", false, "modules list",
  285. "modules list",
  286. "List modules", HandleModules);
  287. m_console.Commands.AddCommand("region", false, "modules load",
  288. "modules load <name>",
  289. "Load a module", HandleModules);
  290. m_console.Commands.AddCommand("region", false, "modules unload",
  291. "modules unload <name>",
  292. "Unload a module", HandleModules);
  293. m_console.Commands.AddCommand("region", false, "Add-InventoryHost",
  294. "Add-InventoryHost <host>",
  295. String.Empty, RunCommand);
  296. m_console.Commands.AddCommand("region", false, "kill uuid",
  297. "kill uuid <UUID>",
  298. "Kill an object by UUID", KillUUID);
  299. }
  300. public override void ShutdownSpecific()
  301. {
  302. if (m_shutdownCommandsFile != String.Empty)
  303. {
  304. RunCommandScript(m_shutdownCommandsFile);
  305. }
  306. base.ShutdownSpecific();
  307. }
  308. /// <summary>
  309. /// Timer to run a specific text file as console commands. Configured in in the main ini file
  310. /// </summary>
  311. /// <param name="sender"></param>
  312. /// <param name="e"></param>
  313. private void RunAutoTimerScript(object sender, EventArgs e)
  314. {
  315. if (m_timedScript != "disabled")
  316. {
  317. RunCommandScript(m_timedScript);
  318. }
  319. }
  320. private void WatchdogTimeoutHandler(System.Threading.Thread thread, int lastTick)
  321. {
  322. int now = Environment.TickCount & Int32.MaxValue;
  323. m_log.ErrorFormat("[WATCHDOG]: Timeout detected for thread \"{0}\". ThreadState={1}. Last tick was {2}ms ago",
  324. thread.Name, thread.ThreadState, now - lastTick);
  325. }
  326. #region Console Commands
  327. /// <summary>
  328. /// Kicks users off the region
  329. /// </summary>
  330. /// <param name="module"></param>
  331. /// <param name="cmdparams">name of avatar to kick</param>
  332. private void KickUserCommand(string module, string[] cmdparams)
  333. {
  334. if (cmdparams.Length < 4)
  335. return;
  336. string alert = null;
  337. if (cmdparams.Length > 4)
  338. alert = String.Format("\n{0}\n", String.Join(" ", cmdparams, 4, cmdparams.Length - 4));
  339. IList agents = m_sceneManager.GetCurrentSceneAvatars();
  340. foreach (ScenePresence presence in agents)
  341. {
  342. RegionInfo regionInfo = presence.Scene.RegionInfo;
  343. if (presence.Firstname.ToLower().Contains(cmdparams[2].ToLower()) &&
  344. presence.Lastname.ToLower().Contains(cmdparams[3].ToLower()))
  345. {
  346. MainConsole.Instance.Output(
  347. String.Format(
  348. "Kicking user: {0,-16}{1,-16}{2,-37} in region: {3,-16}",
  349. presence.Firstname, presence.Lastname, presence.UUID, regionInfo.RegionName));
  350. // kick client...
  351. if (alert != null)
  352. presence.ControllingClient.Kick(alert);
  353. else
  354. presence.ControllingClient.Kick("\nThe OpenSim manager kicked you out.\n");
  355. // ...and close on our side
  356. presence.Scene.IncomingCloseAgent(presence.UUID);
  357. }
  358. }
  359. MainConsole.Instance.Output("");
  360. }
  361. /// <summary>
  362. /// Run an optional startup list of commands
  363. /// </summary>
  364. /// <param name="fileName"></param>
  365. private void RunCommandScript(string fileName)
  366. {
  367. if (File.Exists(fileName))
  368. {
  369. m_log.Info("[COMMANDFILE]: Running " + fileName);
  370. using (StreamReader readFile = File.OpenText(fileName))
  371. {
  372. string currentCommand;
  373. while ((currentCommand = readFile.ReadLine()) != null)
  374. {
  375. if (currentCommand != String.Empty)
  376. {
  377. m_log.Info("[COMMANDFILE]: Running '" + currentCommand + "'");
  378. m_console.RunCommand(currentCommand);
  379. }
  380. }
  381. }
  382. }
  383. }
  384. /// <summary>
  385. /// Opens a file and uses it as input to the console command parser.
  386. /// </summary>
  387. /// <param name="fileName">name of file to use as input to the console</param>
  388. private static void PrintFileToConsole(string fileName)
  389. {
  390. if (File.Exists(fileName))
  391. {
  392. StreamReader readFile = File.OpenText(fileName);
  393. string currentLine;
  394. while ((currentLine = readFile.ReadLine()) != null)
  395. {
  396. m_log.Info("[!]" + currentLine);
  397. }
  398. }
  399. }
  400. private void HandleClearAssets(string module, string[] args)
  401. {
  402. MainConsole.Instance.Output("Not implemented.");
  403. }
  404. /// <summary>
  405. /// Force resending of all updates to all clients in active region(s)
  406. /// </summary>
  407. /// <param name="module"></param>
  408. /// <param name="args"></param>
  409. private void HandleForceUpdate(string module, string[] args)
  410. {
  411. MainConsole.Instance.Output("Updating all clients");
  412. m_sceneManager.ForceCurrentSceneClientUpdate();
  413. }
  414. /// <summary>
  415. /// Edits the scale of a primative with the name specified
  416. /// </summary>
  417. /// <param name="module"></param>
  418. /// <param name="args">0,1, name, x, y, z</param>
  419. private void HandleEditScale(string module, string[] args)
  420. {
  421. if (args.Length == 6)
  422. {
  423. m_sceneManager.HandleEditCommandOnCurrentScene(args);
  424. }
  425. else
  426. {
  427. MainConsole.Instance.Output("Argument error: edit scale <prim name> <x> <y> <z>");
  428. }
  429. }
  430. /// <summary>
  431. /// Creates a new region based on the parameters specified. This will ask the user questions on the console
  432. /// </summary>
  433. /// <param name="module"></param>
  434. /// <param name="cmd">0,1,region name, region XML file</param>
  435. private void HandleCreateRegion(string module, string[] cmd)
  436. {
  437. if (cmd.Length < 4)
  438. {
  439. MainConsole.Instance.Output("Usage: create region <region name> <region_file.ini>");
  440. return;
  441. }
  442. if (cmd[3].EndsWith(".xml"))
  443. {
  444. string regionsDir = ConfigSource.Source.Configs["Startup"].GetString("regionload_regionsdir", "Regions").Trim();
  445. string regionFile = String.Format("{0}/{1}", regionsDir, cmd[3]);
  446. // Allow absolute and relative specifiers
  447. if (cmd[3].StartsWith("/") || cmd[3].StartsWith("\\") || cmd[3].StartsWith(".."))
  448. regionFile = cmd[3];
  449. IScene scene;
  450. CreateRegion(new RegionInfo(cmd[2], regionFile, false, ConfigSource.Source), true, out scene);
  451. }
  452. else if (cmd[3].EndsWith(".ini"))
  453. {
  454. string regionsDir = ConfigSource.Source.Configs["Startup"].GetString("regionload_regionsdir", "Regions").Trim();
  455. string regionFile = String.Format("{0}/{1}", regionsDir, cmd[3]);
  456. // Allow absolute and relative specifiers
  457. if (cmd[3].StartsWith("/") || cmd[3].StartsWith("\\") || cmd[3].StartsWith(".."))
  458. regionFile = cmd[3];
  459. IScene scene;
  460. CreateRegion(new RegionInfo(cmd[2], regionFile, false, ConfigSource.Source, cmd[2]), true, out scene);
  461. }
  462. else
  463. {
  464. MainConsole.Instance.Output("Usage: create region <region name> <region_file.ini>");
  465. return;
  466. }
  467. }
  468. /// <summary>
  469. /// Change and load configuration file data.
  470. /// </summary>
  471. /// <param name="module"></param>
  472. /// <param name="cmd"></param>
  473. private void HandleConfig(string module, string[] cmd)
  474. {
  475. List<string> args = new List<string>(cmd);
  476. args.RemoveAt(0);
  477. string[] cmdparams = args.ToArray();
  478. string n = "CONFIG";
  479. if (cmdparams.Length > 0)
  480. {
  481. switch (cmdparams[0].ToLower())
  482. {
  483. case "set":
  484. if (cmdparams.Length < 4)
  485. {
  486. MainConsole.Instance.Output(String.Format("SYNTAX: {0} SET SECTION KEY VALUE",n));
  487. MainConsole.Instance.Output(String.Format("EXAMPLE: {0} SET ScriptEngine.DotNetEngine NumberOfScriptThreads 5",n));
  488. }
  489. else
  490. {
  491. IConfig c;
  492. IConfigSource source = new IniConfigSource();
  493. c = source.AddConfig(cmdparams[1]);
  494. if (c != null)
  495. {
  496. string _value = String.Join(" ", cmdparams, 3, cmdparams.Length - 3);
  497. c.Set(cmdparams[2], _value);
  498. m_config.Source.Merge(source);
  499. MainConsole.Instance.Output(String.Format("{0} {0} {1} {2} {3}",n,cmdparams[1],cmdparams[2],_value));
  500. }
  501. }
  502. break;
  503. case "get":
  504. if (cmdparams.Length < 3)
  505. {
  506. MainConsole.Instance.Output(String.Format("SYNTAX: {0} GET SECTION KEY",n));
  507. MainConsole.Instance.Output(String.Format("EXAMPLE: {0} GET ScriptEngine.DotNetEngine NumberOfScriptThreads",n));
  508. }
  509. else
  510. {
  511. IConfig c = m_config.Source.Configs[cmdparams[1]];
  512. if (c == null)
  513. {
  514. MainConsole.Instance.Output(String.Format("Section \"{0}\" does not exist.",cmdparams[1]));
  515. break;
  516. }
  517. else
  518. {
  519. MainConsole.Instance.Output(String.Format("{0} GET {1} {2} : {3}",n,cmdparams[1],cmdparams[2],
  520. c.GetString(cmdparams[2])));
  521. }
  522. }
  523. break;
  524. case "save":
  525. if (cmdparams.Length < 2)
  526. {
  527. MainConsole.Instance.Output("SYNTAX: " + n + " SAVE FILE");
  528. return;
  529. }
  530. if (Application.iniFilePath == cmdparams[1])
  531. {
  532. MainConsole.Instance.Output("FILE can not be " + Application.iniFilePath);
  533. return;
  534. }
  535. MainConsole.Instance.Output("Saving configuration file: " + cmdparams[1]);
  536. m_config.Save(cmdparams[1]);
  537. break;
  538. }
  539. }
  540. }
  541. /// <summary>
  542. /// Load, Unload, and list Region modules in use
  543. /// </summary>
  544. /// <param name="module"></param>
  545. /// <param name="cmd"></param>
  546. private void HandleModules(string module, string[] cmd)
  547. {
  548. List<string> args = new List<string>(cmd);
  549. args.RemoveAt(0);
  550. string[] cmdparams = args.ToArray();
  551. if (cmdparams.Length > 0)
  552. {
  553. switch (cmdparams[0].ToLower())
  554. {
  555. case "list":
  556. foreach (IRegionModule irm in m_moduleLoader.GetLoadedSharedModules)
  557. {
  558. MainConsole.Instance.Output(String.Format("Shared region module: {0}", irm.Name));
  559. }
  560. break;
  561. case "unload":
  562. if (cmdparams.Length > 1)
  563. {
  564. foreach (IRegionModule rm in new ArrayList(m_moduleLoader.GetLoadedSharedModules))
  565. {
  566. if (rm.Name.ToLower() == cmdparams[1].ToLower())
  567. {
  568. MainConsole.Instance.Output(String.Format("Unloading module: {0}", rm.Name));
  569. m_moduleLoader.UnloadModule(rm);
  570. }
  571. }
  572. }
  573. break;
  574. case "load":
  575. if (cmdparams.Length > 1)
  576. {
  577. foreach (Scene s in new ArrayList(m_sceneManager.Scenes))
  578. {
  579. MainConsole.Instance.Output(String.Format("Loading module: {0}", cmdparams[1]));
  580. m_moduleLoader.LoadRegionModules(cmdparams[1], s);
  581. }
  582. }
  583. break;
  584. }
  585. }
  586. }
  587. /// <summary>
  588. /// Runs commands issued by the server console from the operator
  589. /// </summary>
  590. /// <param name="command">The first argument of the parameter (the command)</param>
  591. /// <param name="cmdparams">Additional arguments passed to the command</param>
  592. public void RunCommand(string module, string[] cmdparams)
  593. {
  594. List<string> args = new List<string>(cmdparams);
  595. if (args.Count < 1)
  596. return;
  597. string command = args[0];
  598. args.RemoveAt(0);
  599. cmdparams = args.ToArray();
  600. switch (command)
  601. {
  602. case "command-script":
  603. if (cmdparams.Length > 0)
  604. {
  605. RunCommandScript(cmdparams[0]);
  606. }
  607. break;
  608. case "backup":
  609. m_sceneManager.BackupCurrentScene();
  610. break;
  611. case "remove-region":
  612. string regRemoveName = CombineParams(cmdparams, 0);
  613. Scene removeScene;
  614. if (m_sceneManager.TryGetScene(regRemoveName, out removeScene))
  615. RemoveRegion(removeScene, false);
  616. else
  617. MainConsole.Instance.Output("no region with that name");
  618. break;
  619. case "delete-region":
  620. string regDeleteName = CombineParams(cmdparams, 0);
  621. Scene killScene;
  622. if (m_sceneManager.TryGetScene(regDeleteName, out killScene))
  623. RemoveRegion(killScene, true);
  624. else
  625. MainConsole.Instance.Output("no region with that name");
  626. break;
  627. case "restart":
  628. m_sceneManager.RestartCurrentScene();
  629. break;
  630. case "Add-InventoryHost":
  631. if (cmdparams.Length > 0)
  632. {
  633. MainConsole.Instance.Output("Not implemented.");
  634. }
  635. break;
  636. }
  637. }
  638. /// <summary>
  639. /// Change the currently selected region. The selected region is that operated upon by single region commands.
  640. /// </summary>
  641. /// <param name="cmdParams"></param>
  642. protected void ChangeSelectedRegion(string module, string[] cmdparams)
  643. {
  644. if (cmdparams.Length > 2)
  645. {
  646. string newRegionName = CombineParams(cmdparams, 2);
  647. if (!m_sceneManager.TrySetCurrentScene(newRegionName))
  648. MainConsole.Instance.Output(String.Format("Couldn't select region {0}", newRegionName));
  649. }
  650. else
  651. {
  652. MainConsole.Instance.Output("Usage: change region <region name>");
  653. }
  654. string regionName = (m_sceneManager.CurrentScene == null ? "root" : m_sceneManager.CurrentScene.RegionInfo.RegionName);
  655. MainConsole.Instance.Output(String.Format("Currently selected region is {0}", regionName));
  656. m_console.DefaultPrompt = String.Format("Region ({0}) ", regionName);
  657. m_console.ConsoleScene = m_sceneManager.CurrentScene;
  658. }
  659. /// <summary>
  660. /// Turn on some debugging values for OpenSim.
  661. /// </summary>
  662. /// <param name="args"></param>
  663. protected void Debug(string module, string[] args)
  664. {
  665. if (args.Length == 1)
  666. return;
  667. switch (args[1])
  668. {
  669. case "packet":
  670. if (args.Length > 2)
  671. {
  672. int newDebug;
  673. if (int.TryParse(args[2], out newDebug))
  674. {
  675. m_sceneManager.SetDebugPacketLevelOnCurrentScene(newDebug);
  676. }
  677. else
  678. {
  679. MainConsole.Instance.Output("packet debug should be 0..255");
  680. }
  681. MainConsole.Instance.Output(String.Format("New packet debug: {0}", newDebug));
  682. }
  683. break;
  684. case "scene":
  685. if (args.Length == 5)
  686. {
  687. if (m_sceneManager.CurrentScene == null)
  688. {
  689. MainConsole.Instance.Output("Please use 'change region <regioname>' first");
  690. }
  691. else
  692. {
  693. bool scriptingOn = !Convert.ToBoolean(args[2]);
  694. bool collisionsOn = !Convert.ToBoolean(args[3]);
  695. bool physicsOn = !Convert.ToBoolean(args[4]);
  696. m_sceneManager.CurrentScene.SetSceneCoreDebug(scriptingOn, collisionsOn, physicsOn);
  697. MainConsole.Instance.Output(
  698. String.Format(
  699. "Set debug scene scripting = {0}, collisions = {1}, physics = {2}",
  700. !scriptingOn, !collisionsOn, !physicsOn));
  701. }
  702. }
  703. else
  704. {
  705. MainConsole.Instance.Output("debug scene <scripting> <collisions> <physics> (where inside <> is true/false)");
  706. }
  707. break;
  708. default:
  709. MainConsole.Instance.Output("Unknown debug");
  710. break;
  711. }
  712. }
  713. // see BaseOpenSimServer
  714. /// <summary>
  715. /// Many commands list objects for debugging. Some of the types are listed here
  716. /// </summary>
  717. /// <param name="mod"></param>
  718. /// <param name="cmd"></param>
  719. public override void HandleShow(string mod, string[] cmd)
  720. {
  721. base.HandleShow(mod, cmd);
  722. List<string> args = new List<string>(cmd);
  723. args.RemoveAt(0);
  724. string[] showParams = args.ToArray();
  725. switch (showParams[0])
  726. {
  727. case "assets":
  728. MainConsole.Instance.Output("Not implemented.");
  729. break;
  730. case "users":
  731. IList agents;
  732. if (showParams.Length > 1 && showParams[1] == "full")
  733. {
  734. agents = m_sceneManager.GetCurrentScenePresences();
  735. }
  736. else
  737. {
  738. agents = m_sceneManager.GetCurrentSceneAvatars();
  739. }
  740. MainConsole.Instance.Output(String.Format("\nAgents connected: {0}\n", agents.Count));
  741. MainConsole.Instance.Output(
  742. String.Format("{0,-16}{1,-16}{2,-37}{3,-11}{4,-16}{5,-30}", "Firstname", "Lastname",
  743. "Agent ID", "Root/Child", "Region", "Position"));
  744. foreach (ScenePresence presence in agents)
  745. {
  746. RegionInfo regionInfo = presence.Scene.RegionInfo;
  747. string regionName;
  748. if (regionInfo == null)
  749. {
  750. regionName = "Unresolvable";
  751. }
  752. else
  753. {
  754. regionName = regionInfo.RegionName;
  755. }
  756. MainConsole.Instance.Output(
  757. String.Format(
  758. "{0,-16}{1,-16}{2,-37}{3,-11}{4,-16}{5,-30}",
  759. presence.Firstname,
  760. presence.Lastname,
  761. presence.UUID,
  762. presence.IsChildAgent ? "Child" : "Root",
  763. regionName,
  764. presence.AbsolutePosition.ToString()));
  765. }
  766. MainConsole.Instance.Output(String.Empty);
  767. break;
  768. case "connections":
  769. System.Text.StringBuilder connections = new System.Text.StringBuilder("Connections:\n");
  770. m_sceneManager.ForEachScene(
  771. delegate(Scene scene)
  772. {
  773. scene.ForEachClient(
  774. delegate(IClientAPI client)
  775. {
  776. connections.AppendFormat("{0}: {1} ({2}) from {3} on circuit {4}\n",
  777. scene.RegionInfo.RegionName, client.Name, client.AgentId, client.RemoteEndPoint, client.CircuitCode);
  778. }
  779. );
  780. }
  781. );
  782. MainConsole.Instance.Output(connections.ToString());
  783. break;
  784. case "modules":
  785. MainConsole.Instance.Output("The currently loaded shared modules are:");
  786. foreach (IRegionModule module in m_moduleLoader.GetLoadedSharedModules)
  787. {
  788. MainConsole.Instance.Output("Shared Module: " + module.Name);
  789. }
  790. MainConsole.Instance.Output("");
  791. break;
  792. case "regions":
  793. m_sceneManager.ForEachScene(
  794. delegate(Scene scene)
  795. {
  796. MainConsole.Instance.Output(String.Format(
  797. "Region Name: {0}, Region XLoc: {1}, Region YLoc: {2}, Region Port: {3}",
  798. scene.RegionInfo.RegionName,
  799. scene.RegionInfo.RegionLocX,
  800. scene.RegionInfo.RegionLocY,
  801. scene.RegionInfo.InternalEndPoint.Port));
  802. });
  803. break;
  804. case "queues":
  805. Notice(GetQueuesReport());
  806. break;
  807. case "ratings":
  808. m_sceneManager.ForEachScene(
  809. delegate(Scene scene)
  810. {
  811. string rating = "";
  812. if (scene.RegionInfo.RegionSettings.Maturity == 1)
  813. {
  814. rating = "MATURE";
  815. }
  816. else if (scene.RegionInfo.RegionSettings.Maturity == 2)
  817. {
  818. rating = "ADULT";
  819. }
  820. else
  821. {
  822. rating = "PG";
  823. }
  824. MainConsole.Instance.Output(String.Format(
  825. "Region Name: {0}, Region Rating {1}",
  826. scene.RegionInfo.RegionName,
  827. rating));
  828. });
  829. break;
  830. }
  831. }
  832. /// <summary>
  833. /// print UDP Queue data for each client
  834. /// </summary>
  835. /// <returns></returns>
  836. private string GetQueuesReport()
  837. {
  838. string report = String.Empty;
  839. m_sceneManager.ForEachScene(delegate(Scene scene)
  840. {
  841. scene.ForEachClient(delegate(IClientAPI client)
  842. {
  843. if (client is IStatsCollector)
  844. {
  845. report = report + client.FirstName +
  846. " " + client.LastName;
  847. IStatsCollector stats =
  848. (IStatsCollector) client;
  849. 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",
  850. "Send",
  851. "In",
  852. "Out",
  853. "Resend",
  854. "Land",
  855. "Wind",
  856. "Cloud",
  857. "Task",
  858. "Texture",
  859. "Asset");
  860. report = report + stats.Report() +
  861. "\n";
  862. }
  863. });
  864. });
  865. return report;
  866. }
  867. /// <summary>
  868. /// Use XML2 format to serialize data to a file
  869. /// </summary>
  870. /// <param name="module"></param>
  871. /// <param name="cmdparams"></param>
  872. protected void SavePrimsXml2(string module, string[] cmdparams)
  873. {
  874. if (cmdparams.Length > 5)
  875. {
  876. m_sceneManager.SaveNamedPrimsToXml2(cmdparams[3], cmdparams[4]);
  877. }
  878. else
  879. {
  880. m_sceneManager.SaveNamedPrimsToXml2("Primitive", DEFAULT_PRIM_BACKUP_FILENAME);
  881. }
  882. }
  883. /// <summary>
  884. /// Use XML format to serialize data to a file
  885. /// </summary>
  886. /// <param name="module"></param>
  887. /// <param name="cmdparams"></param>
  888. protected void SaveXml(string module, string[] cmdparams)
  889. {
  890. 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.");
  891. if (cmdparams.Length > 0)
  892. {
  893. m_sceneManager.SaveCurrentSceneToXml(cmdparams[2]);
  894. }
  895. else
  896. {
  897. m_sceneManager.SaveCurrentSceneToXml(DEFAULT_PRIM_BACKUP_FILENAME);
  898. }
  899. }
  900. /// <summary>
  901. /// Loads data and region objects from XML format.
  902. /// </summary>
  903. /// <param name="module"></param>
  904. /// <param name="cmdparams"></param>
  905. protected void LoadXml(string module, string[] cmdparams)
  906. {
  907. 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.");
  908. Vector3 loadOffset = new Vector3(0, 0, 0);
  909. if (cmdparams.Length > 2)
  910. {
  911. bool generateNewIDS = false;
  912. if (cmdparams.Length > 3)
  913. {
  914. if (cmdparams[3] == "-newUID")
  915. {
  916. generateNewIDS = true;
  917. }
  918. if (cmdparams.Length > 4)
  919. {
  920. loadOffset.X = (float)Convert.ToDecimal(cmdparams[4], Culture.NumberFormatInfo);
  921. if (cmdparams.Length > 5)
  922. {
  923. loadOffset.Y = (float)Convert.ToDecimal(cmdparams[5], Culture.NumberFormatInfo);
  924. }
  925. if (cmdparams.Length > 6)
  926. {
  927. loadOffset.Z = (float)Convert.ToDecimal(cmdparams[6], Culture.NumberFormatInfo);
  928. }
  929. MainConsole.Instance.Output(String.Format("loadOffsets <X,Y,Z> = <{0},{1},{2}>",loadOffset.X,loadOffset.Y,loadOffset.Z));
  930. }
  931. }
  932. m_sceneManager.LoadCurrentSceneFromXml(cmdparams[0], generateNewIDS, loadOffset);
  933. }
  934. else
  935. {
  936. try
  937. {
  938. m_sceneManager.LoadCurrentSceneFromXml(DEFAULT_PRIM_BACKUP_FILENAME, false, loadOffset);
  939. }
  940. catch (FileNotFoundException)
  941. {
  942. MainConsole.Instance.Output("Default xml not found. Usage: load-xml <filename>");
  943. }
  944. }
  945. }
  946. /// <summary>
  947. /// Serialize region data to XML2Format
  948. /// </summary>
  949. /// <param name="module"></param>
  950. /// <param name="cmdparams"></param>
  951. protected void SaveXml2(string module, string[] cmdparams)
  952. {
  953. if (cmdparams.Length > 2)
  954. {
  955. m_sceneManager.SaveCurrentSceneToXml2(cmdparams[2]);
  956. }
  957. else
  958. {
  959. m_sceneManager.SaveCurrentSceneToXml2(DEFAULT_PRIM_BACKUP_FILENAME);
  960. }
  961. }
  962. /// <summary>
  963. /// Load region data from Xml2Format
  964. /// </summary>
  965. /// <param name="module"></param>
  966. /// <param name="cmdparams"></param>
  967. protected void LoadXml2(string module, string[] cmdparams)
  968. {
  969. if (cmdparams.Length > 2)
  970. {
  971. try
  972. {
  973. m_sceneManager.LoadCurrentSceneFromXml2(cmdparams[2]);
  974. }
  975. catch (FileNotFoundException)
  976. {
  977. MainConsole.Instance.Output("Specified xml not found. Usage: load xml2 <filename>");
  978. }
  979. }
  980. else
  981. {
  982. try
  983. {
  984. m_sceneManager.LoadCurrentSceneFromXml2(DEFAULT_PRIM_BACKUP_FILENAME);
  985. }
  986. catch (FileNotFoundException)
  987. {
  988. MainConsole.Instance.Output("Default xml not found. Usage: load xml2 <filename>");
  989. }
  990. }
  991. }
  992. /// <summary>
  993. /// Load a whole region from an opensimulator archive.
  994. /// </summary>
  995. /// <param name="cmdparams"></param>
  996. protected void LoadOar(string module, string[] cmdparams)
  997. {
  998. try
  999. {
  1000. m_sceneManager.LoadArchiveToCurrentScene(cmdparams);
  1001. }
  1002. catch (Exception e)
  1003. {
  1004. MainConsole.Instance.Output(e.Message);
  1005. }
  1006. }
  1007. /// <summary>
  1008. /// Save a region to a file, including all the assets needed to restore it.
  1009. /// </summary>
  1010. /// <param name="cmdparams"></param>
  1011. protected void SaveOar(string module, string[] cmdparams)
  1012. {
  1013. m_sceneManager.SaveCurrentSceneToArchive(cmdparams);
  1014. }
  1015. private static string CombineParams(string[] commandParams, int pos)
  1016. {
  1017. string result = String.Empty;
  1018. for (int i = pos; i < commandParams.Length; i++)
  1019. {
  1020. result += commandParams[i] + " ";
  1021. }
  1022. result = result.TrimEnd(' ');
  1023. return result;
  1024. }
  1025. /// <summary>
  1026. /// Kill an object given its UUID.
  1027. /// </summary>
  1028. /// <param name="cmdparams"></param>
  1029. protected void KillUUID(string module, string[] cmdparams)
  1030. {
  1031. if (cmdparams.Length > 2)
  1032. {
  1033. UUID id = UUID.Zero;
  1034. SceneObjectGroup grp = null;
  1035. Scene sc = null;
  1036. if (!UUID.TryParse(cmdparams[2], out id))
  1037. {
  1038. MainConsole.Instance.Output("[KillUUID]: Error bad UUID format!");
  1039. return;
  1040. }
  1041. m_sceneManager.ForEachScene(
  1042. delegate(Scene scene)
  1043. {
  1044. SceneObjectPart part = scene.GetSceneObjectPart(id);
  1045. if (part == null)
  1046. return;
  1047. grp = part.ParentGroup;
  1048. sc = scene;
  1049. });
  1050. if (grp == null)
  1051. {
  1052. MainConsole.Instance.Output(String.Format("[KillUUID]: Given UUID {0} not found!", id));
  1053. }
  1054. else
  1055. {
  1056. MainConsole.Instance.Output(String.Format("[KillUUID]: Found UUID {0} in scene {1}", id, sc.RegionInfo.RegionName));
  1057. try
  1058. {
  1059. sc.DeleteSceneObject(grp, false);
  1060. }
  1061. catch (Exception e)
  1062. {
  1063. m_log.ErrorFormat("[KillUUID]: Error while removing objects from scene: " + e);
  1064. }
  1065. }
  1066. }
  1067. else
  1068. {
  1069. MainConsole.Instance.Output("[KillUUID]: Usage: kill uuid <UUID>");
  1070. }
  1071. }
  1072. #endregion
  1073. }
  1074. }