SimulatorFeaturesModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. m_features["MirrorsEnabled"] = true;
  135. m_features["PhysicsMaterialsEnabled"] = true;
  136. m_features["PhysicsShapeTypes"] = new OSDMap()
  137. {
  138. ["convex"] = true,
  139. ["none"] = true,
  140. ["prim"] = true
  141. };
  142. // Extra information for viewers that want to use it
  143. OSDMap extrasMap = m_features.TryGetValue("OpenSimExtras", out OSD oe) ? oe as OSDMap : new OSDMap();
  144. extrasMap["AvatarSkeleton"] = true;
  145. extrasMap["AnimationSet"] = true;
  146. extrasMap["MinSimHeight"] = Constants.MinSimulationHeight;
  147. extrasMap["MaxSimHeight"] = Constants.MaxSimulationHeight;
  148. extrasMap["MinHeightmap"] = Constants.MinTerrainHeightmap;
  149. extrasMap["MaxHeightmap"] = Constants.MaxTerrainHeightmap;
  150. if (m_ExportSupported)
  151. extrasMap["ExportSupported"] = true;
  152. m_features["OpenSimExtras"] = extrasMap;
  153. }
  154. }
  155. public void RegisterCaps(UUID agentID, Caps caps)
  156. {
  157. caps.RegisterSimpleHandler("SimulatorFeatures",
  158. new SimpleStreamHandler("/" + UUID.Random(),
  159. delegate (IOSHttpRequest request, IOSHttpResponse response)
  160. {
  161. HandleSimulatorFeaturesRequest(request, response, caps);
  162. }));
  163. if (m_doScriptSyntax && !m_scriptSyntaxID.IsZero() && m_scriptSyntaxXML != null)
  164. {
  165. caps.RegisterSimpleHandler("LSLSyntax",
  166. new SimpleStreamHandler("/" + UUID.Random(), HandleSyntaxRequest));
  167. }
  168. }
  169. public void AddFeature(string name, OSD value)
  170. {
  171. lock (m_features)
  172. m_features[name] = value;
  173. }
  174. public void AddOpenSimExtraFeature(string name, OSD value)
  175. {
  176. lock (m_features)
  177. {
  178. OSDMap extrasMap;
  179. if (m_features.TryGetValue("OpenSimExtras", out OSD extra))
  180. extrasMap = extra as OSDMap;
  181. else
  182. {
  183. extrasMap = new OSDMap();
  184. m_features["OpenSimExtras"] = extrasMap;
  185. }
  186. extrasMap[name] = value;
  187. }
  188. }
  189. public bool RemoveFeature(string name)
  190. {
  191. lock (m_features)
  192. return m_features.Remove(name);
  193. }
  194. public bool TryGetFeature(string name, out OSD value)
  195. {
  196. lock (m_features)
  197. return m_features.TryGetValue(name, out value);
  198. }
  199. public bool TryGetOpenSimExtraFeature(string name, out OSD value)
  200. {
  201. value = null;
  202. lock (m_features)
  203. {
  204. if (m_features.TryGetValue("OpenSimExtras", out OSD extra) && extra is OSDMap exm)
  205. return exm.TryGetValue(name, out value);
  206. }
  207. return false;
  208. }
  209. public bool OpenSimExtraFeatureContains(string name)
  210. {
  211. lock (m_features)
  212. {
  213. if (m_features.TryGetValue("OpenSimExtras", out OSD extra) && extra is OSDMap exm)
  214. return exm.ContainsKey(name);
  215. }
  216. return false;
  217. }
  218. public OSDMap GetFeatures()
  219. {
  220. lock (m_features)
  221. return new OSDMap(m_features);
  222. }
  223. private OSDMap DeepCopy()
  224. {
  225. // This isn't the cheapest way of doing this but the rate
  226. // of occurrence is low (on sim entry only) and it's a sure
  227. // way to get a true deep copy.
  228. OSD copy = OSDParser.DeserializeLLSDXml(OSDParser.SerializeLLSDXmlToBytes(m_features));
  229. return (OSDMap)copy;
  230. }
  231. private void HandleSimulatorFeaturesRequest(IOSHttpRequest request, IOSHttpResponse response, Caps caps)
  232. {
  233. // m_log.DebugFormat("[SIMULATOR FEATURES MODULE]: SimulatorFeatures request");
  234. if (request.HttpMethod != "GET")
  235. {
  236. response.StatusCode = (int)HttpStatusCode.NotFound;
  237. return;
  238. }
  239. OSDMap copy = DeepCopy();
  240. if ((caps.Flags & Caps.CapsFlags.TPBR) != 0)
  241. {
  242. copy["PBRMaterialSwatchEnabled"] = true;
  243. copy["PBRTerrainEnabled"] = true;
  244. }
  245. // Let's add the agentID to the destination guide, if it is expecting that.
  246. if(copy.TryGetValue("OpenSimExtras", out OSD oe))
  247. {
  248. if(((OSDMap)oe).TryGetValue("destination-guide-url", out OSD dgl))
  249. {
  250. ((OSDMap)oe)["destination-guide-url"] = Replace(dgl.AsString(), "[USERID]", caps.AgentID.ToString());
  251. }
  252. }
  253. if(OnSimulatorFeaturesRequest != null)
  254. {
  255. foreach(SimulatorFeaturesRequestDelegate sd in OnSimulatorFeaturesRequest.GetInvocationList())
  256. try
  257. {
  258. sd?.Invoke(caps.AgentID, ref copy);
  259. }
  260. catch { }
  261. }
  262. //Send back data
  263. response.RawBuffer = OSDParser.SerializeLLSDXmlToBytes(copy);
  264. response.StatusCode = (int)HttpStatusCode.OK;
  265. }
  266. private void HandleSyntaxRequest(IOSHttpRequest request, IOSHttpResponse response)
  267. {
  268. if (request.HttpMethod != "GET" || m_scriptSyntaxXML == null)
  269. {
  270. response.StatusCode = (int)HttpStatusCode.NotFound;
  271. return;
  272. }
  273. response.RawBuffer = m_scriptSyntaxXML;
  274. response.StatusCode = (int)HttpStatusCode.OK;
  275. }
  276. /// <summary>
  277. /// Gets the grid extra features.
  278. /// </summary>
  279. /// <param name='featuresURI'>
  280. /// The URI Robust uses to handle the get_extra_features request
  281. /// </param>
  282. private void GetGridExtraFeatures(Scene scene)
  283. {
  284. Dictionary<string, object> extraFeatures = scene.GridService.GetExtraFeatures();
  285. if (extraFeatures.ContainsKey("Result") && extraFeatures["Result"] != null && extraFeatures["Result"].ToString() == "Failure")
  286. {
  287. m_log.WarnFormat("[SIMULATOR FEATURES MODULE]: Unable to retrieve grid-wide features");
  288. return;
  289. }
  290. GridInfo ginfo = scene.SceneGridInfo;
  291. lock (m_features)
  292. {
  293. OSDMap extrasMap;
  294. if (m_features.TryGetValue("OpenSimExtras", out OSD extra))
  295. extrasMap = extra as OSDMap;
  296. else
  297. {
  298. extrasMap = new OSDMap();
  299. }
  300. foreach (string key in extraFeatures.Keys)
  301. {
  302. string val = (string)extraFeatures[key];
  303. switch(key)
  304. {
  305. case "GridName":
  306. ginfo.GridName = val;
  307. break;
  308. case "GridNick":
  309. ginfo.GridNick = val;
  310. break;
  311. case "GridURL":
  312. ginfo.GridUrl = val;
  313. break;
  314. case "GridURLAlias":
  315. string[] vals = val.Split(',');
  316. if(vals.Length > 0)
  317. ginfo.GridUrlAlias = vals;
  318. break;
  319. case "search-server-url":
  320. ginfo.SearchURL = val;
  321. break;
  322. case "destination-guide-url":
  323. ginfo.DestinationGuideURL = val;
  324. break;
  325. case "currency-base-uri":
  326. // keep this local to avoid issues with diferent modules
  327. // ginfo.EconomyURL = val;
  328. break;
  329. default:
  330. if (key == "ExportSupported")
  331. {
  332. _ = bool.TryParse(val, out m_ExportSupported);
  333. extrasMap[key] = m_ExportSupported;
  334. }
  335. else
  336. extrasMap[key] = val;
  337. break;
  338. }
  339. }
  340. m_features["OpenSimExtras"] = extrasMap;
  341. }
  342. }
  343. private string Replace(string url, string substring, string replacement)
  344. {
  345. if (!String.IsNullOrEmpty(url) && url.Contains(substring))
  346. return url.Replace(substring, replacement);
  347. return url;
  348. }
  349. private void ReadScriptSyntax()
  350. {
  351. lock(m_scriptSyntaxLock)
  352. {
  353. if(!m_doScriptSyntax || !m_scriptSyntaxID.IsZero())
  354. return;
  355. if(!File.Exists("ScriptSyntax.xml"))
  356. return;
  357. try
  358. {
  359. using (StreamReader sr = File.OpenText("ScriptSyntax.xml"))
  360. {
  361. StringBuilder sb = new(400*1024);
  362. char[] trimc = new char[] {' ','\t', '\n', '\r'};
  363. string s = sr.ReadLine();
  364. if(s is null)
  365. return;
  366. s = s.Trim(trimc);
  367. if(!UUID.TryParse(s, out UUID id))
  368. return;
  369. while ((s = sr.ReadLine()) is not null)
  370. {
  371. s = s.Trim(trimc);
  372. if (String.IsNullOrEmpty(s) || s.StartsWith("<!--"))
  373. continue;
  374. sb.Append(s);
  375. }
  376. m_scriptSyntaxXML = Util.UTF8.GetBytes(sb.ToString());
  377. m_scriptSyntaxID = id;
  378. }
  379. }
  380. catch
  381. {
  382. m_log.Error("[SIMULATOR FEATURES MODULE] fail read ScriptSyntax.xml file");
  383. m_scriptSyntaxID = UUID.Zero;
  384. m_scriptSyntaxXML = null;
  385. }
  386. }
  387. }
  388. }
  389. }