UploadObjectAssetModule.cs 13 KB

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