SimulatorFeaturesModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.Reflection;
  31. using log4net;
  32. using Nini.Config;
  33. using Mono.Addins;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. // using OpenSim.Services.Interfaces;
  41. using Caps = OpenSim.Framework.Capabilities.Caps;
  42. namespace OpenSim.Region.ClientStack.Linden
  43. {
  44. /// <summary>
  45. /// SimulatorFeatures capability.
  46. /// </summary>
  47. /// <remarks>
  48. /// This is required for uploading Mesh.
  49. /// Since is accepts an open-ended response, we also send more information
  50. /// for viewers that care to interpret it.
  51. ///
  52. /// NOTE: Part of this code was adapted from the Aurora project, specifically
  53. /// the normal part of the response in the capability handler.
  54. /// </remarks>
  55. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SimulatorFeaturesModule")]
  56. public class SimulatorFeaturesModule : INonSharedRegionModule, ISimulatorFeaturesModule
  57. {
  58. private static readonly ILog m_log =
  59. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  60. public event SimulatorFeaturesRequestDelegate OnSimulatorFeaturesRequest;
  61. private Scene m_scene;
  62. /// <summary>
  63. /// Simulator features
  64. /// </summary>
  65. private OSDMap m_features = new OSDMap();
  66. private string m_SearchURL = string.Empty;
  67. private string m_DestinationGuideURL = string.Empty;
  68. private bool m_ExportSupported = false;
  69. private string m_GridName = string.Empty;
  70. private string m_GridURL = string.Empty;
  71. #region ISharedRegionModule Members
  72. public void Initialise(IConfigSource source)
  73. {
  74. IConfig config = source.Configs["SimulatorFeatures"];
  75. if (config != null)
  76. {
  77. //
  78. // All this is obsolete since getting these features from the grid service!!
  79. // Will be removed after the next release
  80. //
  81. m_SearchURL = config.GetString("SearchServerURI", m_SearchURL);
  82. m_DestinationGuideURL = config.GetString ("DestinationGuideURI", m_DestinationGuideURL);
  83. if (m_DestinationGuideURL == string.Empty) // Make this consistent with the variable in the LoginService config
  84. m_DestinationGuideURL = config.GetString("DestinationGuide", m_DestinationGuideURL);
  85. m_ExportSupported = config.GetBoolean("ExportSupported", m_ExportSupported);
  86. m_GridURL = Util.GetConfigVarFromSections<string>(
  87. source, "GatekeeperURI", new string[] { "Startup", "Hypergrid", "SimulatorFeatures" }, String.Empty);
  88. m_GridName = config.GetString("GridName", string.Empty);
  89. if (m_GridName == string.Empty)
  90. m_GridName = Util.GetConfigVarFromSections<string>(
  91. source, "gridname", new string[] { "GridInfo", "SimulatorFeatures" }, String.Empty);
  92. }
  93. AddDefaultFeatures();
  94. }
  95. public void AddRegion(Scene s)
  96. {
  97. m_scene = s;
  98. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  99. m_scene.RegisterModuleInterface<ISimulatorFeaturesModule>(this);
  100. }
  101. public void RemoveRegion(Scene s)
  102. {
  103. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  104. }
  105. public void RegionLoaded(Scene s)
  106. {
  107. GetGridExtraFeatures(s);
  108. }
  109. public void Close() { }
  110. public string Name { get { return "SimulatorFeaturesModule"; } }
  111. public Type ReplaceableInterface
  112. {
  113. get { return null; }
  114. }
  115. #endregion
  116. /// <summary>
  117. /// Add default features
  118. /// </summary>
  119. /// <remarks>
  120. /// TODO: These should be added from other modules rather than hardcoded.
  121. /// </remarks>
  122. private void AddDefaultFeatures()
  123. {
  124. lock (m_features)
  125. {
  126. m_features["MeshRezEnabled"] = true;
  127. m_features["MeshUploadEnabled"] = true;
  128. m_features["MeshXferEnabled"] = true;
  129. m_features["PhysicsMaterialsEnabled"] = true;
  130. OSDMap typesMap = new OSDMap();
  131. typesMap["convex"] = true;
  132. typesMap["none"] = true;
  133. typesMap["prim"] = true;
  134. m_features["PhysicsShapeTypes"] = typesMap;
  135. // Extra information for viewers that want to use it
  136. // TODO: Take these out of here into their respective modules, like map-server-url
  137. OSDMap extrasMap;
  138. if(m_features.ContainsKey("OpenSimExtras"))
  139. {
  140. extrasMap = (OSDMap)m_features["OpenSimExtras"];
  141. }
  142. else
  143. extrasMap = new OSDMap();
  144. extrasMap["AvatarSkeleton"] = true;
  145. extrasMap["AnimationSet"] = true;
  146. // TODO: Take these out of here into their respective modules, like map-server-url
  147. if (m_SearchURL != string.Empty)
  148. extrasMap["search-server-url"] = m_SearchURL;
  149. if (!string.IsNullOrEmpty(m_DestinationGuideURL))
  150. extrasMap["destination-guide-url"] = m_DestinationGuideURL;
  151. if (m_ExportSupported)
  152. extrasMap["ExportSupported"] = true;
  153. if (m_GridURL != string.Empty)
  154. extrasMap["GridURL"] = m_GridURL;
  155. if (m_GridName != string.Empty)
  156. extrasMap["GridName"] = m_GridName;
  157. if (extrasMap.Count > 0)
  158. m_features["OpenSimExtras"] = extrasMap;
  159. }
  160. }
  161. public void RegisterCaps(UUID agentID, Caps caps)
  162. {
  163. IRequestHandler reqHandler
  164. = new RestHTTPHandler(
  165. "GET", "/CAPS/" + UUID.Random(),
  166. x => { return HandleSimulatorFeaturesRequest(x, agentID); }, "SimulatorFeatures", agentID.ToString());
  167. caps.RegisterHandler("SimulatorFeatures", reqHandler);
  168. }
  169. public void AddFeature(string name, OSD value)
  170. {
  171. lock (m_features)
  172. m_features[name] = value;
  173. }
  174. public bool RemoveFeature(string name)
  175. {
  176. lock (m_features)
  177. return m_features.Remove(name);
  178. }
  179. public bool TryGetFeature(string name, out OSD value)
  180. {
  181. lock (m_features)
  182. return m_features.TryGetValue(name, out value);
  183. }
  184. public OSDMap GetFeatures()
  185. {
  186. lock (m_features)
  187. return new OSDMap(m_features);
  188. }
  189. private OSDMap DeepCopy()
  190. {
  191. // This isn't the cheapest way of doing this but the rate
  192. // of occurrence is low (on sim entry only) and it's a sure
  193. // way to get a true deep copy.
  194. OSD copy = OSDParser.DeserializeLLSDXml(OSDParser.SerializeLLSDXmlString(m_features));
  195. return (OSDMap)copy;
  196. }
  197. private Hashtable HandleSimulatorFeaturesRequest(Hashtable mDhttpMethod, UUID agentID)
  198. {
  199. // m_log.DebugFormat("[SIMULATOR FEATURES MODULE]: SimulatorFeatures request");
  200. OSDMap copy = DeepCopy();
  201. // Let's add the agentID to the destination guide, if it is expecting that.
  202. if (copy.ContainsKey("OpenSimExtras") && ((OSDMap)(copy["OpenSimExtras"])).ContainsKey("destination-guide-url"))
  203. ((OSDMap)copy["OpenSimExtras"])["destination-guide-url"] = Replace(((OSDMap)copy["OpenSimExtras"])["destination-guide-url"], "[USERID]", agentID.ToString());
  204. SimulatorFeaturesRequestDelegate handlerOnSimulatorFeaturesRequest = OnSimulatorFeaturesRequest;
  205. if (handlerOnSimulatorFeaturesRequest != null)
  206. handlerOnSimulatorFeaturesRequest(agentID, ref copy);
  207. //Send back data
  208. Hashtable responsedata = new Hashtable();
  209. responsedata["int_response_code"] = 200;
  210. responsedata["content_type"] = "text/plain";
  211. responsedata["keepalive"] = false;
  212. responsedata["str_response_string"] = OSDParser.SerializeLLSDXmlString(copy);
  213. return responsedata;
  214. }
  215. /// <summary>
  216. /// Gets the grid extra features.
  217. /// </summary>
  218. /// <param name='featuresURI'>
  219. /// The URI Robust uses to handle the get_extra_features request
  220. /// </param>
  221. private void GetGridExtraFeatures(Scene scene)
  222. {
  223. Dictionary<string, object> extraFeatures = scene.GridService.GetExtraFeatures();
  224. if (extraFeatures.ContainsKey("Result") && extraFeatures["Result"] != null && extraFeatures["Result"].ToString() == "Failure")
  225. {
  226. m_log.WarnFormat("[SIMULATOR FEATURES MODULE]: Unable to retrieve grid-wide features");
  227. return;
  228. }
  229. lock (m_features)
  230. {
  231. OSDMap extrasMap = new OSDMap();
  232. foreach(string key in extraFeatures.Keys)
  233. {
  234. extrasMap[key] = (string)extraFeatures[key];
  235. if (key == "ExportSupported")
  236. {
  237. bool.TryParse(extraFeatures[key].ToString(), out m_ExportSupported);
  238. }
  239. }
  240. m_features["OpenSimExtras"] = extrasMap;
  241. }
  242. }
  243. private string Replace(string url, string substring, string replacement)
  244. {
  245. if (!String.IsNullOrEmpty(url) && url.Contains(substring))
  246. return url.Replace(substring, replacement);
  247. return url;
  248. }
  249. }
  250. }