OSSL Script Library/ModInvoke

From OpenSimulator

(Difference between revisions)
Jump to: navigation, search
(Created page with "You can invoke functions defined in a region module from a script using the modInvoke() family of functions. == Enabling modInvoke() == The first thing is to enable modSendCom...")
 
(Introduction)
 
(16 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
== Introduction ==
 +
 
You can invoke functions defined in a region module from a script using the modInvoke() family of functions.  
 
You can invoke functions defined in a region module from a script using the modInvoke() family of functions.  
 +
 +
This functionality is in OpenSimulator 0.7.4-dev and after.
 +
 +
See also [[OSSL_Script_Library/ModSendCommand]] for a version of this functionality which allows passing of data between scripts and region modules through a single modSendCommand().
 +
 +
At the moment, if you want to insert script functions which take more than 3 parameters, on Windows you must either be compiling OpenSimulator yourself with .NET 4.0 or on Mono using Mono 2.8 or higher (preferably 2.10 or higher).
  
 
== Enabling modInvoke() ==
 
== Enabling modInvoke() ==
Line 10: Line 18:
  
 
is set to true and uncommented.
 
is set to true and uncommented.
 
  
 
== The Region Module ==
 
== The Region Module ==
  
/*
+
Here's the region module that implements several functions to be provided to scripts in the region.  
* Copyright (c) Contributors
+
 
* See CONTRIBUTORS.TXT for a full list of copyright holders.
+
<source lang = "csharp">
*
+
* Redistribution and use in source and binary forms, with or without
+
* modification, are permitted provided that the following conditions are met:
+
*    * Redistributions of source code must retain the above copyright
+
*      notice, this list of conditions and the following disclaimer.
+
*    * Redistributions in binary form must reproduce the above copyright
+
*      notice, this list of conditions and the following disclaimer in the
+
*      documentation and/or other materials provided with the distribution.
+
*    * Neither the name of the OpenSim Project nor the
+
*      names of its contributors may be used to endorse or promote products
+
*      derived from this software without specific prior written permission.
+
*
+
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
*/
+
 
using Mono.Addins;
 
using Mono.Addins;
  
Line 68: Line 52:
 
     public class ModInvokeTestModule  : INonSharedRegionModule
 
     public class ModInvokeTestModule  : INonSharedRegionModule
 
     {
 
     {
 +
        private static readonly ILog m_log =
 +
            LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 +
 +
        private IConfig m_config = null;
 +
        private bool m_enabled = true;
 
         private Scene m_scene = null;
 
         private Scene m_scene = null;
 +
       
 
         private IScriptModuleComms m_comms;
 
         private IScriptModuleComms m_comms;
 
          
 
          
Line 78: Line 68:
 
         }
 
         }
  
         public void Initialise(IConfigSource config) {}
+
         public void Initialise(IConfigSource config)
         public void PostInitialise(){}
+
        {
         public void Close(){}
+
            m_log.WarnFormat("[ModInvoke] start configuration");
         public void AddRegion(Scene scene){}
+
           
         public void RemoveRegion(Scene scene){}
+
            try
 +
            {
 +
                if ((m_config = config.Configs["ModInvoke"]) != null)
 +
                    m_enabled = m_config.GetBoolean("Enabled", m_enabled);
 +
            }
 +
            catch (Exception e)
 +
            {
 +
                m_log.ErrorFormat("[ModInvoke] initialization error: {0}",e.Message);
 +
                return;
 +
            }
 +
 
 +
            m_log.ErrorFormat("[ModInvoke] module {0} enabled",(m_enabled ? "is" : "is not"));
 +
        }
 +
 
 +
         public void PostInitialise()
 +
        {
 +
            if (m_enabled) {}
 +
        }
 +
 
 +
         public void Close() { }
 +
         public void AddRegion(Scene scene) { }
 +
         public void RemoveRegion(Scene scene) { }
  
 
         public void RegionLoaded(Scene scene)
 
         public void RegionLoaded(Scene scene)
 
         {
 
         {
             m_scene = scene;
+
             if (m_enabled)
            m_comms = m_scene.RequestModuleInterface<IScriptModuleComms>();
+
            {
 +
                m_scene = scene;
 +
                m_comms = m_scene.RequestModuleInterface<IScriptModuleComms>();
 +
                if (m_comms == null)
 +
                {
 +
                    m_log.WarnFormat("[ModInvoke] ScriptModuleComms interface not defined");
 +
                    m_enabled = false;
  
            m_comms.RegisterScriptInvocation("ModTest0",SI_ModTest0,new Type[] { }, typeof(string));
+
                    return;
            m_comms.RegisterScriptInvocation("ModTest1",SI_ModTest1,new Type[] { typeof(string) }, typeof(string));
+
                }
            m_comms.RegisterScriptInvocation("ModTest2",SI_ModTest2,new Type[] { typeof(int) }, typeof(int));
+
 
            m_comms.RegisterScriptInvocation("ModTest3",SI_ModTest3,new Type[] { typeof(float) }, typeof(float));
+
                m_comms.RegisterScriptInvocation(this,"ModTest0");
 +
                m_comms.RegisterScriptInvocation(this,"ModTest1");
 +
                m_comms.RegisterScriptInvocation(this,"ModTest2");
 +
                m_comms.RegisterScriptInvocation(this,"ModTest3");
 +
                m_comms.RegisterScriptInvocation(this,"ModTest4");
 +
                m_comms.RegisterScriptInvocation(this,"ModTest5");
 +
                m_comms.RegisterScriptInvocation(this,"ModTest6");
 +
                m_comms.RegisterScriptInvocation(this,"ModTest7");
 +
                m_comms.RegisterScriptInvocation(this,"ModTest8");
 +
 
 +
                // Register some constants as well
 +
                m_comms.RegisterConstant("ModConstantInt1",25);
 +
                m_comms.RegisterConstant("ModConstantFloat1",25.000f);
 +
                m_comms.RegisterConstant("ModConstantString1","abcdefg");
 +
            }
 
         }
 
         }
  
Line 103: Line 134:
  
 
#region ScriptInvocationInteface
 
#region ScriptInvocationInteface
         protected object SI_ModTest0(UUID scriptID, object[] parray)
+
         public string ModTest0(UUID hostID, UUID scriptID)
 
         {
 
         {
 +
            m_log.WarnFormat("[ModInvoke] ModTest0 parameter");
 
             return "";
 
             return "";
 
         }
 
         }
  
         protected object SI_ModTest1(UUID scriptID, object[] parray)
+
         public string ModTest1(UUID hostID, UUID scriptID, string value)
 
         {
 
         {
             string value = (string)parray[0];
+
             m_log.WarnFormat("[ModInvoke] ModTest1 parameter: {0}",value);
 
             return value;
 
             return value;
 
         }
 
         }
  
      protected object SI_ModTest2(UUID scriptID, object[] parray)
+
        public int ModTest2(UUID hostID, UUID scriptID, int value)
      {
+
        {
             int value = (int)parray[0];
+
             m_log.WarnFormat("[ModInvoke] ModTest2 parameter: {0}",value);
 
             return value;
 
             return value;
 
         }
 
         }
  
         protected object SI_ModTest3(UUID scriptID, object[] parray)
+
         public float ModTest3(UUID hostID, UUID scriptID, float value)
 
         {
 
         {
             float value = (float)parray[0];
+
             m_log.WarnFormat("[ModInvoke] ModTest3 parameter: {0}",value);
 
             return value;
 
             return value;
 
         }
 
         }
 +
 +
        public UUID ModTest4(UUID hostID, UUID scriptID, UUID value)
 +
        {
 +
            m_log.WarnFormat("[ModInvoke] ModTest4 parameter: {0}",value.ToString());
 +
            return value;
 +
        }
 +
 +
        public OpenMetaverse.Vector3 ModTest5(UUID hostID, UUID scriptID, OpenMetaverse.Vector3 value)
 +
        {
 +
            m_log.WarnFormat("[ModInvoke] ModTest5 parameter: {0}",value.ToString());
 +
            return value;
 +
        }
 +
 +
        public OpenMetaverse.Quaternion ModTest6(UUID hostID, UUID scriptID, OpenMetaverse.Quaternion value)
 +
        {
 +
            m_log.WarnFormat("[ModInvoke] ModTest6 parameter: {0}",value.ToString());
 +
            return value;
 +
        }
 +
 +
        public object[] ModTest7(UUID hostID, UUID scriptID, int count, string val)
 +
        {
 +
            object[] result = new object[count];
 +
            for (int i = 0; i < count; i++)
 +
                result[i] = val;
 +
           
 +
            return result;
 +
        }
 +
 +
        public object[] ModTest8(UUID hostID, UUID scriptID, object[] lparm)
 +
        {
 +
            object[] result = new object[lparm.Length];
 +
 +
            for (int i = 0; i < lparm.Length; i++)
 +
                result[lparm.Length - i - 1] = lparm[i];
 +
           
 +
            return result;
 +
        }
 +
 
#endregion
 
#endregion
 
     }
 
     }
 
}
 
}
</pre>
+
</source>
  
 
The module registers functions through the IScriptModuleComms RegisterScriptInvocation() method. This method takes the name of the function, an invocation delegate, an array of types used to validate parameters to the function, and a return type.  
 
