30 using System.Reflection;
31 using System.Threading;
34 using System.Net.Sockets;
38 using OpenMetaverse.StructuredData;
39 using OpenSim.Framework;
40 using OpenSim.Region.Framework.Interfaces;
41 using OpenSim.Region.Framework.Scenes;
42 using OpenSim.Region.Framework.Scenes.Scripting;
43 using System.Collections.Generic;
44 using System.Text.RegularExpressions;
47 namespace OpenSim.
Region.OptionalModules.Scripting.JsonStore
49 [Extension(Path =
"/OpenSim/RegionModules", NodeName =
"RegionModule", Id =
"JsonStoreScriptModule")]
53 private static readonly ILog m_log =
54 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
56 private IConfig m_config = null;
57 private bool m_enabled =
false;
58 private Scene m_scene = null;
63 private Dictionary<UUID,HashSet<UUID>> m_scriptStores =
new Dictionary<UUID,HashSet<UUID>>();
65 #region Region Module interface
74 get {
return this.GetType().Name; }
88 if ((m_config = config.Configs[
"JsonStore"]) == null)
95 m_enabled = m_config.GetBoolean(
"Enabled", m_enabled);
99 m_log.ErrorFormat(
"[JsonStoreScripts]: initialization error: {0}", e.Message);
104 m_log.DebugFormat(
"[JsonStoreScripts]: module is enabled");
131 scene.EventManager.OnScriptReset += HandleScriptReset;
132 scene.EventManager.OnRemoveScript += HandleScriptReset;
141 scene.EventManager.OnScriptReset -= HandleScriptReset;
142 scene.EventManager.OnRemoveScript -= HandleScriptReset;
152 private void HandleScriptReset(uint localID, UUID itemID)
154 HashSet<UUID> stores;
156 lock (m_scriptStores)
158 if (! m_scriptStores.TryGetValue(itemID, out stores))
160 m_scriptStores.Remove(itemID);
163 foreach (UUID
id in stores)
164 m_store.DestroyStore(id);
181 m_log.ErrorFormat(
"[JsonStoreScripts]: ScriptModuleComms interface not defined");
189 m_log.ErrorFormat(
"[JsonStoreScripts]: JsonModule interface not defined");
196 m_comms.RegisterScriptInvocations(
this);
197 m_comms.RegisterConstants(
this);
202 m_log.WarnFormat(
"[JsonStoreScripts]: script method registration failed; {0}", e.Message);
212 public Type ReplaceableInterface
219 #region ScriptConstantsInterface
225 public static readonly
int JSON_NODETYPE_OBJECT = (
int)JsonStoreNodeType.Object;
231 public static readonly
int JSON_NODETYPE_VALUE = (
int)JsonStoreNodeType.Value;
237 public static readonly
int JSON_VALUETYPE_BOOLEAN = (
int)JsonStoreValueType.Boolean;
243 public static readonly
int JSON_VALUETYPE_FLOAT = (
int)JsonStoreValueType.Float;
251 #region ScriptInvocationInteface
258 public UUID JsonAttachObjectStore(UUID hostID, UUID scriptID)
260 UUID uuid = UUID.Zero;
261 if (! m_store.AttachObjectStore(hostID))
262 GenerateRuntimeError(
"Failed to create Json store");
275 UUID uuid = UUID.Zero;
276 if (! m_store.CreateStore(value, ref uuid))
277 GenerateRuntimeError(
"Failed to create Json store");
279 lock (m_scriptStores)
281 if (! m_scriptStores.ContainsKey(scriptID))
282 m_scriptStores[scriptID] =
new HashSet<UUID>();
284 m_scriptStores[scriptID].Add(uuid);
299 if (m_scriptStores.ContainsKey(scriptID))
300 m_scriptStores[scriptID].Remove(storeID);
303 return m_store.DestroyStore(storeID) ? 1 : 0;
314 return m_store.TestStore(storeID) ? 1 : 0;
323 public UUID JsonRezAtRoot(UUID hostID, UUID scriptID,
string item, Vector3 pos, Vector3 vel, Quaternion rot,
string param)
325 UUID reqID = UUID.Random();
327 o => DoJsonRezObject(hostID, scriptID, reqID, item, pos, vel, rot, param), null,
"JsonStoreScriptModule.DoJsonRezObject");
339 UUID reqID = UUID.Random();
341 o => DoJsonReadNotecard(reqID, hostID, scriptID, storeID, path, notecardIdentifier), null,
"JsonStoreScriptModule.JsonReadNotecard");
353 UUID reqID = UUID.Random();
355 o => DoJsonWriteNotecard(reqID,hostID,scriptID,storeID,path,name), null,
"JsonStoreScriptModule.DoJsonWriteNotecard");
367 string ipath = ConvertList2Path(pathlist);
376 return "**INVALID**";
387 return (
int)m_store.GetNodeType(storeID,path);
398 return (
int)m_store.GetValueType(storeID,path);
407 public int JsonSetValue(UUID hostID, UUID scriptID, UUID storeID,
string path,
string value)
409 return m_store.SetValue(storeID,path,value,
false) ? 1 : 0;
413 public int JsonSetJson(UUID hostID, UUID scriptID, UUID storeID,
string path,
string value)
415 return m_store.SetValue(storeID,path,value,
true) ? 1 : 0;
426 return m_store.RemoveValue(storeID,path) ? 1 : 0;
437 return m_store.GetArrayLength(storeID,path);
446 public string JsonGetValue(UUID hostID, UUID scriptID, UUID storeID,
string path)
448 string value = String.Empty;
449 m_store.GetValue(storeID,path,
false,out value);
454 public string JsonGetJson(UUID hostID, UUID scriptID, UUID storeID,
string path)
456 string value = String.Empty;
457 m_store.GetValue(storeID,path,
true, out value);
469 UUID reqID = UUID.Random();
471 o => DoJsonTakeValue(scriptID,reqID,storeID,path,
false), null,
"JsonStoreScriptModule.DoJsonTakeValue");
478 UUID reqID = UUID.Random();
480 o => DoJsonTakeValue(scriptID,reqID,storeID,path,
true), null,
"JsonStoreScriptModule.DoJsonTakeValueJson");
492 UUID reqID = UUID.Random();
494 o => DoJsonReadValue(scriptID,reqID,storeID,path,
false), null,
"JsonStoreScriptModule.DoJsonReadValue");
501 UUID reqID = UUID.Random();
503 o => DoJsonReadValue(scriptID,reqID,storeID,path,
true), null,
"JsonStoreScriptModule.DoJsonReadValueJson");
516 m_log.InfoFormat(
"[JsonStore] runtime error: {0}",msg);
517 throw new Exception(
"JsonStore Runtime Error: " + msg);
527 m_comms.DispatchReply(scriptID,1,value,reqID.ToString());
535 private void DoJsonTakeValue(UUID scriptID, UUID reqID, UUID storeID,
string path,
bool useJson)
539 m_store.TakeValue(storeID,path,useJson,delegate(
string value) { DispatchValue(scriptID,reqID,value); });
544 m_log.InfoFormat(
"[JsonStoreScripts]: unable to retrieve value; {0}",e.ToString());
547 DispatchValue(scriptID,reqID,
String.Empty);
556 private void DoJsonReadValue(UUID scriptID, UUID reqID, UUID storeID,
string path,
bool useJson)
560 m_store.ReadValue(storeID,path,useJson,delegate(
string value) { DispatchValue(scriptID,reqID,value); });
565 m_log.InfoFormat(
"[JsonStoreScripts]: unable to retrieve value; {0}",e.ToString());
568 DispatchValue(scriptID,reqID,
String.Empty);
576 private void DoJsonReadNotecard(
577 UUID reqID, UUID hostID, UUID scriptID, UUID storeID,
string path,
string notecardIdentifier)
581 if (!
UUID.TryParse(notecardIdentifier, out assetID))
584 assetID = ScriptUtils.GetAssetIdFromItemName(part, notecardIdentifier, (int)AssetType.Notecard);
587 AssetBase a = m_scene.AssetService.Get(assetID.ToString());
589 GenerateRuntimeError(
String.Format(
"Unable to find notecard asset {0}", assetID));
591 if (a.Type != (sbyte)AssetType.Notecard)
592 GenerateRuntimeError(
String.Format(
"Invalid notecard asset {0}", assetID));
594 m_log.DebugFormat(
"[JsonStoreScripts]: read notecard in context {0}",storeID);
598 string jsondata = SLUtil.ParseNotecardToString(a.Data);
599 int result = m_store.SetValue(storeID, path, jsondata,
true) ? 1 : 0;
600 m_comms.DispatchReply(scriptID, result,
"", reqID.ToString());
603 catch(SLUtil.NotANotecardFormatException e)
605 m_log.WarnFormat(
"[JsonStoreScripts]: Notecard parsing failed; assetId {0} at line number {1}", assetID.ToString(), e.lineNumber);
609 m_log.WarnFormat(
"[JsonStoreScripts]: Json parsing failed; {0}", e.Message);
612 GenerateRuntimeError(
String.Format(
"Json parsing failed for {0}", assetID));
613 m_comms.DispatchReply(scriptID, 0,
"", reqID.ToString());
621 private void DoJsonWriteNotecard(UUID reqID, UUID hostID, UUID scriptID, UUID storeID,
string path,
string name)
624 if (! m_store.GetValue(storeID,path,
true, out data))
626 m_comms.DispatchReply(scriptID,0,UUID.Zero.ToString(),reqID.ToString());
633 UUID assetID = UUID.Random();
635 asset.Description =
"Json store";
637 int textLength = data.Length;
638 data =
"Linden text version 2\n{\nLLEmbeddedItems version 1\n{\ncount 0\n}\nText length "
639 + textLength.ToString() +
"\n" + data +
"}\n";
641 asset.Data = Util.UTF8.GetBytes(data);
642 m_scene.AssetService.Store(asset);
647 taskItem.ResetIDs(host.UUID);
648 taskItem.ParentID = host.UUID;
649 taskItem.CreationDate = (uint)Util.UnixTimeSinceEpoch();
650 taskItem.Name = asset.Name;
651 taskItem.Description = asset.Description;
652 taskItem.Type = (int)AssetType.Notecard;
653 taskItem.InvType = (
int)InventoryType.Notecard;
654 taskItem.OwnerID = host.OwnerID;
655 taskItem.CreatorID = host.OwnerID;
657 taskItem.CurrentPermissions = (uint)PermissionMask.All;
658 taskItem.EveryonePermissions = 0;
660 taskItem.GroupID = host.
GroupID;
661 taskItem.GroupPermissions = 0;
663 taskItem.PermsGranter =
UUID.Zero;
664 taskItem.PermsMask = 0;
665 taskItem.AssetID = asset.FullID;
669 m_comms.DispatchReply(scriptID,1,assetID.ToString(),reqID.ToString());
677 protected static Regex m_ArrayPattern =
new Regex(
"^([0-9]+|\\+)$");
678 private string ConvertList2Path(
object[] pathlist)
681 for (
int i = 0; i < pathlist.Length; i++)
685 if (pathlist[i] is
string)
687 token = pathlist[i].ToString();
691 if (m_ArrayPattern.IsMatch(token))
692 token =
'[' + token +
']';
694 else if (pathlist[i] is
int)
696 token =
"[" + pathlist[i].ToString() +
"]";
700 token =
"." + pathlist[i].ToString() +
".";
714 private void DoJsonRezObject(UUID hostID, UUID scriptID, UUID reqID,
string name, Vector3 pos, Vector3 vel, Quaternion rot,
string param)
716 if (Double.IsNaN(rot.X) || Double.IsNaN(rot.Y) || Double.IsNaN(rot.Z) || Double.IsNaN(rot.W))
718 GenerateRuntimeError(
"Invalid rez rotation");
725 GenerateRuntimeError(
String.Format(
"Unable to find rezzing host '{0}'",hostID));
737 GenerateRuntimeError(
String.Format(
"Unable to find object to rez '{0}'",name));
741 if (item.
InvType != (
int)InventoryType.Object)
743 GenerateRuntimeError(
"Can't create requested object; object is missing from database");
747 List<SceneObjectGroup> objlist;
748 List<Vector3> veclist;
750 Vector3 bbox =
new Vector3();
752 bool success = host.RootPart.Inventory.GetRezReadySceneObjects(item, out objlist, out veclist, out bbox, out offsetHeight);
755 GenerateRuntimeError(
"Failed to create object");
761 totalPrims += group.PrimCount;
763 if (! m_scene.Permissions.CanRezObject(totalPrims, item.OwnerID, pos))
765 GenerateRuntimeError(
"Not allowed to create the object");
769 if (! m_scene.Permissions.BypassPermissions())
775 for (
int i = 0; i < objlist.Count; i++)
778 Vector3 curpos = pos + veclist[i];
782 group.RootPart.AttachedPos = group.AbsolutePosition;
786 group.FromPartID = host.RootPart.UUID;
787 m_scene.AddNewSceneObject(group,
true, curpos, rot, vel);
789 UUID storeID = group.UUID;
790 if (! m_store.CreateStore(param, ref storeID))
792 GenerateRuntimeError(
"Unable to create jsonstore for new object");
798 group.RootPart.SetDieAtEdge(
true);
799 group.CreateScriptInstances(0,
true, m_scene.DefaultScriptEngine, 3);
800 group.ResumeScripts();
802 group.ScheduleGroupForFullUpdate();
806 m_comms.DispatchReply(scriptID, objlist.Count-i-1, group.RootPart.UUID.ToString(), reqID.ToString());
UUID JsonCreateStore(UUID hostID, UUID scriptID, string value)
int JsonRemoveValue(UUID hostID, UUID scriptID, UUID storeID, string path)
IEntityInventory Inventory
This part's inventory
void RemoveRegion(Scene scene)
void AddRegion(Scene scene)
int JsonSetJson(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
void GenerateRuntimeError(string msg)
UUID JsonReadValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
string JsonList2Path(UUID hostID, UUID scriptID, object[] pathlist)
UUID JsonReadNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, string notecardIdentifier)
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
UUID JsonRezAtRoot(UUID hostID, UUID scriptID, string item, Vector3 pos, Vector3 vel, Quaternion rot, string param)
int JsonDestroyStore(UUID hostID, UUID scriptID, UUID storeID)
int JsonGetArrayLength(UUID hostID, UUID scriptID, UUID storeID, string path)
UUID JsonWriteNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, string name)
int JsonSetValue(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
Represents an item in a task inventory
int JsonTestStore(UUID hostID, UUID scriptID, UUID storeID)
void Close()
Nothing to do on close
OpenSim.Framework.PermissionMask PermissionMask
Asset class. All Assets are reference by this class or a class derived from this class ...
void RegionLoaded(Scene scene)
Called when all modules have been added for a region. This is where we hook up events ...
int JsonGetValueType(UUID hostID, UUID scriptID, UUID storeID, string path)
UUID JsonReadValue(UUID hostID, UUID scriptID, UUID storeID, string path)
void AddInventoryItem(TaskInventoryItem item, bool allowedDrop)
Add an item to this entity's inventory. If an item with the same name already exists, then an alternative name is chosen.
UUID JsonTakeValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
uint AttachmentPoint
Attachment point of this scene object to an avatar.
Interface for communication between OpenSim modules and in-world scripts
int RemoveInventoryItem(UUID itemID)
Remove an item from this entity's inventory
string JsonGetValue(UUID hostID, UUID scriptID, UUID storeID, string path)
void PostInitialise()
everything is loaded, perform post load configuration
UUID JsonTakeValue(UUID hostID, UUID scriptID, UUID storeID, string path)
void Initialise(IConfigSource config)
Initialise this shared module
static bool CanonicalPathExpression(string ipath, out string opath)
void DispatchValue(UUID scriptID, UUID reqID, string value)
string JsonGetJson(UUID hostID, UUID scriptID, UUID storeID, string path)
int JsonGetNodeType(UUID hostID, UUID scriptID, UUID storeID, string path)
bool IsAttachment
Is this scene object acting as an attachment?