OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
UploadBakedTextureModule.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Specialized;
32 using System.Drawing;
33 using System.Drawing.Imaging;
34 using System.Reflection;
35 using System.IO;
36 using System.Web;
37 using log4net;
38 using Nini.Config;
39 using Mono.Addins;
40 using OpenMetaverse;
41 using OpenMetaverse.StructuredData;
42 using OpenMetaverse.Imaging;
43 using OpenSim.Framework;
44 using OpenSim.Framework.Servers;
45 using OpenSim.Framework.Servers.HttpServer;
46 using OpenSim.Region.Framework.Interfaces;
47 using OpenSim.Region.Framework.Scenes;
48 using OpenSim.Services.Interfaces;
50 using OpenSim.Capabilities.Handlers;
51 
52 namespace OpenSim.Region.ClientStack.Linden
53 {
54  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UploadBakedTextureModule")]
56  {
57  private static readonly ILog m_log =
58  LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
59 
63  private static readonly string m_uploadBakedTexturePath = "0010/";// This is in the LandManagementModule.
64 
65  private Scene m_scene;
66  private bool m_persistBakedTextures;
67 
68  private IBakedTextureModule m_BakedTextureModule;
69  private string m_URL;
70 
71  public void Initialise(IConfigSource source)
72  {
73  IConfig config = source.Configs["ClientStack.LindenCaps"];
74  if (config == null)
75  return;
76 
77  m_URL = config.GetString("Cap_UploadBakedTexture", string.Empty);
78 
79  IConfig appearanceConfig = source.Configs["Appearance"];
80  if (appearanceConfig != null)
81  m_persistBakedTextures = appearanceConfig.GetBoolean("PersistBakedTextures", m_persistBakedTextures);
82  }
83 
84  public void AddRegion(Scene s)
85  {
86  m_scene = s;
87 
88  }
89 
90  public void RemoveRegion(Scene s)
91  {
92  s.EventManager.OnRegisterCaps -= RegisterCaps;
93  s.EventManager.OnNewPresence -= RegisterNewPresence;
94  s.EventManager.OnRemovePresence -= DeRegisterPresence;
95  m_BakedTextureModule = null;
96  m_scene = null;
97  }
98 
99  public void RegionLoaded(Scene s)
100  {
101  m_scene.EventManager.OnRegisterCaps += RegisterCaps;
102  m_scene.EventManager.OnNewPresence += RegisterNewPresence;
103  m_scene.EventManager.OnRemovePresence += DeRegisterPresence;
104 
105  }
106 
107  private void DeRegisterPresence(UUID agentId)
108  {
109  }
110 
111  private void RegisterNewPresence(ScenePresence presence)
112  {
113 // presence.ControllingClient.OnSetAppearance += CaptureAppearanceSettings;
114  }
115 
116 /* not in use. work done in AvatarFactoryModule ValidateBakedTextureCache() and UpdateBakedTextureCache()
117  private void CaptureAppearanceSettings(IClientAPI remoteClient, Primitive.TextureEntry textureEntry, byte[] visualParams, Vector3 avSize, WearableCacheItem[] cacheItems)
118  {
119  // if cacheItems.Length > 0 viewer is giving us current textures information.
120  // baked ones should had been uploaded and in assets cache as local itens
121 
122 
123  if (cacheItems.Length == 0)
124  return; // no textures information, nothing to do
125 
126  ScenePresence p = null;
127  if (!m_scene.TryGetScenePresence(remoteClient.AgentId, out p))
128  return; // what are we doing if there is no presence to cache for?
129 
130  if (p.IsDeleted)
131  return; // does this really work?
132 
133  int maxCacheitemsLoop = cacheItems.Length;
134  if (maxCacheitemsLoop > 20)
135  {
136  maxCacheitemsLoop = AvatarWearable.MAX_WEARABLES;
137  m_log.WarnFormat("[CACHEDBAKES]: Too Many Cache items Provided {0}, the max is {1}. Truncating!", cacheItems.Length, AvatarWearable.MAX_WEARABLES);
138  }
139 
140  m_BakedTextureModule = m_scene.RequestModuleInterface<IBakedTextureModule>();
141 
142 
143  // some nice debug
144  m_log.Debug("[Cacheitems]: " + cacheItems.Length);
145  for (int iter = 0; iter < maxCacheitemsLoop; iter++)
146  {
147  m_log.Debug("[Cacheitems] {" + iter + "/" + cacheItems[iter].TextureIndex + "}: c-" + cacheItems[iter].CacheId + ", t-" +
148  cacheItems[iter].TextureID);
149  }
150 
151  // p.Appearance.WearableCacheItems is in memory primary cashID to textures mapper
152 
153  WearableCacheItem[] existingitems = p.Appearance.WearableCacheItems;
154 
155  if (existingitems == null)
156  {
157  if (m_BakedTextureModule != null)
158  {
159  WearableCacheItem[] savedcache = null;
160  try
161  {
162  if (p.Appearance.WearableCacheItemsDirty)
163  {
164  savedcache = m_BakedTextureModule.Get(p.UUID);
165  p.Appearance.WearableCacheItems = savedcache;
166  p.Appearance.WearableCacheItemsDirty = false;
167  }
168  }
169 
170  catch (Exception)
171  {
172  // The service logs a sufficient error message.
173  }
174 
175 
176  if (savedcache != null)
177  existingitems = savedcache;
178  }
179  }
180 
181  // Existing items null means it's a fully new appearance
182  if (existingitems == null)
183  {
184  for (int i = 0; i < maxCacheitemsLoop; i++)
185  {
186  if (textureEntry.FaceTextures.Length > cacheItems[i].TextureIndex)
187  {
188  Primitive.TextureEntryFace face = textureEntry.FaceTextures[cacheItems[i].TextureIndex];
189  if (face == null)
190  {
191  textureEntry.CreateFace(cacheItems[i].TextureIndex);
192  textureEntry.FaceTextures[cacheItems[i].TextureIndex].TextureID =
193  AppearanceManager.DEFAULT_AVATAR_TEXTURE;
194  continue;
195  }
196  cacheItems[i].TextureID = face.TextureID;
197  if (m_scene.AssetService != null)
198  cacheItems[i].TextureAsset =
199  m_scene.AssetService.GetCached(cacheItems[i].TextureID.ToString());
200  }
201  else
202  {
203  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);
204  }
205  }
206  }
207  else
208  {
209  for (int i = 0; i < maxCacheitemsLoop; i++)
210  {
211  if (textureEntry.FaceTextures.Length > cacheItems[i].TextureIndex)
212  {
213  Primitive.TextureEntryFace face = textureEntry.FaceTextures[cacheItems[i].TextureIndex];
214  if (face == null)
215  {
216  textureEntry.CreateFace(cacheItems[i].TextureIndex);
217  textureEntry.FaceTextures[cacheItems[i].TextureIndex].TextureID =
218  AppearanceManager.DEFAULT_AVATAR_TEXTURE;
219  continue;
220  }
221  cacheItems[i].TextureID =
222  face.TextureID;
223  }
224  else
225  {
226  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);
227  }
228  }
229 
230  for (int i = 0; i < maxCacheitemsLoop; i++)
231  {
232  if (cacheItems[i].TextureAsset == null)
233  {
234  cacheItems[i].TextureAsset =
235  m_scene.AssetService.GetCached(cacheItems[i].TextureID.ToString());
236  }
237  }
238  }
239  p.Appearance.WearableCacheItems = cacheItems;
240 
241  if (m_BakedTextureModule != null)
242  {
243  m_BakedTextureModule.Store(remoteClient.AgentId, cacheItems);
244  p.Appearance.WearableCacheItemsDirty = true;
245 
246  }
247  else
248  p.Appearance.WearableCacheItemsDirty = false;
249 
250  for (int iter = 0; iter < maxCacheitemsLoop; iter++)
251  {
252  m_log.Debug("[CacheitemsLeaving] {" + iter + "/" + cacheItems[iter].TextureIndex + "}: c-" + cacheItems[iter].CacheId + ", t-" +
253  cacheItems[iter].TextureID);
254  }
255  }
256  */
257  public void PostInitialise()
258  {
259  }
260 
261 
262 
263  public void Close() { }
264 
265  public string Name { get { return "UploadBakedTextureModule"; } }
266 
267  public Type ReplaceableInterface
268  {
269  get { return null; }
270  }
271 
272  public void RegisterCaps(UUID agentID, Caps caps)
273  {
274  //caps.RegisterHandler("GetTexture", new StreamHandler("GET", "/CAPS/" + capID, ProcessGetTexture));
275  if (m_URL == "localhost")
276  {
278  caps, m_scene.AssetService, m_persistBakedTextures);
279 
280  caps.RegisterHandler(
281  "UploadBakedTexture",
282  new RestStreamHandler(
283  "POST",
284  "/CAPS/" + caps.CapsObjectPath + m_uploadBakedTexturePath,
285  avatarhandler.UploadBakedTexture,
286  "UploadBakedTexture",
287  agentID.ToString()));
288 
289  }
290  else
291  {
292  caps.RegisterHandler("UploadBakedTexture", m_URL);
293  }
294  }
295  }
296 }
void AddRegion(Scene s)
This is called whenever a Scene is added. For shared modules, this can happen several times...
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
void RemoveRegion(Scene s)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
string UploadBakedTexture(string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
Handle a request from the client for a Uri to upload a baked texture.
OpenSim.Framework.Capabilities.Caps Caps
OpenSim.Framework.Capabilities.Caps Caps
void RegionLoaded(Scene s)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...