PrimitiveBaseShape.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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. public byte[] SculptData
  541. {
  542. get
  543. {
  544. return _sculptData;
  545. }
  546. set
  547. {
  548. // m_log.DebugFormat("[PRIMITIVE BASE SHAPE]: Setting SculptData to data with length {0}", value.Length);
  549. _sculptData = value;
  550. }
  551. }
  552. public int FlexiSoftness
  553. {
  554. get
  555. {
  556. return _flexiSoftness;
  557. }
  558. set
  559. {
  560. _flexiSoftness = value;
  561. }
  562. }
  563. public float FlexiTension {
  564. get {
  565. return _flexiTension;
  566. }
  567. set {
  568. _flexiTension = value;
  569. }
  570. }
  571. public float FlexiDrag {
  572. get {
  573. return _flexiDrag;
  574. }
  575. set {
  576. _flexiDrag = value;
  577. }
  578. }
  579. public float FlexiGravity {
  580. get {
  581. return _flexiGravity;
  582. }
  583. set {
  584. _flexiGravity = value;
  585. }
  586. }
  587. public float FlexiWind {
  588. get {
  589. return _flexiWind;
  590. }
  591. set {
  592. _flexiWind = value;
  593. }
  594. }
  595. public float FlexiForceX {
  596. get {
  597. return _flexiForceX;
  598. }
  599. set {
  600. _flexiForceX = value;
  601. }
  602. }
  603. public float FlexiForceY {
  604. get {
  605. return _flexiForceY;
  606. }
  607. set {
  608. _flexiForceY = value;
  609. }
  610. }
  611. public float FlexiForceZ {
  612. get {
  613. return _flexiForceZ;
  614. }
  615. set {
  616. _flexiForceZ = value;
  617. }
  618. }
  619. public float LightColorR {
  620. get {
  621. return _lightColorR;
  622. }
  623. set {
  624. _lightColorR = value;
  625. }
  626. }
  627. public float LightColorG {
  628. get {
  629. return _lightColorG;
  630. }
  631. set {
  632. _lightColorG = value;
  633. }
  634. }
  635. public float LightColorB {
  636. get {
  637. return _lightColorB;
  638. }
  639. set {
  640. _lightColorB = value;
  641. }
  642. }
  643. public float LightColorA {
  644. get {
  645. return _lightColorA;
  646. }
  647. set {
  648. _lightColorA = value;
  649. }
  650. }
  651. public float LightRadius {
  652. get {
  653. return _lightRadius;
  654. }
  655. set {
  656. _lightRadius = value;
  657. }
  658. }
  659. public float LightCutoff {
  660. get {
  661. return _lightCutoff;
  662. }
  663. set {
  664. _lightCutoff = value;
  665. }
  666. }
  667. public float LightFalloff {
  668. get {
  669. return _lightFalloff;
  670. }
  671. set {
  672. _lightFalloff = value;
  673. }
  674. }
  675. public float LightIntensity {
  676. get {
  677. return _lightIntensity;
  678. }
  679. set {
  680. _lightIntensity = value;
  681. }
  682. }
  683. public bool FlexiEntry {
  684. get {
  685. return _flexiEntry;
  686. }
  687. set {
  688. _flexiEntry = value;
  689. }
  690. }
  691. public bool LightEntry {
  692. get {
  693. return _lightEntry;
  694. }
  695. set {
  696. _lightEntry = value;
  697. }
  698. }
  699. public bool SculptEntry {
  700. get {
  701. return _sculptEntry;
  702. }
  703. set {
  704. _sculptEntry = value;
  705. }
  706. }
  707. public bool ProjectionEntry {
  708. get {
  709. return _projectionEntry;
  710. }
  711. set {
  712. _projectionEntry = value;
  713. }
  714. }
  715. public UUID ProjectionTextureUUID {
  716. get {
  717. return _projectionTextureID;
  718. }
  719. set {
  720. _projectionTextureID = value;
  721. }
  722. }
  723. public float ProjectionFOV {
  724. get {
  725. return _projectionFOV;
  726. }
  727. set {
  728. _projectionFOV = value;
  729. }
  730. }
  731. public float ProjectionFocus {
  732. get {
  733. return _projectionFocus;
  734. }
  735. set {
  736. _projectionFocus = value;
  737. }
  738. }
  739. public float ProjectionAmbiance {
  740. get {
  741. return _projectionAmb;
  742. }
  743. set {
  744. _projectionAmb = value;
  745. }
  746. }
  747. public ulong GetMeshKey(Vector3 size, float lod)
  748. {
  749. ulong hash = 5381;
  750. hash = djb2(hash, this.PathCurve);
  751. hash = djb2(hash, (byte)((byte)this.HollowShape | (byte)this.ProfileShape));
  752. hash = djb2(hash, this.PathBegin);
  753. hash = djb2(hash, this.PathEnd);
  754. hash = djb2(hash, this.PathScaleX);
  755. hash = djb2(hash, this.PathScaleY);
  756. hash = djb2(hash, this.PathShearX);
  757. hash = djb2(hash, this.PathShearY);
  758. hash = djb2(hash, (byte)this.PathTwist);
  759. hash = djb2(hash, (byte)this.PathTwistBegin);
  760. hash = djb2(hash, (byte)this.PathRadiusOffset);
  761. hash = djb2(hash, (byte)this.PathTaperX);
  762. hash = djb2(hash, (byte)this.PathTaperY);
  763. hash = djb2(hash, this.PathRevolutions);
  764. hash = djb2(hash, (byte)this.PathSkew);
  765. hash = djb2(hash, this.ProfileBegin);
  766. hash = djb2(hash, this.ProfileEnd);
  767. hash = djb2(hash, this.ProfileHollow);
  768. // TODO: Separate scale out from the primitive shape data (after
  769. // scaling is supported at the physics engine level)
  770. byte[] scaleBytes = size.GetBytes();
  771. for (int i = 0; i < scaleBytes.Length; i++)
  772. hash = djb2(hash, scaleBytes[i]);
  773. // Include LOD in hash, accounting for endianness
  774. byte[] lodBytes = new byte[4];
  775. Buffer.BlockCopy(BitConverter.GetBytes(lod), 0, lodBytes, 0, 4);
  776. if (!BitConverter.IsLittleEndian)
  777. {
  778. Array.Reverse(lodBytes, 0, 4);
  779. }
  780. for (int i = 0; i < lodBytes.Length; i++)
  781. hash = djb2(hash, lodBytes[i]);
  782. // include sculpt UUID
  783. if (this.SculptEntry)
  784. {
  785. scaleBytes = this.SculptTexture.GetBytes();
  786. for (int i = 0; i < scaleBytes.Length; i++)
  787. hash = djb2(hash, scaleBytes[i]);
  788. }
  789. return hash;
  790. }
  791. private ulong djb2(ulong hash, byte c)
  792. {
  793. return ((hash << 5) + hash) + (ulong)c;
  794. }
  795. private ulong djb2(ulong hash, ushort c)
  796. {
  797. hash = ((hash << 5) + hash) + (ulong)((byte)c);
  798. return ((hash << 5) + hash) + (ulong)(c >> 8);
  799. }
  800. public byte[] ExtraParamsToBytes()
  801. {
  802. // m_log.DebugFormat("[EXTRAPARAMS]: Called ExtraParamsToBytes()");
  803. ushort FlexiEP = 0x10;
  804. ushort LightEP = 0x20;
  805. ushort SculptEP = 0x30;
  806. ushort ProjectionEP = 0x40;
  807. int i = 0;
  808. uint TotalBytesLength = 1; // ExtraParamsNum
  809. uint ExtraParamsNum = 0;
  810. if (_flexiEntry)
  811. {
  812. ExtraParamsNum++;
  813. TotalBytesLength += 16;// data
  814. TotalBytesLength += 2 + 4; // type
  815. }
  816. if (_lightEntry)
  817. {
  818. ExtraParamsNum++;
  819. TotalBytesLength += 16;// data
  820. TotalBytesLength += 2 + 4; // type
  821. }
  822. if (_sculptEntry)
  823. {
  824. ExtraParamsNum++;
  825. TotalBytesLength += 17;// data
  826. TotalBytesLength += 2 + 4; // type
  827. }
  828. if (_projectionEntry)
  829. {
  830. ExtraParamsNum++;
  831. TotalBytesLength += 28;// data
  832. TotalBytesLength += 2 + 4;// type
  833. }
  834. byte[] returnbytes = new byte[TotalBytesLength];
  835. // uint paramlength = ExtraParamsNum;
  836. // Stick in the number of parameters
  837. returnbytes[i++] = (byte)ExtraParamsNum;
  838. if (_flexiEntry)
  839. {
  840. byte[] FlexiData = GetFlexiBytes();
  841. returnbytes[i++] = (byte)(FlexiEP % 256);
  842. returnbytes[i++] = (byte)((FlexiEP >> 8) % 256);
  843. returnbytes[i++] = (byte)(FlexiData.Length % 256);
  844. returnbytes[i++] = (byte)((FlexiData.Length >> 8) % 256);
  845. returnbytes[i++] = (byte)((FlexiData.Length >> 16) % 256);
  846. returnbytes[i++] = (byte)((FlexiData.Length >> 24) % 256);
  847. Array.Copy(FlexiData, 0, returnbytes, i, FlexiData.Length);
  848. i += FlexiData.Length;
  849. }
  850. if (_lightEntry)
  851. {
  852. byte[] LightData = GetLightBytes();
  853. returnbytes[i++] = (byte)(LightEP % 256);
  854. returnbytes[i++] = (byte)((LightEP >> 8) % 256);
  855. returnbytes[i++] = (byte)(LightData.Length % 256);
  856. returnbytes[i++] = (byte)((LightData.Length >> 8) % 256);
  857. returnbytes[i++] = (byte)((LightData.Length >> 16) % 256);
  858. returnbytes[i++] = (byte)((LightData.Length >> 24) % 256);
  859. Array.Copy(LightData, 0, returnbytes, i, LightData.Length);
  860. i += LightData.Length;
  861. }
  862. if (_sculptEntry)
  863. {
  864. byte[] SculptData = GetSculptBytes();
  865. returnbytes[i++] = (byte)(SculptEP % 256);
  866. returnbytes[i++] = (byte)((SculptEP >> 8) % 256);
  867. returnbytes[i++] = (byte)(SculptData.Length % 256);
  868. returnbytes[i++] = (byte)((SculptData.Length >> 8) % 256);
  869. returnbytes[i++] = (byte)((SculptData.Length >> 16) % 256);
  870. returnbytes[i++] = (byte)((SculptData.Length >> 24) % 256);
  871. Array.Copy(SculptData, 0, returnbytes, i, SculptData.Length);
  872. i += SculptData.Length;
  873. }
  874. if (_projectionEntry)
  875. {
  876. byte[] ProjectionData = GetProjectionBytes();
  877. returnbytes[i++] = (byte)(ProjectionEP % 256);
  878. returnbytes[i++] = (byte)((ProjectionEP >> 8) % 256);
  879. returnbytes[i++] = (byte)((ProjectionData.Length) % 256);
  880. returnbytes[i++] = (byte)((ProjectionData.Length >> 16) % 256);
  881. returnbytes[i++] = (byte)((ProjectionData.Length >> 20) % 256);
  882. returnbytes[i++] = (byte)((ProjectionData.Length >> 24) % 256);
  883. Array.Copy(ProjectionData, 0, returnbytes, i, ProjectionData.Length);
  884. i += ProjectionData.Length;
  885. }
  886. if (!_flexiEntry && !_lightEntry && !_sculptEntry && !_projectionEntry)
  887. {
  888. byte[] returnbyte = new byte[1];
  889. returnbyte[0] = 0;
  890. return returnbyte;
  891. }
  892. return returnbytes;
  893. }
  894. public void ReadInUpdateExtraParam(ushort type, bool inUse, byte[] data)
  895. {
  896. const ushort FlexiEP = 0x10;
  897. const ushort LightEP = 0x20;
  898. const ushort SculptEP = 0x30;
  899. const ushort ProjectionEP = 0x40;
  900. switch (type)
  901. {
  902. case FlexiEP:
  903. if (!inUse)
  904. {
  905. _flexiEntry = false;
  906. return;
  907. }
  908. ReadFlexiData(data, 0);
  909. break;
  910. case LightEP:
  911. if (!inUse)
  912. {
  913. _lightEntry = false;
  914. return;
  915. }
  916. ReadLightData(data, 0);
  917. break;
  918. case SculptEP:
  919. if (!inUse)
  920. {
  921. _sculptEntry = false;
  922. return;
  923. }
  924. ReadSculptData(data, 0);
  925. break;
  926. case ProjectionEP:
  927. if (!inUse)
  928. {
  929. _projectionEntry = false;
  930. return;
  931. }
  932. ReadProjectionData(data, 0);
  933. break;
  934. }
  935. }
  936. public void ReadInExtraParamsBytes(byte[] data)
  937. {
  938. if (data == null || data.Length == 1)
  939. return;
  940. const ushort FlexiEP = 0x10;
  941. const ushort LightEP = 0x20;
  942. const ushort SculptEP = 0x30;
  943. const ushort ProjectionEP = 0x40;
  944. bool lGotFlexi = false;
  945. bool lGotLight = false;
  946. bool lGotSculpt = false;
  947. bool lGotFilter = false;
  948. int i = 0;
  949. byte extraParamCount = 0;
  950. if (data.Length > 0)
  951. {
  952. extraParamCount = data[i++];
  953. }
  954. for (int k = 0; k < extraParamCount; k++)
  955. {
  956. ushort epType = Utils.BytesToUInt16(data, i);
  957. i += 2;
  958. // uint paramLength = Helpers.BytesToUIntBig(data, i);
  959. i += 4;
  960. switch (epType)
  961. {
  962. case FlexiEP:
  963. ReadFlexiData(data, i);
  964. i += 16;
  965. lGotFlexi = true;
  966. break;
  967. case LightEP:
  968. ReadLightData(data, i);
  969. i += 16;
  970. lGotLight = true;
  971. break;
  972. case SculptEP:
  973. ReadSculptData(data, i);
  974. i += 17;
  975. lGotSculpt = true;
  976. break;
  977. case ProjectionEP:
  978. ReadProjectionData(data, i);
  979. i += 28;
  980. lGotFilter = true;
  981. break;
  982. }
  983. }
  984. if (!lGotFlexi)
  985. _flexiEntry = false;
  986. if (!lGotLight)
  987. _lightEntry = false;
  988. if (!lGotSculpt)
  989. _sculptEntry = false;
  990. if (!lGotFilter)
  991. _projectionEntry = false;
  992. }
  993. public void ReadSculptData(byte[] data, int pos)
  994. {
  995. byte[] SculptTextureUUID = new byte[16];
  996. UUID SculptUUID = UUID.Zero;
  997. byte SculptTypel = data[16+pos];
  998. if (data.Length+pos >= 17)
  999. {
  1000. _sculptEntry = true;
  1001. SculptTextureUUID = new byte[16];
  1002. SculptTypel = data[16 + pos];
  1003. Array.Copy(data, pos, SculptTextureUUID,0, 16);
  1004. SculptUUID = new UUID(SculptTextureUUID, 0);
  1005. }
  1006. else
  1007. {
  1008. _sculptEntry = false;
  1009. SculptUUID = UUID.Zero;
  1010. SculptTypel = 0x00;
  1011. }
  1012. if (_sculptEntry)
  1013. {
  1014. if (_sculptType != (byte)1 && _sculptType != (byte)2 && _sculptType != (byte)3 && _sculptType != (byte)4)
  1015. _sculptType = 4;
  1016. }
  1017. _sculptTexture = SculptUUID;
  1018. _sculptType = SculptTypel;
  1019. //m_log.Info("[SCULPT]:" + SculptUUID.ToString());
  1020. }
  1021. public byte[] GetSculptBytes()
  1022. {
  1023. byte[] data = new byte[17];
  1024. _sculptTexture.GetBytes().CopyTo(data, 0);
  1025. data[16] = (byte)_sculptType;
  1026. return data;
  1027. }
  1028. public void ReadFlexiData(byte[] data, int pos)
  1029. {
  1030. if (data.Length-pos >= 16)
  1031. {
  1032. _flexiEntry = true;
  1033. _flexiSoftness = ((data[pos] & 0x80) >> 6) | ((data[pos + 1] & 0x80) >> 7);
  1034. _flexiTension = (float)(data[pos++] & 0x7F) / 10.0f;
  1035. _flexiDrag = (float)(data[pos++] & 0x7F) / 10.0f;
  1036. _flexiGravity = (float)(data[pos++] / 10.0f) - 10.0f;
  1037. _flexiWind = (float)data[pos++] / 10.0f;
  1038. Vector3 lForce = new Vector3(data, pos);
  1039. _flexiForceX = lForce.X;
  1040. _flexiForceY = lForce.Y;
  1041. _flexiForceZ = lForce.Z;
  1042. }
  1043. else
  1044. {
  1045. _flexiEntry = false;
  1046. _flexiSoftness = 0;
  1047. _flexiTension = 0.0f;
  1048. _flexiDrag = 0.0f;
  1049. _flexiGravity = 0.0f;
  1050. _flexiWind = 0.0f;
  1051. _flexiForceX = 0f;
  1052. _flexiForceY = 0f;
  1053. _flexiForceZ = 0f;
  1054. }
  1055. }
  1056. public byte[] GetFlexiBytes()
  1057. {
  1058. byte[] data = new byte[16];
  1059. int i = 0;
  1060. // Softness is packed in the upper bits of tension and drag
  1061. data[i] = (byte)((_flexiSoftness & 2) << 6);
  1062. data[i + 1] = (byte)((_flexiSoftness & 1) << 7);
  1063. data[i++] |= (byte)((byte)(_flexiTension * 10.01f) & 0x7F);
  1064. data[i++] |= (byte)((byte)(_flexiDrag * 10.01f) & 0x7F);
  1065. data[i++] = (byte)((_flexiGravity + 10.0f) * 10.01f);
  1066. data[i++] = (byte)(_flexiWind * 10.01f);
  1067. Vector3 lForce = new Vector3(_flexiForceX, _flexiForceY, _flexiForceZ);
  1068. lForce.GetBytes().CopyTo(data, i);
  1069. return data;
  1070. }
  1071. public void ReadLightData(byte[] data, int pos)
  1072. {
  1073. if (data.Length - pos >= 16)
  1074. {
  1075. _lightEntry = true;
  1076. Color4 lColor = new Color4(data, pos, false);
  1077. _lightIntensity = lColor.A;
  1078. _lightColorA = 1f;
  1079. _lightColorR = lColor.R;
  1080. _lightColorG = lColor.G;
  1081. _lightColorB = lColor.B;
  1082. _lightRadius = Utils.BytesToFloat(data, pos + 4);
  1083. _lightCutoff = Utils.BytesToFloat(data, pos + 8);
  1084. _lightFalloff = Utils.BytesToFloat(data, pos + 12);
  1085. }
  1086. else
  1087. {
  1088. _lightEntry = false;
  1089. _lightColorA = 1f;
  1090. _lightColorR = 0f;
  1091. _lightColorG = 0f;
  1092. _lightColorB = 0f;
  1093. _lightRadius = 0f;
  1094. _lightCutoff = 0f;
  1095. _lightFalloff = 0f;
  1096. _lightIntensity = 0f;
  1097. }
  1098. }
  1099. public byte[] GetLightBytes()
  1100. {
  1101. byte[] data = new byte[16];
  1102. // Alpha channel in color is intensity
  1103. Color4 tmpColor = new Color4(_lightColorR,_lightColorG,_lightColorB,_lightIntensity);
  1104. tmpColor.GetBytes().CopyTo(data, 0);
  1105. Utils.FloatToBytes(_lightRadius).CopyTo(data, 4);
  1106. Utils.FloatToBytes(_lightCutoff).CopyTo(data, 8);
  1107. Utils.FloatToBytes(_lightFalloff).CopyTo(data, 12);
  1108. return data;
  1109. }
  1110. public void ReadProjectionData(byte[] data, int pos)
  1111. {
  1112. byte[] ProjectionTextureUUID = new byte[16];
  1113. if (data.Length - pos >= 28)
  1114. {
  1115. _projectionEntry = true;
  1116. Array.Copy(data, pos, ProjectionTextureUUID,0, 16);
  1117. _projectionTextureID = new UUID(ProjectionTextureUUID, 0);
  1118. _projectionFOV = Utils.BytesToFloat(data, pos + 16);
  1119. _projectionFocus = Utils.BytesToFloat(data, pos + 20);
  1120. _projectionAmb = Utils.BytesToFloat(data, pos + 24);
  1121. }
  1122. else
  1123. {
  1124. _projectionEntry = false;
  1125. _projectionTextureID = UUID.Zero;
  1126. _projectionFOV = 0f;
  1127. _projectionFocus = 0f;
  1128. _projectionAmb = 0f;
  1129. }
  1130. }
  1131. public byte[] GetProjectionBytes()
  1132. {
  1133. byte[] data = new byte[28];
  1134. _projectionTextureID.GetBytes().CopyTo(data, 0);
  1135. Utils.FloatToBytes(_projectionFOV).CopyTo(data, 16);
  1136. Utils.FloatToBytes(_projectionFocus).CopyTo(data, 20);
  1137. Utils.FloatToBytes(_projectionAmb).CopyTo(data, 24);
  1138. return data;
  1139. }
  1140. /// <summary>
  1141. /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
  1142. /// </summary>
  1143. /// <returns></returns>
  1144. public Primitive ToOmvPrimitive()
  1145. {
  1146. // position and rotation defaults here since they are not available in PrimitiveBaseShape
  1147. return ToOmvPrimitive(new Vector3(0.0f, 0.0f, 0.0f),
  1148. new Quaternion(0.0f, 0.0f, 0.0f, 1.0f));
  1149. }
  1150. /// <summary>
  1151. /// Creates a OpenMetaverse.Primitive and populates it with converted PrimitiveBaseShape values
  1152. /// </summary>
  1153. /// <param name="position"></param>
  1154. /// <param name="rotation"></param>
  1155. /// <returns></returns>
  1156. public Primitive ToOmvPrimitive(Vector3 position, Quaternion rotation)
  1157. {
  1158. OpenMetaverse.Primitive prim = new OpenMetaverse.Primitive();
  1159. prim.Scale = this.Scale;
  1160. prim.Position = position;
  1161. prim.Rotation = rotation;
  1162. if (this.SculptEntry)
  1163. {
  1164. prim.Sculpt = new Primitive.SculptData();
  1165. prim.Sculpt.Type = (OpenMetaverse.SculptType)this.SculptType;
  1166. prim.Sculpt.SculptTexture = this.SculptTexture;
  1167. }
  1168. prim.PrimData.PathShearX = this.PathShearX < 128 ? (float)this.PathShearX * 0.01f : (float)(this.PathShearX - 256) * 0.01f;
  1169. prim.PrimData.PathShearY = this.PathShearY < 128 ? (float)this.PathShearY * 0.01f : (float)(this.PathShearY - 256) * 0.01f;
  1170. prim.PrimData.PathBegin = (float)this.PathBegin * 2.0e-5f;
  1171. prim.PrimData.PathEnd = 1.0f - (float)this.PathEnd * 2.0e-5f;
  1172. prim.PrimData.PathScaleX = (200 - this.PathScaleX) * 0.01f;
  1173. prim.PrimData.PathScaleY = (200 - this.PathScaleY) * 0.01f;
  1174. prim.PrimData.PathTaperX = this.PathTaperX * 0.01f;
  1175. prim.PrimData.PathTaperY = this.PathTaperY * 0.01f;
  1176. prim.PrimData.PathTwistBegin = this.PathTwistBegin * 0.01f;
  1177. prim.PrimData.PathTwist = this.PathTwist * 0.01f;
  1178. prim.PrimData.ProfileBegin = (float)this.ProfileBegin * 2.0e-5f;
  1179. prim.PrimData.ProfileEnd = 1.0f - (float)this.ProfileEnd * 2.0e-5f;
  1180. prim.PrimData.ProfileHollow = (float)this.ProfileHollow * 2.0e-5f;
  1181. prim.PrimData.profileCurve = this.ProfileCurve;
  1182. prim.PrimData.ProfileHole = (HoleType)this.HollowShape;
  1183. prim.PrimData.PathCurve = (PathCurve)this.PathCurve;
  1184. prim.PrimData.PathRadiusOffset = 0.01f * this.PathRadiusOffset;
  1185. prim.PrimData.PathRevolutions = 1.0f + 0.015f * this.PathRevolutions;
  1186. prim.PrimData.PathSkew = 0.01f * this.PathSkew;
  1187. prim.PrimData.PCode = OpenMetaverse.PCode.Prim;
  1188. prim.PrimData.State = 0;
  1189. if (this.FlexiEntry)
  1190. {
  1191. prim.Flexible = new Primitive.FlexibleData();
  1192. prim.Flexible.Drag = this.FlexiDrag;
  1193. prim.Flexible.Force = new Vector3(this.FlexiForceX, this.FlexiForceY, this.FlexiForceZ);
  1194. prim.Flexible.Gravity = this.FlexiGravity;
  1195. prim.Flexible.Softness = this.FlexiSoftness;
  1196. prim.Flexible.Tension = this.FlexiTension;
  1197. prim.Flexible.Wind = this.FlexiWind;
  1198. }
  1199. if (this.LightEntry)
  1200. {
  1201. prim.Light = new Primitive.LightData();
  1202. prim.Light.Color = new Color4(this.LightColorR, this.LightColorG, this.LightColorB, this.LightColorA);
  1203. prim.Light.Cutoff = this.LightCutoff;
  1204. prim.Light.Falloff = this.LightFalloff;
  1205. prim.Light.Intensity = this.LightIntensity;
  1206. prim.Light.Radius = this.LightRadius;
  1207. }
  1208. prim.Textures = this.Textures;
  1209. prim.Properties = new Primitive.ObjectProperties();
  1210. prim.Properties.Name = "Primitive";
  1211. prim.Properties.Description = "";
  1212. prim.Properties.CreatorID = UUID.Zero;
  1213. prim.Properties.GroupID = UUID.Zero;
  1214. prim.Properties.OwnerID = UUID.Zero;
  1215. prim.Properties.Permissions = new Permissions();
  1216. prim.Properties.SalePrice = 10;
  1217. prim.Properties.SaleType = new SaleType();
  1218. return prim;
  1219. }
  1220. /// <summary>
  1221. /// Encapsulates a list of media entries.
  1222. /// </summary>
  1223. /// This class is necessary because we want to replace auto-serialization of MediaEntry with something more
  1224. /// OSD like and less vulnerable to change.
  1225. public class MediaList : List<MediaEntry>, IXmlSerializable
  1226. {
  1227. public const string MEDIA_TEXTURE_TYPE = "sl";
  1228. public MediaList() : base() {}
  1229. public MediaList(IEnumerable<MediaEntry> collection) : base(collection) {}
  1230. public MediaList(int capacity) : base(capacity) {}
  1231. public XmlSchema GetSchema()
  1232. {
  1233. return null;
  1234. }
  1235. public string ToXml()
  1236. {
  1237. lock (this)
  1238. {
  1239. using (StringWriter sw = new StringWriter())
  1240. {
  1241. using (XmlTextWriter xtw = new XmlTextWriter(sw))
  1242. {
  1243. xtw.WriteStartElement("OSMedia");
  1244. xtw.WriteAttributeString("type", MEDIA_TEXTURE_TYPE);
  1245. xtw.WriteAttributeString("version", "0.1");
  1246. OSDArray meArray = new OSDArray();
  1247. foreach (MediaEntry me in this)
  1248. {
  1249. OSD osd = (null == me ? new OSD() : me.GetOSD());
  1250. meArray.Add(osd);
  1251. }
  1252. xtw.WriteStartElement("OSData");
  1253. xtw.WriteRaw(OSDParser.SerializeLLSDXmlString(meArray));
  1254. xtw.WriteEndElement();
  1255. xtw.WriteEndElement();
  1256. xtw.Flush();
  1257. return sw.ToString();
  1258. }
  1259. }
  1260. }
  1261. }
  1262. public void WriteXml(XmlWriter writer)
  1263. {
  1264. writer.WriteRaw(ToXml());
  1265. }
  1266. public static MediaList FromXml(string rawXml)
  1267. {
  1268. MediaList ml = new MediaList();
  1269. ml.ReadXml(rawXml);
  1270. return ml;
  1271. }
  1272. public void ReadXml(string rawXml)
  1273. {
  1274. using (StringReader sr = new StringReader(rawXml))
  1275. {
  1276. using (XmlTextReader xtr = new XmlTextReader(sr))
  1277. {
  1278. xtr.MoveToContent();
  1279. string type = xtr.GetAttribute("type");
  1280. //m_log.DebugFormat("[MOAP]: Loaded media texture entry with type {0}", type);
  1281. if (type != MEDIA_TEXTURE_TYPE)
  1282. return;
  1283. xtr.ReadStartElement("OSMedia");
  1284. OSDArray osdMeArray = (OSDArray)OSDParser.DeserializeLLSDXml(xtr.ReadInnerXml());
  1285. foreach (OSD osdMe in osdMeArray)
  1286. {
  1287. MediaEntry me = (osdMe is OSDMap ? MediaEntry.FromOSD(osdMe) : new MediaEntry());
  1288. Add(me);
  1289. }
  1290. xtr.ReadEndElement();
  1291. }
  1292. }
  1293. }
  1294. public void ReadXml(XmlReader reader)
  1295. {
  1296. if (reader.IsEmptyElement)
  1297. return;
  1298. ReadXml(reader.ReadInnerXml());
  1299. }
  1300. }
  1301. }
  1302. }