AvatarFactoryModule.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 OpenSim 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.Reflection;
  30. using System.Threading;
  31. using OpenMetaverse;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenSim.Data.Base;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications.Cache;
  37. using OpenSim.Region.Environment.Interfaces;
  38. using OpenSim.Region.Environment.Scenes;
  39. namespace OpenSim.Region.Environment.Modules.Avatar.AvatarFactory
  40. {
  41. public class AvatarFactoryModule : IAvatarFactory, IRegionModule
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private Scene m_scene = null;
  45. private static readonly AvatarAppearance def = new AvatarAppearance();
  46. public bool TryGetAvatarAppearance(UUID avatarId, out AvatarAppearance appearance)
  47. {
  48. CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(avatarId);
  49. //if ((profile != null) && (profile.RootFolder != null))
  50. if (profile != null)
  51. {
  52. appearance = m_scene.CommsManager.AvatarService.GetUserAppearance(avatarId);
  53. if (appearance != null)
  54. {
  55. //SetAppearanceAssets(profile, ref appearance);
  56. m_log.InfoFormat("[APPEARANCE]: Found : {0}", appearance.ToString());
  57. return true;
  58. }
  59. }
  60. appearance = CreateDefault(avatarId);
  61. m_log.InfoFormat("[APPEARANCE]: Appearance not found for {0}, creating default", avatarId);
  62. return false;
  63. }
  64. private AvatarAppearance CreateDefault(UUID avatarId)
  65. {
  66. AvatarAppearance appearance = null;
  67. AvatarWearable[] wearables;
  68. byte[] visualParams;
  69. GetDefaultAvatarAppearance(out wearables, out visualParams);
  70. appearance = new AvatarAppearance(avatarId, wearables, visualParams);
  71. return appearance;
  72. }
  73. public void Initialise(Scene scene, IConfigSource source)
  74. {
  75. scene.RegisterModuleInterface<IAvatarFactory>(this);
  76. scene.EventManager.OnNewClient += NewClient;
  77. if (m_scene == null)
  78. {
  79. m_scene = scene;
  80. }
  81. }
  82. public void PostInitialise()
  83. {
  84. }
  85. public void Close()
  86. {
  87. }
  88. public string Name
  89. {
  90. get { return "Default Avatar Factory"; }
  91. }
  92. public bool IsSharedModule
  93. {
  94. get { return false; }
  95. }
  96. public void NewClient(IClientAPI client)
  97. {
  98. client.OnAvatarNowWearing += AvatarIsWearing;
  99. }
  100. public void RemoveClient(IClientAPI client)
  101. {
  102. // client.OnAvatarNowWearing -= AvatarIsWearing;
  103. }
  104. public void SetAppearanceAssets(CachedUserInfo profile, ref AvatarAppearance appearance)
  105. {
  106. if (profile.RootFolder != null)
  107. {
  108. for (int i = 0; i < 13; i++)
  109. {
  110. if (appearance.Wearables[i].ItemID == UUID.Zero)
  111. {
  112. appearance.Wearables[i].AssetID = UUID.Zero;
  113. }
  114. else
  115. {
  116. InventoryItemBase baseItem = profile.RootFolder.FindItem(appearance.Wearables[i].ItemID);
  117. if (baseItem != null)
  118. {
  119. appearance.Wearables[i].AssetID = baseItem.AssetID;
  120. }
  121. else
  122. {
  123. m_log.ErrorFormat("[APPEARANCE]: Can't find inventory item {0}, setting to default", appearance.Wearables[i].ItemID);
  124. appearance.Wearables[i].AssetID = def.Wearables[i].AssetID;
  125. }
  126. }
  127. }
  128. }
  129. else
  130. {
  131. m_log.Error("[APPEARANCE]: you have no inventory, appearance stuff isn't going to work");
  132. }
  133. }
  134. /// <summary>
  135. /// Update what the avatar is wearing using an item from their inventory.
  136. /// </summary>
  137. /// <param name="sender"></param>
  138. /// <param name="e"></param>
  139. public void AvatarIsWearing(Object sender, AvatarWearingArgs e)
  140. {
  141. IClientAPI clientView = (IClientAPI)sender;
  142. ScenePresence avatar = m_scene.GetScenePresence(clientView.AgentId);
  143. if (avatar == null)
  144. {
  145. m_log.Error("[APPEARANCE]: Avatar is child agent, ignoring AvatarIsWearing event");
  146. return;
  147. }
  148. CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(clientView.AgentId);
  149. AvatarAppearance avatAppearance = null;
  150. if (!TryGetAvatarAppearance(clientView.AgentId, out avatAppearance))
  151. {
  152. m_log.Info("[APPEARANCE]: We didn't seem to find the appearance, falling back to ScenePresence");
  153. avatAppearance = avatar.Appearance;
  154. }
  155. m_log.DebugFormat("[APPEARANCE]: Received wearables for {0}", clientView.Name);
  156. if (profile != null)
  157. {
  158. if (profile.RootFolder != null)
  159. {
  160. foreach (AvatarWearingArgs.Wearable wear in e.NowWearing)
  161. {
  162. if (wear.Type < 13)
  163. {
  164. avatAppearance.Wearables[wear.Type].ItemID = wear.ItemID;
  165. }
  166. }
  167. SetAppearanceAssets(profile, ref avatAppearance);
  168. m_scene.CommsManager.AvatarService.UpdateUserAppearance(clientView.AgentId, avatAppearance);
  169. avatar.Appearance = avatAppearance;
  170. }
  171. else
  172. {
  173. m_log.WarnFormat(
  174. "[APPEARANCE]: Inventory has not yet been received for {0}, cannot set wearables",
  175. clientView.Name);
  176. }
  177. }
  178. else
  179. {
  180. m_log.WarnFormat("[APPEARANCE]: Cannot set wearables for {0}, no user profile found", clientView.Name);
  181. }
  182. }
  183. public static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
  184. {
  185. visualParams = GetDefaultVisualParams();
  186. wearables = AvatarWearable.DefaultWearables;
  187. }
  188. public void UpdateDatabase(UUID user, AvatarAppearance appearance)
  189. {
  190. m_scene.CommsManager.AvatarService.UpdateUserAppearance(user, appearance);
  191. }
  192. private static byte[] GetDefaultVisualParams()
  193. {
  194. byte[] visualParams;
  195. visualParams = new byte[218];
  196. for (int i = 0; i < 218; i++)
  197. {
  198. visualParams[i] = 100;
  199. }
  200. return visualParams;
  201. }
  202. }
  203. }