ObjectAdd.cs 15 KB

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