Primitive2.cs 17 KB

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