Primitive2.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using OpenSim.types;
  5. using libsecondlife;
  6. using libsecondlife.Packets;
  7. using OpenSim.Framework.Interfaces;
  8. using OpenSim.Physics.Manager;
  9. using OpenSim.Framework.Types;
  10. using OpenSim.Framework.Inventory;
  11. namespace OpenSim.world
  12. {
  13. public class Primitive2 : Entity
  14. {
  15. protected PrimData primData;
  16. //private ObjectUpdatePacket OurPacket;
  17. private LLVector3 positionLastFrame = new LLVector3(0, 0, 0);
  18. private Dictionary<uint, ClientView> m_clientThreads;
  19. private ulong m_regionHandle;
  20. private const uint FULL_MASK_PERMISSIONS = 2147483647;
  21. private bool physicsEnabled = false;
  22. private Dictionary<LLUUID, InventoryItem> inventoryItems;
  23. #region Properties
  24. public LLVector3 Scale
  25. {
  26. set
  27. {
  28. this.primData.Scale = value;
  29. //this.dirtyFlag = true;
  30. }
  31. get
  32. {
  33. return this.primData.Scale;
  34. }
  35. }
  36. public PhysicsActor PhysActor
  37. {
  38. set
  39. {
  40. this._physActor = value;
  41. }
  42. }
  43. public override LLVector3 Pos
  44. {
  45. get
  46. {
  47. return base.Pos;
  48. }
  49. set
  50. {
  51. base.Pos = value;
  52. }
  53. }
  54. #endregion
  55. public Primitive2(Dictionary<uint, ClientView> clientThreads, ulong regionHandle, World world)
  56. {
  57. m_clientThreads = clientThreads;
  58. m_regionHandle = regionHandle;
  59. m_world = world;
  60. inventoryItems = new Dictionary<LLUUID, InventoryItem>();
  61. }
  62. public Primitive2(Dictionary<uint, ClientView> clientThreads, ulong regionHandle, World world, LLUUID owner)
  63. {
  64. m_clientThreads = clientThreads;
  65. m_regionHandle = regionHandle;
  66. m_world = world;
  67. inventoryItems = new Dictionary<LLUUID, InventoryItem>();
  68. this.primData = new PrimData();
  69. this.primData.CreationDate = (Int32)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
  70. this.primData.OwnerID = owner;
  71. }
  72. public byte[] GetByteArray()
  73. {
  74. byte[] result = null;
  75. List<byte[]> dataArrays = new List<byte[]>();
  76. dataArrays.Add(primData.ToBytes());
  77. foreach (Entity child in children)
  78. {
  79. if (child is OpenSim.world.Primitive2)
  80. {
  81. dataArrays.Add(((OpenSim.world.Primitive2)child).GetByteArray());
  82. }
  83. }
  84. byte[] primstart = Helpers.StringToField("<Prim>");
  85. byte[] primend = Helpers.StringToField("</Prim>");
  86. int totalLength = primstart.Length + primend.Length;
  87. for (int i = 0; i < dataArrays.Count; i++)
  88. {
  89. totalLength += dataArrays[i].Length;
  90. }
  91. result = new byte[totalLength];
  92. int arraypos = 0;
  93. Array.Copy(primstart, 0, result, 0, primstart.Length);
  94. arraypos += primstart.Length;
  95. for (int i = 0; i < dataArrays.Count; i++)
  96. {
  97. Array.Copy(dataArrays[i], 0, result, arraypos, dataArrays[i].Length);
  98. arraypos += dataArrays[i].Length;
  99. }
  100. Array.Copy(primend, 0, result, arraypos, primend.Length);
  101. return result;
  102. }
  103. #region Overridden Methods
  104. public override void update()
  105. {
  106. LLVector3 pos2 = new LLVector3(0, 0, 0);
  107. }
  108. public override void BackUp()
  109. {
  110. }
  111. #endregion
  112. #region Packet handlers
  113. public void UpdatePosition(LLVector3 pos)
  114. {
  115. }
  116. public void UpdateShape(ObjectShapePacket.ObjectDataBlock addPacket)
  117. {
  118. this.primData.PathBegin = addPacket.PathBegin;
  119. this.primData.PathEnd = addPacket.PathEnd;
  120. this.primData.PathScaleX = addPacket.PathScaleX;
  121. this.primData.PathScaleY = addPacket.PathScaleY;
  122. this.primData.PathShearX = addPacket.PathShearX;
  123. this.primData.PathShearY = addPacket.PathShearY;
  124. this.primData.PathSkew = addPacket.PathSkew;
  125. this.primData.ProfileBegin = addPacket.ProfileBegin;
  126. this.primData.ProfileEnd = addPacket.ProfileEnd;
  127. this.primData.PathCurve = addPacket.PathCurve;
  128. this.primData.ProfileCurve = addPacket.ProfileCurve;
  129. this.primData.ProfileHollow = addPacket.ProfileHollow;
  130. this.primData.PathRadiusOffset = addPacket.PathRadiusOffset;
  131. this.primData.PathRevolutions = addPacket.PathRevolutions;
  132. this.primData.PathTaperX = addPacket.PathTaperX;
  133. this.primData.PathTaperY = addPacket.PathTaperY;
  134. this.primData.PathTwist = addPacket.PathTwist;
  135. this.primData.PathTwistBegin = addPacket.PathTwistBegin;
  136. }
  137. public void UpdateTexture(byte[] tex)
  138. {
  139. this.primData.Texture = tex;
  140. //this.dirtyFlag = true;
  141. }
  142. public void UpdateObjectFlags(ObjectFlagUpdatePacket pack)
  143. {
  144. }
  145. public void AssignToParent(Primitive prim)
  146. {
  147. }
  148. public void GetProperites(ClientView client)
  149. {
  150. ObjectPropertiesPacket proper = new ObjectPropertiesPacket();
  151. proper.ObjectData = new ObjectPropertiesPacket.ObjectDataBlock[1];
  152. proper.ObjectData[0] = new ObjectPropertiesPacket.ObjectDataBlock();
  153. proper.ObjectData[0].ItemID = LLUUID.Zero;
  154. proper.ObjectData[0].CreationDate = (ulong)this.primData.CreationDate;
  155. proper.ObjectData[0].CreatorID = this.primData.OwnerID;
  156. proper.ObjectData[0].FolderID = LLUUID.Zero;
  157. proper.ObjectData[0].FromTaskID = LLUUID.Zero;
  158. proper.ObjectData[0].GroupID = LLUUID.Zero;
  159. proper.ObjectData[0].InventorySerial = 0;
  160. proper.ObjectData[0].LastOwnerID = LLUUID.Zero;
  161. proper.ObjectData[0].ObjectID = this.uuid;
  162. proper.ObjectData[0].OwnerID = primData.OwnerID;
  163. proper.ObjectData[0].TouchName = new byte[0];
  164. proper.ObjectData[0].TextureID = new byte[0];
  165. proper.ObjectData[0].SitName = new byte[0];
  166. proper.ObjectData[0].Name = new byte[0];
  167. proper.ObjectData[0].Description = new byte[0];
  168. proper.ObjectData[0].OwnerMask = this.primData.OwnerMask;
  169. proper.ObjectData[0].NextOwnerMask = this.primData.NextOwnerMask;
  170. proper.ObjectData[0].GroupMask = this.primData.GroupMask;
  171. proper.ObjectData[0].EveryoneMask = this.primData.EveryoneMask;
  172. proper.ObjectData[0].BaseMask = this.primData.BaseMask;
  173. client.OutPacket(proper);
  174. }
  175. #endregion
  176. # region Inventory Methods
  177. public bool AddToInventory(InventoryItem item)
  178. {
  179. return false;
  180. }
  181. public InventoryItem RemoveFromInventory(LLUUID itemID)
  182. {
  183. return null;
  184. }
  185. public void RequestInventoryInfo(ClientView simClient, RequestTaskInventoryPacket packet)
  186. {
  187. }
  188. public void RequestXferInventory(ClientView simClient, ulong xferID)
  189. {
  190. //will only currently work if the total size of the inventory data array is under about 1000 bytes
  191. SendXferPacketPacket send = new SendXferPacketPacket();
  192. send.XferID.ID = xferID;
  193. send.XferID.Packet = 1 + 2147483648;
  194. send.DataPacket.Data = this.ConvertInventoryToBytes();
  195. simClient.OutPacket(send);
  196. }
  197. public byte[] ConvertInventoryToBytes()
  198. {
  199. System.Text.Encoding enc = System.Text.Encoding.ASCII;
  200. byte[] result = new byte[0];
  201. List<byte[]> inventoryData = new List<byte[]>();
  202. int totallength = 0;
  203. foreach (InventoryItem invItem in inventoryItems.Values)
  204. {
  205. byte[] data = enc.GetBytes(invItem.ExportString());
  206. inventoryData.Add(data);
  207. totallength += data.Length;
  208. }
  209. //TODO: copy arrays into the single result array
  210. return result;
  211. }
  212. public void CreateInventoryFromBytes(byte[] data)
  213. {
  214. }
  215. #endregion
  216. #region Update viewers Methods
  217. //should change these mehtods, so that outgoing packets are sent through the avatar class
  218. public void SendFullUpdateToClient(ClientView remoteClient)
  219. {
  220. LLVector3 lPos;
  221. if (this._physActor != null && this.physicsEnabled)
  222. {
  223. PhysicsVector pPos = this._physActor.Position;
  224. lPos = new LLVector3(pPos.X, pPos.Y, pPos.Z);
  225. }
  226. else
  227. {
  228. lPos = this.Pos;
  229. }
  230. ObjectUpdatePacket outPacket = new ObjectUpdatePacket();
  231. outPacket.ObjectData = new ObjectUpdatePacket.ObjectDataBlock[1];
  232. outPacket.ObjectData[0] = this.CreateUpdateBlock();
  233. byte[] pb = lPos.GetBytes();
  234. Array.Copy(pb, 0, outPacket.ObjectData[0].ObjectData, 0, pb.Length);
  235. remoteClient.OutPacket(outPacket);
  236. }
  237. public void SendFullUpdateToAllClients()
  238. {
  239. }
  240. public void SendTerseUpdateToClient(ClientView RemoteClient)
  241. {
  242. }
  243. public void SendTerseUpdateToALLClients()
  244. {
  245. }
  246. #endregion
  247. #region Create Methods
  248. public void CreateFromPacket(ObjectAddPacket addPacket, LLUUID ownerID, uint localID)
  249. {
  250. PrimData PData = new PrimData();
  251. this.primData = PData;
  252. this.primData.CreationDate = (Int32)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
  253. PData.OwnerID = ownerID;
  254. PData.PCode = addPacket.ObjectData.PCode;
  255. PData.PathBegin = addPacket.ObjectData.PathBegin;
  256. PData.PathEnd = addPacket.ObjectData.PathEnd;
  257. PData.PathScaleX = addPacket.ObjectData.PathScaleX;
  258. PData.PathScaleY = addPacket.ObjectData.PathScaleY;
  259. PData.PathShearX = addPacket.ObjectData.PathShearX;
  260. PData.PathShearY = addPacket.ObjectData.PathShearY;
  261. PData.PathSkew = addPacket.ObjectData.PathSkew;
  262. PData.ProfileBegin = addPacket.ObjectData.ProfileBegin;
  263. PData.ProfileEnd = addPacket.ObjectData.ProfileEnd;
  264. PData.Scale = addPacket.ObjectData.Scale;
  265. PData.PathCurve = addPacket.ObjectData.PathCurve;
  266. PData.ProfileCurve = addPacket.ObjectData.ProfileCurve;
  267. PData.ParentID = 0;
  268. PData.ProfileHollow = addPacket.ObjectData.ProfileHollow;
  269. PData.PathRadiusOffset = addPacket.ObjectData.PathRadiusOffset;
  270. PData.PathRevolutions = addPacket.ObjectData.PathRevolutions;
  271. PData.PathTaperX = addPacket.ObjectData.PathTaperX;
  272. PData.PathTaperY = addPacket.ObjectData.PathTaperY;
  273. PData.PathTwist = addPacket.ObjectData.PathTwist;
  274. PData.PathTwistBegin = addPacket.ObjectData.PathTwistBegin;
  275. LLVector3 pos1 = addPacket.ObjectData.RayEnd;
  276. this.primData.FullID = this.uuid = LLUUID.Random();
  277. this.localid = (uint)(localID);
  278. this.primData.Position = this.Pos = pos1;
  279. }
  280. public void CreateFromBytes(byte[] data)
  281. {
  282. }
  283. public void CreateFromPrimData(PrimData primData)
  284. {
  285. this.CreateFromPrimData(primData, primData.Position, primData.LocalID, false);
  286. }
  287. public void CreateFromPrimData(PrimData primData, LLVector3 posi, uint localID, bool newprim)
  288. {
  289. }
  290. #endregion
  291. #region Packet Update Methods
  292. protected void SetDefaultPacketValues(ObjectUpdatePacket.ObjectDataBlock objdata)
  293. {
  294. objdata.PSBlock = new byte[0];
  295. objdata.ExtraParams = new byte[1];
  296. objdata.MediaURL = new byte[0];
  297. objdata.NameValue = new byte[0];
  298. objdata.Text = new byte[0];
  299. objdata.TextColor = new byte[4];
  300. objdata.JointAxisOrAnchor = new LLVector3(0, 0, 0);
  301. objdata.JointPivot = new LLVector3(0, 0, 0);
  302. objdata.Material = 3;
  303. objdata.TextureAnim = new byte[0];
  304. objdata.Sound = LLUUID.Zero;
  305. LLObject.TextureEntry ntex = new LLObject.TextureEntry(new LLUUID("00000000-0000-0000-5005-000000000005"));
  306. this.primData.Texture = objdata.TextureEntry = ntex.ToBytes();
  307. objdata.State = 0;
  308. objdata.Data = new byte[0];
  309. objdata.ObjectData = new byte[60];
  310. objdata.ObjectData[46] = 128;
  311. objdata.ObjectData[47] = 63;
  312. }
  313. protected void SetPacketShapeData(ObjectUpdatePacket.ObjectDataBlock objectData)
  314. {
  315. objectData.OwnerID = this.primData.OwnerID;
  316. objectData.PCode = this.primData.PCode;
  317. objectData.PathBegin = this.primData.PathBegin;
  318. objectData.PathEnd = this.primData.PathEnd;
  319. objectData.PathScaleX = this.primData.PathScaleX;
  320. objectData.PathScaleY = this.primData.PathScaleY;
  321. objectData.PathShearX = this.primData.PathShearX;
  322. objectData.PathShearY = this.primData.PathShearY;
  323. objectData.PathSkew = this.primData.PathSkew;
  324. objectData.ProfileBegin = this.primData.ProfileBegin;
  325. objectData.ProfileEnd = this.primData.ProfileEnd;
  326. objectData.Scale = this.primData.Scale;
  327. objectData.PathCurve = this.primData.PathCurve;
  328. objectData.ProfileCurve = this.primData.ProfileCurve;
  329. objectData.ParentID = this.primData.ParentID;
  330. objectData.ProfileHollow = this.primData.ProfileHollow;
  331. objectData.PathRadiusOffset = this.primData.PathRadiusOffset;
  332. objectData.PathRevolutions = this.primData.PathRevolutions;
  333. objectData.PathTaperX = this.primData.PathTaperX;
  334. objectData.PathTaperY = this.primData.PathTaperY;
  335. objectData.PathTwist = this.primData.PathTwist;
  336. objectData.PathTwistBegin = this.primData.PathTwistBegin;
  337. }
  338. #endregion
  339. protected ObjectUpdatePacket.ObjectDataBlock CreateUpdateBlock()
  340. {
  341. ObjectUpdatePacket.ObjectDataBlock objupdate = new ObjectUpdatePacket.ObjectDataBlock();
  342. this.SetDefaultPacketValues(objupdate);
  343. objupdate.UpdateFlags = 32 + 65536 + 131072 + 256 + 4 + 8 + 2048 + 524288 + 268435456;
  344. this.SetPacketShapeData(objupdate);
  345. byte[] pb = this.Pos.GetBytes();
  346. Array.Copy(pb, 0, objupdate.ObjectData, 0, pb.Length);
  347. return objupdate;
  348. }
  349. protected ImprovedTerseObjectUpdatePacket.ObjectDataBlock CreateImprovedBlock()
  350. {
  351. uint ID = this.localid;
  352. byte[] bytes = new byte[60];
  353. int i = 0;
  354. ImprovedTerseObjectUpdatePacket.ObjectDataBlock dat = new ImprovedTerseObjectUpdatePacket.ObjectDataBlock();
  355. dat.TextureEntry = new byte[0];
  356. bytes[i++] = (byte)(ID % 256);
  357. bytes[i++] = (byte)((ID >> 8) % 256);
  358. bytes[i++] = (byte)((ID >> 16) % 256);
  359. bytes[i++] = (byte)((ID >> 24) % 256);
  360. bytes[i++] = 0;
  361. bytes[i++] = 0;
  362. LLVector3 lPos;
  363. Axiom.MathLib.Quaternion lRot;
  364. if (this._physActor != null && this.physicsEnabled)
  365. {
  366. PhysicsVector pPos = this._physActor.Position;
  367. lPos = new LLVector3(pPos.X, pPos.Y, pPos.Z);
  368. lRot = this._physActor.Orientation;
  369. }
  370. else
  371. {
  372. lPos = this.Pos;
  373. lRot = this.rotation;
  374. }
  375. byte[] pb = lPos.GetBytes();
  376. Array.Copy(pb, 0, bytes, i, pb.Length);
  377. i += 12;
  378. ushort ac = 32767;
  379. //vel
  380. bytes[i++] = (byte)(ac % 256);
  381. bytes[i++] = (byte)((ac >> 8) % 256);
  382. bytes[i++] = (byte)(ac % 256);
  383. bytes[i++] = (byte)((ac >> 8) % 256);
  384. bytes[i++] = (byte)(ac % 256);
  385. bytes[i++] = (byte)((ac >> 8) % 256);
  386. //accel
  387. bytes[i++] = (byte)(ac % 256);
  388. bytes[i++] = (byte)((ac >> 8) % 256);
  389. bytes[i++] = (byte)(ac % 256);
  390. bytes[i++] = (byte)((ac >> 8) % 256);
  391. bytes[i++] = (byte)(ac % 256);
  392. bytes[i++] = (byte)((ac >> 8) % 256);
  393. ushort rw, rx, ry, rz;
  394. rw = (ushort)(32768 * (lRot.w + 1));
  395. rx = (ushort)(32768 * (lRot.x + 1));
  396. ry = (ushort)(32768 * (lRot.y + 1));
  397. rz = (ushort)(32768 * (lRot.z + 1));
  398. //rot
  399. bytes[i++] = (byte)(rx % 256);
  400. bytes[i++] = (byte)((rx >> 8) % 256);
  401. bytes[i++] = (byte)(ry % 256);
  402. bytes[i++] = (byte)((ry >> 8) % 256);
  403. bytes[i++] = (byte)(rz % 256);
  404. bytes[i++] = (byte)((rz >> 8) % 256);
  405. bytes[i++] = (byte)(rw % 256);
  406. bytes[i++] = (byte)((rw >> 8) % 256);
  407. //rotation vel
  408. bytes[i++] = (byte)(ac % 256);
  409. bytes[i++] = (byte)((ac >> 8) % 256);
  410. bytes[i++] = (byte)(ac % 256);
  411. bytes[i++] = (byte)((ac >> 8) % 256);
  412. bytes[i++] = (byte)(ac % 256);
  413. bytes[i++] = (byte)((ac >> 8) % 256);
  414. dat.Data = bytes;
  415. return dat;
  416. }
  417. }
  418. }