PrimitiveBaseShape.cs 46 KB

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