LSL_Types.cs 68 KB

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