XBakesModule.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 OpenMetaverse;
  28. using Nini.Config;
  29. using System;
  30. using System.IO;
  31. using System.Text;
  32. using System.Xml;
  33. using System.Xml.Serialization;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using System.Reflection;
  37. using log4net;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.ServiceAuth;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Services.Interfaces;
  43. using Mono.Addins;
  44. namespace OpenSim.Region.CoreModules.Avatar.BakedTextures
  45. {
  46. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XBakes.Module")]
  47. public class XBakesModule : INonSharedRegionModule, IBakedTextureModule
  48. {
  49. protected Scene m_Scene;
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. private UTF8Encoding enc = new UTF8Encoding();
  52. private string m_URL = String.Empty;
  53. private static XmlSerializer m_serializer = new XmlSerializer(typeof(AssetBase));
  54. private static bool m_enabled = false;
  55. private static IServiceAuth m_Auth;
  56. public void Initialise(IConfigSource configSource)
  57. {
  58. IConfig config = configSource.Configs["XBakes"];
  59. if (config == null)
  60. return;
  61. m_URL = config.GetString("URL", String.Empty);
  62. if (m_URL == String.Empty)
  63. return;
  64. m_enabled = true;
  65. m_Auth = ServiceAuth.Create(configSource, "XBakes");
  66. }
  67. public void AddRegion(Scene scene)
  68. {
  69. if (!m_enabled)
  70. return;
  71. // m_log.InfoFormat("[XBakes]: Enabled for region {0}", scene.RegionInfo.RegionName);
  72. m_Scene = scene;
  73. scene.RegisterModuleInterface<IBakedTextureModule>(this);
  74. }
  75. public void RegionLoaded(Scene scene)
  76. {
  77. }
  78. public void RemoveRegion(Scene scene)
  79. {
  80. }
  81. public void Close()
  82. {
  83. }
  84. public string Name
  85. {
  86. get { return "XBakes.Module"; }
  87. }
  88. public Type ReplaceableInterface
  89. {
  90. get { return null; }
  91. }
  92. public WearableCacheItem[] Get(UUID id)
  93. {
  94. if (m_URL == String.Empty)
  95. return null;
  96. using (RestClient rc = new RestClient(m_URL))
  97. {
  98. List<WearableCacheItem> ret = new List<WearableCacheItem>();
  99. rc.AddResourcePath("bakes");
  100. rc.AddResourcePath(id.ToString());
  101. rc.RequestMethod = "GET";
  102. try
  103. {
  104. using(Stream s = rc.Request(m_Auth))
  105. {
  106. using(XmlTextReader sr = new XmlTextReader(s))
  107. {
  108. sr.ReadStartElement("BakedAppearance");
  109. while (sr.LocalName == "BakedTexture")
  110. {
  111. string sTextureIndex = sr.GetAttribute("TextureIndex");
  112. int lTextureIndex = Convert.ToInt32(sTextureIndex);
  113. string sCacheId = sr.GetAttribute("CacheId");
  114. UUID.TryParse(sCacheId, out UUID lCacheId);
  115. sr.ReadStartElement("BakedTexture");
  116. if (sr.Name == "AssetBase")
  117. {
  118. AssetBase a = (AssetBase)m_serializer.Deserialize(sr);
  119. ret.Add(new WearableCacheItem()
  120. {
  121. CacheId = lCacheId,
  122. TextureIndex = (uint)lTextureIndex,
  123. TextureAsset = a,
  124. TextureID = a.FullID
  125. });
  126. sr.ReadEndElement();
  127. }
  128. }
  129. while (sr.LocalName == "BESetA")
  130. {
  131. string sTextureIndex = sr.GetAttribute("TextureIndex");
  132. int lTextureIndex = Convert.ToInt32(sTextureIndex);
  133. string sCacheId = sr.GetAttribute("CacheId");
  134. UUID.TryParse(sCacheId, out UUID lCacheId);
  135. sr.ReadStartElement("BESetA");
  136. if (sr.Name == "AssetBase")
  137. {
  138. AssetBase a = (AssetBase)m_serializer.Deserialize(sr);
  139. ret.Add(new WearableCacheItem()
  140. {
  141. CacheId = lCacheId,
  142. TextureIndex = (uint)lTextureIndex,
  143. TextureAsset = a,
  144. TextureID = a.FullID
  145. });
  146. sr.ReadEndElement();
  147. }
  148. }
  149. m_log.DebugFormat("[XBakes]: read {0} textures for user {1}",ret.Count,id);
  150. }
  151. return ret.ToArray();
  152. }
  153. }
  154. catch (XmlException)
  155. {
  156. return null;
  157. }
  158. }
  159. }
  160. public void Store(UUID agentId)
  161. {
  162. }
  163. public void UpdateMeshAvatar(UUID agentId)
  164. {
  165. }
  166. public void Store(UUID agentId, WearableCacheItem[] data)
  167. {
  168. if (m_URL == String.Empty)
  169. return;
  170. int numberWears = 0;
  171. MemoryStream reqStream;
  172. using (MemoryStream bakeStream = new MemoryStream())
  173. using (XmlTextWriter bakeWriter = new XmlTextWriter(bakeStream, null))
  174. {
  175. bakeWriter.WriteStartElement(String.Empty, "BakedAppearance", String.Empty);
  176. List<int> extended = new List<int>();
  177. for (int i = 0; i < data.Length; i++)
  178. {
  179. if (data[i] != null && data[i].TextureAsset != null)
  180. {
  181. if(data[i].TextureIndex > 26)
  182. {
  183. extended.Add(i);
  184. continue;
  185. }
  186. bakeWriter.WriteStartElement(String.Empty, "BakedTexture", String.Empty);
  187. bakeWriter.WriteAttributeString(String.Empty, "TextureIndex", String.Empty, data[i].TextureIndex.ToString());
  188. bakeWriter.WriteAttributeString(String.Empty, "CacheId", String.Empty, data[i].CacheId.ToString());
  189. // if (data[i].TextureAsset != null)
  190. m_serializer.Serialize(bakeWriter, data[i].TextureAsset);
  191. bakeWriter.WriteEndElement();
  192. numberWears++;
  193. }
  194. }
  195. if(extended.Count > 0)
  196. {
  197. foreach(int i in extended)
  198. {
  199. bakeWriter.WriteStartElement(String.Empty, "BESetA", String.Empty);
  200. bakeWriter.WriteAttributeString(String.Empty, "TextureIndex", String.Empty, data[i].TextureIndex.ToString());
  201. bakeWriter.WriteAttributeString(String.Empty, "CacheId", String.Empty, data[i].CacheId.ToString());
  202. m_serializer.Serialize(bakeWriter, data[i].TextureAsset);
  203. bakeWriter.WriteEndElement();
  204. numberWears++;
  205. }
  206. }
  207. bakeWriter.WriteEndElement();
  208. bakeWriter.Flush();
  209. reqStream = new MemoryStream(bakeStream.ToArray());
  210. }
  211. Util.FireAndForget(
  212. delegate
  213. {
  214. using(RestClient rc = new RestClient(m_URL))
  215. {
  216. rc.AddResourcePath("bakes");
  217. rc.AddResourcePath(agentId.ToString());
  218. rc.RequestMethod = "POST";
  219. rc.Request(reqStream, m_Auth);
  220. m_log.DebugFormat("[XBakes]: stored {0} textures for user {1}", numberWears, agentId);
  221. }
  222. if(reqStream != null)
  223. reqStream.Dispose();
  224. }, null, "XBakesModule.Store"
  225. );
  226. }
  227. }
  228. }