1
0

SimulatorFeaturesModule.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.Reflection;
  30. using log4net;
  31. using Nini.Config;
  32. using Mono.Addins;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. using Caps = OpenSim.Framework.Capabilities.Caps;
  41. namespace OpenSim.Region.ClientStack.Linden
  42. {
  43. /// <summary>
  44. /// SimulatorFeatures capability.
  45. /// </summary>
  46. /// <remarks>
  47. /// This is required for uploading Mesh.
  48. /// Since is accepts an open-ended response, we also send more information
  49. /// for viewers that care to interpret it.
  50. ///
  51. /// NOTE: Part of this code was adapted from the Aurora project, specifically
  52. /// the normal part of the response in the capability handler.
  53. /// </remarks>
  54. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SimulatorFeaturesModule")]
  55. public class SimulatorFeaturesModule : ISharedRegionModule, ISimulatorFeaturesModule
  56. {
  57. // private static readonly ILog m_log =
  58. // LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  59. public event SimulatorFeaturesRequestDelegate OnSimulatorFeaturesRequest;
  60. private Scene m_scene;
  61. /// <summary>
  62. /// Simulator features
  63. /// </summary>
  64. private OSDMap m_features = new OSDMap();
  65. private string m_SearchURL = string.Empty;
  66. private bool m_ExportSupported = false;
  67. #region ISharedRegionModule Members
  68. public void Initialise(IConfigSource source)
  69. {
  70. IConfig config = source.Configs["SimulatorFeatures"];
  71. if (config != null)
  72. {
  73. m_SearchURL = config.GetString("SearchServerURI", string.Empty);
  74. m_ExportSupported = config.GetBoolean("ExportSupported", m_ExportSupported);
  75. }
  76. AddDefaultFeatures();
  77. }
  78. public void AddRegion(Scene s)
  79. {
  80. m_scene = s;
  81. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  82. m_scene.RegisterModuleInterface<ISimulatorFeaturesModule>(this);
  83. }
  84. public void RemoveRegion(Scene s)
  85. {
  86. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  87. }
  88. public void RegionLoaded(Scene s)
  89. {
  90. }
  91. public void PostInitialise()
  92. {
  93. }
  94. public void Close() { }
  95. public string Name { get { return "SimulatorFeaturesModule"; } }
  96. public Type ReplaceableInterface
  97. {
  98. get { return null; }
  99. }
  100. #endregion
  101. /// <summary>
  102. /// Add default features
  103. /// </summary>
  104. /// <remarks>
  105. /// TODO: These should be added from other modules rather than hardcoded.
  106. /// </remarks>
  107. private void AddDefaultFeatures()
  108. {
  109. lock (m_features)
  110. {
  111. m_features["MeshRezEnabled"] = true;
  112. m_features["MeshUploadEnabled"] = true;
  113. m_features["MeshXferEnabled"] = true;
  114. m_features["PhysicsMaterialsEnabled"] = true;
  115. OSDMap typesMap = new OSDMap();
  116. typesMap["convex"] = true;
  117. typesMap["none"] = true;
  118. typesMap["prim"] = true;
  119. m_features["PhysicsShapeTypes"] = typesMap;
  120. // Extra information for viewers that want to use it
  121. // TODO: Take these out of here into their respective modules, like map-server-url
  122. OSDMap extrasMap = new OSDMap();
  123. if (m_SearchURL != string.Empty)
  124. extrasMap["search-server-url"] = m_SearchURL;
  125. if (m_ExportSupported)
  126. extrasMap["ExportSupported"] = true;
  127. if (extrasMap.Count > 0)
  128. m_features["OpenSimExtras"] = extrasMap;
  129. }
  130. }
  131. public void RegisterCaps(UUID agentID, Caps caps)
  132. {
  133. IRequestHandler reqHandler
  134. = new RestHTTPHandler(
  135. "GET", "/CAPS/" + UUID.Random(),
  136. x => { return HandleSimulatorFeaturesRequest(x, agentID); }, "SimulatorFeatures", agentID.ToString());
  137. caps.RegisterHandler("SimulatorFeatures", reqHandler);
  138. }
  139. public void AddFeature(string name, OSD value)
  140. {
  141. lock (m_features)
  142. m_features[name] = value;
  143. }
  144. public bool RemoveFeature(string name)
  145. {
  146. lock (m_features)
  147. return m_features.Remove(name);
  148. }
  149. public bool TryGetFeature(string name, out OSD value)
  150. {
  151. lock (m_features)
  152. return m_features.TryGetValue(name, out value);
  153. }
  154. public OSDMap GetFeatures()
  155. {
  156. lock (m_features)
  157. return new OSDMap(m_features);
  158. }
  159. private OSDMap DeepCopy()
  160. {
  161. // This isn't the cheapest way of doing this but the rate
  162. // of occurrence is low (on sim entry only) and it's a sure
  163. // way to get a true deep copy.
  164. OSD copy = OSDParser.DeserializeLLSDXml(OSDParser.SerializeLLSDXmlString(m_features));
  165. return (OSDMap)copy;
  166. }
  167. private Hashtable HandleSimulatorFeaturesRequest(Hashtable mDhttpMethod, UUID agentID)
  168. {
  169. // m_log.DebugFormat("[SIMULATOR FEATURES MODULE]: SimulatorFeatures request");
  170. OSDMap copy = DeepCopy();
  171. SimulatorFeaturesRequestDelegate handlerOnSimulatorFeaturesRequest = OnSimulatorFeaturesRequest;
  172. if (handlerOnSimulatorFeaturesRequest != null)
  173. handlerOnSimulatorFeaturesRequest(agentID, ref copy);
  174. //Send back data
  175. Hashtable responsedata = new Hashtable();
  176. responsedata["int_response_code"] = 200;
  177. responsedata["content_type"] = "text/plain";
  178. responsedata["keepalive"] = false;
  179. responsedata["str_response_string"] = OSDParser.SerializeLLSDXmlString(copy);
  180. return responsedata;
  181. }
  182. }
  183. }