GridService.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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.Generic;
  29. using System.Net;
  30. using System.Reflection;
  31. using Nini.Config;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Data;
  36. using OpenSim.Server.Base;
  37. using OpenSim.Services.Interfaces;
  38. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  39. using OpenMetaverse;
  40. namespace OpenSim.Services.GridService
  41. {
  42. public class GridService : GridServiceBase, IGridService
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private bool m_DeleteOnUnregister = true;
  48. private static GridService m_RootInstance = null;
  49. protected IConfigSource m_config;
  50. protected static HypergridLinker m_HypergridLinker;
  51. protected IAuthenticationService m_AuthenticationService = null;
  52. protected bool m_AllowDuplicateNames = false;
  53. protected bool m_AllowHypergridMapSearch = false;
  54. public GridService(IConfigSource config)
  55. : base(config)
  56. {
  57. m_log.DebugFormat("[GRID SERVICE]: Starting...");
  58. m_config = config;
  59. IConfig gridConfig = config.Configs["GridService"];
  60. if (gridConfig != null)
  61. {
  62. m_DeleteOnUnregister = gridConfig.GetBoolean("DeleteOnUnregister", true);
  63. string authService = gridConfig.GetString("AuthenticationService", String.Empty);
  64. if (authService != String.Empty)
  65. {
  66. Object[] args = new Object[] { config };
  67. m_AuthenticationService = ServerUtils.LoadPlugin<IAuthenticationService>(authService, args);
  68. }
  69. m_AllowDuplicateNames = gridConfig.GetBoolean("AllowDuplicateNames", m_AllowDuplicateNames);
  70. m_AllowHypergridMapSearch = gridConfig.GetBoolean("AllowHypergridMapSearch", m_AllowHypergridMapSearch);
  71. }
  72. if (m_RootInstance == null)
  73. {
  74. m_RootInstance = this;
  75. if (MainConsole.Instance != null)
  76. {
  77. MainConsole.Instance.Commands.AddCommand("Regions", true,
  78. "deregister region id",
  79. "deregister region id <Region UUID>",
  80. "Deregister a region manually.",
  81. String.Empty,
  82. HandleDeregisterRegion);
  83. // A messy way of stopping this command being added if we are in standalone (since the simulator
  84. // has an identically named command
  85. //
  86. // XXX: We're relying on the OpenSimulator version being registered first, which is not well defined.
  87. if (MainConsole.Instance.Commands.Resolve(new string[] { "show", "regions" }).Length == 0)
  88. MainConsole.Instance.Commands.AddCommand("Regions", true,
  89. "show regions",
  90. "show regions",
  91. "Show details on all regions",
  92. String.Empty,
  93. HandleShowRegions);
  94. MainConsole.Instance.Commands.AddCommand("Regions", true,
  95. "show region name",
  96. "show region name <Region name>",
  97. "Show details on a region",
  98. String.Empty,
  99. HandleShowRegion);
  100. MainConsole.Instance.Commands.AddCommand("Regions", true,
  101. "show region at",
  102. "show region at <x-coord> <y-coord>",
  103. "Show details on a region at the given co-ordinate.",
  104. "For example, show region at 1000 1000",
  105. HandleShowRegionAt);
  106. MainConsole.Instance.Commands.AddCommand("Regions", true,
  107. "set region flags",
  108. "set region flags <Region name> <flags>",
  109. "Set database flags for region",
  110. String.Empty,
  111. HandleSetFlags);
  112. }
  113. m_HypergridLinker = new HypergridLinker(m_config, this, m_Database);
  114. }
  115. }
  116. #region IGridService
  117. public string RegisterRegion(UUID scopeID, GridRegion regionInfos)
  118. {
  119. IConfig gridConfig = m_config.Configs["GridService"];
  120. if (regionInfos.RegionID == UUID.Zero)
  121. return "Invalid RegionID - cannot be zero UUID";
  122. RegionData region = m_Database.Get(regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID);
  123. if ((region != null) && (region.RegionID != regionInfos.RegionID))
  124. {
  125. m_log.WarnFormat("[GRID SERVICE]: Region {0} tried to register in coordinates {1}, {2} which are already in use in scope {3}.",
  126. regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID);
  127. return "Region overlaps another region";
  128. }
  129. if (region != null)
  130. {
  131. // There is a preexisting record
  132. //
  133. // Get it's flags
  134. //
  135. OpenSim.Framework.RegionFlags rflags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(region.Data["flags"]);
  136. // Is this a reservation?
  137. //
  138. if ((rflags & OpenSim.Framework.RegionFlags.Reservation) != 0)
  139. {
  140. // Regions reserved for the null key cannot be taken.
  141. if ((string)region.Data["PrincipalID"] == UUID.Zero.ToString())
  142. return "Region location is reserved";
  143. // Treat it as an auth request
  144. //
  145. // NOTE: Fudging the flags value here, so these flags
  146. // should not be used elsewhere. Don't optimize
  147. // this with the later retrieval of the same flags!
  148. rflags |= OpenSim.Framework.RegionFlags.Authenticate;
  149. }
  150. if ((rflags & OpenSim.Framework.RegionFlags.Authenticate) != 0)
  151. {
  152. // Can we authenticate at all?
  153. //
  154. if (m_AuthenticationService == null)
  155. return "No authentication possible";
  156. if (!m_AuthenticationService.Verify(new UUID(region.Data["PrincipalID"].ToString()), regionInfos.Token, 30))
  157. return "Bad authentication";
  158. }
  159. }
  160. // If we get here, the destination is clear. Now for the real check.
  161. if (!m_AllowDuplicateNames)
  162. {
  163. List<RegionData> dupe = m_Database.Get(regionInfos.RegionName, scopeID);
  164. if (dupe != null && dupe.Count > 0)
  165. {
  166. foreach (RegionData d in dupe)
  167. {
  168. if (d.RegionID != regionInfos.RegionID)
  169. {
  170. m_log.WarnFormat("[GRID SERVICE]: Region {0} tried to register duplicate name with ID {1}.",
  171. regionInfos.RegionName, regionInfos.RegionID);
  172. return "Duplicate region name";
  173. }
  174. }
  175. }
  176. }
  177. // If there is an old record for us, delete it if it is elsewhere.
  178. region = m_Database.Get(regionInfos.RegionID, scopeID);
  179. if ((region != null) && (region.RegionID == regionInfos.RegionID) &&
  180. ((region.posX != regionInfos.RegionLocX) || (region.posY != regionInfos.RegionLocY)))
  181. {
  182. if ((Convert.ToInt32(region.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.NoMove) != 0)
  183. return "Can't move this region";
  184. if ((Convert.ToInt32(region.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.LockedOut) != 0)
  185. return "Region locked out";
  186. // Region reregistering in other coordinates. Delete the old entry
  187. m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) was previously registered at {2}-{3}. Deleting old entry.",
  188. regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY);
  189. try
  190. {
  191. m_Database.Delete(regionInfos.RegionID);
  192. }
  193. catch (Exception e)
  194. {
  195. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  196. }
  197. }
  198. // Everything is ok, let's register
  199. RegionData rdata = RegionInfo2RegionData(regionInfos);
  200. rdata.ScopeID = scopeID;
  201. if (region != null)
  202. {
  203. int oldFlags = Convert.ToInt32(region.Data["flags"]);
  204. oldFlags &= ~(int)OpenSim.Framework.RegionFlags.Reservation;
  205. rdata.Data["flags"] = oldFlags.ToString(); // Preserve flags
  206. }
  207. else
  208. {
  209. rdata.Data["flags"] = "0";
  210. if ((gridConfig != null) && rdata.RegionName != string.Empty)
  211. {
  212. int newFlags = 0;
  213. string regionName = rdata.RegionName.Trim().Replace(' ', '_');
  214. newFlags = ParseFlags(newFlags, gridConfig.GetString("DefaultRegionFlags", String.Empty));
  215. newFlags = ParseFlags(newFlags, gridConfig.GetString("Region_" + regionName, String.Empty));
  216. newFlags = ParseFlags(newFlags, gridConfig.GetString("Region_" + rdata.RegionID.ToString(), String.Empty));
  217. rdata.Data["flags"] = newFlags.ToString();
  218. }
  219. }
  220. int flags = Convert.ToInt32(rdata.Data["flags"]);
  221. flags |= (int)OpenSim.Framework.RegionFlags.RegionOnline;
  222. rdata.Data["flags"] = flags.ToString();
  223. try
  224. {
  225. rdata.Data["last_seen"] = Util.UnixTimeSinceEpoch();
  226. m_Database.Store(rdata);
  227. }
  228. catch (Exception e)
  229. {
  230. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  231. }
  232. m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) registered successfully at {2}-{3}",
  233. regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionCoordX, regionInfos.RegionCoordY);
  234. return String.Empty;
  235. }
  236. public bool DeregisterRegion(UUID regionID)
  237. {
  238. RegionData region = m_Database.Get(regionID, UUID.Zero);
  239. if (region == null)
  240. return false;
  241. m_log.DebugFormat(
  242. "[GRID SERVICE]: Deregistering region {0} ({1}) at {2}-{3}",
  243. region.RegionName, region.RegionID, region.coordX, region.coordY);
  244. int flags = Convert.ToInt32(region.Data["flags"]);
  245. if (!m_DeleteOnUnregister || (flags & (int)OpenSim.Framework.RegionFlags.Persistent) != 0)
  246. {
  247. flags &= ~(int)OpenSim.Framework.RegionFlags.RegionOnline;
  248. region.Data["flags"] = flags.ToString();
  249. region.Data["last_seen"] = Util.UnixTimeSinceEpoch();
  250. try
  251. {
  252. m_Database.Store(region);
  253. }
  254. catch (Exception e)
  255. {
  256. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  257. }
  258. return true;
  259. }
  260. return m_Database.Delete(regionID);
  261. }
  262. public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  263. {
  264. List<GridRegion> rinfos = new List<GridRegion>();
  265. RegionData region = m_Database.Get(regionID, scopeID);
  266. if (region != null)
  267. {
  268. // Not really? Maybe?
  269. List<RegionData> rdatas = m_Database.Get(region.posX - (int)Constants.RegionSize - 1, region.posY - (int)Constants.RegionSize - 1,
  270. region.posX + (int)Constants.RegionSize + 1, region.posY + (int)Constants.RegionSize + 1, scopeID);
  271. foreach (RegionData rdata in rdatas)
  272. {
  273. if (rdata.RegionID != regionID)
  274. {
  275. int flags = Convert.ToInt32(rdata.Data["flags"]);
  276. if ((flags & (int)Framework.RegionFlags.Hyperlink) == 0) // no hyperlinks as neighbours
  277. rinfos.Add(RegionData2RegionInfo(rdata));
  278. }
  279. }
  280. // m_log.DebugFormat("[GRID SERVICE]: region {0} has {1} neighbours", region.RegionName, rinfos.Count);
  281. }
  282. else
  283. {
  284. m_log.WarnFormat(
  285. "[GRID SERVICE]: GetNeighbours() called for scope {0}, region {1} but no such region found",
  286. scopeID, regionID);
  287. }
  288. return rinfos;
  289. }
  290. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  291. {
  292. RegionData rdata = m_Database.Get(regionID, scopeID);
  293. if (rdata != null)
  294. return RegionData2RegionInfo(rdata);
  295. return null;
  296. }
  297. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  298. {
  299. int snapX = (int)(x / Constants.RegionSize) * (int)Constants.RegionSize;
  300. int snapY = (int)(y / Constants.RegionSize) * (int)Constants.RegionSize;
  301. RegionData rdata = m_Database.Get(snapX, snapY, scopeID);
  302. if (rdata != null)
  303. return RegionData2RegionInfo(rdata);
  304. return null;
  305. }
  306. public GridRegion GetRegionByName(UUID scopeID, string name)
  307. {
  308. List<RegionData> rdatas = m_Database.Get(name, scopeID);
  309. if ((rdatas != null) && (rdatas.Count > 0))
  310. return RegionData2RegionInfo(rdatas[0]); // get the first
  311. if (m_AllowHypergridMapSearch)
  312. {
  313. GridRegion r = GetHypergridRegionByName(scopeID, name);
  314. if (r != null)
  315. return r;
  316. }
  317. return null;
  318. }
  319. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  320. {
  321. // m_log.DebugFormat("[GRID SERVICE]: GetRegionsByName {0}", name);
  322. List<RegionData> rdatas = m_Database.Get(name + "%", scopeID);
  323. int count = 0;
  324. List<GridRegion> rinfos = new List<GridRegion>();
  325. if (rdatas != null)
  326. {
  327. // m_log.DebugFormat("[GRID SERVICE]: Found {0} regions", rdatas.Count);
  328. foreach (RegionData rdata in rdatas)
  329. {
  330. if (count++ < maxNumber)
  331. rinfos.Add(RegionData2RegionInfo(rdata));
  332. }
  333. }
  334. if (m_AllowHypergridMapSearch && (rdatas == null || (rdatas != null && rdatas.Count == 0)))
  335. {
  336. GridRegion r = GetHypergridRegionByName(scopeID, name);
  337. if (r != null)
  338. rinfos.Add(r);
  339. }
  340. return rinfos;
  341. }
  342. /// <summary>
  343. /// Get a hypergrid region.
  344. /// </summary>
  345. /// <param name="scopeID"></param>
  346. /// <param name="name"></param>
  347. /// <returns>null if no hypergrid region could be found.</returns>
  348. protected GridRegion GetHypergridRegionByName(UUID scopeID, string name)
  349. {
  350. if (name.Contains("."))
  351. return m_HypergridLinker.LinkRegion(scopeID, name);
  352. else
  353. return null;
  354. }
  355. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  356. {
  357. int xminSnap = (int)(xmin / Constants.RegionSize) * (int)Constants.RegionSize;
  358. int xmaxSnap = (int)(xmax / Constants.RegionSize) * (int)Constants.RegionSize;
  359. int yminSnap = (int)(ymin / Constants.RegionSize) * (int)Constants.RegionSize;
  360. int ymaxSnap = (int)(ymax / Constants.RegionSize) * (int)Constants.RegionSize;
  361. List<RegionData> rdatas = m_Database.Get(xminSnap, yminSnap, xmaxSnap, ymaxSnap, scopeID);
  362. List<GridRegion> rinfos = new List<GridRegion>();
  363. foreach (RegionData rdata in rdatas)
  364. rinfos.Add(RegionData2RegionInfo(rdata));
  365. return rinfos;
  366. }
  367. #endregion
  368. #region Data structure conversions
  369. public RegionData RegionInfo2RegionData(GridRegion rinfo)
  370. {
  371. RegionData rdata = new RegionData();
  372. rdata.posX = (int)rinfo.RegionLocX;
  373. rdata.posY = (int)rinfo.RegionLocY;
  374. rdata.RegionID = rinfo.RegionID;
  375. rdata.RegionName = rinfo.RegionName;
  376. rdata.Data = rinfo.ToKeyValuePairs();
  377. rdata.Data["regionHandle"] = Utils.UIntsToLong((uint)rdata.posX, (uint)rdata.posY);
  378. rdata.Data["owner_uuid"] = rinfo.EstateOwner.ToString();
  379. return rdata;
  380. }
  381. public GridRegion RegionData2RegionInfo(RegionData rdata)
  382. {
  383. GridRegion rinfo = new GridRegion(rdata.Data);
  384. rinfo.RegionLocX = rdata.posX;
  385. rinfo.RegionLocY = rdata.posY;
  386. rinfo.RegionID = rdata.RegionID;
  387. rinfo.RegionName = rdata.RegionName;
  388. rinfo.ScopeID = rdata.ScopeID;
  389. return rinfo;
  390. }
  391. #endregion
  392. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  393. {
  394. List<GridRegion> ret = new List<GridRegion>();
  395. List<RegionData> regions = m_Database.GetDefaultRegions(scopeID);
  396. foreach (RegionData r in regions)
  397. {
  398. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  399. ret.Add(RegionData2RegionInfo(r));
  400. }
  401. m_log.DebugFormat("[GRID SERVICE]: GetDefaultRegions returning {0} regions", ret.Count);
  402. return ret;
  403. }
  404. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  405. {
  406. List<GridRegion> ret = new List<GridRegion>();
  407. List<RegionData> regions = m_Database.GetFallbackRegions(scopeID, x, y);
  408. foreach (RegionData r in regions)
  409. {
  410. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  411. ret.Add(RegionData2RegionInfo(r));
  412. }
  413. m_log.DebugFormat("[GRID SERVICE]: Fallback returned {0} regions", ret.Count);
  414. return ret;
  415. }
  416. public List<GridRegion> GetHyperlinks(UUID scopeID)
  417. {
  418. List<GridRegion> ret = new List<GridRegion>();
  419. List<RegionData> regions = m_Database.GetHyperlinks(scopeID);
  420. foreach (RegionData r in regions)
  421. {
  422. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  423. ret.Add(RegionData2RegionInfo(r));
  424. }
  425. m_log.DebugFormat("[GRID SERVICE]: Hyperlinks returned {0} regions", ret.Count);
  426. return ret;
  427. }
  428. public int GetRegionFlags(UUID scopeID, UUID regionID)
  429. {
  430. RegionData region = m_Database.Get(regionID, scopeID);
  431. if (region != null)
  432. {
  433. int flags = Convert.ToInt32(region.Data["flags"]);
  434. //m_log.DebugFormat("[GRID SERVICE]: Request for flags of {0}: {1}", regionID, flags);
  435. return flags;
  436. }
  437. else
  438. return -1;
  439. }
  440. private void HandleDeregisterRegion(string module, string[] cmd)
  441. {
  442. if (cmd.Length != 4)
  443. {
  444. MainConsole.Instance.Output("Syntax: degregister region id <Region UUID>");
  445. return;
  446. }
  447. string rawRegionUuid = cmd[3];
  448. UUID regionUuid;
  449. if (!UUID.TryParse(rawRegionUuid, out regionUuid))
  450. {
  451. MainConsole.Instance.OutputFormat("{0} is not a valid region uuid", rawRegionUuid);
  452. return;
  453. }
  454. GridRegion region = GetRegionByUUID(UUID.Zero, regionUuid);
  455. if (region == null)
  456. {
  457. MainConsole.Instance.OutputFormat("No region with UUID {0}", regionUuid);
  458. return;
  459. }
  460. if (DeregisterRegion(regionUuid))
  461. {
  462. MainConsole.Instance.OutputFormat("Deregistered {0} {1}", region.RegionName, regionUuid);
  463. }
  464. else
  465. {
  466. // I don't think this can ever occur if we know that the region exists.
  467. MainConsole.Instance.OutputFormat("Error deregistering {0} {1}", region.RegionName, regionUuid);
  468. }
  469. return;
  470. }
  471. private void HandleShowRegions(string module, string[] cmd)
  472. {
  473. if (cmd.Length != 2)
  474. {
  475. MainConsole.Instance.Output("Syntax: show regions");
  476. return;
  477. }
  478. List<RegionData> regions = m_Database.Get(int.MinValue, int.MinValue, int.MaxValue, int.MaxValue, UUID.Zero);
  479. OutputRegionsToConsoleSummary(regions);
  480. }
  481. private void HandleShowRegion(string module, string[] cmd)
  482. {
  483. if (cmd.Length != 4)
  484. {
  485. MainConsole.Instance.Output("Syntax: show region name <region name>");
  486. return;
  487. }
  488. string regionName = cmd[3];
  489. List<RegionData> regions = m_Database.Get(regionName, UUID.Zero);
  490. if (regions == null || regions.Count < 1)
  491. {
  492. MainConsole.Instance.Output("No region with name {0} found", regionName);
  493. return;
  494. }
  495. OutputRegionsToConsole(regions);
  496. }
  497. private void HandleShowRegionAt(string module, string[] cmd)
  498. {
  499. if (cmd.Length != 5)
  500. {
  501. MainConsole.Instance.Output("Syntax: show region at <x-coord> <y-coord>");
  502. return;
  503. }
  504. int x, y;
  505. if (!int.TryParse(cmd[3], out x))
  506. {
  507. MainConsole.Instance.Output("x-coord must be an integer");
  508. return;
  509. }
  510. if (!int.TryParse(cmd[4], out y))
  511. {
  512. MainConsole.Instance.Output("y-coord must be an integer");
  513. return;
  514. }
  515. RegionData region = m_Database.Get(x * (int)Constants.RegionSize, y * (int)Constants.RegionSize, UUID.Zero);
  516. if (region == null)
  517. {
  518. MainConsole.Instance.OutputFormat("No region found at {0},{1}", x, y);
  519. return;
  520. }
  521. OutputRegionToConsole(region);
  522. }
  523. private void OutputRegionToConsole(RegionData r)
  524. {
  525. OpenSim.Framework.RegionFlags flags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(r.Data["flags"]);
  526. ConsoleDisplayList dispList = new ConsoleDisplayList();
  527. dispList.AddRow("Region Name", r.RegionName);
  528. dispList.AddRow("Region ID", r.RegionID);
  529. dispList.AddRow("Location", string.Format("{0},{1}", r.coordX, r.coordY));
  530. dispList.AddRow("URI", r.Data["serverURI"]);
  531. dispList.AddRow("Owner ID", r.Data["owner_uuid"]);
  532. dispList.AddRow("Flags", flags);
  533. MainConsole.Instance.Output(dispList.ToString());
  534. }
  535. private void OutputRegionsToConsole(List<RegionData> regions)
  536. {
  537. foreach (RegionData r in regions)
  538. OutputRegionToConsole(r);
  539. }
  540. private void OutputRegionsToConsoleSummary(List<RegionData> regions)
  541. {
  542. ConsoleDisplayTable dispTable = new ConsoleDisplayTable();
  543. dispTable.AddColumn("Name", 16);
  544. dispTable.AddColumn("ID", 36);
  545. dispTable.AddColumn("Position", 11);
  546. dispTable.AddColumn("Owner ID", 36);
  547. dispTable.AddColumn("Flags", 60);
  548. foreach (RegionData r in regions)
  549. {
  550. OpenSim.Framework.RegionFlags flags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(r.Data["flags"]);
  551. dispTable.AddRow(
  552. r.RegionName,
  553. r.RegionID.ToString(),
  554. string.Format("{0},{1}", r.coordX, r.coordY),
  555. r.Data["owner_uuid"].ToString(),
  556. flags.ToString());
  557. }
  558. MainConsole.Instance.Output(dispTable.ToString());
  559. }
  560. private int ParseFlags(int prev, string flags)
  561. {
  562. OpenSim.Framework.RegionFlags f = (OpenSim.Framework.RegionFlags)prev;
  563. string[] parts = flags.Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
  564. foreach (string p in parts)
  565. {
  566. int val;
  567. try
  568. {
  569. if (p.StartsWith("+"))
  570. {
  571. val = (int)Enum.Parse(typeof(OpenSim.Framework.RegionFlags), p.Substring(1));
  572. f |= (OpenSim.Framework.RegionFlags)val;
  573. }
  574. else if (p.StartsWith("-"))
  575. {
  576. val = (int)Enum.Parse(typeof(OpenSim.Framework.RegionFlags), p.Substring(1));
  577. f &= ~(OpenSim.Framework.RegionFlags)val;
  578. }
  579. else
  580. {
  581. val = (int)Enum.Parse(typeof(OpenSim.Framework.RegionFlags), p);
  582. f |= (OpenSim.Framework.RegionFlags)val;
  583. }
  584. }
  585. catch (Exception)
  586. {
  587. MainConsole.Instance.Output("Error in flag specification: " + p);
  588. }
  589. }
  590. return (int)f;
  591. }
  592. private void HandleSetFlags(string module, string[] cmd)
  593. {
  594. if (cmd.Length < 5)
  595. {
  596. MainConsole.Instance.Output("Syntax: set region flags <region name> <flags>");
  597. return;
  598. }
  599. List<RegionData> regions = m_Database.Get(cmd[3], UUID.Zero);
  600. if (regions == null || regions.Count < 1)
  601. {
  602. MainConsole.Instance.Output("Region not found");
  603. return;
  604. }
  605. foreach (RegionData r in regions)
  606. {
  607. int flags = Convert.ToInt32(r.Data["flags"]);
  608. flags = ParseFlags(flags, cmd[4]);
  609. r.Data["flags"] = flags.ToString();
  610. OpenSim.Framework.RegionFlags f = (OpenSim.Framework.RegionFlags)flags;
  611. MainConsole.Instance.Output(String.Format("Set region {0} to {1}", r.RegionName, f));
  612. m_Database.Store(r);
  613. }
  614. }
  615. }
  616. }