MMRScriptConsts.cs 11 KB

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