1
0

UploadObjectAssetModule.cs 15 KB

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