clockwerk-opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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.Framework.Communications;
42 using OpenSim.Region.Framework.Interfaces;
43 using OpenSim.Region.Framework.Scenes;
44 using OpenSim.Services.Interfaces;
45 using Mono.Addins;
46  
47 namespace OpenSim.Region.CoreModules.Avatar.BakedTextures
48 {
49 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XBakes.Module")]
50 public class XBakesModule : INonSharedRegionModule, IBakedTextureModule
51 {
52 protected Scene m_Scene;
53 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
54 private UTF8Encoding enc = new UTF8Encoding();
55 private string m_URL = String.Empty;
56 private static XmlSerializer m_serializer = new XmlSerializer(typeof(AssetBase));
57  
58 private static IServiceAuth m_Auth;
59  
60 public void Initialise(IConfigSource configSource)
61 {
62 IConfig config = configSource.Configs["XBakes"];
63 if (config == null)
64 return;
65  
66 m_URL = config.GetString("URL", String.Empty);
67 m_Auth = ServiceAuth.Create(configSource, "XBakes");
68 }
69  
70 public void AddRegion(Scene scene)
71 {
72 // m_log.InfoFormat("[XBakes]: Enabled for region {0}", scene.RegionInfo.RegionName);
73 m_Scene = scene;
74  
75 scene.RegisterModuleInterface<IBakedTextureModule>(this);
76 }
77  
78 public void RegionLoaded(Scene scene)
79 {
80 }
81  
82 public void RemoveRegion(Scene scene)
83 {
84 }
85  
86 public void Close()
87 {
88 }
89  
90 public string Name
91 {
92 get { return "XBakes.Module"; }
93 }
94  
95 public Type ReplaceableInterface
96 {
97 get { return null; }
98 }
99  
100 public WearableCacheItem[] Get(UUID id)
101 {
102 if (m_URL == String.Empty)
103 return null;
104  
105 int size = 0;
106 RestClient rc = new RestClient(m_URL);
107 List<WearableCacheItem> ret = new List<WearableCacheItem>();
108 rc.AddResourcePath("bakes");
109 rc.AddResourcePath(id.ToString());
110  
111 rc.RequestMethod = "GET";
112  
113 try
114 {
115 Stream s = rc.Request(m_Auth);
116 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 ++size;
131  
132 sr.ReadStartElement("BakedTexture");
133 AssetBase a = (AssetBase)m_serializer.Deserialize(sr);
134 ret.Add(new WearableCacheItem() { CacheId = lCacheId, TextureIndex = (uint)lTextureIndex, TextureAsset = a, TextureID = a.FullID });
135  
136 sr.ReadEndElement();
137 }
138 m_log.DebugFormat("[XBakes]: read {0} textures for user {1}", ret.Count, id);
139 sr.Close();
140 s.Close();
141  
142  
143 return ret.ToArray();
144 }
145 catch (XmlException)
146 {
147 return null;
148 }
149 }
150  
151 public void Store(UUID agentId, WearableCacheItem[] data)
152 {
153 if (m_URL == String.Empty)
154 return;
155  
156 MemoryStream bakeStream = new MemoryStream();
157 XmlTextWriter bakeWriter = new XmlTextWriter(bakeStream, null);
158  
159 bakeWriter.WriteStartElement(String.Empty, "BakedAppearance", String.Empty);
160  
161 for (int i = 0; i < data.Length; i++)
162 {
163 if (data[i] != null)
164 {
165 bakeWriter.WriteStartElement(String.Empty, "BakedTexture", String.Empty);
166 bakeWriter.WriteAttributeString(String.Empty, "TextureIndex", String.Empty, data[i].TextureIndex.ToString());
167 bakeWriter.WriteAttributeString(String.Empty, "CacheId", String.Empty, data[i].CacheId.ToString());
168 if (data[i].TextureAsset != null)
169 m_serializer.Serialize(bakeWriter, data[i].TextureAsset);
170  
171 bakeWriter.WriteEndElement();
172 }
173 }
174  
175 bakeWriter.WriteEndElement();
176 bakeWriter.Flush();
177  
178 RestClient rc = new RestClient(m_URL);
179 rc.AddResourcePath("bakes");
180 rc.AddResourcePath(agentId.ToString());
181  
182 rc.RequestMethod = "POST";
183  
184 MemoryStream reqStream = new MemoryStream(bakeStream.ToArray());
185 Util.FireAndForget(
186 delegate
187 {
188 rc.Request(reqStream, m_Auth);
189 m_log.DebugFormat("[XBakes]: stored {0} textures for user {1}", data.Length, agentId);
190 }
191 );
192 }
193 }
194 }