diff options
Diffstat (limited to 'OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs')
-rw-r--r-- | OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs | 95 |
1 files changed, 85 insertions, 10 deletions
diff --git a/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs b/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs index a90362e..cab30de 100644 --- a/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs +++ b/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs | |||
@@ -35,6 +35,8 @@ using OpenSim.Region.Framework.Interfaces; | |||
35 | using OpenSim.Region.Framework.Scenes; | 35 | using OpenSim.Region.Framework.Scenes; |
36 | using Mono.Addins; | 36 | using Mono.Addins; |
37 | using OpenMetaverse; | 37 | using OpenMetaverse; |
38 | using System.Linq; | ||
39 | using System.Linq.Expressions; | ||
38 | 40 | ||
39 | namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms | 41 | namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms |
40 | { | 42 | { |
@@ -47,15 +49,15 @@ namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms | |||
47 | #region ScriptInvocation | 49 | #region ScriptInvocation |
48 | protected class ScriptInvocationData | 50 | protected class ScriptInvocationData |
49 | { | 51 | { |
50 | public ScriptInvocation ScriptInvocationFn { get; private set; } | 52 | public Delegate ScriptInvocationDelegate { get; private set; } |
51 | public string FunctionName { get; private set; } | 53 | public string FunctionName { get; private set; } |
52 | public Type[] TypeSignature { get; private set; } | 54 | public Type[] TypeSignature { get; private set; } |
53 | public Type ReturnType { get; private set; } | 55 | public Type ReturnType { get; private set; } |
54 | 56 | ||
55 | public ScriptInvocationData(string fname, ScriptInvocation fn, Type[] callsig, Type returnsig) | 57 | public ScriptInvocationData(string fname, Delegate fn, Type[] callsig, Type returnsig) |
56 | { | 58 | { |
57 | FunctionName = fname; | 59 | FunctionName = fname; |
58 | ScriptInvocationFn = fn; | 60 | ScriptInvocationDelegate = fn; |
59 | TypeSignature = callsig; | 61 | TypeSignature = callsig; |
60 | ReturnType = returnsig; | 62 | ReturnType = returnsig; |
61 | } | 63 | } |
@@ -126,14 +128,72 @@ namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms | |||
126 | m_scriptModule.PostScriptEvent(script, "link_message", args); | 128 | m_scriptModule.PostScriptEvent(script, "link_message", args); |
127 | } | 129 | } |
128 | 130 | ||
129 | public void RegisterScriptInvocation(string fname, ScriptInvocation fcall, Type[] csig, Type rsig) | 131 | public void RegisterScriptInvocation(object target, string meth) |
130 | { | 132 | { |
133 | MethodInfo mi = target.GetType().GetMethod(meth, | ||
134 | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); | ||
135 | if (mi == null) | ||
136 | { | ||
137 | m_log.WarnFormat("[MODULE COMMANDS] Failed to register method {0}",meth); | ||
138 | return; | ||
139 | } | ||
140 | |||
141 | RegisterScriptInvocation(target, mi); | ||
142 | } | ||
143 | |||
144 | public void RegisterScriptInvocation(object target, string[] meth) | ||
145 | { | ||
146 | foreach (string m in meth) | ||
147 | RegisterScriptInvocation(target, m); | ||
148 | } | ||
149 | |||
150 | public void RegisterScriptInvocation(object target, MethodInfo mi) | ||
151 | { | ||
152 | m_log.DebugFormat("[MODULE COMMANDS] Register method {0} from type {1}", mi.Name, target.GetType().Name); | ||
153 | |||
154 | Type delegateType; | ||
155 | var typeArgs = mi.GetParameters() | ||
156 | .Select(p => p.ParameterType) | ||
157 | .ToList(); | ||
158 | |||
159 | if (mi.ReturnType == typeof(void)) | ||
160 | { | ||
161 | delegateType = Expression.GetActionType(typeArgs.ToArray()); | ||
162 | } | ||
163 | else | ||
164 | { | ||
165 | typeArgs.Add(mi.ReturnType); | ||
166 | delegateType = Expression.GetFuncType(typeArgs.ToArray()); | ||
167 | } | ||
168 | |||
169 | Delegate fcall = Delegate.CreateDelegate(delegateType, target, mi); | ||
170 | |||
131 | lock (m_scriptInvocation) | 171 | lock (m_scriptInvocation) |
132 | { | 172 | { |
133 | m_scriptInvocation[fname] = new ScriptInvocationData(fname,fcall,csig,rsig); | 173 | ParameterInfo[] parameters = fcall.Method.GetParameters (); |
174 | if (parameters.Length < 2) // Must have two UUID params | ||
175 | return; | ||
176 | |||
177 | // Hide the first two parameters | ||
178 | Type[] parmTypes = new Type[parameters.Length - 2]; | ||
179 | for (int i = 2 ; i < parameters.Length ; i++) | ||
180 | parmTypes[i - 2] = parameters[i].ParameterType; | ||
181 | m_scriptInvocation[fcall.Method.Name] = new ScriptInvocationData(fcall.Method.Name, fcall, parmTypes, fcall.Method.ReturnType); | ||
134 | } | 182 | } |
135 | } | 183 | } |
136 | 184 | ||
185 | public Delegate[] GetScriptInvocationList() | ||
186 | { | ||
187 | List<Delegate> ret = new List<Delegate>(); | ||
188 | |||
189 | lock (m_scriptInvocation) | ||
190 | { | ||
191 | foreach (ScriptInvocationData d in m_scriptInvocation.Values) | ||
192 | ret.Add(d.ScriptInvocationDelegate); | ||
193 | } | ||
194 | return ret.ToArray(); | ||
195 | } | ||
196 | |||
137 | public string LookupModInvocation(string fname) | 197 | public string LookupModInvocation(string fname) |
138 | { | 198 | { |
139 | lock (m_scriptInvocation) | 199 | lock (m_scriptInvocation) |
@@ -147,19 +207,29 @@ namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms | |||
147 | return "modInvokeI"; | 207 | return "modInvokeI"; |
148 | else if (sid.ReturnType == typeof(float)) | 208 | else if (sid.ReturnType == typeof(float)) |
149 | return "modInvokeF"; | 209 | return "modInvokeF"; |
210 | else if (sid.ReturnType == typeof(UUID)) | ||
211 | return "modInvokeK"; | ||
212 | else if (sid.ReturnType == typeof(OpenMetaverse.Vector3)) | ||
213 | return "modInvokeV"; | ||
214 | else if (sid.ReturnType == typeof(OpenMetaverse.Quaternion)) | ||
215 | return "modInvokeR"; | ||
216 | else if (sid.ReturnType == typeof(object[])) | ||
217 | return "modInvokeL"; | ||
218 | |||
219 | m_log.WarnFormat("[MODULE COMMANDS] failed to find match for {0} with return type {1}",fname,sid.ReturnType.Name); | ||
150 | } | 220 | } |
151 | } | 221 | } |
152 | 222 | ||
153 | return null; | 223 | return null; |
154 | } | 224 | } |
155 | 225 | ||
156 | public ScriptInvocation LookupScriptInvocation(string fname) | 226 | public Delegate LookupScriptInvocation(string fname) |
157 | { | 227 | { |
158 | lock (m_scriptInvocation) | 228 | lock (m_scriptInvocation) |
159 | { | 229 | { |
160 | ScriptInvocationData sid; | 230 | ScriptInvocationData sid; |
161 | if (m_scriptInvocation.TryGetValue(fname,out sid)) | 231 | if (m_scriptInvocation.TryGetValue(fname,out sid)) |
162 | return sid.ScriptInvocationFn; | 232 | return sid.ScriptInvocationDelegate; |
163 | } | 233 | } |
164 | 234 | ||
165 | return null; | 235 | return null; |
@@ -189,10 +259,15 @@ namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms | |||
189 | return null; | 259 | return null; |
190 | } | 260 | } |
191 | 261 | ||
192 | public object InvokeOperation(UUID scriptid, string fname, params object[] parms) | 262 | public object InvokeOperation(UUID hostid, UUID scriptid, string fname, params object[] parms) |
193 | { | 263 | { |
194 | ScriptInvocation fn = LookupScriptInvocation(fname); | 264 | List<object> olist = new List<object>(); |
195 | return fn(scriptid,parms); | 265 | olist.Add(hostid); |
266 | olist.Add(scriptid); | ||
267 | foreach (object o in parms) | ||
268 | olist.Add(o); | ||
269 | Delegate fn = LookupScriptInvocation(fname); | ||
270 | return fn.DynamicInvoke(olist.ToArray()); | ||
196 | } | 271 | } |
197 | #endregion | 272 | #endregion |
198 | 273 | ||