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...")
 
(The Region Module)
Line 14: Line 14:
 
== The Region Module ==
 
== The Region Module ==
  
/*
 
* Copyright (c) Contributors
 
* See CONTRIBUTORS.TXT for a full list of copyright holders.
 
*
 
* 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 46: Line 20:
 
using System.Threading;
 
using System.Threading;
 
using System.Text;
 
using System.Text;
using System.Net;
 
using System.Net.Sockets;
 
 
using log4net;
 
using log4net;
 
using Nini.Config;
 
using Nini.Config;
using OpenMetaverse;
 
using OpenMetaverse.StructuredData;
 
 
using OpenSim.Framework;
 
using OpenSim.Framework;
 
using OpenSim.Region.Framework.Interfaces;
 
using OpenSim.Region.Framework.Interfaces;
 
using OpenSim.Region.Framework.Scenes;
 
using OpenSim.Region.Framework.Scenes;
 
using System.Collections.Generic;
 
using System.Collections.Generic;
using System.Text.RegularExpressions;
 
 
 
              
 
              
 
[assembly: Addin("ModInvokeTest", "0.1")]
 
[assembly: Addin("ModInvokeTest", "0.1")]
Line 133: Line 101:
  
 
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 ==

Revision as of 13:53, 15 March 2012

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 modSendCommand() in OpenSim.ini. Make sure the line

AllowMODFunctions = true

is set to true and uncommented.


The Region Module

using Mono.Addins;

using System; using System.Reflection; using System.Threading; using System.Text; using log4net; using Nini.Config; using OpenSim.Framework; using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Scenes; using System.Collections.Generic;

[assembly: Addin("ModInvokeTest", "0.1")] [assembly: AddinDependency("OpenSim", "0.5")]

namespace ModInvokeTest {

   [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
   public class ModInvokeTestModule  : INonSharedRegionModule
   {
       private Scene m_scene = null;
       private IScriptModuleComms m_comms;
       
  1. region IRegionModule Members
       public string Name
       {
           get { return this.GetType().Name; }
       }
       public void Initialise(IConfigSource config) {}
       public void PostInitialise(){}
       public void Close(){}
       public void AddRegion(Scene scene){}
       public void RemoveRegion(Scene scene){}
       public void RegionLoaded(Scene scene)
       {
           m_scene = scene;
           m_comms = m_scene.RequestModuleInterface<IScriptModuleComms>();
           m_comms.RegisterScriptInvocation("ModTest0",SI_ModTest0,new Type[] { }, typeof(string));
           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));
       }
       public Type ReplaceableInterface
       {
           get { return null; }
       }
  1. endregion
  1. region ScriptInvocationInteface
       protected object SI_ModTest0(UUID scriptID, object[] parray)
       {
           return "";
       }
       protected object SI_ModTest1(UUID scriptID, object[] parray)
       {
           string value = (string)parray[0];
           return value;
       }
      protected object SI_ModTest2(UUID scriptID, object[] parray)
      {
           int value = (int)parray[0];
           return value;
       }
       protected object SI_ModTest3(UUID scriptID, object[] parray)
       {
           float value = (float)parray[0];
           return value;
       }
  1. endregion
   }

} </pre>

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 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);
    }
}
Personal tools
General
About This Wiki