UploadObjectAssetModule.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 Mono.Addins;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenMetaverse.Messages.Linden;
  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. using OSD = OpenMetaverse.StructuredData.OSD;
  41. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  42. using ExtraParamType = OpenMetaverse.ExtraParamType;
  43. namespace OpenSim.Region.ClientStack.Linden
  44. {
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UploadObjectAssetModule")]
  46. public class UploadObjectAssetModule : INonSharedRegionModule
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private Scene m_scene;
  50. #region Region Module interfaceBase Members
  51. public Type ReplaceableInterface
  52. {
  53. get { return null; }
  54. }
  55. public void Initialise(IConfigSource source)
  56. {
  57. }
  58. public void AddRegion(Scene pScene)
  59. {
  60. m_scene = pScene;
  61. }
  62. public void RemoveRegion(Scene scene)
  63. {
  64. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  65. m_scene = null;
  66. }
  67. public void RegionLoaded(Scene scene)
  68. {
  69. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  70. }
  71. #endregion
  72. #region Region Module interface
  73. public void Close() { }
  74. public string Name { get { return "UploadObjectAssetModuleModule"; } }
  75. public void RegisterCaps(UUID agentID, Caps caps)
  76. {
  77. caps.RegisterSimpleHandler("UploadObjectAsset",
  78. new SimpleOSDMapHandler("POST","/" + UUID.Random(), delegate (IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map)
  79. {
  80. ProcessAdd(httpRequest, httpResponse, map, agentID, caps);
  81. }));
  82. // m_log.Debug("[UPLOAD OBJECT ASSET MODULE]: /CAPS/" + capID);
  83. }
  84. #endregion
  85. /// <summary>
  86. /// Parses add request
  87. /// </summary>
  88. /// <param name="request"></param>
  89. /// <param name="agentID"></param>
  90. /// <param name="cap"></param>
  91. /// <returns></returns>
  92. public void ProcessAdd(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap map, UUID agentID, Caps cap)
  93. {
  94. httpResponse.KeepAlive = false;
  95. if (!m_scene.TryGetScenePresence(agentID, out ScenePresence avatar))
  96. {
  97. httpResponse.StatusCode = (int)HttpStatusCode.Gone;
  98. return;
  99. }
  100. UploadObjectAssetMessage message = new UploadObjectAssetMessage();
  101. try
  102. {
  103. message.Deserialize(map);
  104. }
  105. catch (Exception ex)
  106. {
  107. m_log.Error("[UPLOAD OBJECT ASSET MODULE]: Error deserializing message " + ex.ToString());
  108. message = null;
  109. }
  110. if (message == null)
  111. {
  112. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  113. return;
  114. }
  115. try
  116. {
  117. Vector3 pos = avatar.AbsolutePosition + Vector3.UnitXRotated(avatar.Rotation);
  118. Quaternion rot = Quaternion.Identity;
  119. Vector3 rootpos = Vector3.Zero;
  120. SceneObjectGroup rootGroup = null;
  121. SceneObjectGroup[] allparts = new SceneObjectGroup[message.Objects.Length];
  122. for (int i = 0; i < message.Objects.Length; i++)
  123. {
  124. UploadObjectAssetMessage.Object obj = message.Objects[i];
  125. PrimitiveBaseShape pbs = PrimitiveBaseShape.CreateBox();
  126. if (i == 0)
  127. rootpos = obj.Position;
  128. for (int extparams = 0; extparams < obj.ExtraParams.Length; extparams++)
  129. {
  130. UploadObjectAssetMessage.Object.ExtraParam extraParam = obj.ExtraParams[extparams];
  131. switch ((ushort)extraParam.Type)
  132. {
  133. case (ushort)ExtraParamType.Sculpt:
  134. Primitive.SculptData sculpt = new Primitive.SculptData(extraParam.ExtraParamData, 0);
  135. pbs.SculptEntry = true;
  136. pbs.SculptTexture = obj.SculptID;
  137. pbs.SculptType = (byte)sculpt.Type;
  138. break;
  139. case (ushort)ExtraParamType.Flexible:
  140. Primitive.FlexibleData flex = new Primitive.FlexibleData(extraParam.ExtraParamData, 0);
  141. pbs.FlexiEntry = true;
  142. pbs.FlexiDrag = flex.Drag;
  143. pbs.FlexiForceX = flex.Force.X;
  144. pbs.FlexiForceY = flex.Force.Y;
  145. pbs.FlexiForceZ = flex.Force.Z;
  146. pbs.FlexiGravity = flex.Gravity;
  147. pbs.FlexiSoftness = flex.Softness;
  148. pbs.FlexiTension = flex.Tension;
  149. pbs.FlexiWind = flex.Wind;
  150. break;
  151. case (ushort)ExtraParamType.Light:
  152. Primitive.LightData light = new Primitive.LightData(extraParam.ExtraParamData, 0);
  153. pbs.LightColorA = light.Color.A;
  154. pbs.LightColorB = light.Color.B;
  155. pbs.LightColorG = light.Color.G;
  156. pbs.LightColorR = light.Color.R;
  157. pbs.LightCutoff = light.Cutoff;
  158. pbs.LightEntry = true;
  159. pbs.LightFalloff = light.Falloff;
  160. pbs.LightIntensity = light.Intensity;
  161. pbs.LightRadius = light.Radius;
  162. break;
  163. case 0x40:
  164. pbs.ReadProjectionData(extraParam.ExtraParamData, 0);
  165. break;
  166. }
  167. }
  168. pbs.PathBegin = (ushort) obj.PathBegin;
  169. pbs.PathCurve = (byte) obj.PathCurve;
  170. pbs.PathEnd = (ushort) obj.PathEnd;
  171. pbs.PathRadiusOffset = (sbyte) obj.RadiusOffset;
  172. pbs.PathRevolutions = (byte) obj.Revolutions;
  173. pbs.PathScaleX = (byte) obj.ScaleX;
  174. pbs.PathScaleY = (byte) obj.ScaleY;
  175. pbs.PathShearX = (byte) obj.ShearX;
  176. pbs.PathShearY = (byte) obj.ShearY;
  177. pbs.PathSkew = (sbyte) obj.Skew;
  178. pbs.PathTaperX = (sbyte) obj.TaperX;
  179. pbs.PathTaperY = (sbyte) obj.TaperY;
  180. pbs.PathTwist = (sbyte) obj.Twist;
  181. pbs.PathTwistBegin = (sbyte) obj.TwistBegin;
  182. pbs.HollowShape = (HollowShape) obj.ProfileHollow;
  183. pbs.PCode = (byte) PCode.Prim;
  184. pbs.ProfileBegin = (ushort) obj.ProfileBegin;
  185. pbs.ProfileCurve = (byte) obj.ProfileCurve;
  186. pbs.ProfileEnd = (ushort) obj.ProfileEnd;
  187. pbs.Scale = obj.Scale;
  188. pbs.State = (byte) 0;
  189. pbs.LastAttachPoint = (byte) 0;
  190. SceneObjectPart prim = new SceneObjectPart();
  191. prim.UUID = UUID.Random();
  192. prim.CreatorID = agentID;
  193. prim.OwnerID = agentID;
  194. prim.GroupID = obj.GroupID;
  195. prim.LastOwnerID = prim.OwnerID;
  196. prim.RezzerID = agentID;
  197. prim.CreationDate = Util.UnixTimeSinceEpoch();
  198. prim.Name = obj.Name;
  199. prim.Description = "";
  200. prim.PayPrice[0] = -2;
  201. prim.PayPrice[1] = -2;
  202. prim.PayPrice[2] = -2;
  203. prim.PayPrice[3] = -2;
  204. prim.PayPrice[4] = -2;
  205. Primitive.TextureEntry tmp =
  206. new Primitive.TextureEntry(UUID.Parse("89556747-24cb-43ed-920b-47caed15465f"));
  207. for (int j = 0; j < obj.Faces.Length; j++)
  208. {
  209. UploadObjectAssetMessage.Object.Face face = obj.Faces[j];
  210. Primitive.TextureEntryFace primFace = tmp.CreateFace((uint) j);
  211. primFace.Bump = face.Bump;
  212. primFace.RGBA = face.Color;
  213. primFace.Fullbright = face.Fullbright;
  214. primFace.Glow = face.Glow;
  215. primFace.TextureID = face.ImageID;
  216. primFace.Rotation = face.ImageRot;
  217. primFace.MediaFlags = ((face.MediaFlags & 1) != 0);
  218. primFace.OffsetU = face.OffsetS;
  219. primFace.OffsetV = face.OffsetT;
  220. primFace.RepeatU = face.ScaleS;
  221. primFace.RepeatV = face.ScaleT;
  222. primFace.TexMapType = (MappingType) (face.MediaFlags & 6);
  223. }
  224. pbs.TextureEntry = tmp.GetBytes();
  225. prim.Shape = pbs;
  226. prim.Scale = obj.Scale;
  227. SceneObjectGroup grp = new SceneObjectGroup();
  228. grp.SetRootPart(prim);
  229. prim.ParentID = 0;
  230. if (i == 0)
  231. rootGroup = grp;
  232. grp.AttachToScene(m_scene);
  233. grp.AbsolutePosition = obj.Position;
  234. prim.RotationOffset = obj.Rotation;
  235. // Required for linking
  236. grp.RootPart.ClearUpdateSchedule();
  237. if (m_scene.Permissions.CanRezObject(1, avatar.UUID, pos))
  238. {
  239. m_scene.AddSceneObject(grp);
  240. grp.AbsolutePosition = obj.Position;
  241. }
  242. allparts[i] = grp;
  243. }
  244. for (int j = 1; j < allparts.Length; j++)
  245. {
  246. // Required for linking
  247. rootGroup.RootPart.ClearUpdateSchedule();
  248. allparts[j].RootPart.ClearUpdateSchedule();
  249. rootGroup.LinkToGroup(allparts[j]);
  250. }
  251. rootGroup.ScheduleGroupForUpdate(PrimUpdateFlags.FullUpdatewithAnimMatOvr);
  252. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  253. httpResponse.RawBuffer = Util.UTF8NBGetbytes(String.Format("<llsd><map><key>local_id</key>{0}</map></llsd>", ConvertUintToBytes(allparts[0].LocalId)));
  254. }
  255. catch{ }
  256. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  257. }
  258. private string ConvertUintToBytes(uint val)
  259. {
  260. byte[] resultbytes = Utils.UIntToBytes(val);
  261. if (BitConverter.IsLittleEndian)
  262. Array.Reverse(resultbytes);
  263. return String.Format("<binary encoding=\"base64\">{0}</binary>", Convert.ToBase64String(resultbytes));
  264. }
  265. }
  266. }