AvatarFactoryModule.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.Reflection;
  29. using log4net;
  30. using Nini.Config;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications.Cache;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Region.CoreModules.Avatar.AvatarFactory
  38. {
  39. public class AvatarFactoryModule : IAvatarFactory, IRegionModule
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private Scene m_scene = null;
  43. private static readonly AvatarAppearance def = new AvatarAppearance();
  44. public bool TryGetAvatarAppearance(UUID avatarId, out AvatarAppearance appearance)
  45. {
  46. CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(avatarId);
  47. //if ((profile != null) && (profile.RootFolder != null))
  48. if (profile != null)
  49. {
  50. appearance = m_scene.CommsManager.AvatarService.GetUserAppearance(avatarId);
  51. if (appearance != null)
  52. {
  53. //SetAppearanceAssets(profile, ref appearance);
  54. //m_log.DebugFormat("[APPEARANCE]: Found : {0}", appearance.ToString());
  55. return true;
  56. }
  57. }
  58. appearance = CreateDefault(avatarId);
  59. m_log.ErrorFormat("[APPEARANCE]: Appearance not found for {0}, creating default", avatarId);
  60. return false;
  61. }
  62. private AvatarAppearance CreateDefault(UUID avatarId)
  63. {
  64. AvatarAppearance appearance = null;
  65. AvatarWearable[] wearables;
  66. byte[] visualParams;
  67. GetDefaultAvatarAppearance(out wearables, out visualParams);
  68. appearance = new AvatarAppearance(avatarId, wearables, visualParams);
  69. return appearance;
  70. }
  71. public void Initialise(Scene scene, IConfigSource source)
  72. {
  73. scene.RegisterModuleInterface<IAvatarFactory>(this);
  74. scene.EventManager.OnNewClient += NewClient;
  75. if (m_scene == null)
  76. {
  77. m_scene = scene;
  78. }
  79. }
  80. public void PostInitialise()
  81. {
  82. }
  83. public void Close()
  84. {
  85. }
  86. public string Name
  87. {
  88. get { return "Default Avatar Factory"; }
  89. }
  90. public bool IsSharedModule
  91. {
  92. get { return false; }
  93. }
  94. public void NewClient(IClientAPI client)
  95. {
  96. client.OnAvatarNowWearing += AvatarIsWearing;
  97. }
  98. public void RemoveClient(IClientAPI client)
  99. {
  100. // client.OnAvatarNowWearing -= AvatarIsWearing;
  101. }
  102. public void SetAppearanceAssets(UUID userID, ref AvatarAppearance appearance)
  103. {
  104. IInventoryService invService = m_scene.InventoryService;
  105. if (invService.GetRootFolder(userID) != null)
  106. {
  107. for (int i = 0; i < 13; i++)
  108. {
  109. if (appearance.Wearables[i].ItemID == UUID.Zero)
  110. {
  111. appearance.Wearables[i].AssetID = UUID.Zero;
  112. }
  113. else
  114. {
  115. InventoryItemBase baseItem = new InventoryItemBase(appearance.Wearables[i].ItemID, userID);
  116. baseItem = invService.GetItem(baseItem);
  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.WarnFormat("[APPEARANCE]: user {0} has no inventory, appearance isn't going to work", userID);
  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. AvatarAppearance avatAppearance = null;
  149. if (!TryGetAvatarAppearance(clientView.AgentId, out avatAppearance))
  150. {
  151. m_log.Warn("[APPEARANCE]: We didn't seem to find the appearance, falling back to ScenePresence");
  152. avatAppearance = avatar.Appearance;
  153. }
  154. //m_log.DebugFormat("[APPEARANCE]: Received wearables for {0}", clientView.Name);
  155. foreach (AvatarWearingArgs.Wearable wear in e.NowWearing)
  156. {
  157. if (wear.Type < 13)
  158. {
  159. avatAppearance.Wearables[wear.Type].ItemID = wear.ItemID;
  160. }
  161. }
  162. SetAppearanceAssets(avatar.UUID, ref avatAppearance);
  163. m_scene.CommsManager.AvatarService.UpdateUserAppearance(clientView.AgentId, avatAppearance);
  164. avatar.Appearance = avatAppearance;
  165. }
  166. public static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
  167. {
  168. visualParams = GetDefaultVisualParams();
  169. wearables = AvatarWearable.DefaultWearables;
  170. }
  171. public void UpdateDatabase(UUID user, AvatarAppearance appearance)
  172. {
  173. m_scene.CommsManager.AvatarService.UpdateUserAppearance(user, appearance);
  174. }
  175. private static byte[] GetDefaultVisualParams()
  176. {
  177. byte[] visualParams;
  178. visualParams = new byte[218];
  179. for (int i = 0; i < 218; i++)
  180. {
  181. visualParams[i] = 100;
  182. }
  183. return visualParams;
  184. }
  185. }
  186. }