OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
XBakesModule.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 OpenMetaverse;
29 using Nini.Config;
30 using System;
31 using System.IO;
32 using System.Text;
33 using System.Xml;
34 using System.Xml.Serialization;
35 using System.Collections;
36 using System.Collections.Generic;
37 using System.Reflection;
38 using log4net;
39 using OpenSim.Framework;
40 using OpenSim.Framework.ServiceAuth;
41 using OpenSim.Region.Framework.Interfaces;
42 using OpenSim.Region.Framework.Scenes;
43 using OpenSim.Services.Interfaces;
44 using Mono.Addins;
45 
46 namespace OpenSim.Region.CoreModules.Avatar.BakedTextures
47 {
48  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XBakes.Module")]
50  {
51  protected Scene m_Scene;
52  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
53  private UTF8Encoding enc = new UTF8Encoding();
54  private string m_URL = String.Empty;
55  private static XmlSerializer m_serializer = new XmlSerializer(typeof(AssetBase));
56 
57  private static IServiceAuth m_Auth;
58 
59  public void Initialise(IConfigSource configSource)
60  {
61  IConfig config = configSource.Configs["XBakes"];
62  if (config == null)
63  return;
64 
65  m_URL = config.GetString("URL", String.Empty);
66  m_Auth = ServiceAuth.Create(configSource, "XBakes");
67  }
68 
69  public void AddRegion(Scene scene)
70  {
71  // m_log.InfoFormat("[XBakes]: Enabled for region {0}", scene.RegionInfo.RegionName);
72  m_Scene = scene;
73 
74  scene.RegisterModuleInterface<IBakedTextureModule>(this);
75  }
76 
77  public void RegionLoaded(Scene scene)
78  {
79  }
80 
81  public void RemoveRegion(Scene scene)
82  {
83  }
84 
85  public void Close()
86  {
87  }
88 
89  public string Name
90  {
91  get { return "XBakes.Module"; }
92  }
93 
94  public Type ReplaceableInterface
95  {
96  get { return null; }
97  }
98 
99  public WearableCacheItem[] Get(UUID id)
100  {
101  if (m_URL == String.Empty)
102  return null;
103 
104  using (RestClient rc = new RestClient(m_URL))
105  {
106  List<WearableCacheItem> ret = new List<WearableCacheItem>();
107  rc.AddResourcePath("bakes");
108  rc.AddResourcePath(id.ToString());
109 
110  rc.RequestMethod = "GET";
111 
112  try
113  {
114  using(Stream s = rc.Request(m_Auth))
115  {
116  using(XmlTextReader sr = new XmlTextReader(s))
117  {
118  sr.ReadStartElement("BakedAppearance");
119  while(sr.LocalName == "BakedTexture")
120  {
121  string sTextureIndex = sr.GetAttribute("TextureIndex");
122  int lTextureIndex = Convert.ToInt32(sTextureIndex);
123  string sCacheId = sr.GetAttribute("CacheId");
124  UUID lCacheId = UUID.Zero;
125  if(!(UUID.TryParse(sCacheId,out lCacheId)))
126  {
127  // ?? Nothing here
128  }
129 
130  sr.ReadStartElement("BakedTexture");
131  if(sr.Name=="AssetBase")
132  {
133  AssetBase a = (AssetBase)m_serializer.Deserialize(sr);
134  ret.Add(new WearableCacheItem() { CacheId = lCacheId,TextureIndex = (uint)lTextureIndex,TextureAsset = a,TextureID = a.FullID });
135  sr.ReadEndElement();
136  }
137  }
138  m_log.DebugFormat("[XBakes]: read {0} textures for user {1}",ret.Count,id);
139  }
140  return ret.ToArray();
141  }
142  }
143  catch (XmlException)
144  {
145  return null;
146  }
147  }
148  }
149 
150  public void Store(UUID agentId)
151  {
152  }
153 
154  public void UpdateMeshAvatar(UUID agentId)
155  {
156  }
157 
158  public void Store(UUID agentId, WearableCacheItem[] data)
159  {
160  if (m_URL == String.Empty)
161  return;
162 
163  int numberWears = 0;
164  MemoryStream reqStream;
165 
166  using (MemoryStream bakeStream = new MemoryStream())
167  using (XmlTextWriter bakeWriter = new XmlTextWriter(bakeStream, null))
168  {
169  bakeWriter.WriteStartElement(String.Empty, "BakedAppearance", String.Empty);
170 
171  for (int i = 0; i < data.Length; i++)
172  {
173  if (data[i] != null && data[i].TextureAsset != null)
174  {
175  bakeWriter.WriteStartElement(String.Empty, "BakedTexture", String.Empty);
176  bakeWriter.WriteAttributeString(String.Empty, "TextureIndex", String.Empty, data[i].TextureIndex.ToString());
177  bakeWriter.WriteAttributeString(String.Empty, "CacheId", String.Empty, data[i].CacheId.ToString());
178 // if (data[i].TextureAsset != null)
179  m_serializer.Serialize(bakeWriter, data[i].TextureAsset);
180 
181  bakeWriter.WriteEndElement();
182  numberWears++;
183  }
184  }
185 
186  bakeWriter.WriteEndElement();
187  bakeWriter.Flush();
188 
189  reqStream = new MemoryStream(bakeStream.ToArray());
190  }
191 
192  Util.FireAndForget(
193  delegate
194  {
195  using(RestClient rc = new RestClient(m_URL))
196  {
197  rc.AddResourcePath("bakes");
198  rc.AddResourcePath(agentId.ToString());
199  rc.RequestMethod = "POST";
200 
201  rc.Request(reqStream, m_Auth);
202  m_log.DebugFormat("[XBakes]: stored {0} textures for user {1}", numberWears, agentId);
203  }
204  }, null, "XBakesModule.Store"
205  );
206  }
207  }
208 }
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
Definition: XBakesModule.cs:77
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
Definition: XBakesModule.cs:69
Interactive OpenSim region server
Definition: OpenSim.cs:55
void Store(UUID agentId, WearableCacheItem[] data)
void Initialise(IConfigSource configSource)
This is called to initialize the region module. For shared modules, this is called exactly once...
Definition: XBakesModule.cs:59
Implementation of a generic REST client
Definition: RestClient.cs:59
UUID FullID
Asset UUID
Definition: AssetBase.cs:168
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
Definition: XBakesModule.cs:81
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
Definition: XBakesModule.cs:85