LSL_Types.cs 57 KB

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