ObjectAdd.cs 15 KB

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