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