1
0

PrimitiveBaseShape.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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.Generic;
  29. using System.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. using System.Xml.Serialization;
  36. using log4net;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. namespace OpenSim.Framework
  40. {
  41. public enum ProfileShape : byte
  42. {
  43. Circle = 0,
  44. Square = 1,
  45. IsometricTriangle = 2,
  46. EquilateralTriangle = 3,
  47. RightTriangle = 4,
  48. HalfCircle = 5
  49. }
  50. public enum HollowShape : byte
  51. {
  52. Same = 0,
  53. Circle = 16,
  54. Square = 32,
  55. Triangle = 48
  56. }
  57. public enum PCodeEnum : byte
  58. {
  59. Primitive = 9,
  60. Avatar = 47,
  61. Grass = 95,
  62. NewTree = 111,
  63. ParticleSystem = 143,
  64. Tree = 255
  65. }
  66. public enum Extrusion : byte
  67. {
  68. Straight = 16,
  69. Curve1 = 32,
  70. Curve2 = 48,
  71. Flexible = 128
  72. }
  73. [Serializable]
  74. public class PrimitiveBaseShape
  75. {
  76. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  77. private static readonly byte[] DEFAULT_TEXTURE = new Primitive.TextureEntry(new UUID("89556747-24cb-43ed-920b-47caed15465f")).GetBytes();
  78. private byte[] m_textureEntry;
  79. private ushort _pathBegin;
  80. private byte _pathCurve;
  81. private ushort _pathEnd;
  82. private sbyte _pathRadiusOffset;
  83. private byte _pathRevolutions;
  84. private byte _pathScaleX;
  85. private byte _pathScaleY;
  86. private byte _pathShearX;
  87. private byte _pathShearY;
  88. private sbyte _pathSkew;
  89. private sbyte _pathTaperX;
  90. private sbyte _pathTaperY;
  91. private sbyte _pathTwist;
  92. private sbyte _pathTwistBegin;
  93. private byte _pCode;
  94. private ushort _profileBegin;
  95. private ushort _profileEnd;
  96. private ushort _profileHollow;
  97. private Vector3 _scale;
  98. private byte _state;
  99. private ProfileShape _profileShape;
  100. private HollowShape _hollowShape;
  101. // Sculpted
  102. [XmlIgnore] private UUID _sculptTexture;
  103. [XmlIgnore] private byte _sculptType;
  104. [XmlIgnore] private byte[] _sculptData = Utils.EmptyBytes;
  105. // Flexi
  106. [XmlIgnore] private int _flexiSoftness;
  107. [XmlIgnore] private float _flexiTension;
  108. [XmlIgnore] private float _flexiDrag;
  109. [XmlIgnore] private float _flexiGravity;
  110. [XmlIgnore] private float _flexiWind;
  111. [XmlIgnore] private float _flexiForceX;
  112. [XmlIgnore] private float _flexiForceY;
  113. [XmlIgnore] private float _flexiForceZ;
  114. //Bright n sparkly
  115. [XmlIgnore] private float _lightColorR;
  116. [XmlIgnore] private float _lightColorG;
  117. [XmlIgnore] private float _lightColorB;
  118. [XmlIgnore] private float _lightColorA = 1.0f;
  119. [XmlIgnore] private float _lightRadius;
  120. [XmlIgnore] private float _lightCutoff;
  121. [XmlIgnore] private float _lightFalloff;
  122. [XmlIgnore] private float _lightIntensity = 1.0f;
  123. [XmlIgnore] private bool _flexiEntry;
  124. [XmlIgnore] private bool _lightEntry;
  125. [XmlIgnore] private bool _sculptEntry;
  126. // Light Projection Filter
  127. [XmlIgnore] private bool _projectionEntry;
  128. [XmlIgnore] private UUID _projectionTextureID;
  129. [XmlIgnore] private float _projectionFOV;
  130. [XmlIgnore] private float _projectionFocus;
  131. [XmlIgnore] private float _projectionAmb;
  132. public byte ProfileCurve
  133. {
  134. get { return (byte)((byte)HollowShape | (byte)ProfileShape); }
  135. set
  136. {
  137. // Handle hollow shape component
  138. byte hollowShapeByte = (byte)(value & 0xf0);
  139. if (!Enum.IsDefined(typeof(HollowShape), hollowShapeByte))
  140. {
  141. m_log.WarnFormat(
  142. "[SHAPE]: Attempt to set a ProfileCurve with a hollow shape value of {0}, which isn't a valid enum. Replacing with default shape.",
  143. hollowShapeByte);
  144. this._hollowShape = HollowShape.Same;
  145. }
  146. else
  147. {
  148. this._hollowShape = (HollowShape)hollowShapeByte;
  149. }
  150. // Handle profile shape component
  151. byte profileShapeByte = (byte)(value & 0xf);
  152. if (!Enum.IsDefined(typeof(ProfileShape), profileShapeByte))
  153. {
  154. m_log.WarnFormat(
  155. "[SHAPE]: Attempt to set a ProfileCurve with a profile shape value of {0}, which isn't a valid enum. Replacing with square.",
  156. profileShapeByte);
  157. this._profileShape = ProfileShape.Square;
  158. }
  159. else
  160. {
  161. this._profileShape = (ProfileShape)profileShapeByte;
  162. }
  163. }
  164. }
  165. /// <summary>
  166. /// Entries to store media textures on each face
  167. /// </summary>
  168. /// Do not change this value directly - always do it through an IMoapModule.
  169. /// Lock before manipulating.
  170. public MediaList Media { get; set; }
  171. public PrimitiveBaseShape()
  172. {
  173. PCode = (byte)PCodeEnum.Primitive;
  174. m_textureEntry = DEFAULT_TEXTURE;
  175. }
  176. /// <summary>
  177. /// Construct a PrimitiveBaseShape object from a OpenMetaverse.Primitive object
  178. /// </summary>
  179. /// <param name="prim"></param>
  180. public PrimitiveBaseShape(Primitive prim)
  181. {
  182. // m_log.DebugFormat("[PRIMITIVE BASE SHAPE]: Creating from {0}", prim.ID);
  183. PCode = (byte)prim.PrimData.PCode;
  184. State = prim.PrimData.State;
  185. PathBegin = Primitive.PackBeginCut(prim.PrimData.PathBegin);
  186. PathEnd = Primitive.PackEndCut(prim.PrimData.PathEnd);
  187. PathScaleX = Primitive.PackPathScale(prim.PrimData.PathScaleX);
  188. PathScaleY = Primitive.PackPathScale(prim.PrimData.PathScaleY);
  189. PathShearX = (byte)Primitive.PackPathShear(prim.PrimData.PathShearX);
  190. PathShearY = (byte)Primitive.PackPathShear(prim.PrimData.PathShearY);
  191. PathSkew = Primitive.PackPathTwist(prim.PrimData.PathSkew);
  192. ProfileBegin = Primitive.PackBeginCut(prim.PrimData.ProfileBegin);
  193. ProfileEnd = Primitive.PackEndCut(prim.PrimData.ProfileEnd);
  194. Scale = prim.Scale;
  195. PathCurve = (byte)prim.PrimData.PathCurve;
  196. ProfileCurve = (byte)prim.PrimData.ProfileCurve;
  197. ProfileHollow = Primitive.PackProfileHollow(prim.PrimData.ProfileHollow);
  198. PathRadiusOffset = Primitive.PackPathTwist(prim.PrimData.PathRadiusOffset);
  199. PathRevolutions = Primitive.PackPathRevolutions(prim.PrimData.PathRevolutions);
  200. PathTaperX = Primitive.PackPathTaper(prim.PrimData.PathTaperX);
  201. PathTaperY = Primitive.PackPathTaper(prim.PrimData.PathTaperY);
  202. PathTwist = Primitive.PackPathTwist(prim.PrimData.PathTwist);
  203. PathTwistBegin = Primitive.PackPathTwist(prim.PrimData.PathTwistBegin);
  204. m_textureEntry = prim.Textures.GetBytes();
  205. if (prim.Sculpt != null)
  206. {
  207. SculptEntry = (prim.Sculpt.Type != OpenMetaverse.SculptType.None);
  208. SculptData = prim.Sculpt.GetBytes();
  209. SculptTexture = prim.Sculpt.SculptTexture;
  210. SculptType = (byte)prim.Sculpt.Type;
  211. }
  212. else
  213. {
  214. SculptType = (byte)OpenMetaverse.SculptType.None;
  215. }
  216. }
  217. [XmlIgnore]
  218. public Primitive.TextureEntry Textures
  219. {
  220. get
  221. {
  222. // m_log.DebugFormat("[SHAPE]: get m_textureEntry length {0}", m_textureEntry.Length);
  223. try { return new Primitive.TextureEntry(m_textureEntry, 0, m_textureEntry.Length); }
  224. catch { }
  225. m_log.Warn("[SHAPE]: Failed to decode texture, length=" + ((m_textureEntry != null) ? m_textureEntry.Length : 0));
  226. return new Primitive.TextureEntry(UUID.Zero);
  227. }
  228. set { m_textureEntry = value.GetBytes(); }
  229. }
  230. public byte[] TextureEntry
  231. {
  232. get { return m_textureEntry; }
  233. set
  234. {
  235. if (value == null)
  236. m_textureEntry = new byte[1];
  237. else
  238. m_textureEntry = value;
  239. }
  240. }
  241. public static PrimitiveBaseShape Default
  242. {
  243. get
  244. {
  245. PrimitiveBaseShape boxShape = CreateBox();
  246. boxShape.SetScale(0.5f);
  247. return boxShape;
  248. }
  249. }
  250. public static PrimitiveBaseShape Create()
  251. {
  252. PrimitiveBaseShape shape = new PrimitiveBaseShape();
  253. return shape;
  254. }
  255. public static PrimitiveBaseShape CreateBox()
  256. {
  257. PrimitiveBaseShape shape = Create();
  258. shape._pathCurve = (byte) Extrusion.Straight;
  259. shape._profileShape = ProfileShape.Square;
  260. shape._pathScaleX = 100;
  261. shape._pathScaleY = 100;
  262. return shape;
  263. }
  264. public static PrimitiveBaseShape CreateSphere()
  265. {
  266. PrimitiveBaseShape shape = Create();
  267. shape._pathCurve = (byte) Extrusion.Curve1;
  268. shape._profileShape = ProfileShape.HalfCircle;
  269. shape._pathScaleX = 100;
  270. shape._pathScaleY = 100;
  271. return shape;
  272. }
  273. public static PrimitiveBaseShape CreateCylinder()
  274. {
  275. PrimitiveBaseShape shape = Create();
  276. shape._pathCurve = (byte) Extrusion.Curve1;
  277. shape._profileShape = ProfileShape.Square;
  278. shape._pathScaleX = 100;
  279. shape._pathScaleY = 100;
  280. return shape;
  281. }
  282. public void SetScale(float side)
  283. {
  284. _scale = new Vector3(side, side, side);
  285. }
  286. public void SetHeigth(float height)
  287. {
  288. _scale.Z = height;
  289. }
  290. public void SetRadius(float radius)
  291. {
  292. _scale.X = _scale.Y = radius * 2f;
  293. }
  294. // TODO: void returns need to change of course
  295. public virtual void GetMesh()
  296. {
  297. }
  298. public PrimitiveBaseShape Copy()
  299. {
  300. return (PrimitiveBaseShape) MemberwiseClone();
  301. }
  302. public static PrimitiveBaseShape CreateCylinder(float radius, float heigth)
  303. {
  304. PrimitiveBaseShape shape = CreateCylinder();
  305. shape.SetHeigth(heigth);
  306. shape.SetRadius(radius);
  307. return shape;
  308. }
  309. public void SetPathRange(Vector3 pathRange)
  310. {
  311. _pathBegin = Primitive.PackBeginCut(pathRange.X);
  312. _pathEnd = Primitive.PackEndCut(pathRange.Y);
  313. }
  314. public void SetPathRange(float begin, float end)
  315. {
  316. _pathBegin = Primitive.PackBeginCut(begin);
  317. _pathEnd = Primitive.PackEndCut(end);
  318. }
  319. public void SetSculptProperties(byte sculptType, UUID SculptTextureUUID)
  320. {
  321. _sculptType = sculptType;
  322. _sculptTexture = SculptTextureUUID;
  323. }
  324. public void SetProfileRange(Vector3 profileRange)
  325. {
  326. _profileBegin = Primitive.PackBeginCut(profileRange.X);
  327. _profileEnd = Primitive.PackEndCut(profileRange.Y);
  328. }
  329. public void SetProfileRange(float begin, float end)
  330. {
  331. _profileBegin = Primitive.PackBeginCut(begin);
  332. _profileEnd = Primitive.PackEndCut(end);
  333. }
  334. public byte[] ExtraParams
  335. {
  336. get
  337. {
  338. return ExtraParamsToBytes();
  339. }
  340. set
  341. {
  342. ReadInExtraParamsBytes(value);
  343. }
  344. }
  345. public ushort PathBegin {
  346. get {
  347. return _pathBegin;
  348. }
  349. set {
  350. _pathBegin = value;
  351. }
  352. }
  353. public byte PathCurve {
  354. get {
  355. return _pathCurve;
  356. }
  357. set {
  358. _pathCurve = value;
  359. }
  360. }
  361. public ushort PathEnd {
  362. get {
  363. return _pathEnd;
  364. }
  365. set {
  366. _pathEnd = value;
  367. }
  368. }
  369. public sbyte PathRadiusOffset {
  370. get {
  371. return _pathRadiusOffset;
  372. }
  373. set {
  374. _pathRadiusOffset = value;
  375. }
  376. }
  377. public byte PathRevolutions {
  378. get {
  379. return _pathRevolutions;
  380. }
  381. set {
  382. _pathRevolutions = value;
  383. }
  384. }
  385. public byte PathScaleX {
  386. get {
  387. return _pathScaleX;
  388. }
  389. set {
  390. _pathScaleX = value;
  391. }
  392. }
  393. public byte PathScaleY {
  394. get {
  395. return _pathScaleY;
  396. }
  397. set {
  398. _pathScaleY = value;
  399. }
  400. }
  401. public byte PathShearX {
  402. get {
  403. return _pathShearX;
  404. }
  405. set {
  406. _pathShearX = value;
  407. }
  408. }
  409. public byte PathShearY {
  410. get {
  411. return _pathShearY;
  412. }
  413. set {
  414. _pathShearY = value;
  415. }
  416. }
  417. public sbyte PathSkew {
  418. get {
  419. return _pathSkew;
  420. }
  421. set {
  422. _pathSkew = value;
  423. }
  424. }
  425. public sbyte PathTaperX {
  426. get {
  427. return _pathTaperX;
  428. }
  429. set {
  430. _pathTaperX = value;
  431. }
  432. }
  433. public sbyte PathTaperY {
  434. get {
  435. return _pathTaperY;
  436. }
  437. set {
  438. _pathTaperY = value;
  439. }
  440. }
  441. public sbyte PathTwist {
  442. get {
  443. return _pathTwist;
  444. }
  445. set {
  446. _pathTwist = value;
  447. }
  448. }
  449. public sbyte PathTwistBegin {
  450. get {
  451. return _pathTwistBegin;
  452. }
  453. set {
  454. _pathTwistBegin = value;
  455. }
  456. }
  457. public byte PCode {
  458. get {
  459. return _pCode;
  460. }
  461. set {
  462. _pCode = value;
  463. }
  464. }
  465. public ushort ProfileBegin {
  466. get {
  467. return _profileBegin;
  468. }
  469. set {
  470. _profileBegin = value;
  471. }
  472. }
  473. public ushort ProfileEnd {
  474. get {
  475. return _profileEnd;
  476. }
  477. set {
  478. _profileEnd = value;
  479. }
  480. }
  481. public ushort ProfileHollow {
  482. get {
  483. return _profileHollow;
  484. }
  485. set {
  486. _profileHollow = value;
  487. }
  488. }
  489. public Vector3 Scale {
  490. get {
  491. return _scale;
  492. }
  493. set {
  494. _scale = value;
  495. }
  496. }
  497. public byte State {
  498. get {
  499. return _state;
  500. }
  501. set {
  502. _state = value;
  503. }
  504. }
  505. public ProfileShape ProfileShape {
  506. get {
  507. return _profileShape;
  508. }
  509. set {
  510. _profileShape = value;
  511. }
  512. }
  513. public HollowShape HollowShape {
  514. get {
  515. return _hollowShape;
  516. }
  517. set {
  518. _hollowShape = value;
  519. }
  520. }
  521. public UUID SculptTexture {
  522. get {
  523. return _sculptTexture;
  524. }
  525. set {
  526. _sculptTexture = value;
  527. }
  528. }
  529. public byte SculptType
  530. {
  531. get
  532. {
  533. return _sculptType;
  534. }
  535. set
  536. {
  537. _sculptType = value;
  538. }
  539. }
  540. // This is only used at runtime. For sculpties this holds the texture data, and for meshes
  541. // the mesh data.
  542. public byte[] SculptData
  543. {
  544. get
  545. {
  546. return _sculptData;
  547. }
  548. set
  549. {
  550. // m_log.DebugFormat("[PRIMITIVE BASE SHAPE]: Setting SculptData to data with length {0}", value.Length);
  551. _sculptData = value;
  552. }
  553. }
  554. public int FlexiSoftness
  555. {
  556. get
  557. {
  558. return _flexiSoftness;
  559. }
  560. set
  561. {
  562. _flexiSoftness = value;
  563. }
  564. }
  565. public float FlexiTension {
  566. get {
  567. return _flexiTension;
  568. }
  569. set {
  570. _flexiTension = value;
  571. }
  572. }
  573. public float FlexiDrag {
  574. get {
  575. return _flexiDrag;
  576. }
  577. set {
  578. _flexiDrag = value;
  579. }
  580. }
  581. public float FlexiGravity {
  582. get {
  583. return _flexiGravity;
  584. }
  585. set {
  586. _flexiGravity = value;
  587. }
  588. }
  589. public float FlexiWind {
  590. get {
  591. return _flexiWind;
  592. }
  593. set {
  594. _flexiWind = value;
  595. }
  596. }
  597. public float FlexiForceX {
  598. get {
  599. return _flexiForceX;
  600. }
  601. set {
  602. _flexiForceX = value;
  603. }
  604. }
  605. public float FlexiForceY {
  606. get {
  607. return _flexiForceY;
  608. }
  609. set {
  610. _flexiForceY = value;
  611. }
  612. }
  613. public float FlexiForceZ {
  614. get {
  615. return _flexiForceZ;
  616. }
  617. set {
  618. _flexiForceZ = value;
  619. }
  620. }
  621. public float LightColorR {
  622. get {
  623. return _lightColorR;
  624. }
  625. set {
  626. _lightColorR = value;
  627. }
  628. }
  629. public float LightColorG {
  630. get {
  631. return _lightColorG;
  632. }
  633. set {
  634. _lightColorG = value;
  635. }
  636. }
  637. public float LightColorB {
  638. get {
  639. return _lightColorB;
  640. }
  641. set {
  642. _lightColorB = value;
  643. }
  644. }
  645. public float LightColorA {
  646. get {
  647. return _lightColorA;
  648. }
  649. set {
  650. _lightColorA = value;
  651. }
  652. }
  653. public float LightRadius {
  654. get {
  655. return _lightRadius;
  656. }
  657. set {
  658. _lightRadius = value;
  659. }
  660. }
  661. public float LightCutoff {
  662. get {
  663. return _lightCutoff;
  664. }
  665. set {
  666. _lightCutoff = value;
  667. }
  668. }
  669. public float LightFalloff {
  670. get {
  671. return _lightFalloff;
  672. }
  673. set {
  674. _lightFalloff = value;
  675. }
  676. }
  677. public float LightIntensity {
  678. get {
  679. return _lightIntensity;
  680. }
  681. set {
  682. _lightIntensity = value;
  683. }
  684. }
  685. public bool FlexiEntry {
  686. get {
  687. return _flexiEntry;
  688. }
  689. set {
  690. _flexiEntry = value;
  691. }
  692. }
  693. public bool LightEntry {
  694. get {
  695. return _lightEntry;
  696. }
  697. set {
  698. _lightEntry = value;
  699. }
  700. }
  701. public bool SculptEntry {
  702. get {
  703. return _sculptEntry;
  704. }
  705. set {
  706. _sculptEntry = value;
  707. }
  708. }
  709. public bool ProjectionEntry {
  710. get {
  711. return _projectionEntry;
  712. }
  713. set {
  714. _projectionEntry = value;
  715. }
  716. }
  717. public UUID ProjectionTextureUUID {
  718. get {
  719. return _projectionTextureID;
  720. }
  721. set {
  722. _projectionTextureID = value;
  723. }
  724. }
  725. public float ProjectionFOV {
  726. get {
  727. return _projectionFOV;
  728. }
  729. set {
  730. _projectionFOV = value;
  731. }
  732. }
  733. public float ProjectionFocus {
  734. get {
  735. return _projectionFocus;
  736. }
  737. set {
  738. _projectionFocus = value;
  739. }
  740. }
  741. public float ProjectionAmbiance {
  742. get {
  743. return _projectionAmb;
  744. }
  745. set {
  746. _projectionAmb = value;
  747. }
  748. }
  749. public ulong GetMeshKey(Vector3 size, float lod)
  750. {
  751. ulong hash = 5381;
  752. hash = djb2(hash, this.PathCurve);
  753. hash = djb2(hash, (byte)((byte)this.HollowShape | (byte)this.ProfileShape));
  754. hash = djb2(hash, this.PathBegin);
  755. hash = djb2(hash, this.PathEnd);
  756. hash = djb2(hash, this.PathScaleX);
  757. hash = djb2(hash, this.PathScaleY);
  758. hash = djb2(hash, this.PathShearX);
  759. hash = djb2(hash, this.PathShearY);
  760. hash = djb2(hash, (byte)this.PathTwist);
  761. hash = djb2(hash, (byte)this.PathTwistBegin);
  762. hash = djb2(hash, (byte)this.PathRadiusOffset);
  763. hash = djb2(hash, (byte)this.PathTaperX);
  764. hash = djb2(hash, (byte)this.PathTaperY);
  765. hash = djb2(hash, this.PathRevolutions);
  766. hash = djb2(hash, (byte)this.PathSkew);
  767. hash = djb2(hash, this.ProfileBegin);
  768. hash = djb2(hash, this.ProfileEnd);
  769. hash = djb2(hash, this.ProfileHollow);
  770. // TODO: Separate scale out from the primitive shape data (after
  771. // scaling is supported at the physics engine level)
  772. byte[] scaleBytes = size.GetBytes();
  773. for (int i = 0; i < scaleBytes.Length; i++)
  774. hash = djb2(hash, scaleBytes[i]);
  775. // Include LOD in hash, accounting for endianness
  776. byte[] lodBytes = new byte[4];
  777. Buffer.BlockCopy(BitConverter.GetBytes(lod), 0, lodBytes, 0, 4);
  778. if (!BitConverter.IsLittleEndian)
  779. {
  780. Array.Reverse(lodBytes, 0, 4);
  781. }
  782. for (int i = 0; i < lodBytes.Length; i++)
  783. hash = djb2(hash, lodBytes[i]);
  784. // include sculpt UUID
  785. if (this.SculptEntry)
  786. {
  787. scaleBytes = this.SculptTexture.GetBytes();
  788. for (int i = 0; i < scaleBytes.Length; i++)
  789. hash = djb2(hash, scaleBytes[i]);
  790. }
  791. return hash;
  792. }
  793. private ulong djb2(ulong hash, byte c)
  794. {
  795. return ((hash << 5) + hash) + (ulong)c;
  796. }
  797. private ulong djb2(ulong hash, ushort c)
  798. {
  799. hash = ((hash << 5) + hash) + (ulong)((byte)c);
  800. return ((hash << 5) + hash) + (ulong)(c >> 8);
  801. }
  802. public byte[] ExtraParamsToBytes()
  803. {
  804. // m_log.DebugFormat("[EXTRAPARAMS]: Called ExtraParamsToBytes()");
  805. ushort FlexiEP = 0x10;
  806. ushort LightEP = 0x20;
  807. ushort SculptEP = 0x30;
  808. ushort ProjectionEP = 0x40;
  809. int i = 0;
  810. uint TotalBytesLength = 1; // ExtraParamsNum
  811. uint ExtraParamsNum = 0;
  812. if (_flexiEntry)
  813. {
  814. ExtraParamsNum++;
  815. TotalBytesLength += 16;// data
  816. TotalBytesLength += 2 + 4; // type
  817. }
  818. if (_lightEntry)
  819. {
  820. ExtraParamsNum++;
  821. TotalBytesLength += 16;// data
  822. TotalBytesLength += 2 + 4; // type
  823. }
  824. if (_sculptEntry)
  825. {
  826. ExtraParamsNum++;
  827. TotalBytesLength += 17;// data
  828. TotalBytesLength += 2 + 4; // type
  829. }
  830. if (_projectionEntry)
  831. {
  832. ExtraParamsNum++;
  833. TotalBytesLength += 28;// data
  834. TotalBytesLength += 2 + 4;// type
  835. }
  836. byte[] returnbytes = new byte[TotalBytesLength];
  837. // uint paramlength = ExtraParamsNum;
  838. // Stick in the number of parameters
  839. returnbytes[i++] = (byte)ExtraParamsNum;
  840. if (_flexiEntry)
  841. {
  842. byte[] FlexiData = GetFlexiBytes();
  843. returnbytes[i++] = (byte)(FlexiEP % 256);
  844. returnbytes[i++] = (byte)((FlexiEP >> 8) % 256);
  845. returnbytes[i++] = (byte)(FlexiData.Length % 256);
  846. returnbytes[i++] = (byte)((FlexiData.Length >> 8) % 256);
  847. returnbytes[i++] = (byte)((FlexiData.Length >> 16) % 256);
  848. returnbytes[i++] = (byte)((FlexiData.Length >> 24) % 256);
  849. Array.Copy(FlexiData, 0, returnbytes, i, FlexiData.Length);
  850. i += FlexiData.Length;
  851. }
  852. if (_lightEntry)
  853. {
  854. byte[] LightData = GetLightBytes();
  855. returnbytes[i++] = (byte)(LightEP % 256);
  856. returnbytes[i++] = (byte)((LightEP >> 8) % 256);
  857. returnbytes[i++] = (byte)(LightData.Length % 256);
  858. returnbytes[i++] = (byte)((LightData.Length >> 8) % 256);
  859. returnbytes[i++] = (byte)((LightData.Length >> 16) % 256);
  860. returnbytes[i++] = (byte)((LightData.Length >> 24) % 256);
  861. Array.Copy(LightData, 0, returnbytes, i, LightData.Length);
  862. i += LightData.Length;
  863. }
  864. if (_sculptEntry)
  865. {
  866. byte[] SculptData = GetSculptBytes();
  867. returnbytes[i++] = (byte)(SculptEP % 256);
  868. returnbytes[i++] = (byte)((SculptEP >> 8) % 256);
  869. returnbytes[i++] = (byte)(SculptData.Length % 256);
  870. returnbytes[i++] = (byte)((SculptData.Length >> 8) % 256);
  871. returnbytes[i++] = (byte)((SculptData.Length >> 16) % 256);
  872. returnbytes[i++] = (byte)((SculptData.Length >> 24) % 256);
  873. Array.Copy(SculptData, 0, returnbytes, i, SculptData.Length);
  874. i += SculptData.Length;
  875. }
  876. if (_projectionEntry)
  877. {
  878. byte[] ProjectionData = GetProjectionBytes();
  879. returnbytes[i++] = (byte)(ProjectionEP % 256);
  880. returnbytes[i++] = (byte)((ProjectionEP >> 8) % 256);
  881. returnbytes[i++] = (byte)((ProjectionData.Length) % 256);
  882. returnbytes[i++] = (byte)((ProjectionData.Length >> 16) % 256);
  883. returnbytes[i++] = (byte)((ProjectionData.Length >> 20) % 256);
  884. returnbytes[i++] = (byte)((ProjectionData.Length >> 24) % 256);
  885. Array.Copy(ProjectionData, 0, returnbytes, i, ProjectionData.Length);
  886. i += ProjectionData.Length;
  887. }
  888. if (!_flexiEntry && !_lightEntry && !_sculptEntry && !_projectionEntry)
  889. {
  890. byte[] returnbyte = new byte[1];
  891. returnbyte[0] = 0;
  892. return returnbyte;
  893. }
  894. return returnbytes;
  895. }
  896. public void ReadInUpdateExtraParam(ushort type, bool inUse, byte[] data)
  897. {
  898. const ushort FlexiEP = 0x10;
  899. const ushort LightEP = 0x20;
  900. const ushort SculptEP = 0x30;
  901. const ushort ProjectionEP = 0x40;
  902. switch (type)
  903. {
  904. case FlexiEP:
  905. if (!inUse)
  906. {
  907. _flexiEntry = false;
  908. return;
  909. }
  910. ReadFlexiData(data, 0);
  911. break;
  912. case LightEP:
  913. if (!inUse)
  914. {
  915. _lightEntry = false;
  916. return;
  917. }
  918. ReadLightData(data, 0);
  919. break;
  920. case SculptEP:
  921. if (!inUse)
  922. {
  923. _sculptEntry = false;
  924. return;
  925. }
  926. ReadSculptData(data, 0);
  927. break;
  928. case ProjectionEP:
  929. if (!inUse)
  930. {
  931. _projectionEntry = false;
  932. return;
  933. }
  934. ReadProjectionData(data, 0);
  935. break;
  936. }
  937. }
  938. public void ReadInExtraParamsBytes(byte[] data)
  939. {
  940. if (data == null || data.Length == 1)
  941. return;
  942. const ushort FlexiEP = 0x10;
  943. const ushort LightEP = 0x20;
  944. const ushort SculptEP = 0x30;
  945. const ushort ProjectionEP = 0x40;
  946. bool lGotFlexi = false;
  947. bool lGotLight = false;
  948. bool lGotSculpt = false;
  949. bool lGotFilter = false;
  950. int i = 0;
  951. byte extraParamCount = 0;
  952. if (data.Length > 0)
  953. {
  954. extraParamCount = data[i++];
  955. }
  956. for (int k = 0; k < extraParamCount; k++)
  957. {
  958. ushort epType = Utils.BytesToUInt16(data, i);
  959. i += 2;
  960. // uint paramLength = Helpers.BytesToUIntBig(data, i);
  961. i += 4;
  962. switch (epType)
  963. {
  964. case FlexiEP:
  965. ReadFlexiData(data, i);
  966. i += 16;
  967. lGotFlexi = true;
  968. break;
  969. case LightEP:
  970. ReadLightData(data, i);
  971. i += 16;
  972. lGotLight = true;
  973. break;
  974. case SculptEP:
  975. ReadSculptData(data, i);
  976. i += 17;
  977. lGotSculpt = true;
  978. break;
  979. case ProjectionEP:
  980. ReadProjectionData(data, i);
  981. i += 28;
  982. lGotFilter = true;
  983. break;
  984. }
  985. }
  986. if (!lGotFlexi)
  987. _flexiEntry = false;
  988. if (!lGotLight)
  989. _lightEntry = false;
  990. if (!lGotSculpt)
  991. _sculptEntry = false;
  992. if (!lGotFilter)
  993. _projectionEntry = false;
  994. }
  995. public void ReadSculptData(byte[] data, int pos)
  996. {
  997. UUID SculptUUID;
  998. byte SculptTypel;
  999. if (data.Length-pos >= 17)
  1000. {
  1001. _sculptEntry = true;
  1002. byte[] SculptTextureUUID = new byte[16];
  1003. SculptTypel = data[16 + pos];
  1004. Array.Copy(data, pos, SculptTextureUUID,0, 16);
  1005. SculptUUID = new UUID(SculptTextureUUID, 0);
  1006. }
  1007. else
  1008. {
  1009. _sculptEntry = false;
  1010. SculptUUID = UUID.Zero;
  1011. SculptTypel = 0x00;
  1012. }
  1013. if (_sculptEntry)
  1014. {
  1015. if (_sculptType != (byte)1 && _sculptType != (byte)2 && _sculptType != (byte)3 && _sculptType != (byte)4)
  1016. _sculptType = 4;
  1017. }
  1018. _sculptTexture = SculptUUID;
  1019. _sculptType = SculptTypel;
  1020. //m_log.Info("[SCULPT]:" + SculptUUID.ToString());
  1021. }
  1022. public byte[] GetSculptBytes()
  1023. {
  1024. byte[] data = new byte[17];
  1025. _sculptTexture.GetBytes().CopyTo(data, 0);
  1026. data[16] = (byte)_sculptType;
  1027. return data;
  1028. }
  1029. public void ReadFlexiData(byte[] data, int pos)
  1030. {
  1031. if (data.Length-pos >= 16)
  1032. {
  1033. _flexiEntry = true;
  1034. _flexiSoftness = ((data[pos] & 0x80) >> 6) | ((data[pos + 1] & 0x80) >> 7);
  1035. _flexiTension = (float)(data[pos++] & 0x7F) / 10.0f;
  1036. _flexiDrag = (float)(data[pos++] & 0x7F) / 10.0f;
  1037. _flexiGravity = (float)(data[pos++] / 10.0f) - 10.0f;
  1038. _flexiWind = (float)data[pos++] / 10.0f;
  1039. Vector3 lForce = new Vector3(data, pos);
  1040. _flexiForceX = lForce.X;
  1041. _flexiForceY = lForce.Y;
  1042. _flexiForceZ = lForce.Z;
  1043. }
  1044. else
  1045. {
  1046. _flexiEntry = false;
  1047. _flexiSoftness = 0;
  1048. _flexiTension = 0.0f;
  1049. _flexiDrag = 0.0f;
  1050. _flexiGravity = 0.0f;
  1051. _flexiWind = 0.0f;
  1052. _flexiForceX = 0f;
  1053. _flexiForceY = 0f;
  1054. _flexiForceZ = 0f;
  1055. }
  1056. }
  1057. public byte[] GetFlexiBytes()
  1058. {
  1059. byte[] data = new byte[16];
  1060. int i = 0;
  1061. // Softness is packed in the upper bits of tension and drag
  1062. data[i] = (byte)((_flexiSoftness & 2) << 6);
  1063. data[i + 1] = (byte)((_flexiSoftness & 1) << 7);
  1064. data[i++] |= (byte)((byte)(_flexiTension * 10.01f) & 0x7F);
  1065. data[i++] |= (byte)((byte)(_flexiDrag * 10.01f) & 0x7F);
  1066. data[i++] = (byte)((_flexiGravity + 10.0f) * 10.01f);
  1067. data[i++] = (byte)(_flexiWind * 10.01f);
  1068. Vector3 lForce = new Vector3(_flexiForceX, _flexiForceY, _flexiForceZ);
  1069. lForce.GetBytes().CopyTo(data, i);
  1070. return data;
  1071. }
  1072. public void ReadLightData(byte[] data, int pos)
  1073. {
  1074. if (data.Length - pos >= 16)
  1075. {
  1076. _lightEntry = true;
  1077. Color4 lColor = new Color4(data, pos, false);
  1078. _lightIntensity = lColor.A;
  1079. _lightColorA = 1f;
  1080. _lightColorR = lColor.R;
  1081. _lightColorG = lColor.G;
  1082. _lightColorB = lColor.B;
  1083. _lightRadius = Utils.BytesToFloat(data, pos + 4);
  1084. _lightCutoff = Utils.BytesToFloat(data, pos + 8);
  1085. _lightFalloff = Utils.BytesToFloat(data, pos + 12);
  1086. }
  1087. else
  1088. {
  1089. _lightEntry = false;
  1090. _lightColorA = 1f;
  1091. _lightColorR = 0f;
  1092. _lightColorG = 0f;
  1093. _lightColorB = 0f;
  1094. _lightRadius = 0f;
  1095. _lightCutoff = 0f;
  1096. _lightFalloff = 0f;
  1097. _lightIntensity = 0f;
  1098. }
  1099. }
  1100. public byte[] GetLightBytes()
  1101. {
  1102. byte[] data = new byte[16];
  1103. // Alpha channel in color is intensity
  1104. Color4 tmpColor = new Color4(_lightColorR,_lightColorG,_lightColorB,_lightIntensity);
  1105. tmpColor.GetBytes().CopyTo(data, 0);
  1106. Utils.FloatToBytes(_lightRadius).CopyTo(data, 4);
  1107. Utils.FloatToBytes(_lightCutoff).CopyTo(data, 8);
  1108. Utils.FloatToBytes(_lightFalloff).CopyTo(data, 12);
  1109. return data;
  1110. }
  1111. public void ReadProjectionData(byte[] data, int pos)
  1112. {
  1113. byte[] ProjectionTextureUUID = new byte[16];
  1114. if (data.Length - pos >= 28)
  1115. {
  1116. _projectionEntry = true;
  1117. Array.Copy(data, pos, ProjectionTextureUUID,0, 16);
  1118. _projectionTextureID = new UUID(ProjectionTextureUUID, 0);
  1119. _projectionFOV = Utils.BytesToFloat(data, pos + 16);
  1120. _projectionFocus = Utils.BytesToFloat(data, pos + 20);
  1121. _projectionAmb = Utils.BytesToFloat(data, pos + 24);
  1122. }
  1123. else
  1124. {
  1125. _projectionEntry = false;
  1126. _projectionTextureID = UUID.Zero;
  1127. _projectionFOV = 0f;
  1128. _projectionFocus = 0f;
  1129. _projectionAmb = 0f;
  1130. }
  1131. }
  1132. public byte[] GetProjectionBytes()
  1133. {
  1134. byte[] data = new byte[28];
  1135. _projectionTextureID.GetBytes().CopyTo(data, 0);
  1136. Utils.FloatToBytes(_projectionFOV).CopyTo(data, 16);
  1137. Utils.FloatToBytes(_projectionFocus).CopyTo(data, 20);
  1138. Utils.FloatToBytes(_projectionAmb).CopyTo(data, 24);
  1139. return data;
  1140. }
  1141. /// <summary>
  1142. /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
  1143. /// </summary>
  1144. /// <returns></returns>
  1145. public Primitive ToOmvPrimitive()
  1146. {
  1147. // position and rotation defaults here since they are not available in PrimitiveBaseShape
  1148. return ToOmvPrimitive(new Vector3(0.0f, 0.0f, 0.0f),
  1149. new Quaternion(0.0f, 0.0f, 0.0f, 1.0f));
  1150. }
  1151. /// <summary>
  1152. /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
  1153. /// </summary>
  1154. /// <param name="position"></param>
  1155. /// <param name="rotation"></param>
  1156. /// <returns></returns>
  1157. public Primitive ToOmvPrimitive(Vector3 position, Quaternion rotation)
  1158. {
  1159. OpenMetaverse.Primitive prim = new OpenMetaverse.Primitive();
  1160. prim.Scale = this.Scale;
  1161. prim.Position = position;
  1162. prim.Rotation = rotation;
  1163. if (this.SculptEntry)
  1164. {
  1165. prim.Sculpt = new Primitive.SculptData();
  1166. prim.Sculpt.Type = (OpenMetaverse.SculptType)this.SculptType;
  1167. prim.Sculpt.SculptTexture = this.SculptTexture;
  1168. }
  1169. prim.PrimData.PathShearX = this.PathShearX < 128 ? (float)this.PathShearX * 0.01f : (float)(this.PathShearX - 256) * 0.01f;
  1170. prim.PrimData.PathShearY = this.PathShearY < 128 ? (float)this.PathShearY * 0.01f : (float)(this.PathShearY - 256) * 0.01f;
  1171. prim.PrimData.PathBegin = (float)this.PathBegin * 2.0e-5f;
  1172. prim.PrimData.PathEnd = 1.0f - (float)this.PathEnd * 2.0e-5f;
  1173. prim.PrimData.PathScaleX = (200 - this.PathScaleX) * 0.01f;
  1174. prim.PrimData.PathScaleY = (200 - this.PathScaleY) * 0.01f;
  1175. prim.PrimData.PathTaperX = this.PathTaperX * 0.01f;
  1176. prim.PrimData.PathTaperY = this.PathTaperY * 0.01f;
  1177. prim.PrimData.PathTwistBegin = this.PathTwistBegin * 0.01f;
  1178. prim.PrimData.PathTwist = this.PathTwist * 0.01f;
  1179. prim.PrimData.ProfileBegin = (float)this.ProfileBegin * 2.0e-5f;
  1180. prim.PrimData.ProfileEnd = 1.0f - (float)this.ProfileEnd * 2.0e-5f;
  1181. prim.PrimData.ProfileHollow = (float)this.ProfileHollow * 2.0e-5f;
  1182. prim.PrimData.profileCurve = this.ProfileCurve;
  1183. prim.PrimData.ProfileHole = (HoleType)this.HollowShape;
  1184. prim.PrimData.PathCurve = (PathCurve)this.PathCurve;
  1185. prim.PrimData.PathRadiusOffset = 0.01f * this.PathRadiusOffset;
  1186. prim.PrimData.PathRevolutions = 1.0f + 0.015f * this.PathRevolutions;
  1187. prim.PrimData.PathSkew = 0.01f * this.PathSkew;
  1188. prim.PrimData.PCode = OpenMetaverse.PCode.Prim;
  1189. prim.PrimData.State = 0;
  1190. if (this.FlexiEntry)
  1191. {
  1192. prim.Flexible = new Primitive.FlexibleData();
  1193. prim.Flexible.Drag = this.FlexiDrag;
  1194. prim.Flexible.Force = new Vector3(this.FlexiForceX, this.FlexiForceY, this.FlexiForceZ);
  1195. prim.Flexible.Gravity = this.FlexiGravity;
  1196. prim.Flexible.Softness = this.FlexiSoftness;
  1197. prim.Flexible.Tension = this.FlexiTension;
  1198. prim.Flexible.Wind = this.FlexiWind;
  1199. }
  1200. if (this.LightEntry)
  1201. {
  1202. prim.Light = new Primitive.LightData();
  1203. prim.Light.Color = new Color4(this.LightColorR, this.LightColorG, this.LightColorB, this.LightColorA);
  1204. prim.Light.Cutoff = this.LightCutoff;
  1205. prim.Light.Falloff = this.LightFalloff;
  1206. prim.Light.Intensity = this.LightIntensity;
  1207. prim.Light.Radius = this.LightRadius;
  1208. }
  1209. prim.Textures = this.Textures;
  1210. prim.Properties = new Primitive.ObjectProperties();
  1211. prim.Properties.Name = "Primitive";
  1212. prim.Properties.Description = "";
  1213. prim.Properties.CreatorID = UUID.Zero;
  1214. prim.Properties.GroupID = UUID.Zero;
  1215. prim.Properties.OwnerID = UUID.Zero;
  1216. prim.Properties.Permissions = new Permissions();
  1217. prim.Properties.SalePrice = 10;
  1218. prim.Properties.SaleType = new SaleType();
  1219. return prim;
  1220. }
  1221. /// <summary>
  1222. /// Encapsulates a list of media entries.
  1223. /// </summary>
  1224. /// This class is necessary because we want to replace auto-serialization of MediaEntry with something more
  1225. /// OSD like and less vulnerable to change.
  1226. public class MediaList : List<MediaEntry>, IXmlSerializable
  1227. {
  1228. public const string MEDIA_TEXTURE_TYPE = "sl";
  1229. public MediaList() : base() {}
  1230. public MediaList(IEnumerable<MediaEntry> collection) : base(collection) {}
  1231. public MediaList(int capacity) : base(capacity) {}
  1232. public XmlSchema GetSchema()
  1233. {
  1234. return null;
  1235. }
  1236. public string ToXml()
  1237. {
  1238. lock (this)
  1239. {
  1240. using (StringWriter sw = new StringWriter())
  1241. {
  1242. using (XmlTextWriter xtw = new XmlTextWriter(sw))
  1243. {
  1244. xtw.WriteStartElement("OSMedia");
  1245. xtw.WriteAttributeString("type", MEDIA_TEXTURE_TYPE);
  1246. xtw.WriteAttributeString("version", "0.1");
  1247. OSDArray meArray = new OSDArray();
  1248. foreach (MediaEntry me in this)
  1249. {
  1250. OSD osd = (null == me ? new OSD() : me.GetOSD());
  1251. meArray.Add(osd);
  1252. }
  1253. xtw.WriteStartElement("OSData");
  1254. xtw.WriteRaw(OSDParser.SerializeLLSDXmlString(meArray));
  1255. xtw.WriteEndElement();
  1256. xtw.WriteEndElement();
  1257. xtw.Flush();
  1258. return sw.ToString();
  1259. }
  1260. }
  1261. }
  1262. }
  1263. public void WriteXml(XmlWriter writer)
  1264. {
  1265. writer.WriteRaw(ToXml());
  1266. }
  1267. public static MediaList FromXml(string rawXml)
  1268. {
  1269. MediaList ml = new MediaList();
  1270. ml.ReadXml(rawXml);
  1271. return ml;
  1272. }
  1273. public void ReadXml(string rawXml)
  1274. {
  1275. using (StringReader sr = new StringReader(rawXml))
  1276. {
  1277. using (XmlTextReader xtr = new XmlTextReader(sr))
  1278. {
  1279. xtr.MoveToContent();
  1280. string type = xtr.GetAttribute("type");
  1281. //m_log.DebugFormat("[MOAP]: Loaded media texture entry with type {0}", type);
  1282. if (type != MEDIA_TEXTURE_TYPE)
  1283. return;
  1284. xtr.ReadStartElement("OSMedia");
  1285. OSDArray osdMeArray = (OSDArray)OSDParser.DeserializeLLSDXml(xtr.ReadInnerXml());
  1286. foreach (OSD osdMe in osdMeArray)
  1287. {
  1288. MediaEntry me = (osdMe is OSDMap ? MediaEntry.FromOSD(osdMe) : new MediaEntry());
  1289. Add(me);
  1290. }
  1291. xtr.ReadEndElement();
  1292. }
  1293. }
  1294. }
  1295. public void ReadXml(XmlReader reader)
  1296. {
  1297. if (reader.IsEmptyElement)
  1298. return;
  1299. ReadXml(reader.ReadInnerXml());
  1300. }
  1301. }
  1302. }
  1303. }