LSL_Types.cs 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  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;
  29. using System.Globalization;
  30. using System.Text.RegularExpressions;
  31. using OpenSim.Framework;
  32. namespace OpenSim.Region.ScriptEngine.Shared
  33. {
  34. [Serializable]
  35. public partial class LSL_Types
  36. {
  37. // Types are kept is separate .dll to avoid having to add whatever .dll it is in it to script AppDomain
  38. [Serializable]
  39. public struct Vector3
  40. {
  41. public double x;
  42. public double y;
  43. public double z;
  44. #region Constructors
  45. public Vector3(Vector3 vector)
  46. {
  47. x = (float)vector.x;
  48. y = (float)vector.y;
  49. z = (float)vector.z;
  50. }
  51. public Vector3(double X, double Y, double Z)
  52. {
  53. x = X;
  54. y = Y;
  55. z = Z;
  56. }
  57. public Vector3(string str)
  58. {
  59. str = str.Replace('<', ' ');
  60. str = str.Replace('>', ' ');
  61. string[] tmps = str.Split(new Char[] { ',', '<', '>' });
  62. if (tmps.Length < 3)
  63. {
  64. x=y=z=0;
  65. return;
  66. }
  67. bool res;
  68. res = Double.TryParse(tmps[0], NumberStyles.Float, Culture.NumberFormatInfo, out x);
  69. res = res & Double.TryParse(tmps[1], NumberStyles.Float, Culture.NumberFormatInfo, out y);
  70. res = res & Double.TryParse(tmps[2], NumberStyles.Float, Culture.NumberFormatInfo, out z);
  71. }
  72. #endregion
  73. #region Overriders
  74. public override string ToString()
  75. {
  76. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", x, y, z);
  77. return s;
  78. }
  79. public static explicit operator LSLString(Vector3 vec)
  80. {
  81. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", vec.x, vec.y, vec.z);
  82. return new LSLString(s);
  83. }
  84. public static explicit operator string(Vector3 vec)
  85. {
  86. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", vec.x, vec.y, vec.z);
  87. return s;
  88. }
  89. public static explicit operator Vector3(string s)
  90. {
  91. return new Vector3(s);
  92. }
  93. public static implicit operator list(Vector3 vec)
  94. {
  95. return new list(new object[] { vec });
  96. }
  97. public static bool operator ==(Vector3 lhs, Vector3 rhs)
  98. {
  99. return (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z);
  100. }
  101. public static bool operator !=(Vector3 lhs, Vector3 rhs)
  102. {
  103. return !(lhs == rhs);
  104. }
  105. public override int GetHashCode()
  106. {
  107. return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode());
  108. }
  109. public override bool Equals(object o)
  110. {
  111. if (!(o is Vector3)) return false;
  112. Vector3 vector = (Vector3)o;
  113. return (x == vector.x && y == vector.y && z == vector.z);
  114. }
  115. public static Vector3 operator -(Vector3 vector)
  116. {
  117. return new Vector3(-vector.x, -vector.y, -vector.z);
  118. }
  119. #endregion
  120. #region Vector & Vector Math
  121. // Vector-Vector Math
  122. public static Vector3 operator +(Vector3 lhs, Vector3 rhs)
  123. {
  124. return new Vector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
  125. }
  126. public static Vector3 operator -(Vector3 lhs, Vector3 rhs)
  127. {
  128. return new Vector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
  129. }
  130. public static LSLFloat operator *(Vector3 lhs, Vector3 rhs)
  131. {
  132. return Dot(lhs, rhs);
  133. }
  134. public static Vector3 operator %(Vector3 v1, Vector3 v2)
  135. {
  136. //Cross product
  137. Vector3 tv;
  138. tv.x = (v1.y * v2.z) - (v1.z * v2.y);
  139. tv.y = (v1.z * v2.x) - (v1.x * v2.z);
  140. tv.z = (v1.x * v2.y) - (v1.y * v2.x);
  141. return tv;
  142. }
  143. #endregion
  144. #region Vector & Float Math
  145. // Vector-Float and Float-Vector Math
  146. public static Vector3 operator *(Vector3 vec, float val)
  147. {
  148. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  149. }
  150. public static Vector3 operator *(float val, Vector3 vec)
  151. {
  152. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  153. }
  154. public static Vector3 operator /(Vector3 v, float f)
  155. {
  156. v.x = v.x / f;
  157. v.y = v.y / f;
  158. v.z = v.z / f;
  159. return v;
  160. }
  161. #endregion
  162. #region Vector & Double Math
  163. public static Vector3 operator *(Vector3 vec, double val)
  164. {
  165. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  166. }
  167. public static Vector3 operator *(double val, Vector3 vec)
  168. {
  169. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  170. }
  171. public static Vector3 operator /(Vector3 v, double f)
  172. {
  173. v.x = v.x / f;
  174. v.y = v.y / f;
  175. v.z = v.z / f;
  176. return v;
  177. }
  178. #endregion
  179. #region Vector & Rotation Math
  180. // Vector-Rotation Math
  181. public static Vector3 operator *(Vector3 v, Quaternion r)
  182. {
  183. Quaternion vq = new Quaternion(v.x, v.y, v.z, 0);
  184. Quaternion nq = new Quaternion(-r.x, -r.y, -r.z, r.s);
  185. // adapted for operator * computing "b * a"
  186. Quaternion result = nq * (vq * r);
  187. return new Vector3(result.x, result.y, result.z);
  188. }
  189. public static Vector3 operator /(Vector3 v, Quaternion r)
  190. {
  191. r.s = -r.s;
  192. return v * r;
  193. }
  194. #endregion
  195. #region Static Helper Functions
  196. public static double Dot(Vector3 v1, Vector3 v2)
  197. {
  198. return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
  199. }
  200. public static Vector3 Cross(Vector3 v1, Vector3 v2)
  201. {
  202. return new Vector3
  203. (
  204. v1.y * v2.z - v1.z * v2.y,
  205. v1.z * v2.x - v1.x * v2.z,
  206. v1.x * v2.y - v1.y * v2.x
  207. );
  208. }
  209. public static double Mag(Vector3 v)
  210. {
  211. return Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  212. }
  213. public static Vector3 Norm(Vector3 vector)
  214. {
  215. double mag = Mag(vector);
  216. if (mag > 0.0)
  217. {
  218. double invMag = 1.0 / mag;
  219. return vector * invMag;
  220. }
  221. return new Vector3(0, 0, 0);
  222. }
  223. #endregion
  224. }
  225. [Serializable]
  226. public struct Quaternion
  227. {
  228. public double x;
  229. public double y;
  230. public double z;
  231. public double s;
  232. #region Constructors
  233. public Quaternion(Quaternion Quat)
  234. {
  235. x = (float)Quat.x;
  236. y = (float)Quat.y;
  237. z = (float)Quat.z;
  238. s = (float)Quat.s;
  239. if (x == 0 && y == 0 && z == 0 && s == 0)
  240. s = 1;
  241. }
  242. public Quaternion(double X, double Y, double Z, double S)
  243. {
  244. x = X;
  245. y = Y;
  246. z = Z;
  247. s = S;
  248. if (x == 0 && y == 0 && z == 0 && s == 0)
  249. s = 1;
  250. }
  251. public Quaternion(string str)
  252. {
  253. str = str.Replace('<', ' ');
  254. str = str.Replace('>', ' ');
  255. string[] tmps = str.Split(new Char[] { ',', '<', '>' });
  256. if (tmps.Length < 4)
  257. {
  258. x=y=z=s=0;
  259. return;
  260. }
  261. bool res;
  262. res = Double.TryParse(tmps[0], NumberStyles.Float, Culture.NumberFormatInfo, out x);
  263. res = res & Double.TryParse(tmps[1], NumberStyles.Float, Culture.NumberFormatInfo, out y);
  264. res = res & Double.TryParse(tmps[2], NumberStyles.Float, Culture.NumberFormatInfo, out z);
  265. res = res & Double.TryParse(tmps[3], NumberStyles.Float, Culture.NumberFormatInfo, out s);
  266. if (x == 0 && y == 0 && z == 0 && s == 0)
  267. s = 1;
  268. }
  269. #endregion
  270. #region Overriders
  271. public override int GetHashCode()
  272. {
  273. return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode() ^ s.GetHashCode());
  274. }
  275. public override bool Equals(object o)
  276. {
  277. if (!(o is Quaternion)) return false;
  278. Quaternion quaternion = (Quaternion)o;
  279. return x == quaternion.x && y == quaternion.y && z == quaternion.z && s == quaternion.s;
  280. }
  281. public override string ToString()
  282. {
  283. string st=String.Format(Culture.FormatProvider, "<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", x, y, z, s);
  284. return st;
  285. }
  286. public static explicit operator string(Quaternion r)
  287. {
  288. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
  289. return s;
  290. }
  291. public static explicit operator LSLString(Quaternion r)
  292. {
  293. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
  294. return new LSLString(s);
  295. }
  296. public static explicit operator Quaternion(string s)
  297. {
  298. return new Quaternion(s);
  299. }
  300. public static implicit operator list(Quaternion r)
  301. {
  302. return new list(new object[] { r });
  303. }
  304. public static bool operator ==(Quaternion lhs, Quaternion rhs)
  305. {
  306. // Return true if the fields match:
  307. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.s == rhs.s;
  308. }
  309. public static bool operator !=(Quaternion lhs, Quaternion rhs)
  310. {
  311. return !(lhs == rhs);
  312. }
  313. public static double Mag(Quaternion q)
  314. {
  315. return Math.Sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.s * q.s);
  316. }
  317. #endregion
  318. public static Quaternion operator +(Quaternion a, Quaternion b)
  319. {
  320. return new Quaternion(a.x + b.x, a.y + b.y, a.z + b.z, a.s + b.s);
  321. }
  322. public static Quaternion operator /(Quaternion a, Quaternion b)
  323. {
  324. b.s = -b.s;
  325. return a * b;
  326. }
  327. public static Quaternion operator -(Quaternion a, Quaternion b)
  328. {
  329. return new Quaternion(a.x - b.x, a.y - b.y, a.z - b.z, a.s - b.s);
  330. }
  331. // using the equations below, we need to do "b * a" to be compatible with LSL
  332. public static Quaternion operator *(Quaternion b, Quaternion a)
  333. {
  334. Quaternion c;
  335. c.x = a.s * b.x + a.x * b.s + a.y * b.z - a.z * b.y;
  336. c.y = a.s * b.y + a.y * b.s + a.z * b.x - a.x * b.z;
  337. c.z = a.s * b.z + a.z * b.s + a.x * b.y - a.y * b.x;
  338. c.s = a.s * b.s - a.x * b.x - a.y * b.y - a.z * b.z;
  339. return c;
  340. }
  341. }
  342. [Serializable]
  343. public class list
  344. {
  345. private object[] m_data;
  346. public list(params object[] args)
  347. {
  348. m_data = args;
  349. }
  350. public int Length
  351. {
  352. get
  353. {
  354. if (m_data == null)
  355. m_data=new Object[0];
  356. return m_data.Length;
  357. }
  358. }
  359. public int Size
  360. {
  361. get
  362. {
  363. if (m_data == null)
  364. m_data=new Object[0];
  365. int size = 0;
  366. foreach (Object o in m_data)
  367. {
  368. if (o is LSL_Types.LSLInteger)
  369. size += 4;
  370. else if (o is LSL_Types.LSLFloat)
  371. size += 8;
  372. else if (o is LSL_Types.LSLString)
  373. size += ((LSL_Types.LSLString)o).m_string.Length;
  374. else if (o is LSL_Types.key)
  375. size += ((LSL_Types.key)o).value.Length;
  376. else if (o is LSL_Types.Vector3)
  377. size += 32;
  378. else if (o is LSL_Types.Quaternion)
  379. size += 64;
  380. else if (o is int)
  381. size += 4;
  382. else if (o is string)
  383. size += ((string)o).Length;
  384. else if (o is float)
  385. size += 8;
  386. else if (o is double)
  387. size += 16;
  388. else
  389. throw new Exception("Unknown type in List.Size: " + o.GetType().ToString());
  390. }
  391. return size;
  392. }
  393. }
  394. public object[] Data
  395. {
  396. get {
  397. if (m_data == null)
  398. m_data=new Object[0];
  399. return m_data;
  400. }
  401. set {m_data = value; }
  402. }
  403. // Function to obtain LSL type from an index. This is needed
  404. // because LSL lists allow for multiple types, and safely
  405. // iterating in them requires a type check.
  406. public Type GetLSLListItemType(int itemIndex)
  407. {
  408. return m_data[itemIndex].GetType();
  409. }
  410. // Member functions to obtain item as specific types.
  411. // For cases where implicit conversions would apply if items
  412. // were not in a list (e.g. integer to float, but not float
  413. // to integer) functions check for alternate types so as to
  414. // down-cast from Object to the correct type.
  415. // Note: no checks for item index being valid are performed
  416. public LSL_Types.LSLFloat GetLSLFloatItem(int itemIndex)
  417. {
  418. if (m_data[itemIndex] is LSL_Types.LSLInteger)
  419. {
  420. return (LSL_Types.LSLInteger)m_data[itemIndex];
  421. }
  422. else if (m_data[itemIndex] is Int32)
  423. {
  424. return new LSL_Types.LSLFloat((int)m_data[itemIndex]);
  425. }
  426. else if (m_data[itemIndex] is float)
  427. {
  428. return new LSL_Types.LSLFloat((float)m_data[itemIndex]);
  429. }
  430. else if (m_data[itemIndex] is Double)
  431. {
  432. return new LSL_Types.LSLFloat((Double)m_data[itemIndex]);
  433. }
  434. else if (m_data[itemIndex] is LSL_Types.LSLString)
  435. {
  436. return new LSL_Types.LSLFloat(m_data[itemIndex].ToString());
  437. }
  438. else
  439. {
  440. return (LSL_Types.LSLFloat)m_data[itemIndex];
  441. }
  442. }
  443. public LSL_Types.LSLString GetLSLStringItem(int itemIndex)
  444. {
  445. if (m_data[itemIndex] is LSL_Types.key)
  446. {
  447. return (LSL_Types.key)m_data[itemIndex];
  448. }
  449. else if (m_data[itemIndex] is String)
  450. {
  451. return new LSL_Types.LSLString((string)m_data[itemIndex]);
  452. }
  453. else if (m_data[itemIndex] is LSL_Types.LSLFloat)
  454. {
  455. return new LSL_Types.LSLString((LSLFloat)m_data[itemIndex]);
  456. }
  457. else if (m_data[itemIndex] is LSL_Types.LSLInteger)
  458. {
  459. return new LSL_Types.LSLString((LSLInteger)m_data[itemIndex]);
  460. }
  461. else
  462. {
  463. return (LSL_Types.LSLString)m_data[itemIndex];
  464. }
  465. }
  466. public LSL_Types.LSLInteger GetLSLIntegerItem(int itemIndex)
  467. {
  468. if (m_data[itemIndex] is LSL_Types.LSLInteger)
  469. return (LSL_Types.LSLInteger)m_data[itemIndex];
  470. if (m_data[itemIndex] is LSL_Types.LSLFloat)
  471. return new LSLInteger((int)m_data[itemIndex]);
  472. else if (m_data[itemIndex] is Int32)
  473. return new LSLInteger((int)m_data[itemIndex]);
  474. else if (m_data[itemIndex] is LSL_Types.LSLString)
  475. return new LSLInteger((string)m_data[itemIndex]);
  476. else
  477. throw new InvalidCastException();
  478. }
  479. public LSL_Types.Vector3 GetVector3Item(int itemIndex)
  480. {
  481. return (LSL_Types.Vector3)m_data[itemIndex];
  482. }
  483. public LSL_Types.Quaternion GetQuaternionItem(int itemIndex)
  484. {
  485. return (LSL_Types.Quaternion)m_data[itemIndex];
  486. }
  487. public LSL_Types.key GetKeyItem(int itemIndex)
  488. {
  489. return (LSL_Types.key)m_data[itemIndex];
  490. }
  491. public static list operator +(list a, list b)
  492. {
  493. object[] tmp;
  494. tmp = new object[a.Length + b.Length];
  495. a.Data.CopyTo(tmp, 0);
  496. b.Data.CopyTo(tmp, a.Length);
  497. return new list(tmp);
  498. }
  499. private void ExtendAndAdd(object o)
  500. {
  501. Array.Resize(ref m_data, Length + 1);
  502. m_data.SetValue(o, Length - 1);
  503. }
  504. public static list operator +(list a, LSLString s)
  505. {
  506. a.ExtendAndAdd(s);
  507. return a;
  508. }
  509. public static list operator +(list a, LSLInteger i)
  510. {
  511. a.ExtendAndAdd(i);
  512. return a;
  513. }
  514. public static list operator +(list a, LSLFloat d)
  515. {
  516. a.ExtendAndAdd(d);
  517. return a;
  518. }
  519. public static bool operator ==(list a, list b)
  520. {
  521. int la = -1;
  522. int lb = -1;
  523. try { la = a.Length; }
  524. catch (NullReferenceException) { }
  525. try { lb = b.Length; }
  526. catch (NullReferenceException) { }
  527. return la == lb;
  528. }
  529. public static bool operator !=(list a, list b)
  530. {
  531. int la = -1;
  532. int lb = -1;
  533. try { la = a.Length; }
  534. catch (NullReferenceException) { }
  535. try {lb = b.Length;}
  536. catch (NullReferenceException) { }
  537. return la != lb;
  538. }
  539. public void Add(object o)
  540. {
  541. object[] tmp;
  542. tmp = new object[m_data.Length + 1];
  543. m_data.CopyTo(tmp, 0);
  544. tmp[m_data.Length] = o;
  545. m_data = tmp;
  546. }
  547. public bool Contains(object o)
  548. {
  549. bool ret = false;
  550. foreach (object i in Data)
  551. {
  552. if (i == o)
  553. {
  554. ret = true;
  555. break;
  556. }
  557. }
  558. return ret;
  559. }
  560. public list DeleteSublist(int start, int end)
  561. {
  562. // Not an easy one
  563. // If start <= end, remove that part
  564. // if either is negative, count from the end of the array
  565. // if the resulting start > end, remove all BUT that part
  566. Object[] ret;
  567. if (start < 0)
  568. start=m_data.Length+start;
  569. if (start < 0)
  570. start=0;
  571. if (end < 0)
  572. end=m_data.Length+end;
  573. if (end < 0)
  574. end=0;
  575. if (start > end)
  576. {
  577. if (end >= m_data.Length)
  578. return new list(new Object[0]);
  579. if (start >= m_data.Length)
  580. start=m_data.Length-1;
  581. return GetSublist(end, start);
  582. }
  583. // start >= 0 && end >= 0 here
  584. if (start >= m_data.Length)
  585. {
  586. ret=new Object[m_data.Length];
  587. Array.Copy(m_data, 0, ret, 0, m_data.Length);
  588. return new list(ret);
  589. }
  590. if (end >= m_data.Length)
  591. end=m_data.Length-1;
  592. // now, this makes the math easier
  593. int remove=end+1-start;
  594. ret=new Object[m_data.Length-remove];
  595. if (ret.Length == 0)
  596. return new list(ret);
  597. int src;
  598. int dest=0;
  599. for (src = 0; src < m_data.Length; src++)
  600. {
  601. if (src < start || src > end)
  602. ret[dest++]=m_data[src];
  603. }
  604. return new list(ret);
  605. }
  606. public list GetSublist(int start, int end)
  607. {
  608. object[] ret;
  609. // Take care of neg start or end's
  610. // NOTE that either index may still be negative after
  611. // adding the length, so we must take additional
  612. // measures to protect against this. Note also that
  613. // after normalisation the negative indices are no
  614. // longer relative to the end of the list.
  615. if (start < 0)
  616. {
  617. start = m_data.Length + start;
  618. }
  619. if (end < 0)
  620. {
  621. end = m_data.Length + end;
  622. }
  623. // The conventional case is start <= end
  624. // NOTE that the case of an empty list is
  625. // dealt with by the initial test. Start
  626. // less than end is taken to be the most
  627. // common case.
  628. if (start <= end)
  629. {
  630. // Start sublist beyond length
  631. // Also deals with start AND end still negative
  632. if (start >= m_data.Length || end < 0)
  633. {
  634. return new list();
  635. }
  636. // Sublist extends beyond the end of the supplied list
  637. if (end >= m_data.Length)
  638. {
  639. end = m_data.Length - 1;
  640. }
  641. // Sublist still starts before the beginning of the list
  642. if (start < 0)
  643. {
  644. start = 0;
  645. }
  646. ret = new object[end - start + 1];
  647. Array.Copy(m_data, start, ret, 0, end - start + 1);
  648. return new list(ret);
  649. }
  650. // Deal with the segmented case: 0->end + start->EOL
  651. else
  652. {
  653. list result = null;
  654. // If end is negative, then prefix list is empty
  655. if (end < 0)
  656. {
  657. result = new list();
  658. // If start is still negative, then the whole of
  659. // the existing list is returned. This case is
  660. // only admitted if end is also still negative.
  661. if (start < 0)
  662. {
  663. return this;
  664. }
  665. }
  666. else
  667. {
  668. result = GetSublist(0,end);
  669. }
  670. // If start is outside of list, then just return
  671. // the prefix, whatever it is.
  672. if (start >= m_data.Length)
  673. {
  674. return result;
  675. }
  676. return result + GetSublist(start, Data.Length);
  677. }
  678. }
  679. private static int compare(object left, object right, int ascending)
  680. {
  681. if (!left.GetType().Equals(right.GetType()))
  682. {
  683. // unequal types are always "equal" for comparison purposes.
  684. // this way, the bubble sort will never swap them, and we'll
  685. // get that feathered effect we're looking for
  686. return 0;
  687. }
  688. int ret = 0;
  689. if (left is key)
  690. {
  691. key l = (key)left;
  692. key r = (key)right;
  693. ret = String.CompareOrdinal(l.value, r.value);
  694. }
  695. else if (left is LSLString)
  696. {
  697. LSLString l = (LSLString)left;
  698. LSLString r = (LSLString)right;
  699. ret = String.CompareOrdinal(l.m_string, r.m_string);
  700. }
  701. else if (left is LSLInteger)
  702. {
  703. LSLInteger l = (LSLInteger)left;
  704. LSLInteger r = (LSLInteger)right;
  705. ret = Math.Sign(l.value - r.value);
  706. }
  707. else if (left is LSLFloat)
  708. {
  709. LSLFloat l = (LSLFloat)left;
  710. LSLFloat r = (LSLFloat)right;
  711. ret = Math.Sign(l.value - r.value);
  712. }
  713. else if (left is Vector3)
  714. {
  715. Vector3 l = (Vector3)left;
  716. Vector3 r = (Vector3)right;
  717. ret = Math.Sign(Vector3.Mag(l) - Vector3.Mag(r));
  718. }
  719. else if (left is Quaternion)
  720. {
  721. Quaternion l = (Quaternion)left;
  722. Quaternion r = (Quaternion)right;
  723. ret = Math.Sign(Quaternion.Mag(l) - Quaternion.Mag(r));
  724. }
  725. if (ascending == 0)
  726. {
  727. ret = 0 - ret;
  728. }
  729. return ret;
  730. }
  731. class HomogeneousComparer : IComparer
  732. {
  733. public HomogeneousComparer()
  734. {
  735. }
  736. public int Compare(object lhs, object rhs)
  737. {
  738. return compare(lhs, rhs, 1);
  739. }
  740. }
  741. public list Sort(int stride, int ascending)
  742. {
  743. if (Data.Length == 0)
  744. return new list(); // Don't even bother
  745. object[] ret = new object[Data.Length];
  746. Array.Copy(Data, 0, ret, 0, Data.Length);
  747. if (stride <= 0)
  748. {
  749. stride = 1;
  750. }
  751. // we can optimize here in the case where stride == 1 and the list
  752. // consists of homogeneous types
  753. if (stride == 1)
  754. {
  755. bool homogeneous = true;
  756. int index;
  757. for (index = 1; index < Data.Length; index++)
  758. {
  759. if (!Data[0].GetType().Equals(Data[index].GetType()))
  760. {
  761. homogeneous = false;
  762. break;
  763. }
  764. }
  765. if (homogeneous)
  766. {
  767. Array.Sort(ret, new HomogeneousComparer());
  768. if (ascending == 0)
  769. {
  770. Array.Reverse(ret);
  771. }
  772. return new list(ret);
  773. }
  774. }
  775. // Because of the desired type specific feathered sorting behavior
  776. // requried by the spec, we MUST use a non-optimized bubble sort here.
  777. // Anything else will give you the incorrect behavior.
  778. // begin bubble sort...
  779. int i;
  780. int j;
  781. int k;
  782. int n = Data.Length;
  783. for (i = 0; i < (n-stride); i += stride)
  784. {
  785. for (j = i + stride; j < n; j += stride)
  786. {
  787. if (compare(ret[i], ret[j], ascending) > 0)
  788. {
  789. for (k = 0; k < stride; k++)
  790. {
  791. object tmp = ret[i + k];
  792. ret[i + k] = ret[j + k];
  793. ret[j + k] = tmp;
  794. }
  795. }
  796. }
  797. }
  798. // end bubble sort
  799. return new list(ret);
  800. }
  801. #region CSV Methods
  802. public static list FromCSV(string csv)
  803. {
  804. return new list(csv.Split(','));
  805. }
  806. public string ToCSV()
  807. {
  808. string ret = "";
  809. foreach (object o in this.Data)
  810. {
  811. if (ret == "")
  812. {
  813. ret = o.ToString();
  814. }
  815. else
  816. {
  817. ret = ret + ", " + o.ToString();
  818. }
  819. }
  820. return ret;
  821. }
  822. private string ToSoup()
  823. {
  824. string output;
  825. output = String.Empty;
  826. if (m_data.Length == 0)
  827. {
  828. return String.Empty;
  829. }
  830. foreach (object o in m_data)
  831. {
  832. output = output + o.ToString();
  833. }
  834. return output;
  835. }
  836. public static explicit operator String(list l)
  837. {
  838. return l.ToSoup();
  839. }
  840. public static explicit operator LSLString(list l)
  841. {
  842. return new LSLString(l.ToSoup());
  843. }
  844. public override string ToString()
  845. {
  846. return ToSoup();
  847. }
  848. #endregion
  849. #region Statistic Methods
  850. public double Min()
  851. {
  852. double minimum = double.PositiveInfinity;
  853. double entry;
  854. for (int i = 0; i < Data.Length; i++)
  855. {
  856. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  857. {
  858. if (entry < minimum) minimum = entry;
  859. }
  860. }
  861. return minimum;
  862. }
  863. public double Max()
  864. {
  865. double maximum = double.NegativeInfinity;
  866. double entry;
  867. for (int i = 0; i < Data.Length; i++)
  868. {
  869. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  870. {
  871. if (entry > maximum) maximum = entry;
  872. }
  873. }
  874. return maximum;
  875. }
  876. public double Range()
  877. {
  878. return (this.Max() / this.Min());
  879. }
  880. public int NumericLength()
  881. {
  882. int count = 0;
  883. double entry;
  884. for (int i = 0; i < Data.Length; i++)
  885. {
  886. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  887. {
  888. count++;
  889. }
  890. }
  891. return count;
  892. }
  893. public static list ToDoubleList(list src)
  894. {
  895. list ret = new list();
  896. double entry;
  897. for (int i = 0; i < src.Data.Length - 1; i++)
  898. {
  899. if (double.TryParse(src.Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  900. {
  901. ret.Add(entry);
  902. }
  903. }
  904. return ret;
  905. }
  906. public double Sum()
  907. {
  908. double sum = 0;
  909. double entry;
  910. for (int i = 0; i < Data.Length; i++)
  911. {
  912. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  913. {
  914. sum = sum + entry;
  915. }
  916. }
  917. return sum;
  918. }
  919. public double SumSqrs()
  920. {
  921. double sum = 0;
  922. double entry;
  923. for (int i = 0; i < Data.Length; i++)
  924. {
  925. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  926. {
  927. sum = sum + Math.Pow(entry, 2);
  928. }
  929. }
  930. return sum;
  931. }
  932. public double Mean()
  933. {
  934. return (this.Sum() / this.NumericLength());
  935. }
  936. public void NumericSort()
  937. {
  938. IComparer Numeric = new NumericComparer();
  939. Array.Sort(Data, Numeric);
  940. }
  941. public void AlphaSort()
  942. {
  943. IComparer Alpha = new AlphaCompare();
  944. Array.Sort(Data, Alpha);
  945. }
  946. public double Median()
  947. {
  948. return Qi(0.5);
  949. }
  950. public double GeometricMean()
  951. {
  952. double ret = 1.0;
  953. list nums = ToDoubleList(this);
  954. for (int i = 0; i < nums.Data.Length; i++)
  955. {
  956. ret *= (double)nums.Data[i];
  957. }
  958. return Math.Exp(Math.Log(ret) / (double)nums.Data.Length);
  959. }
  960. public double HarmonicMean()
  961. {
  962. double ret = 0.0;
  963. list nums = ToDoubleList(this);
  964. for (int i = 0; i < nums.Data.Length; i++)
  965. {
  966. ret += 1.0 / (double)nums.Data[i];
  967. }
  968. return ((double)nums.Data.Length / ret);
  969. }
  970. public double Variance()
  971. {
  972. double s = 0;
  973. list num = ToDoubleList(this);
  974. for (int i = 0; i < num.Data.Length; i++)
  975. {
  976. s += Math.Pow((double)num.Data[i], 2);
  977. }
  978. return (s - num.Data.Length * Math.Pow(num.Mean(), 2)) / (num.Data.Length - 1);
  979. }
  980. public double StdDev()
  981. {
  982. return Math.Sqrt(this.Variance());
  983. }
  984. public double Qi(double i)
  985. {
  986. list j = this;
  987. j.NumericSort();
  988. if (Math.Ceiling(this.Length * i) == this.Length * i)
  989. {
  990. return (double)((double)j.Data[(int)(this.Length * i - 1)] + (double)j.Data[(int)(this.Length * i)]) / 2;
  991. }
  992. else
  993. {
  994. return (double)j.Data[((int)(Math.Ceiling(this.Length * i))) - 1];
  995. }
  996. }
  997. #endregion
  998. public string ToPrettyString()
  999. {
  1000. string output;
  1001. if (m_data.Length == 0)
  1002. {
  1003. return "[]";
  1004. }
  1005. output = "[";
  1006. foreach (object o in m_data)
  1007. {
  1008. if (o is String)
  1009. {
  1010. output = output + "\"" + o + "\", ";
  1011. }
  1012. else
  1013. {
  1014. output = output + o.ToString() + ", ";
  1015. }
  1016. }
  1017. output = output.Substring(0, output.Length - 2);
  1018. output = output + "]";
  1019. return output;
  1020. }
  1021. public class AlphaCompare : IComparer
  1022. {
  1023. int IComparer.Compare(object x, object y)
  1024. {
  1025. return string.Compare(x.ToString(), y.ToString());
  1026. }
  1027. }
  1028. public class NumericComparer : IComparer
  1029. {
  1030. int IComparer.Compare(object x, object y)
  1031. {
  1032. double a;
  1033. double b;
  1034. if (!double.TryParse(x.ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out a))
  1035. {
  1036. a = 0.0;
  1037. }
  1038. if (!double.TryParse(y.ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out b))
  1039. {
  1040. b = 0.0;
  1041. }
  1042. if (a < b)
  1043. {
  1044. return -1;
  1045. }
  1046. else if (a == b)
  1047. {
  1048. return 0;
  1049. }
  1050. else
  1051. {
  1052. return 1;
  1053. }
  1054. }
  1055. }
  1056. public override bool Equals(object o)
  1057. {
  1058. if (!(o is list))
  1059. return false;
  1060. return Data.Length == ((list)o).Data.Length;
  1061. }
  1062. public override int GetHashCode()
  1063. {
  1064. return Data.GetHashCode();
  1065. }
  1066. }
  1067. //
  1068. // BELOW IS WORK IN PROGRESS... IT WILL CHANGE, SO DON'T USE YET! :)
  1069. //
  1070. public struct StringTest
  1071. {
  1072. // Our own little string
  1073. internal string actualString;
  1074. public static implicit operator bool(StringTest mString)
  1075. {
  1076. if (mString.actualString.Length == 0)
  1077. return true;
  1078. return false;
  1079. }
  1080. public override string ToString()
  1081. {
  1082. return actualString;
  1083. }
  1084. }
  1085. [Serializable]
  1086. public struct key
  1087. {
  1088. public string value;
  1089. #region Constructors
  1090. public key(string s)
  1091. {
  1092. value = s;
  1093. }
  1094. #endregion
  1095. #region Methods
  1096. static public bool Parse2Key(string s)
  1097. {
  1098. Regex isuuid = new Regex(@"^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$", RegexOptions.Compiled);
  1099. if (isuuid.IsMatch(s))
  1100. {
  1101. return true;
  1102. }
  1103. else
  1104. {
  1105. return false;
  1106. }
  1107. }
  1108. #endregion
  1109. #region Operators
  1110. static public implicit operator Boolean(key k)
  1111. {
  1112. if (k.value.Length == 0)
  1113. {
  1114. return false;
  1115. }
  1116. if (k.value == "00000000-0000-0000-0000-000000000000")
  1117. {
  1118. return false;
  1119. }
  1120. Regex isuuid = new Regex(@"^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$", RegexOptions.Compiled);
  1121. if (isuuid.IsMatch(k.value))
  1122. {
  1123. return true;
  1124. }
  1125. else
  1126. {
  1127. return false;
  1128. }
  1129. }
  1130. static public implicit operator key(string s)
  1131. {
  1132. return new key(s);
  1133. }
  1134. static public implicit operator String(key k)
  1135. {
  1136. return k.value;
  1137. }
  1138. static public implicit operator LSLString(key k)
  1139. {
  1140. return k.value;
  1141. }
  1142. public static bool operator ==(key k1, key k2)
  1143. {
  1144. return k1.value == k2.value;
  1145. }
  1146. public static bool operator !=(key k1, key k2)
  1147. {
  1148. return k1.value != k2.value;
  1149. }
  1150. #endregion
  1151. #region Overriders
  1152. public override bool Equals(object o)
  1153. {
  1154. return o.ToString() == value;
  1155. }
  1156. public override int GetHashCode()
  1157. {
  1158. return value.GetHashCode();
  1159. }
  1160. public override string ToString()
  1161. {
  1162. return value;
  1163. }
  1164. #endregion
  1165. }
  1166. [Serializable]
  1167. public struct LSLString
  1168. {
  1169. public string m_string;
  1170. #region Constructors
  1171. public LSLString(string s)
  1172. {
  1173. m_string = s;
  1174. }
  1175. public LSLString(double d)
  1176. {
  1177. string s=String.Format(Culture.FormatProvider, "{0:0.000000}", d);
  1178. m_string=s;
  1179. }
  1180. public LSLString(LSLFloat f)
  1181. {
  1182. string s = String.Format(Culture.FormatProvider, "{0:0.000000}", f.value);
  1183. m_string=s;
  1184. }
  1185. public LSLString(LSLInteger i)
  1186. {
  1187. string s = String.Format("{0}", i);
  1188. m_string = s;
  1189. }
  1190. #endregion
  1191. #region Operators
  1192. static public implicit operator Boolean(LSLString s)
  1193. {
  1194. if (s.m_string.Length == 0)
  1195. {
  1196. return false;
  1197. }
  1198. else
  1199. {
  1200. return true;
  1201. }
  1202. }
  1203. static public implicit operator String(LSLString s)
  1204. {
  1205. return s.m_string;
  1206. }
  1207. static public implicit operator LSLString(string s)
  1208. {
  1209. return new LSLString(s);
  1210. }
  1211. public static string ToString(LSLString s)
  1212. {
  1213. return s.m_string;
  1214. }
  1215. public override string ToString()
  1216. {
  1217. return m_string;
  1218. }
  1219. public static bool operator ==(LSLString s1, string s2)
  1220. {
  1221. return s1.m_string == s2;
  1222. }
  1223. public static bool operator !=(LSLString s1, string s2)
  1224. {
  1225. return s1.m_string != s2;
  1226. }
  1227. public static LSLString operator +(LSLString s1, LSLString s2)
  1228. {
  1229. return new LSLString(s1.m_string + s2.m_string);
  1230. }
  1231. public static explicit operator double(LSLString s)
  1232. {
  1233. return new LSLFloat(s).value;
  1234. }
  1235. public static explicit operator LSLInteger(LSLString s)
  1236. {
  1237. return new LSLInteger(s.m_string);
  1238. }
  1239. public static explicit operator LSLString(double d)
  1240. {
  1241. return new LSLString(d);
  1242. }
  1243. public static explicit operator LSLString(LSLFloat f)
  1244. {
  1245. return new LSLString(f);
  1246. }
  1247. static public explicit operator LSLString(bool b)
  1248. {
  1249. if (b)
  1250. return new LSLString("1");
  1251. else
  1252. return new LSLString("0");
  1253. }
  1254. public static implicit operator Vector3(LSLString s)
  1255. {
  1256. return new Vector3(s.m_string);
  1257. }
  1258. public static implicit operator Quaternion(LSLString s)
  1259. {
  1260. return new Quaternion(s.m_string);
  1261. }
  1262. public static implicit operator LSLFloat(LSLString s)
  1263. {
  1264. return new LSLFloat(s);
  1265. }
  1266. public static implicit operator list(LSLString s)
  1267. {
  1268. return new list(new object[]{s});
  1269. }
  1270. #endregion
  1271. #region Overriders
  1272. public override bool Equals(object o)
  1273. {
  1274. return m_string == o.ToString();
  1275. }
  1276. public override int GetHashCode()
  1277. {
  1278. return m_string.GetHashCode();
  1279. }
  1280. #endregion
  1281. #region " Standard string functions "
  1282. //Clone,CompareTo,Contains
  1283. //CopyTo,EndsWith,Equals,GetEnumerator,GetHashCode,GetType,GetTypeCode
  1284. //IndexOf,IndexOfAny,Insert,IsNormalized,LastIndexOf,LastIndexOfAny
  1285. //Length,Normalize,PadLeft,PadRight,Remove,Replace,Split,StartsWith,Substring,ToCharArray,ToLowerInvariant
  1286. //ToString,ToUpper,ToUpperInvariant,Trim,TrimEnd,TrimStart
  1287. public bool Contains(string value) { return m_string.Contains(value); }
  1288. public int IndexOf(string value) { return m_string.IndexOf(value); }
  1289. public int Length { get { return m_string.Length; } }
  1290. #endregion
  1291. }
  1292. [Serializable]
  1293. public struct LSLInteger
  1294. {
  1295. public int value;
  1296. #region Constructors
  1297. public LSLInteger(int i)
  1298. {
  1299. value = i;
  1300. }
  1301. public LSLInteger(uint i)
  1302. {
  1303. value = (int)i;
  1304. }
  1305. public LSLInteger(double d)
  1306. {
  1307. value = (int)d;
  1308. }
  1309. public LSLInteger(string s)
  1310. {
  1311. Regex r = new Regex("(^[ ]*0[xX][0-9A-Fa-f][0-9A-Fa-f]*)|(^[ ]*-?[0-9][0-9]*)");
  1312. Match m = r.Match(s);
  1313. string v = m.Groups[0].Value;
  1314. if (v == String.Empty)
  1315. {
  1316. value = 0;
  1317. }
  1318. else
  1319. {
  1320. try
  1321. {
  1322. if (v.Contains("x") || v.Contains("X"))
  1323. {
  1324. value = int.Parse(v.Substring(2), System.Globalization.NumberStyles.HexNumber);
  1325. }
  1326. else
  1327. {
  1328. value = int.Parse(v, System.Globalization.NumberStyles.Integer);
  1329. }
  1330. }
  1331. catch (OverflowException)
  1332. {
  1333. value = -1;
  1334. }
  1335. }
  1336. }
  1337. #endregion
  1338. #region Operators
  1339. static public implicit operator int(LSLInteger i)
  1340. {
  1341. return i.value;
  1342. }
  1343. static public explicit operator uint(LSLInteger i)
  1344. {
  1345. return (uint)i.value;
  1346. }
  1347. static public explicit operator LSLString(LSLInteger i)
  1348. {
  1349. return new LSLString(i.ToString());
  1350. }
  1351. public static implicit operator list(LSLInteger i)
  1352. {
  1353. return new list(new object[] { i });
  1354. }
  1355. static public implicit operator Boolean(LSLInteger i)
  1356. {
  1357. if (i.value == 0)
  1358. {
  1359. return false;
  1360. }
  1361. else
  1362. {
  1363. return true;
  1364. }
  1365. }
  1366. static public implicit operator LSLInteger(int i)
  1367. {
  1368. return new LSLInteger(i);
  1369. }
  1370. static public explicit operator LSLInteger(string s)
  1371. {
  1372. return new LSLInteger(s);
  1373. }
  1374. static public implicit operator LSLInteger(uint u)
  1375. {
  1376. return new LSLInteger(u);
  1377. }
  1378. static public explicit operator LSLInteger(double d)
  1379. {
  1380. return new LSLInteger(d);
  1381. }
  1382. static public explicit operator LSLInteger(LSLFloat f)
  1383. {
  1384. return new LSLInteger(f.value);
  1385. }
  1386. static public implicit operator LSLInteger(bool b)
  1387. {
  1388. if (b)
  1389. return new LSLInteger(1);
  1390. else
  1391. return new LSLInteger(0);
  1392. }
  1393. static public LSLInteger operator ==(LSLInteger i1, LSLInteger i2)
  1394. {
  1395. bool ret = i1.value == i2.value;
  1396. return new LSLInteger((ret ? 1 : 0));
  1397. }
  1398. static public LSLInteger operator !=(LSLInteger i1, LSLInteger i2)
  1399. {
  1400. bool ret = i1.value != i2.value;
  1401. return new LSLInteger((ret ? 1 : 0));
  1402. }
  1403. static public LSLInteger operator <(LSLInteger i1, LSLInteger i2)
  1404. {
  1405. bool ret = i1.value < i2.value;
  1406. return new LSLInteger((ret ? 1 : 0));
  1407. }
  1408. static public LSLInteger operator <=(LSLInteger i1, LSLInteger i2)
  1409. {
  1410. bool ret = i1.value <= i2.value;
  1411. return new LSLInteger((ret ? 1 : 0));
  1412. }
  1413. static public LSLInteger operator >(LSLInteger i1, LSLInteger i2)
  1414. {
  1415. bool ret = i1.value > i2.value;
  1416. return new LSLInteger((ret ? 1 : 0));
  1417. }
  1418. static public LSLInteger operator >=(LSLInteger i1, LSLInteger i2)
  1419. {
  1420. bool ret = i1.value >= i2.value;
  1421. return new LSLInteger((ret ? 1 : 0));
  1422. }
  1423. static public LSLInteger operator +(LSLInteger i1, int i2)
  1424. {
  1425. return new LSLInteger(i1.value + i2);
  1426. }
  1427. static public LSLInteger operator -(LSLInteger i1, int i2)
  1428. {
  1429. return new LSLInteger(i1.value - i2);
  1430. }
  1431. static public LSLInteger operator *(LSLInteger i1, int i2)
  1432. {
  1433. return new LSLInteger(i1.value * i2);
  1434. }
  1435. static public LSLInteger operator /(LSLInteger i1, int i2)
  1436. {
  1437. return new LSLInteger(i1.value / i2);
  1438. }
  1439. // static public LSLFloat operator +(LSLInteger i1, double f)
  1440. // {
  1441. // return new LSLFloat((double)i1.value + f);
  1442. // }
  1443. //
  1444. // static public LSLFloat operator -(LSLInteger i1, double f)
  1445. // {
  1446. // return new LSLFloat((double)i1.value - f);
  1447. // }
  1448. //
  1449. // static public LSLFloat operator *(LSLInteger i1, double f)
  1450. // {
  1451. // return new LSLFloat((double)i1.value * f);
  1452. // }
  1453. //
  1454. // static public LSLFloat operator /(LSLInteger i1, double f)
  1455. // {
  1456. // return new LSLFloat((double)i1.value / f);
  1457. // }
  1458. static public LSLInteger operator -(LSLInteger i)
  1459. {
  1460. return new LSLInteger(-i.value);
  1461. }
  1462. static public LSLInteger operator ~(LSLInteger i)
  1463. {
  1464. return new LSLInteger(~i.value);
  1465. }
  1466. public override bool Equals(Object o)
  1467. {
  1468. if (!(o is LSLInteger))
  1469. return false;
  1470. return value == ((LSLInteger)o).value;
  1471. }
  1472. public override int GetHashCode()
  1473. {
  1474. return value;
  1475. }
  1476. static public LSLInteger operator &(LSLInteger i1, LSLInteger i2)
  1477. {
  1478. int ret = i1.value & i2.value;
  1479. return ret;
  1480. }
  1481. static public LSLInteger operator %(LSLInteger i1, LSLInteger i2)
  1482. {
  1483. int ret = i1.value % i2.value;
  1484. return ret;
  1485. }
  1486. static public LSLInteger operator |(LSLInteger i1, LSLInteger i2)
  1487. {
  1488. int ret = i1.value | i2.value;
  1489. return ret;
  1490. }
  1491. static public LSLInteger operator ^(LSLInteger i1, LSLInteger i2)
  1492. {
  1493. int ret = i1.value ^ i2.value;
  1494. return ret;
  1495. }
  1496. static public LSLInteger operator !(LSLInteger i1)
  1497. {
  1498. return i1.value == 0 ? 1 : 0;
  1499. }
  1500. public static LSLInteger operator ++(LSLInteger i)
  1501. {
  1502. i.value++;
  1503. return i;
  1504. }
  1505. public static LSLInteger operator --(LSLInteger i)
  1506. {
  1507. i.value--;
  1508. return i;
  1509. }
  1510. public static LSLInteger operator << (LSLInteger i, int s)
  1511. {
  1512. return i.value << s;
  1513. }
  1514. public static LSLInteger operator >> (LSLInteger i, int s)
  1515. {
  1516. return i.value >> s;
  1517. }
  1518. static public implicit operator System.Double(LSLInteger i)
  1519. {
  1520. return (double)i.value;
  1521. }
  1522. public static bool operator true(LSLInteger i)
  1523. {
  1524. return i.value != 0;
  1525. }
  1526. public static bool operator false(LSLInteger i)
  1527. {
  1528. return i.value == 0;
  1529. }
  1530. #endregion
  1531. #region Overriders
  1532. public override string ToString()
  1533. {
  1534. return this.value.ToString();
  1535. }
  1536. #endregion
  1537. }
  1538. [Serializable]
  1539. public struct LSLFloat
  1540. {
  1541. public double value;
  1542. #region Constructors
  1543. public LSLFloat(int i)
  1544. {
  1545. this.value = (double)i;
  1546. }
  1547. public LSLFloat(double d)
  1548. {
  1549. this.value = d;
  1550. }
  1551. public LSLFloat(string s)
  1552. {
  1553. Regex r = new Regex("^ *(\\+|-)?([0-9]+\\.?[0-9]*|\\.[0-9]+)([eE](\\+|-)?[0-9]+)?");
  1554. Match m = r.Match(s);
  1555. string v = m.Groups[0].Value;
  1556. v = v.Trim();
  1557. if (v == String.Empty || v == null)
  1558. v = "0.0";
  1559. else
  1560. if (!v.Contains(".") && !v.ToLower().Contains("e"))
  1561. v = v + ".0";
  1562. else
  1563. if (v.EndsWith("."))
  1564. v = v + "0";
  1565. this.value = double.Parse(v, System.Globalization.NumberStyles.Float, Culture.NumberFormatInfo);
  1566. }
  1567. #endregion
  1568. #region Operators
  1569. static public explicit operator float(LSLFloat f)
  1570. {
  1571. return (float)f.value;
  1572. }
  1573. static public explicit operator int(LSLFloat f)
  1574. {
  1575. return (int)f.value;
  1576. }
  1577. static public explicit operator uint(LSLFloat f)
  1578. {
  1579. return (uint) Math.Abs(f.value);
  1580. }
  1581. static public implicit operator Boolean(LSLFloat f)
  1582. {
  1583. if (f.value == 0.0)
  1584. {
  1585. return false;
  1586. }
  1587. else
  1588. {
  1589. return true;
  1590. }
  1591. }
  1592. static public implicit operator LSLFloat(int i)
  1593. {
  1594. return new LSLFloat(i);
  1595. }
  1596. static public implicit operator LSLFloat(LSLInteger i)
  1597. {
  1598. return new LSLFloat(i.value);
  1599. }
  1600. static public explicit operator LSLFloat(string s)
  1601. {
  1602. return new LSLFloat(s);
  1603. }
  1604. public static implicit operator list(LSLFloat f)
  1605. {
  1606. return new list(new object[] { f });
  1607. }
  1608. static public implicit operator LSLFloat(double d)
  1609. {
  1610. return new LSLFloat(d);
  1611. }
  1612. static public implicit operator LSLFloat(bool b)
  1613. {
  1614. if (b)
  1615. return new LSLFloat(1.0);
  1616. else
  1617. return new LSLFloat(0.0);
  1618. }
  1619. static public bool operator ==(LSLFloat f1, LSLFloat f2)
  1620. {
  1621. return f1.value == f2.value;
  1622. }
  1623. static public bool operator !=(LSLFloat f1, LSLFloat f2)
  1624. {
  1625. return f1.value != f2.value;
  1626. }
  1627. static public LSLFloat operator ++(LSLFloat f)
  1628. {
  1629. f.value++;
  1630. return f;
  1631. }
  1632. static public LSLFloat operator --(LSLFloat f)
  1633. {
  1634. f.value--;
  1635. return f;
  1636. }
  1637. static public LSLFloat operator +(LSLFloat f, int i)
  1638. {
  1639. return new LSLFloat(f.value + (double)i);
  1640. }
  1641. static public LSLFloat operator -(LSLFloat f, int i)
  1642. {
  1643. return new LSLFloat(f.value - (double)i);
  1644. }
  1645. static public LSLFloat operator *(LSLFloat f, int i)
  1646. {
  1647. return new LSLFloat(f.value * (double)i);
  1648. }
  1649. static public LSLFloat operator /(LSLFloat f, int i)
  1650. {
  1651. return new LSLFloat(f.value / (double)i);
  1652. }
  1653. static public LSLFloat operator +(LSLFloat lhs, LSLFloat rhs)
  1654. {
  1655. return new LSLFloat(lhs.value + rhs.value);
  1656. }
  1657. static public LSLFloat operator -(LSLFloat lhs, LSLFloat rhs)
  1658. {
  1659. return new LSLFloat(lhs.value - rhs.value);
  1660. }
  1661. static public LSLFloat operator *(LSLFloat lhs, LSLFloat rhs)
  1662. {
  1663. return new LSLFloat(lhs.value * rhs.value);
  1664. }
  1665. static public LSLFloat operator /(LSLFloat lhs, LSLFloat rhs)
  1666. {
  1667. return new LSLFloat(lhs.value / rhs.value);
  1668. }
  1669. static public LSLFloat operator -(LSLFloat f)
  1670. {
  1671. return new LSLFloat(-f.value);
  1672. }
  1673. static public implicit operator System.Double(LSLFloat f)
  1674. {
  1675. return f.value;
  1676. }
  1677. #endregion
  1678. #region Overriders
  1679. public override string ToString()
  1680. {
  1681. return String.Format(Culture.FormatProvider, "{0:0.000000}", this.value);
  1682. }
  1683. public override bool Equals(Object o)
  1684. {
  1685. if (!(o is LSLFloat))
  1686. return false;
  1687. return value == ((LSLFloat)o).value;
  1688. }
  1689. public override int GetHashCode()
  1690. {
  1691. return value.GetHashCode();
  1692. }
  1693. #endregion
  1694. }
  1695. }
  1696. }