SimulatorFeaturesModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 bool m_ExportSupported = false;
  70. private bool m_doScriptSyntax;
  71. static private object m_scriptSyntaxLock = new object();
  72. static private UUID m_scriptSyntaxID = UUID.Zero;
  73. static private byte[] m_scriptSyntaxXML = null;
  74. #region ISharedRegionModule Members
  75. public void Initialise(IConfigSource source)
  76. {
  77. IConfig config = source.Configs["SimulatorFeatures"];
  78. m_doScriptSyntax = true;
  79. if (config != null)
  80. {
  81. m_ExportSupported = config.GetBoolean("ExportSupported", m_ExportSupported);
  82. m_doScriptSyntax = config.GetBoolean("ScriptSyntax", m_doScriptSyntax);
  83. }
  84. ReadScriptSyntax();
  85. AddDefaultFeatures();
  86. }
  87. public void AddRegion(Scene s)
  88. {
  89. m_scene = s;
  90. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  91. m_scene.RegisterModuleInterface<ISimulatorFeaturesModule>(this);
  92. }
  93. public void RemoveRegion(Scene s)
  94. {
  95. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  96. }
  97. public void RegionLoaded(Scene s)
  98. {
  99. GetGridExtraFeatures(s);
  100. }
  101. public void Close() { }
  102. public string Name { get { return "SimulatorFeaturesModule"; } }
  103. public Type ReplaceableInterface
  104. {
  105. get { return null; }
  106. }
  107. #endregion
  108. /// <summary>
  109. /// Add default features
  110. /// </summary>
  111. /// <remarks>
  112. /// TODO: These should be added from other modules rather than hardcoded.
  113. /// </remarks>
  114. private void AddDefaultFeatures()
  115. {
  116. lock (m_features)
  117. {
  118. m_features["MeshRezEnabled"] = true;
  119. m_features["MeshUploadEnabled"] = true;
  120. m_features["MeshXferEnabled"] = true;
  121. m_features["BakesOnMeshEnabled"] = true;
  122. m_features["PhysicsMaterialsEnabled"] = true;
  123. OSDMap typesMap = new OSDMap();
  124. typesMap["convex"] = true;
  125. typesMap["none"] = true;
  126. typesMap["prim"] = true;
  127. m_features["PhysicsShapeTypes"] = typesMap;
  128. if(m_doScriptSyntax && m_scriptSyntaxID != UUID.Zero)
  129. m_features["LSLSyntaxId"] = OSD.FromUUID(m_scriptSyntaxID);
  130. OSDMap meshAnim = new OSDMap();
  131. meshAnim["AnimatedObjectMaxTris"] = OSD.FromInteger(150000);
  132. meshAnim["MaxAgentAnimatedObjectAttachments"] = OSD.FromInteger(2);
  133. m_features["AnimatedObjects"] = meshAnim;
  134. m_features["MaxAgentAttachments"] = OSD.FromInteger(Constants.MaxAgentAttachments);
  135. m_features["MaxAgentGroupsBasic"] = OSD.FromInteger(Constants.MaxAgentGroups);
  136. m_features["MaxAgentGroupsPremium"] = OSD.FromInteger(Constants.MaxAgentGroups);
  137. // Extra information for viewers that want to use it
  138. // TODO: Take these out of here into their respective modules, like map-server-url
  139. OSDMap extrasMap;
  140. if(m_features.ContainsKey("OpenSimExtras"))
  141. {
  142. extrasMap = (OSDMap)m_features["OpenSimExtras"];
  143. }
  144. else
  145. extrasMap = new OSDMap();
  146. extrasMap["AvatarSkeleton"] = true;
  147. extrasMap["AnimationSet"] = true;
  148. extrasMap["MinSimHeight"] = Constants.MinSimulationHeight;
  149. extrasMap["MaxSimHeight"] = Constants.MaxSimulationHeight;
  150. extrasMap["MinHeightmap"] = Constants.MinTerrainHeightmap;
  151. extrasMap["MaxHeightmap"] = Constants.MaxTerrainHeightmap;
  152. if (m_ExportSupported)
  153. extrasMap["ExportSupported"] = true;
  154. if (extrasMap.Count > 0)
  155. m_features["OpenSimExtras"] = extrasMap;
  156. }
  157. }
  158. public void RegisterCaps(UUID agentID, Caps caps)
  159. {
  160. caps.RegisterSimpleHandler("SimulatorFeatures",
  161. new SimpleStreamHandler("/" + UUID.Random(),
  162. delegate (IOSHttpRequest request, IOSHttpResponse response)
  163. {
  164. HandleSimulatorFeaturesRequest(request, response, agentID);
  165. }));
  166. if (m_doScriptSyntax && m_scriptSyntaxID != UUID.Zero && m_scriptSyntaxXML != null)
  167. {
  168. caps.RegisterSimpleHandler("LSLSyntax",
  169. new SimpleStreamHandler("/" + UUID.Random(), HandleSyntaxRequest));
  170. }
  171. }
  172. public void AddFeature(string name, OSD value)
  173. {
  174. lock (m_features)
  175. m_features[name] = value;
  176. }
  177. public void AddOpenSimExtraFeature(string name, OSD value)
  178. {
  179. lock (m_features)
  180. {
  181. OSDMap extrasMap;
  182. if (m_features.TryGetValue("OpenSimExtras", out OSD extra))
  183. extrasMap = extra as OSDMap;
  184. else
  185. {
  186. extrasMap = new OSDMap();
  187. }
  188. extrasMap[name] = value;
  189. m_features["OpenSimExtras"] = extrasMap;
  190. }
  191. }
  192. public bool RemoveFeature(string name)
  193. {
  194. lock (m_features)
  195. return m_features.Remove(name);
  196. }
  197. public bool TryGetFeature(string name, out OSD value)
  198. {
  199. lock (m_features)
  200. return m_features.TryGetValue(name, out value);
  201. }
  202. public bool TryGetOpenSimExtraFeature(string name, out OSD value)
  203. {
  204. value = null;
  205. lock (m_features)
  206. {
  207. if (!m_features.TryGetValue("OpenSimExtras", out OSD extra))
  208. return false;
  209. if(!(extra is OSDMap))
  210. return false;
  211. return (extra as OSDMap).TryGetValue(name, out value);
  212. }
  213. }
  214. public OSDMap GetFeatures()
  215. {
  216. lock (m_features)
  217. return new OSDMap(m_features);
  218. }
  219. private OSDMap DeepCopy()
  220. {
  221. // This isn't the cheapest way of doing this but the rate
  222. // of occurrence is low (on sim entry only) and it's a sure
  223. // way to get a true deep copy.
  224. OSD copy = OSDParser.DeserializeLLSDXml(OSDParser.SerializeLLSDXmlString(m_features));
  225. return (OSDMap)copy;
  226. }
  227. private void HandleSimulatorFeaturesRequest(IOSHttpRequest request, IOSHttpResponse response, UUID agentID)
  228. {
  229. // m_log.DebugFormat("[SIMULATOR FEATURES MODULE]: SimulatorFeatures request");
  230. if (request.HttpMethod != "GET")
  231. {
  232. response.StatusCode = (int)HttpStatusCode.NotFound;
  233. return;
  234. }
  235. ScenePresence sp = m_scene.GetScenePresence(agentID);
  236. if (sp == null)
  237. {
  238. response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
  239. response.AddHeader("Retry-After", "5");
  240. return;
  241. }
  242. OSDMap copy = DeepCopy();
  243. // Let's add the agentID to the destination guide, if it is expecting that.
  244. if (copy.ContainsKey("OpenSimExtras") && ((OSDMap)(copy["OpenSimExtras"])).ContainsKey("destination-guide-url"))
  245. ((OSDMap)copy["OpenSimExtras"])["destination-guide-url"] = Replace(((OSDMap)copy["OpenSimExtras"])["destination-guide-url"], "[USERID]", agentID.ToString());
  246. OnSimulatorFeaturesRequest?.Invoke(agentID, ref copy);
  247. //Send back data
  248. response.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeLLSDXmlString(copy));
  249. response.StatusCode = (int)HttpStatusCode.OK;
  250. }
  251. private void HandleSyntaxRequest(IOSHttpRequest request, IOSHttpResponse response)
  252. {
  253. if (request.HttpMethod != "GET" || m_scriptSyntaxXML == null)
  254. {
  255. response.StatusCode = (int)HttpStatusCode.NotFound;
  256. return;
  257. }
  258. response.RawBuffer = m_scriptSyntaxXML;
  259. response.StatusCode = (int)HttpStatusCode.OK;
  260. }
  261. /// <summary>
  262. /// Gets the grid extra features.
  263. /// </summary>
  264. /// <param name='featuresURI'>
  265. /// The URI Robust uses to handle the get_extra_features request
  266. /// </param>
  267. private void GetGridExtraFeatures(Scene scene)
  268. {
  269. Dictionary<string, object> extraFeatures = scene.GridService.GetExtraFeatures();
  270. if (extraFeatures.ContainsKey("Result") && extraFeatures["Result"] != null && extraFeatures["Result"].ToString() == "Failure")
  271. {
  272. m_log.WarnFormat("[SIMULATOR FEATURES MODULE]: Unable to retrieve grid-wide features");
  273. return;
  274. }
  275. GridInfo ginfo = scene.SceneGridInfo;
  276. lock (m_features)
  277. {
  278. OSDMap extrasMap;
  279. if (m_features.TryGetValue("OpenSimExtras", out OSD extra))
  280. extrasMap = extra as OSDMap;
  281. else
  282. {
  283. extrasMap = new OSDMap();
  284. }
  285. foreach (string key in extraFeatures.Keys)
  286. {
  287. string val = (string)extraFeatures[key];
  288. switch(key)
  289. {
  290. case "GridName":
  291. ginfo.GridName = val;
  292. break;
  293. case "GridNick":
  294. ginfo.GridNick = val;
  295. break;
  296. case "GridURL":
  297. ginfo.GridUrl = val;
  298. break;
  299. case "GridURLAlias":
  300. string[] vals = val.Split(',');
  301. if(vals.Length > 0)
  302. ginfo.GridUrlAlias = vals;
  303. break;
  304. case "search-server-url":
  305. ginfo.SearchURL = val;
  306. break;
  307. case "destination-guide-url":
  308. ginfo.DestinationGuideURL = val;
  309. break;
  310. /* keep this local to avoid issues with diferent modules
  311. case "currency-base-uri":
  312. ginfo.EconomyURL = val;
  313. break;
  314. */
  315. default:
  316. extrasMap[key] = val;
  317. if (key == "ExportSupported")
  318. {
  319. bool.TryParse(val, out m_ExportSupported);
  320. }
  321. break;
  322. }
  323. }
  324. m_features["OpenSimExtras"] = extrasMap;
  325. }
  326. }
  327. private string Replace(string url, string substring, string replacement)
  328. {
  329. if (!String.IsNullOrEmpty(url) && url.Contains(substring))
  330. return url.Replace(substring, replacement);
  331. return url;
  332. }
  333. private void ReadScriptSyntax()
  334. {
  335. lock(m_scriptSyntaxLock)
  336. {
  337. if(!m_doScriptSyntax || m_scriptSyntaxID != UUID.Zero)
  338. return;
  339. if(!File.Exists("ScriptSyntax.xml"))
  340. return;
  341. try
  342. {
  343. using (StreamReader sr = File.OpenText("ScriptSyntax.xml"))
  344. {
  345. StringBuilder sb = new StringBuilder(400*1024);
  346. string s="";
  347. char[] trimc = new char[] {' ','\t', '\n', '\r'};
  348. s = sr.ReadLine();
  349. if(s == null)
  350. return;
  351. s = s.Trim(trimc);
  352. UUID id;
  353. if(!UUID.TryParse(s,out id))
  354. return;
  355. while ((s = sr.ReadLine()) != null)
  356. {
  357. s = s.Trim(trimc);
  358. if (String.IsNullOrEmpty(s) || s.StartsWith("<!--"))
  359. continue;
  360. sb.Append(s);
  361. }
  362. m_scriptSyntaxXML = Util.UTF8.GetBytes(sb.ToString());
  363. m_scriptSyntaxID = id;
  364. }
  365. }
  366. catch
  367. {
  368. m_log.Error("[SIMULATOR FEATURES MODULE] fail read ScriptSyntax.xml file");
  369. m_scriptSyntaxID = UUID.Zero;
  370. m_scriptSyntaxXML = null;
  371. }
  372. }
  373. }
  374. }
  375. }