diff options
Diffstat (limited to 'OpenSim/ScriptEngine/Shared/ScriptStructure.cs')
-rw-r--r-- | OpenSim/ScriptEngine/Shared/ScriptStructure.cs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/OpenSim/ScriptEngine/Shared/ScriptStructure.cs b/OpenSim/ScriptEngine/Shared/ScriptStructure.cs new file mode 100644 index 0000000..cbf333b --- /dev/null +++ b/OpenSim/ScriptEngine/Shared/ScriptStructure.cs | |||
@@ -0,0 +1,109 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Reflection; | ||
4 | using System.Text; | ||
5 | using OpenMetaverse; | ||
6 | using OpenSim.Region.ScriptEngine.Interfaces; | ||
7 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
8 | using OpenSim.ScriptEngine.Shared; | ||
9 | |||
10 | namespace OpenSim.ScriptEngine.Shared | ||
11 | { | ||
12 | public struct ScriptStructure | ||
13 | { | ||
14 | public RegionInfoStructure RegionInfo; | ||
15 | public ScriptMetaData ScriptMetaData; | ||
16 | |||
17 | public ScriptAssemblies.IScript ScriptObject; | ||
18 | public string State; | ||
19 | public bool Running; | ||
20 | public bool Disabled; | ||
21 | public string Source; | ||
22 | public int StartParam; | ||
23 | public AppDomain AppDomain; | ||
24 | public Dictionary<string, IScriptApi> Apis; | ||
25 | public Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> LineMap; | ||
26 | public uint LocalID; | ||
27 | public UUID ItemID; | ||
28 | public string AssemblyFileName; | ||
29 | |||
30 | public string ScriptID { get { return LocalID.ToString() + "." + ItemID.ToString(); } } | ||
31 | public string Name { get { return "Script:" + ScriptID; } } | ||
32 | private bool Initialized; | ||
33 | private Dictionary<string, Delegate> InternalFunctions; | ||
34 | public string AssemblyName; | ||
35 | |||
36 | public void ExecuteEvent(EventParams p) | ||
37 | { | ||
38 | ExecuteMethod(p, true); | ||
39 | } | ||
40 | |||
41 | public void ExecuteMethod(EventParams p) | ||
42 | { | ||
43 | ExecuteMethod(p, false); | ||
44 | } | ||
45 | private void ExecuteMethod(EventParams p, bool isEvent) | ||
46 | { | ||
47 | // First time initialization? | ||
48 | if (!Initialized) | ||
49 | { | ||
50 | Initialized = true; | ||
51 | CacheInternalFunctions(); | ||
52 | } | ||
53 | |||
54 | lock (InternalFunctions) | ||
55 | { | ||
56 | // Make function name | ||
57 | string FunctionName; | ||
58 | if (isEvent) | ||
59 | FunctionName = State + "_event_" + p.EventName; | ||
60 | else | ||
61 | FunctionName = p.EventName; | ||
62 | |||
63 | // Check if this function exist | ||
64 | if (!InternalFunctions.ContainsKey(FunctionName)) | ||
65 | { | ||
66 | // TODO: Send message in-world | ||
67 | //RegionInfo.Scene. | ||
68 | RegionInfo.Logger.ErrorFormat("[{0}] Script function \"{1}\" was not found.", Name, FunctionName); | ||
69 | return; | ||
70 | } | ||
71 | |||
72 | // Execute script function | ||
73 | try | ||
74 | { | ||
75 | InternalFunctions[FunctionName].DynamicInvoke(p.Params); | ||
76 | } | ||
77 | catch (Exception e) | ||
78 | { | ||
79 | RegionInfo.Logger.ErrorFormat("[{0}] Execute \"{1}\" failed: {2}", Name, FunctionName, e.ToString()); | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | /// <summary> | ||
85 | /// Cache functions into a dictionary with delegates. Should be faster than reflection. | ||
86 | /// </summary> | ||
87 | private void CacheInternalFunctions() | ||
88 | { | ||
89 | Type scriptObjectType = ScriptObject.GetType(); | ||
90 | InternalFunctions = new Dictionary<string, Delegate>(); | ||
91 | |||
92 | MethodInfo[] methods = scriptObjectType.GetMethods(); | ||
93 | lock (InternalFunctions) | ||
94 | { | ||
95 | // Read all methods into a dictionary | ||
96 | foreach (MethodInfo mi in methods) | ||
97 | { | ||
98 | // TODO: We don't support overloading | ||
99 | if (!InternalFunctions.ContainsKey(mi.Name)) | ||
100 | InternalFunctions.Add(mi.Name, Delegate.CreateDelegate(scriptObjectType, ScriptObject, mi)); | ||
101 | else | ||
102 | RegionInfo.Logger.ErrorFormat("[{0}] Error: Script function \"{1}\" is already added. We do not support overloading.", | ||
103 | Name, mi.Name); | ||
104 | } | ||
105 | } | ||
106 | } | ||
107 | |||
108 | } | ||
109 | } \ No newline at end of file | ||