UploadObjectAssetModule.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.Collections.Specialized;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Web;
  33. using Mono.Addins;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenMetaverse.Messages.Linden;
  39. using OpenSim.Framework;
  40. using OpenSim.Framework.Servers;
  41. using OpenSim.Framework.Servers.HttpServer;
  42. using OpenSim.Region.Framework.Interfaces;
  43. using OpenSim.Region.Framework.Scenes;
  44. using OpenSim.Services.Interfaces;
  45. using Caps = OpenSim.Framework.Capabilities.Caps;
  46. using OSD = OpenMetaverse.StructuredData.OSD;
  47. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  48. using OpenSim.Framework.Capabilities;
  49. using ExtraParamType = OpenMetaverse.ExtraParamType;
  50. namespace OpenSim.Region.ClientStack.Linden
  51. {
  52. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  53. public class UploadObjectAssetModule : INonSharedRegionModule
  54. {
  55. private static readonly ILog m_log =
  56. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  57. private Scene m_scene;
  58. #region IRegionModuleBase Members
  59. public Type ReplaceableInterface
  60. {
  61. get { return null; }
  62. }
  63. public void Initialise(IConfigSource source)
  64. {
  65. }
  66. public void AddRegion(Scene pScene)
  67. {
  68. m_scene = pScene;
  69. }
  70. public void RemoveRegion(Scene scene)
  71. {
  72. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  73. m_scene = null;
  74. }
  75. public void RegionLoaded(Scene scene)
  76. {
  77. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  78. }
  79. #endregion
  80. #region IRegionModule Members
  81. public void Close() { }
  82. public string Name { get { return "UploadObjectAssetModuleModule"; } }
  83. public void RegisterCaps(UUID agentID, Caps caps)
  84. {
  85. UUID capID = UUID.Random();
  86. // m_log.Debug("[UPLOAD OBJECT ASSET MODULE]: /CAPS/" + capID);
  87. caps.RegisterHandler(
  88. "UploadObjectAsset",
  89. new RestHTTPHandler(
  90. "POST",
  91. "/CAPS/OA/" + capID + "/",
  92. httpMethod => ProcessAdd(httpMethod, agentID, caps),
  93. "UploadObjectAsset",
  94. agentID.ToString()));
  95. /*
  96. caps.RegisterHandler("NewFileAgentInventoryVariablePrice",
  97. new LLSDStreamhandler<LLSDAssetUploadRequest, LLSDNewFileAngentInventoryVariablePriceReplyResponse>("POST",
  98. "/CAPS/" + capID.ToString(),
  99. delegate(LLSDAssetUploadRequest req)
  100. {
  101. return NewAgentInventoryRequest(req,agentID);
  102. }));
  103. */
  104. }
  105. #endregion
  106. /// <summary>
  107. /// Parses add request
  108. /// </summary>
  109. /// <param name="request"></param>
  110. /// <param name="AgentId"></param>
  111. /// <param name="cap"></param>
  112. /// <returns></returns>
  113. public Hashtable ProcessAdd(Hashtable request, UUID AgentId, Caps cap)
  114. {
  115. Hashtable responsedata = new Hashtable();
  116. responsedata["int_response_code"] = 400; //501; //410; //404;
  117. responsedata["content_type"] = "text/plain";
  118. responsedata["keepalive"] = false;
  119. responsedata["str_response_string"] = "Request wasn't what was expected";
  120. ScenePresence avatar;
  121. if (!m_scene.TryGetScenePresence(AgentId, out avatar))
  122. return responsedata;
  123. OSDMap r = (OSDMap)OSDParser.Deserialize((string)request["requestbody"]);
  124. UploadObjectAssetMessage message = new UploadObjectAssetMessage();
  125. try
  126. {
  127. message.Deserialize(r);
  128. }
  129. catch (Exception ex)
  130. {
  131. m_log.Error("[UPLOAD OBJECT ASSET MODULE]: Error deserializing message " + ex.ToString());
  132. message = null;
  133. }
  134. if (message == null)
  135. {
  136. responsedata["int_response_code"] = 400; //501; //410; //404;
  137. responsedata["content_type"] = "text/plain";
  138. responsedata["keepalive"] = false;
  139. responsedata["str_response_string"] =
  140. "<llsd><map><key>error</key><string>Error parsing Object</string></map></llsd>";
  141. return responsedata;
  142. }
  143. Vector3 pos = avatar.AbsolutePosition + (Vector3.UnitX * avatar.Rotation);
  144. Quaternion rot = Quaternion.Identity;
  145. Vector3 rootpos = Vector3.Zero;
  146. // Quaternion rootrot = Quaternion.Identity;
  147. SceneObjectGroup rootGroup = null;
  148. SceneObjectGroup[] allparts = new SceneObjectGroup[message.Objects.Length];
  149. for (int i = 0; i < message.Objects.Length; i++)
  150. {
  151. UploadObjectAssetMessage.Object obj = message.Objects[i];
  152. PrimitiveBaseShape pbs = PrimitiveBaseShape.CreateBox();
  153. if (i == 0)
  154. {
  155. rootpos = obj.Position;
  156. // rootrot = obj.Rotation;
  157. }
  158. // Combine the extraparams data into it's ugly blob again....
  159. //int bytelength = 0;
  160. //for (int extparams = 0; extparams < obj.ExtraParams.Length; extparams++)
  161. //{
  162. // bytelength += obj.ExtraParams[extparams].ExtraParamData.Length;
  163. //}
  164. //byte[] extraparams = new byte[bytelength];
  165. //int position = 0;
  166. //for (int extparams = 0; extparams < obj.ExtraParams.Length; extparams++)
  167. //{
  168. // Buffer.BlockCopy(obj.ExtraParams[extparams].ExtraParamData, 0, extraparams, position,
  169. // obj.ExtraParams[extparams].ExtraParamData.Length);
  170. //
  171. // position += obj.ExtraParams[extparams].ExtraParamData.Length;
  172. // }
  173. //pbs.ExtraParams = extraparams;
  174. for (int extparams = 0; extparams < obj.ExtraParams.Length; extparams++)
  175. {
  176. UploadObjectAssetMessage.Object.ExtraParam extraParam = obj.ExtraParams[extparams];
  177. switch ((ushort)extraParam.Type)
  178. {
  179. case (ushort)ExtraParamType.Sculpt:
  180. Primitive.SculptData sculpt = new Primitive.SculptData(extraParam.ExtraParamData, 0);
  181. pbs.SculptEntry = true;
  182. pbs.SculptTexture = obj.SculptID;
  183. pbs.SculptType = (byte)sculpt.Type;
  184. break;
  185. case (ushort)ExtraParamType.Flexible:
  186. Primitive.FlexibleData flex = new Primitive.FlexibleData(extraParam.ExtraParamData, 0);
  187. pbs.FlexiEntry = true;
  188. pbs.FlexiDrag = flex.Drag;
  189. pbs.FlexiForceX = flex.Force.X;
  190. pbs.FlexiForceY = flex.Force.Y;
  191. pbs.FlexiForceZ = flex.Force.Z;
  192. pbs.FlexiGravity = flex.Gravity;
  193. pbs.FlexiSoftness = flex.Softness;
  194. pbs.FlexiTension = flex.Tension;
  195. pbs.FlexiWind = flex.Wind;
  196. break;
  197. case (ushort)ExtraParamType.Light:
  198. Primitive.LightData light = new Primitive.LightData(extraParam.ExtraParamData, 0);
  199. pbs.LightColorA = light.Color.A;
  200. pbs.LightColorB = light.Color.B;
  201. pbs.LightColorG = light.Color.G;
  202. pbs.LightColorR = light.Color.R;
  203. pbs.LightCutoff = light.Cutoff;
  204. pbs.LightEntry = true;
  205. pbs.LightFalloff = light.Falloff;
  206. pbs.LightIntensity = light.Intensity;
  207. pbs.LightRadius = light.Radius;
  208. break;
  209. case 0x40:
  210. pbs.ReadProjectionData(extraParam.ExtraParamData, 0);
  211. break;
  212. }
  213. }
  214. pbs.PathBegin = (ushort) obj.PathBegin;
  215. pbs.PathCurve = (byte) obj.PathCurve;
  216. pbs.PathEnd = (ushort) obj.PathEnd;
  217. pbs.PathRadiusOffset = (sbyte) obj.RadiusOffset;
  218. pbs.PathRevolutions = (byte) obj.Revolutions;
  219. pbs.PathScaleX = (byte) obj.ScaleX;
  220. pbs.PathScaleY = (byte) obj.ScaleY;
  221. pbs.PathShearX = (byte) obj.ShearX;
  222. pbs.PathShearY = (byte) obj.ShearY;
  223. pbs.PathSkew = (sbyte) obj.Skew;
  224. pbs.PathTaperX = (sbyte) obj.TaperX;
  225. pbs.PathTaperY = (sbyte) obj.TaperY;
  226. pbs.PathTwist = (sbyte) obj.Twist;
  227. pbs.PathTwistBegin = (sbyte) obj.TwistBegin;
  228. pbs.HollowShape = (HollowShape) obj.ProfileHollow;
  229. pbs.PCode = (byte) PCode.Prim;
  230. pbs.ProfileBegin = (ushort) obj.ProfileBegin;
  231. pbs.ProfileCurve = (byte) obj.ProfileCurve;
  232. pbs.ProfileEnd = (ushort) obj.ProfileEnd;
  233. pbs.Scale = obj.Scale;
  234. pbs.State = (byte) 0;
  235. SceneObjectPart prim = new SceneObjectPart();
  236. prim.UUID = UUID.Random();
  237. prim.CreatorID = AgentId;
  238. prim.OwnerID = AgentId;
  239. prim.GroupID = obj.GroupID;
  240. prim.LastOwnerID = prim.OwnerID;
  241. prim.CreationDate = Util.UnixTimeSinceEpoch();
  242. prim.Name = obj.Name;
  243. prim.Description = "";
  244. prim.PayPrice[0] = -2;
  245. prim.PayPrice[1] = -2;
  246. prim.PayPrice[2] = -2;
  247. prim.PayPrice[3] = -2;
  248. prim.PayPrice[4] = -2;
  249. Primitive.TextureEntry tmp =
  250. new Primitive.TextureEntry(UUID.Parse("89556747-24cb-43ed-920b-47caed15465f"));
  251. for (int j = 0; j < obj.Faces.Length; j++)
  252. {
  253. UploadObjectAssetMessage.Object.Face face = obj.Faces[j];
  254. Primitive.TextureEntryFace primFace = tmp.CreateFace((uint) j);
  255. primFace.Bump = face.Bump;
  256. primFace.RGBA = face.Color;
  257. primFace.Fullbright = face.Fullbright;
  258. primFace.Glow = face.Glow;
  259. primFace.TextureID = face.ImageID;
  260. primFace.Rotation = face.ImageRot;
  261. primFace.MediaFlags = ((face.MediaFlags & 1) != 0);
  262. primFace.OffsetU = face.OffsetS;
  263. primFace.OffsetV = face.OffsetT;
  264. primFace.RepeatU = face.ScaleS;
  265. primFace.RepeatV = face.ScaleT;
  266. primFace.TexMapType = (MappingType) (face.MediaFlags & 6);
  267. }
  268. pbs.TextureEntry = tmp.GetBytes();
  269. prim.Shape = pbs;
  270. prim.Scale = obj.Scale;
  271. SceneObjectGroup grp = new SceneObjectGroup();
  272. grp.SetRootPart(prim);
  273. prim.ParentID = 0;
  274. if (i == 0)
  275. {
  276. rootGroup = grp;
  277. }
  278. grp.AttachToScene(m_scene);
  279. grp.AbsolutePosition = obj.Position;
  280. prim.RotationOffset = obj.Rotation;
  281. // Required for linking
  282. grp.RootPart.ClearUpdateSchedule();
  283. if (m_scene.Permissions.CanRezObject(1, avatar.UUID, pos))
  284. {
  285. m_scene.AddSceneObject(grp);
  286. grp.AbsolutePosition = obj.Position;
  287. }
  288. allparts[i] = grp;
  289. }
  290. for (int j = 1; j < allparts.Length; j++)
  291. {
  292. // Required for linking
  293. rootGroup.RootPart.ClearUpdateSchedule();
  294. allparts[j].RootPart.ClearUpdateSchedule();
  295. rootGroup.LinkToGroup(allparts[j]);
  296. }
  297. rootGroup.ScheduleGroupForFullUpdate();
  298. pos
  299. = m_scene.GetNewRezLocation(
  300. Vector3.Zero, rootpos, UUID.Zero, rot, (byte)1, 1, true, allparts[0].GroupScale, false);
  301. responsedata["int_response_code"] = 200; //501; //410; //404;
  302. responsedata["content_type"] = "text/plain";
  303. responsedata["keepalive"] = false;
  304. responsedata["str_response_string"] = String.Format("<llsd><map><key>local_id</key>{0}</map></llsd>", ConvertUintToBytes(allparts[0].LocalId));
  305. return responsedata;
  306. }
  307. private string ConvertUintToBytes(uint val)
  308. {
  309. byte[] resultbytes = Utils.UIntToBytes(val);
  310. if (BitConverter.IsLittleEndian)
  311. Array.Reverse(resultbytes);
  312. return String.Format("<binary encoding=\"base64\">{0}</binary>", Convert.ToBase64String(resultbytes));
  313. }
  314. }
  315. }