MMRScriptConsts.cs 11 KB

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