SimulatorFeaturesModule.cs 15 KB

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