LSL_Types.cs 60 KB

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