1
0

ObjectAdd.cs 14 KB

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