GridService.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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-id>+",
  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(Util.EscapeForLike(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 tried to register using a duplicate name. New region: {0} ({1}), existing region: {2} ({3}).",
  171. regionInfos.RegionName, regionInfos.RegionID, d.RegionName, d.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} with flags {4}",
  233. regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionCoordX, regionInfos.RegionCoordY,
  234. (OpenSim.Framework.RegionFlags)flags);
  235. return String.Empty;
  236. }
  237. public bool DeregisterRegion(UUID regionID)
  238. {
  239. RegionData region = m_Database.Get(regionID, UUID.Zero);
  240. if (region == null)
  241. return false;
  242. m_log.DebugFormat(
  243. "[GRID SERVICE]: Deregistering region {0} ({1}) at {2}-{3}",
  244. region.RegionName, region.RegionID, region.coordX, region.coordY);
  245. int flags = Convert.ToInt32(region.Data["flags"]);
  246. if (!m_DeleteOnUnregister || (flags & (int)OpenSim.Framework.RegionFlags.Persistent) != 0)
  247. {
  248. flags &= ~(int)OpenSim.Framework.RegionFlags.RegionOnline;
  249. region.Data["flags"] = flags.ToString();
  250. region.Data["last_seen"] = Util.UnixTimeSinceEpoch();
  251. try
  252. {
  253. m_Database.Store(region);
  254. }
  255. catch (Exception e)
  256. {
  257. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  258. }
  259. return true;
  260. }
  261. return m_Database.Delete(regionID);
  262. }
  263. public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  264. {
  265. List<GridRegion> rinfos = new List<GridRegion>();
  266. RegionData region = m_Database.Get(regionID, scopeID);
  267. if (region != null)
  268. {
  269. // Not really? Maybe?
  270. List<RegionData> rdatas = m_Database.Get(region.posX - (int)Constants.RegionSize - 1, region.posY - (int)Constants.RegionSize - 1,
  271. region.posX + (int)Constants.RegionSize + 1, region.posY + (int)Constants.RegionSize + 1, scopeID);
  272. foreach (RegionData rdata in rdatas)
  273. {
  274. if (rdata.RegionID != regionID)
  275. {
  276. int flags = Convert.ToInt32(rdata.Data["flags"]);
  277. if ((flags & (int)Framework.RegionFlags.Hyperlink) == 0) // no hyperlinks as neighbours
  278. rinfos.Add(RegionData2RegionInfo(rdata));
  279. }
  280. }
  281. // m_log.DebugFormat("[GRID SERVICE]: region {0} has {1} neighbours", region.RegionName, rinfos.Count);
  282. }
  283. else
  284. {
  285. m_log.WarnFormat(
  286. "[GRID SERVICE]: GetNeighbours() called for scope {0}, region {1} but no such region found",
  287. scopeID, regionID);
  288. }
  289. return rinfos;
  290. }
  291. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  292. {
  293. RegionData rdata = m_Database.Get(regionID, scopeID);
  294. if (rdata != null)
  295. return RegionData2RegionInfo(rdata);
  296. return null;
  297. }
  298. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  299. {
  300. int snapX = (int)(x / Constants.RegionSize) * (int)Constants.RegionSize;
  301. int snapY = (int)(y / Constants.RegionSize) * (int)Constants.RegionSize;
  302. RegionData rdata = m_Database.Get(snapX, snapY, scopeID);
  303. if (rdata != null)
  304. return RegionData2RegionInfo(rdata);
  305. return null;
  306. }
  307. public GridRegion GetRegionByName(UUID scopeID, string name)
  308. {
  309. List<RegionData> rdatas = m_Database.Get(Util.EscapeForLike(name), scopeID);
  310. if ((rdatas != null) && (rdatas.Count > 0))
  311. return RegionData2RegionInfo(rdatas[0]); // get the first
  312. if (m_AllowHypergridMapSearch)
  313. {
  314. GridRegion r = GetHypergridRegionByName(scopeID, name);
  315. if (r != null)
  316. return r;
  317. }
  318. return null;
  319. }
  320. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  321. {
  322. // m_log.DebugFormat("[GRID SERVICE]: GetRegionsByName {0}", name);
  323. List<RegionData> rdatas = m_Database.Get(Util.EscapeForLike(name) + "%", scopeID);
  324. int count = 0;
  325. List<GridRegion> rinfos = new List<GridRegion>();
  326. if (rdatas != null)
  327. {
  328. // m_log.DebugFormat("[GRID SERVICE]: Found {0} regions", rdatas.Count);
  329. foreach (RegionData rdata in rdatas)
  330. {
  331. if (count++ < maxNumber)
  332. rinfos.Add(RegionData2RegionInfo(rdata));
  333. }
  334. }
  335. if (m_AllowHypergridMapSearch && (rdatas == null || (rdatas != null && rdatas.Count == 0)))
  336. {
  337. GridRegion r = GetHypergridRegionByName(scopeID, name);
  338. if (r != null)
  339. rinfos.Add(r);
  340. }
  341. return rinfos;
  342. }
  343. /// <summary>
  344. /// Get a hypergrid region.
  345. /// </summary>
  346. /// <param name="scopeID"></param>
  347. /// <param name="name"></param>
  348. /// <returns>null if no hypergrid region could be found.</returns>
  349. protected GridRegion GetHypergridRegionByName(UUID scopeID, string name)
  350. {
  351. if (name.Contains("."))
  352. return m_HypergridLinker.LinkRegion(scopeID, name);
  353. else
  354. return null;
  355. }
  356. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  357. {
  358. int xminSnap = (int)(xmin / Constants.RegionSize) * (int)Constants.RegionSize;
  359. int xmaxSnap = (int)(xmax / Constants.RegionSize) * (int)Constants.RegionSize;
  360. int yminSnap = (int)(ymin / Constants.RegionSize) * (int)Constants.RegionSize;
  361. int ymaxSnap = (int)(ymax / Constants.RegionSize) * (int)Constants.RegionSize;
  362. List<RegionData> rdatas = m_Database.Get(xminSnap, yminSnap, xmaxSnap, ymaxSnap, scopeID);
  363. List<GridRegion> rinfos = new List<GridRegion>();
  364. foreach (RegionData rdata in rdatas)
  365. rinfos.Add(RegionData2RegionInfo(rdata));
  366. return rinfos;
  367. }
  368. #endregion
  369. #region Data structure conversions
  370. public RegionData RegionInfo2RegionData(GridRegion rinfo)
  371. {
  372. RegionData rdata = new RegionData();
  373. rdata.posX = (int)rinfo.RegionLocX;
  374. rdata.posY = (int)rinfo.RegionLocY;
  375. rdata.RegionID = rinfo.RegionID;
  376. rdata.RegionName = rinfo.RegionName;
  377. rdata.Data = rinfo.ToKeyValuePairs();
  378. rdata.Data["regionHandle"] = Utils.UIntsToLong((uint)rdata.posX, (uint)rdata.posY);
  379. rdata.Data["owner_uuid"] = rinfo.EstateOwner.ToString();
  380. return rdata;
  381. }
  382. public GridRegion RegionData2RegionInfo(RegionData rdata)
  383. {
  384. GridRegion rinfo = new GridRegion(rdata.Data);
  385. rinfo.RegionLocX = rdata.posX;
  386. rinfo.RegionLocY = rdata.posY;
  387. rinfo.RegionID = rdata.RegionID;
  388. rinfo.RegionName = rdata.RegionName;
  389. rinfo.ScopeID = rdata.ScopeID;
  390. return rinfo;
  391. }
  392. #endregion
  393. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  394. {
  395. List<GridRegion> ret = new List<GridRegion>();
  396. List<RegionData> regions = m_Database.GetDefaultRegions(scopeID);
  397. foreach (RegionData r in regions)
  398. {
  399. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  400. ret.Add(RegionData2RegionInfo(r));
  401. }
  402. m_log.DebugFormat("[GRID SERVICE]: GetDefaultRegions returning {0} regions", ret.Count);
  403. return ret;
  404. }
  405. public List<GridRegion> GetDefaultHypergridRegions(UUID scopeID)
  406. {
  407. List<GridRegion> ret = new List<GridRegion>();
  408. List<RegionData> regions = m_Database.GetDefaultHypergridRegions(scopeID);
  409. foreach (RegionData r in regions)
  410. {
  411. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  412. ret.Add(RegionData2RegionInfo(r));
  413. }
  414. int hgDefaultRegionsFoundOnline = regions.Count;
  415. // For now, hypergrid default regions will always be given precedence but we will also return simple default
  416. // regions in case no specific hypergrid regions are specified.
  417. ret.AddRange(GetDefaultRegions(scopeID));
  418. int normalDefaultRegionsFoundOnline = ret.Count - hgDefaultRegionsFoundOnline;
  419. m_log.DebugFormat(
  420. "[GRID SERVICE]: GetDefaultHypergridRegions returning {0} hypergrid default and {1} normal default regions",
  421. hgDefaultRegionsFoundOnline, normalDefaultRegionsFoundOnline);
  422. return ret;
  423. }
  424. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  425. {
  426. List<GridRegion> ret = new List<GridRegion>();
  427. List<RegionData> regions = m_Database.GetFallbackRegions(scopeID, x, y);
  428. foreach (RegionData r in regions)
  429. {
  430. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  431. ret.Add(RegionData2RegionInfo(r));
  432. }
  433. m_log.DebugFormat("[GRID SERVICE]: Fallback returned {0} regions", ret.Count);
  434. return ret;
  435. }
  436. public List<GridRegion> GetHyperlinks(UUID scopeID)
  437. {
  438. List<GridRegion> ret = new List<GridRegion>();
  439. List<RegionData> regions = m_Database.GetHyperlinks(scopeID);
  440. foreach (RegionData r in regions)
  441. {
  442. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  443. ret.Add(RegionData2RegionInfo(r));
  444. }
  445. m_log.DebugFormat("[GRID SERVICE]: Hyperlinks returned {0} regions", ret.Count);
  446. return ret;
  447. }
  448. public int GetRegionFlags(UUID scopeID, UUID regionID)
  449. {
  450. RegionData region = m_Database.Get(regionID, scopeID);
  451. if (region != null)
  452. {
  453. int flags = Convert.ToInt32(region.Data["flags"]);
  454. //m_log.DebugFormat("[GRID SERVICE]: Request for flags of {0}: {1}", regionID, flags);
  455. return flags;
  456. }
  457. else
  458. return -1;
  459. }
  460. private void HandleDeregisterRegion(string module, string[] cmd)
  461. {
  462. if (cmd.Length < 4)
  463. {
  464. MainConsole.Instance.Output("Usage: degregister region id <region-id>+");
  465. return;
  466. }
  467. for (int i = 3; i < cmd.Length; i++)
  468. {
  469. string rawRegionUuid = cmd[i];
  470. UUID regionUuid;
  471. if (!UUID.TryParse(rawRegionUuid, out regionUuid))
  472. {
  473. MainConsole.Instance.OutputFormat("{0} is not a valid region uuid", rawRegionUuid);
  474. return;
  475. }
  476. GridRegion region = GetRegionByUUID(UUID.Zero, regionUuid);
  477. if (region == null)
  478. {
  479. MainConsole.Instance.OutputFormat("No region with UUID {0}", regionUuid);
  480. return;
  481. }
  482. if (DeregisterRegion(regionUuid))
  483. {
  484. MainConsole.Instance.OutputFormat("Deregistered {0} {1}", region.RegionName, regionUuid);
  485. }
  486. else
  487. {
  488. // I don't think this can ever occur if we know that the region exists.
  489. MainConsole.Instance.OutputFormat("Error deregistering {0} {1}", region.RegionName, regionUuid);
  490. }
  491. }
  492. }
  493. private void HandleShowRegions(string module, string[] cmd)
  494. {
  495. if (cmd.Length != 2)
  496. {
  497. MainConsole.Instance.Output("Syntax: show regions");
  498. return;
  499. }
  500. List<RegionData> regions = m_Database.Get(int.MinValue, int.MinValue, int.MaxValue, int.MaxValue, UUID.Zero);
  501. OutputRegionsToConsoleSummary(regions);
  502. }
  503. private void HandleShowRegion(string module, string[] cmd)
  504. {
  505. if (cmd.Length != 4)
  506. {
  507. MainConsole.Instance.Output("Syntax: show region name <region name>");
  508. return;
  509. }
  510. string regionName = cmd[3];
  511. List<RegionData> regions = m_Database.Get(Util.EscapeForLike(regionName), UUID.Zero);
  512. if (regions == null || regions.Count < 1)
  513. {
  514. MainConsole.Instance.Output("No region with name {0} found", regionName);
  515. return;
  516. }
  517. OutputRegionsToConsole(regions);
  518. }
  519. private void HandleShowRegionAt(string module, string[] cmd)
  520. {
  521. if (cmd.Length != 5)
  522. {
  523. MainConsole.Instance.Output("Syntax: show region at <x-coord> <y-coord>");
  524. return;
  525. }
  526. int x, y;
  527. if (!int.TryParse(cmd[3], out x))
  528. {
  529. MainConsole.Instance.Output("x-coord must be an integer");
  530. return;
  531. }
  532. if (!int.TryParse(cmd[4], out y))
  533. {
  534. MainConsole.Instance.Output("y-coord must be an integer");
  535. return;
  536. }
  537. RegionData region = m_Database.Get(x * (int)Constants.RegionSize, y * (int)Constants.RegionSize, UUID.Zero);
  538. if (region == null)
  539. {
  540. MainConsole.Instance.OutputFormat("No region found at {0},{1}", x, y);
  541. return;
  542. }
  543. OutputRegionToConsole(region);
  544. }
  545. private void OutputRegionToConsole(RegionData r)
  546. {
  547. OpenSim.Framework.RegionFlags flags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(r.Data["flags"]);
  548. ConsoleDisplayList dispList = new ConsoleDisplayList();
  549. dispList.AddRow("Region Name", r.RegionName);
  550. dispList.AddRow("Region ID", r.RegionID);
  551. dispList.AddRow("Location", string.Format("{0},{1}", r.coordX, r.coordY));
  552. dispList.AddRow("URI", r.Data["serverURI"]);
  553. dispList.AddRow("Owner ID", r.Data["owner_uuid"]);
  554. dispList.AddRow("Flags", flags);
  555. MainConsole.Instance.Output(dispList.ToString());
  556. }
  557. private void OutputRegionsToConsole(List<RegionData> regions)
  558. {
  559. foreach (RegionData r in regions)
  560. OutputRegionToConsole(r);
  561. }
  562. private void OutputRegionsToConsoleSummary(List<RegionData> regions)
  563. {
  564. ConsoleDisplayTable dispTable = new ConsoleDisplayTable();
  565. dispTable.AddColumn("Name", 16);
  566. dispTable.AddColumn("ID", 36);
  567. dispTable.AddColumn("Position", 11);
  568. dispTable.AddColumn("Owner ID", 36);
  569. dispTable.AddColumn("Flags", 60);
  570. foreach (RegionData r in regions)
  571. {
  572. OpenSim.Framework.RegionFlags flags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(r.Data["flags"]);
  573. dispTable.AddRow(
  574. r.RegionName,
  575. r.RegionID.ToString(),
  576. string.Format("{0},{1}", r.coordX, r.coordY),
  577. r.Data["owner_uuid"].ToString(),
  578. flags.ToString());
  579. }
  580. MainConsole.Instance.Output(dispTable.ToString());
  581. }
  582. private int ParseFlags(int prev, string flags)
  583. {
  584. OpenSim.Framework.RegionFlags f = (OpenSim.Framework.RegionFlags)prev;
  585. string[] parts = flags.Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
  586. foreach (string p in parts)
  587. {
  588. int val;
  589. try
  590. {
  591. if (p.StartsWith("+"))
  592. {
  593. val = (int)Enum.Parse(typeof(OpenSim.Framework.RegionFlags), p.Substring(1));
  594. f |= (OpenSim.Framework.RegionFlags)val;
  595. }
  596. else if (p.StartsWith("-"))
  597. {
  598. val = (int)Enum.Parse(typeof(OpenSim.Framework.RegionFlags), p.Substring(1));
  599. f &= ~(OpenSim.Framework.RegionFlags)val;
  600. }
  601. else
  602. {
  603. val = (int)Enum.Parse(typeof(OpenSim.Framework.RegionFlags), p);
  604. f |= (OpenSim.Framework.RegionFlags)val;
  605. }
  606. }
  607. catch (Exception)
  608. {
  609. MainConsole.Instance.Output("Error in flag specification: " + p);
  610. }
  611. }
  612. return (int)f;
  613. }
  614. private void HandleSetFlags(string module, string[] cmd)
  615. {
  616. if (cmd.Length < 5)
  617. {
  618. MainConsole.Instance.Output("Syntax: set region flags <region name> <flags>");
  619. return;
  620. }
  621. List<RegionData> regions = m_Database.Get(Util.EscapeForLike(cmd[3]), UUID.Zero);
  622. if (regions == null || regions.Count < 1)
  623. {
  624. MainConsole.Instance.Output("Region not found");
  625. return;
  626. }
  627. foreach (RegionData r in regions)
  628. {
  629. int flags = Convert.ToInt32(r.Data["flags"]);
  630. flags = ParseFlags(flags, cmd[4]);
  631. r.Data["flags"] = flags.ToString();
  632. OpenSim.Framework.RegionFlags f = (OpenSim.Framework.RegionFlags)flags;
  633. MainConsole.Instance.Output(String.Format("Set region {0} to {1}", r.RegionName, f));
  634. m_Database.Store(r);
  635. }
  636. }
  637. }
  638. }