ObjectAdd.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. UUID capuuid = UUID.Random();
  84. // m_log.InfoFormat("[OBJECTADD]: {0}", "/CAPS/OA/" + capuuid + "/");
  85. caps.RegisterHandler(
  86. "ObjectAdd",
  87. new RestHTTPHandler(
  88. "POST",
  89. "/CAPS/OA/" + capuuid + "/",
  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. //UUID session_id = UUID.Zero;
  106. bool bypass_raycast = false;
  107. uint everyone_mask = 0;
  108. uint group_mask = 0;
  109. uint next_owner_mask = 0;
  110. uint flags = 0;
  111. UUID group_id = UUID.Zero;
  112. int hollow = 0;
  113. int material = 0;
  114. int p_code = 0;
  115. int path_begin = 0;
  116. int path_curve = 0;
  117. int path_end = 0;
  118. int path_radius_offset = 0;
  119. int path_revolutions = 0;
  120. int path_scale_x = 0;
  121. int path_scale_y = 0;
  122. int path_shear_x = 0;
  123. int path_shear_y = 0;
  124. int path_skew = 0;
  125. int path_taper_x = 0;
  126. int path_taper_y = 0;
  127. int path_twist = 0;
  128. int path_twist_begin = 0;
  129. int profile_begin = 0;
  130. int profile_curve = 0;
  131. int profile_end = 0;
  132. Vector3 ray_end = Vector3.Zero;
  133. bool ray_end_is_intersection = false;
  134. Vector3 ray_start = Vector3.Zero;
  135. UUID ray_target_id = UUID.Zero;
  136. Quaternion rotation = Quaternion.Identity;
  137. Vector3 scale = Vector3.Zero;
  138. int state = 0;
  139. int lastattach = 0;
  140. if (r.Type != OSDType.Map) // not a proper req
  141. return responsedata;
  142. OSDMap rm = (OSDMap)r;
  143. if (rm.ContainsKey("ObjectData")) //v2
  144. {
  145. if (rm["ObjectData"].Type != OSDType.Map)
  146. {
  147. responsedata["str_response_string"] = "Has ObjectData key, but data not in expected format";
  148. return responsedata;
  149. }
  150. OSDMap ObjMap = (OSDMap)rm["ObjectData"];
  151. bypass_raycast = ObjMap["BypassRaycast"].AsBoolean();
  152. everyone_mask = readuintval(ObjMap["EveryoneMask"]);
  153. flags = readuintval(ObjMap["Flags"]);
  154. group_mask = readuintval(ObjMap["GroupMask"]);
  155. material = ObjMap["Material"].AsInteger();
  156. next_owner_mask = readuintval(ObjMap["NextOwnerMask"]);
  157. p_code = ObjMap["PCode"].AsInteger();
  158. if (ObjMap.ContainsKey("Path"))
  159. {
  160. if (ObjMap["Path"].Type != OSDType.Map)
  161. {
  162. responsedata["str_response_string"] = "Has Path key, but data not in expected format";
  163. return responsedata;
  164. }
  165. OSDMap PathMap = (OSDMap)ObjMap["Path"];
  166. path_begin = PathMap["Begin"].AsInteger();
  167. path_curve = PathMap["Curve"].AsInteger();
  168. path_end = PathMap["End"].AsInteger();
  169. path_radius_offset = PathMap["RadiusOffset"].AsInteger();
  170. path_revolutions = PathMap["Revolutions"].AsInteger();
  171. path_scale_x = PathMap["ScaleX"].AsInteger();
  172. path_scale_y = PathMap["ScaleY"].AsInteger();
  173. path_shear_x = PathMap["ShearX"].AsInteger();
  174. path_shear_y = PathMap["ShearY"].AsInteger();
  175. path_skew = PathMap["Skew"].AsInteger();
  176. path_taper_x = PathMap["TaperX"].AsInteger();
  177. path_taper_y = PathMap["TaperY"].AsInteger();
  178. path_twist = PathMap["Twist"].AsInteger();
  179. path_twist_begin = PathMap["TwistBegin"].AsInteger();
  180. }
  181. if (ObjMap.ContainsKey("Profile"))
  182. {
  183. if (ObjMap["Profile"].Type != OSDType.Map)
  184. {
  185. responsedata["str_response_string"] = "Has Profile key, but data not in expected format";
  186. return responsedata;
  187. }
  188. OSDMap ProfileMap = (OSDMap)ObjMap["Profile"];
  189. profile_begin = ProfileMap["Begin"].AsInteger();
  190. profile_curve = ProfileMap["Curve"].AsInteger();
  191. profile_end = ProfileMap["End"].AsInteger();
  192. hollow = ProfileMap["Hollow"].AsInteger();
  193. }
  194. ray_end_is_intersection = ObjMap["RayEndIsIntersection"].AsBoolean();
  195. ray_target_id = ObjMap["RayTargetId"].AsUUID();
  196. state = ObjMap["State"].AsInteger();
  197. lastattach = ObjMap["LastAttachPoint"].AsInteger();
  198. try
  199. {
  200. ray_end = ((OSDArray)ObjMap["RayEnd"]).AsVector3();
  201. ray_start = ((OSDArray)ObjMap["RayStart"]).AsVector3();
  202. scale = ((OSDArray)ObjMap["Scale"]).AsVector3();
  203. rotation = ((OSDArray)ObjMap["Rotation"]).AsQuaternion();
  204. }
  205. catch (Exception)
  206. {
  207. responsedata["str_response_string"] = "RayEnd, RayStart, Scale or Rotation wasn't in the expected format";
  208. return responsedata;
  209. }
  210. if (rm.ContainsKey("AgentData"))
  211. {
  212. if (rm["AgentData"].Type != OSDType.Map)
  213. {
  214. responsedata["str_response_string"] = "Has AgentData key, but data not in expected format";
  215. return responsedata;
  216. }
  217. OSDMap AgentDataMap = (OSDMap)rm["AgentData"];
  218. //session_id = AgentDataMap["SessionId"].AsUUID();
  219. group_id = AgentDataMap["GroupId"].AsUUID();
  220. }
  221. }
  222. else
  223. { //v1
  224. bypass_raycast = rm["bypass_raycast"].AsBoolean();
  225. everyone_mask = readuintval(rm["everyone_mask"]);
  226. flags = readuintval(rm["flags"]);
  227. group_id = rm["group_id"].AsUUID();
  228. group_mask = readuintval(rm["group_mask"]);
  229. hollow = rm["hollow"].AsInteger();
  230. material = rm["material"].AsInteger();
  231. next_owner_mask = readuintval(rm["next_owner_mask"]);
  232. hollow = rm["hollow"].AsInteger();
  233. p_code = rm["p_code"].AsInteger();
  234. path_begin = rm["path_begin"].AsInteger();
  235. path_curve = rm["path_curve"].AsInteger();
  236. path_end = rm["path_end"].AsInteger();
  237. path_radius_offset = rm["path_radius_offset"].AsInteger();
  238. path_revolutions = rm["path_revolutions"].AsInteger();
  239. path_scale_x = rm["path_scale_x"].AsInteger();
  240. path_scale_y = rm["path_scale_y"].AsInteger();
  241. path_shear_x = rm["path_shear_x"].AsInteger();
  242. path_shear_y = rm["path_shear_y"].AsInteger();
  243. path_skew = rm["path_skew"].AsInteger();
  244. path_taper_x = rm["path_taper_x"].AsInteger();
  245. path_taper_y = rm["path_taper_y"].AsInteger();
  246. path_twist = rm["path_twist"].AsInteger();
  247. path_twist_begin = rm["path_twist_begin"].AsInteger();
  248. profile_begin = rm["profile_begin"].AsInteger();
  249. profile_curve = rm["profile_curve"].AsInteger();
  250. profile_end = rm["profile_end"].AsInteger();
  251. ray_end_is_intersection = rm["ray_end_is_intersection"].AsBoolean();
  252. ray_target_id = rm["ray_target_id"].AsUUID();
  253. //session_id = rm["session_id"].AsUUID();
  254. state = rm["state"].AsInteger();
  255. lastattach = rm["last_attach_point"].AsInteger();
  256. try
  257. {
  258. ray_end = ((OSDArray)rm["ray_end"]).AsVector3();
  259. ray_start = ((OSDArray)rm["ray_start"]).AsVector3();
  260. rotation = ((OSDArray)rm["rotation"]).AsQuaternion();
  261. scale = ((OSDArray)rm["scale"]).AsVector3();
  262. }
  263. catch (Exception)
  264. {
  265. responsedata["str_response_string"] = "RayEnd, RayStart, Scale or Rotation wasn't in the expected format";
  266. return responsedata;
  267. }
  268. }
  269. 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);
  270. PrimitiveBaseShape pbs = PrimitiveBaseShape.CreateBox();
  271. pbs.PathBegin = (ushort)path_begin;
  272. pbs.PathCurve = (byte)path_curve;
  273. pbs.PathEnd = (ushort)path_end;
  274. pbs.PathRadiusOffset = (sbyte)path_radius_offset;
  275. pbs.PathRevolutions = (byte)path_revolutions;
  276. pbs.PathScaleX = (byte)path_scale_x;
  277. pbs.PathScaleY = (byte)path_scale_y;
  278. pbs.PathShearX = (byte)path_shear_x;
  279. pbs.PathShearY = (byte)path_shear_y;
  280. pbs.PathSkew = (sbyte)path_skew;
  281. pbs.PathTaperX = (sbyte)path_taper_x;
  282. pbs.PathTaperY = (sbyte)path_taper_y;
  283. pbs.PathTwist = (sbyte)path_twist;
  284. pbs.PathTwistBegin = (sbyte)path_twist_begin;
  285. pbs.HollowShape = (HollowShape)hollow;
  286. pbs.PCode = (byte)p_code;
  287. pbs.ProfileBegin = (ushort)profile_begin;
  288. pbs.ProfileCurve = (byte)profile_curve;
  289. pbs.ProfileEnd = (ushort)profile_end;
  290. pbs.Scale = scale;
  291. pbs.State = (byte)state;
  292. pbs.LastAttachPoint = (byte)lastattach;
  293. SceneObjectGroup obj = null; ;
  294. if (m_scene.Permissions.CanRezObject(1, avatar.UUID, pos))
  295. {
  296. // rez ON the ground, not IN the ground
  297. // pos.Z += 0.25F;
  298. obj = m_scene.AddNewPrim(avatar.UUID, group_id, pos, rotation, pbs);
  299. }
  300. if (obj == null)
  301. return responsedata;
  302. SceneObjectPart rootpart = obj.RootPart;
  303. rootpart.Shape = pbs;
  304. rootpart.Flags |= (PrimFlags)flags;
  305. rootpart.EveryoneMask = everyone_mask;
  306. rootpart.GroupID = group_id;
  307. rootpart.GroupMask = group_mask;
  308. rootpart.NextOwnerMask = next_owner_mask;
  309. rootpart.Material = (byte)material;
  310. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  311. responsedata["int_response_code"] = 200; //501; //410; //404;
  312. responsedata["content_type"] = "text/plain";
  313. responsedata["keepalive"] = false;
  314. responsedata["str_response_string"] = String.Format("<llsd><map><key>local_id</key>{0}</map></llsd>", ConvertUintToBytes(obj.LocalId));
  315. return responsedata;
  316. }
  317. private uint readuintval(OSD obj)
  318. {
  319. byte[] tmp = obj.AsBinary();
  320. if (BitConverter.IsLittleEndian)
  321. Array.Reverse(tmp);
  322. return Utils.BytesToUInt(tmp);
  323. }
  324. private string ConvertUintToBytes(uint val)
  325. {
  326. byte[] resultbytes = Utils.UIntToBytes(val);
  327. if (BitConverter.IsLittleEndian)
  328. Array.Reverse(resultbytes);
  329. return String.Format("<binary encoding=\"base64\">{0}</binary>", Convert.ToBase64String(resultbytes));
  330. }
  331. }
  332. }