The module registers functions through the IScriptModuleComms RegisterScriptInvocation() method. This method takes the name of the function, an invocation delegate, an array of types used to validate parameters to the function, and a return type.  
  
 
The functions in the region module can assume that the parameters passed through the argument array match the signature that was registered. That is, the function in the region module does not need to perform any kind of type checking.
 
The functions in the region module can assume that the parameters passed through the argument array match the signature that was registered. That is, the function in the region module does not need to perform any kind of type checking.
 
  
 
== The In-world Script ==
 
== The In-world Script ==
Line 139: Line 208:
 
Here's the in-world script that calls the functions defined in the region module.  
 
Here's the in-world script that calls the functions defined in the region module.  
  
<pre>
+
<source lang="lsl">
 
default
 
default
 
{
 
{
Line 153: Line 222:
 
         float v3 = ModTest3(3.14) + 4.56;
 
         float v3 = ModTest3(3.14) + 4.56;
 
         llOwnerSay("ModTest3: " + (string)v3);
 
         llOwnerSay("ModTest3: " + (string)v3);
 +
       
 +
        key v4 = ModTest4(llGetKey());
 +
        llOwnerSay("ModTest4: " + (string)v4);
 +
       
 +
        vector v5 = ModTest5(llGetPos());
 +
        llOwnerSay("ModTest5: " + (string)v5);
 +
       
 +
        rotation v6 = ModTest6(llGetRot());
 +
        llOwnerSay("ModTest6: " + (string)v6);
 +
       
 +
        list v7 = ModTest7(5,"hello");
 +
        llOwnerSay("ModTest7: " + (string)llGetListLength(v7));
 +
        llOwnerSay("ModTest7: " + llList2CSV(v7));
 +
       
 +
        list v8 = ["a", 1, llGetKey(), llGetPos(), llGetRot()];
 +
        list v9 = ModTest8(v8);
 +
        llOwnerSay("ModTest8: " + (string)llGetListLength(v9));
 +
        llOwnerSay("ModTest8: " + llList2CSV(v9));
 +
       
 
     }
 
     }
 
}
 
}
</pre>
+
</source>
 +
 
 +
