SimulatorFeaturesModule.cs 17 KB

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