1
0

ObjectAdd.cs 15 KB

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