EventQueueHelper.cs 19 KB

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