SimianAvatarServiceConnector.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 System;
  28. using System.Collections.Generic;
  29. using System.Collections.Specialized;
  30. // DEBUG ON
  31. using System.Diagnostics;
  32. // DEBUG OFF
  33. using System.Reflection;
  34. using log4net;
  35. using Mono.Addins;
  36. using Nini.Config;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Services.Interfaces;
  41. using OpenMetaverse;
  42. using OpenMetaverse.StructuredData;
  43. namespace OpenSim.Services.Connectors.SimianGrid
  44. {
  45. /// <summary>
  46. /// Connects avatar appearance data to the SimianGrid backend
  47. /// </summary>
  48. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  49. public class SimianAvatarServiceConnector : IAvatarService, ISharedRegionModule
  50. {
  51. private static readonly ILog m_log =
  52. LogManager.GetLogger(
  53. MethodBase.GetCurrentMethod().DeclaringType);
  54. // private static string ZeroID = UUID.Zero.ToString();
  55. private string m_serverUrl = String.Empty;
  56. private bool m_Enabled = false;
  57. #region ISharedRegionModule
  58. public Type ReplaceableInterface { get { return null; } }
  59. public void RegionLoaded(Scene scene) { }
  60. public void PostInitialise() { }
  61. public void Close() { }
  62. public SimianAvatarServiceConnector() { }
  63. public string Name { get { return "SimianAvatarServiceConnector"; } }
  64. public void AddRegion(Scene scene) { if (m_Enabled) { scene.RegisterModuleInterface<IAvatarService>(this); } }
  65. public void RemoveRegion(Scene scene) { if (m_Enabled) { scene.UnregisterModuleInterface<IAvatarService>(this); } }
  66. #endregion ISharedRegionModule
  67. public SimianAvatarServiceConnector(IConfigSource source)
  68. {
  69. CommonInit(source);
  70. }
  71. public void Initialise(IConfigSource source)
  72. {
  73. IConfig moduleConfig = source.Configs["Modules"];
  74. if (moduleConfig != null)
  75. {
  76. string name = moduleConfig.GetString("AvatarServices", "");
  77. if (name == Name)
  78. CommonInit(source);
  79. }
  80. }
  81. private void CommonInit(IConfigSource source)
  82. {
  83. IConfig gridConfig = source.Configs["AvatarService"];
  84. if (gridConfig != null)
  85. {
  86. string serviceUrl = gridConfig.GetString("AvatarServerURI");
  87. if (!String.IsNullOrEmpty(serviceUrl))
  88. {
  89. if (!serviceUrl.EndsWith("/") && !serviceUrl.EndsWith("="))
  90. serviceUrl = serviceUrl + '/';
  91. m_serverUrl = serviceUrl;
  92. m_Enabled = true;
  93. }
  94. }
  95. if (String.IsNullOrEmpty(m_serverUrl))
  96. m_log.Info("[SIMIAN AVATAR CONNECTOR]: No AvatarServerURI specified, disabling connector");
  97. }
  98. #region IAvatarService
  99. // <summary>
  100. // Retrieves the LLPackedAppearance field from user data and unpacks
  101. // it into an AvatarAppearance structure
  102. // </summary>
  103. // <param name="userID"></param>
  104. public AvatarAppearance GetAppearance(UUID userID)
  105. {
  106. NameValueCollection requestArgs = new NameValueCollection
  107. {
  108. { "RequestMethod", "GetUser" },
  109. { "UserID", userID.ToString() }
  110. };
  111. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  112. if (response["Success"].AsBoolean())
  113. {
  114. OSDMap map = null;
  115. try { map = OSDParser.DeserializeJson(response["LLPackedAppearance"].AsString()) as OSDMap; }
  116. catch { }
  117. if (map != null)
  118. {
  119. AvatarAppearance appearance = new AvatarAppearance(map);
  120. // DEBUG ON
  121. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR] retrieved appearance for {0}:\n{1}",userID,appearance.ToString());
  122. // DEBUG OFF
  123. return appearance;
  124. }
  125. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR]: Failed to decode appearance for {0}",userID);
  126. return null;
  127. }
  128. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR]: Failed to get appearance for {0}: {1}",
  129. userID,response["Message"].AsString());
  130. return null;
  131. }
  132. // <summary>
  133. // </summary>
  134. // <param name=""></param>
  135. public bool SetAppearance(UUID userID, AvatarAppearance appearance)
  136. {
  137. OSDMap map = appearance.Pack();
  138. if (map == null)
  139. {
  140. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR]: Failed to encode appearance for {0}",userID);
  141. return false;
  142. }
  143. // m_log.DebugFormat("[SIMIAN AVATAR CONNECTOR] save appearance for {0}",userID);
  144. NameValueCollection requestArgs = new NameValueCollection
  145. {
  146. { "RequestMethod", "AddUserData" },
  147. { "UserID", userID.ToString() },
  148. { "LLPackedAppearance", OSDParser.SerializeJsonString(map) }
  149. };
  150. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  151. bool success = response["Success"].AsBoolean();
  152. if (! success)
  153. m_log.WarnFormat("[SIMIAN AVATAR CONNECTOR]: Failed to save appearance for {0}: {1}",
  154. userID,response["Message"].AsString());
  155. return success;
  156. }
  157. // <summary>
  158. // </summary>
  159. // <param name=""></param>
  160. public AvatarData GetAvatar(UUID userID)
  161. {
  162. NameValueCollection requestArgs = new NameValueCollection
  163. {
  164. { "RequestMethod", "GetUser" },
  165. { "UserID", userID.ToString() }
  166. };
  167. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  168. if (response["Success"].AsBoolean())
  169. {
  170. OSDMap map = null;
  171. try { map = OSDParser.DeserializeJson(response["LLAppearance"].AsString()) as OSDMap; }
  172. catch { }
  173. if (map != null)
  174. {
  175. AvatarWearable[] wearables = new AvatarWearable[13];
  176. wearables[0] = new AvatarWearable(map["ShapeItem"].AsUUID(), map["ShapeAsset"].AsUUID());
  177. wearables[1] = new AvatarWearable(map["SkinItem"].AsUUID(), map["SkinAsset"].AsUUID());
  178. wearables[2] = new AvatarWearable(map["HairItem"].AsUUID(), map["HairAsset"].AsUUID());
  179. wearables[3] = new AvatarWearable(map["EyesItem"].AsUUID(), map["EyesAsset"].AsUUID());
  180. wearables[4] = new AvatarWearable(map["ShirtItem"].AsUUID(), map["ShirtAsset"].AsUUID());
  181. wearables[5] = new AvatarWearable(map["PantsItem"].AsUUID(), map["PantsAsset"].AsUUID());
  182. wearables[6] = new AvatarWearable(map["ShoesItem"].AsUUID(), map["ShoesAsset"].AsUUID());
  183. wearables[7] = new AvatarWearable(map["SocksItem"].AsUUID(), map["SocksAsset"].AsUUID());
  184. wearables[8] = new AvatarWearable(map["JacketItem"].AsUUID(), map["JacketAsset"].AsUUID());
  185. wearables[9] = new AvatarWearable(map["GlovesItem"].AsUUID(), map["GlovesAsset"].AsUUID());
  186. wearables[10] = new AvatarWearable(map["UndershirtItem"].AsUUID(), map["UndershirtAsset"].AsUUID());
  187. wearables[11] = new AvatarWearable(map["UnderpantsItem"].AsUUID(), map["UnderpantsAsset"].AsUUID());
  188. wearables[12] = new AvatarWearable(map["SkirtItem"].AsUUID(), map["SkirtAsset"].AsUUID());
  189. AvatarAppearance appearance = new AvatarAppearance();
  190. appearance.Wearables = wearables;
  191. appearance.AvatarHeight = (float)map["Height"].AsReal();
  192. AvatarData avatar = new AvatarData(appearance);
  193. // Get attachments
  194. map = null;
  195. try { map = OSDParser.DeserializeJson(response["LLAttachments"].AsString()) as OSDMap; }
  196. catch { }
  197. if (map != null)
  198. {
  199. foreach (KeyValuePair<string, OSD> kvp in map)
  200. avatar.Data[kvp.Key] = kvp.Value.AsString();
  201. }
  202. return avatar;
  203. }
  204. else
  205. {
  206. m_log.Warn("[SIMIAN AVATAR CONNECTOR]: Failed to get user appearance for " + userID +
  207. ", LLAppearance is missing or invalid");
  208. return null;
  209. }
  210. }
  211. else
  212. {
  213. m_log.Warn("[SIMIAN AVATAR CONNECTOR]: Failed to get user appearance for " + userID + ": " +
  214. response["Message"].AsString());
  215. }
  216. return null;
  217. }
  218. // <summary>
  219. // </summary>
  220. // <param name=""></param>
  221. public bool SetAvatar(UUID userID, AvatarData avatar)
  222. {
  223. m_log.Debug("[SIMIAN AVATAR CONNECTOR]: SetAvatar called for " + userID);
  224. if (avatar.AvatarType == 1) // LLAvatar
  225. {
  226. AvatarAppearance appearance = avatar.ToAvatarAppearance();
  227. OSDMap map = new OSDMap();
  228. map["Height"] = OSD.FromReal(appearance.AvatarHeight);
  229. map["BodyItem"] = appearance.Wearables[AvatarWearable.BODY][0].ItemID.ToString();
  230. map["EyesItem"] = appearance.Wearables[AvatarWearable.EYES][0].ItemID.ToString();
  231. map["GlovesItem"] = appearance.Wearables[AvatarWearable.GLOVES][0].ItemID.ToString();
  232. map["HairItem"] = appearance.Wearables[AvatarWearable.HAIR][0].ItemID.ToString();
  233. map["JacketItem"] = appearance.Wearables[AvatarWearable.JACKET][0].ItemID.ToString();
  234. map["PantsItem"] = appearance.Wearables[AvatarWearable.PANTS][0].ItemID.ToString();
  235. map["ShirtItem"] = appearance.Wearables[AvatarWearable.SHIRT][0].ItemID.ToString();
  236. map["ShoesItem"] = appearance.Wearables[AvatarWearable.SHOES][0].ItemID.ToString();
  237. map["SkinItem"] = appearance.Wearables[AvatarWearable.SKIN][0].ItemID.ToString();
  238. map["SkirtItem"] = appearance.Wearables[AvatarWearable.SKIRT][0].ItemID.ToString();
  239. map["SocksItem"] = appearance.Wearables[AvatarWearable.SOCKS][0].ItemID.ToString();
  240. map["UnderPantsItem"] = appearance.Wearables[AvatarWearable.UNDERPANTS][0].ItemID.ToString();
  241. map["UnderShirtItem"] = appearance.Wearables[AvatarWearable.UNDERSHIRT][0].ItemID.ToString();
  242. map["BodyAsset"] = appearance.Wearables[AvatarWearable.BODY][0].AssetID.ToString();
  243. map["EyesAsset"] = appearance.Wearables[AvatarWearable.EYES][0].AssetID.ToString();
  244. map["GlovesAsset"] = appearance.Wearables[AvatarWearable.GLOVES][0].AssetID.ToString();
  245. map["HairAsset"] = appearance.Wearables[AvatarWearable.HAIR][0].AssetID.ToString();
  246. map["JacketAsset"] = appearance.Wearables[AvatarWearable.JACKET][0].AssetID.ToString();
  247. map["PantsAsset"] = appearance.Wearables[AvatarWearable.PANTS][0].AssetID.ToString();
  248. map["ShirtAsset"] = appearance.Wearables[AvatarWearable.SHIRT][0].AssetID.ToString();
  249. map["ShoesAsset"] = appearance.Wearables[AvatarWearable.SHOES][0].AssetID.ToString();
  250. map["SkinAsset"] = appearance.Wearables[AvatarWearable.SKIN][0].AssetID.ToString();
  251. map["SkirtAsset"] = appearance.Wearables[AvatarWearable.SKIRT][0].AssetID.ToString();
  252. map["SocksAsset"] = appearance.Wearables[AvatarWearable.SOCKS][0].AssetID.ToString();
  253. map["UnderPantsAsset"] = appearance.Wearables[AvatarWearable.UNDERPANTS][0].AssetID.ToString();
  254. map["UnderShirtAsset"] = appearance.Wearables[AvatarWearable.UNDERSHIRT][0].AssetID.ToString();
  255. OSDMap items = new OSDMap();
  256. foreach (KeyValuePair<string, string> kvp in avatar.Data)
  257. {
  258. if (kvp.Key.StartsWith("_ap_"))
  259. items.Add(kvp.Key, OSD.FromString(kvp.Value));
  260. }
  261. NameValueCollection requestArgs = new NameValueCollection
  262. {
  263. { "RequestMethod", "AddUserData" },
  264. { "UserID", userID.ToString() },
  265. { "LLAppearance", OSDParser.SerializeJsonString(map) },
  266. { "LLAttachments", OSDParser.SerializeJsonString(items) }
  267. };
  268. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  269. bool success = response["Success"].AsBoolean();
  270. if (!success)
  271. m_log.Warn("[SIMIAN AVATAR CONNECTOR]: Failed saving appearance for " + userID + ": " + response["Message"].AsString());
  272. return success;
  273. }
  274. else
  275. {
  276. m_log.Error("[SIMIAN AVATAR CONNECTOR]: Can't save appearance for " + userID + ". Unhandled avatar type " + avatar.AvatarType);
  277. return false;
  278. }
  279. }
  280. public bool ResetAvatar(UUID userID)
  281. {
  282. m_log.Error("[SIMIAN AVATAR CONNECTOR]: ResetAvatar called for " + userID + ", implement this");
  283. return false;
  284. }
  285. public bool SetItems(UUID userID, string[] names, string[] values)
  286. {
  287. m_log.Error("[SIMIAN AVATAR CONNECTOR]: SetItems called for " + userID + " with " + names.Length + " names and " + values.Length + " values, implement this");
  288. return false;
  289. }
  290. public bool RemoveItems(UUID userID, string[] names)
  291. {
  292. m_log.Error("[SIMIAN AVATAR CONNECTOR]: RemoveItems called for " + userID + " with " + names.Length + " names, implement this");
  293. return false;
  294. }
  295. #endregion IAvatarService
  296. }
  297. }