diff --git a/OpenSim/Region/Framework/Interfaces/IScriptModuleComms.cs b/OpenSim/Region/Framework/Interfaces/IScriptModuleComms.cs index ed71a951cb..dae7c00207 100644 --- a/OpenSim/Region/Framework/Interfaces/IScriptModuleComms.cs +++ b/OpenSim/Region/Framework/Interfaces/IScriptModuleComms.cs @@ -46,9 +46,38 @@ namespace OpenSim.Region.Framework.Interfaces /// event ScriptCommand OnScriptCommand; + /// + /// Register an instance method as a script call by method name + /// + /// + /// void RegisterScriptInvocation(object target, string method); + + /// + /// Register a static or instance method as a script call by method info + /// + /// If target is a Type object, will assume method is static. + /// void RegisterScriptInvocation(object target, MethodInfo method); + + /// + /// Register one or more instance methods as script calls by method name + /// + /// + /// void RegisterScriptInvocation(object target, string[] methods); + + /// + /// Register one or more static methods as script calls by method name + /// + /// + /// + void RegisterScriptInvocation(Type target, string[] methods); + + /// + /// Returns an array of all registered script calls + /// + /// Delegate[] GetScriptInvocationList(); Delegate LookupScriptInvocation(string fname); diff --git a/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs b/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs index 705a84700d..8d100e82a8 100644 --- a/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs +++ b/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs @@ -130,13 +130,25 @@ namespace OpenSim.Region.OptionalModules.Scripting.ScriptModuleComms m_scriptModule.PostScriptEvent(script, "link_message", args); } + private static MethodInfo GetMethodInfoFromType(Type target, string meth, bool searchInstanceMethods) + { + BindingFlags getMethodFlags = + BindingFlags.NonPublic | BindingFlags.Public; + + if (searchInstanceMethods) + getMethodFlags |= BindingFlags.Instance; + else + getMethodFlags |= BindingFlags.Static; + + return target.GetMethod(meth, getMethodFlags); + } + public void RegisterScriptInvocation(object target, string meth) { - MethodInfo mi = target.GetType().GetMethod(meth, - BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + MethodInfo mi = GetMethodInfoFromType(target.GetType(), meth, true); if (mi == null) { - m_log.WarnFormat("[MODULE COMMANDS] Failed to register method {0}",meth); + m_log.WarnFormat("[MODULE COMMANDS] Failed to register method {0}", meth); return; } @@ -154,35 +166,51 @@ namespace OpenSim.Region.OptionalModules.Scripting.ScriptModuleComms m_log.DebugFormat("[MODULE COMMANDS] Register method {0} from type {1}", mi.Name, target.GetType().Name); Type delegateType; - var typeArgs = mi.GetParameters() + List typeArgs = mi.GetParameters() .Select(p => p.ParameterType) .ToList(); if (mi.ReturnType == typeof(void)) { - delegateType = Expression.GetActionType(typeArgs.ToArray()); + delegateType = Expression.GetActionType(typeArgs.ToArray()); } else { - typeArgs.Add(mi.ReturnType); - delegateType = Expression.GetFuncType(typeArgs.ToArray()); + typeArgs.Add(mi.ReturnType); + delegateType = Expression.GetFuncType(typeArgs.ToArray()); } - Delegate fcall = Delegate.CreateDelegate(delegateType, target, mi); + Delegate fcall; + if (!(target is Type)) + fcall = Delegate.CreateDelegate(delegateType, target, mi); + else + fcall = Delegate.CreateDelegate(delegateType, (Type)target, mi.Name); lock (m_scriptInvocation) { - ParameterInfo[] parameters = fcall.Method.GetParameters (); + ParameterInfo[] parameters = fcall.Method.GetParameters(); if (parameters.Length < 2) // Must have two UUID params return; // Hide the first two parameters Type[] parmTypes = new Type[parameters.Length - 2]; - for (int i = 2 ; i < parameters.Length ; i++) + for (int i = 2; i < parameters.Length; i++) parmTypes[i - 2] = parameters[i].ParameterType; m_scriptInvocation[fcall.Method.Name] = new ScriptInvocationData(fcall.Method.Name, fcall, parmTypes, fcall.Method.ReturnType); } } + + public void RegisterScriptInvocation(Type target, string[] methods) + { + foreach (string method in methods) + { + MethodInfo mi = GetMethodInfoFromType(target, method, false); + if (mi == null) + m_log.WarnFormat("[MODULE COMMANDS] Failed to register method {0}", method); + else + RegisterScriptInvocation(target, mi); + } + } public Delegate[] GetScriptInvocationList() { diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs index d5281605b8..bf292885fe 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs @@ -341,7 +341,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api return GetLinkParts(m_host, linkType); } - private List GetLinkParts(SceneObjectPart part, int linkType) + public static List GetLinkParts(SceneObjectPart part, int linkType) { List ret = new List(); if (part == null || part.ParentGroup == null || part.ParentGroup.IsDeleted)