HGCommands.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.Reflection;
  30. using System.Xml;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Region.Framework;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Region.Framework.Scenes.Hypergrid;
  39. namespace OpenSim
  40. {
  41. public class HGCommands
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. public static IHyperlink HGServices = null;
  45. private static uint m_autoMappingX = 0;
  46. private static uint m_autoMappingY = 0;
  47. private static bool m_enableAutoMapping = false;
  48. public static Scene CreateScene(RegionInfo regionInfo, AgentCircuitManager circuitManager, CommunicationsManager m_commsManager,
  49. StorageManager storageManager, ModuleLoader m_moduleLoader, ConfigSettings m_configSettings, OpenSimConfigSource m_config, string m_version)
  50. {
  51. HGSceneCommunicationService sceneGridService = new HGSceneCommunicationService(m_commsManager, HGServices);
  52. return
  53. new HGScene(
  54. regionInfo, circuitManager, m_commsManager, sceneGridService, storageManager,
  55. m_moduleLoader, false, m_configSettings.PhysicalPrim,
  56. m_configSettings.See_into_region_from_neighbor, m_config.Source, m_version);
  57. }
  58. public static void RunHGCommand(string command, string[] cmdparams, Scene scene)
  59. {
  60. if (command.Equals("link-mapping"))
  61. {
  62. if (cmdparams.Length == 2)
  63. {
  64. try
  65. {
  66. m_autoMappingX = Convert.ToUInt32(cmdparams[0]);
  67. m_autoMappingY = Convert.ToUInt32(cmdparams[1]);
  68. m_enableAutoMapping = true;
  69. }
  70. catch (Exception)
  71. {
  72. m_autoMappingX = 0;
  73. m_autoMappingY = 0;
  74. m_enableAutoMapping = false;
  75. }
  76. }
  77. }
  78. else if (command.Equals("link-region"))
  79. {
  80. if (cmdparams.Length < 3)
  81. {
  82. if ((cmdparams.Length == 1) || (cmdparams.Length == 2))
  83. {
  84. LoadXmlLinkFile(cmdparams, scene);
  85. }
  86. else
  87. {
  88. LinkRegionCmdUsage();
  89. }
  90. return;
  91. }
  92. if (cmdparams[2].Contains(":"))
  93. {
  94. // New format
  95. uint xloc, yloc;
  96. string mapName;
  97. try
  98. {
  99. xloc = Convert.ToUInt32(cmdparams[0]);
  100. yloc = Convert.ToUInt32(cmdparams[1]);
  101. mapName = cmdparams[2];
  102. if (cmdparams.Length > 3)
  103. for (int i = 3; i < cmdparams.Length; i++)
  104. mapName += " " + cmdparams[i];
  105. m_log.Info(">> MapName: " + mapName);
  106. //internalPort = Convert.ToUInt32(cmdparams[4]);
  107. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  108. }
  109. catch (Exception e)
  110. {
  111. m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message);
  112. LinkRegionCmdUsage();
  113. return;
  114. }
  115. HGHyperlink.TryLinkRegionToCoords(scene, null, mapName, xloc, yloc);
  116. }
  117. else
  118. {
  119. // old format
  120. RegionInfo regInfo;
  121. uint xloc, yloc;
  122. uint externalPort;
  123. string externalHostName;
  124. try
  125. {
  126. xloc = Convert.ToUInt32(cmdparams[0]);
  127. yloc = Convert.ToUInt32(cmdparams[1]);
  128. externalPort = Convert.ToUInt32(cmdparams[3]);
  129. externalHostName = cmdparams[2];
  130. //internalPort = Convert.ToUInt32(cmdparams[4]);
  131. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  132. }
  133. catch (Exception e)
  134. {
  135. m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message);
  136. LinkRegionCmdUsage();
  137. return;
  138. }
  139. //if (TryCreateLink(xloc, yloc, externalPort, externalHostName, out regInfo))
  140. if (HGHyperlink.TryCreateLink(scene, null, xloc, yloc, "", externalPort, externalHostName, out regInfo))
  141. {
  142. if (cmdparams.Length >= 5)
  143. {
  144. regInfo.RegionName = "";
  145. for (int i = 4; i < cmdparams.Length; i++)
  146. regInfo.RegionName += cmdparams[i] + " ";
  147. }
  148. }
  149. }
  150. return;
  151. }
  152. else if (command.Equals("unlink-region"))
  153. {
  154. if (cmdparams.Length < 1)
  155. {
  156. UnlinkRegionCmdUsage();
  157. return;
  158. }
  159. if (HGHyperlink.TryUnlinkRegion(scene, cmdparams[0]))
  160. m_log.InfoFormat("[HGrid]: Successfully unlinked {0}", cmdparams[0]);
  161. else
  162. m_log.InfoFormat("[HGrid]: Unable to unlink {0}, region not found", cmdparams[0]);
  163. }
  164. }
  165. private static void LoadXmlLinkFile(string[] cmdparams, Scene scene)
  166. {
  167. //use http://www.hgurl.com/hypergrid.xml for test
  168. try
  169. {
  170. XmlReader r = XmlReader.Create(cmdparams[0]);
  171. XmlConfigSource cs = new XmlConfigSource(r);
  172. string[] excludeSections = null;
  173. if (cmdparams.Length == 2)
  174. {
  175. if (cmdparams[1].ToLower().StartsWith("excludelist:"))
  176. {
  177. string excludeString = cmdparams[1].ToLower();
  178. excludeString = excludeString.Remove(0, 12);
  179. char[] splitter = {';'};
  180. excludeSections = excludeString.Split(splitter);
  181. }
  182. }
  183. for (int i = 0; i < cs.Configs.Count; i++)
  184. {
  185. bool skip = false;
  186. if ((excludeSections != null) && (excludeSections.Length > 0))
  187. {
  188. for (int n = 0; n < excludeSections.Length; n++)
  189. {
  190. if (excludeSections[n] == cs.Configs[i].Name.ToLower())
  191. {
  192. skip = true;
  193. break;
  194. }
  195. }
  196. }
  197. if (!skip)
  198. {
  199. ReadLinkFromConfig(cs.Configs[i], scene);
  200. }
  201. }
  202. }
  203. catch (Exception e)
  204. {
  205. m_log.Error(e.ToString());
  206. }
  207. }
  208. private static void ReadLinkFromConfig(IConfig config, Scene scene)
  209. {
  210. RegionInfo regInfo;
  211. uint xloc, yloc;
  212. uint externalPort;
  213. string externalHostName;
  214. uint realXLoc, realYLoc;
  215. xloc = Convert.ToUInt32(config.GetString("xloc", "0"));
  216. yloc = Convert.ToUInt32(config.GetString("yloc", "0"));
  217. externalPort = Convert.ToUInt32(config.GetString("externalPort", "0"));
  218. externalHostName = config.GetString("externalHostName", "");
  219. realXLoc = Convert.ToUInt32(config.GetString("real-xloc", "0"));
  220. realYLoc = Convert.ToUInt32(config.GetString("real-yloc", "0"));
  221. if (m_enableAutoMapping)
  222. {
  223. xloc = (uint) ((xloc%100) + m_autoMappingX);
  224. yloc = (uint) ((yloc%100) + m_autoMappingY);
  225. }
  226. if (((realXLoc == 0) && (realYLoc == 0)) ||
  227. (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) &&
  228. ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896))))
  229. {
  230. if (
  231. HGHyperlink.TryCreateLink(scene, null, xloc, yloc, "", externalPort,
  232. externalHostName, out regInfo))
  233. {
  234. regInfo.RegionName = config.GetString("localName", "");
  235. }
  236. }
  237. }
  238. private static void LinkRegionCmdUsage()
  239. {
  240. m_log.Info("Usage: link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>]");
  241. m_log.Info("Usage: link-region <Xloc> <Yloc> <HostName> <HttpPort> [<LocalName>]");
  242. m_log.Info("Usage: link-region <URI_of_xml> [<exclude>]");
  243. }
  244. private static void UnlinkRegionCmdUsage()
  245. {
  246. m_log.Info("Usage: unlink-region <HostName>:<HttpPort>");
  247. m_log.Info("Usage: unlink-region <LocalName>");
  248. }
  249. }
  250. }