XBakesModule.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.Framework.Communications;
  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 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. m_Auth = ServiceAuth.Create(configSource, "XBakes");
  63. }
  64. public void AddRegion(Scene scene)
  65. {
  66. // m_log.InfoFormat("[XBakes]: Enabled for region {0}", scene.RegionInfo.RegionName);
  67. m_Scene = scene;
  68. scene.RegisterModuleInterface<IBakedTextureModule>(this);
  69. }
  70. public void RegionLoaded(Scene scene)
  71. {
  72. }
  73. public void RemoveRegion(Scene scene)
  74. {
  75. }
  76. public void Close()
  77. {
  78. }
  79. public string Name
  80. {
  81. get { return "XBakes.Module"; }
  82. }
  83. public Type ReplaceableInterface
  84. {
  85. get { return null; }
  86. }
  87. public WearableCacheItem[] Get(UUID id)
  88. {
  89. if (m_URL == String.Empty)
  90. return null;
  91. int size = 0;
  92. using (RestClient rc = new RestClient(m_URL))
  93. {
  94. List<WearableCacheItem> ret = new List<WearableCacheItem>();
  95. rc.AddResourcePath("bakes");
  96. rc.AddResourcePath(id.ToString());
  97. rc.RequestMethod = "GET";
  98. try
  99. {
  100. Stream s = rc.Request(m_Auth);
  101. using (XmlTextReader sr = new XmlTextReader(s))
  102. {
  103. sr.ReadStartElement("BakedAppearance");
  104. while (sr.LocalName == "BakedTexture")
  105. {
  106. string sTextureIndex = sr.GetAttribute("TextureIndex");
  107. int lTextureIndex = Convert.ToInt32(sTextureIndex);
  108. string sCacheId = sr.GetAttribute("CacheId");
  109. UUID lCacheId = UUID.Zero;
  110. if (!(UUID.TryParse(sCacheId, out lCacheId)))
  111. {
  112. // ?? Nothing here
  113. }
  114. ++size;
  115. sr.ReadStartElement("BakedTexture");
  116. AssetBase a = (AssetBase)m_serializer.Deserialize(sr);
  117. ret.Add(new WearableCacheItem() { CacheId = lCacheId, TextureIndex = (uint)lTextureIndex, TextureAsset = a, TextureID = a.FullID });
  118. sr.ReadEndElement();
  119. }
  120. m_log.DebugFormat("[XBakes]: read {0} textures for user {1}", ret.Count, id);
  121. }
  122. return ret.ToArray();
  123. }
  124. catch (XmlException)
  125. {
  126. return null;
  127. }
  128. }
  129. }
  130. public void Store(UUID agentId, WearableCacheItem[] data)
  131. {
  132. if (m_URL == String.Empty)
  133. return;
  134. MemoryStream reqStream;
  135. using (MemoryStream bakeStream = new MemoryStream())
  136. using (XmlTextWriter bakeWriter = new XmlTextWriter(bakeStream, null))
  137. {
  138. bakeWriter.WriteStartElement(String.Empty, "BakedAppearance", String.Empty);
  139. for (int i = 0; i < data.Length; i++)
  140. {
  141. if (data[i] != null)
  142. {
  143. bakeWriter.WriteStartElement(String.Empty, "BakedTexture", String.Empty);
  144. bakeWriter.WriteAttributeString(String.Empty, "TextureIndex", String.Empty, data[i].TextureIndex.ToString());
  145. bakeWriter.WriteAttributeString(String.Empty, "CacheId", String.Empty, data[i].CacheId.ToString());
  146. if (data[i].TextureAsset != null)
  147. m_serializer.Serialize(bakeWriter, data[i].TextureAsset);
  148. bakeWriter.WriteEndElement();
  149. }
  150. }
  151. bakeWriter.WriteEndElement();
  152. bakeWriter.Flush();
  153. reqStream = new MemoryStream(bakeStream.ToArray());
  154. }
  155. RestClient rc = new RestClient(m_URL);
  156. rc.AddResourcePath("bakes");
  157. rc.AddResourcePath(agentId.ToString());
  158. rc.RequestMethod = "POST";
  159. Util.FireAndForget(
  160. delegate
  161. {
  162. rc.Request(reqStream, m_Auth);
  163. m_log.DebugFormat("[XBakes]: stored {0} textures for user {1}", data.Length, agentId);
  164. }, null, "XBakesModule.Store"
  165. );
  166. }
  167. }
  168. }