diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptBase.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptBase.cs | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptBase.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptBase.cs new file mode 100644 index 0000000..aa2c9c2 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptBase.cs | |||
@@ -0,0 +1,124 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Runtime.Remoting.Lifetime; | ||
30 | using System.Threading; | ||
31 | using System.Reflection; | ||
32 | using System.Collections; | ||
33 | using System.Collections.Generic; | ||
34 | using OpenSim.Region.ScriptEngine.Interfaces; | ||
35 | using OpenSim.Region.ScriptEngine.Shared; | ||
36 | |||
37 | namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase | ||
38 | { | ||
39 | public partial class ScriptBaseClass : MarshalByRefObject, IScript | ||
40 | { | ||
41 | private Dictionary<string,MethodInfo> inits = new Dictionary<string,MethodInfo>(); | ||
42 | |||
43 | public ScriptBaseClass() | ||
44 | { | ||
45 | MethodInfo[] myArrayMethodInfo = GetType().GetMethods(BindingFlags.Public|BindingFlags.Instance); | ||
46 | |||
47 | foreach(MethodInfo mi in myArrayMethodInfo) | ||
48 | { | ||
49 | if(mi.Name.Length > 7 && mi.Name.Substring(0, 7) == "ApiType") | ||
50 | { | ||
51 | string type=mi.Name.Substring(7); | ||
52 | inits[type]=mi; | ||
53 | } | ||
54 | } | ||
55 | } | ||
56 | |||
57 | public string[] GetApis() | ||
58 | { | ||
59 | string[] apis = new string[inits.Count]; | ||
60 | inits.Keys.CopyTo(apis, 0); | ||
61 | return apis; | ||
62 | } | ||
63 | |||
64 | public void InitApi(string api, IScriptApi data) | ||
65 | { | ||
66 | if(!inits.ContainsKey(api)) | ||
67 | return; | ||
68 | |||
69 | MethodInfo mi = inits[api]; | ||
70 | |||
71 | Object[] args = new Object[1]; | ||
72 | args[0] = data; | ||
73 | |||
74 | mi.Invoke(this, args); | ||
75 | } | ||
76 | |||
77 | private Dictionary<string, object> m_InitialValues = | ||
78 | new Dictionary<string, object>(); | ||
79 | private Dictionary<string, FieldInfo> m_Fields = | ||
80 | new Dictionary<string, FieldInfo>(); | ||
81 | |||
82 | public Dictionary<string, object> GetVars() | ||
83 | { | ||
84 | Dictionary<string, object> vars = new Dictionary<string, object>(); | ||
85 | |||
86 | if (m_Fields == null) | ||
87 | return vars; | ||
88 | |||
89 | m_Fields.Clear(); | ||
90 | |||
91 | Type t = GetType(); | ||
92 | |||
93 | FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | | ||
94 | BindingFlags.Public | | ||
95 | BindingFlags.Instance | | ||
96 | BindingFlags.DeclaredOnly); | ||
97 | |||
98 | foreach (FieldInfo field in fields) | ||
99 | { | ||
100 | m_Fields[field.Name]=field; | ||
101 | |||
102 | vars[field.Name]=field.GetValue(this); | ||
103 | } | ||
104 | |||
105 | return vars; | ||
106 | } | ||
107 | |||
108 | public void SetVars(Dictionary<string, object> vars) | ||
109 | { | ||
110 | foreach (KeyValuePair<string, object> var in vars) | ||
111 | { | ||
112 | if (m_Fields.ContainsKey(var.Key)) | ||
113 | { | ||
114 | m_Fields[var.Key].SetValue(this, var.Value); | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | |||
119 | public void ResetVars() | ||
120 | { | ||
121 | SetVars(m_InitialValues); | ||
122 | } | ||
123 | } | ||
124 | } | ||