GridService.cs 31 KB

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