ChildAgentDataUpdate.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 OpenSim 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 OpenMetaverse;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using OSD = OpenMetaverse.StructuredData.OSD;
  32. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  33. using OSDArray = OpenMetaverse.StructuredData.OSDArray;
  34. using OSDParser = OpenMetaverse.StructuredData.OSDParser;
  35. namespace OpenSim.Framework
  36. {
  37. // Soon to be dismissed
  38. [Serializable]
  39. public class ChildAgentDataUpdate
  40. {
  41. public Guid ActiveGroupID;
  42. public Guid AgentID;
  43. public bool alwaysrun;
  44. public float AVHeight;
  45. public sLLVector3 cameraPosition;
  46. public float drawdistance;
  47. public float godlevel;
  48. public uint GroupAccess;
  49. public sLLVector3 Position;
  50. public ulong regionHandle;
  51. public byte[] throttles;
  52. public sLLVector3 Velocity;
  53. public ChildAgentDataUpdate()
  54. {
  55. }
  56. public ChildAgentDataUpdate(AgentData agent)
  57. {
  58. ActiveGroupID = agent.ActiveGroupID.Guid;
  59. AgentID = agent.AgentID.Guid;
  60. alwaysrun = agent.AlwaysRun;
  61. AVHeight = agent.Size.Z;
  62. cameraPosition = new sLLVector3(agent.Center);
  63. drawdistance = agent.Far;
  64. godlevel = (float)agent.GodLevel;
  65. if (agent.Groups.Length > 0)
  66. GroupAccess = (uint)agent.Groups[0].GroupPowers;
  67. Position = new sLLVector3(agent.Position);
  68. regionHandle = agent.RegionHandle;
  69. throttles = agent.Throttles;
  70. Velocity = new sLLVector3(agent.Velocity);
  71. }
  72. public ChildAgentDataUpdate(AgentPosition agent)
  73. {
  74. AgentID = agent.AgentID.Guid;
  75. AVHeight = agent.Size.Z;
  76. cameraPosition = new sLLVector3(agent.Center);
  77. drawdistance = agent.Far;
  78. Position = new sLLVector3(agent.Position);
  79. regionHandle = agent.RegionHandle;
  80. throttles = agent.Throttles;
  81. Velocity = new sLLVector3(agent.Velocity);
  82. }
  83. }
  84. public interface IAgentData
  85. {
  86. UUID AgentID { get; set; }
  87. OSDMap PackUpdateMessage();
  88. void UnpackUpdateMessage(OSDMap map);
  89. }
  90. /// <summary>
  91. /// Replacement for ChildAgentDataUpdate. Used over RESTComms and LocalComms.
  92. /// </summary>
  93. public class AgentPosition : IAgentData
  94. {
  95. private UUID m_id;
  96. public UUID AgentID
  97. {
  98. get { return m_id; }
  99. set { m_id = value; }
  100. }
  101. public ulong RegionHandle;
  102. public uint CircuitCode;
  103. public UUID SessionID;
  104. public float Far;
  105. public Vector3 Position;
  106. public Vector3 Velocity;
  107. public Vector3 Center;
  108. public Vector3 Size;
  109. public Vector3 AtAxis;
  110. public Vector3 LeftAxis;
  111. public Vector3 UpAxis;
  112. public bool ChangedGrid;
  113. // This probably shouldn't be here
  114. public byte[] Throttles;
  115. public OSDMap PackUpdateMessage()
  116. {
  117. OSDMap args = new OSDMap();
  118. args["message_type"] = OSD.FromString("AgentPosition");
  119. args["region_handle"] = OSD.FromString(RegionHandle.ToString());
  120. args["circuit_code"] = OSD.FromString(CircuitCode.ToString());
  121. args["agent_uuid"] = OSD.FromUUID(AgentID);
  122. args["session_uuid"] = OSD.FromUUID(SessionID);
  123. args["position"] = OSD.FromString(Position.ToString());
  124. args["velocity"] = OSD.FromString(Velocity.ToString());
  125. args["center"] = OSD.FromString(Center.ToString());
  126. args["size"] = OSD.FromString(Size.ToString());
  127. args["at_axis"] = OSD.FromString(AtAxis.ToString());
  128. args["left_axis"] = OSD.FromString(LeftAxis.ToString());
  129. args["up_axis"] = OSD.FromString(UpAxis.ToString());
  130. args["far"] = OSD.FromReal(Far);
  131. args["changed_grid"] = OSD.FromBoolean(ChangedGrid);
  132. if ((Throttles != null) && (Throttles.Length > 0))
  133. args["throttles"] = OSD.FromBinary(Throttles);
  134. return args;
  135. }
  136. public void UnpackUpdateMessage(OSDMap args)
  137. {
  138. if (args.ContainsKey("region_handle"))
  139. UInt64.TryParse(args["region_handle"].AsString(), out RegionHandle);
  140. if (args["circuit_code"] != null)
  141. UInt32.TryParse((string)args["circuit_code"].AsString(), out CircuitCode);
  142. if (args["agent_uuid"] != null)
  143. AgentID = args["agent_uuid"].AsUUID();
  144. if (args["session_uuid"] != null)
  145. SessionID = args["session_uuid"].AsUUID();
  146. if (args["position"] != null)
  147. Vector3.TryParse(args["position"].AsString(), out Position);
  148. if (args["velocity"] != null)
  149. Vector3.TryParse(args["velocity"].AsString(), out Velocity);
  150. if (args["center"] != null)
  151. Vector3.TryParse(args["center"].AsString(), out Center);
  152. if (args["size"] != null)
  153. Vector3.TryParse(args["size"].AsString(), out Size);
  154. if (args["at_axis"] != null)
  155. Vector3.TryParse(args["at_axis"].AsString(), out AtAxis);
  156. if (args["left_axis"] != null)
  157. Vector3.TryParse(args["left_axis"].AsString(), out AtAxis);
  158. if (args["up_axis"] != null)
  159. Vector3.TryParse(args["up_axis"].AsString(), out AtAxis);
  160. if (args["changed_grid"] != null)
  161. ChangedGrid = args["changed_grid"].AsBoolean();
  162. if (args["far"] != null)
  163. Far = (float)(args["far"].AsReal());
  164. if (args["throttles"] != null)
  165. Throttles = args["throttles"].AsBinary();
  166. }
  167. /// <summary>
  168. /// Soon to be decommissioned
  169. /// </summary>
  170. /// <param name="cAgent"></param>
  171. public void CopyFrom(ChildAgentDataUpdate cAgent)
  172. {
  173. AgentID = new UUID(cAgent.AgentID);
  174. // next: ???
  175. Size = new Vector3();
  176. Size.Z = cAgent.AVHeight;
  177. Center = new Vector3(cAgent.cameraPosition.x, cAgent.cameraPosition.y, cAgent.cameraPosition.z);
  178. Far = cAgent.drawdistance;
  179. Position = new Vector3(cAgent.Position.x, cAgent.Position.y, cAgent.Position.z);
  180. RegionHandle = cAgent.regionHandle;
  181. Throttles = cAgent.throttles;
  182. Velocity = new Vector3(cAgent.Velocity.x, cAgent.Velocity.y, cAgent.Velocity.z);
  183. }
  184. }
  185. public class AgentGroupData
  186. {
  187. public UUID GroupID;
  188. public ulong GroupPowers;
  189. public bool AcceptNotices;
  190. public AgentGroupData(UUID id, ulong powers, bool notices)
  191. {
  192. GroupID = id;
  193. GroupPowers = powers;
  194. AcceptNotices = notices;
  195. }
  196. public AgentGroupData(OSDMap args)
  197. {
  198. UnpackUpdateMessage(args);
  199. }
  200. public OSDMap PackUpdateMessage()
  201. {
  202. OSDMap groupdata = new OSDMap();
  203. groupdata["group_id"] = OSD.FromUUID(GroupID);
  204. groupdata["group_powers"] = OSD.FromString(GroupPowers.ToString());
  205. groupdata["accept_notices"] = OSD.FromBoolean(AcceptNotices);
  206. return groupdata;
  207. }
  208. public void UnpackUpdateMessage(OSDMap args)
  209. {
  210. if (args["group_id"] != null)
  211. GroupID = args["group_id"].AsUUID();
  212. if (args["group_powers"] != null)
  213. UInt64.TryParse((string)args["group_powers"].AsString(), out GroupPowers);
  214. if (args["accept_notices"] != null)
  215. AcceptNotices = args["accept_notices"].AsBoolean();
  216. }
  217. }
  218. public class AgentAnimationData
  219. {
  220. public UUID Animation;
  221. public UUID ObjectID;
  222. public AgentAnimationData(OSDMap args)
  223. {
  224. UnpackUpdateMessage(args);
  225. }
  226. public OSDMap PackUpdateMessage()
  227. {
  228. OSDMap anim = new OSDMap();
  229. anim["animation"] = OSD.FromUUID(Animation);
  230. anim["object_id"] = OSD.FromUUID(ObjectID);
  231. return anim;
  232. }
  233. public void UnpackUpdateMessage(OSDMap args)
  234. {
  235. if (args["animation"] != null)
  236. Animation = args["animation"].AsUUID();
  237. if (args["object_id"] != null)
  238. ObjectID = args["object_id"].AsUUID();
  239. }
  240. }
  241. public class AgentData : IAgentData
  242. {
  243. private UUID m_id;
  244. public UUID AgentID
  245. {
  246. get { return m_id; }
  247. set { m_id = value; }
  248. }
  249. public ulong RegionHandle;
  250. public uint CircuitCode;
  251. public UUID SessionID;
  252. public Vector3 Position;
  253. public Vector3 Velocity;
  254. public Vector3 Center;
  255. public Vector3 Size;
  256. public Vector3 AtAxis;
  257. public Vector3 LeftAxis;
  258. public Vector3 UpAxis;
  259. public bool ChangedGrid;
  260. public float Far;
  261. public float Aspect;
  262. //public int[] Throttles;
  263. public byte[] Throttles;
  264. public uint LocomotionState;
  265. public Quaternion HeadRotation;
  266. public Quaternion BodyRotation;
  267. public uint ControlFlags;
  268. public float EnergyLevel;
  269. public Byte GodLevel;
  270. public bool AlwaysRun;
  271. public UUID PreyAgent;
  272. public Byte AgentAccess;
  273. public UUID[] AgentTextures;
  274. public UUID ActiveGroupID;
  275. public AgentGroupData[] Groups;
  276. public AgentAnimationData[] Anims;
  277. public UUID GranterID;
  278. public Dictionary<string, string> NVPairs;
  279. public byte[] VisualParams;
  280. public string CallbackURI;
  281. public OSDMap PackUpdateMessage()
  282. {
  283. OSDMap args = new OSDMap();
  284. args["message_type"] = OSD.FromString("AgentData");
  285. args["region_handle"] = OSD.FromString(RegionHandle.ToString());
  286. args["circuit_code"] = OSD.FromString(CircuitCode.ToString());
  287. args["agent_uuid"] = OSD.FromUUID(AgentID);
  288. args["session_uuid"] = OSD.FromUUID(SessionID);
  289. args["position"] = OSD.FromString(Position.ToString());
  290. args["velocity"] = OSD.FromString(Velocity.ToString());
  291. args["center"] = OSD.FromString(Center.ToString());
  292. args["size"] = OSD.FromString(Size.ToString());
  293. args["at_axis"] = OSD.FromString(AtAxis.ToString());
  294. args["left_axis"] = OSD.FromString(LeftAxis.ToString());
  295. args["up_axis"] = OSD.FromString(UpAxis.ToString());
  296. args["changed_grid"] = OSD.FromBoolean(ChangedGrid);
  297. args["far"] = OSD.FromReal(Far);
  298. args["aspect"] = OSD.FromReal(Aspect);
  299. if ((Throttles != null) && (Throttles.Length > 0))
  300. args["throttles"] = OSD.FromBinary(Throttles);
  301. args["locomotion_state"] = OSD.FromString(LocomotionState.ToString());
  302. args["head_rotation"] = OSD.FromString(HeadRotation.ToString());
  303. args["body_rotation"] = OSD.FromString(BodyRotation.ToString());
  304. args["control_flags"] = OSD.FromString(ControlFlags.ToString());
  305. args["energy_level"] = OSD.FromReal(EnergyLevel);
  306. args["god_level"] = OSD.FromString(GodLevel.ToString());
  307. args["always_run"] = OSD.FromBoolean(AlwaysRun);
  308. args["prey_agent"] = OSD.FromUUID(PreyAgent);
  309. args["agent_access"] = OSD.FromString(AgentAccess.ToString());
  310. if ((AgentTextures != null) && (AgentTextures.Length > 0))
  311. {
  312. OSDArray textures = new OSDArray(AgentTextures.Length);
  313. foreach (UUID uuid in AgentTextures)
  314. textures.Add(OSD.FromUUID(uuid));
  315. args["agent_textures"] = textures;
  316. }
  317. args["active_group_id"] = OSD.FromUUID(ActiveGroupID);
  318. if ((Groups != null) && (Groups.Length > 0))
  319. {
  320. OSDArray groups = new OSDArray(Groups.Length);
  321. foreach (AgentGroupData agd in Groups)
  322. groups.Add(agd.PackUpdateMessage());
  323. args["groups"] = groups;
  324. }
  325. if ((Anims != null) && (Anims.Length > 0))
  326. {
  327. OSDArray anims = new OSDArray(Anims.Length);
  328. foreach (AgentAnimationData aanim in Anims)
  329. anims.Add(aanim.PackUpdateMessage());
  330. args["animations"] = anims;
  331. }
  332. if ((VisualParams != null) && (VisualParams.Length > 0))
  333. args["visual_params"] = OSD.FromBinary(VisualParams);
  334. // Last few fields are still missing: granter and NVPais
  335. if ((CallbackURI != null) && (!CallbackURI.Equals("")))
  336. args["callback_uri"] = OSD.FromString(CallbackURI);
  337. return args;
  338. }
  339. /// <summary>
  340. /// Deserialization of agent data.
  341. /// Avoiding reflection makes it painful to write, but that's the price!
  342. /// </summary>
  343. /// <param name="hash"></param>
  344. public void UnpackUpdateMessage(OSDMap args)
  345. {
  346. if (args.ContainsKey("region_handle"))
  347. UInt64.TryParse(args["region_handle"].AsString(), out RegionHandle);
  348. if (args["circuit_code"] != null)
  349. UInt32.TryParse((string)args["circuit_code"].AsString(), out CircuitCode);
  350. if (args["agent_uuid"] != null)
  351. AgentID = args["agent_uuid"].AsUUID();
  352. if (args["session_uuid"] != null)
  353. SessionID = args["session_uuid"].AsUUID();
  354. if (args["position"] != null)
  355. Vector3.TryParse(args["position"].AsString(), out Position);
  356. if (args["velocity"] != null)
  357. Vector3.TryParse(args["velocity"].AsString(), out Velocity);
  358. if (args["center"] != null)
  359. Vector3.TryParse(args["center"].AsString(), out Center);
  360. if (args["size"] != null)
  361. Vector3.TryParse(args["size"].AsString(), out Size);
  362. if (args["at_axis"] != null)
  363. Vector3.TryParse(args["at_axis"].AsString(), out AtAxis);
  364. if (args["left_axis"] != null)
  365. Vector3.TryParse(args["left_axis"].AsString(), out AtAxis);
  366. if (args["up_axis"] != null)
  367. Vector3.TryParse(args["up_axis"].AsString(), out AtAxis);
  368. if (args["changed_grid"] != null)
  369. ChangedGrid = args["changed_grid"].AsBoolean();
  370. if (args["far"] != null)
  371. Far = (float)(args["far"].AsReal());
  372. if (args["aspect"] != null)
  373. Aspect = (float)args["aspect"].AsReal();
  374. if (args["throttles"] != null)
  375. Throttles = args["throttles"].AsBinary();
  376. if (args["locomotion_state"] != null)
  377. UInt32.TryParse(args["locomotion_state"].AsString(), out LocomotionState);
  378. if (args["head_rotation"] != null)
  379. Quaternion.TryParse(args["head_rotation"].AsString(), out HeadRotation);
  380. if (args["body_rotation"] != null)
  381. Quaternion.TryParse(args["body_rotation"].AsString(), out BodyRotation);
  382. if (args["control_flags"] != null)
  383. UInt32.TryParse(args["control_flags"].AsString(), out ControlFlags);
  384. if (args["energy_level"] != null)
  385. EnergyLevel = (float)(args["energy_level"].AsReal());
  386. if (args["god_level"] != null)
  387. Byte.TryParse(args["god_level"].AsString(), out GodLevel);
  388. if (args["always_run"] != null)
  389. AlwaysRun = args["always_run"].AsBoolean();
  390. if (args["prey_agent"] != null)
  391. PreyAgent = args["prey_agent"].AsUUID();
  392. if (args["agent_access"] != null)
  393. Byte.TryParse(args["agent_access"].AsString(), out AgentAccess);
  394. if ((args["agent_textures"] != null) && (args["agent_textures"]).Type == OpenMetaverse.StructuredData.OSDType.Array)
  395. {
  396. OSDArray textures = (OSDArray)(args["agent_textures"]);
  397. AgentTextures = new UUID[textures.Count];
  398. int i = 0;
  399. foreach (OSD o in textures)
  400. AgentTextures[i++] = o.AsUUID();
  401. }
  402. if (args["active_group_id"] != null)
  403. ActiveGroupID = args["active_group_id"].AsUUID();
  404. if ((args["groups"] != null) && (args["groups"]).Type == OpenMetaverse.StructuredData.OSDType.Array)
  405. {
  406. OSDArray groups = (OSDArray)(args["groups"]);
  407. Groups = new AgentGroupData[groups.Count];
  408. int i = 0;
  409. foreach (OSD o in groups)
  410. {
  411. if (o.Type == OpenMetaverse.StructuredData.OSDType.Map)
  412. {
  413. Groups[i++] = new AgentGroupData((OSDMap)o);
  414. }
  415. }
  416. }
  417. if ((args["animations"] != null) && (args["animations"]).Type == OpenMetaverse.StructuredData.OSDType.Array)
  418. {
  419. OSDArray anims = (OSDArray)(args["animations"]);
  420. Anims = new AgentAnimationData[anims.Count];
  421. int i = 0;
  422. foreach (OSD o in anims)
  423. {
  424. if (o.Type == OpenMetaverse.StructuredData.OSDType.Map)
  425. {
  426. Anims[i++] = new AgentAnimationData((OSDMap)o);
  427. }
  428. }
  429. }
  430. if (args["visual_params"] != null)
  431. VisualParams = args["visual_params"].AsBinary();
  432. if (args["callback_uri"] != null)
  433. CallbackURI = args["callback_uri"].AsString();
  434. }
  435. public AgentData()
  436. {
  437. }
  438. public AgentData(Hashtable hash)
  439. {
  440. //UnpackUpdateMessage(hash);
  441. }
  442. /// <summary>
  443. /// Soon to be decommissioned
  444. /// </summary>
  445. /// <param name="cAgent"></param>
  446. public void CopyFrom(ChildAgentDataUpdate cAgent)
  447. {
  448. ActiveGroupID = new UUID(cAgent.ActiveGroupID);
  449. AgentID = new UUID(cAgent.AgentID);
  450. AlwaysRun = cAgent.alwaysrun;
  451. // next: ???
  452. Size = new Vector3();
  453. Size.Z = cAgent.AVHeight;
  454. Center = new Vector3(cAgent.cameraPosition.x, cAgent.cameraPosition.y, cAgent.cameraPosition.z);
  455. Far = cAgent.drawdistance;
  456. // downcasting ???
  457. GodLevel = (byte)(cAgent.godlevel);
  458. Groups = new AgentGroupData[1];
  459. Groups[0] = new AgentGroupData(new UUID(cAgent.ActiveGroupID), cAgent.GroupAccess, true);
  460. Position = new Vector3(cAgent.Position.x, cAgent.Position.y, cAgent.Position.z);
  461. RegionHandle = cAgent.regionHandle;
  462. Throttles = cAgent.throttles;
  463. Velocity = new Vector3(cAgent.Velocity.x, cAgent.Velocity.y, cAgent.Velocity.z);
  464. }
  465. public void Dump()
  466. {
  467. System.Console.WriteLine("------------ AgentData ------------");
  468. System.Console.WriteLine("UUID: " + AgentID);
  469. System.Console.WriteLine("Region: " + RegionHandle);
  470. System.Console.WriteLine("Position: " + Position);
  471. }
  472. }
  473. }