Add support for key, vector, rotation and list types for both

arguments and return values to the modInvoke family of functions.

See http://opensimulator.org/wiki/OSSL_Script_Library/ModInvoke
0.7.4.1
Mic Bowman 2012-03-24 22:43:42 -07:00
parent 164706043d
commit a14437ad5a
4 changed files with 238 additions and 75 deletions

View File

@ -147,6 +147,14 @@ namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms
return "modInvokeI";
else if (sid.ReturnType == typeof(float))
return "modInvokeF";
else if (sid.ReturnType == typeof(UUID))
return "modInvokeK";
else if (sid.ReturnType == typeof(OpenMetaverse.Vector3))
return "modInvokeV";
else if (sid.ReturnType == typeof(OpenMetaverse.Quaternion))
return "modInvokeR";
else if (sid.ReturnType == typeof(object[]))
return "modInvokeL";
}
}

View File

@ -120,33 +120,101 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
///
/// </summary>
/// <param name="fname">The name of the function to invoke</param>
/// <param name="fname">List of parameters</param>
/// <param name="parms">List of parameters</param>
/// <returns>string result of the invocation</returns>
public string modInvokeS(string fname, params object[] parms)
public LSL_String modInvokeS(string fname, params object[] parms)
{
Type returntype = m_comms.LookupReturnType(fname);
if (returntype != typeof(string))
MODError(String.Format("return type mismatch for {0}",fname));
return (string)modInvoke(fname,parms);
string result = (string)modInvoke(fname,parms);
return new LSL_String(result);
}
public int modInvokeI(string fname, params object[] parms)
public LSL_Integer modInvokeI(string fname, params object[] parms)
{
Type returntype = m_comms.LookupReturnType(fname);
if (returntype != typeof(int))
MODError(String.Format("return type mismatch for {0}",fname));
return (int)modInvoke(fname,parms);
int result = (int)modInvoke(fname,parms);
return new LSL_Integer(result);
}
public float modInvokeF(string fname, params object[] parms)
public LSL_Float modInvokeF(string fname, params object[] parms)
{
Type returntype = m_comms.LookupReturnType(fname);
if (returntype != typeof(float))
MODError(String.Format("return type mismatch for {0}",fname));
return (float)modInvoke(fname,parms);
float result = (float)modInvoke(fname,parms);
return new LSL_Float(result);
}
public LSL_Key modInvokeK(string fname, params object[] parms)
{
Type returntype = m_comms.LookupReturnType(fname);
if (returntype != typeof(UUID))
MODError(String.Format("return type mismatch for {0}",fname));
UUID result = (UUID)modInvoke(fname,parms);
return new LSL_Key(result.ToString());
}
public LSL_Vector modInvokeV(string fname, params object[] parms)
{
Type returntype = m_comms.LookupReturnType(fname);
if (returntype != typeof(OpenMetaverse.Vector3))
MODError(String.Format("return type mismatch for {0}",fname));
OpenMetaverse.Vector3 result = (OpenMetaverse.Vector3)modInvoke(fname,parms);
return new LSL_Vector(result.X,result.Y,result.Z);
}
public LSL_Rotation modInvokeR(string fname, params object[] parms)
{
Type returntype = m_comms.LookupReturnType(fname);
if (returntype != typeof(OpenMetaverse.Quaternion))
MODError(String.Format("return type mismatch for {0}",fname));
OpenMetaverse.Quaternion result = (OpenMetaverse.Quaternion)modInvoke(fname,parms);
return new LSL_Rotation(result.X,result.Y,result.Z,result.W);
}
public LSL_List modInvokeL(string fname, params object[] parms)
{
Type returntype = m_comms.LookupReturnType(fname);
if (returntype != typeof(object[]))
MODError(String.Format("return type mismatch for {0}",fname));
object[] result = (object[])modInvoke(fname,parms);
object[] llist = new object[result.Length];
for (int i = 0; i < result.Length; i++)
{
if (result[i] is string)
llist[i] = new LSL_String((string)result[i]);
else if (result[i] is int)
llist[i] = new LSL_Integer((int)result[i]);
else if (result[i] is float)
llist[i] = new LSL_Float((float)result[i]);
else if (result[i] is OpenMetaverse.Vector3)
{
OpenMetaverse.Vector3 vresult = (OpenMetaverse.Vector3)result[i];
llist[i] = new LSL_Vector(vresult.X,vresult.Y,vresult.Z);
}
else if (result[i] is OpenMetaverse.Quaternion)
{
OpenMetaverse.Quaternion qresult = (OpenMetaverse.Quaternion)result[i];
llist[i] = new LSL_Rotation(qresult.X,qresult.Y,qresult.Z,qresult.W);
}
else
{
MODError(String.Format("unknown list element returned by {0}",fname));
}
}
return new LSL_List(llist);
}
/// <summary>
@ -168,63 +236,30 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
MODError(String.Format("wrong number of parameters to function {0}",fname));
object[] convertedParms = new object[parms.Length];
for (int i = 0; i < parms.Length; i++)
convertedParms[i] = ConvertFromLSL(parms[i],signature[i]);
// now call the function, the contract with the function is that it will always return
// non-null but don't trust it completely
try
{
if (parms[i] is LSL_String)
{
if (signature[i] != typeof(string))
MODError(String.Format("parameter type mismatch in {0}; expecting {1}",fname,signature[i].Name));
object result = m_comms.InvokeOperation(m_itemID,fname,convertedParms);
if (result != null)
return result;
convertedParms[i] = (string)(LSL_String)parms[i];
}
else if (parms[i] is LSL_Integer)
{
if (signature[i] != typeof(int))
MODError(String.Format("parameter type mismatch in {0}; expecting {1}",fname,signature[i].Name));
convertedParms[i] = (int)(LSL_Integer)parms[i];
}
else if (parms[i] is LSL_Float)
{
if (signature[i] != typeof(float))
MODError(String.Format("parameter type mismatch in {0}; expecting {1}",fname,signature[i].Name));
convertedParms[i] = (float)(LSL_Float)parms[i];
}
else if (parms[i] is LSL_Key)
{
if (signature[i] != typeof(string))
MODError(String.Format("parameter type mismatch in {0}; expecting {1}",fname,signature[i].Name));
convertedParms[i] = (string)(LSL_Key)parms[i];
}
else if (parms[i] is LSL_Rotation)
{
if (signature[i] != typeof(string))
MODError(String.Format("parameter type mismatch in {0}; expecting {1}",fname,signature[i].Name));
convertedParms[i] = (string)(LSL_Rotation)parms[i];
}
else if (parms[i] is LSL_Vector)
{
if (signature[i] != typeof(string))
MODError(String.Format("parameter type mismatch in {0}; expecting {1}",fname,signature[i].Name));
convertedParms[i] = (string)(LSL_Vector)parms[i];
}
else
{
if (signature[i] != parms[i].GetType())
MODError(String.Format("parameter type mismatch in {0}; expecting {1}",fname,signature[i].Name));
convertedParms[i] = parms[i];
}
MODError(String.Format("Invocation of {0} failed; null return value",fname));
}
catch (Exception e)
{
MODError(String.Format("Invocation of {0} failed; {1}",fname,e.Message));
}
return m_comms.InvokeOperation(m_itemID,fname,convertedParms);
return null;
}
/// <summary>
/// Send a command to functions registered on an event
/// </summary>
public string modSendCommand(string module, string command, string k)
{
if (!m_MODFunctionsEnabled)
@ -239,5 +274,101 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
return req.ToString();
}
/// <summary>
/// </summary>
protected object ConvertFromLSL(object lslparm, Type type)
{
// ---------- String ----------
if (lslparm is LSL_String)
{
if (type == typeof(string))
return (string)(LSL_String)lslparm;
// Need to check for UUID since keys are often treated as strings
if (type == typeof(UUID))
return new UUID((string)(LSL_String)lslparm);
}
// ---------- Integer ----------
else if (lslparm is LSL_Integer)
{
if (type == typeof(int))
return (int)(LSL_Integer)lslparm;
}
// ---------- Float ----------
else if (lslparm is LSL_Float)
{
if (type == typeof(float))
return (float)(LSL_Float)lslparm;
}
// ---------- Key ----------
else if (lslparm is LSL_Key)
{
if (type == typeof(UUID))
return new UUID((LSL_Key)lslparm);
}
// ---------- Rotation ----------
else if (lslparm is LSL_Rotation)
{
if (type == typeof(OpenMetaverse.Quaternion))
{
LSL_Rotation rot = (LSL_Rotation)lslparm;
return new OpenMetaverse.Quaternion((float)rot.x,(float)rot.y,(float)rot.z,(float)rot.s);
}
}
// ---------- Vector ----------
else if (lslparm is LSL_Vector)
{
if (type == typeof(OpenMetaverse.Vector3))
{
LSL_Vector vect = (LSL_Vector)lslparm;
return new OpenMetaverse.Vector3((float)vect.x,(float)vect.y,(float)vect.z);
}
}
// ---------- List ----------
else if (lslparm is LSL_List)
{
if (type == typeof(object[]))
{
object[] plist = (lslparm as LSL_List).Data;
object[] result = new object[plist.Length];
for (int i = 0; i < plist.Length; i++)
{
if (plist[i] is LSL_String)
result[i] = (string)(LSL_String)plist[i];
else if (plist[i] is LSL_Integer)
result[i] = (int)(LSL_Integer)plist[i];
else if (plist[i] is LSL_Float)
result[i] = (float)(LSL_Float)plist[i];
else if (plist[i] is LSL_Key)
result[i] = new UUID((LSL_Key)plist[i]);
else if (plist[i] is LSL_Rotation)
{
LSL_Rotation rot = (LSL_Rotation)plist[i];
result[i] = new OpenMetaverse.Quaternion((float)rot.x,(float)rot.y,(float)rot.z,(float)rot.s);
}
else if (plist[i] is LSL_Vector)
{
LSL_Vector vect = (LSL_Vector)plist[i];
result[i] = new OpenMetaverse.Vector3((float)vect.x,(float)vect.y,(float)vect.z);
}
else
MODError("unknown LSL list element type");
}
return result;
}
}
MODError(String.Format("parameter type mismatch; expecting {0}",type.Name));
return null;
}
}
}

