ChildAgentDataUpdate.cs 18 KB

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