EventQueueHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 OpenSimulator 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.Net;
  29. using OpenMetaverse;
  30. using OpenMetaverse.Packets;
  31. using OpenMetaverse.StructuredData;
  32. using OpenMetaverse.Messages.Linden;
  33. using OpenSim.Framework;
  34. namespace OpenSim.Region.ClientStack.Linden
  35. {
  36. public class EventQueueHelper
  37. {
  38. private EventQueueHelper() {} // no construction possible, it's an utility class
  39. private static byte[] ulongToByteArray(ulong uLongValue)
  40. {
  41. // Reverse endianness of RegionHandle
  42. return new byte[]
  43. {
  44. (byte)((uLongValue >> 56) % 256),
  45. (byte)((uLongValue >> 48) % 256),
  46. (byte)((uLongValue >> 40) % 256),
  47. (byte)((uLongValue >> 32) % 256),
  48. (byte)((uLongValue >> 24) % 256),
  49. (byte)((uLongValue >> 16) % 256),
  50. (byte)((uLongValue >> 8) % 256),
  51. (byte)(uLongValue % 256)
  52. };
  53. }
  54. // private static byte[] uintToByteArray(uint uIntValue)
  55. // {
  56. // byte[] result = new byte[4];
  57. // Utils.UIntToBytesBig(uIntValue, result, 0);
  58. // return result;
  59. // }
  60. public static OSD BuildEvent(string eventName, OSD eventBody)
  61. {
  62. OSDMap llsdEvent = new OSDMap(2);
  63. llsdEvent.Add("message", new OSDString(eventName));
  64. llsdEvent.Add("body", eventBody);
  65. return llsdEvent;
  66. }
  67. public static OSD EnableSimulator(ulong handle, IPEndPoint endPoint, int regionSizeX, int regionSizeY)
  68. {
  69. OSDMap llsdSimInfo = new OSDMap(5);
  70. llsdSimInfo.Add("Handle", new OSDBinary(ulongToByteArray(handle)));
  71. llsdSimInfo.Add("IP", new OSDBinary(endPoint.Address.GetAddressBytes()));
  72. llsdSimInfo.Add("Port", OSD.FromInteger(endPoint.Port));
  73. llsdSimInfo.Add("RegionSizeX", OSD.FromUInteger((uint)regionSizeX));
  74. llsdSimInfo.Add("RegionSizeY", OSD.FromUInteger((uint)regionSizeY));
  75. OSDArray arr = new OSDArray(1);
  76. arr.Add(llsdSimInfo);
  77. OSDMap llsdBody = new OSDMap(1);
  78. llsdBody.Add("SimulatorInfo", arr);
  79. return BuildEvent("EnableSimulator", llsdBody);
  80. }
  81. /*
  82. public static OSD DisableSimulator(ulong handle)
  83. {
  84. //OSDMap llsdSimInfo = new OSDMap(1);
  85. //llsdSimInfo.Add("Handle", new OSDBinary(regionHandleToByteArray(handle)));
  86. //OSDArray arr = new OSDArray(1);
  87. //arr.Add(llsdSimInfo);
  88. OSDMap llsdBody = new OSDMap(0);
  89. //llsdBody.Add("SimulatorInfo", arr);
  90. return BuildEvent("DisableSimulator", llsdBody);
  91. }
  92. */
  93. public static OSD CrossRegion(ulong handle, Vector3 pos, Vector3 lookAt,
  94. IPEndPoint newRegionExternalEndPoint,
  95. string capsURL, UUID agentID, UUID sessionID,
  96. int regionSizeX, int regionSizeY)
  97. {
  98. OSDArray lookAtArr = new OSDArray(3);
  99. lookAtArr.Add(OSD.FromReal(lookAt.X));
  100. lookAtArr.Add(OSD.FromReal(lookAt.Y));
  101. lookAtArr.Add(OSD.FromReal(lookAt.Z));
  102. OSDArray positionArr = new OSDArray(3);
  103. positionArr.Add(OSD.FromReal(pos.X));
  104. positionArr.Add(OSD.FromReal(pos.Y));
  105. positionArr.Add(OSD.FromReal(pos.Z));
  106. OSDMap infoMap = new OSDMap(2);
  107. infoMap.Add("LookAt", lookAtArr);
  108. infoMap.Add("Position", positionArr);
  109. OSDArray infoArr = new OSDArray(1);
  110. infoArr.Add(infoMap);
  111. OSDMap agentDataMap = new OSDMap(2);
  112. agentDataMap.Add("AgentID", OSD.FromUUID(agentID));
  113. agentDataMap.Add("SessionID", OSD.FromUUID(sessionID));
  114. OSDArray agentDataArr = new OSDArray(1);
  115. agentDataArr.Add(agentDataMap);
  116. OSDMap regionDataMap = new OSDMap(6);
  117. regionDataMap.Add("RegionHandle", OSD.FromBinary(ulongToByteArray(handle)));
  118. regionDataMap.Add("SeedCapability", OSD.FromString(capsURL));
  119. regionDataMap.Add("SimIP", OSD.FromBinary(newRegionExternalEndPoint.Address.GetAddressBytes()));
  120. regionDataMap.Add("SimPort", OSD.FromInteger(newRegionExternalEndPoint.Port));
  121. regionDataMap.Add("RegionSizeX", OSD.FromUInteger((uint)regionSizeX));
  122. regionDataMap.Add("RegionSizeY", OSD.FromUInteger((uint)regionSizeY));
  123. OSDArray regionDataArr = new OSDArray(1);
  124. regionDataArr.Add(regionDataMap);
  125. OSDMap llsdBody = new OSDMap(3);
  126. llsdBody.Add("Info", infoArr);
  127. llsdBody.Add("AgentData", agentDataArr);
  128. llsdBody.Add("RegionData", regionDataArr);
  129. return BuildEvent("CrossedRegion", llsdBody);
  130. }
  131. public static OSD TeleportFinishEvent(
  132. ulong regionHandle, byte simAccess, IPEndPoint regionExternalEndPoint,
  133. uint locationID, uint flags, string capsURL, UUID agentID,
  134. int regionSizeX, int regionSizeY)
  135. {
  136. // not sure why flags get overwritten here
  137. if ((flags & (uint)TeleportFlags.IsFlying) != 0)
  138. flags = (uint)TeleportFlags.ViaLocation | (uint)TeleportFlags.IsFlying;
  139. else
  140. flags = (uint)TeleportFlags.ViaLocation;
  141. OSDMap info = new OSDMap();
  142. info.Add("AgentID", OSD.FromUUID(agentID));
  143. info.Add("LocationID", OSD.FromInteger(4)); // TODO what is this?
  144. info.Add("RegionHandle", OSD.FromBinary(ulongToByteArray(regionHandle)));
  145. info.Add("SeedCapability", OSD.FromString(capsURL));
  146. info.Add("SimAccess", OSD.FromInteger(simAccess));
  147. info.Add("SimIP", OSD.FromBinary(regionExternalEndPoint.Address.GetAddressBytes()));
  148. info.Add("SimPort", OSD.FromInteger(regionExternalEndPoint.Port));
  149. // info.Add("TeleportFlags", OSD.FromULong(1L << 4)); // AgentManager.TeleportFlags.ViaLocation
  150. info.Add("TeleportFlags", OSD.FromUInteger(flags));
  151. info.Add("RegionSizeX", OSD.FromUInteger((uint)regionSizeX));
  152. info.Add("RegionSizeY", OSD.FromUInteger((uint)regionSizeY));
  153. OSDArray infoArr = new OSDArray();
  154. infoArr.Add(info);
  155. OSDMap body = new OSDMap();
  156. body.Add("Info", infoArr);
  157. return BuildEvent("TeleportFinish", body);
  158. }
  159. public static OSD ScriptRunningReplyEvent(UUID objectID, UUID itemID, bool running, bool mono)
  160. {
  161. OSDMap script = new OSDMap();
  162. script.Add("ObjectID", OSD.FromUUID(objectID));
  163. script.Add("ItemID", OSD.FromUUID(itemID));
  164. script.Add("Running", OSD.FromBoolean(running));
  165. script.Add("Mono", OSD.FromBoolean(mono));
  166. OSDArray scriptArr = new OSDArray();
  167. scriptArr.Add(script);
  168. OSDMap body = new OSDMap();
  169. body.Add("Script", scriptArr);
  170. return BuildEvent("ScriptRunningReply", body);
  171. }
  172. public static OSD EstablishAgentCommunication(UUID agentID, string simIpAndPort, string seedcap,
  173. ulong regionHandle, int regionSizeX, int regionSizeY)
  174. {
  175. OSDMap body = new OSDMap(6)
  176. {
  177. {"agent-id", new OSDUUID(agentID)},
  178. {"sim-ip-and-port", new OSDString(simIpAndPort)},
  179. {"seed-capability", new OSDString(seedcap)},
  180. {"region-handle", OSD.FromULong(regionHandle)},
  181. {"region-size-x", OSD.FromUInteger((uint)regionSizeX)},
  182. {"region-size-y", OSD.FromUInteger((uint)regionSizeY)}
  183. };
  184. return BuildEvent("EstablishAgentCommunication", body);
  185. }
  186. public static OSD KeepAliveEvent()
  187. {
  188. return BuildEvent("FAKEEVENT", new OSDMap());
  189. }
  190. public static OSD AgentParams(UUID agentID, bool checkEstate, int godLevel, bool limitedToEstate)
  191. {
  192. OSDMap body = new OSDMap(4);
  193. body.Add("agent_id", new OSDUUID(agentID));
  194. body.Add("check_estate", new OSDInteger(checkEstate ? 1 : 0));
  195. body.Add("god_level", new OSDInteger(godLevel));
  196. body.Add("limited_to_estate", new OSDInteger(limitedToEstate ? 1 : 0));
  197. return body;
  198. }
  199. public static OSD InstantMessageParams(UUID fromAgent, string message, UUID toAgent,
  200. string fromName, byte dialog, uint timeStamp, bool offline, int parentEstateID,
  201. Vector3 position, uint ttl, UUID transactionID, bool fromGroup, byte[] binaryBucket)
  202. {
  203. OSDMap messageParams = new OSDMap(15);
  204. messageParams.Add("type", new OSDInteger((int)dialog));
  205. OSDArray positionArray = new OSDArray(3);
  206. positionArray.Add(OSD.FromReal(position.X));
  207. positionArray.Add(OSD.FromReal(position.Y));
  208. positionArray.Add(OSD.FromReal(position.Z));
  209. messageParams.Add("position", positionArray);
  210. messageParams.Add("region_id", new OSDUUID(UUID.Zero));
  211. messageParams.Add("to_id", new OSDUUID(toAgent));
  212. messageParams.Add("source", new OSDInteger(0));
  213. OSDMap data = new OSDMap(1);
  214. data.Add("binary_bucket", OSD.FromBinary(binaryBucket));
  215. messageParams.Add("data", data);
  216. messageParams.Add("message", new OSDString(message));
  217. messageParams.Add("id", new OSDUUID(transactionID));
  218. messageParams.Add("from_name", new OSDString(fromName));
  219. messageParams.Add("timestamp", new OSDInteger((int)timeStamp));
  220. messageParams.Add("offline", new OSDInteger(offline ? 1 : 0));
  221. messageParams.Add("parent_estate_id", new OSDInteger(parentEstateID));
  222. messageParams.Add("ttl", new OSDInteger((int)ttl));
  223. messageParams.Add("from_id", new OSDUUID(fromAgent));
  224. messageParams.Add("from_group", new OSDInteger(fromGroup ? 1 : 0));
  225. return messageParams;
  226. }
  227. public static OSD InstantMessage(UUID fromAgent, string message, UUID toAgent,
  228. string fromName, byte dialog, uint timeStamp, bool offline, int parentEstateID,
  229. Vector3 position, uint ttl, UUID transactionID, bool fromGroup, byte[] binaryBucket,
  230. bool checkEstate, int godLevel, bool limitedToEstate)
  231. {
  232. OSDMap im = new OSDMap(2);
  233. im.Add("message_params", InstantMessageParams(fromAgent, message, toAgent,
  234. fromName, dialog, timeStamp, offline, parentEstateID,
  235. position, ttl, transactionID, fromGroup, binaryBucket));
  236. im.Add("agent_params", AgentParams(fromAgent, checkEstate, godLevel, limitedToEstate));
  237. return im;
  238. }
  239. public static OSD ChatterboxInvitation(UUID sessionID, string sessionName,
  240. UUID fromAgent, string message, UUID toAgent, string fromName, byte dialog,
  241. uint timeStamp, bool offline, int parentEstateID, Vector3 position,
  242. uint ttl, UUID transactionID, bool fromGroup, byte[] binaryBucket)
  243. {
  244. OSDMap body = new OSDMap(5);
  245. body.Add("session_id", new OSDUUID(sessionID));
  246. body.Add("from_name", new OSDString(fromName));
  247. body.Add("session_name", new OSDString(sessionName));
  248. body.Add("from_id", new OSDUUID(fromAgent));
  249. body.Add("instantmessage", InstantMessage(fromAgent, message, toAgent,
  250. fromName, dialog, timeStamp, offline, parentEstateID, position,
  251. ttl, transactionID, fromGroup, binaryBucket, true, 0, true));
  252. OSDMap chatterboxInvitation = new OSDMap(2);
  253. chatterboxInvitation.Add("message", new OSDString("ChatterBoxInvitation"));
  254. chatterboxInvitation.Add("body", body);
  255. return chatterboxInvitation;
  256. }
  257. public static OSD ChatterBoxSessionAgentListUpdates(UUID sessionID,
  258. UUID agentID, bool canVoiceChat, bool isModerator, bool textMute, bool isEnterorLeave)
  259. {
  260. OSDMap body = new OSDMap();
  261. OSDMap agentUpdates = new OSDMap();
  262. OSDMap infoDetail = new OSDMap();
  263. OSDMap mutes = new OSDMap();
  264. // this should be a list of agents and parameters
  265. // foreach agent
  266. mutes.Add("text", OSD.FromBoolean(textMute));
  267. infoDetail.Add("can_voice_chat", OSD.FromBoolean(canVoiceChat));
  268. infoDetail.Add("is_moderator", OSD.FromBoolean(isModerator));
  269. infoDetail.Add("mutes", mutes);
  270. OSDMap info = new OSDMap();
  271. info.Add("info", infoDetail);
  272. if(isEnterorLeave)
  273. info.Add("transition",OSD.FromString("ENTER"));
  274. else
  275. info.Add("transition",OSD.FromString("LEAVE"));
  276. agentUpdates.Add(agentID.ToString(), info);
  277. // foreach end
  278. body.Add("agent_updates", agentUpdates);
  279. body.Add("session_id", OSD.FromUUID(sessionID));
  280. body.Add("updates", new OSD());
  281. OSDMap chatterBoxSessionAgentListUpdates = new OSDMap();
  282. chatterBoxSessionAgentListUpdates.Add("message", OSD.FromString("ChatterBoxSessionAgentListUpdates"));
  283. chatterBoxSessionAgentListUpdates.Add("body", body);
  284. return chatterBoxSessionAgentListUpdates;
  285. }
  286. public static OSD ChatterBoxForceClose(UUID sessionID, string reason)
  287. {
  288. OSDMap body = new OSDMap(2);
  289. body.Add("session_id", new OSDUUID(sessionID));
  290. body.Add("reason", new OSDString(reason));
  291. OSDMap chatterBoxForceClose = new OSDMap(2);
  292. chatterBoxForceClose.Add("message", new OSDString("ForceCloseChatterBoxSession"));
  293. chatterBoxForceClose.Add("body", body);
  294. return chatterBoxForceClose;
  295. }
  296. public static OSD GroupMembershipData(UUID receiverAgent, GroupMembershipData[] data)
  297. {
  298. OSDArray AgentData = new OSDArray(1);
  299. OSDMap AgentDataMap = new OSDMap(1);
  300. AgentDataMap.Add("AgentID", OSD.FromUUID(receiverAgent));
  301. AgentData.Add(AgentDataMap);
  302. OSDArray GroupData = new OSDArray(data.Length);
  303. OSDArray NewGroupData = new OSDArray(data.Length);
  304. foreach (GroupMembershipData membership in data)
  305. {
  306. OSDMap GroupDataMap = new OSDMap(6);
  307. OSDMap NewGroupDataMap = new OSDMap(1);
  308. GroupDataMap.Add("GroupID", OSD.FromUUID(membership.GroupID));
  309. GroupDataMap.Add("GroupPowers", OSD.FromULong(membership.GroupPowers));
  310. GroupDataMap.Add("AcceptNotices", OSD.FromBoolean(membership.AcceptNotices));
  311. GroupDataMap.Add("GroupInsigniaID", OSD.FromUUID(membership.GroupPicture));
  312. GroupDataMap.Add("Contribution", OSD.FromInteger(membership.Contribution));
  313. GroupDataMap.Add("GroupName", OSD.FromString(membership.GroupName));
  314. NewGroupDataMap.Add("ListInProfile", OSD.FromBoolean(membership.ListInProfile));
  315. GroupData.Add(GroupDataMap);
  316. NewGroupData.Add(NewGroupDataMap);
  317. }
  318. OSDMap llDataStruct = new OSDMap(3);
  319. llDataStruct.Add("AgentData", AgentData);
  320. llDataStruct.Add("GroupData", GroupData);
  321. llDataStruct.Add("NewGroupData", NewGroupData);
  322. return BuildEvent("AgentGroupDataUpdate", llDataStruct);
  323. }
  324. public static OSD PlacesQuery(PlacesReplyPacket PlacesReply)
  325. {
  326. OSDMap placesReply = new OSDMap();
  327. placesReply.Add("message", OSD.FromString("PlacesReplyMessage"));
  328. OSDMap body = new OSDMap();
  329. OSDArray agentData = new OSDArray();
  330. OSDMap agentDataMap = new OSDMap();
  331. agentDataMap.Add("AgentID", OSD.FromUUID(PlacesReply.AgentData.AgentID));
  332. agentDataMap.Add("QueryID", OSD.FromUUID(PlacesReply.AgentData.QueryID));
  333. agentDataMap.Add("TransactionID", OSD.FromUUID(PlacesReply.TransactionData.TransactionID));
  334. agentData.Add(agentDataMap);
  335. body.Add("AgentData", agentData);
  336. OSDArray QueryData = new OSDArray();
  337. foreach (PlacesReplyPacket.QueryDataBlock groupDataBlock in PlacesReply.QueryData)
  338. {
  339. OSDMap QueryDataMap = new OSDMap();
  340. QueryDataMap.Add("ActualArea", OSD.FromInteger(groupDataBlock.ActualArea));
  341. QueryDataMap.Add("BillableArea", OSD.FromInteger(groupDataBlock.BillableArea));
  342. QueryDataMap.Add("Description", OSD.FromBinary(groupDataBlock.Desc));
  343. QueryDataMap.Add("Dwell", OSD.FromInteger((int)groupDataBlock.Dwell));
  344. QueryDataMap.Add("Flags", OSD.FromString(Convert.ToString(groupDataBlock.Flags)));
  345. QueryDataMap.Add("GlobalX", OSD.FromInteger((int)groupDataBlock.GlobalX));
  346. QueryDataMap.Add("GlobalY", OSD.FromInteger((int)groupDataBlock.GlobalY));
  347. QueryDataMap.Add("GlobalZ", OSD.FromInteger((int)groupDataBlock.GlobalZ));
  348. QueryDataMap.Add("Name", OSD.FromBinary(groupDataBlock.Name));
  349. QueryDataMap.Add("OwnerID", OSD.FromUUID(groupDataBlock.OwnerID));
  350. QueryDataMap.Add("SimName", OSD.FromBinary(groupDataBlock.SimName));
  351. QueryDataMap.Add("SnapShotID", OSD.FromUUID(groupDataBlock.SnapshotID));
  352. QueryDataMap.Add("ProductSku", OSD.FromInteger(0));
  353. QueryDataMap.Add("Price", OSD.FromInteger(groupDataBlock.Price));
  354. QueryData.Add(QueryDataMap);
  355. }
  356. body.Add("QueryData", QueryData);
  357. placesReply.Add("QueryData[]", body);
  358. return placesReply;
  359. }
  360. public static OSD ParcelProperties(ParcelPropertiesMessage parcelPropertiesMessage)
  361. {
  362. OSDMap message = new OSDMap();
  363. message.Add("message", OSD.FromString("ParcelProperties"));
  364. OSD message_body = parcelPropertiesMessage.Serialize();
  365. message.Add("body", message_body);
  366. return message;
  367. }
  368. public static OSD partPhysicsProperties(uint localID, byte physhapetype,
  369. float density, float friction, float bounce, float gravmod)
  370. {
  371. OSDMap physinfo = new OSDMap(6);
  372. physinfo["LocalID"] = localID;
  373. physinfo["Density"] = density;
  374. physinfo["Friction"] = friction;
  375. physinfo["GravityMultiplier"] = gravmod;
  376. physinfo["Restitution"] = bounce;
  377. physinfo["PhysicsShapeType"] = (int)physhapetype;
  378. OSDArray array = new OSDArray(1);
  379. array.Add(physinfo);
  380. OSDMap llsdBody = new OSDMap(1);
  381. llsdBody.Add("ObjectData", array);
  382. return BuildEvent("ObjectPhysicsProperties", llsdBody);
  383. }
  384. }
  385. }