XBakesModule.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 lCacheId = UUID.Zero;
  115. if(!(UUID.TryParse(sCacheId,out lCacheId)))
  116. {
  117. // ?? Nothing here
  118. }
  119. sr.ReadStartElement("BakedTexture");
  120. if(sr.Name=="AssetBase")
  121. {
  122. AssetBase a = (AssetBase)m_serializer.Deserialize(sr);
  123. ret.Add(new WearableCacheItem()
  124. {
  125. CacheId = lCacheId,
  126. TextureIndex = (uint)lTextureIndex,
  127. TextureAsset = a,
  128. TextureID = a.FullID
  129. });
  130. sr.ReadEndElement();
  131. }
  132. }
  133. m_log.DebugFormat("[XBakes]: read {0} textures for user {1}",ret.Count,id);
  134. }
  135. return ret.ToArray();
  136. }
  137. }
  138. catch (XmlException)
  139. {
  140. return null;
  141. }
  142. }
  143. }
  144. public void Store(UUID agentId)
  145. {
  146. }
  147. public void UpdateMeshAvatar(UUID agentId)
  148. {
  149. }
  150. public void Store(UUID agentId, WearableCacheItem[] data)
  151. {
  152. if (m_URL == String.Empty)
  153. return;
  154. int numberWears = 0;
  155. MemoryStream reqStream;
  156. using (MemoryStream bakeStream = new MemoryStream())
  157. using (XmlTextWriter bakeWriter = new XmlTextWriter(bakeStream, null))
  158. {
  159. bakeWriter.WriteStartElement(String.Empty, "BakedAppearance", String.Empty);
  160. for (int i = 0; i < data.Length; i++)
  161. {
  162. if (data[i] != null && data[i].TextureAsset != null)
  163. {
  164. bakeWriter.WriteStartElement(String.Empty, "BakedTexture", String.Empty);
  165. bakeWriter.WriteAttributeString(String.Empty, "TextureIndex", String.Empty, data[i].TextureIndex.ToString());
  166. bakeWriter.WriteAttributeString(String.Empty, "CacheId", String.Empty, data[i].CacheId.ToString());
  167. // if (data[i].TextureAsset != null)
  168. m_serializer.Serialize(bakeWriter, data[i].TextureAsset);
  169. bakeWriter.WriteEndElement();
  170. numberWears++;
  171. }
  172. }
  173. bakeWriter.WriteEndElement();
  174. bakeWriter.Flush();
  175. reqStream = new MemoryStream(bakeStream.ToArray());
  176. }
  177. Util.FireAndForget(
  178. delegate
  179. {
  180. using(RestClient rc = new RestClient(m_URL))
  181. {
  182. rc.AddResourcePath("bakes");
  183. rc.AddResourcePath(agentId.ToString());
  184. rc.RequestMethod = "POST";
  185. rc.Request(reqStream, m_Auth);
  186. m_log.DebugFormat("[XBakes]: stored {0} textures for user {1}", numberWears, agentId);
  187. }
  188. if(reqStream != null)
  189. reqStream.Dispose();
  190. }, null, "XBakesModule.Store"
  191. );
  192. }
  193. }
  194. }