ObjectAdd.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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.Collections;
  29. using System.Reflection;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenMetaverse.StructuredData;
  34. using Mono.Addins;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. using Caps=OpenSim.Framework.Capabilities.Caps;
  41. namespace OpenSim.Region.ClientStack.Linden
  42. {
  43. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "ObjectAdd")]
  44. public class ObjectAdd : INonSharedRegionModule
  45. {
  46. // private static readonly ILog m_log =
  47. // LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private Scene m_scene;
  49. #region INonSharedRegionModule Members
  50. public void Initialise(IConfigSource pSource)
  51. {
  52. }
  53. public void AddRegion(Scene scene)
  54. {
  55. m_scene = scene;
  56. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  57. }
  58. public void RemoveRegion(Scene scene)
  59. {
  60. if (m_scene == scene)
  61. {
  62. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  63. m_scene = null;
  64. }
  65. }
  66. public void RegionLoaded(Scene scene)
  67. {
  68. }
  69. public void Close()
  70. {
  71. }
  72. public string Name
  73. {
  74. get { return "ObjectAddModule"; }
  75. }
  76. public Type ReplaceableInterface
  77. {
  78. get { return null; }
  79. }
  80. #endregion
  81. public void RegisterCaps(UUID agentID, Caps caps)
  82. {
  83. string capUrl = "/CAPS/" + UUID.Random() + "/";
  84. // m_log.InfoFormat("[OBJECTADD]: {0}", "/CAPS/OA/" + capuuid + "/");
  85. caps.RegisterHandler(
  86. "ObjectAdd",
  87. new RestHTTPHandler(
  88. "POST",
  89. capUrl,
  90. httpMethod => ProcessAdd(httpMethod, agentID, caps),
  91. "ObjectAdd",
  92. agentID.ToString())); ;
  93. }
  94. public Hashtable ProcessAdd(Hashtable request, UUID AgentId, Caps cap)
  95. {
  96. Hashtable responsedata = new Hashtable();
  97. responsedata["int_response_code"] = 400; //501; //410; //404;
  98. responsedata["content_type"] = "text/plain";
  99. responsedata["keepalive"] = false;
  100. responsedata["str_response_string"] = "Request wasn't what was expected";
  101. ScenePresence avatar;
  102. if (!m_scene.TryGetScenePresence(AgentId, out avatar))
  103. return responsedata;
  104. OSD r = OSDParser.DeserializeLLSDXml((string)request["requestbody"]);
  105. if (r.Type != OSDType.Map) // not a proper req
  106. return responsedata;
  107. //UUID session_id = UUID.Zero;
  108. bool bypass_raycast = false;
  109. uint everyone_mask = 0;
  110. uint group_mask = 0;
  111. uint next_owner_mask = 0;
  112. uint flags = 0;
  113. UUID group_id = UUID.Zero;
  114. int hollow = 0;
  115. int material = 0;
  116. int p_code = 0;
  117. int path_begin = 0;
  118. int path_curve = 0;
  119. int path_end = 0;
  120. int path_radius_offset = 0;
  121. int path_revolutions = 0;
  122. int path_scale_x = 0;
  123. int path_scale_y = 0;
  124. int path_shear_x = 0;
  125. int path_shear_y = 0;
  126. int path_skew = 0;
  127. int path_taper_x = 0;
  128. int path_taper_y = 0;
  129. int path_twist = 0;
  130. int path_twist_begin = 0;
  131. int profile_begin = 0;
  132. int profile_curve = 0;
  133. int profile_end = 0;
  134. Vector3 ray_end = Vector3.Zero;
  135. bool ray_end_is_intersection = false;
  136. Vector3 ray_start = Vector3.Zero;
  137. UUID ray_target_id = UUID.Zero;
  138. Quaternion rotation = Quaternion.Identity;
  139. Vector3 scale = Vector3.Zero;
  140. int state = 0;
  141. int lastattach = 0;
  142. OSDMap rm = (OSDMap)r;
  143. OSD tmpOSD;
  144. if (rm.TryGetValue("ObjectData", out tmpOSD)) //v2
  145. {
  146. if (tmpOSD.Type != OSDType.Map)
  147. {
  148. responsedata["str_response_string"] = "Has ObjectData key, but data not in expected format";
  149. return responsedata;
  150. }
  151. OSDMap ObjMap = (OSDMap)tmpOSD;
  152. bypass_raycast = ObjMap["BypassRaycast"].AsBoolean();
  153. everyone_mask = ReadUIntVal(ObjMap["EveryoneMask"]);
  154. flags = ReadUIntVal(ObjMap["Flags"]);
  155. group_mask = ReadUIntVal(ObjMap["GroupMask"]);
  156. material = ObjMap["Material"].AsInteger();
  157. next_owner_mask = ReadUIntVal(ObjMap["NextOwnerMask"]);
  158. p_code = ObjMap["PCode"].AsInteger();
  159. if (ObjMap.TryGetValue("Path", out tmpOSD))
  160. {
  161. if (tmpOSD.Type != OSDType.Map)
  162. {
  163. responsedata["str_response_string"] = "Has Path key, but data not in expected format";
  164. return responsedata;
  165. }
  166. OSDMap PathMap = (OSDMap)tmpOSD;
  167. path_begin = PathMap["Begin"].AsInteger();
  168. path_curve = PathMap["Curve"].AsInteger();
  169. path_end = PathMap["End"].AsInteger();
  170. path_radius_offset = PathMap["RadiusOffset"].AsInteger();
  171. path_revolutions = PathMap["Revolutions"].AsInteger();
  172. path_scale_x = PathMap["ScaleX"].AsInteger();
  173. path_scale_y = PathMap["ScaleY"].AsInteger();
  174. path_shear_x = PathMap["ShearX"].AsInteger();
  175. path_shear_y = PathMap["ShearY"].AsInteger();
  176. path_skew = PathMap["Skew"].AsInteger();
  177. path_taper_x = PathMap["TaperX"].AsInteger();
  178. path_taper_y = PathMap["TaperY"].AsInteger();
  179. path_twist = PathMap["Twist"].AsInteger();
  180. path_twist_begin = PathMap["TwistBegin"].AsInteger();
  181. }
  182. if (ObjMap.TryGetValue("Profile", out tmpOSD))
  183. {
  184. if (tmpOSD.Type != OSDType.Map)
  185. {
  186. responsedata["str_response_string"] = "Has Profile key, but data not in expected format";
  187. return responsedata;
  188. }
  189. OSDMap ProfileMap = (OSDMap)tmpOSD;
  190. profile_begin = ProfileMap["Begin"].AsInteger();
  191. profile_curve = ProfileMap["Curve"].AsInteger();
  192. profile_end = ProfileMap["End"].AsInteger();
  193. hollow = ProfileMap["Hollow"].AsInteger();
  194. }
  195. ray_end_is_intersection = ObjMap["RayEndIsIntersection"].AsBoolean();
  196. ray_target_id = ObjMap["RayTargetId"].AsUUID();
  197. state = ObjMap["State"].AsInteger();
  198. lastattach = ObjMap["LastAttachPoint"].AsInteger();
  199. try
  200. {
  201. ray_end = ((OSDArray)ObjMap["RayEnd"]).AsVector3();
  202. ray_start = ((OSDArray)ObjMap["RayStart"]).AsVector3();
  203. scale = ((OSDArray)ObjMap["Scale"]).AsVector3();
  204. rotation = ((OSDArray)ObjMap["Rotation"]).AsQuaternion();
  205. }
  206. catch (Exception)
  207. {
  208. responsedata["str_response_string"] = "RayEnd, RayStart, Scale or Rotation wasn't in the expected format";
  209. return responsedata;
  210. }
  211. if (rm.TryGetValue("AgentData", out tmpOSD))
  212. {
  213. if (tmpOSD.Type != OSDType.Map)
  214. {
  215. responsedata["str_response_string"] = "Has AgentData key, but data not in expected format";
  216. return responsedata;
  217. }
  218. OSDMap AgentDataMap = (OSDMap)tmpOSD;
  219. //session_id = AgentDataMap["SessionId"].AsUUID();
  220. group_id = AgentDataMap["GroupId"].AsUUID();
  221. }
  222. }
  223. else
  224. { //v1
  225. bypass_raycast = rm["bypass_raycast"].AsBoolean();
  226. everyone_mask = ReadUIntVal(rm["everyone_mask"]);
  227. flags = ReadUIntVal(rm["flags"]);
  228. group_id = rm["group_id"].AsUUID();
  229. group_mask = ReadUIntVal(rm["group_mask"]);
  230. hollow = rm["hollow"].AsInteger();
  231. material = rm["material"].AsInteger();
  232. next_owner_mask = ReadUIntVal(rm["next_owner_mask"]);
  233. hollow = rm["hollow"].AsInteger();
  234. p_code = rm["p_code"].AsInteger();
  235. path_begin = rm["path_begin"].AsInteger();
  236. path_curve = rm["path_curve"].AsInteger();
  237. path_end = rm["path_end"].AsInteger();
  238. path_radius_offset = rm["path_radius_offset"].AsInteger();
  239. path_revolutions = rm["path_revolutions"].AsInteger();
  240. path_scale_x = rm["path_scale_x"].AsInteger();
  241. path_scale_y = rm["path_scale_y"].AsInteger();
  242. path_shear_x = rm["path_shear_x"].AsInteger();
  243. path_shear_y = rm["path_shear_y"].AsInteger();
  244. path_skew = rm["path_skew"].AsInteger();
  245. path_taper_x = rm["path_taper_x"].AsInteger();
  246. path_taper_y = rm["path_taper_y"].AsInteger();
  247. path_twist = rm["path_twist"].AsInteger();
  248. path_twist_begin = rm["path_twist_begin"].AsInteger();
  249. profile_begin = rm["profile_begin"].AsInteger();
  250. profile_curve = rm["profile_curve"].AsInteger();
  251. profile_end = rm["profile_end"].AsInteger();
  252. ray_end_is_intersection = rm["ray_end_is_intersection"].AsBoolean();
  253. ray_target_id = rm["ray_target_id"].AsUUID();
  254. //session_id = rm["session_id"].AsUUID();
  255. state = rm["state"].AsInteger();
  256. lastattach = rm["last_attach_point"].AsInteger();
  257. try
  258. {
  259. ray_end = ((OSDArray)rm["ray_end"]).AsVector3();
  260. ray_start = ((OSDArray)rm["ray_start"]).AsVector3();
  261. rotation = ((OSDArray)rm["rotation"]).AsQuaternion();
  262. scale = ((OSDArray)rm["scale"]).AsVector3();
  263. }
  264. catch (Exception)
  265. {
  266. responsedata["str_response_string"] = "RayEnd, RayStart, Scale or Rotation wasn't in the expected format";
  267. return responsedata;
  268. }
  269. }
  270. Vector3 pos = m_scene.GetNewRezLocation(ray_start, ray_end, ray_target_id, rotation, (bypass_raycast) ? (byte)1 : (byte)0, (ray_end_is_intersection) ? (byte)1 : (byte)0, true, scale, false);
  271. PrimitiveBaseShape pbs = PrimitiveBaseShape.CreateBox();
  272. pbs.PathBegin = (ushort)path_begin;
  273. pbs.PathCurve = (byte)path_curve;
  274. pbs.PathEnd = (ushort)path_end;
  275. pbs.PathRadiusOffset = (sbyte)path_radius_offset;
  276. pbs.PathRevolutions = (byte)path_revolutions;
  277. pbs.PathScaleX = (byte)path_scale_x;
  278. pbs.PathScaleY = (byte)path_scale_y;
  279. pbs.PathShearX = (byte)path_shear_x;
  280. pbs.PathShearY = (byte)path_shear_y;
  281. pbs.PathSkew = (sbyte)path_skew;
  282. pbs.PathTaperX = (sbyte)path_taper_x;
  283. pbs.PathTaperY = (sbyte)path_taper_y;
  284. pbs.PathTwist = (sbyte)path_twist;
  285. pbs.PathTwistBegin = (sbyte)path_twist_begin;
  286. pbs.HollowShape = (HollowShape)hollow;
  287. pbs.PCode = (byte)p_code;
  288. pbs.ProfileBegin = (ushort)profile_begin;
  289. pbs.ProfileCurve = (byte)profile_curve;
  290. pbs.ProfileEnd = (ushort)profile_end;
  291. pbs.Scale = scale;
  292. pbs.State = (byte)state;
  293. pbs.LastAttachPoint = (byte)lastattach;
  294. SceneObjectGroup obj = null; ;
  295. if (m_scene.Permissions.CanRezObject(1, avatar.UUID, pos))
  296. {
  297. // rez ON the ground, not IN the ground
  298. // pos.Z += 0.25F;
  299. obj = m_scene.AddNewPrim(avatar.UUID, group_id, pos, rotation, pbs);
  300. }
  301. if (obj == null)
  302. return responsedata;
  303. SceneObjectPart rootpart = obj.RootPart;
  304. rootpart.Shape = pbs;
  305. rootpart.Flags |= (PrimFlags)flags;
  306. rootpart.EveryoneMask = everyone_mask;
  307. rootpart.GroupID = group_id;
  308. rootpart.GroupMask = group_mask;
  309. rootpart.NextOwnerMask = next_owner_mask;
  310. rootpart.Material = (byte)material;
  311. obj.InvalidateDeepEffectivePerms();
  312. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  313. responsedata["int_response_code"] = 200; //501; //410; //404;
  314. responsedata["content_type"] = "text/plain";
  315. responsedata["keepalive"] = false;
  316. responsedata["str_response_string"] = String.Format("<llsd><map><key>local_id</key>{0}</map></llsd>", ConvertUintToBytes(obj.LocalId));
  317. return responsedata;
  318. }
  319. private uint ReadUIntVal(OSD obj)
  320. {
  321. byte[] tmp = obj.AsBinary();
  322. if (BitConverter.IsLittleEndian)
  323. Array.Reverse(tmp);
  324. return Utils.BytesToUInt(tmp);
  325. }
  326. private string ConvertUintToBytes(uint val)
  327. {
  328. byte[] resultbytes = Utils.UIntToBytes(val);
  329. if (BitConverter.IsLittleEndian)
  330. Array.Reverse(resultbytes);
  331. return String.Format("<binary encoding=\"base64\">{0}</binary>", Convert.ToBase64String(resultbytes));
  332. }
  333. }
  334. }