AvatarFactoryModule.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 libsecondlife;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenSim.Data.Base;
  35. using OpenSim.Data.MapperFactory;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Communications.Cache;
  38. using OpenSim.Region.Environment.Interfaces;
  39. using OpenSim.Region.Environment.Scenes;
  40. namespace OpenSim.Region.Modules.AvatarFactory
  41. {
  42. public class AvatarFactoryModule : IAvatarFactory, IRegionModule
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private Scene m_scene = null;
  46. private static readonly AvatarAppearance def = new AvatarAppearance();
  47. public bool TryGetAvatarAppearance(LLUUID avatarId, out AvatarAppearance appearance)
  48. {
  49. CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(avatarId);
  50. if ((profile != null) && (profile.RootFolder != null))
  51. {
  52. appearance = m_scene.CommsManager.UserService.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.ToString());
  62. return false;
  63. }
  64. private AvatarAppearance CreateDefault(LLUUID 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 true; }
  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 == LLUUID.Zero)
  111. {
  112. appearance.Wearables[i].AssetID = LLUUID.Zero;
  113. }
  114. else
  115. {
  116. LLUUID assetId;
  117. InventoryItemBase baseItem = profile.RootFolder.FindItem(appearance.Wearables[i].ItemID);
  118. if (baseItem != null)
  119. {
  120. appearance.Wearables[i].AssetID = baseItem.AssetID;
  121. }
  122. else
  123. {
  124. m_log.ErrorFormat("[APPEARANCE] Can't find inventory item {0}, setting to default", appearance.Wearables[i].ItemID);
  125. appearance.Wearables[i].AssetID = def.Wearables[i].AssetID;
  126. }
  127. }
  128. }
  129. }
  130. else
  131. {
  132. m_log.Error("[APPEARANCE] you have no inventory, appearance stuff isn't going to work");
  133. }
  134. }
  135. public void AvatarIsWearing(Object sender, AvatarWearingArgs e)
  136. {
  137. IClientAPI clientView = (IClientAPI)sender;
  138. ScenePresence avatar = m_scene.GetScenePresence(clientView.AgentId);
  139. if (avatar == null) {
  140. m_log.Info("Avatar is child agent, ignoring AvatarIsWearing event");
  141. return;
  142. }
  143. CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(clientView.AgentId);
  144. AvatarAppearance avatAppearance = null;
  145. if (!TryGetAvatarAppearance(clientView.AgentId, out avatAppearance)) {
  146. m_log.Info("We didn't seem to find the appearance, falling back to ScenePresense");
  147. avatAppearance = avatar.Appearance;
  148. }
  149. m_log.Info("Calling Avatar is Wearing");
  150. if (profile != null)
  151. {
  152. if (profile.RootFolder != null)
  153. {
  154. foreach (AvatarWearingArgs.Wearable wear in e.NowWearing)
  155. {
  156. if (wear.Type < 13)
  157. {
  158. avatAppearance.Wearables[wear.Type].ItemID = wear.ItemID;
  159. }
  160. }
  161. SetAppearanceAssets(profile, ref avatAppearance);
  162. m_scene.CommsManager.UserService.UpdateUserAppearance(clientView.AgentId, avatAppearance);
  163. avatar.Appearance = avatAppearance;
  164. }
  165. else
  166. {
  167. m_log.Error("Root Profile is null, we can't set the appearance");
  168. }
  169. }
  170. }
  171. public static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
  172. {
  173. visualParams = GetDefaultVisualParams();
  174. wearables = AvatarWearable.DefaultWearables;
  175. }
  176. public void UpdateDatabase(LLUUID user, AvatarAppearance appearance)
  177. {
  178. m_scene.CommsManager.UserService.UpdateUserAppearance(user, appearance);
  179. }
  180. private static byte[] GetDefaultVisualParams()
  181. {
  182. byte[] visualParams;
  183. visualParams = new byte[218];
  184. for (int i = 0; i < 218; i++)
  185. {
  186. visualParams[i] = 100;
  187. }
  188. return visualParams;
  189. }
  190. }
  191. }