AvatarFactoryModule.cs 12 KB

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