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