SimulatorFeaturesModule.cs 17 KB

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