1
0

SimulatorFeaturesModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 : ISharedRegionModule, 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. // These are normaly set in their respective modules
  78. m_SearchURL = config.GetString("SearchServerURI", m_SearchURL);
  79. m_DestinationGuideURL = config.GetString ("DestinationGuideURI", m_DestinationGuideURL);
  80. if (m_DestinationGuideURL == string.Empty) // Make this consistent with the variable in the LoginService config
  81. m_DestinationGuideURL = config.GetString("DestinationGuide", m_DestinationGuideURL);
  82. m_ExportSupported = config.GetBoolean("ExportSupported", m_ExportSupported);
  83. m_GridURL = Util.GetConfigVarFromSections<string>(
  84. source, "GatekeeperURI", new string[] { "Startup", "Hypergrid", "SimulatorFeatures" }, String.Empty);
  85. m_GridName = config.GetString("GridName", string.Empty);
  86. if (m_GridName == string.Empty)
  87. m_GridName = Util.GetConfigVarFromSections<string>(
  88. source, "gridname", new string[] { "GridInfo", "SimulatorFeatures" }, String.Empty);
  89. }
  90. AddDefaultFeatures();
  91. }
  92. public void AddRegion(Scene s)
  93. {
  94. m_scene = s;
  95. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  96. m_scene.RegisterModuleInterface<ISimulatorFeaturesModule>(this);
  97. }
  98. public void RemoveRegion(Scene s)
  99. {
  100. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  101. }
  102. public void RegionLoaded(Scene s)
  103. {
  104. GetGridExtraFeatures(s);
  105. }
  106. public void PostInitialise()
  107. {
  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. if (m_SearchURL != string.Empty)
  145. extrasMap["search-server-url"] = m_SearchURL;
  146. if (!string.IsNullOrEmpty(m_DestinationGuideURL))
  147. extrasMap["destination-guide-url"] = m_DestinationGuideURL;
  148. if (m_ExportSupported)
  149. extrasMap["ExportSupported"] = true;
  150. if (m_GridURL != string.Empty)
  151. extrasMap["GridURL"] = m_GridURL;
  152. if (m_GridName != string.Empty)
  153. extrasMap["GridName"] = m_GridName;
  154. if (extrasMap.Count > 0)
  155. m_features["OpenSimExtras"] = extrasMap;
  156. }
  157. }
  158. public void RegisterCaps(UUID agentID, Caps caps)
  159. {
  160. IRequestHandler reqHandler
  161. = new RestHTTPHandler(
  162. "GET", "/CAPS/" + UUID.Random(),
  163. x => { return HandleSimulatorFeaturesRequest(x, agentID); }, "SimulatorFeatures", agentID.ToString());
  164. caps.RegisterHandler("SimulatorFeatures", reqHandler);
  165. }
  166. public void AddFeature(string name, OSD value)
  167. {
  168. lock (m_features)
  169. m_features[name] = value;
  170. }
  171. public bool RemoveFeature(string name)
  172. {
  173. lock (m_features)
  174. return m_features.Remove(name);
  175. }
  176. public bool TryGetFeature(string name, out OSD value)
  177. {
  178. lock (m_features)
  179. return m_features.TryGetValue(name, out value);
  180. }
  181. public OSDMap GetFeatures()
  182. {
  183. lock (m_features)
  184. return new OSDMap(m_features);
  185. }
  186. private OSDMap DeepCopy()
  187. {
  188. // This isn't the cheapest way of doing this but the rate
  189. // of occurrence is low (on sim entry only) and it's a sure
  190. // way to get a true deep copy.
  191. OSD copy = OSDParser.DeserializeLLSDXml(OSDParser.SerializeLLSDXmlString(m_features));
  192. return (OSDMap)copy;
  193. }
  194. private Hashtable HandleSimulatorFeaturesRequest(Hashtable mDhttpMethod, UUID agentID)
  195. {
  196. m_log.DebugFormat("[SIMULATOR FEATURES MODULE]: SimulatorFeatures request");
  197. OSDMap copy = DeepCopy();
  198. // Let's add the agentID to the destination guide, if it is expecting that.
  199. if (copy.ContainsKey("OpenSimExtras") && ((OSDMap)(copy["OpenSimExtras"])).ContainsKey("destination-guide-url"))
  200. ((OSDMap)copy["OpenSimExtras"])["destination-guide-url"] = Replace(((OSDMap)copy["OpenSimExtras"])["destination-guide-url"], "[USERID]", agentID.ToString());
  201. SimulatorFeaturesRequestDelegate handlerOnSimulatorFeaturesRequest = OnSimulatorFeaturesRequest;
  202. if (handlerOnSimulatorFeaturesRequest != null)
  203. handlerOnSimulatorFeaturesRequest(agentID, ref copy);
  204. //Send back data
  205. Hashtable responsedata = new Hashtable();
  206. responsedata["int_response_code"] = 200;
  207. responsedata["content_type"] = "text/plain";
  208. responsedata["keepalive"] = false;
  209. responsedata["str_response_string"] = OSDParser.SerializeLLSDXmlString(copy);
  210. return responsedata;
  211. }
  212. /// <summary>
  213. /// Gets the grid extra features.
  214. /// </summary>
  215. /// <param name='featuresURI'>
  216. /// The URI Robust uses to handle the get_extra_features request
  217. /// </param>
  218. private void GetGridExtraFeatures(Scene scene)
  219. {
  220. Dictionary<string, object> extraFeatures = scene.GridService.GetExtraFeatures();
  221. if (extraFeatures.ContainsKey("Result") && extraFeatures["Result"] != null && extraFeatures["Result"].ToString() == "Failure")
  222. {
  223. m_log.WarnFormat("[SIMULATOR FEATURES MODULE]: Unable to retrieve grid-wide features");
  224. return;
  225. }
  226. lock (m_features)
  227. {
  228. OSDMap extrasMap = new OSDMap();
  229. foreach(string key in extraFeatures.Keys)
  230. {
  231. extrasMap[key] = (string)extraFeatures[key];
  232. if (key == "ExportSupported")
  233. {
  234. bool.TryParse(extraFeatures[key].ToString(), out m_ExportSupported);
  235. }
  236. }
  237. m_features["OpenSimExtras"] = extrasMap;
  238. }
  239. }
  240. private string Replace(string url, string substring, string replacement)
  241. {
  242. if (!String.IsNullOrEmpty(url) && url.Contains(substring))
  243. return url.Replace(substring, replacement);
  244. return url;
  245. }
  246. }
  247. }