LSL_Types.cs 65 KB

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