View File

@ -28,26 +28,26 @@
using System.Collections;
using OpenSim.Region.ScriptEngine.Interfaces;
using key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
using vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
namespace OpenSim.Region.ScriptEngine.Shared.Api.Interfaces
{
public interface IMOD_Api
{
// Invocation functions
string modInvokeS(string fname, params object[] parms);
int modInvokeI(string fname, params object[] parms);
float modInvokeF(string fname, params object[] parms);
// vector modInvokeV(string fname, params object[] parms);
// rotation modInvokeV(string fname, params object[] parms);
// key modInvokeK(string fname, params object[] parms);
// list modInvokeL(string fname, params object[] parms);
LSL_String modInvokeS(string fname, params object[] parms);
LSL_Integer modInvokeI(string fname, params object[] parms);
LSL_Float modInvokeF(string fname, params object[] parms);
LSL_Key modInvokeK(string fname, params object[] parms);
LSL_Vector modInvokeV(string fname, params object[] parms);
LSL_Rotation modInvokeR(string fname, params object[] parms);
LSL_List modInvokeL(string fname, params object[] parms);
//Module functions
string modSendCommand(string modules, string command, string k);

View File

@ -39,10 +39,14 @@ using integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
using vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
using rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
using key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase
{
@ -58,21 +62,41 @@ namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase
m_MOD_Functions = (IMOD_Api)api;
}
public string modInvokeS(string fname, params object[] parms)
public LSL_String modInvokeS(string fname, params object[] parms)
{
return m_MOD_Functions.modInvokeS(fname, parms);
}
public int modInvokeI(string fname, params object[] parms)
public LSL_Integer modInvokeI(string fname, params object[] parms)
{
return m_MOD_Functions.modInvokeI(fname, parms);
}
public float modInvokeF(string fname, params object[] parms)
public LSL_Float modInvokeF(string fname, params object[] parms)
{
return m_MOD_Functions.modInvokeF(fname, parms);
}
public LSL_Key modInvokeK(string fname, params object[] parms)
{
return m_MOD_Functions.modInvokeK(fname, parms);
}
public LSL_Vector modInvokeV(string fname, params object[] parms)
{
return m_MOD_Functions.modInvokeV(fname, parms);
}
public LSL_Rotation modInvokeR(string fname, params object[] parms)
{
return m_MOD_Functions.modInvokeR(fname, parms);
}
public LSL_List modInvokeL(string fname, params object[] parms)
{
return m_MOD_Functions.modInvokeL(fname, parms);
}
public string modSendCommand(string module, string command, string k)
{
return m_MOD_Functions.modSendCommand(module, command, k);