AvatarFactoryModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Threading;
  31. using libsecondlife;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications.Cache;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Region.Environment.Interfaces;
  37. using OpenSim.Region.Environment.Scenes;
  38. using OpenSim.Framework.Data;
  39. using TribalMedia.Framework.Data;
  40. namespace OpenSim.Region.Environment.Modules
  41. {
  42. public class AvatarFactoryModule : IAvatarFactory
  43. {
  44. private Scene m_scene = null;
  45. private readonly Dictionary<LLUUID, AvatarAppearance> m_avatarsAppearance = new Dictionary<LLUUID, AvatarAppearance>();
  46. private bool m_enablePersist = false;
  47. private string m_connectionString;
  48. private bool m_configured = false;
  49. private BaseDatabaseConnector m_databaseMapper;
  50. private AppearanceTableMapper m_appearanceMapper;
  51. private Dictionary<LLUUID, EventWaitHandle> m_fetchesInProgress = new Dictionary<LLUUID, EventWaitHandle>();
  52. private object m_syncLock = new object();
  53. public bool TryGetAvatarAppearance(LLUUID avatarId, out AvatarAppearance appearance)
  54. {
  55. //should only let one thread at a time do this part
  56. EventWaitHandle waitHandle = null;
  57. bool fetchInProgress = false;
  58. lock (m_syncLock)
  59. {
  60. appearance = CheckCache(avatarId);
  61. if (appearance != null)
  62. {
  63. return true;
  64. }
  65. //not in cache so check to see if another thread is already fetching it
  66. if (m_fetchesInProgress.TryGetValue(avatarId, out waitHandle))
  67. {
  68. fetchInProgress = true;
  69. }
  70. else
  71. {
  72. fetchInProgress = false;
  73. //no thread already fetching this appearance, so add a wait handle to list
  74. //for any following threads that want the same appearance
  75. waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
  76. m_fetchesInProgress.Add(avatarId, waitHandle);
  77. }
  78. }
  79. if (fetchInProgress)
  80. {
  81. waitHandle.WaitOne();
  82. appearance = CheckCache(avatarId);
  83. if (appearance != null)
  84. {
  85. waitHandle = null;
  86. return true;
  87. }
  88. else
  89. {
  90. waitHandle = null;
  91. return false;
  92. }
  93. }
  94. else
  95. {
  96. Thread.Sleep(5000);
  97. //this is the first thread to request this appearance
  98. //so let it check the db and if not found then create a default appearance
  99. //and add that to the cache
  100. appearance = CheckDatabase(avatarId);
  101. if (appearance != null)
  102. {
  103. //appearance has now been added to cache so lets pulse any waiting threads
  104. lock (m_syncLock)
  105. {
  106. m_fetchesInProgress.Remove(avatarId);
  107. waitHandle.Set();
  108. }
  109. // waitHandle.Close();
  110. waitHandle = null;
  111. return true;
  112. }
  113. //not found a appearance for the user, so create a new default one
  114. appearance = CreateDefault(avatarId);
  115. if (appearance != null)
  116. {
  117. //update database
  118. if (m_enablePersist)
  119. {
  120. m_appearanceMapper.Add(avatarId.UUID, appearance);
  121. }
  122. //add appearance to dictionary cache
  123. lock (m_avatarsAppearance)
  124. {
  125. m_avatarsAppearance[avatarId] = appearance;
  126. }
  127. //appearance has now been added to cache so lets pulse any waiting threads
  128. lock (m_syncLock)
  129. {
  130. m_fetchesInProgress.Remove(avatarId);
  131. waitHandle.Set();
  132. }
  133. // waitHandle.Close();
  134. waitHandle = null;
  135. return true;
  136. }
  137. else
  138. {
  139. //something went wrong, so release the wait handle and remove it
  140. //all waiting threads will fail to find cached appearance
  141. //but its better for them to fail than wait for ever
  142. lock (m_syncLock)
  143. {
  144. m_fetchesInProgress.Remove(avatarId);
  145. waitHandle.Set();
  146. }
  147. //waitHandle.Close();
  148. waitHandle = null;
  149. return false;
  150. }
  151. }
  152. }
  153. private AvatarAppearance CreateDefault(LLUUID avatarId)
  154. {
  155. AvatarAppearance appearance = null;
  156. AvatarWearable[] wearables;
  157. byte[] visualParams;
  158. GetDefaultAvatarAppearance(out wearables, out visualParams);
  159. appearance = new AvatarAppearance(avatarId, wearables, visualParams);
  160. return appearance;
  161. }
  162. private AvatarAppearance CheckDatabase(LLUUID avatarId)
  163. {
  164. AvatarAppearance appearance = null;
  165. if (m_enablePersist)
  166. {
  167. if (m_appearanceMapper.TryGetValue(avatarId.UUID, out appearance))
  168. {
  169. appearance.VisualParams = GetDefaultVisualParams();
  170. appearance.TextureEntry = AvatarAppearance.GetDefaultTextureEntry();
  171. lock (m_avatarsAppearance)
  172. {
  173. m_avatarsAppearance[avatarId] = appearance;
  174. }
  175. }
  176. }
  177. return appearance;
  178. }
  179. private AvatarAppearance CheckCache(LLUUID avatarId)
  180. {
  181. AvatarAppearance appearance = null;
  182. lock (m_avatarsAppearance)
  183. {
  184. if (m_avatarsAppearance.ContainsKey(avatarId))
  185. {
  186. appearance = m_avatarsAppearance[avatarId];
  187. }
  188. }
  189. return appearance;
  190. }
  191. public void Initialise(Scene scene, IConfigSource source)
  192. {
  193. scene.RegisterModuleInterface<IAvatarFactory>(this);
  194. scene.EventManager.OnNewClient += NewClient;
  195. if (m_scene == null)
  196. {
  197. m_scene = scene;
  198. }
  199. if (!m_configured)
  200. {
  201. m_configured = true;
  202. try
  203. {
  204. m_enablePersist = source.Configs["Startup"].GetBoolean("appearance_persist", false);
  205. m_connectionString = source.Configs["Startup"].GetString("appearance_connection_string", "");
  206. }
  207. catch (Exception)
  208. {
  209. }
  210. if (m_enablePersist)
  211. {
  212. m_databaseMapper = new MySQLDatabaseMapper(m_connectionString);
  213. m_appearanceMapper = new AppearanceTableMapper(m_databaseMapper, "AvatarAppearance");
  214. }
  215. }
  216. }
  217. public void PostInitialise()
  218. {
  219. }
  220. public void Close()
  221. {
  222. }
  223. public string Name
  224. {
  225. get { return "Default Avatar Factory"; }
  226. }
  227. public bool IsSharedModule
  228. {
  229. get { return true; }
  230. }
  231. public void NewClient(IClientAPI client)
  232. {
  233. client.OnAvatarNowWearing += AvatarIsWearing;
  234. }
  235. public void RemoveClient(IClientAPI client)
  236. {
  237. // client.OnAvatarNowWearing -= AvatarIsWearing;
  238. }
  239. public void AvatarIsWearing(Object sender, AvatarWearingArgs e)
  240. {
  241. IClientAPI clientView = (IClientAPI)sender;
  242. CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(clientView.AgentId);
  243. if (profile != null)
  244. {
  245. if (profile.RootFolder != null)
  246. {
  247. if (m_avatarsAppearance.ContainsKey(clientView.AgentId))
  248. {
  249. AvatarAppearance avatAppearance = null;
  250. lock (m_avatarsAppearance)
  251. {
  252. avatAppearance = m_avatarsAppearance[clientView.AgentId];
  253. }
  254. foreach (AvatarWearingArgs.Wearable wear in e.NowWearing)
  255. {
  256. if (wear.Type < 13)
  257. {
  258. if (wear.ItemID == LLUUID.Zero)
  259. {
  260. avatAppearance.Wearables[wear.Type].ItemID = LLUUID.Zero;
  261. avatAppearance.Wearables[wear.Type].AssetID = LLUUID.Zero;
  262. UpdateDatabase(clientView.AgentId, avatAppearance);
  263. }
  264. else
  265. {
  266. LLUUID assetId;
  267. InventoryItemBase baseItem = profile.RootFolder.HasItem(wear.ItemID);
  268. if (baseItem != null)
  269. {
  270. assetId = baseItem.assetID;
  271. avatAppearance.Wearables[wear.Type].AssetID = assetId;
  272. avatAppearance.Wearables[wear.Type].ItemID = wear.ItemID;
  273. UpdateDatabase(clientView.AgentId, avatAppearance);
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. public void UpdateDatabase(LLUUID userID, AvatarAppearance avatAppearance)
  283. {
  284. if (m_enablePersist)
  285. {
  286. m_appearanceMapper.Update(userID.UUID, avatAppearance);
  287. }
  288. }
  289. public static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
  290. {
  291. visualParams = GetDefaultVisualParams();
  292. wearables = AvatarWearable.DefaultWearables;
  293. }
  294. private static byte[] GetDefaultVisualParams()
  295. {
  296. byte[] visualParams;
  297. visualParams = new byte[218];
  298. for (int i = 0; i < 218; i++)
  299. {
  300. visualParams[i] = 100;
  301. }
  302. return visualParams;
  303. }
  304. }
  305. }