1
0

UploadObjectAssetModule.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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", Id = "UploadObjectAssetModule")]
  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 Region Module interfaceBase 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 Region Module interface
  81. public void Close() { }
  82. public string Name { get { return "UploadObjectAssetModuleModule"; } }
  83. public void RegisterCaps(UUID agentID, Caps caps)
  84. {
  85. string capUrl = "/CAPS/" + UUID.Random() + "/";
  86. // m_log.Debug("[UPLOAD OBJECT ASSET MODULE]: /CAPS/" + capID);
  87. caps.RegisterHandler(
  88. "UploadObjectAsset",
  89. new RestHTTPHandler(
  90. "POST",
  91. capUrl,
  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. pbs.LastAttachPoint = (byte) 0;
  236. SceneObjectPart prim = new SceneObjectPart();
  237. prim.UUID = UUID.Random();
  238. prim.CreatorID = AgentId;
  239. prim.OwnerID = AgentId;
  240. prim.GroupID = obj.GroupID;
  241. prim.LastOwnerID = prim.OwnerID;
  242. prim.RezzerID = AgentId;
  243. prim.CreationDate = Util.UnixTimeSinceEpoch();
  244. prim.Name = obj.Name;
  245. prim.Description = "";
  246. prim.PayPrice[0] = -2;
  247. prim.PayPrice[1] = -2;
  248. prim.PayPrice[2] = -2;
  249. prim.PayPrice[3] = -2;
  250. prim.PayPrice[4] = -2;
  251. Primitive.TextureEntry tmp =
  252. new Primitive.TextureEntry(UUID.Parse("89556747-24cb-43ed-920b-47caed15465f"));
  253. for (int j = 0; j < obj.Faces.Length; j++)
  254. {
  255. UploadObjectAssetMessage.Object.Face face = obj.Faces[j];
  256. Primitive.TextureEntryFace primFace = tmp.CreateFace((uint) j);
  257. primFace.Bump = face.Bump;
  258. primFace.RGBA = face.Color;
  259. primFace.Fullbright = face.Fullbright;
  260. primFace.Glow = face.Glow;
  261. primFace.TextureID = face.ImageID;
  262. primFace.Rotation = face.ImageRot;
  263. primFace.MediaFlags = ((face.MediaFlags & 1) != 0);
  264. primFace.OffsetU = face.OffsetS;
  265. primFace.OffsetV = face.OffsetT;
  266. primFace.RepeatU = face.ScaleS;
  267. primFace.RepeatV = face.ScaleT;
  268. primFace.TexMapType = (MappingType) (face.MediaFlags & 6);
  269. }
  270. pbs.TextureEntry = tmp.GetBytes();
  271. prim.Shape = pbs;
  272. prim.Scale = obj.Scale;
  273. SceneObjectGroup grp = new SceneObjectGroup();
  274. grp.SetRootPart(prim);
  275. prim.ParentID = 0;
  276. if (i == 0)
  277. {
  278. rootGroup = grp;
  279. }
  280. grp.AttachToScene(m_scene);
  281. grp.AbsolutePosition = obj.Position;
  282. prim.RotationOffset = obj.Rotation;
  283. // Required for linking
  284. grp.RootPart.ClearUpdateSchedule();
  285. if (m_scene.Permissions.CanRezObject(1, avatar.UUID, pos))
  286. {
  287. m_scene.AddSceneObject(grp);
  288. grp.AbsolutePosition = obj.Position;
  289. }
  290. allparts[i] = grp;
  291. }
  292. for (int j = 1; j < allparts.Length; j++)
  293. {
  294. // Required for linking
  295. rootGroup.RootPart.ClearUpdateSchedule();
  296. allparts[j].RootPart.ClearUpdateSchedule();
  297. rootGroup.LinkToGroup(allparts[j]);
  298. }
  299. rootGroup.ScheduleGroupForFullAnimUpdate();
  300. pos
  301. = m_scene.GetNewRezLocation(
  302. Vector3.Zero, rootpos, UUID.Zero, rot, (byte)1, 1, true, allparts[0].GroupScale, false);
  303. responsedata["int_response_code"] = 200; //501; //410; //404;
  304. responsedata["content_type"] = "text/plain";
  305. responsedata["keepalive"] = false;
  306. responsedata["str_response_string"] = String.Format("<llsd><map><key>local_id</key>{0}</map></llsd>", ConvertUintToBytes(allparts[0].LocalId));
  307. return responsedata;
  308. }
  309. private string ConvertUintToBytes(uint val)
  310. {
  311. byte[] resultbytes = Utils.UIntToBytes(val);
  312. if (BitConverter.IsLittleEndian)
  313. Array.Reverse(resultbytes);
  314. return String.Format("<binary encoding=\"base64\">{0}</binary>", Convert.ToBase64String(resultbytes));
  315. }
  316. }
  317. }