GridService.cs 31 KB

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