1
0

UploadBakedTextureModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.Generic;
  30. using System.Collections.Specialized;
  31. using System.Drawing;
  32. using System.Drawing.Imaging;
  33. using System.Reflection;
  34. using System.IO;
  35. using System.Web;
  36. using log4net;
  37. using Nini.Config;
  38. using Mono.Addins;
  39. using OpenMetaverse;
  40. using OpenMetaverse.StructuredData;
  41. using OpenMetaverse.Imaging;
  42. using OpenSim.Framework;
  43. using OpenSim.Framework.Servers;
  44. using OpenSim.Framework.Servers.HttpServer;
  45. using OpenSim.Region.Framework.Interfaces;
  46. using OpenSim.Region.Framework.Scenes;
  47. using OpenSim.Services.Interfaces;
  48. using Caps = OpenSim.Framework.Capabilities.Caps;
  49. using OpenSim.Capabilities.Handlers;
  50. namespace OpenSim.Region.ClientStack.Linden
  51. {
  52. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UploadBakedTextureModule")]
  53. public class UploadBakedTextureModule : INonSharedRegionModule
  54. {
  55. private static readonly ILog m_log =
  56. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  57. /// <summary>
  58. /// For historical reasons this is fixed, but there
  59. /// </summary>
  60. private static readonly string m_uploadBakedTexturePath = "0010/";// This is in the LandManagementModule.
  61. private Scene m_scene;
  62. private bool m_persistBakedTextures;
  63. private IBakedTextureModule m_BakedTextureModule;
  64. public void Initialise(IConfigSource source)
  65. {
  66. IConfig appearanceConfig = source.Configs["Appearance"];
  67. if (appearanceConfig != null)
  68. m_persistBakedTextures = appearanceConfig.GetBoolean("PersistBakedTextures", m_persistBakedTextures);
  69. }
  70. public void AddRegion(Scene s)
  71. {
  72. m_scene = s;
  73. }
  74. public void RemoveRegion(Scene s)
  75. {
  76. s.EventManager.OnRegisterCaps -= RegisterCaps;
  77. s.EventManager.OnNewPresence -= RegisterNewPresence;
  78. s.EventManager.OnRemovePresence -= DeRegisterPresence;
  79. m_BakedTextureModule = null;
  80. m_scene = null;
  81. }
  82. public void RegionLoaded(Scene s)
  83. {
  84. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  85. m_scene.EventManager.OnNewPresence += RegisterNewPresence;
  86. m_scene.EventManager.OnRemovePresence += DeRegisterPresence;
  87. }
  88. private void DeRegisterPresence(UUID agentId)
  89. {
  90. ScenePresence presence = null;
  91. if (m_scene.TryGetScenePresence(agentId, out presence))
  92. {
  93. presence.ControllingClient.OnSetAppearance -= CaptureAppearanceSettings;
  94. }
  95. }
  96. private void RegisterNewPresence(ScenePresence presence)
  97. {
  98. presence.ControllingClient.OnSetAppearance += CaptureAppearanceSettings;
  99. }
  100. private void CaptureAppearanceSettings(IClientAPI remoteClient, Primitive.TextureEntry textureEntry, byte[] visualParams, Vector3 avSize, WearableCacheItem[] cacheItems)
  101. {
  102. int maxCacheitemsLoop = cacheItems.Length;
  103. if (maxCacheitemsLoop > AvatarWearable.MAX_WEARABLES)
  104. {
  105. maxCacheitemsLoop = AvatarWearable.MAX_WEARABLES;
  106. m_log.WarnFormat("[CACHEDBAKES]: Too Many Cache items Provided {0}, the max is {1}. Truncating!", cacheItems.Length, AvatarWearable.MAX_WEARABLES);
  107. }
  108. m_BakedTextureModule = m_scene.RequestModuleInterface<IBakedTextureModule>();
  109. if (cacheItems.Length > 0)
  110. {
  111. // m_log.Debug("[Cacheitems]: " + cacheItems.Length);
  112. // for (int iter = 0; iter < maxCacheitemsLoop; iter++)
  113. // {
  114. // m_log.Debug("[Cacheitems] {" + iter + "/" + cacheItems[iter].TextureIndex + "}: c-" + cacheItems[iter].CacheId + ", t-" +
  115. // cacheItems[iter].TextureID);
  116. // }
  117. ScenePresence p = null;
  118. if (m_scene.TryGetScenePresence(remoteClient.AgentId, out p))
  119. {
  120. WearableCacheItem[] existingitems = p.Appearance.WearableCacheItems;
  121. if (existingitems == null)
  122. {
  123. if (m_BakedTextureModule != null)
  124. {
  125. WearableCacheItem[] savedcache = null;
  126. try
  127. {
  128. if (p.Appearance.WearableCacheItemsDirty)
  129. {
  130. savedcache = m_BakedTextureModule.Get(p.UUID);
  131. p.Appearance.WearableCacheItems = savedcache;
  132. p.Appearance.WearableCacheItemsDirty = false;
  133. }
  134. }
  135. /*
  136. * The following Catch types DO NOT WORK with m_BakedTextureModule.Get
  137. * it jumps to the General Packet Exception Handler if you don't catch Exception!
  138. *
  139. catch (System.Net.Sockets.SocketException)
  140. {
  141. cacheItems = null;
  142. }
  143. catch (WebException)
  144. {
  145. cacheItems = null;
  146. }
  147. catch (InvalidOperationException)
  148. {
  149. cacheItems = null;
  150. } */
  151. catch (Exception)
  152. {
  153. // The service logs a sufficient error message.
  154. }
  155. if (savedcache != null)
  156. existingitems = savedcache;
  157. }
  158. }
  159. // Existing items null means it's a fully new appearance
  160. if (existingitems == null)
  161. {
  162. for (int i = 0; i < maxCacheitemsLoop; i++)
  163. {
  164. if (textureEntry.FaceTextures.Length > cacheItems[i].TextureIndex)
  165. {
  166. Primitive.TextureEntryFace face = textureEntry.FaceTextures[cacheItems[i].TextureIndex];
  167. if (face == null)
  168. {
  169. textureEntry.CreateFace(cacheItems[i].TextureIndex);
  170. textureEntry.FaceTextures[cacheItems[i].TextureIndex].TextureID =
  171. AppearanceManager.DEFAULT_AVATAR_TEXTURE;
  172. continue;
  173. }
  174. cacheItems[i].TextureID =face.TextureID;
  175. if (m_scene.AssetService != null)
  176. cacheItems[i].TextureAsset =
  177. m_scene.AssetService.GetCached(cacheItems[i].TextureID.ToString());
  178. }
  179. else
  180. {
  181. m_log.WarnFormat("[CACHEDBAKES]: Invalid Texture Index Provided, Texture doesn't exist or hasn't been uploaded yet {0}, the max is {1}. Skipping!", cacheItems[i].TextureIndex, textureEntry.FaceTextures.Length);
  182. }
  183. }
  184. }
  185. else
  186. {
  187. // for each uploaded baked texture
  188. for (int i = 0; i < maxCacheitemsLoop; i++)
  189. {
  190. if (textureEntry.FaceTextures.Length > cacheItems[i].TextureIndex)
  191. {
  192. Primitive.TextureEntryFace face = textureEntry.FaceTextures[cacheItems[i].TextureIndex];
  193. if (face == null)
  194. {
  195. textureEntry.CreateFace(cacheItems[i].TextureIndex);
  196. textureEntry.FaceTextures[cacheItems[i].TextureIndex].TextureID =
  197. AppearanceManager.DEFAULT_AVATAR_TEXTURE;
  198. continue;
  199. }
  200. cacheItems[i].TextureID =
  201. face.TextureID;
  202. }
  203. else
  204. {
  205. m_log.WarnFormat("[CACHEDBAKES]: Invalid Texture Index Provided, Texture doesn't exist or hasn't been uploaded yet {0}, the max is {1}. Skipping!", cacheItems[i].TextureIndex, textureEntry.FaceTextures.Length);
  206. }
  207. }
  208. for (int i = 0; i < maxCacheitemsLoop; i++)
  209. {
  210. if (cacheItems[i].TextureAsset == null)
  211. {
  212. cacheItems[i].TextureAsset =
  213. m_scene.AssetService.GetCached(cacheItems[i].TextureID.ToString());
  214. }
  215. }
  216. }
  217. p.Appearance.WearableCacheItems = cacheItems;
  218. if (m_BakedTextureModule != null)
  219. {
  220. m_BakedTextureModule.Store(remoteClient.AgentId, cacheItems);
  221. p.Appearance.WearableCacheItemsDirty = true;
  222. }
  223. }
  224. }
  225. }
  226. public void PostInitialise()
  227. {
  228. }
  229. public void Close() { }
  230. public string Name { get { return "UploadBakedTextureModule"; } }
  231. public Type ReplaceableInterface
  232. {
  233. get { return null; }
  234. }
  235. public void RegisterCaps(UUID agentID, Caps caps)
  236. {
  237. UploadBakedTextureHandler avatarhandler = new UploadBakedTextureHandler(
  238. caps, m_scene.AssetService, m_persistBakedTextures);
  239. caps.RegisterHandler(
  240. "UploadBakedTexture",
  241. new RestStreamHandler(
  242. "POST",
  243. "/CAPS/" + caps.CapsObjectPath + m_uploadBakedTexturePath,
  244. avatarhandler.UploadBakedTexture,
  245. "UploadBakedTexture",
  246. agentID.ToString()));
  247. }
  248. }
  249. }