== Notes ==
 +
 
 +
One thing to note regarding parameter and return types: the LSL_Types are not available in the region module; however, all of the OpenMetaverse types are. Use UUID for a key and string, int, OpenMetaverse.Vector3, etc for other types.
 +
 
 +
[[Category:Scripts]]

Latest revision as of 12:41, 25 September 2013

Contents

[edit] Introduction

You can invoke functions defined in a region module from a script using the modInvoke() family of functions.

This functionality is in OpenSimulator 0.7.4-dev and after.

See also OSSL_Script_Library/ModSendCommand for a version of this functionality which allows passing of data between scripts and region modules through a single modSendCommand().

At the moment, if you want to insert script functions which take more than 3 parameters, on Windows you must either be compiling OpenSimulator yourself with .NET 4.0 or on Mono using Mono 2.8 or higher (preferably 2.10 or higher).

[edit] Enabling modInvoke()

The first thing is to enable modSendCommand() in OpenSim.ini. Make sure the line

AllowMODFunctions = true

is set to true and uncommented.

[edit] The Region Module

Here's the region module that implements several functions to be provided to scripts in the region.

using Mono.Addins;
 
using System;
using System.Reflection;
using System.Threading;
using System.Text;
using System.Net;
using System.Net.Sockets;
using log4net;
using Nini.Config;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
using OpenSim.Framework;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
using System.Collections.Generic;
using System.Text.RegularExpressions;
 
 
[assembly: Addin("ModInvokeTest", "0.1")]
[assembly: AddinDependency("OpenSim", "0.5")]
 
