OSSL Script Library/ModInvoke
From OpenSimulator
(→The Region Module) |
(→The Region Module) |
||
Line 29: | Line 29: | ||
using OpenSim.Region.Framework.Scenes; | using OpenSim.Region.Framework.Scenes; | ||
using System.Collections.Generic; | using System.Collections.Generic; | ||
+ | using OpenMetaverse; | ||
+ | using OpenMetaverse.StructuredData; | ||
[assembly: Addin("ModInvokeTest", "0.1")] | [assembly: Addin("ModInvokeTest", "0.1")] |
Revision as of 21:34, 24 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
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 log4net; using Nini.Config; using OpenSim.Framework; using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Scenes; using System.Collections.Generic; using OpenMetaverse; using OpenMetaverse.StructuredData; [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; #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)); m_comms.RegisterScriptInvocation("ModTest4",SI_ModTest4,new Type[] { typeof(UUID) }, typeof(UUID)); m_comms.RegisterScriptInvocation("ModTest5",SI_ModTest5, new Type[] { typeof(OpenMetaverse.Vector3) },typeof(OpenMetaverse.Vector3)); m_comms.RegisterScriptInvocation("ModTest6",SI_ModTest6, new Type[] { typeof(OpenMetaverse.Quaternion)}, typeof(OpenMetaverse.Quaternion)); m_comms.RegisterScriptInvocation("ModTest7",SI_ModTest7, new Type[] { typeof(int), typeof(string) },typeof(object[])); m_comms.RegisterScriptInvocation("ModTest8",SI_ModTest8, new Type[] { typeof(object[]) },typeof(object[])); } public Type ReplaceableInterface { get { return null; } } #endregion #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; } protected object SI_ModTest4(UUID scriptID, object[] parray) { UUID value = (UUID)parray[0]; return value; } protected object SI_ModTest5(UUID scriptID, object[] parray) { OpenMetaverse.Vector3 value = (OpenMetaverse.Vector3)parray[0]; return value; } protected object SI_ModTest6(UUID scriptID, object[] parray) { OpenMetaverse.Quaternion value = (OpenMetaverse.Quaternion)parray[0]; return value; } protected object SI_ModTest7(UUID scriptID, object[] parray) { int count = (int)parray[0]; string val = (string)parray[1]; object[] result = new object[count]; for (int i = 0; i < count; i++) result[i] = val; return result; } protected object SI_ModTest8(UUID scriptID, object[] parray) { object[] lparm = (object[])parray[0]; 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.
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)); } }