1
0

SimulatorFeaturesModule.cs 17 KB

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