From e17392acbb46e1e48e169069a822f8b814762214 Mon Sep 17 00:00:00 2001 From: Mic Bowman Date: Wed, 6 Feb 2013 17:29:17 -0800 Subject: [PATCH] Enables script access to the per object dynamic attributes through the JsonStore script functions. Adds JsonAttachObjectStore to associate a store identifier with an object (scripts can only access the store in their host object, this could be extended but isn't necessary for now). Note this opens a method to the DAMap OSDMap. This will be removed later, but greatly simplifies the code for now. The JsonStore and these scripts are disabled by default. --- OpenSim/Framework/DAMap.cs | 8 +++ .../Framework/Interfaces/IJsonStoreModule.cs | 1 + .../Scripting/JsonStore/JsonStore.cs | 64 +++++++++++++++---- .../Scripting/JsonStore/JsonStoreModule.cs | 28 ++++++++ .../JsonStore/JsonStoreScriptModule.cs | 16 +++++ 5 files changed, 104 insertions(+), 13 deletions(-) diff --git a/OpenSim/Framework/DAMap.cs b/OpenSim/Framework/DAMap.cs index 291c8b8385..7d7738a2a0 100644 --- a/OpenSim/Framework/DAMap.cs +++ b/OpenSim/Framework/DAMap.cs @@ -73,6 +73,14 @@ namespace OpenSim.Framework m_map = (OSDMap)OSDParser.DeserializeLLSDXml(rawXml); } + // WARNING: this is temporary for experimentation only, it will be removed!!!! + public OSDMap TopLevelMap + { + get { return m_map; } + set { m_map = value; } + } + + public void ReadXml(XmlReader reader) { ReadXml(reader.ReadInnerXml()); diff --git a/OpenSim/Region/Framework/Interfaces/IJsonStoreModule.cs b/OpenSim/Region/Framework/Interfaces/IJsonStoreModule.cs index 0bb45673cc..cc7885a7a0 100644 --- a/OpenSim/Region/Framework/Interfaces/IJsonStoreModule.cs +++ b/OpenSim/Region/Framework/Interfaces/IJsonStoreModule.cs @@ -35,6 +35,7 @@ namespace OpenSim.Region.Framework.Interfaces public interface IJsonStoreModule { + bool AttachObjectStore(UUID objectID); bool CreateStore(string value, ref UUID result); bool DestroyStore(UUID storeID); bool TestStore(UUID storeID); diff --git a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStore.cs b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStore.cs index 0b7b31bd32..751e463a49 100644 --- a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStore.cs +++ b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStore.cs @@ -49,7 +49,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private OSD m_ValueStore; + protected virtual OSD ValueStore { get; set; } protected class TakeValueCallbackClass { @@ -108,17 +108,18 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore /// /// // ----------------------------------------------------------------- - public JsonStore() : this("") {} - - public JsonStore(string value) + public JsonStore() { m_TakeStore = new List(); m_ReadStore = new List(); - + } + + public JsonStore(string value) + { if (String.IsNullOrEmpty(value)) - m_ValueStore = new OSDMap(); + ValueStore = new OSDMap(); else - m_ValueStore = OSDParser.DeserializeJson(value); + ValueStore = OSDParser.DeserializeJson(value); } // ----------------------------------------------------------------- @@ -129,7 +130,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore public bool TestPath(string expr, bool useJson) { Stack path = ParsePathExpression(expr); - OSD result = ProcessPathExpression(m_ValueStore,path); + OSD result = ProcessPathExpression(ValueStore,path); if (result == null) return false; @@ -148,7 +149,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore public bool GetValue(string expr, out string value, bool useJson) { Stack path = ParsePathExpression(expr); - OSD result = ProcessPathExpression(m_ValueStore,path); + OSD result = ProcessPathExpression(ValueStore,path); return ConvertOutputValue(result,out value,useJson); } @@ -184,7 +185,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore Stack path = ParsePathExpression(expr); string pexpr = PathExpressionToKey(path); - OSD result = ProcessPathExpression(m_ValueStore,path); + OSD result = ProcessPathExpression(ValueStore,path); if (result == null) { m_TakeStore.Add(new TakeValueCallbackClass(pexpr,useJson,cback)); @@ -215,7 +216,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore Stack path = ParsePathExpression(expr); string pexpr = PathExpressionToKey(path); - OSD result = ProcessPathExpression(m_ValueStore,path); + OSD result = ProcessPathExpression(ValueStore,path); if (result == null) { m_ReadStore.Add(new TakeValueCallbackClass(pexpr,useJson,cback)); @@ -245,7 +246,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore Stack path = ParsePathExpression(expr); if (path.Count == 0) { - m_ValueStore = ovalue; + ValueStore = ovalue; return true; } @@ -254,7 +255,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore if (pexpr != "") pexpr += "."; - OSD result = ProcessPathExpression(m_ValueStore,path); + OSD result = ProcessPathExpression(ValueStore,path); if (result == null) return false; @@ -522,4 +523,41 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore return pkey; } } + + public class JsonObjectStore : JsonStore + { + private static readonly ILog m_log = + LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + private Scene m_scene; + private UUID m_objectID; + + protected override OSD ValueStore + { + get + { + SceneObjectPart sop = m_scene.GetSceneObjectPart(m_objectID); + if (sop == null) + { + // This is bad + return null; + } + + return sop.DynAttrs.TopLevelMap; + } + + // cannot set the top level + set + { + m_log.InfoFormat("[JsonStore] cannot set top level value in object store"); + } + } + + public JsonObjectStore(Scene scene, UUID oid) : base() + { + m_scene = scene; + m_objectID = oid; + } + } + } diff --git a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreModule.cs b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreModule.cs index b9b3ebca21..a36ef4257d 100644 --- a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreModule.cs +++ b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreModule.cs @@ -170,6 +170,34 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore #region ScriptInvocationInteface + // ----------------------------------------------------------------- + /// + /// + /// + // ----------------------------------------------------------------- + public bool AttachObjectStore(UUID objectID) + { + if (! m_enabled) return false; + + SceneObjectPart sop = m_scene.GetSceneObjectPart(objectID); + if (sop == null) + { + m_log.InfoFormat("[JsonStore] unable to attach to unknown object; {0}",objectID); + return false; + } + + lock (m_JsonValueStore) + { + if (m_JsonValueStore.ContainsKey(objectID)) + return true; + + JsonStore map = new JsonObjectStore(m_scene,objectID); + m_JsonValueStore.Add(objectID,map); + } + + return true; + } + // ----------------------------------------------------------------- /// /// diff --git a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs index ec880a7220..48b4a9f302 100644 --- a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs +++ b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs @@ -169,6 +169,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore m_comms.RegisterScriptInvocations(this); // m_comms.RegisterScriptInvocation(this, "JsonCreateStore"); + // m_comms.RegisterScriptInvocation(this, "JsonAttachObjectStore"); // m_comms.RegisterScriptInvocation(this, "JsonDestroyStore"); // m_comms.RegisterScriptInvocation(this, "JsonTestStore"); @@ -214,6 +215,21 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore #endregion #region ScriptInvocationInteface + // ----------------------------------------------------------------- + /// + /// + /// + // ----------------------------------------------------------------- + [ScriptInvocation] + public UUID JsonAttachObjectStore(UUID hostID, UUID scriptID) + { + UUID uuid = UUID.Zero; + if (! m_store.AttachObjectStore(hostID)) + GenerateRuntimeError("Failed to create Json store"); + + return hostID; + } + // ----------------------------------------------------------------- /// ///