namespace ModInvokeTest
{
    [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
 
    public class ModInvokeTestModule  : INonSharedRegionModule
    {
        private static readonly ILog m_log =
            LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 
        private IConfig m_config = null;
        private bool m_enabled = true;
        private Scene m_scene = null;
 
        private IScriptModuleComms m_comms;
 
#region IRegionModule Members
 
        public string Name
        {
            get { return this.GetType().Name; }
        }
 
        public void Initialise(IConfigSource config)
        {
            m_log.WarnFormat("[ModInvoke] start configuration");
 
            try 
            {
                if ((m_config = config.Configs["ModInvoke"]) != null)
                    m_enabled = m_config.GetBoolean("Enabled", m_enabled);
            }
            catch (Exception e)
            {
                m_log.ErrorFormat("[ModInvoke] initialization error: {0}",e.Message);
                return;
            }
 
            m_log.ErrorFormat("[ModInvoke] module {0} enabled",(m_enabled ? "is" : "is not"));
        }
 
        public void PostInitialise()
        {
            if (m_enabled) {}
        }
 
        public void Close() { }
        public void AddRegion(Scene scene) { }
        public void RemoveRegion(Scene scene)  { }
 
        public void RegionLoaded(Scene scene)
        {
            if (m_enabled)
            {
                m_scene = scene;
                m_comms = m_scene.RequestModuleInterface<IScriptModuleComms>();
                if (m_comms == null)
                {
                    m_log.WarnFormat("[ModInvoke] ScriptModuleComms interface not defined");
                    m_enabled = false;
 
                    return;
                }
 
                m_comms.RegisterScriptInvocation(this,"ModTest0");
                m_comms.RegisterScriptInvocation(this,"ModTest1");
                m_comms.RegisterScriptInvocation(this,"ModTest2");
                m_comms.RegisterScriptInvocation(this,"ModTest3");
                m_comms.RegisterScriptInvocation(this,"ModTest4");
                m_comms.RegisterScriptInvocation(this,"ModTest5");
                m_comms.RegisterScriptInvocation(this,"ModTest6");
                m_comms.RegisterScriptInvocation(this,"ModTest7");
                m_comms.RegisterScriptInvocation(this,"ModTest8");
 
                // Register some constants as well
                m_comms.RegisterConstant("ModConstantInt1",25);
                m_comms.RegisterConstant("ModConstantFloat1",25.000f);
                m_comms.RegisterConstant("ModConstantString1","abcdefg");
            }
        }
 
        public Type ReplaceableInterface
        {
            get { return null; }
        }
 
#endregion
 
#region ScriptInvocationInteface
        public string ModTest0(UUID hostID, UUID scriptID)
        {
            m_log.WarnFormat("[ModInvoke] ModTest0 parameter");
            return "";
        }
 
        public string ModTest1(UUID hostID, UUID scriptID, string value)
        {
            m_log.WarnFormat("[ModInvoke] ModTest1 parameter: {0}",value);
            return value;
        }
 
        public int ModTest2(UUID hostID, UUID scriptID, int value)
        {
            m_log.WarnFormat("[ModInvoke] ModTest2 parameter: {0}",value);
            return value;
        }
 
        public float ModTest3(UUID hostID, UUID scriptID, float value)
        {
            m_log.WarnFormat("[ModInvoke] ModTest3 parameter: {0}",value);
            return value;
        }
 
        public UUID ModTest4(UUID hostID, UUID scriptID, UUID value)
        {
            m_log.WarnFormat("[ModInvoke] ModTest4 parameter: {0}",value.ToString());
            return value;
        }
 
        public OpenMetaverse.Vector3 ModTest5(UUID hostID, UUID scriptID, OpenMetaverse.Vector3 value)
        {
            m_log.WarnFormat("[ModInvoke] ModTest5 parameter: {0}",value.ToString());
            return value;
        }
 
        public OpenMetaverse.Quaternion ModTest6(UUID hostID, UUID scriptID, OpenMetaverse.Quaternion value)
        {
            m_log.WarnFormat("[ModInvoke] ModTest6 parameter: {0}",value.ToString());
            return value;
        }
 
        public object[] ModTest7(UUID hostID, UUID scriptID, int count, string val)
        {
            object[] result = new object[count];
            for (int i = 0; i < count; i++)
                result[i] = val;
 
            return result;
        }
 
        public object[] ModTest8(UUID hostID, UUID scriptID, object[] lparm)
        {
            object[] result = new object[lparm.Length];
 
            for (int i = 0; i < lparm.Length; i++)
                result[lparm.Length - i - 1] = lparm[i];
 
            return result;
        }
 
#endregion
    }
}

The module registers functions through the IScriptModuleComms RegisterScriptInvocation() method. This method takes the name of the function, an invocation delegate, an array of types used to validate parameters to the function, and a return type.

The functions in the region module can assume that the parameters passed through the argument array match the signature that was registered. That is, the function in the region module does not need to perform any kind of type checking.

[edit] The In-world Script

Here's the in-world script that calls the functions defined in the region module.

default
{
    state_entry()
    {
        llSay(0, "Script running");
        llOwnerSay("ModTest0: " + ModTest0());
        llOwnerSay("ModTest1: " + ModTest1("one"));
 
        integer v2 = ModTest2(2) + 2;
        llOwnerSay("ModTest2: " + (string)v2);
 
        float v3 = ModTest3(3.14) + 4.56;
        llOwnerSay("ModTest3: " + (string)v3);
 
        key v4 = ModTest4(llGetKey());
        llOwnerSay("ModTest4: " + (string)v4);
 
        vector v5 = ModTest5(llGetPos());
        llOwnerSay("ModTest5: " + (string)v5);
 
        rotation v6 = ModTest6(llGetRot());
        llOwnerSay("ModTest6: " + (string)v6);
 
        list v7 = ModTest7(5,"hello");
        llOwnerSay("ModTest7: " + (string)llGetListLength(v7));
        llOwnerSay("ModTest7: " + llList2CSV(v7));
 
        list v8 = ["a", 1, llGetKey(), llGetPos(), llGetRot()];
        list v9 = ModTest8(v8);
        llOwnerSay("ModTest8: " + (string)llGetListLength(v9));
        llOwnerSay("ModTest8: " + llList2CSV(v9));
 
    }
}

[edit] Notes

One thing to note regarding parameter and return types: the LSL_Types are not available in the region module; however, all of the OpenMetaverse types are. Use UUID for a key and string, int, OpenMetaverse.Vector3, etc for other types.

Personal tools
General
About This Wiki