diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/YEngine/MMRScriptConsts.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/YEngine/MMRScriptConsts.cs | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptConsts.cs b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptConsts.cs new file mode 100644 index 0000000..b44c4e2 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptConsts.cs | |||
@@ -0,0 +1,275 @@ | |||
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 OpenSimulator 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.Collections.Generic; | ||
30 | using System.Reflection; | ||
31 | using System.Text; | ||
32 | |||
33 | using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat; | ||
34 | using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger; | ||
35 | using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString; | ||
36 | using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list; | ||
37 | using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion; | ||
38 | using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString; | ||
39 | using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3; | ||
40 | |||
41 | namespace OpenSim.Region.ScriptEngine.Yengine | ||
42 | { | ||
43 | |||
44 | public class ScriptConst | ||
45 | { | ||
46 | |||
47 | public static Dictionary<string, ScriptConst> scriptConstants = Init(); | ||
48 | |||
49 | /** | ||
50 | * @brief look up the value of a given built-in constant. | ||
51 | * @param name = name of constant | ||
52 | * @returns null: no constant by that name defined | ||
53 | * else: pointer to ScriptConst struct | ||
54 | */ | ||
55 | public static ScriptConst Lookup(string name) | ||
56 | { | ||
57 | ScriptConst sc; | ||
58 | if(!scriptConstants.TryGetValue(name, out sc)) | ||
59 | sc = null; | ||
60 | return sc; | ||
61 | } | ||
62 | |||
63 | private static Dictionary<string, ScriptConst> Init() | ||
64 | { | ||
65 | Dictionary<string, ScriptConst> sc = new Dictionary<string, ScriptConst>(); | ||
66 | |||
67 | // For every event code, define XMREVENTCODE_<eventname> and XMREVENTMASKn_<eventname> symbols. | ||
68 | for(int i = 0; i < 64; i++) | ||
69 | { | ||
70 | try | ||
71 | { | ||
72 | string s = ((ScriptEventCode)i).ToString(); | ||
73 | if((s.Length > 0) && (s[0] >= 'a') && (s[0] <= 'z')) | ||
74 | { | ||
75 | new ScriptConst(sc, | ||
76 | "XMREVENTCODE_" + s, | ||
77 | new CompValuInteger(new TokenTypeInt(null), i)); | ||
78 | int n = i / 32 + 1; | ||
79 | int m = 1 << (i % 32); | ||
80 | new ScriptConst(sc, | ||
81 | "XMREVENTMASK" + n + "_" + s, | ||
82 | new CompValuInteger(new TokenTypeInt(null), m)); | ||
83 | } | ||
84 | } | ||
85 | catch { } | ||
86 | } | ||
87 | |||
88 | // Also get all the constants from XMRInstAbstract and ScriptBaseClass etc as well. | ||
89 | for(Type t = typeof(XMRInstAbstract); t != typeof(object); t = t.BaseType) | ||
90 | { | ||
91 | AddInterfaceConstants(sc, t.GetFields()); | ||
92 | } | ||
93 | |||
94 | return sc; | ||
95 | } | ||
96 | |||
97 | /** | ||
98 | * @brief Add all constants defined by the given interface. | ||
99 | */ | ||
100 | // this one accepts only upper-case named fields | ||
101 | public static void AddInterfaceConstants(Dictionary<string, ScriptConst> sc, FieldInfo[] allFields) | ||
102 | { | ||
103 | List<FieldInfo> ucfs = new List<FieldInfo>(allFields.Length); | ||
104 | foreach(FieldInfo f in allFields) | ||
105 | { | ||
106 | string fieldName = f.Name; | ||
107 | int i; | ||
108 | for(i = fieldName.Length; --i >= 0;) | ||
109 | { | ||
110 | if("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_".IndexOf(fieldName[i]) < 0) | ||
111 | break; | ||
112 | } | ||
113 | if(i < 0) | ||
114 | ucfs.Add(f); | ||
115 | } | ||
116 | AddInterfaceConstants(sc, ucfs.GetEnumerator()); | ||
117 | } | ||
118 | |||
119 | // this one accepts all fields given to it | ||
120 | public static void AddInterfaceConstants(Dictionary<string, ScriptConst> sc, IEnumerator<FieldInfo> fields) | ||
121 | { | ||
122 | if(sc == null) | ||
123 | sc = scriptConstants; | ||
124 | |||
125 | for(fields.Reset(); fields.MoveNext();) | ||
126 | { | ||
127 | FieldInfo constField = fields.Current; | ||
128 | Type fieldType = constField.FieldType; | ||
129 | CompValu cv; | ||
130 | |||
131 | // The location of a simple number is the number itself. | ||
132 | // Access to the value gets compiled as an ldc instruction. | ||
133 | if(fieldType == typeof(double)) | ||
134 | { | ||
135 | cv = new CompValuFloat(new TokenTypeFloat(null), | ||
136 | (double)(double)constField.GetValue(null)); | ||
137 | } | ||
138 | else if(fieldType == typeof(int)) | ||
139 | { | ||
140 | cv = new CompValuInteger(new TokenTypeInt(null), | ||
141 | (int)constField.GetValue(null)); | ||
142 | } | ||
143 | else if(fieldType == typeof(LSL_Integer)) | ||
144 | { | ||
145 | cv = new CompValuInteger(new TokenTypeInt(null), | ||
146 | ((LSL_Integer)constField.GetValue(null)).value); | ||
147 | } | ||
148 | |||
149 | // The location of a string is the string itself. | ||
150 | // Access to the value gets compiled as an ldstr instruction. | ||
151 | else if(fieldType == typeof(string)) | ||
152 | { | ||
153 | cv = new CompValuString(new TokenTypeStr(null), | ||
154 | (string)constField.GetValue(null)); | ||
155 | } | ||
156 | else if(fieldType == typeof(LSL_String)) | ||
157 | { | ||
158 | cv = new CompValuString(new TokenTypeStr(null), | ||
159 | (string)(LSL_String)constField.GetValue(null)); | ||
160 | } | ||
161 | |||
162 | // The location of everything else (objects) is the static field in the interface definition. | ||
163 | // Access to the value gets compiled as an ldsfld instruction. | ||
164 | else | ||
165 | { | ||
166 | cv = new CompValuSField(TokenType.FromSysType(null, fieldType), constField); | ||
167 | } | ||
168 | |||
169 | // Add to dictionary. | ||
170 | new ScriptConst(sc, constField.Name, cv); | ||
171 | } | ||
172 | } | ||
173 | |||
174 | /** | ||
175 | * @brief Add arbitrary constant available to script compilation. | ||
176 | * CAUTION: These values get compiled-in to a script and must not | ||
177 | * change over time as previously compiled scripts will | ||
178 | * still have the old values. | ||
179 | */ | ||
180 | public static ScriptConst AddConstant(string name, object value) | ||
181 | { | ||
182 | CompValu cv = null; | ||
183 | |||
184 | if(value is char) | ||
185 | { | ||
186 | cv = new CompValuChar(new TokenTypeChar(null), (char)value); | ||
187 | } | ||
188 | if(value is double) | ||
189 | { | ||
190 | cv = new CompValuFloat(new TokenTypeFloat(null), (double)(double)value); | ||
191 | } | ||
192 | if(value is float) | ||
193 | { | ||
194 | cv = new CompValuFloat(new TokenTypeFloat(null), (double)(float)value); | ||
195 | } | ||
196 | if(value is int) | ||
197 | { | ||
198 | cv = new CompValuInteger(new TokenTypeInt(null), (int)value); | ||
199 | } | ||
200 | if(value is string) | ||
201 | { | ||
202 | cv = new CompValuString(new TokenTypeStr(null), (string)value); | ||
203 | } | ||
204 | |||
205 | if(value is LSL_Float) | ||
206 | { | ||
207 | cv = new CompValuFloat(new TokenTypeFloat(null), (double)((LSL_Float)value).value); | ||
208 | } | ||
209 | if(value is LSL_Integer) | ||
210 | { | ||
211 | cv = new CompValuInteger(new TokenTypeInt(null), ((LSL_Integer)value).value); | ||
212 | } | ||
213 | if(value is LSL_Rotation) | ||
214 | { | ||
215 | LSL_Rotation r = (LSL_Rotation)value; | ||
216 | CompValu x = new CompValuFloat(new TokenTypeFloat(null), r.x); | ||
217 | CompValu y = new CompValuFloat(new TokenTypeFloat(null), r.y); | ||
218 | CompValu z = new CompValuFloat(new TokenTypeFloat(null), r.z); | ||
219 | CompValu s = new CompValuFloat(new TokenTypeFloat(null), r.s); | ||
220 | cv = new CompValuRot(new TokenTypeRot(null), x, y, z, s); | ||
221 | } | ||
222 | if(value is LSL_String) | ||
223 | { | ||
224 | cv = new CompValuString(new TokenTypeStr(null), (string)(LSL_String)value); | ||
225 | } | ||
226 | if(value is LSL_Vector) | ||
227 | { | ||
228 | LSL_Vector v = (LSL_Vector)value; | ||
229 | CompValu x = new CompValuFloat(new TokenTypeFloat(null), v.x); | ||
230 | CompValu y = new CompValuFloat(new TokenTypeFloat(null), v.y); | ||
231 | CompValu z = new CompValuFloat(new TokenTypeFloat(null), v.z); | ||
232 | cv = new CompValuVec(new TokenTypeVec(null), x, y, z); | ||
233 | } | ||
234 | |||
235 | if(value is OpenMetaverse.Quaternion) | ||
236 | { | ||
237 | OpenMetaverse.Quaternion r = (OpenMetaverse.Quaternion)value; | ||
238 | CompValu x = new CompValuFloat(new TokenTypeFloat(null), r.X); | ||
239 | CompValu y = new CompValuFloat(new TokenTypeFloat(null), r.Y); | ||
240 | CompValu z = new CompValuFloat(new TokenTypeFloat(null), r.Z); | ||
241 | CompValu s = new CompValuFloat(new TokenTypeFloat(null), r.W); | ||
242 | cv = new CompValuRot(new TokenTypeRot(null), x, y, z, s); | ||
243 | } | ||
244 | if(value is OpenMetaverse.UUID) | ||
245 | { | ||
246 | cv = new CompValuString(new TokenTypeKey(null), value.ToString()); | ||
247 | } | ||
248 | if(value is OpenMetaverse.Vector3) | ||
249 | { | ||
250 | OpenMetaverse.Vector3 v = (OpenMetaverse.Vector3)value; | ||
251 | CompValu x = new CompValuFloat(new TokenTypeFloat(null), v.X); | ||
252 | CompValu y = new CompValuFloat(new TokenTypeFloat(null), v.Y); | ||
253 | CompValu z = new CompValuFloat(new TokenTypeFloat(null), v.Z); | ||
254 | cv = new CompValuVec(new TokenTypeVec(null), x, y, z); | ||
255 | } | ||
256 | |||
257 | if(cv == null) | ||
258 | throw new Exception("bad type " + value.GetType().Name); | ||
259 | return new ScriptConst(scriptConstants, name, cv); | ||
260 | } | ||
261 | |||
262 | /* | ||
263 | * Instance variables | ||
264 | */ | ||
265 | public string name; | ||
266 | public CompValu rVal; | ||
267 | |||
268 | private ScriptConst(Dictionary<string, ScriptConst> lc, string name, CompValu rVal) | ||
269 | { | ||
270 | lc.Add(name, this); | ||
271 | this.name = name; | ||
272 | this.rVal = rVal; | ||
273 | } | ||
274 | } | ||
275 | } | ||