Primitive2.cs 18 KB

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