JsonStoreScriptModule.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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(
  282. o => DoJsonRezObject(hostID, scriptID, reqID, item, pos, vel, rot, param), null, "JsonStoreScriptModule.DoJsonRezObject");
  283. return reqID;
  284. }
  285. // -----------------------------------------------------------------
  286. /// <summary>
  287. ///
  288. /// </summary>
  289. // -----------------------------------------------------------------
  290. [ScriptInvocation]
  291. public UUID JsonReadNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, string notecardIdentifier)
  292. {
  293. UUID reqID = UUID.Random();
  294. Util.FireAndForget(
  295. o => DoJsonReadNotecard(reqID, hostID, scriptID, storeID, path, notecardIdentifier), null, "JsonStoreScriptModule.JsonReadNotecard");
  296. return reqID;
  297. }
  298. // -----------------------------------------------------------------
  299. /// <summary>
  300. ///
  301. /// </summary>
  302. // -----------------------------------------------------------------
  303. [ScriptInvocation]
  304. public UUID JsonWriteNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, string name)
  305. {
  306. UUID reqID = UUID.Random();
  307. Util.FireAndForget(
  308. o => DoJsonWriteNotecard(reqID,hostID,scriptID,storeID,path,name), null, "JsonStoreScriptModule.DoJsonWriteNotecard");
  309. return reqID;
  310. }
  311. // -----------------------------------------------------------------
  312. /// <summary>
  313. ///
  314. /// </summary>
  315. // -----------------------------------------------------------------
  316. [ScriptInvocation]
  317. public string JsonList2Path(UUID hostID, UUID scriptID, object[] pathlist)
  318. {
  319. string ipath = ConvertList2Path(pathlist);
  320. string opath;
  321. if (JsonStore.CanonicalPathExpression(ipath,out opath))
  322. return opath;
  323. // This won't parse if passed to the other routines as opposed to
  324. // returning an empty string which is a valid path and would overwrite
  325. // the entire store
  326. return "**INVALID**";
  327. }
  328. // -----------------------------------------------------------------
  329. /// <summary>
  330. ///
  331. /// </summary>
  332. // -----------------------------------------------------------------
  333. [ScriptInvocation]
  334. public int JsonGetNodeType(UUID hostID, UUID scriptID, UUID storeID, string path)
  335. {
  336. return (int)m_store.GetNodeType(storeID,path);
  337. }
  338. // -----------------------------------------------------------------
  339. /// <summary>
  340. ///
  341. /// </summary>
  342. // -----------------------------------------------------------------
  343. [ScriptInvocation]
  344. public int JsonGetValueType(UUID hostID, UUID scriptID, UUID storeID, string path)
  345. {
  346. return (int)m_store.GetValueType(storeID,path);
  347. }
  348. // -----------------------------------------------------------------
  349. /// <summary>
  350. ///
  351. /// </summary>
  352. // -----------------------------------------------------------------
  353. [ScriptInvocation]
  354. public int JsonSetValue(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
  355. {
  356. return m_store.SetValue(storeID,path,value,false) ? 1 : 0;
  357. }
  358. [ScriptInvocation]
  359. public int JsonSetJson(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
  360. {
  361. return m_store.SetValue(storeID,path,value,true) ? 1 : 0;
  362. }
  363. // -----------------------------------------------------------------
  364. /// <summary>
  365. ///
  366. /// </summary>
  367. // -----------------------------------------------------------------
  368. [ScriptInvocation]
  369. public int JsonRemoveValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  370. {
  371. return m_store.RemoveValue(storeID,path) ? 1 : 0;
  372. }
  373. // -----------------------------------------------------------------
  374. /// <summary>
  375. ///
  376. /// </summary>
  377. // -----------------------------------------------------------------
  378. [ScriptInvocation]
  379. public int JsonGetArrayLength(UUID hostID, UUID scriptID, UUID storeID, string path)
  380. {
  381. return m_store.GetArrayLength(storeID,path);
  382. }
  383. // -----------------------------------------------------------------
  384. /// <summary>
  385. ///
  386. /// </summary>
  387. // -----------------------------------------------------------------
  388. [ScriptInvocation]
  389. public string JsonGetValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  390. {
  391. string value = String.Empty;
  392. m_store.GetValue(storeID,path,false,out value);
  393. return value;
  394. }
  395. [ScriptInvocation]
  396. public string JsonGetJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  397. {
  398. string value = String.Empty;
  399. m_store.GetValue(storeID,path,true, out value);
  400. return value;
  401. }
  402. // -----------------------------------------------------------------
  403. /// <summary>
  404. ///
  405. /// </summary>
  406. // -----------------------------------------------------------------
  407. [ScriptInvocation]
  408. public UUID JsonTakeValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  409. {
  410. UUID reqID = UUID.Random();
  411. Util.FireAndForget(
  412. o => DoJsonTakeValue(scriptID,reqID,storeID,path,false), null, "JsonStoreScriptModule.DoJsonTakeValue");
  413. return reqID;
  414. }
  415. [ScriptInvocation]
  416. public UUID JsonTakeValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  417. {
  418. UUID reqID = UUID.Random();
  419. Util.FireAndForget(
  420. o => DoJsonTakeValue(scriptID,reqID,storeID,path,true), null, "JsonStoreScriptModule.DoJsonTakeValueJson");
  421. return reqID;
  422. }
  423. // -----------------------------------------------------------------
  424. /// <summary>
  425. ///
  426. /// </summary>
  427. // -----------------------------------------------------------------
  428. [ScriptInvocation]
  429. public UUID JsonReadValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  430. {
  431. UUID reqID = UUID.Random();
  432. Util.FireAndForget(
  433. o => DoJsonReadValue(scriptID,reqID,storeID,path,false), null, "JsonStoreScriptModule.DoJsonReadValue");
  434. return reqID;
  435. }
  436. [ScriptInvocation]
  437. public UUID JsonReadValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  438. {
  439. UUID reqID = UUID.Random();
  440. Util.FireAndForget(
  441. o => DoJsonReadValue(scriptID,reqID,storeID,path,true), null, "JsonStoreScriptModule.DoJsonReadValueJson");
  442. return reqID;
  443. }
  444. #endregion
  445. // -----------------------------------------------------------------
  446. /// <summary>
  447. ///
  448. /// </summary>
  449. // -----------------------------------------------------------------
  450. protected void GenerateRuntimeError(string msg)
  451. {
  452. m_log.InfoFormat("[JsonStore] runtime error: {0}",msg);
  453. throw new Exception("JsonStore Runtime Error: " + msg);
  454. }
  455. // -----------------------------------------------------------------
  456. /// <summary>
  457. ///
  458. /// </summary>
  459. // -----------------------------------------------------------------
  460. protected void DispatchValue(UUID scriptID, UUID reqID, string value)
  461. {
  462. m_comms.DispatchReply(scriptID,1,value,reqID.ToString());
  463. }
  464. // -----------------------------------------------------------------
  465. /// <summary>
  466. ///
  467. /// </summary>
  468. // -----------------------------------------------------------------
  469. private void DoJsonTakeValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
  470. {
  471. try
  472. {
  473. m_store.TakeValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
  474. return;
  475. }
  476. catch (Exception e)
  477. {
  478. m_log.InfoFormat("[JsonStoreScripts]: unable to retrieve value; {0}",e.ToString());
  479. }
  480. DispatchValue(scriptID,reqID,String.Empty);
  481. }
  482. // -----------------------------------------------------------------
  483. /// <summary>
  484. ///
  485. /// </summary>
  486. // -----------------------------------------------------------------
  487. private void DoJsonReadValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
  488. {
  489. try
  490. {
  491. m_store.ReadValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
  492. return;
  493. }
  494. catch (Exception e)
  495. {
  496. m_log.InfoFormat("[JsonStoreScripts]: unable to retrieve value; {0}",e.ToString());
  497. }
  498. DispatchValue(scriptID,reqID,String.Empty);
  499. }
  500. // -----------------------------------------------------------------
  501. /// <summary>
  502. ///
  503. /// </summary>
  504. // -----------------------------------------------------------------
  505. private void DoJsonReadNotecard(
  506. UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, string notecardIdentifier)
  507. {
  508. UUID assetID;
  509. if (!UUID.TryParse(notecardIdentifier, out assetID))
  510. {
  511. SceneObjectPart part = m_scene.GetSceneObjectPart(hostID);
  512. assetID = ScriptUtils.GetAssetIdFromItemName(part, notecardIdentifier, (int)AssetType.Notecard);
  513. }
  514. AssetBase a = m_scene.AssetService.Get(assetID.ToString());
  515. if (a == null)
  516. GenerateRuntimeError(String.Format("Unable to find notecard asset {0}", assetID));
  517. if (a.Type != (sbyte)AssetType.Notecard)
  518. GenerateRuntimeError(String.Format("Invalid notecard asset {0}", assetID));
  519. m_log.DebugFormat("[JsonStoreScripts]: read notecard in context {0}",storeID);
  520. try
  521. {
  522. string jsondata = SLUtil.ParseNotecardToString(a.Data);
  523. int result = m_store.SetValue(storeID, path, jsondata,true) ? 1 : 0;
  524. m_comms.DispatchReply(scriptID, result, "", reqID.ToString());
  525. return;
  526. }
  527. catch(SLUtil.NotANotecardFormatException e)
  528. {
  529. m_log.WarnFormat("[JsonStoreScripts]: Notecard parsing failed; assetId {0} at line number {1}", assetID.ToString(), e.lineNumber);
  530. }
  531. catch (Exception e)
  532. {
  533. m_log.WarnFormat("[JsonStoreScripts]: Json parsing failed; {0}", e.Message);
  534. }
  535. GenerateRuntimeError(String.Format("Json parsing failed for {0}", assetID));
  536. m_comms.DispatchReply(scriptID, 0, "", reqID.ToString());
  537. }
  538. // -----------------------------------------------------------------
  539. /// <summary>
  540. ///
  541. /// </summary>
  542. // -----------------------------------------------------------------
  543. private void DoJsonWriteNotecard(UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, string name)
  544. {
  545. string data;
  546. if (! m_store.GetValue(storeID,path,true, out data))
  547. {
  548. m_comms.DispatchReply(scriptID,0,UUID.Zero.ToString(),reqID.ToString());
  549. return;
  550. }
  551. SceneObjectPart host = m_scene.GetSceneObjectPart(hostID);
  552. // Create new asset
  553. UUID assetID = UUID.Random();
  554. AssetBase asset = new AssetBase(assetID, name, (sbyte)AssetType.Notecard, host.OwnerID.ToString());
  555. asset.Description = "Json store";
  556. int textLength = data.Length;
  557. data = "Linden text version 2\n{\nLLEmbeddedItems version 1\n{\ncount 0\n}\nText length "
  558. + textLength.ToString() + "\n" + data + "}\n";
  559. asset.Data = Util.UTF8.GetBytes(data);
  560. m_scene.AssetService.Store(asset);
  561. // Create Task Entry
  562. TaskInventoryItem taskItem = new TaskInventoryItem();
  563. taskItem.ResetIDs(host.UUID);
  564. taskItem.ParentID = host.UUID;
  565. taskItem.CreationDate = (uint)Util.UnixTimeSinceEpoch();
  566. taskItem.Name = asset.Name;
  567. taskItem.Description = asset.Description;
  568. taskItem.Type = (int)AssetType.Notecard;
  569. taskItem.InvType = (int)InventoryType.Notecard;
  570. taskItem.OwnerID = host.OwnerID;
  571. taskItem.CreatorID = host.OwnerID;
  572. taskItem.BasePermissions = (uint)PermissionMask.All;
  573. taskItem.CurrentPermissions = (uint)PermissionMask.All;
  574. taskItem.EveryonePermissions = 0;
  575. taskItem.NextPermissions = (uint)PermissionMask.All;
  576. taskItem.GroupID = host.GroupID;
  577. taskItem.GroupPermissions = 0;
  578. taskItem.Flags = 0;
  579. taskItem.PermsGranter = UUID.Zero;
  580. taskItem.PermsMask = 0;
  581. taskItem.AssetID = asset.FullID;
  582. host.Inventory.AddInventoryItem(taskItem, false);
  583. host.ParentGroup.InvalidateEffectivePerms();
  584. m_comms.DispatchReply(scriptID,1,assetID.ToString(),reqID.ToString());
  585. }
  586. // -----------------------------------------------------------------
  587. /// <summary>
  588. /// Convert a list of values that are path components to a single string path
  589. /// </summary>
  590. // -----------------------------------------------------------------
  591. protected static Regex m_ArrayPattern = new Regex("^([0-9]+|\\+)$");
  592. private string ConvertList2Path(object[] pathlist)
  593. {
  594. string path = "";
  595. for (int i = 0; i < pathlist.Length; i++)
  596. {
  597. string token = "";
  598. if (pathlist[i] is string)
  599. {
  600. token = pathlist[i].ToString();
  601. // Check to see if this is a bare number which would not be a valid
  602. // identifier otherwise
  603. if (m_ArrayPattern.IsMatch(token))
  604. token = '[' + token + ']';
  605. }
  606. else if (pathlist[i] is int)
  607. {
  608. token = "[" + pathlist[i].ToString() + "]";
  609. }
  610. else
  611. {
  612. token = "." + pathlist[i].ToString() + ".";
  613. }
  614. path += token + ".";
  615. }
  616. return path;
  617. }
  618. // -----------------------------------------------------------------
  619. /// <summary>
  620. ///
  621. /// </summary>
  622. // -----------------------------------------------------------------
  623. private void DoJsonRezObject(UUID hostID, UUID scriptID, UUID reqID, string name, Vector3 pos, Vector3 vel, Quaternion rot, string param)
  624. {
  625. if (Double.IsNaN(rot.X) || Double.IsNaN(rot.Y) || Double.IsNaN(rot.Z) || Double.IsNaN(rot.W))
  626. {
  627. GenerateRuntimeError("Invalid rez rotation");
  628. return;
  629. }
  630. SceneObjectGroup host = m_scene.GetSceneObjectGroup(hostID);
  631. if (host == null)
  632. {
  633. GenerateRuntimeError(String.Format("Unable to find rezzing host '{0}'",hostID));
  634. return;
  635. }
  636. // hpos = host.RootPart.GetWorldPosition()
  637. // float dist = (float)llVecDist(hpos, pos);
  638. // if (dist > m_ScriptDistanceFactor * 10.0f)
  639. // return;
  640. TaskInventoryItem item = host.RootPart.Inventory.GetInventoryItem(name);
  641. if (item == null)
  642. {
  643. GenerateRuntimeError(String.Format("Unable to find object to rez '{0}'",name));
  644. return;
  645. }
  646. if (item.InvType != (int)InventoryType.Object)
  647. {
  648. GenerateRuntimeError("Can't create requested object; object is missing from database");
  649. return;
  650. }
  651. List<SceneObjectGroup> objlist;
  652. List<Vector3> veclist;
  653. Vector3 bbox = new Vector3();
  654. float offsetHeight;
  655. bool success = host.RootPart.Inventory.GetRezReadySceneObjects(item, out objlist, out veclist, out bbox, out offsetHeight);
  656. if (! success)
  657. {
  658. GenerateRuntimeError("Failed to create object");
  659. return;
  660. }
  661. int totalPrims = 0;
  662. foreach (SceneObjectGroup group in objlist)
  663. totalPrims += group.PrimCount;
  664. if (! m_scene.Permissions.CanRezObject(totalPrims, item.OwnerID, pos))
  665. {
  666. GenerateRuntimeError("Not allowed to create the object");
  667. return;
  668. }
  669. if (! m_scene.Permissions.BypassPermissions())
  670. {
  671. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  672. host.RootPart.Inventory.RemoveInventoryItem(item.ItemID);
  673. }
  674. for (int i = 0; i < objlist.Count; i++)
  675. {
  676. SceneObjectGroup group = objlist[i];
  677. Vector3 curpos = pos + veclist[i];
  678. if (group.IsAttachment == false && group.RootPart.Shape.State != 0)
  679. {
  680. group.RootPart.AttachedPos = group.AbsolutePosition;
  681. group.RootPart.Shape.LastAttachPoint = (byte)group.AttachmentPoint;
  682. }
  683. group.RezzerID = host.RootPart.UUID;
  684. m_scene.AddNewSceneObject(group, true, curpos, rot, vel);
  685. UUID storeID = group.UUID;
  686. if (! m_store.CreateStore(param, ref storeID))
  687. {
  688. GenerateRuntimeError("Unable to create jsonstore for new object");
  689. continue;
  690. }
  691. // We can only call this after adding the scene object, since the scene object references the scene
  692. // to find out if scripts should be activated at all.
  693. group.RootPart.SetDieAtEdge(true);
  694. group.CreateScriptInstances(0, true, m_scene.DefaultScriptEngine, 3);
  695. group.ResumeScripts();
  696. group.ScheduleGroupForFullUpdate();
  697. // send the reply back to the host object, use the integer param to indicate the number
  698. // of remaining objects
  699. m_comms.DispatchReply(scriptID, objlist.Count-i-1, group.RootPart.UUID.ToString(), reqID.ToString());
  700. }
  701. }
  702. }
  703. }