AvatarFactoryModule.cs 13 KB

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