XBakesModule.cs 10 KB

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