GridService.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 string LogHeader = "[GRID SERVICE]";
  48. private bool m_DeleteOnUnregister = true;
  49. private static GridService m_RootInstance = null;
  50. protected IConfigSource m_config;
  51. protected static HypergridLinker m_HypergridLinker;
  52. protected IAuthenticationService m_AuthenticationService = null;
  53. protected bool m_AllowDuplicateNames = false;
  54. protected bool m_AllowHypergridMapSearch = false;
  55. protected bool m_SuppressVarregionOverlapCheckOnRegistration = false;
  56. public GridService(IConfigSource config)
  57. : base(config)
  58. {
  59. m_log.DebugFormat("[GRID SERVICE]: Starting...");
  60. m_config = config;
  61. IConfig gridConfig = config.Configs["GridService"];
  62. if (gridConfig != null)
  63. {
  64. m_DeleteOnUnregister = gridConfig.GetBoolean("DeleteOnUnregister", true);
  65. string authService = gridConfig.GetString("AuthenticationService", String.Empty);
  66. if (authService != String.Empty)
  67. {
  68. Object[] args = new Object[] { config };
  69. m_AuthenticationService = ServerUtils.LoadPlugin<IAuthenticationService>(authService, args);
  70. }
  71. m_AllowDuplicateNames = gridConfig.GetBoolean("AllowDuplicateNames", m_AllowDuplicateNames);
  72. m_AllowHypergridMapSearch = gridConfig.GetBoolean("AllowHypergridMapSearch", m_AllowHypergridMapSearch);
  73. m_SuppressVarregionOverlapCheckOnRegistration = gridConfig.GetBoolean("SuppressVarregionOverlapCheckOnRegistration", m_SuppressVarregionOverlapCheckOnRegistration);
  74. }
  75. if (m_RootInstance == null)
  76. {
  77. m_RootInstance = this;
  78. if (MainConsole.Instance != null)
  79. {
  80. MainConsole.Instance.Commands.AddCommand("Regions", true,
  81. "deregister region id",
  82. "deregister region id <region-id>+",
  83. "Deregister a region manually.",
  84. String.Empty,
  85. HandleDeregisterRegion);
  86. // A messy way of stopping this command being added if we are in standalone (since the simulator
  87. // has an identically named command
  88. //
  89. // XXX: We're relying on the OpenSimulator version being registered first, which is not well defined.
  90. if (MainConsole.Instance.Commands.Resolve(new string[] { "show", "regions" }).Length == 0)
  91. MainConsole.Instance.Commands.AddCommand("Regions", true,
  92. "show regions",
  93. "show regions",
  94. "Show details on all regions",
  95. String.Empty,
  96. HandleShowRegions);
  97. MainConsole.Instance.Commands.AddCommand("Regions", true,
  98. "show region name",
  99. "show region name <Region name>",
  100. "Show details on a region",
  101. String.Empty,
  102. HandleShowRegion);
  103. MainConsole.Instance.Commands.AddCommand("Regions", true,
  104. "show region at",
  105. "show region at <x-coord> <y-coord>",
  106. "Show details on a region at the given co-ordinate.",
  107. "For example, show region at 1000 1000",
  108. HandleShowRegionAt);
  109. MainConsole.Instance.Commands.AddCommand("General", true,
  110. "show grid size",
  111. "show grid size",
  112. "Show the current grid size (excluding hyperlink references)",
  113. String.Empty,
  114. HandleShowGridSize);
  115. MainConsole.Instance.Commands.AddCommand("Regions", true,
  116. "set region flags",
  117. "set region flags <Region name> <flags>",
  118. "Set database flags for region",
  119. String.Empty,
  120. HandleSetFlags);
  121. }
  122. m_HypergridLinker = new HypergridLinker(m_config, this, m_Database);
  123. }
  124. }
  125. #region IGridService
  126. public string RegisterRegion(UUID scopeID, GridRegion regionInfos)
  127. {
  128. IConfig gridConfig = m_config.Configs["GridService"];
  129. if (regionInfos.RegionID == UUID.Zero)
  130. return "Invalid RegionID - cannot be zero UUID";
  131. String reason = "Region overlaps another region";
  132. RegionData region = FindAnyConflictingRegion(regionInfos, scopeID, out reason);
  133. // If there is a conflicting region, if it has the same ID and same coordinates
  134. // then it is a region re-registering (permissions and ownership checked later).
  135. if ((region != null)
  136. && ( (region.coordX != regionInfos.RegionCoordX)
  137. || (region.coordY != regionInfos.RegionCoordY)
  138. || (region.RegionID != regionInfos.RegionID) )
  139. )
  140. {
  141. // If not same ID and same coordinates, this new region has conflicts and can't be registered.
  142. m_log.WarnFormat("{0} Register region conflict in scope {1}. {2}", LogHeader, scopeID, reason);
  143. return reason;
  144. }
  145. if (region != null)
  146. {
  147. // There is a preexisting record
  148. //
  149. // Get it's flags
  150. //
  151. OpenSim.Framework.RegionFlags rflags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(region.Data["flags"]);
  152. // Is this a reservation?
  153. //
  154. if ((rflags & OpenSim.Framework.RegionFlags.Reservation) != 0)
  155. {
  156. // Regions reserved for the null key cannot be taken.
  157. if ((string)region.Data["PrincipalID"] == UUID.Zero.ToString())
  158. return "Region location is reserved";
  159. // Treat it as an auth request
  160. //
  161. // NOTE: Fudging the flags value here, so these flags
  162. // should not be used elsewhere. Don't optimize
  163. // this with the later retrieval of the same flags!
  164. rflags |= OpenSim.Framework.RegionFlags.Authenticate;
  165. }
  166. if ((rflags & OpenSim.Framework.RegionFlags.Authenticate) != 0)
  167. {
  168. // Can we authenticate at all?
  169. //
  170. if (m_AuthenticationService == null)
  171. return "No authentication possible";
  172. if (!m_AuthenticationService.Verify(new UUID(region.Data["PrincipalID"].ToString()), regionInfos.Token, 30))
  173. return "Bad authentication";
  174. }
  175. }
  176. // If we get here, the destination is clear. Now for the real check.
  177. if (!m_AllowDuplicateNames)
  178. {
  179. List<RegionData> dupe = m_Database.Get(Util.EscapeForLike(regionInfos.RegionName), scopeID);
  180. if (dupe != null && dupe.Count > 0)
  181. {
  182. foreach (RegionData d in dupe)
  183. {
  184. if (d.RegionID != regionInfos.RegionID)
  185. {
  186. m_log.WarnFormat("[GRID SERVICE]: Region tried to register using a duplicate name. New region: {0} ({1}), existing region: {2} ({3}).",
  187. regionInfos.RegionName, regionInfos.RegionID, d.RegionName, d.RegionID);
  188. return "Duplicate region name";
  189. }
  190. }
  191. }
  192. }
  193. // If there is an old record for us, delete it if it is elsewhere.
  194. region = m_Database.Get(regionInfos.RegionID, scopeID);
  195. if ((region != null) && (region.RegionID == regionInfos.RegionID) &&
  196. ((region.posX != regionInfos.RegionLocX) || (region.posY != regionInfos.RegionLocY)))
  197. {
  198. if ((Convert.ToInt32(region.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.NoMove) != 0)
  199. return "Can't move this region";
  200. if ((Convert.ToInt32(region.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.LockedOut) != 0)
  201. return "Region locked out";
  202. // Region reregistering in other coordinates. Delete the old entry
  203. m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) was previously registered at {2}-{3}. Deleting old entry.",
  204. regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY);
  205. try
  206. {
  207. m_Database.Delete(regionInfos.RegionID);
  208. }
  209. catch (Exception e)
  210. {
  211. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  212. }
  213. }
  214. // Everything is ok, let's register
  215. RegionData rdata = RegionInfo2RegionData(regionInfos);
  216. rdata.ScopeID = scopeID;
  217. if (region != null)
  218. {
  219. int oldFlags = Convert.ToInt32(region.Data["flags"]);
  220. oldFlags &= ~(int)OpenSim.Framework.RegionFlags.Reservation;
  221. rdata.Data["flags"] = oldFlags.ToString(); // Preserve flags
  222. }
  223. else
  224. {
  225. rdata.Data["flags"] = "0";
  226. if ((gridConfig != null) && rdata.RegionName != string.Empty)
  227. {
  228. int newFlags = 0;
  229. string regionName = rdata.RegionName.Trim().Replace(' ', '_');
  230. newFlags = ParseFlags(newFlags, gridConfig.GetString("DefaultRegionFlags", String.Empty));
  231. newFlags = ParseFlags(newFlags, gridConfig.GetString("Region_" + regionName, String.Empty));
  232. newFlags = ParseFlags(newFlags, gridConfig.GetString("Region_" + rdata.RegionID.ToString(), String.Empty));
  233. rdata.Data["flags"] = newFlags.ToString();
  234. }
  235. }
  236. int flags = Convert.ToInt32(rdata.Data["flags"]);
  237. flags |= (int)OpenSim.Framework.RegionFlags.RegionOnline;
  238. rdata.Data["flags"] = flags.ToString();
  239. try
  240. {
  241. rdata.Data["last_seen"] = Util.UnixTimeSinceEpoch();
  242. m_Database.Store(rdata);
  243. }
  244. catch (Exception e)
  245. {
  246. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  247. }
  248. m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) registered successfully at {2}-{3} with flags {4}",
  249. regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionCoordX, regionInfos.RegionCoordY,
  250. (OpenSim.Framework.RegionFlags)flags);
  251. return String.Empty;
  252. }
  253. /// <summary>
  254. /// Search the region map for regions conflicting with this region.
  255. /// The region to be added is passed and we look for any existing regions that are
  256. /// in the requested location, that are large varregions that overlap this region, or
  257. /// are previously defined regions that would lie under this new region.
  258. /// </summary>
  259. /// <param name="regionInfos">Information on region requested to be added to the world map</param>
  260. /// <param name="scopeID">Grid id for region</param>
  261. /// <param name="reason">The reason the returned region conflicts with passed region</param>
  262. /// <returns></returns>
  263. private RegionData FindAnyConflictingRegion(GridRegion regionInfos, UUID scopeID, out string reason)
  264. {
  265. reason = "Reregistration";
  266. // First see if there is an existing region right where this region is trying to go
  267. // (We keep this result so it can be returned if suppressing errors)
  268. RegionData noErrorRegion = m_Database.Get(regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID);
  269. RegionData region = noErrorRegion;
  270. if (region != null
  271. && region.RegionID == regionInfos.RegionID
  272. && region.sizeX == regionInfos.RegionSizeX
  273. && region.sizeY == regionInfos.RegionSizeY)
  274. {
  275. // If this seems to be exactly the same region, return this as it could be
  276. // a re-registration (permissions checked by calling routine).
  277. m_log.DebugFormat("{0} FindAnyConflictingRegion: re-register of {1}",
  278. LogHeader, RegionString(regionInfos));
  279. return region;
  280. }
  281. // No region exactly there or we're resizing an existing region.
  282. // Fetch regions that could be varregions overlapping requested location.
  283. int xmin = regionInfos.RegionLocX - (int)Constants.MaximumRegionSize + 10;
  284. int xmax = regionInfos.RegionLocX;
  285. int ymin = regionInfos.RegionLocY - (int)Constants.MaximumRegionSize + 10;
  286. int ymax = regionInfos.RegionLocY;
  287. List<RegionData> rdatas = m_Database.Get(xmin, ymin, xmax, ymax, scopeID);
  288. foreach (RegionData rdata in rdatas)
  289. {
  290. // m_log.DebugFormat("{0} FindAnyConflictingRegion: find existing. Checking {1}", LogHeader, RegionString(rdata) );
  291. if ( (rdata.posX + rdata.sizeX > regionInfos.RegionLocX)
  292. && (rdata.posY + rdata.sizeY > regionInfos.RegionLocY) )
  293. {
  294. region = rdata;
  295. m_log.WarnFormat("{0} FindAnyConflictingRegion: conflict of {1} by existing varregion {2}",
  296. LogHeader, RegionString(regionInfos), RegionString(region));
  297. reason = String.Format("Region location is overlapped by existing varregion {0}",
  298. RegionString(region));
  299. if (m_SuppressVarregionOverlapCheckOnRegistration)
  300. region = noErrorRegion;
  301. return region;
  302. }
  303. }
  304. // There isn't a region that overlaps this potential region.
  305. // See if this potential region overlaps an existing region.
  306. // First, a shortcut of not looking for overlap if new region is legacy region sized
  307. // and connot overlap anything.
  308. if (regionInfos.RegionSizeX != Constants.RegionSize
  309. || regionInfos.RegionSizeY != Constants.RegionSize)
  310. {
  311. // trim range looked for so we don't pick up neighbor regions just off the edges
  312. xmin = regionInfos.RegionLocX;
  313. xmax = regionInfos.RegionLocX + regionInfos.RegionSizeX - 10;
  314. ymin = regionInfos.RegionLocY;
  315. ymax = regionInfos.RegionLocY + regionInfos.RegionSizeY - 10;
  316. rdatas = m_Database.Get(xmin, ymin, xmax, ymax, scopeID);
  317. // If the region is being resized, the found region could be ourself.
  318. foreach (RegionData rdata in rdatas)
  319. {
  320. // m_log.DebugFormat("{0} FindAnyConflictingRegion: see if overlap. Checking {1}", LogHeader, RegionString(rdata) );
  321. if (region == null || region.RegionID != regionInfos.RegionID)
  322. {
  323. region = rdata;
  324. m_log.WarnFormat("{0} FindAnyConflictingRegion: conflict of varregion {1} overlaps existing region {2}",
  325. LogHeader, RegionString(regionInfos), RegionString(region));
  326. reason = String.Format("Region {0} would overlap existing region {1}",
  327. RegionString(regionInfos), RegionString(region));
  328. if (m_SuppressVarregionOverlapCheckOnRegistration)
  329. region = noErrorRegion;
  330. return region;
  331. }
  332. }
  333. }
  334. // If we get here, region is either null (nothing found here) or
  335. // is the non-conflicting region found at the location being requested.
  336. return region;
  337. }
  338. // String describing name and region location of passed region
  339. private String RegionString(RegionData reg)
  340. {
  341. return String.Format("{0}/{1} at <{2},{3}>",
  342. reg.RegionName, reg.RegionID, reg.coordX, reg.coordY);
  343. }
  344. // String describing name and region location of passed region
  345. private String RegionString(GridRegion reg)
  346. {
  347. return String.Format("{0}/{1} at <{2},{3}>",
  348. reg.RegionName, reg.RegionID, reg.RegionCoordX, reg.RegionCoordY);
  349. }
  350. public bool DeregisterRegion(UUID regionID)
  351. {
  352. RegionData region = m_Database.Get(regionID, UUID.Zero);
  353. if (region == null)
  354. return false;
  355. m_log.DebugFormat(
  356. "[GRID SERVICE]: Deregistering region {0} ({1}) at {2}-{3}",
  357. region.RegionName, region.RegionID, region.coordX, region.coordY);
  358. int flags = Convert.ToInt32(region.Data["flags"]);
  359. if (!m_DeleteOnUnregister || (flags & (int)OpenSim.Framework.RegionFlags.Persistent) != 0)
  360. {
  361. flags &= ~(int)OpenSim.Framework.RegionFlags.RegionOnline;
  362. region.Data["flags"] = flags.ToString();
  363. region.Data["last_seen"] = Util.UnixTimeSinceEpoch();
  364. try
  365. {
  366. m_Database.Store(region);
  367. }
  368. catch (Exception e)
  369. {
  370. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  371. }
  372. return true;
  373. }
  374. return m_Database.Delete(regionID);
  375. }
  376. public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  377. {
  378. List<GridRegion> rinfos = new List<GridRegion>();
  379. RegionData region = m_Database.Get(regionID, scopeID);
  380. if (region != null)
  381. {
  382. // Not really? Maybe?
  383. // The adjacent regions are presumed to be the same size as the current region
  384. List<RegionData> rdatas = m_Database.Get(
  385. region.posX - region.sizeX - 1, region.posY - region.sizeY - 1,
  386. region.posX + region.sizeX + 1, region.posY + region.sizeY + 1, scopeID);
  387. foreach (RegionData rdata in rdatas)
  388. {
  389. if (rdata.RegionID != regionID)
  390. {
  391. int flags = Convert.ToInt32(rdata.Data["flags"]);
  392. if ((flags & (int)Framework.RegionFlags.Hyperlink) == 0) // no hyperlinks as neighbours
  393. rinfos.Add(RegionData2RegionInfo(rdata));
  394. }
  395. }
  396. // m_log.DebugFormat("[GRID SERVICE]: region {0} has {1} neighbours", region.RegionName, rinfos.Count);
  397. }
  398. else
  399. {
  400. m_log.WarnFormat(
  401. "[GRID SERVICE]: GetNeighbours() called for scope {0}, region {1} but no such region found",
  402. scopeID, regionID);
  403. }
  404. return rinfos;
  405. }
  406. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  407. {
  408. RegionData rdata = m_Database.Get(regionID, scopeID);
  409. if (rdata != null)
  410. return RegionData2RegionInfo(rdata);
  411. return null;
  412. }
  413. // Get a region given its base coordinates.
  414. // NOTE: this is NOT 'get a region by some point in the region'. The coordinate MUST
  415. // be the base coordinate of the region.
  416. // The snapping is technically unnecessary but is harmless because regions are always
  417. // multiples of the legacy region size (256).
  418. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  419. {
  420. int snapX = (int)(x / Constants.RegionSize) * (int)Constants.RegionSize;
  421. int snapY = (int)(y / Constants.RegionSize) * (int)Constants.RegionSize;
  422. RegionData rdata = m_Database.Get(snapX, snapY, scopeID);
  423. if (rdata != null)
  424. return RegionData2RegionInfo(rdata);
  425. return null;
  426. }
  427. public GridRegion GetRegionByName(UUID scopeID, string name)
  428. {
  429. List<RegionData> rdatas = m_Database.Get(Util.EscapeForLike(name), scopeID);
  430. if ((rdatas != null) && (rdatas.Count > 0))
  431. return RegionData2RegionInfo(rdatas[0]); // get the first
  432. if (m_AllowHypergridMapSearch)
  433. {
  434. GridRegion r = GetHypergridRegionByName(scopeID, name);
  435. if (r != null)
  436. return r;
  437. }
  438. return null;
  439. }
  440. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  441. {
  442. // m_log.DebugFormat("[GRID SERVICE]: GetRegionsByName {0}", name);
  443. List<RegionData> rdatas = m_Database.Get(Util.EscapeForLike(name) + "%", scopeID);
  444. int count = 0;
  445. List<GridRegion> rinfos = new List<GridRegion>();
  446. if (rdatas != null)
  447. {
  448. // m_log.DebugFormat("[GRID SERVICE]: Found {0} regions", rdatas.Count);
  449. foreach (RegionData rdata in rdatas)
  450. {
  451. if (count++ < maxNumber)
  452. rinfos.Add(RegionData2RegionInfo(rdata));
  453. }
  454. }
  455. if (m_AllowHypergridMapSearch && (rdatas == null || (rdatas != null && rdatas.Count == 0)))
  456. {
  457. GridRegion r = GetHypergridRegionByName(scopeID, name);
  458. if (r != null)
  459. rinfos.Add(r);
  460. }
  461. return rinfos;
  462. }
  463. /// <summary>
  464. /// Get a hypergrid region.
  465. /// </summary>
  466. /// <param name="scopeID"></param>
  467. /// <param name="name"></param>
  468. /// <returns>null if no hypergrid region could be found.</returns>
  469. protected GridRegion GetHypergridRegionByName(UUID scopeID, string name)
  470. {
  471. if (name.Contains("."))
  472. return m_HypergridLinker.LinkRegion(scopeID, name);
  473. else
  474. return null;
  475. }
  476. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  477. {
  478. int xminSnap = (int)(xmin / Constants.RegionSize) * (int)Constants.RegionSize;
  479. int xmaxSnap = (int)(xmax / Constants.RegionSize) * (int)Constants.RegionSize;
  480. int yminSnap = (int)(ymin / Constants.RegionSize) * (int)Constants.RegionSize;
  481. int ymaxSnap = (int)(ymax / Constants.RegionSize) * (int)Constants.RegionSize;
  482. List<RegionData> rdatas = m_Database.Get(xminSnap, yminSnap, xmaxSnap, ymaxSnap, scopeID);
  483. List<GridRegion> rinfos = new List<GridRegion>();
  484. foreach (RegionData rdata in rdatas)
  485. rinfos.Add(RegionData2RegionInfo(rdata));
  486. return rinfos;
  487. }
  488. #endregion
  489. #region Data structure conversions
  490. public RegionData RegionInfo2RegionData(GridRegion rinfo)
  491. {
  492. RegionData rdata = new RegionData();
  493. rdata.posX = (int)rinfo.RegionLocX;
  494. rdata.posY = (int)rinfo.RegionLocY;
  495. rdata.sizeX = rinfo.RegionSizeX;
  496. rdata.sizeY = rinfo.RegionSizeY;
  497. rdata.RegionID = rinfo.RegionID;
  498. rdata.RegionName = rinfo.RegionName;
  499. rdata.Data = rinfo.ToKeyValuePairs();
  500. rdata.Data["regionHandle"] = Utils.UIntsToLong((uint)rdata.posX, (uint)rdata.posY);
  501. rdata.Data["owner_uuid"] = rinfo.EstateOwner.ToString();
  502. return rdata;
  503. }
  504. public GridRegion RegionData2RegionInfo(RegionData rdata)
  505. {
  506. GridRegion rinfo = new GridRegion(rdata.Data);
  507. rinfo.RegionLocX = rdata.posX;
  508. rinfo.RegionLocY = rdata.posY;
  509. rinfo.RegionSizeX = rdata.sizeX;
  510. rinfo.RegionSizeY = rdata.sizeY;
  511. rinfo.RegionID = rdata.RegionID;
  512. rinfo.RegionName = rdata.RegionName;
  513. rinfo.ScopeID = rdata.ScopeID;
  514. return rinfo;
  515. }
  516. #endregion
  517. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  518. {
  519. List<GridRegion> ret = new List<GridRegion>();
  520. List<RegionData> regions = m_Database.GetDefaultRegions(scopeID);
  521. foreach (RegionData r in regions)
  522. {
  523. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  524. ret.Add(RegionData2RegionInfo(r));
  525. }
  526. m_log.DebugFormat("[GRID SERVICE]: GetDefaultRegions returning {0} regions", ret.Count);
  527. return ret;
  528. }
  529. public List<GridRegion> GetDefaultHypergridRegions(UUID scopeID)
  530. {
  531. List<GridRegion> ret = new List<GridRegion>();
  532. List<RegionData> regions = m_Database.GetDefaultHypergridRegions(scopeID);
  533. foreach (RegionData r in regions)
  534. {
  535. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  536. ret.Add(RegionData2RegionInfo(r));
  537. }
  538. int hgDefaultRegionsFoundOnline = regions.Count;
  539. // For now, hypergrid default regions will always be given precedence but we will also return simple default
  540. // regions in case no specific hypergrid regions are specified.
  541. ret.AddRange(GetDefaultRegions(scopeID));
  542. int normalDefaultRegionsFoundOnline = ret.Count - hgDefaultRegionsFoundOnline;
  543. m_log.DebugFormat(
  544. "[GRID SERVICE]: GetDefaultHypergridRegions returning {0} hypergrid default and {1} normal default regions",
  545. hgDefaultRegionsFoundOnline, normalDefaultRegionsFoundOnline);
  546. return ret;
  547. }
  548. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  549. {
  550. List<GridRegion> ret = new List<GridRegion>();
  551. List<RegionData> regions = m_Database.GetFallbackRegions(scopeID, x, y);
  552. foreach (RegionData r in regions)
  553. {
  554. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  555. ret.Add(RegionData2RegionInfo(r));
  556. }
  557. m_log.DebugFormat("[GRID SERVICE]: Fallback returned {0} regions", ret.Count);
  558. return ret;
  559. }
  560. public List<GridRegion> GetHyperlinks(UUID scopeID)
  561. {
  562. List<GridRegion> ret = new List<GridRegion>();
  563. List<RegionData> regions = m_Database.GetHyperlinks(scopeID);
  564. foreach (RegionData r in regions)
  565. {
  566. if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  567. ret.Add(RegionData2RegionInfo(r));
  568. }
  569. m_log.DebugFormat("[GRID SERVICE]: Hyperlinks returned {0} regions", ret.Count);
  570. return ret;
  571. }
  572. public int GetRegionFlags(UUID scopeID, UUID regionID)
  573. {
  574. RegionData region = m_Database.Get(regionID, scopeID);
  575. if (region != null)
  576. {
  577. int flags = Convert.ToInt32(region.Data["flags"]);
  578. //m_log.DebugFormat("[GRID SERVICE]: Request for flags of {0}: {1}", regionID, flags);
  579. return flags;
  580. }
  581. else
  582. return -1;
  583. }
  584. private void HandleDeregisterRegion(string module, string[] cmd)
  585. {
  586. if (cmd.Length < 4)
  587. {
  588. MainConsole.Instance.Output("Usage: degregister region id <region-id>+");
  589. return;
  590. }
  591. for (int i = 3; i < cmd.Length; i++)
  592. {
  593. string rawRegionUuid = cmd[i];
  594. UUID regionUuid;
  595. if (!UUID.TryParse(rawRegionUuid, out regionUuid))
  596. {
  597. MainConsole.Instance.OutputFormat("{0} is not a valid region uuid", rawRegionUuid);
  598. return;
  599. }
  600. GridRegion region = GetRegionByUUID(UUID.Zero, regionUuid);
  601. if (region == null)
  602. {
  603. MainConsole.Instance.OutputFormat("No region with UUID {0}", regionUuid);
  604. return;
  605. }
  606. if (DeregisterRegion(regionUuid))
  607. {
  608. MainConsole.Instance.OutputFormat("Deregistered {0} {1}", region.RegionName, regionUuid);
  609. }
  610. else
  611. {
  612. // I don't think this can ever occur if we know that the region exists.
  613. MainConsole.Instance.OutputFormat("Error deregistering {0} {1}", region.RegionName, regionUuid);
  614. }
  615. }
  616. }
  617. private void HandleShowRegions(string module, string[] cmd)
  618. {
  619. if (cmd.Length != 2)
  620. {
  621. MainConsole.Instance.Output("Syntax: show regions");
  622. return;
  623. }
  624. List<RegionData> regions = m_Database.Get(int.MinValue, int.MinValue, int.MaxValue, int.MaxValue, UUID.Zero);
  625. OutputRegionsToConsoleSummary(regions);
  626. }
  627. private void HandleShowGridSize(string module, string[] cmd)
  628. {
  629. List<RegionData> regions = m_Database.Get(int.MinValue, int.MinValue, int.MaxValue, int.MaxValue, UUID.Zero);
  630. double size = 0;
  631. foreach (RegionData region in regions)
  632. {
  633. int flags = Convert.ToInt32(region.Data["flags"]);
  634. if ((flags & (int)Framework.RegionFlags.Hyperlink) == 0)
  635. size += region.sizeX * region.sizeY;
  636. }
  637. MainConsole.Instance.Output("This is a very rough approximation.");
  638. MainConsole.Instance.Output("Although it will not count regions that are actually links to others over the Hypergrid, ");
  639. MainConsole.Instance.Output("it will count regions that are inactive but were not deregistered from the grid service");
  640. MainConsole.Instance.Output("(e.g. simulator crashed rather than shutting down cleanly).\n");
  641. MainConsole.Instance.OutputFormat("Grid size: {0} km squared.", size / 1000000);
  642. }
  643. private void HandleShowRegion(string module, string[] cmd)
  644. {
  645. if (cmd.Length != 4)
  646. {
  647. MainConsole.Instance.Output("Syntax: show region name <region name>");
  648. return;
  649. }
  650. string regionName = cmd[3];
  651. List<RegionData> regions = m_Database.Get(Util.EscapeForLike(regionName), UUID.Zero);
  652. if (regions == null || regions.Count < 1)
  653. {
  654. MainConsole.Instance.Output("No region with name {0} found", regionName);
  655. return;
  656. }
  657. OutputRegionsToConsole(regions);
  658. }
  659. private void HandleShowRegionAt(string module, string[] cmd)
  660. {
  661. if (cmd.Length != 5)
  662. {
  663. MainConsole.Instance.Output("Syntax: show region at <x-coord> <y-coord>");
  664. return;
  665. }
  666. uint x, y;
  667. if (!uint.TryParse(cmd[3], out x))
  668. {
  669. MainConsole.Instance.Output("x-coord must be an integer");
  670. return;
  671. }
  672. if (!uint.TryParse(cmd[4], out y))
  673. {
  674. MainConsole.Instance.Output("y-coord must be an integer");
  675. return;
  676. }
  677. RegionData region = m_Database.Get((int)Util.RegionToWorldLoc(x), (int)Util.RegionToWorldLoc(y), UUID.Zero);
  678. if (region == null)
  679. {
  680. MainConsole.Instance.OutputFormat("No region found at {0},{1}", x, y);
  681. return;
  682. }
  683. OutputRegionToConsole(region);
  684. }
  685. private void OutputRegionToConsole(RegionData r)
  686. {
  687. OpenSim.Framework.RegionFlags flags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(r.Data["flags"]);
  688. ConsoleDisplayList dispList = new ConsoleDisplayList();
  689. dispList.AddRow("Region Name", r.RegionName);
  690. dispList.AddRow("Region ID", r.RegionID);
  691. dispList.AddRow("Position", string.Format("{0},{1}", r.coordX, r.coordY));
  692. dispList.AddRow("Size", string.Format("{0}x{1}", r.sizeX, r.sizeY));
  693. dispList.AddRow("URI", r.Data["serverURI"]);
  694. dispList.AddRow("Owner ID", r.Data["owner_uuid"]);
  695. dispList.AddRow("Flags", flags);
  696. MainConsole.Instance.Output(dispList.ToString());
  697. }
  698. private void OutputRegionsToConsole(List<RegionData> regions)
  699. {
  700. foreach (RegionData r in regions)
  701. OutputRegionToConsole(r);
  702. }
  703. private void OutputRegionsToConsoleSummary(List<RegionData> regions)
  704. {
  705. ConsoleDisplayTable dispTable = new ConsoleDisplayTable();
  706. dispTable.AddColumn("Name", 44);
  707. dispTable.AddColumn("ID", 36);
  708. dispTable.AddColumn("Position", 11);
  709. dispTable.AddColumn("Size", 11);
  710. dispTable.AddColumn("Flags", 60);
  711. foreach (RegionData r in regions)
  712. {
  713. OpenSim.Framework.RegionFlags flags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(r.Data["flags"]);
  714. dispTable.AddRow(
  715. r.RegionName,
  716. r.RegionID.ToString(),
  717. string.Format("{0},{1}", r.coordX, r.coordY),
  718. string.Format("{0}x{1}", r.sizeX, r.sizeY),
  719. flags.ToString());
  720. }
  721. MainConsole.Instance.Output(dispTable.ToString());
  722. }
  723. private int ParseFlags(int prev, string flags)
  724. {
  725. OpenSim.Framework.RegionFlags f = (OpenSim.Framework.RegionFlags)prev;
  726. string[] parts = flags.Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
  727. foreach (string p in parts)
  728. {
  729. int val;
  730. try
  731. {
  732. if (p.StartsWith("+"))
  733. {
  734. val = (int)Enum.Parse(typeof(OpenSim.Framework.RegionFlags), p.Substring(1));
  735. f |= (OpenSim.Framework.RegionFlags)val;
  736. }
  737. else if (p.StartsWith("-"))
  738. {
  739. val = (int)Enum.Parse(typeof(OpenSim.Framework.RegionFlags), p.Substring(1));
  740. f &= ~(OpenSim.Framework.RegionFlags)val;
  741. }
  742. else
  743. {
  744. val = (int)Enum.Parse(typeof(OpenSim.Framework.RegionFlags), p);
  745. f |= (OpenSim.Framework.RegionFlags)val;
  746. }
  747. }
  748. catch (Exception)
  749. {
  750. MainConsole.Instance.Output("Error in flag specification: " + p);
  751. }
  752. }
  753. return (int)f;
  754. }
  755. private void HandleSetFlags(string module, string[] cmd)
  756. {
  757. if (cmd.Length < 5)
  758. {
  759. MainConsole.Instance.Output("Syntax: set region flags <region name> <flags>");
  760. return;
  761. }
  762. List<RegionData> regions = m_Database.Get(Util.EscapeForLike(cmd[3]), UUID.Zero);
  763. if (regions == null || regions.Count < 1)
  764. {
  765. MainConsole.Instance.Output("Region not found");
  766. return;
  767. }
  768. foreach (RegionData r in regions)
  769. {
  770. int flags = Convert.ToInt32(r.Data["flags"]);
  771. flags = ParseFlags(flags, cmd[4]);
  772. r.Data["flags"] = flags.ToString();
  773. OpenSim.Framework.RegionFlags f = (OpenSim.Framework.RegionFlags)flags;
  774. MainConsole.Instance.Output(String.Format("Set region {0} to {1}", r.RegionName, f));
  775. m_Database.Store(r);
  776. }
  777. }
  778. }
  779. }