HypergridLinker.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Linq;
  32. using System.Net;
  33. using System.Reflection;
  34. using System.Xml;
  35. using Nini.Config;
  36. using log4net;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Data;
  40. using OpenSim.Server.Base;
  41. using OpenSim.Services.Interfaces;
  42. using OpenSim.Services.Connectors.Hypergrid;
  43. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  44. using OpenMetaverse;
  45. namespace OpenSim.Services.GridService
  46. {
  47. public class HypergridLinker : IHypergridLinker
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private static uint m_autoMappingX = 0;
  51. private static uint m_autoMappingY = 0;
  52. private static bool m_enableAutoMapping = false;
  53. protected IRegionData m_Database;
  54. protected GridService m_GridService;
  55. protected IAssetService m_AssetService;
  56. protected GatekeeperServiceConnector m_GatekeeperConnector;
  57. protected UUID m_ScopeID = UUID.Zero;
  58. // protected bool m_Check4096 = true;
  59. protected string m_MapTileDirectory = string.Empty;
  60. protected GridInfo m_ThisGridInfo;
  61. public HypergridLinker(IConfigSource config, GridService gridService, IRegionData db)
  62. {
  63. IConfig gridConfig = config.Configs["GridService"];
  64. if (gridConfig == null)
  65. return;
  66. if (!gridConfig.GetBoolean("HypergridLinker", false))
  67. return;
  68. m_Database = db;
  69. m_GridService = gridService;
  70. m_log.DebugFormat("[HYPERGRID LINKER]: Starting with db {0}", db.GetType());
  71. string assetService = gridConfig.GetString("AssetService", string.Empty);
  72. object[] args = new object[] { config };
  73. if (assetService != string.Empty)
  74. m_AssetService = ServerUtils.LoadPlugin<IAssetService>(assetService, args);
  75. string scope = gridConfig.GetString("ScopeID", string.Empty);
  76. if (scope != string.Empty)
  77. UUID.TryParse(scope, out m_ScopeID);
  78. //m_Check4096 = gridConfig.GetBoolean("Check4096", true);
  79. m_MapTileDirectory = gridConfig.GetString("MapTileDirectory", "maptiles");
  80. m_ThisGridInfo = new GridInfo(config);
  81. if(!m_ThisGridInfo.HasHGConfig)
  82. throw new Exception("missing HyperGrid configuration");
  83. m_log.DebugFormat("[HYPERGRID LINKER]: Local Gatekeeper: {0}", m_ThisGridInfo.GateKeeperURL);
  84. m_GatekeeperConnector = new GatekeeperServiceConnector(m_AssetService);
  85. m_log.Debug("[HYPERGRID LINKER]: Loaded all services...");
  86. if (!string.IsNullOrEmpty(m_MapTileDirectory))
  87. {
  88. try
  89. {
  90. Directory.CreateDirectory(m_MapTileDirectory);
  91. }
  92. catch (Exception e)
  93. {
  94. m_log.WarnFormat("[HYPERGRID LINKER]: Could not create map tile storage directory {0}: {1}", m_MapTileDirectory, e);
  95. m_MapTileDirectory = string.Empty;
  96. }
  97. }
  98. if (MainConsole.Instance != null)
  99. {
  100. MainConsole.Instance.Commands.AddCommand("Hypergrid", false, "link-region",
  101. "link-region <Xloc> <Yloc> <ServerURI> [<RemoteRegionName>]",
  102. "Link a HyperGrid Region. Examples for <ServerURI>: http://grid.net:8002/ or http://example.org/path/foo.php", RunCommand);
  103. MainConsole.Instance.Commands.AddCommand("Hypergrid", false, "link-region",
  104. "link-region <Xloc> <Yloc> <RegionIP> <RegionPort> [<RemoteRegionName>]",
  105. "Link a hypergrid region (deprecated)", RunCommand);
  106. MainConsole.Instance.Commands.AddCommand("Hypergrid", false, "unlink-region",
  107. "unlink-region <local name>",
  108. "Unlink a hypergrid region", RunCommand);
  109. MainConsole.Instance.Commands.AddCommand("Hypergrid", false, "link-mapping", "link-mapping [<x> <y>]",
  110. "Set local coordinate to map HG regions to", RunCommand);
  111. MainConsole.Instance.Commands.AddCommand("Hypergrid", false, "show hyperlinks", "show hyperlinks",
  112. "List the HG regions", HandleShow);
  113. }
  114. }
  115. #region Link Region
  116. // from map search
  117. public GridRegion LinkRegion(UUID scopeID, string regionDescriptor)
  118. {
  119. string reason = string.Empty;
  120. uint xloc = Util.RegionToWorldLoc((uint)random.Next(0, Int16.MaxValue));
  121. return TryLinkRegionToCoords(scopeID, regionDescriptor, (int)xloc, 0, out reason);
  122. }
  123. public GridRegion LinkRegion(UUID scopeID, RegionURI rurl)
  124. {
  125. if(!rurl.IsValid)
  126. return null;
  127. if (rurl.IsLocalGrid)
  128. {
  129. m_log.InfoFormat("[HYPERGRID LINKER]: Cannot hyperlink to regions on the same grid");
  130. return null;
  131. }
  132. int xloc = random.Next(0, short.MaxValue) << 8;
  133. if(TryCreateLinkImpl(scopeID, xloc, 0, rurl, UUID.Zero, out GridRegion regInfo))
  134. return regInfo;
  135. return null;
  136. }
  137. private static IPEndPoint dummyIP = new IPEndPoint(0,0);
  138. private bool TryCreateLinkImpl(UUID scopeID, int xloc, int yloc, RegionURI rurl, UUID ownerID, out GridRegion regInfo)
  139. {
  140. m_log.InfoFormat("[HYPERGRID LINKER]: Link to {0} {1}, in <{2},{3}>",
  141. rurl.HostUrl, rurl.RegionName, Util.WorldToRegionLoc((uint)xloc), Util.WorldToRegionLoc((uint)yloc));
  142. // Check for free coordinates
  143. GridRegion region = m_GridService.GetRegionByPosition(scopeID, xloc, yloc);
  144. if (region != null)
  145. {
  146. m_log.WarnFormat("[HYPERGRID LINKER]: Coordinates <{0},{1}> are already occupied by region {2} with uuid {3}",
  147. Util.WorldToRegionLoc((uint)xloc), Util.WorldToRegionLoc((uint)yloc),
  148. region.RegionName, region.RegionID);
  149. regInfo = null;
  150. return false;
  151. }
  152. regInfo = new GridRegion()
  153. {
  154. HttpPort = (uint)rurl.Port,
  155. ExternalHostName = rurl.Host,
  156. ServerURI = rurl.HostUrl,
  157. RegionName = rurl.RegionName,
  158. RegionLocX = xloc,
  159. RegionLocY = yloc,
  160. ScopeID = scopeID,
  161. EstateOwner = ownerID,
  162. InternalEndPoint = dummyIP
  163. };
  164. // Finally, link it
  165. ulong handle = 0;
  166. UUID regionID = UUID.Zero;
  167. string externalName = string.Empty;
  168. string imageURL = string.Empty;
  169. int sizeX = (int)Constants.RegionSize;
  170. int sizeY = (int)Constants.RegionSize;
  171. if (!m_GatekeeperConnector.LinkRegion(regInfo, out regionID, out handle, out externalName, out imageURL, out string reason, out sizeX, out sizeY))
  172. return false;
  173. if (regionID == UUID.Zero)
  174. {
  175. m_log.Warn("[HYPERGRID LINKER]: Unable to link region: " + reason);
  176. return false;
  177. }
  178. region = m_GridService.GetRegionByUUID(scopeID, regionID);
  179. if (region != null)
  180. {
  181. m_log.DebugFormat("[HYPERGRID LINKER]: Region already exists in coordinates <{0},{1}>",
  182. Util.WorldToRegionLoc((uint)region.RegionLocX), Util.WorldToRegionLoc((uint)region.RegionLocY));
  183. regInfo = region;
  184. return true;
  185. }
  186. regInfo.RegionID = regionID;
  187. regInfo.RegionSizeX = sizeX;
  188. regInfo.RegionSizeY = sizeY;
  189. if (externalName == string.Empty)
  190. regInfo.RegionName = regInfo.ServerURI;
  191. else
  192. regInfo.RegionName = externalName;
  193. m_log.DebugFormat("[HYPERGRID LINKER]: naming linked region {0}, handle {1}", regInfo.RegionName, handle.ToString());
  194. // Get the map image
  195. regInfo.TerrainImage = GetMapImage(regionID, imageURL);
  196. // Store the origin's coordinates somewhere
  197. regInfo.RegionSecret = handle.ToString();
  198. AddHyperlinkRegion(regInfo, handle);
  199. m_log.InfoFormat("[HYPERGRID LINKER]: Successfully linked to region {0} at <{1},{2}> with image {3}",
  200. regInfo.RegionName, Util.WorldToRegionLoc((uint)regInfo.RegionLocX), Util.WorldToRegionLoc((uint)regInfo.RegionLocY), regInfo.TerrainImage);
  201. return true;
  202. }
  203. private static Random random = new Random();
  204. // From the command line link-region (obsolete) and the map
  205. private GridRegion TryLinkRegionToCoords(UUID scopeID, string mapName, int xloc, int yloc, out string reason)
  206. {
  207. return TryLinkRegionToCoords(scopeID, mapName, xloc, yloc, UUID.Zero, out reason);
  208. }
  209. public bool IsLocalGrid(string UriHost)
  210. {
  211. return m_ThisGridInfo.IsLocalGrid(UriHost) == 1;
  212. }
  213. public GridRegion TryLinkRegionToCoords(UUID scopeID, string mapName, int xloc, int yloc, UUID ownerID, out string reason)
  214. {
  215. reason = string.Empty;
  216. GridRegion regInfo = null;
  217. string serverURI = string.Empty;
  218. string regionName = string.Empty;
  219. if (!Util.buildHGRegionURI(mapName, out serverURI, out regionName))
  220. {
  221. reason = "Wrong URI format for link-region";
  222. return null;
  223. }
  224. if (TryCreateLink(scopeID, xloc, yloc, regionName, 0, null, serverURI, ownerID, out regInfo, out reason))
  225. {
  226. regInfo.RegionName = serverURI + regionName;
  227. return regInfo;
  228. }
  229. return null;
  230. }
  231. private bool TryCreateLink(UUID scopeID, int xloc, int yloc, string remoteRegionName, uint externalPort, string externalHostName, UUID ownerID, out GridRegion regInfo, out string reason)
  232. {
  233. return TryCreateLink(scopeID, xloc, yloc, remoteRegionName, externalPort, externalHostName, null, ownerID, out regInfo, out reason);
  234. }
  235. private bool TryCreateLink(UUID scopeID, int xloc, int yloc, string remoteRegionName, uint externalPort, string externalHostName, string serverURI, UUID ownerID, out GridRegion regInfo, out string reason)
  236. {
  237. lock (this)
  238. {
  239. return TryCreateLinkImpl(scopeID, xloc, yloc, remoteRegionName, externalPort, externalHostName, serverURI, ownerID, out regInfo, out reason);
  240. }
  241. }
  242. private bool TryCreateLinkImpl(UUID scopeID, int xloc, int yloc, string remoteRegionName, uint externalPort, string externalHostName, string serverURI, UUID ownerID, out GridRegion regInfo, out string reason)
  243. {
  244. m_log.InfoFormat("[HYPERGRID LINKER]: Link to {0} {1}, in <{2},{3}>",
  245. ((serverURI == null) ? (externalHostName + ":" + externalPort) : serverURI),
  246. remoteRegionName, Util.WorldToRegionLoc((uint)xloc), Util.WorldToRegionLoc((uint)yloc));
  247. reason = string.Empty;
  248. Uri uri = null;
  249. regInfo = new GridRegion();
  250. if (externalPort > 0)
  251. regInfo.HttpPort = externalPort;
  252. else
  253. regInfo.HttpPort = 80;
  254. if (externalHostName != null)
  255. regInfo.ExternalHostName = externalHostName;
  256. else
  257. regInfo.ExternalHostName = "0.0.0.0";
  258. if (serverURI != null)
  259. {
  260. regInfo.ServerURI = serverURI;
  261. try
  262. {
  263. uri = new Uri(serverURI);
  264. regInfo.ExternalHostName = uri.Host;
  265. regInfo.HttpPort = (uint)uri.Port;
  266. }
  267. catch {}
  268. }
  269. if (remoteRegionName != string.Empty)
  270. regInfo.RegionName = remoteRegionName;
  271. regInfo.RegionLocX = xloc;
  272. regInfo.RegionLocY = yloc;
  273. regInfo.ScopeID = scopeID;
  274. regInfo.EstateOwner = ownerID;
  275. if(IsLocalGrid(regInfo.ServerURI))
  276. {
  277. m_log.InfoFormat("[HYPERGRID LINKER]: Cannot hyperlink to regions on the same grid");
  278. reason = "Cannot hyperlink to regions on the same grid";
  279. return false;
  280. }
  281. // Check for free coordinates
  282. GridRegion region = m_GridService.GetRegionByPosition(regInfo.ScopeID, regInfo.RegionLocX, regInfo.RegionLocY);
  283. if (region != null)
  284. {
  285. m_log.WarnFormat("[HYPERGRID LINKER]: Coordinates <{0},{1}> are already occupied by region {2} with uuid {3}",
  286. Util.WorldToRegionLoc((uint)regInfo.RegionLocX), Util.WorldToRegionLoc((uint)regInfo.RegionLocY),
  287. region.RegionName, region.RegionID);
  288. reason = "Coordinates are already in use";
  289. return false;
  290. }
  291. try
  292. {
  293. regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0);
  294. }
  295. catch (Exception e)
  296. {
  297. m_log.Warn("[HYPERGRID LINKER]: Wrong format for link-region: " + e.Message);
  298. reason = "Internal error";
  299. return false;
  300. }
  301. // Finally, link it
  302. ulong handle = 0;
  303. UUID regionID = UUID.Zero;
  304. string externalName = string.Empty;
  305. string imageURL = string.Empty;
  306. int sizeX = (int)Constants.RegionSize;
  307. int sizeY = (int)Constants.RegionSize;
  308. if (!m_GatekeeperConnector.LinkRegion(regInfo, out regionID, out handle, out externalName, out imageURL, out reason, out sizeX, out sizeY))
  309. return false;
  310. if (regionID == UUID.Zero)
  311. {
  312. m_log.Warn("[HYPERGRID LINKER]: Unable to link region");
  313. reason = "Remote region could not be found";
  314. return false;
  315. }
  316. region = m_GridService.GetRegionByUUID(scopeID, regionID);
  317. if (region != null)
  318. {
  319. m_log.DebugFormat("[HYPERGRID LINKER]: Region already exists in coordinates <{0},{1}>",
  320. Util.WorldToRegionLoc((uint)region.RegionLocX), Util.WorldToRegionLoc((uint)region.RegionLocY));
  321. regInfo = region;
  322. return true;
  323. }
  324. // We are now performing this check for each individual teleport in the EntityTransferModule instead. This
  325. // allows us to give better feedback when teleports fail because of the distance reason (which can't be
  326. // done here) and it also hypergrid teleports that are within range (possibly because the source grid
  327. // itself has regions that are very far apart).
  328. // uint x, y;
  329. // if (m_Check4096 && !Check4096(handle, out x, out y))
  330. // {
  331. // //RemoveHyperlinkRegion(regInfo.RegionID);
  332. // reason = "Region is too far (" + x + ", " + y + ")";
  333. // m_log.Info("[HYPERGRID LINKER]: Unable to link, region is too far (" + x + ", " + y + ")");
  334. // //return false;
  335. // }
  336. regInfo.RegionID = regionID;
  337. regInfo.RegionSizeX = sizeX;
  338. regInfo.RegionSizeY = sizeY;
  339. if (externalName == string.Empty)
  340. regInfo.RegionName = regInfo.ServerURI;
  341. else
  342. regInfo.RegionName = externalName;
  343. m_log.DebugFormat("[HYPERGRID LINKER]: naming linked region {0}, handle {1}", regInfo.RegionName, handle.ToString());
  344. // Get the map image
  345. regInfo.TerrainImage = GetMapImage(regionID, imageURL);
  346. // Store the origin's coordinates somewhere
  347. regInfo.RegionSecret = handle.ToString();
  348. AddHyperlinkRegion(regInfo, handle);
  349. m_log.InfoFormat("[HYPERGRID LINKER]: Successfully linked to region {0} at <{1},{2}> with image {3}",
  350. regInfo.RegionName, Util.WorldToRegionLoc((uint)regInfo.RegionLocX), Util.WorldToRegionLoc((uint)regInfo.RegionLocY), regInfo.TerrainImage);
  351. return true;
  352. }
  353. public bool TryUnlinkRegion(string mapName)
  354. {
  355. m_log.DebugFormat("[HYPERGRID LINKER]: Request to unlink {0}", mapName);
  356. GridRegion regInfo = null;
  357. List<RegionData> regions = m_Database.Get(Util.EscapeForLike(mapName), m_ScopeID);
  358. if (regions != null && regions.Count > 0)
  359. {
  360. OpenSim.Framework.RegionFlags rflags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(regions[0].Data["flags"]);
  361. if ((rflags & OpenSim.Framework.RegionFlags.Hyperlink) != 0)
  362. {
  363. regInfo = new GridRegion();
  364. regInfo.RegionID = regions[0].RegionID;
  365. regInfo.ScopeID = m_ScopeID;
  366. }
  367. }
  368. if (regInfo != null)
  369. {
  370. RemoveHyperlinkRegion(regInfo.RegionID);
  371. return true;
  372. }
  373. else
  374. {
  375. m_log.InfoFormat("[HYPERGRID LINKER]: Region {0} not found", mapName);
  376. return false;
  377. }
  378. }
  379. // Not currently used
  380. // /// <summary>
  381. // /// Cope with this viewer limitation.
  382. // /// </summary>
  383. // /// <param name="regInfo"></param>
  384. // /// <returns></returns>
  385. // public bool Check4096(ulong realHandle, out uint x, out uint y)
  386. // {
  387. // uint ux = 0, uy = 0;
  388. // Utils.LongToUInts(realHandle, out ux, out uy);
  389. // x = Util.WorldToRegionLoc(ux);
  390. // y = Util.WorldToRegionLoc(uy);
  391. //
  392. // const uint limit = Util.RegionToWorldLoc(4096 - 1);
  393. // uint xmin = ux - limit;
  394. // uint xmax = ux + limit;
  395. // uint ymin = uy - limit;
  396. // uint ymax = uy + limit;
  397. // // World map boundary checks
  398. // if (xmin < 0 || xmin > ux)
  399. // xmin = 0;
  400. // if (xmax > int.MaxValue || xmax < ux)
  401. // xmax = int.MaxValue;
  402. // if (ymin < 0 || ymin > uy)
  403. // ymin = 0;
  404. // if (ymax > int.MaxValue || ymax < uy)
  405. // ymax = int.MaxValue;
  406. //
  407. // // Check for any regions that are within the possible teleport range to the linked region
  408. // List<GridRegion> regions = m_GridService.GetRegionRange(m_ScopeID, (int)xmin, (int)xmax, (int)ymin, (int)ymax);
  409. // if (regions.Count == 0)
  410. // {
  411. // return false;
  412. // }
  413. // else
  414. // {
  415. // // Check for regions which are not linked regions
  416. // List<GridRegion> hyperlinks = m_GridService.GetHyperlinks(m_ScopeID);
  417. // IEnumerable<GridRegion> availableRegions = regions.Except(hyperlinks);
  418. // if (availableRegions.Count() == 0)
  419. // return false;
  420. // }
  421. //
  422. // return true;
  423. // }
  424. private void AddHyperlinkRegion(GridRegion regionInfo, ulong regionHandle)
  425. {
  426. RegionData rdata = m_GridService.RegionInfo2RegionData(regionInfo);
  427. int flags = (int)OpenSim.Framework.RegionFlags.Hyperlink + (int)OpenSim.Framework.RegionFlags.NoDirectLogin + (int)OpenSim.Framework.RegionFlags.RegionOnline;
  428. rdata.Data["flags"] = flags.ToString();
  429. m_Database.Store(rdata);
  430. }
  431. private void RemoveHyperlinkRegion(UUID regionID)
  432. {
  433. m_Database.Delete(regionID);
  434. }
  435. public UUID GetMapImage(UUID regionID, string imageURL)
  436. {
  437. return m_GatekeeperConnector.GetMapImage(regionID, imageURL, m_MapTileDirectory);
  438. }
  439. #endregion
  440. #region Console Commands
  441. public void HandleShow(string module, string[] cmd)
  442. {
  443. if (cmd.Length != 2)
  444. {
  445. MainConsole.Instance.Output("Syntax: show hyperlinks");
  446. return;
  447. }
  448. List<RegionData> regions = m_Database.GetHyperlinks(UUID.Zero);
  449. if (regions == null || regions.Count < 1)
  450. {
  451. MainConsole.Instance.Output("No hyperlinks");
  452. return;
  453. }
  454. MainConsole.Instance.Output("Region Name");
  455. MainConsole.Instance.Output("Location Region UUID");
  456. MainConsole.Instance.Output(new string('-', 72));
  457. foreach (RegionData r in regions)
  458. {
  459. MainConsole.Instance.Output(
  460. String.Format("{0}\n{2,-32} {1}\n",
  461. r.RegionName, r.RegionID,
  462. String.Format("{0},{1} ({2},{3})", r.posX, r.posY,
  463. Util.WorldToRegionLoc((uint)r.posX), Util.WorldToRegionLoc((uint)r.posY)
  464. )
  465. )
  466. );
  467. }
  468. return;
  469. }
  470. public void RunCommand(string module, string[] cmdparams)
  471. {
  472. List<string> args = new List<string>(cmdparams);
  473. if (args.Count < 1)
  474. return;
  475. string command = args[0];
  476. args.RemoveAt(0);
  477. cmdparams = args.ToArray();
  478. RunHGCommand(command, cmdparams);
  479. }
  480. private void RunLinkRegionCommand(string[] cmdparams)
  481. {
  482. int xloc, yloc;
  483. string serverURI;
  484. string remoteName = null;
  485. xloc = (int)Util.RegionToWorldLoc((uint)Convert.ToInt32(cmdparams[0]));
  486. yloc = (int)Util.RegionToWorldLoc((uint)Convert.ToInt32(cmdparams[1]));
  487. serverURI = cmdparams[2];
  488. if (cmdparams.Length > 3)
  489. remoteName = string.Join(" ", cmdparams, 3, cmdparams.Length - 3);
  490. string reason = string.Empty;
  491. GridRegion regInfo;
  492. if (TryCreateLink(UUID.Zero, xloc, yloc, remoteName, 0, null, serverURI, UUID.Zero, out regInfo, out reason))
  493. MainConsole.Instance.Output("Hyperlink established");
  494. else
  495. MainConsole.Instance.Output("Failed to link region: " + reason);
  496. }
  497. private void RunHGCommand(string command, string[] cmdparams)
  498. {
  499. if (command.Equals("link-mapping"))
  500. {
  501. if (cmdparams.Length == 2)
  502. {
  503. try
  504. {
  505. m_autoMappingX = Convert.ToUInt32(cmdparams[0]);
  506. m_autoMappingY = Convert.ToUInt32(cmdparams[1]);
  507. m_enableAutoMapping = true;
  508. }
  509. catch (Exception)
  510. {
  511. m_autoMappingX = 0;
  512. m_autoMappingY = 0;
  513. m_enableAutoMapping = false;
  514. }
  515. }
  516. }
  517. else if (command.Equals("link-region"))
  518. {
  519. if (cmdparams.Length < 3)
  520. {
  521. if ((cmdparams.Length == 1) || (cmdparams.Length == 2))
  522. {
  523. LoadXmlLinkFile(cmdparams);
  524. }
  525. else
  526. {
  527. LinkRegionCmdUsage();
  528. }
  529. return;
  530. }
  531. //this should be the prefererred way of setting up hg links now
  532. if (cmdparams[2].StartsWith("http"))
  533. {
  534. RunLinkRegionCommand(cmdparams);
  535. }
  536. else if (cmdparams[2].Contains(":"))
  537. {
  538. // New format
  539. string[] parts = cmdparams[2].Split(':');
  540. if (parts.Length > 2)
  541. {
  542. // Insert remote region name
  543. ArrayList parameters = new ArrayList(cmdparams);
  544. parameters.Insert(3, parts[2]);
  545. cmdparams = (string[])parameters.ToArray(typeof(string));
  546. }
  547. cmdparams[2] = "http://" + parts[0] + ':' + parts[1];
  548. RunLinkRegionCommand(cmdparams);
  549. }
  550. else
  551. {
  552. // old format
  553. GridRegion regInfo;
  554. uint xloc, yloc;
  555. uint externalPort;
  556. string externalHostName;
  557. try
  558. {
  559. xloc = Convert.ToUInt32(cmdparams[0]);
  560. yloc = Convert.ToUInt32(cmdparams[1]);
  561. externalPort = Convert.ToUInt32(cmdparams[3]);
  562. externalHostName = cmdparams[2];
  563. //internalPort = Convert.ToUInt32(cmdparams[4]);
  564. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  565. }
  566. catch (Exception e)
  567. {
  568. MainConsole.Instance.Output("[HGrid] Wrong format for link-region command: " + e.Message);
  569. LinkRegionCmdUsage();
  570. return;
  571. }
  572. // Convert cell coordinates given by the user to meters
  573. xloc = Util.RegionToWorldLoc(xloc);
  574. yloc = Util.RegionToWorldLoc(yloc);
  575. string reason = string.Empty;
  576. if (TryCreateLink(UUID.Zero, (int)xloc, (int)yloc,
  577. string.Empty, externalPort, externalHostName, UUID.Zero, out regInfo, out reason))
  578. {
  579. // What is this? The GridRegion instance will be discarded anyway,
  580. // which effectively ignores any local name given with the command.
  581. //if (cmdparams.Length >= 5)
  582. //{
  583. // regInfo.RegionName = "";
  584. // for (int i = 4; i < cmdparams.Length; i++)
  585. // regInfo.RegionName += cmdparams[i] + " ";
  586. //}
  587. }
  588. }
  589. return;
  590. }
  591. else if (command.Equals("unlink-region"))
  592. {
  593. if (cmdparams.Length < 1)
  594. {
  595. UnlinkRegionCmdUsage();
  596. return;
  597. }
  598. string region = string.Join(" ", cmdparams);
  599. if (TryUnlinkRegion(region))
  600. MainConsole.Instance.Output("Successfully unlinked " + region);
  601. else
  602. MainConsole.Instance.Output("Unable to unlink " + region + ", region not found.");
  603. }
  604. }
  605. private void LoadXmlLinkFile(string[] cmdparams)
  606. {
  607. //use http://www.hgurl.com/hypergrid.xml for test
  608. try
  609. {
  610. XmlReader r = XmlReader.Create(cmdparams[0]);
  611. XmlConfigSource cs = new XmlConfigSource(r);
  612. string[] excludeSections = null;
  613. if (cmdparams.Length == 2)
  614. {
  615. if (cmdparams[1].ToLower().StartsWith("excludelist:"))
  616. {
  617. string excludeString = cmdparams[1].ToLower();
  618. excludeString = excludeString.Remove(0, 12);
  619. char[] splitter = { ';' };
  620. excludeSections = excludeString.Split(splitter);
  621. }
  622. }
  623. for (int i = 0; i < cs.Configs.Count; i++)
  624. {
  625. bool skip = false;
  626. if ((excludeSections != null) && (excludeSections.Length > 0))
  627. {
  628. for (int n = 0; n < excludeSections.Length; n++)
  629. {
  630. if (excludeSections[n] == cs.Configs[i].Name.ToLower())
  631. {
  632. skip = true;
  633. break;
  634. }
  635. }
  636. }
  637. if (!skip)
  638. {
  639. ReadLinkFromConfig(cs.Configs[i]);
  640. }
  641. }
  642. }
  643. catch (Exception e)
  644. {
  645. m_log.Error(e.ToString());
  646. }
  647. }
  648. private void ReadLinkFromConfig(IConfig config)
  649. {
  650. GridRegion regInfo;
  651. uint xloc, yloc;
  652. uint externalPort;
  653. string externalHostName;
  654. uint realXLoc, realYLoc;
  655. xloc = Convert.ToUInt32(config.GetString("xloc", "0"));
  656. yloc = Convert.ToUInt32(config.GetString("yloc", "0"));
  657. externalPort = Convert.ToUInt32(config.GetString("externalPort", "0"));
  658. externalHostName = config.GetString("externalHostName", "");
  659. realXLoc = Convert.ToUInt32(config.GetString("real-xloc", "0"));
  660. realYLoc = Convert.ToUInt32(config.GetString("real-yloc", "0"));
  661. if (m_enableAutoMapping)
  662. {
  663. xloc = (xloc % 100) + m_autoMappingX;
  664. yloc = (yloc % 100) + m_autoMappingY;
  665. }
  666. if (((realXLoc == 0) && (realYLoc == 0)) ||
  667. (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) &&
  668. ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896))))
  669. {
  670. xloc = Util.RegionToWorldLoc(xloc);
  671. yloc = Util.RegionToWorldLoc(yloc);
  672. string reason = string.Empty;
  673. if (TryCreateLink(UUID.Zero, (int)xloc, (int)yloc,
  674. string.Empty, externalPort, externalHostName, UUID.Zero, out regInfo, out reason))
  675. {
  676. regInfo.RegionName = config.GetString("localName", "");
  677. }
  678. else
  679. MainConsole.Instance.Output("Unable to link " + externalHostName + ": " + reason);
  680. }
  681. }
  682. private void LinkRegionCmdUsage()
  683. {
  684. MainConsole.Instance.Output("Usage: link-region <Xloc> <Yloc> <ServerURI> [<RemoteRegionName>]");
  685. MainConsole.Instance.Output("Usage (deprecated): link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>]");
  686. MainConsole.Instance.Output("Usage (deprecated): link-region <Xloc> <Yloc> <HostName> <HttpPort> [<LocalName>]");
  687. MainConsole.Instance.Output("Usage: link-region <URI_of_xml> [<exclude>]");
  688. }
  689. private void UnlinkRegionCmdUsage()
  690. {
  691. MainConsole.Instance.Output("Usage: unlink-region <LocalName>");
  692. }
  693. #endregion
  694. }
  695. }