JsonStoreScriptModule.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Copyright (c) Contributors
  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 OpenSim 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 Mono.Addins;
  28. using System;
  29. using System.Reflection;
  30. using System.Threading;
  31. using System.Text;
  32. using System.Net;
  33. using System.Net.Sockets;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Region.Framework.Scenes.Scripting;
  42. using System.Collections.Generic;
  43. using System.Text.RegularExpressions;
  44. using PermissionMask = OpenSim.Framework.PermissionMask;
  45. namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
  46. {
  47. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "JsonStoreScriptModule")]
  48. public class JsonStoreScriptModule : INonSharedRegionModule
  49. {
  50. private static readonly ILog m_log =
  51. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private IConfig m_config = null;
  53. private bool m_enabled = false;
  54. private Scene m_scene = null;
  55. private IScriptModuleComms m_comms;
  56. private IJsonStoreModule m_store;
  57. private Dictionary<UUID,HashSet<UUID>> m_scriptStores = new Dictionary<UUID,HashSet<UUID>>();
  58. #region Region Module interface
  59. // -----------------------------------------------------------------
  60. /// <summary>
  61. /// Name of this shared module is it's class name
  62. /// </summary>
  63. // -----------------------------------------------------------------
  64. public string Name
  65. {
  66. get { return this.GetType().Name; }
  67. }
  68. // -----------------------------------------------------------------
  69. /// <summary>
  70. /// Initialise this shared module
  71. /// </summary>
  72. /// <param name="scene">this region is getting initialised</param>
  73. /// <param name="source">nini config, we are not using this</param>
  74. // -----------------------------------------------------------------
  75. public void Initialise(IConfigSource config)
  76. {
  77. try
  78. {
  79. if ((m_config = config.Configs["JsonStore"]) == null)
  80. {
  81. // There is no configuration, the module is disabled
  82. // m_log.InfoFormat("[JsonStoreScripts] no configuration info");
  83. return;
  84. }
  85. m_enabled = m_config.GetBoolean("Enabled", m_enabled);
  86. }
  87. catch (Exception e)
  88. {
  89. m_log.ErrorFormat("[JsonStoreScripts]: initialization error: {0}", e.Message);
  90. return;
  91. }
  92. if (m_enabled)
  93. m_log.DebugFormat("[JsonStoreScripts]: module is enabled");
  94. }
  95. // -----------------------------------------------------------------
  96. /// <summary>
  97. /// everything is loaded, perform post load configuration
  98. /// </summary>
  99. // -----------------------------------------------------------------
  100. public void PostInitialise()
  101. {
  102. }
  103. // -----------------------------------------------------------------
  104. /// <summary>
  105. /// Nothing to do on close
  106. /// </summary>
  107. // -----------------------------------------------------------------
  108. public void Close()
  109. {
  110. }
  111. // -----------------------------------------------------------------
  112. /// <summary>
  113. /// </summary>
  114. // -----------------------------------------------------------------
  115. public void AddRegion(Scene scene)
  116. {
  117. scene.EventManager.OnScriptReset += HandleScriptReset;
  118. scene.EventManager.OnRemoveScript += HandleScriptReset;
  119. }
  120. // -----------------------------------------------------------------
  121. /// <summary>
  122. /// </summary>
  123. // -----------------------------------------------------------------
  124. public void RemoveRegion(Scene scene)
  125. {
  126. scene.EventManager.OnScriptReset -= HandleScriptReset;
  127. scene.EventManager.OnRemoveScript -= HandleScriptReset;
  128. // need to remove all references to the scene in the subscription
  129. // list to enable full garbage collection of the scene object
  130. }
  131. // -----------------------------------------------------------------
  132. /// <summary>
  133. /// </summary>
  134. // -----------------------------------------------------------------
  135. private void HandleScriptReset(uint localID, UUID itemID)
  136. {
  137. HashSet<UUID> stores;
  138. lock (m_scriptStores)
  139. {
  140. if (! m_scriptStores.TryGetValue(itemID, out stores))
  141. return;
  142. m_scriptStores.Remove(itemID);
  143. }
  144. foreach (UUID id in stores)
  145. m_store.DestroyStore(id);
  146. }
  147. // -----------------------------------------------------------------
  148. /// <summary>
  149. /// Called when all modules have been added for a region. This is
  150. /// where we hook up events
  151. /// </summary>
  152. // -----------------------------------------------------------------
  153. public void RegionLoaded(Scene scene)
  154. {
  155. if (m_enabled)
  156. {
  157. m_scene = scene;
  158. m_comms = m_scene.RequestModuleInterface<IScriptModuleComms>();
  159. if (m_comms == null)
  160. {
  161. m_log.ErrorFormat("[JsonStoreScripts]: ScriptModuleComms interface not defined");
  162. m_enabled = false;
  163. return;
  164. }
  165. m_store = m_scene.RequestModuleInterface<IJsonStoreModule>();
  166. if (m_store == null)
  167. {
  168. m_log.ErrorFormat("[JsonStoreScripts]: JsonModule interface not defined");
  169. m_enabled = false;
  170. return;
  171. }
  172. try
  173. {
  174. m_comms.RegisterScriptInvocations(this);
  175. m_comms.RegisterConstants(this);
  176. }
  177. catch (Exception e)
  178. {
  179. // See http://opensimulator.org/mantis/view.php?id=5971 for more information
  180. m_log.WarnFormat("[JsonStoreScripts]: script method registration failed; {0}", e.Message);
  181. m_enabled = false;
  182. }
  183. }
  184. }
  185. /// -----------------------------------------------------------------
  186. /// <summary>
  187. /// </summary>
  188. // -----------------------------------------------------------------
  189. public Type ReplaceableInterface
  190. {
  191. get { return null; }
  192. }
  193. #endregion
  194. #region ScriptConstantsInterface
  195. [ScriptConstant]
  196. public static readonly int JSON_NODETYPE_UNDEF = (int)JsonStoreNodeType.Undefined;
  197. [ScriptConstant]
  198. public static readonly int JSON_NODETYPE_OBJECT = (int)JsonStoreNodeType.Object;
  199. [ScriptConstant]
  200. public static readonly int JSON_NODETYPE_ARRAY = (int)JsonStoreNodeType.Array;
  201. [ScriptConstant]
  202. public static readonly int JSON_NODETYPE_VALUE = (int)JsonStoreNodeType.Value;
  203. [ScriptConstant]
  204. public static readonly int JSON_VALUETYPE_UNDEF = (int)JsonStoreValueType.Undefined;
  205. [ScriptConstant]
  206. public static readonly int JSON_VALUETYPE_BOOLEAN = (int)JsonStoreValueType.Boolean;
  207. [ScriptConstant]
  208. public static readonly int JSON_VALUETYPE_INTEGER = (int)JsonStoreValueType.Integer;
  209. [ScriptConstant]
  210. public static readonly int JSON_VALUETYPE_FLOAT = (int)JsonStoreValueType.Float;
  211. [ScriptConstant]
  212. public static readonly int JSON_VALUETYPE_STRING = (int)JsonStoreValueType.String;
  213. #endregion
  214. #region ScriptInvocationInteface
  215. // -----------------------------------------------------------------
  216. /// <summary>
  217. ///
  218. /// </summary>
  219. // -----------------------------------------------------------------
  220. [ScriptInvocation]
  221. public UUID JsonAttachObjectStore(UUID hostID, UUID scriptID)
  222. {
  223. UUID uuid = UUID.Zero;
  224. if (! m_store.AttachObjectStore(hostID))
  225. GenerateRuntimeError("Failed to create Json store");
  226. return hostID;
  227. }
  228. // -----------------------------------------------------------------
  229. /// <summary>
  230. ///
  231. /// </summary>
  232. // -----------------------------------------------------------------
  233. [ScriptInvocation]
  234. public UUID JsonCreateStore(UUID hostID, UUID scriptID, string value)
  235. {
  236. UUID uuid = UUID.Zero;
  237. if (! m_store.CreateStore(value, ref uuid))
  238. GenerateRuntimeError("Failed to create Json store");
  239. lock (m_scriptStores)
  240. {
  241. if (! m_scriptStores.ContainsKey(scriptID))
  242. m_scriptStores[scriptID] = new HashSet<UUID>();
  243. m_scriptStores[scriptID].Add(uuid);
  244. }
  245. return uuid;
  246. }
  247. // -----------------------------------------------------------------
  248. /// <summary>
  249. ///
  250. /// </summary>
  251. // -----------------------------------------------------------------
  252. [ScriptInvocation]
  253. public int JsonDestroyStore(UUID hostID, UUID scriptID, UUID storeID)
  254. {
  255. lock(m_scriptStores)
  256. {
  257. if (m_scriptStores.ContainsKey(scriptID))
  258. m_scriptStores[scriptID].Remove(storeID);
  259. }
  260. return m_store.DestroyStore(storeID) ? 1 : 0;
  261. }
  262. // -----------------------------------------------------------------
  263. /// <summary>
  264. ///
  265. /// </summary>
  266. // -----------------------------------------------------------------
  267. [ScriptInvocation]
  268. public int JsonTestStore(UUID hostID, UUID scriptID, UUID storeID)
  269. {
  270. return m_store.TestStore(storeID) ? 1 : 0;
  271. }
  272. // -----------------------------------------------------------------
  273. /// <summary>
  274. ///
  275. /// </summary>
  276. // -----------------------------------------------------------------
  277. [ScriptInvocation]
  278. public UUID JsonRezAtRoot(UUID hostID, UUID scriptID, string item, Vector3 pos, Vector3 vel, Quaternion rot, string param)
  279. {
  280. UUID reqID = UUID.Random();
  281. Util.FireAndForget(o => DoJsonRezObject(hostID, scriptID, reqID, item, pos, vel, rot, param));
  282. return reqID;
  283. }
  284. // -----------------------------------------------------------------
  285. /// <summary>
  286. ///
  287. /// </summary>
  288. // -----------------------------------------------------------------
  289. [ScriptInvocation]
  290. public UUID JsonReadNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, string notecardIdentifier)
  291. {
  292. UUID reqID = UUID.Random();
  293. Util.FireAndForget(o => DoJsonReadNotecard(reqID, hostID, scriptID, storeID, path, notecardIdentifier));
  294. return reqID;
  295. }
  296. // -----------------------------------------------------------------
  297. /// <summary>
  298. ///
  299. /// </summary>
  300. // -----------------------------------------------------------------
  301. [ScriptInvocation]
  302. public UUID JsonWriteNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, string name)
  303. {
  304. UUID reqID = UUID.Random();
  305. Util.FireAndForget(delegate(object o) { DoJsonWriteNotecard(reqID,hostID,scriptID,storeID,path,name); });
  306. return reqID;
  307. }
  308. // -----------------------------------------------------------------
  309. /// <summary>
  310. ///
  311. /// </summary>
  312. // -----------------------------------------------------------------
  313. [ScriptInvocation]
  314. public string JsonList2Path(UUID hostID, UUID scriptID, object[] pathlist)
  315. {
  316. string ipath = ConvertList2Path(pathlist);
  317. string opath;
  318. if (JsonStore.CanonicalPathExpression(ipath,out opath))
  319. return opath;
  320. // This won't parse if passed to the other routines as opposed to
  321. // returning an empty string which is a valid path and would overwrite
  322. // the entire store
  323. return "**INVALID**";
  324. }
  325. // -----------------------------------------------------------------
  326. /// <summary>
  327. ///
  328. /// </summary>
  329. // -----------------------------------------------------------------
  330. [ScriptInvocation]
  331. public int JsonGetNodeType(UUID hostID, UUID scriptID, UUID storeID, string path)
  332. {
  333. return (int)m_store.GetNodeType(storeID,path);
  334. }
  335. // -----------------------------------------------------------------
  336. /// <summary>
  337. ///
  338. /// </summary>
  339. // -----------------------------------------------------------------
  340. [ScriptInvocation]
  341. public int JsonGetValueType(UUID hostID, UUID scriptID, UUID storeID, string path)
  342. {
  343. return (int)m_store.GetValueType(storeID,path);
  344. }
  345. // -----------------------------------------------------------------
  346. /// <summary>
  347. ///
  348. /// </summary>
  349. // -----------------------------------------------------------------
  350. [ScriptInvocation]
  351. public int JsonSetValue(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
  352. {
  353. return m_store.SetValue(storeID,path,value,false) ? 1 : 0;
  354. }
  355. [ScriptInvocation]
  356. public int JsonSetJson(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
  357. {
  358. return m_store.SetValue(storeID,path,value,true) ? 1 : 0;
  359. }
  360. // -----------------------------------------------------------------
  361. /// <summary>
  362. ///
  363. /// </summary>
  364. // -----------------------------------------------------------------
  365. [ScriptInvocation]
  366. public int JsonRemoveValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  367. {
  368. return m_store.RemoveValue(storeID,path) ? 1 : 0;
  369. }
  370. // -----------------------------------------------------------------
  371. /// <summary>
  372. ///
  373. /// </summary>
  374. // -----------------------------------------------------------------
  375. [ScriptInvocation]
  376. public int JsonGetArrayLength(UUID hostID, UUID scriptID, UUID storeID, string path)
  377. {
  378. return m_store.GetArrayLength(storeID,path);
  379. }
  380. // -----------------------------------------------------------------
  381. /// <summary>
  382. ///
  383. /// </summary>
  384. // -----------------------------------------------------------------
  385. [ScriptInvocation]
  386. public string JsonGetValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  387. {
  388. string value = String.Empty;
  389. m_store.GetValue(storeID,path,false,out value);
  390. return value;
  391. }
  392. [ScriptInvocation]
  393. public string JsonGetJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  394. {
  395. string value = String.Empty;
  396. m_store.GetValue(storeID,path,true, out value);
  397. return value;
  398. }
  399. // -----------------------------------------------------------------
  400. /// <summary>
  401. ///
  402. /// </summary>
  403. // -----------------------------------------------------------------
  404. [ScriptInvocation]
  405. public UUID JsonTakeValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  406. {
  407. UUID reqID = UUID.Random();
  408. Util.FireAndForget(delegate(object o) { DoJsonTakeValue(scriptID,reqID,storeID,path,false); });
  409. return reqID;
  410. }
  411. [ScriptInvocation]
  412. public UUID JsonTakeValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  413. {
  414. UUID reqID = UUID.Random();
  415. Util.FireAndForget(delegate(object o) { DoJsonTakeValue(scriptID,reqID,storeID,path,true); });
  416. return reqID;
  417. }
  418. // -----------------------------------------------------------------
  419. /// <summary>
  420. ///
  421. /// </summary>
  422. // -----------------------------------------------------------------
  423. [ScriptInvocation]
  424. public UUID JsonReadValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  425. {
  426. UUID reqID = UUID.Random();
  427. Util.FireAndForget(delegate(object o) { DoJsonReadValue(scriptID,reqID,storeID,path,false); });
  428. return reqID;
  429. }
  430. [ScriptInvocation]
  431. public UUID JsonReadValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  432. {
  433. UUID reqID = UUID.Random();
  434. Util.FireAndForget(delegate(object o) { DoJsonReadValue(scriptID,reqID,storeID,path,true); });
  435. return reqID;
  436. }
  437. #endregion
  438. // -----------------------------------------------------------------
  439. /// <summary>
  440. ///
  441. /// </summary>
  442. // -----------------------------------------------------------------
  443. protected void GenerateRuntimeError(string msg)
  444. {
  445. m_log.InfoFormat("[JsonStore] runtime error: {0}",msg);
  446. throw new Exception("JsonStore Runtime Error: " + msg);
  447. }
  448. // -----------------------------------------------------------------
  449. /// <summary>
  450. ///
  451. /// </summary>
  452. // -----------------------------------------------------------------
  453. protected void DispatchValue(UUID scriptID, UUID reqID, string value)
  454. {
  455. m_comms.DispatchReply(scriptID,1,value,reqID.ToString());
  456. }
  457. // -----------------------------------------------------------------
  458. /// <summary>
  459. ///
  460. /// </summary>
  461. // -----------------------------------------------------------------
  462. private void DoJsonTakeValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
  463. {
  464. try
  465. {
  466. m_store.TakeValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
  467. return;
  468. }
  469. catch (Exception e)
  470. {
  471. m_log.InfoFormat("[JsonStoreScripts]: unable to retrieve value; {0}",e.ToString());
  472. }
  473. DispatchValue(scriptID,reqID,String.Empty);
  474. }
  475. // -----------------------------------------------------------------
  476. /// <summary>
  477. ///
  478. /// </summary>
  479. // -----------------------------------------------------------------
  480. private void DoJsonReadValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
  481. {
  482. try
  483. {
  484. m_store.ReadValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
  485. return;
  486. }
  487. catch (Exception e)
  488. {
  489. m_log.InfoFormat("[JsonStoreScripts]: unable to retrieve value; {0}",e.ToString());
  490. }
  491. DispatchValue(scriptID,reqID,String.Empty);
  492. }
  493. // -----------------------------------------------------------------
  494. /// <summary>
  495. ///
  496. /// </summary>
  497. // -----------------------------------------------------------------
  498. private void DoJsonReadNotecard(
  499. UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, string notecardIdentifier)
  500. {
  501. UUID assetID;
  502. if (!UUID.TryParse(notecardIdentifier, out assetID))
  503. {
  504. SceneObjectPart part = m_scene.GetSceneObjectPart(hostID);
  505. assetID = ScriptUtils.GetAssetIdFromItemName(part, notecardIdentifier, (int)AssetType.Notecard);
  506. }
  507. AssetBase a = m_scene.AssetService.Get(assetID.ToString());
  508. if (a == null)
  509. GenerateRuntimeError(String.Format("Unable to find notecard asset {0}", assetID));
  510. if (a.Type != (sbyte)AssetType.Notecard)
  511. GenerateRuntimeError(String.Format("Invalid notecard asset {0}", assetID));
  512. m_log.DebugFormat("[JsonStoreScripts]: read notecard in context {0}",storeID);
  513. try
  514. {
  515. string jsondata = SLUtil.ParseNotecardToString(Encoding.UTF8.GetString(a.Data));
  516. int result = m_store.SetValue(storeID, path, jsondata,true) ? 1 : 0;
  517. m_comms.DispatchReply(scriptID, result, "", reqID.ToString());
  518. return;
  519. }
  520. catch (Exception e)
  521. {
  522. m_log.WarnFormat("[JsonStoreScripts]: Json parsing failed; {0}", e.Message);
  523. }
  524. GenerateRuntimeError(String.Format("Json parsing failed for {0}", assetID));
  525. m_comms.DispatchReply(scriptID, 0, "", reqID.ToString());
  526. }
  527. // -----------------------------------------------------------------
  528. /// <summary>
  529. ///
  530. /// </summary>
  531. // -----------------------------------------------------------------
  532. private void DoJsonWriteNotecard(UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, string name)
  533. {
  534. string data;
  535. if (! m_store.GetValue(storeID,path,true, out data))
  536. {
  537. m_comms.DispatchReply(scriptID,0,UUID.Zero.ToString(),reqID.ToString());
  538. return;
  539. }
  540. SceneObjectPart host = m_scene.GetSceneObjectPart(hostID);
  541. // Create new asset
  542. UUID assetID = UUID.Random();
  543. AssetBase asset = new AssetBase(assetID, name, (sbyte)AssetType.Notecard, host.OwnerID.ToString());
  544. asset.Description = "Json store";
  545. int textLength = data.Length;
  546. data = "Linden text version 2\n{\nLLEmbeddedItems version 1\n{\ncount 0\n}\nText length "
  547. + textLength.ToString() + "\n" + data + "}\n";
  548. asset.Data = Util.UTF8.GetBytes(data);
  549. m_scene.AssetService.Store(asset);
  550. // Create Task Entry
  551. TaskInventoryItem taskItem = new TaskInventoryItem();
  552. taskItem.ResetIDs(host.UUID);
  553. taskItem.ParentID = host.UUID;
  554. taskItem.CreationDate = (uint)Util.UnixTimeSinceEpoch();
  555. taskItem.Name = asset.Name;
  556. taskItem.Description = asset.Description;
  557. taskItem.Type = (int)AssetType.Notecard;
  558. taskItem.InvType = (int)InventoryType.Notecard;
  559. taskItem.OwnerID = host.OwnerID;
  560. taskItem.CreatorID = host.OwnerID;
  561. taskItem.BasePermissions = (uint)PermissionMask.All;
  562. taskItem.CurrentPermissions = (uint)PermissionMask.All;
  563. taskItem.EveryonePermissions = 0;
  564. taskItem.NextPermissions = (uint)PermissionMask.All;
  565. taskItem.GroupID = host.GroupID;
  566. taskItem.GroupPermissions = 0;
  567. taskItem.Flags = 0;
  568. taskItem.PermsGranter = UUID.Zero;
  569. taskItem.PermsMask = 0;
  570. taskItem.AssetID = asset.FullID;
  571. host.Inventory.AddInventoryItem(taskItem, false);
  572. m_comms.DispatchReply(scriptID,1,assetID.ToString(),reqID.ToString());
  573. }
  574. // -----------------------------------------------------------------
  575. /// <summary>
  576. /// Convert a list of values that are path components to a single string path
  577. /// </summary>
  578. // -----------------------------------------------------------------
  579. protected static Regex m_ArrayPattern = new Regex("^([0-9]+|\\+)$");
  580. private string ConvertList2Path(object[] pathlist)
  581. {
  582. string path = "";
  583. for (int i = 0; i < pathlist.Length; i++)
  584. {
  585. string token = "";
  586. if (pathlist[i] is string)
  587. {
  588. token = pathlist[i].ToString();
  589. // Check to see if this is a bare number which would not be a valid
  590. // identifier otherwise
  591. if (m_ArrayPattern.IsMatch(token))
  592. token = '[' + token + ']';
  593. }
  594. else if (pathlist[i] is int)
  595. {
  596. token = "[" + pathlist[i].ToString() + "]";
  597. }
  598. else
  599. {
  600. token = "." + pathlist[i].ToString() + ".";
  601. }
  602. path += token + ".";
  603. }
  604. return path;
  605. }
  606. // -----------------------------------------------------------------
  607. /// <summary>
  608. ///
  609. /// </summary>
  610. // -----------------------------------------------------------------
  611. private void DoJsonRezObject(UUID hostID, UUID scriptID, UUID reqID, string name, Vector3 pos, Vector3 vel, Quaternion rot, string param)
  612. {
  613. if (Double.IsNaN(rot.X) || Double.IsNaN(rot.Y) || Double.IsNaN(rot.Z) || Double.IsNaN(rot.W))
  614. {
  615. GenerateRuntimeError("Invalid rez rotation");
  616. return;
  617. }
  618. SceneObjectGroup host = m_scene.GetSceneObjectGroup(hostID);
  619. if (host == null)
  620. {
  621. GenerateRuntimeError(String.Format("Unable to find rezzing host '{0}'",hostID));
  622. return;
  623. }
  624. // hpos = host.RootPart.GetWorldPosition()
  625. // float dist = (float)llVecDist(hpos, pos);
  626. // if (dist > m_ScriptDistanceFactor * 10.0f)
  627. // return;
  628. TaskInventoryItem item = host.RootPart.Inventory.GetInventoryItem(name);
  629. if (item == null)
  630. {
  631. GenerateRuntimeError(String.Format("Unable to find object to rez '{0}'",name));
  632. return;
  633. }
  634. if (item.InvType != (int)InventoryType.Object)
  635. {
  636. GenerateRuntimeError("Can't create requested object; object is missing from database");
  637. return;
  638. }
  639. List<SceneObjectGroup> objlist;
  640. List<Vector3> veclist;
  641. bool success = host.RootPart.Inventory.GetRezReadySceneObjects(item, out objlist, out veclist);
  642. if (! success)
  643. {
  644. GenerateRuntimeError("Failed to create object");
  645. return;
  646. }
  647. int totalPrims = 0;
  648. foreach (SceneObjectGroup group in objlist)
  649. totalPrims += group.PrimCount;
  650. if (! m_scene.Permissions.CanRezObject(totalPrims, item.OwnerID, pos))
  651. {
  652. GenerateRuntimeError("Not allowed to create the object");
  653. return;
  654. }
  655. if (! m_scene.Permissions.BypassPermissions())
  656. {
  657. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  658. host.RootPart.Inventory.RemoveInventoryItem(item.ItemID);
  659. }
  660. for (int i = 0; i < objlist.Count; i++)
  661. {
  662. SceneObjectGroup group = objlist[i];
  663. Vector3 curpos = pos + veclist[i];
  664. if (group.IsAttachment == false && group.RootPart.Shape.State != 0)
  665. {
  666. group.RootPart.AttachedPos = group.AbsolutePosition;
  667. group.RootPart.Shape.LastAttachPoint = (byte)group.AttachmentPoint;
  668. }
  669. group.FromPartID = host.RootPart.UUID;
  670. m_scene.AddNewSceneObject(group, true, curpos, rot, vel);
  671. UUID storeID = group.UUID;
  672. if (! m_store.CreateStore(param, ref storeID))
  673. {
  674. GenerateRuntimeError("Unable to create jsonstore for new object");
  675. continue;
  676. }
  677. // We can only call this after adding the scene object, since the scene object references the scene
  678. // to find out if scripts should be activated at all.
  679. group.RootPart.SetDieAtEdge(true);
  680. group.CreateScriptInstances(0, true, m_scene.DefaultScriptEngine, 3);
  681. group.ResumeScripts();
  682. group.ScheduleGroupForFullUpdate();
  683. // send the reply back to the host object, use the integer param to indicate the number
  684. // of remaining objects
  685. m_comms.DispatchReply(scriptID, objlist.Count-i-1, group.RootPart.UUID.ToString(), reqID.ToString());
  686. }
  687. }
  688. }
  689. }