Merge branch 'master' into careminster

avinationmerge
Melanie 2012-08-31 14:40:30 +01:00
commit c0feaf54de
3 changed files with 68 additions and 11 deletions

View File

@ -46,9 +46,38 @@ namespace OpenSim.Region.Framework.Interfaces
/// </summary>
event ScriptCommand OnScriptCommand;
/// <summary>
/// Register an instance method as a script call by method name
/// </summary>
/// <param name="target"></param>
/// <param name="method"></param>
void RegisterScriptInvocation(object target, string method);
/// <summary>
/// Register a static or instance method as a script call by method info
/// </summary>
/// <param name="target">If target is a Type object, will assume method is static.</param>
/// <param name="method"></param>
void RegisterScriptInvocation(object target, MethodInfo method);
/// <summary>
/// Register one or more instance methods as script calls by method name
/// </summary>
/// <param name="target"></param>
/// <param name="methods"></param>
void RegisterScriptInvocation(object target, string[] methods);
/// <summary>
/// Register one or more static methods as script calls by method name
/// </summary>
/// <param name="target"></param>
/// <param name="methods"></param>
void RegisterScriptInvocation(Type target, string[] methods);
/// <summary>
/// Returns an array of all registered script calls
/// </summary>
/// <returns></returns>
Delegate[] GetScriptInvocationList();
Delegate LookupScriptInvocation(string fname);

View File

@ -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<Type> 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()
{

View File

@ -341,7 +341,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
return GetLinkParts(m_host, linkType);
}
private List<SceneObjectPart> GetLinkParts(SceneObjectPart part, int linkType)
public static List<SceneObjectPart> GetLinkParts(SceneObjectPart part, int linkType)
{
List<SceneObjectPart> ret = new List<SceneObjectPart>();
if (part == null || part.ParentGroup == null || part.ParentGroup.IsDeleted)