PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Aurora/AuroraDotNetEngine/APIs/LSL_Types.cs

https://bitbucket.org/VirtualReality/software-testing
C# | 2268 lines | 1798 code | 364 blank | 106 comment | 272 complexity | 1a9e32a0cd215e6e789ebe44cc512d75 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.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 Aurora-Sim 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 Aurora.Framework;
  28. using System;
  29. using System.Collections;
  30. using System.Globalization;
  31. using System.Text.RegularExpressions;
  32. using Aurora.Framework.Utilities;
  33. namespace Aurora.ScriptEngine.AuroraDotNetEngine
  34. {
  35. [Serializable]
  36. public partial class LSL_Types
  37. {
  38. // Types are kept is separate .dll to avoid having to add whatever .dll it is in it to script AppDomain
  39. [Serializable]
  40. public struct Vector3
  41. {
  42. public LSLFloat x;
  43. public LSLFloat y;
  44. public LSLFloat z;
  45. #region Constructors
  46. public Vector3(Vector3 vector)
  47. {
  48. x = (float) vector.x;
  49. y = (float) vector.y;
  50. z = (float) vector.z;
  51. }
  52. public Vector3(OpenMetaverse.Vector3 vector)
  53. {
  54. x = (float) vector.X;
  55. y = (float) vector.Y;
  56. z = (float) vector.Z;
  57. }
  58. public Vector3(double X, double Y, double Z)
  59. {
  60. x = X;
  61. y = Y;
  62. z = Z;
  63. }
  64. public Vector3(string str)
  65. {
  66. str = str.Replace('<', ' ');
  67. str = str.Replace('>', ' ');
  68. string[] tmps = str.Split(new Char[] {',', '<', '>'});
  69. if (tmps.Length < 3)
  70. {
  71. x = y = z = 0;
  72. return;
  73. }
  74. bool res;
  75. double xx, yy, zz;
  76. res = Double.TryParse(tmps[0], NumberStyles.Float, Culture.NumberFormatInfo, out xx);
  77. res = res & Double.TryParse(tmps[1], NumberStyles.Float, Culture.NumberFormatInfo, out yy);
  78. res = res & Double.TryParse(tmps[2], NumberStyles.Float, Culture.NumberFormatInfo, out zz);
  79. x = new LSLFloat(xx);
  80. y = new LSLFloat(yy);
  81. z = new LSLFloat(zz);
  82. }
  83. #endregion
  84. #region Overriders
  85. public override string ToString()
  86. {
  87. string s = String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", x, y, z);
  88. return s;
  89. }
  90. public static explicit operator LSLString(Vector3 vec)
  91. {
  92. string s = String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", vec.x, vec.y, vec.z);
  93. return new LSLString(s);
  94. }
  95. public static explicit operator string(Vector3 vec)
  96. {
  97. string s = String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", vec.x, vec.y, vec.z);
  98. return s;
  99. }
  100. public static explicit operator Vector3(string s)
  101. {
  102. return new Vector3(s);
  103. }
  104. public static implicit operator bool(Vector3 s)
  105. {
  106. return s.x != 0 ||
  107. s.y != 0 ||
  108. s.z != 0;
  109. }
  110. public static implicit operator list(Vector3 vec)
  111. {
  112. return new list(new object[] {vec});
  113. }
  114. public static bool operator ==(Vector3 lhs, Vector3 rhs)
  115. {
  116. return (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z);
  117. }
  118. public static bool operator !=(Vector3 lhs, Vector3 rhs)
  119. {
  120. return !(lhs == rhs);
  121. }
  122. public override int GetHashCode()
  123. {
  124. return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode());
  125. }
  126. public override bool Equals(object o)
  127. {
  128. if (!(o is Vector3)) return false;
  129. Vector3 vector = (Vector3) o;
  130. return (x == vector.x && y == vector.y && z == vector.z);
  131. }
  132. public static Vector3 operator -(Vector3 vector)
  133. {
  134. return new Vector3(-vector.x, -vector.y, -vector.z);
  135. }
  136. #endregion
  137. #region Vector & Vector Math
  138. // Vector-Vector Math
  139. public static Vector3 operator +(Vector3 lhs, Vector3 rhs)
  140. {
  141. return new Vector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
  142. }
  143. public static Vector3 operator -(Vector3 lhs, Vector3 rhs)
  144. {
  145. return new Vector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
  146. }
  147. public static LSLFloat operator *(Vector3 lhs, Vector3 rhs)
  148. {
  149. return Dot(lhs, rhs);
  150. }
  151. public static Vector3 operator %(Vector3 v1, Vector3 v2)
  152. {
  153. //Cross product
  154. Vector3 tv;
  155. tv.x = (v1.y*v2.z) - (v1.z*v2.y);
  156. tv.y = (v1.z*v2.x) - (v1.x*v2.z);
  157. tv.z = (v1.x*v2.y) - (v1.y*v2.x);
  158. return tv;
  159. }
  160. #endregion
  161. #region Vector & Float Math
  162. // Vector-Float and Float-Vector Math
  163. public static Vector3 operator *(Vector3 vec, float val)
  164. {
  165. return new Vector3(vec.x*val, vec.y*val, vec.z*val);
  166. }
  167. public static Vector3 operator *(float 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, float 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 & Double Math
  180. public static Vector3 operator *(Vector3 vec, double val)
  181. {
  182. return new Vector3(vec.x*val, vec.y*val, vec.z*val);
  183. }
  184. public static Vector3 operator *(double val, Vector3 vec)
  185. {
  186. return new Vector3(vec.x*val, vec.y*val, vec.z*val);
  187. }
  188. public static Vector3 operator /(Vector3 v, double f)
  189. {
  190. v.x = v.x/f;
  191. v.y = v.y/f;
  192. v.z = v.z/f;
  193. return v;
  194. }
  195. #endregion
  196. #region Vector & Rotation Math
  197. // Vector-Rotation Math
  198. public static Vector3 operator *(Vector3 v, Quaternion r)
  199. {
  200. Quaternion vq = new Quaternion(v.x, v.y, v.z, 0);
  201. Quaternion nq = new Quaternion(-r.x, -r.y, -r.z, r.s);
  202. // adapted for operator * computing "b * a"
  203. Quaternion result = nq*(vq*r);
  204. return new Vector3(result.x, result.y, result.z);
  205. }
  206. public static Vector3 operator /(Vector3 v, Quaternion r)
  207. {
  208. r.s = -r.s;
  209. return v*r;
  210. }
  211. #endregion
  212. #region Static Helper Functions
  213. public static double Dot(Vector3 v1, Vector3 v2)
  214. {
  215. return (v1.x*v2.x) + (v1.y*v2.y) + (v1.z*v2.z);
  216. }
  217. public static Vector3 Cross(Vector3 v1, Vector3 v2)
  218. {
  219. return new Vector3
  220. (
  221. v1.y*v2.z - v1.z*v2.y,
  222. v1.z*v2.x - v1.x*v2.z,
  223. v1.x*v2.y - v1.y*v2.x
  224. );
  225. }
  226. public static double Mag(Vector3 v)
  227. {
  228. return Math.Sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
  229. }
  230. public static Vector3 Norm(Vector3 vector)
  231. {
  232. double mag = Mag(vector);
  233. if (mag > 0.0)
  234. {
  235. double invMag = 1.0/mag;
  236. return vector*invMag;
  237. }
  238. return new Vector3(0, 0, 0);
  239. }
  240. public OpenMetaverse.Vector3 ToVector3()
  241. {
  242. return new OpenMetaverse.Vector3((float) x.value, (float) y.value, (float) z.value);
  243. }
  244. #endregion
  245. }
  246. [Serializable]
  247. public struct Quaternion
  248. {
  249. public double x;
  250. public double y;
  251. public double z;
  252. public double s;
  253. #region Constructors
  254. public Quaternion(Quaternion Quat)
  255. {
  256. x = (float) Quat.x;
  257. y = (float) Quat.y;
  258. z = (float) Quat.z;
  259. s = (float) Quat.s;
  260. if (x == 0 && y == 0 && z == 0 && s == 0)
  261. s = 1;
  262. }
  263. public Quaternion(double X, double Y, double Z, double S)
  264. {
  265. x = X;
  266. y = Y;
  267. z = Z;
  268. s = S;
  269. if (x == 0 && y == 0 && z == 0 && s == 0)
  270. s = 1;
  271. }
  272. public Quaternion(string str)
  273. {
  274. str = str.Replace('<', ' ');
  275. str = str.Replace('>', ' ');
  276. string[] tmps = str.Split(new Char[] {',', '<', '>'});
  277. if (tmps.Length < 4)
  278. {
  279. x = y = z = s = 0;
  280. return;
  281. }
  282. bool res = Double.TryParse(tmps[0], NumberStyles.Float, Culture.NumberFormatInfo, out x);
  283. res = res & Double.TryParse(tmps[1], NumberStyles.Float, Culture.NumberFormatInfo, out y);
  284. res = res & Double.TryParse(tmps[2], NumberStyles.Float, Culture.NumberFormatInfo, out z);
  285. res = res & Double.TryParse(tmps[3], NumberStyles.Float, Culture.NumberFormatInfo, out s);
  286. if (x == 0 && y == 0 && z == 0 && s == 0)
  287. s = 1;
  288. }
  289. public Quaternion(OpenMetaverse.Quaternion rot)
  290. {
  291. x = rot.X;
  292. y = rot.Y;
  293. z = rot.Z;
  294. s = rot.W;
  295. }
  296. #endregion
  297. #region Overriders
  298. public override int GetHashCode()
  299. {
  300. return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode() ^ s.GetHashCode());
  301. }
  302. public override bool Equals(object o)
  303. {
  304. if (!(o is Quaternion)) return false;
  305. Quaternion quaternion = (Quaternion) o;
  306. return x == quaternion.x && y == quaternion.y && z == quaternion.z && s == quaternion.s;
  307. }
  308. public override string ToString()
  309. {
  310. string st = String.Format(Culture.FormatProvider,
  311. "<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", x, y, z, s);
  312. return st;
  313. }
  314. public static explicit operator string(Quaternion r)
  315. {
  316. string s = String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
  317. return s;
  318. }
  319. public static explicit operator LSLString(Quaternion r)
  320. {
  321. string s = String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
  322. return new LSLString(s);
  323. }
  324. public static explicit operator Quaternion(string s)
  325. {
  326. return new Quaternion(s);
  327. }
  328. public static implicit operator bool(Quaternion s)
  329. {
  330. return s.x != 0 ||
  331. s.y != 0 ||
  332. s.z != 0 ||
  333. s.s != 1;
  334. }
  335. public static implicit operator list(Quaternion r)
  336. {
  337. return new list(new object[] {r});
  338. }
  339. public static bool operator ==(Quaternion lhs, Quaternion rhs)
  340. {
  341. // Return true if the fields match:
  342. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.s == rhs.s;
  343. }
  344. public static bool operator !=(Quaternion lhs, Quaternion rhs)
  345. {
  346. return !(lhs == rhs);
  347. }
  348. public static double Mag(Quaternion q)
  349. {
  350. return Math.Sqrt(q.x*q.x + q.y*q.y + q.z*q.z + q.s*q.s);
  351. }
  352. #endregion
  353. public static Quaternion operator +(Quaternion a, Quaternion b)
  354. {
  355. return new Quaternion(a.x + b.x, a.y + b.y, a.z + b.z, a.s + b.s);
  356. }
  357. public static Quaternion operator /(Quaternion a, Quaternion b)
  358. {
  359. b.s = -b.s;
  360. return a*b;
  361. }
  362. public static Quaternion operator -(Quaternion a, Quaternion b)
  363. {
  364. return new Quaternion(a.x - b.x, a.y - b.y, a.z - b.z, a.s - b.s);
  365. }
  366. // using the equations below, we need to do "b * a" to be compatible with LSL
  367. public static Quaternion operator *(Quaternion b, Quaternion a)
  368. {
  369. Quaternion c;
  370. c.x = a.s*b.x + a.x*b.s + a.y*b.z - a.z*b.y;
  371. c.y = a.s*b.y + a.y*b.s + a.z*b.x - a.x*b.z;
  372. c.z = a.s*b.z + a.z*b.s + a.x*b.y - a.y*b.x;
  373. c.s = a.s*b.s - a.x*b.x - a.y*b.y - a.z*b.z;
  374. return c;
  375. }
  376. public OpenMetaverse.Quaternion ToQuaternion()
  377. {
  378. return new OpenMetaverse.Quaternion((float) x, (float) y, (float) z, (float) s);
  379. }
  380. public OpenMetaverse.Vector4 ToVector4()
  381. {
  382. return new OpenMetaverse.Vector4((float) x, (float) y, (float) z, (float) s);
  383. }
  384. }
  385. [Serializable]
  386. public class list : IEnumerator
  387. {
  388. private object[] m_data;
  389. public list(params object[] args)
  390. {
  391. m_data = args;
  392. }
  393. public int Length
  394. {
  395. get
  396. {
  397. if (m_data == null)
  398. m_data = new Object[0];
  399. return m_data.Length;
  400. }
  401. }
  402. public int Size
  403. {
  404. get
  405. {
  406. if (m_data == null)
  407. m_data = new Object[0];
  408. int size = 0;
  409. foreach (Object o in m_data)
  410. {
  411. if (o is LSL_Types.LSLInteger)
  412. size += 4;
  413. else if (o is LSL_Types.LSLFloat)
  414. size += 8;
  415. else if (o is LSL_Types.LSLString)
  416. size += ((LSL_Types.LSLString) o).m_string.Length;
  417. else if (o is LSL_Types.key)
  418. size += ((LSL_Types.key) o).value.Length;
  419. else if (o is LSL_Types.Vector3)
  420. size += 32;
  421. else if (o is LSL_Types.Quaternion)
  422. size += 64;
  423. else if (o is int)
  424. size += 4;
  425. else if (o is string)
  426. size += ((string) o).Length;
  427. else if (o is float)
  428. size += 8;
  429. else if (o is double)
  430. size += 16;
  431. else
  432. throw new Exception("Unknown type in List.Size: " + o.GetType().ToString());
  433. }
  434. return size;
  435. }
  436. }
  437. public object[] Data
  438. {
  439. get
  440. {
  441. if (m_data == null)
  442. m_data = new Object[0];
  443. return m_data;
  444. }
  445. set { m_data = value; }
  446. }
  447. // Function to obtain LSL type from an index. This is needed
  448. // because LSL lists allow for multiple types, and safely
  449. // iterating in them requires a type check.
  450. public Type GetLSLListItemType(int itemIndex)
  451. {
  452. return m_data[itemIndex].GetType();
  453. }
  454. // Member functions to obtain item as specific types.
  455. // For cases where implicit conversions would apply if items
  456. // were not in a list (e.g. integer to float, but not float
  457. // to integer) functions check for alternate types so as to
  458. // down-cast from Object to the correct type.
  459. // Note: no checks for item index being valid are performed
  460. public LSL_Types.LSLFloat GetLSLFloatItem(int itemIndex)
  461. {
  462. if (m_data[itemIndex] is LSL_Types.LSLInteger)
  463. {
  464. return (LSL_Types.LSLInteger) m_data[itemIndex];
  465. }
  466. else if (m_data[itemIndex] is Int32)
  467. {
  468. return new LSL_Types.LSLFloat((int) m_data[itemIndex]);
  469. }
  470. else if (m_data[itemIndex] is float)
  471. {
  472. return new LSL_Types.LSLFloat((float) m_data[itemIndex]);
  473. }
  474. else if (m_data[itemIndex] is Double)
  475. {
  476. return new LSL_Types.LSLFloat((Double) m_data[itemIndex]);
  477. }
  478. else if (m_data[itemIndex] is LSL_Types.LSLString)
  479. {
  480. return new LSL_Types.LSLFloat(m_data[itemIndex].ToString());
  481. }
  482. else
  483. {
  484. return (LSL_Types.LSLFloat) m_data[itemIndex];
  485. }
  486. }
  487. public LSL_Types.LSLString GetLSLStringItem(int itemIndex)
  488. {
  489. if (m_data[itemIndex] is LSL_Types.key)
  490. {
  491. return (LSL_Types.key) m_data[itemIndex];
  492. }
  493. else if (m_data[itemIndex] is String)
  494. {
  495. return new LSL_Types.LSLString((string) m_data[itemIndex]);
  496. }
  497. else if (m_data[itemIndex] is LSL_Types.LSLFloat)
  498. {
  499. return new LSL_Types.LSLString((LSLFloat) m_data[itemIndex]);
  500. }
  501. else if (m_data[itemIndex] is LSL_Types.LSLInteger)
  502. {
  503. return new LSL_Types.LSLString((LSLInteger) m_data[itemIndex]);
  504. }
  505. else
  506. {
  507. return (LSL_Types.LSLString) m_data[itemIndex];
  508. }
  509. }
  510. public LSL_Types.LSLInteger GetLSLIntegerItem(int itemIndex)
  511. {
  512. if (m_data[itemIndex] is LSL_Types.LSLInteger)
  513. return (LSL_Types.LSLInteger) m_data[itemIndex];
  514. if (m_data[itemIndex] is LSL_Types.LSLFloat)
  515. return new LSLInteger((int) m_data[itemIndex]);
  516. else if (m_data[itemIndex] is Int32)
  517. return new LSLInteger((int) m_data[itemIndex]);
  518. else if (m_data[itemIndex] is LSL_Types.LSLString)
  519. return new LSLInteger(((LSL_Types.LSLString) m_data[itemIndex]).m_string);
  520. else
  521. throw new InvalidCastException(string.Format(
  522. "{0} expected but {1} given",
  523. typeof (LSL_Types.LSLInteger).Name,
  524. m_data[itemIndex] != null
  525. ? m_data[itemIndex].GetType().Name
  526. : "null"));
  527. }
  528. public LSL_Types.Vector3 GetVector3Item(int itemIndex)
  529. {
  530. if (m_data[itemIndex] is LSL_Types.Vector3)
  531. {
  532. return (LSL_Types.Vector3) m_data[itemIndex];
  533. }
  534. else if (m_data[itemIndex] is OpenMetaverse.Vector3)
  535. {
  536. return new LSL_Types.Vector3(
  537. (OpenMetaverse.Vector3) m_data[itemIndex]);
  538. }
  539. else
  540. {
  541. throw new InvalidCastException(string.Format(
  542. "{0} expected but {1} given",
  543. typeof (LSL_Types.Vector3).Name,
  544. m_data[itemIndex] != null
  545. ? m_data[itemIndex].GetType().Name
  546. : "null"));
  547. }
  548. }
  549. public LSL_Types.Quaternion GetQuaternionItem(int itemIndex)
  550. {
  551. if (m_data[itemIndex] is LSL_Types.Quaternion)
  552. {
  553. return (LSL_Types.Quaternion) m_data[itemIndex];
  554. }
  555. else if (m_data[itemIndex] is OpenMetaverse.Quaternion)
  556. {
  557. return new LSL_Types.Quaternion(
  558. (OpenMetaverse.Quaternion) m_data[itemIndex]);
  559. }
  560. else
  561. {
  562. throw new InvalidCastException(string.Format(
  563. "{0} expected but {1} given",
  564. typeof (LSL_Types.Quaternion).Name,
  565. m_data[itemIndex] != null
  566. ? m_data[itemIndex].GetType().Name
  567. : "null"));
  568. }
  569. }
  570. public LSL_Types.key GetKeyItem(int itemIndex)
  571. {
  572. return (LSL_Types.key) m_data[itemIndex];
  573. }
  574. public static list operator +(list a, list b)
  575. {
  576. object[] tmp;
  577. tmp = new object[a.Length + b.Length];
  578. a.Data.CopyTo(tmp, 0);
  579. b.Data.CopyTo(tmp, a.Length);
  580. return new list(tmp);
  581. }
  582. private void ExtendAndAdd(object o)
  583. {
  584. Array.Resize(ref m_data, Length + 1);
  585. m_data.SetValue(o, Length - 1);
  586. }
  587. public static implicit operator bool(list s)
  588. {
  589. return s.Length != 0;
  590. }
  591. public static list operator +(list a, LSLString s)
  592. {
  593. a.ExtendAndAdd(s);
  594. return a;
  595. }
  596. public static list operator +(list a, LSLInteger i)
  597. {
  598. a.ExtendAndAdd(i);
  599. return a;
  600. }
  601. public static list operator +(list a, LSLFloat d)
  602. {
  603. a.ExtendAndAdd(d);
  604. return a;
  605. }
  606. public static bool operator ==(list a, list b)
  607. {
  608. int la = -1;
  609. int lb = -1;
  610. try
  611. {
  612. la = a.Length;
  613. }
  614. catch (NullReferenceException)
  615. {
  616. }
  617. try
  618. {
  619. lb = b.Length;
  620. }
  621. catch (NullReferenceException)
  622. {
  623. }
  624. return la == lb;
  625. }
  626. public static bool operator !=(list a, list b)
  627. {
  628. int la = -1;
  629. int lb = -1;
  630. try
  631. {
  632. la = a.Length;
  633. }
  634. catch (NullReferenceException)
  635. {
  636. }
  637. try
  638. {
  639. lb = b.Length;
  640. }
  641. catch (NullReferenceException)
  642. {
  643. }
  644. return la != lb;
  645. }
  646. public void Add(object o)
  647. {
  648. object[] tmp;
  649. tmp = new object[m_data.Length + 1];
  650. m_data.CopyTo(tmp, 0);
  651. tmp[m_data.Length] = o;
  652. m_data = tmp;
  653. }
  654. public bool Contains(object o)
  655. {
  656. bool ret = false;
  657. foreach (object i in Data)
  658. {
  659. if (i == o)
  660. {
  661. ret = true;
  662. break;
  663. }
  664. }
  665. return ret;
  666. }
  667. public list DeleteSublist(int start, int end)
  668. {
  669. // Not an easy one
  670. // If start <= end, remove that part
  671. // if either is negative, count from the end of the array
  672. // if the resulting start > end, remove all BUT that part
  673. Object[] ret;
  674. if (start < 0)
  675. start = m_data.Length + start;
  676. if (start < 0)
  677. start = 0;
  678. if (end < 0)
  679. end = m_data.Length + end;
  680. if (end < 0)
  681. end = 0;
  682. if (start > end)
  683. {
  684. if (end >= m_data.Length)
  685. return new list(new Object[0]);
  686. if (start >= m_data.Length)
  687. start = m_data.Length - 1;
  688. return GetSublist(end, start);
  689. }
  690. // start >= 0 && end >= 0 here
  691. if (start >= m_data.Length)
  692. {
  693. ret = new Object[m_data.Length];
  694. Array.Copy(m_data, 0, ret, 0, m_data.Length);
  695. return new list(ret);
  696. }
  697. if (end >= m_data.Length)
  698. end = m_data.Length - 1;
  699. // now, this makes the math easier
  700. int remove = end + 1 - start;
  701. ret = new Object[m_data.Length - remove];
  702. if (ret.Length == 0)
  703. return new list(ret);
  704. int src;
  705. int dest = 0;
  706. for (src = 0; src < m_data.Length; src++)
  707. {
  708. if (src < start || src > end)
  709. ret[dest++] = m_data[src];
  710. }
  711. return new list(ret);
  712. }
  713. public list GetSublist(int start, int end)
  714. {
  715. object[] ret;
  716. // Take care of neg start or end's
  717. // NOTE that either index may still be negative after
  718. // adding the length, so we must take additional
  719. // measures to protect against this. Note also that
  720. // after normalisation the negative indices are no
  721. // longer relative to the end of the list.
  722. if (start < 0)
  723. {
  724. start = m_data.Length + start;
  725. }
  726. if (end < 0)
  727. {
  728. end = m_data.Length + end;
  729. }
  730. // The conventional case is start <= end
  731. // NOTE that the case of an empty list is
  732. // dealt with by the initial test. Start
  733. // less than end is taken to be the most
  734. // common case.
  735. if (start <= end)
  736. {
  737. // Start sublist beyond length
  738. // Also deals with start AND end still negative
  739. if (start >= m_data.Length || end < 0)
  740. {
  741. return new list();
  742. }
  743. // Sublist extends beyond the end of the supplied list
  744. if (end >= m_data.Length)
  745. {
  746. end = m_data.Length - 1;
  747. }
  748. // Sublist still starts before the beginning of the list
  749. if (start < 0)
  750. {
  751. start = 0;
  752. }
  753. ret = new object[end - start + 1];
  754. Array.Copy(m_data, start, ret, 0, end - start + 1);
  755. return new list(ret);
  756. }
  757. // Deal with the segmented case: 0->end + start->EOL
  758. else
  759. {
  760. list result = null;
  761. // If end is negative, then prefix list is empty
  762. if (end < 0)
  763. {
  764. result = new list();
  765. // If start is still negative, then the whole of
  766. // the existing list is returned. This case is
  767. // only admitted if end is also still negative.
  768. if (start < 0)
  769. {
  770. return this;
  771. }
  772. }
  773. else
  774. {
  775. result = GetSublist(0, end);
  776. }
  777. // If start is outside of list, then just return
  778. // the prefix, whatever it is.
  779. if (start >= m_data.Length)
  780. {
  781. return result;
  782. }
  783. return result + GetSublist(start, Data.Length);
  784. }
  785. }
  786. private static int compare(object left, object right, int ascending)
  787. {
  788. if (!left.GetType().Equals(right.GetType()))
  789. {
  790. // unequal types are always "equal" for comparison purposes.
  791. // this way, the bubble sort will never swap them, and we'll
  792. // get that feathered effect we're looking for
  793. return 0;
  794. }
  795. int ret = 0;
  796. if (left is key)
  797. {
  798. key l = (key) left;
  799. key r = (key) right;
  800. ret = String.CompareOrdinal(l.value, r.value);
  801. }
  802. else if (left is LSLString)
  803. {
  804. LSLString l = (LSLString) left;
  805. LSLString r = (LSLString) right;
  806. ret = String.CompareOrdinal(l.m_string, r.m_string);
  807. }
  808. else if (left is LSLInteger)
  809. {
  810. LSLInteger l = (LSLInteger) left;
  811. LSLInteger r = (LSLInteger) right;
  812. ret = Math.Sign(l.value - r.value);
  813. }
  814. else if (left is LSLFloat)
  815. {
  816. LSLFloat l = (LSLFloat) left;
  817. LSLFloat r = (LSLFloat) right;
  818. ret = Math.Sign(l.value - r.value);
  819. }
  820. else if (left is Vector3)
  821. {
  822. Vector3 l = (Vector3) left;
  823. Vector3 r = (Vector3) right;
  824. ret = Math.Sign(Vector3.Mag(l) - Vector3.Mag(r));
  825. }
  826. else if (left is Quaternion)
  827. {
  828. Quaternion l = (Quaternion) left;
  829. Quaternion r = (Quaternion) right;
  830. ret = Math.Sign(Quaternion.Mag(l) - Quaternion.Mag(r));
  831. }
  832. if (ascending == 0)
  833. {
  834. ret = 0 - ret;
  835. }
  836. return ret;
  837. }
  838. private class HomogeneousComparer : IComparer
  839. {
  840. public HomogeneousComparer()
  841. {
  842. }
  843. public int Compare(object lhs, object rhs)
  844. {
  845. return compare(lhs, rhs, 1);
  846. }
  847. }
  848. public list Sort(int stride, int ascending)
  849. {
  850. if (Data.Length == 0)
  851. return new list(); // Don't even bother
  852. object[] ret = new object[Data.Length];
  853. Array.Copy(Data, 0, ret, 0, Data.Length);
  854. if (stride <= 0)
  855. {
  856. stride = 1;
  857. }
  858. // we can optimize here in the case where stride == 1 and the list
  859. // consists of homogeneous types
  860. if (stride == 1)
  861. {
  862. bool homogeneous = true;
  863. int index;
  864. for (index = 1; index < Data.Length; index++)
  865. {
  866. if (!Data[0].GetType().Equals(Data[index].GetType()))
  867. {
  868. homogeneous = false;
  869. break;
  870. }
  871. }
  872. if (homogeneous)
  873. {
  874. Array.Sort(ret, new HomogeneousComparer());
  875. if (ascending == 0)
  876. {
  877. Array.Reverse(ret);
  878. }
  879. return new list(ret);
  880. }
  881. }
  882. // Because of the desired type specific feathered sorting behavior
  883. // requried by the spec, we MUST use a non-optimized bubble sort here.
  884. // Anything else will give you the incorrect behavior.
  885. // begin bubble sort...
  886. int i;
  887. int j;
  888. int k;
  889. int n = Data.Length;
  890. for (i = 0; i < (n - stride); i += stride)
  891. {
  892. for (j = i + stride; j < n; j += stride)
  893. {
  894. if (compare(ret[i], ret[j], ascending) > 0)
  895. {
  896. for (k = 0; k < stride; k++)
  897. {
  898. object tmp = ret[i + k];
  899. ret[i + k] = ret[j + k];
  900. ret[j + k] = tmp;
  901. }
  902. }
  903. }
  904. }
  905. // end bubble sort
  906. return new list(ret);
  907. }
  908. #region CSV Methods
  909. public static list FromCSV(string csv)
  910. {
  911. return new list(csv.Split(','));
  912. }
  913. public string ToCSV()
  914. {
  915. string ret = "";
  916. foreach (object o in this.Data)
  917. {
  918. if (ret == "")
  919. {
  920. ret = o.ToString();
  921. }
  922. else
  923. {
  924. ret = ret + ", " + o.ToString();
  925. }
  926. }
  927. return ret;
  928. }
  929. private string ToSoup()
  930. {
  931. string output;
  932. output = String.Empty;
  933. if (m_data.Length == 0)
  934. {
  935. return String.Empty;
  936. }
  937. foreach (object o in m_data)
  938. {
  939. output = output + o.ToString();
  940. }
  941. return output;
  942. }
  943. public static explicit operator String(list l)
  944. {
  945. return l.ToSoup();
  946. }
  947. public static explicit operator LSLString(list l)
  948. {
  949. return new LSLString(l.ToSoup());
  950. }
  951. public override string ToString()
  952. {
  953. return ToSoup();
  954. }
  955. #endregion
  956. #region Statistic Methods
  957. public double Min()
  958. {
  959. double minimum = double.PositiveInfinity;
  960. double entry;
  961. for (int i = 0; i < Data.Length; i++)
  962. {
  963. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  964. {
  965. if (entry < minimum) minimum = entry;
  966. }
  967. }
  968. return minimum;
  969. }
  970. public double Max()
  971. {
  972. double maximum = double.NegativeInfinity;
  973. double entry;
  974. for (int i = 0; i < Data.Length; i++)
  975. {
  976. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  977. {
  978. if (entry > maximum) maximum = entry;
  979. }
  980. }
  981. return maximum;
  982. }
  983. public double Range()
  984. {
  985. return (this.Max()/this.Min());
  986. }
  987. public int NumericLength()
  988. {
  989. int count = 0;
  990. double entry;
  991. for (int i = 0; i < Data.Length; i++)
  992. {
  993. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  994. {
  995. count++;
  996. }
  997. }
  998. return count;
  999. }
  1000. public static list ToDoubleList(list src)
  1001. {
  1002. list ret = new list();
  1003. double entry;
  1004. for (int i = 0; i < src.Data.Length - 1; i++)
  1005. {
  1006. if (double.TryParse(src.Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  1007. {
  1008. ret.Add(entry);
  1009. }
  1010. }
  1011. return ret;
  1012. }
  1013. public double Sum()
  1014. {
  1015. double sum = 0;
  1016. double entry;
  1017. for (int i = 0; i < Data.Length; i++)
  1018. {
  1019. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  1020. {
  1021. sum = sum + entry;
  1022. }
  1023. }
  1024. return sum;
  1025. }
  1026. public double SumSqrs()
  1027. {
  1028. double sum = 0;
  1029. double entry;
  1030. for (int i = 0; i < Data.Length; i++)
  1031. {
  1032. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  1033. {
  1034. sum = sum + Math.Pow(entry, 2);
  1035. }
  1036. }
  1037. return sum;
  1038. }
  1039. public double Mean()
  1040. {
  1041. return (this.Sum()/this.NumericLength());
  1042. }
  1043. public void NumericSort()
  1044. {
  1045. IComparer Numeric = new NumericComparer();
  1046. Array.Sort(Data, Numeric);
  1047. }
  1048. public void AlphaSort()
  1049. {
  1050. IComparer Alpha = new AlphaCompare();
  1051. Array.Sort(Data, Alpha);
  1052. }
  1053. public double Median()
  1054. {
  1055. return Qi(0.5);
  1056. }
  1057. public double GeometricMean()
  1058. {
  1059. double ret = 1.0;
  1060. list nums = ToDoubleList(this);
  1061. for (int i = 0; i < nums.Data.Length; i++)
  1062. {
  1063. ret *= (double) nums.Data[i];
  1064. }
  1065. return Math.Exp(Math.Log(ret)/(double) nums.Data.Length);
  1066. }
  1067. public double HarmonicMean()
  1068. {
  1069. double ret = 0.0;
  1070. list nums = ToDoubleList(this);
  1071. for (int i = 0; i < nums.Data.Length; i++)
  1072. {
  1073. ret += 1.0/(double) nums.Data[i];
  1074. }
  1075. return ((double) nums.Data.Length/ret);
  1076. }
  1077. public double Variance()
  1078. {
  1079. double s = 0;
  1080. list num = ToDoubleList(this);
  1081. for (int i = 0; i < num.Data.Length; i++)
  1082. {
  1083. s += Math.Pow((double) num.Data[i], 2);
  1084. }
  1085. return (s - num.Data.Length*Math.Pow(num.Mean(), 2))/(num.Data.Length - 1);
  1086. }
  1087. public double StdDev()
  1088. {
  1089. return Math.Sqrt(this.Variance());
  1090. }
  1091. public double Qi(double i)
  1092. {
  1093. list j = this;
  1094. j.NumericSort();
  1095. if (Math.Ceiling(this.Length*i) == this.Length*i)
  1096. {
  1097. return (double) ((double) j.Data[(int) (this.Length*i - 1)] + (double) j.Data[(int) (this.Length*i)])/2;
  1098. }
  1099. else
  1100. {
  1101. return (double) j.Data[((int) (Math.Ceiling(this.Length*i))) - 1];
  1102. }
  1103. }
  1104. #endregion
  1105. public string ToPrettyString()
  1106. {
  1107. string output;
  1108. if (m_data.Length == 0)
  1109. {
  1110. return "[]";
  1111. }
  1112. output = "[";
  1113. foreach (object o in m_data)
  1114. {
  1115. if (o is String)
  1116. {
  1117. output = output + "\"" + o + "\", ";
  1118. }
  1119. else
  1120. {
  1121. output = output + o.ToString() + ", ";
  1122. }
  1123. }
  1124. output = output.Substring(0, output.Length - 2);
  1125. output = output + "]";
  1126. return output;
  1127. }
  1128. public class AlphaCompare : IComparer
  1129. {
  1130. int IComparer.Compare(object x, object y)
  1131. {
  1132. return string.Compare(x.ToString(), y.ToString());
  1133. }
  1134. }
  1135. public class NumericComparer : IComparer
  1136. {
  1137. int IComparer.Compare(object x, object y)
  1138. {
  1139. double a;
  1140. double b;
  1141. if (!double.TryParse(x.ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out a))
  1142. {
  1143. a = 0.0;
  1144. }
  1145. if (!double.TryParse(y.ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out b))
  1146. {
  1147. b = 0.0;
  1148. }
  1149. if (a < b)
  1150. {
  1151. return -1;
  1152. }
  1153. else if (a == b)
  1154. {
  1155. return 0;
  1156. }
  1157. else
  1158. {
  1159. return 1;
  1160. }
  1161. }
  1162. }
  1163. public override bool Equals(object o)
  1164. {
  1165. if (!(o is list))
  1166. return false;
  1167. return Data.Length == ((list) o).Data.Length;
  1168. }
  1169. public override int GetHashCode()
  1170. {
  1171. return Data.GetHashCode();
  1172. }
  1173. #region IEnumerator Members
  1174. private int i = 0;
  1175. public object Current
  1176. {
  1177. get { return m_data[i]; }
  1178. }
  1179. public bool MoveNext()
  1180. {
  1181. i++;
  1182. if (m_data.Length == i)
  1183. return false;
  1184. return true;
  1185. }
  1186. public void Reset()
  1187. {
  1188. i = 0;
  1189. }
  1190. #endregion
  1191. }
  1192. [Serializable]
  1193. public struct key
  1194. {
  1195. public string value;
  1196. #region Constructors
  1197. public key(string s)
  1198. {
  1199. value = s;
  1200. }
  1201. public key(OpenMetaverse.UUID s)
  1202. {
  1203. value = s.ToString();
  1204. }
  1205. public key(LSLString s)
  1206. {
  1207. value = s.ToString();
  1208. }
  1209. #endregion
  1210. #region Methods
  1211. public static bool Parse2Key(string s)
  1212. {
  1213. Regex isuuid =
  1214. 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}$",
  1215. RegexOptions.Compiled);
  1216. if (isuuid.IsMatch(s))
  1217. {
  1218. return true;
  1219. }
  1220. else
  1221. {
  1222. return false;
  1223. }
  1224. }
  1225. #endregion
  1226. #region Operators
  1227. public static implicit operator Boolean(key k)
  1228. {
  1229. if (k.value.Length == 0)
  1230. {
  1231. return false;
  1232. }
  1233. if (k.value == "00000000-0000-0000-0000-000000000000")
  1234. {
  1235. return false;
  1236. }
  1237. Regex isuuid =
  1238. 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}$",
  1239. RegexOptions.Compiled);
  1240. if (isuuid.IsMatch(k.value))
  1241. {
  1242. return true;
  1243. }
  1244. else
  1245. {
  1246. return false;
  1247. }
  1248. }
  1249. public static implicit operator key(string s)
  1250. {
  1251. return new key(s);
  1252. }
  1253. public static implicit operator String(key k)
  1254. {
  1255. return k.value;
  1256. }
  1257. public static implicit operator LSLString(key k)
  1258. {
  1259. return k.value;
  1260. }
  1261. public static bool operator ==(key k1, key k2)
  1262. {
  1263. return k1.value == k2.value;
  1264. }
  1265. public static bool operator !=(key k1, key k2)
  1266. {
  1267. return k1.value != k2.value;
  1268. }
  1269. #endregion
  1270. #region Overriders
  1271. public override bool Equals(object o)
  1272. {
  1273. return o.ToString() == value;
  1274. }
  1275. public override int GetHashCode()
  1276. {
  1277. return value.GetHashCode();
  1278. }
  1279. public override string ToString()
  1280. {
  1281. return value;
  1282. }
  1283. #endregion
  1284. }
  1285. [Serializable]
  1286. public struct LSLString
  1287. {
  1288. public string m_string;
  1289. #region Constructors
  1290. public LSLString(string s)
  1291. {
  1292. m_string = s;
  1293. }
  1294. public LSLString(OpenMetaverse.UUID s)
  1295. {
  1296. m_string = s.ToString();
  1297. }
  1298. public LSLString(double d)
  1299. {
  1300. string s = String.Form

Large files files are truncated, but you can click here to view the full file