AvatarFactoryModule.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. using OpenSim.Services.Interfaces;
  36. namespace OpenSim.Region.CoreModules.Avatar.AvatarFactory
  37. {
  38. public class AvatarFactoryModule : IAvatarFactory, IRegionModule
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private Scene m_scene = null;
  42. private static readonly AvatarAppearance def = new AvatarAppearance();
  43. public bool TryGetAvatarAppearance(UUID avatarId, out AvatarAppearance appearance)
  44. {
  45. AvatarData avatar = m_scene.AvatarService.GetAvatar(avatarId);
  46. //if ((profile != null) && (profile.RootFolder != null))
  47. if (avatar != null)
  48. {
  49. appearance = avatar.ToAvatarAppearance(avatarId);
  50. return true;
  51. }
  52. m_log.ErrorFormat("[APPEARANCE]: Appearance not found for {0}, creating default", avatarId);
  53. appearance = CreateDefault(avatarId);
  54. return false;
  55. }
  56. private AvatarAppearance CreateDefault(UUID avatarId)
  57. {
  58. AvatarAppearance appearance = null;
  59. AvatarWearable[] wearables;
  60. byte[] visualParams;
  61. GetDefaultAvatarAppearance(out wearables, out visualParams);
  62. appearance = new AvatarAppearance(avatarId, wearables, visualParams);
  63. return appearance;
  64. }
  65. public void Initialise(Scene scene, IConfigSource source)
  66. {
  67. scene.RegisterModuleInterface<IAvatarFactory>(this);
  68. scene.EventManager.OnNewClient += NewClient;
  69. if (m_scene == null)
  70. {
  71. m_scene = scene;
  72. }
  73. }
  74. public void PostInitialise()
  75. {
  76. }
  77. public void Close()
  78. {
  79. }
  80. public string Name
  81. {
  82. get { return "Default Avatar Factory"; }
  83. }
  84. public bool IsSharedModule
  85. {
  86. get { return false; }
  87. }
  88. public void NewClient(IClientAPI client)
  89. {
  90. client.OnAvatarNowWearing += AvatarIsWearing;
  91. }
  92. public void RemoveClient(IClientAPI client)
  93. {
  94. // client.OnAvatarNowWearing -= AvatarIsWearing;
  95. }
  96. public void SetAppearanceAssets(UUID userID, ref AvatarAppearance appearance)
  97. {
  98. IInventoryService invService = m_scene.InventoryService;
  99. if (invService.GetRootFolder(userID) != null)
  100. {
  101. for (int i = 0; i < 13; i++)
  102. {
  103. if (appearance.Wearables[i].ItemID == UUID.Zero)
  104. {
  105. appearance.Wearables[i].AssetID = UUID.Zero;
  106. }
  107. else
  108. {
  109. InventoryItemBase baseItem = new InventoryItemBase(appearance.Wearables[i].ItemID, userID);
  110. baseItem = invService.GetItem(baseItem);
  111. if (baseItem != null)
  112. {
  113. appearance.Wearables[i].AssetID = baseItem.AssetID;
  114. }
  115. else
  116. {
  117. m_log.ErrorFormat(
  118. "[APPEARANCE]: Can't find inventory item {0} for {1}, setting to default",
  119. appearance.Wearables[i].ItemID, (WearableType)i);
  120. appearance.Wearables[i].AssetID = def.Wearables[i].AssetID;
  121. }
  122. }
  123. }
  124. }
  125. else
  126. {
  127. m_log.WarnFormat("[APPEARANCE]: user {0} has no inventory, appearance isn't going to work", userID);
  128. }
  129. }
  130. /// <summary>
  131. /// Update what the avatar is wearing using an item from their inventory.
  132. /// </summary>
  133. /// <param name="sender"></param>
  134. /// <param name="e"></param>
  135. public void AvatarIsWearing(Object sender, AvatarWearingArgs e)
  136. {
  137. m_log.DebugFormat("[APPEARANCE]: AvatarIsWearing");
  138. IClientAPI clientView = (IClientAPI)sender;
  139. ScenePresence sp = m_scene.GetScenePresence(clientView.AgentId);
  140. if (sp == null)
  141. {
  142. m_log.Error("[APPEARANCE]: Avatar is child agent, ignoring AvatarIsWearing event");
  143. return;
  144. }
  145. AvatarAppearance avatAppearance = sp.Appearance;
  146. //if (!TryGetAvatarAppearance(clientView.AgentId, out avatAppearance))
  147. //{
  148. // m_log.Warn("[APPEARANCE]: We didn't seem to find the appearance, falling back to ScenePresence");
  149. // avatAppearance = sp.Appearance;
  150. //}
  151. //m_log.DebugFormat("[APPEARANCE]: Received wearables for {0}", clientView.Name);
  152. foreach (AvatarWearingArgs.Wearable wear in e.NowWearing)
  153. {
  154. if (wear.Type < 13)
  155. {
  156. avatAppearance.Wearables[wear.Type].ItemID = wear.ItemID;
  157. }
  158. }
  159. SetAppearanceAssets(sp.UUID, ref avatAppearance);
  160. AvatarData adata = new AvatarData(avatAppearance);
  161. m_scene.AvatarService.SetAvatar(clientView.AgentId, adata);
  162. sp.Appearance = avatAppearance;
  163. }
  164. public static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
  165. {
  166. visualParams = GetDefaultVisualParams();
  167. wearables = AvatarWearable.DefaultWearables;
  168. }
  169. public void UpdateDatabase(UUID user, AvatarAppearance appearance)
  170. {
  171. m_log.DebugFormat("[APPEARANCE]: UpdateDatabase");
  172. AvatarData adata = new AvatarData(appearance);
  173. m_scene.AvatarService.SetAvatar(user, adata);
  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. }