AvatarFactoryModule.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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.Collections.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using System.Text;
  32. using System.Timers;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. namespace OpenSim.Region.CoreModules.Avatar.AvatarFactory
  41. {
  42. public class AvatarFactoryModule : IAvatarFactoryModule, IRegionModule
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. public const string BAKED_TEXTURES_REPORT_FORMAT = "{0,-9} {1}";
  46. private Scene m_scene = null;
  47. private int m_savetime = 5; // seconds to wait before saving changed appearance
  48. private int m_sendtime = 2; // seconds to wait before sending changed appearance
  49. private int m_checkTime = 500; // milliseconds to wait between checks for appearance updates
  50. private System.Timers.Timer m_updateTimer = new System.Timers.Timer();
  51. private Dictionary<UUID,long> m_savequeue = new Dictionary<UUID,long>();
  52. private Dictionary<UUID,long> m_sendqueue = new Dictionary<UUID,long>();
  53. private object m_setAppearanceLock = new object();
  54. #region IRegionModule
  55. public void Initialise(Scene scene, IConfigSource config)
  56. {
  57. scene.RegisterModuleInterface<IAvatarFactoryModule>(this);
  58. scene.EventManager.OnNewClient += SubscribeToClientEvents;
  59. IConfig appearanceConfig = config.Configs["Appearance"];
  60. if (appearanceConfig != null)
  61. {
  62. m_savetime = Convert.ToInt32(appearanceConfig.GetString("DelayBeforeAppearanceSave",Convert.ToString(m_savetime)));
  63. m_sendtime = Convert.ToInt32(appearanceConfig.GetString("DelayBeforeAppearanceSend",Convert.ToString(m_sendtime)));
  64. // m_log.InfoFormat("[AVFACTORY] configured for {0} save and {1} send",m_savetime,m_sendtime);
  65. }
  66. if (m_scene == null)
  67. m_scene = scene;
  68. }
  69. public void PostInitialise()
  70. {
  71. m_updateTimer.Enabled = false;
  72. m_updateTimer.AutoReset = true;
  73. m_updateTimer.Interval = m_checkTime; // 500 milliseconds wait to start async ops
  74. m_updateTimer.Elapsed += new ElapsedEventHandler(HandleAppearanceUpdateTimer);
  75. }
  76. public void Close()
  77. {
  78. }
  79. public string Name
  80. {
  81. get { return "Default Avatar Factory"; }
  82. }
  83. public bool IsSharedModule
  84. {
  85. get { return false; }
  86. }
  87. private void SubscribeToClientEvents(IClientAPI client)
  88. {
  89. client.OnRequestWearables += Client_OnRequestWearables;
  90. client.OnSetAppearance += Client_OnSetAppearance;
  91. client.OnAvatarNowWearing += Client_OnAvatarNowWearing;
  92. }
  93. #endregion
  94. #region IAvatarFactoryModule
  95. /// </summary>
  96. /// <param name="sp"></param>
  97. /// <param name="texture"></param>
  98. /// <param name="visualParam"></param>
  99. public void SetAppearance(IScenePresence sp, AvatarAppearance appearance)
  100. {
  101. SetAppearance(sp, appearance.Texture, appearance.VisualParams);
  102. }
  103. /// <summary>
  104. /// Set appearance data (texture asset IDs and slider settings)
  105. /// </summary>
  106. /// <param name="sp"></param>
  107. /// <param name="texture"></param>
  108. /// <param name="visualParam"></param>
  109. public void SetAppearance(IScenePresence sp, Primitive.TextureEntry textureEntry, byte[] visualParams)
  110. {
  111. // m_log.DebugFormat(
  112. // "[AVFACTORY]: start SetAppearance for {0}, te {1}, visualParams {2}",
  113. // sp.Name, textureEntry, visualParams);
  114. // TODO: This is probably not necessary any longer, just assume the
  115. // textureEntry set implies that the appearance transaction is complete
  116. bool changed = false;
  117. // Process the texture entry transactionally, this doesn't guarantee that Appearance is
  118. // going to be handled correctly but it does serialize the updates to the appearance
  119. lock (m_setAppearanceLock)
  120. {
  121. // Process the visual params, this may change height as well
  122. if (visualParams != null)
  123. {
  124. // string[] visualParamsStrings = new string[visualParams.Length];
  125. // for (int i = 0; i < visualParams.Length; i++)
  126. // visualParamsStrings[i] = visualParams[i].ToString();
  127. // m_log.DebugFormat(
  128. // "[AVFACTORY]: Setting visual params for {0} to {1}",
  129. // client.Name, string.Join(", ", visualParamsStrings));
  130. float oldHeight = sp.Appearance.AvatarHeight;
  131. changed = sp.Appearance.SetVisualParams(visualParams);
  132. if (sp.Appearance.AvatarHeight != oldHeight && sp.Appearance.AvatarHeight > 0)
  133. ((ScenePresence)sp).SetHeight(sp.Appearance.AvatarHeight);
  134. }
  135. // Process the baked texture array
  136. if (textureEntry != null)
  137. {
  138. // m_log.DebugFormat("[AVFACTORY]: Received texture update for {0} {1}", sp.Name, sp.UUID);
  139. // WriteBakedTexturesReport(sp, m_log.DebugFormat);
  140. changed = sp.Appearance.SetTextureEntries(textureEntry) || changed;
  141. // WriteBakedTexturesReport(sp, m_log.DebugFormat);
  142. // If bake textures are missing and this is not an NPC, request a rebake from client
  143. if (!ValidateBakedTextureCache(sp) && (((ScenePresence)sp).PresenceType != PresenceType.Npc))
  144. RequestRebake(sp, true);
  145. // This appears to be set only in the final stage of the appearance
  146. // update transaction. In theory, we should be able to do an immediate
  147. // appearance send and save here.
  148. }
  149. // NPC should send to clients immediately and skip saving appearance
  150. if (((ScenePresence)sp).PresenceType == PresenceType.Npc)
  151. {
  152. SendAppearance((ScenePresence)sp);
  153. return;
  154. }
  155. // save only if there were changes, send no matter what (doesn't hurt to send twice)
  156. if (changed)
  157. QueueAppearanceSave(sp.ControllingClient.AgentId);
  158. QueueAppearanceSend(sp.ControllingClient.AgentId);
  159. }
  160. // m_log.WarnFormat("[AVFACTORY]: complete SetAppearance for {0}:\n{1}",client.AgentId,sp.Appearance.ToString());
  161. }
  162. private void SendAppearance(ScenePresence sp)
  163. {
  164. // Send the appearance to everyone in the scene
  165. sp.SendAppearanceToAllOtherAgents();
  166. // Send animations back to the avatar as well
  167. sp.Animator.SendAnimPack();
  168. }
  169. public bool SendAppearance(UUID agentId)
  170. {
  171. // m_log.DebugFormat("[AVFACTORY]: Sending appearance for {0}", agentId);
  172. ScenePresence sp = m_scene.GetScenePresence(agentId);
  173. if (sp == null)
  174. {
  175. // This is expected if the user has gone away.
  176. // m_log.DebugFormat("[AVFACTORY]: Agent {0} no longer in the scene", agentId);
  177. return false;
  178. }
  179. SendAppearance(sp);
  180. return true;
  181. }
  182. public Dictionary<BakeType, Primitive.TextureEntryFace> GetBakedTextureFaces(UUID agentId)
  183. {
  184. ScenePresence sp = m_scene.GetScenePresence(agentId);
  185. if (sp == null)
  186. return new Dictionary<BakeType, Primitive.TextureEntryFace>();
  187. return GetBakedTextureFaces(sp);
  188. }
  189. public bool SaveBakedTextures(UUID agentId)
  190. {
  191. ScenePresence sp = m_scene.GetScenePresence(agentId);
  192. if (sp == null)
  193. return false;
  194. m_log.DebugFormat(
  195. "[AV FACTORY]: Permanently saving baked textures for {0} in {1}",
  196. sp.Name, m_scene.RegionInfo.RegionName);
  197. Dictionary<BakeType, Primitive.TextureEntryFace> bakedTextures = GetBakedTextureFaces(sp);
  198. if (bakedTextures.Count == 0)
  199. return false;
  200. foreach (BakeType bakeType in bakedTextures.Keys)
  201. {
  202. Primitive.TextureEntryFace bakedTextureFace = bakedTextures[bakeType];
  203. if (bakedTextureFace == null)
  204. {
  205. // This can happen legitimately, since some baked textures might not exist
  206. //m_log.WarnFormat(
  207. // "[AV FACTORY]: No texture ID set for {0} for {1} in {2} not found when trying to save permanently",
  208. // bakeType, sp.Name, m_scene.RegionInfo.RegionName);
  209. continue;
  210. }
  211. AssetBase asset = m_scene.AssetService.Get(bakedTextureFace.TextureID.ToString());
  212. if (asset != null)
  213. {
  214. asset.Temporary = false;
  215. asset.Local = false;
  216. m_scene.AssetService.Store(asset);
  217. }
  218. else
  219. {
  220. m_log.WarnFormat(
  221. "[AV FACTORY]: Baked texture id {0} not found for bake {1} for avatar {2} in {3} when trying to save permanently",
  222. bakedTextureFace.TextureID, bakeType, sp.Name, m_scene.RegionInfo.RegionName);
  223. }
  224. }
  225. return true;
  226. }
  227. /// <summary>
  228. /// Queue up a request to send appearance.
  229. /// </summary>
  230. /// <remarks>
  231. /// Makes it possible to accumulate changes without sending out each one separately.
  232. /// </remarks>
  233. /// <param name="agentId"></param>
  234. public void QueueAppearanceSend(UUID agentid)
  235. {
  236. // m_log.DebugFormat("[AVFACTORY]: Queue appearance send for {0}", agentid);
  237. // 10000 ticks per millisecond, 1000 milliseconds per second
  238. long timestamp = DateTime.Now.Ticks + Convert.ToInt64(m_sendtime * 1000 * 10000);
  239. lock (m_sendqueue)
  240. {
  241. m_sendqueue[agentid] = timestamp;
  242. m_updateTimer.Start();
  243. }
  244. }
  245. public void QueueAppearanceSave(UUID agentid)
  246. {
  247. // m_log.WarnFormat("[AVFACTORY]: Queue appearance save for {0}", agentid);
  248. // 10000 ticks per millisecond, 1000 milliseconds per second
  249. long timestamp = DateTime.Now.Ticks + Convert.ToInt64(m_savetime * 1000 * 10000);
  250. lock (m_savequeue)
  251. {
  252. m_savequeue[agentid] = timestamp;
  253. m_updateTimer.Start();
  254. }
  255. }
  256. public bool ValidateBakedTextureCache(IScenePresence sp)
  257. {
  258. bool defonly = true; // are we only using default textures
  259. // Process the texture entry
  260. for (int i = 0; i < AvatarAppearance.BAKE_INDICES.Length; i++)
  261. {
  262. int idx = AvatarAppearance.BAKE_INDICES[i];
  263. Primitive.TextureEntryFace face = sp.Appearance.Texture.FaceTextures[idx];
  264. // if there is no texture entry, skip it
  265. if (face == null)
  266. continue;
  267. // m_log.DebugFormat(
  268. // "[AVFACTORY]: Looking for texture {0}, id {1} for {2} {3}",
  269. // face.TextureID, idx, client.Name, client.AgentId);
  270. // if the texture is one of the "defaults" then skip it
  271. // this should probably be more intelligent (skirt texture doesnt matter
  272. // if the avatar isnt wearing a skirt) but if any of the main baked
  273. // textures is default then the rest should be as well
  274. if (face.TextureID == UUID.Zero || face.TextureID == AppearanceManager.DEFAULT_AVATAR_TEXTURE)
  275. continue;
  276. defonly = false; // found a non-default texture reference
  277. if (m_scene.AssetService.Get(face.TextureID.ToString()) == null)
  278. return false;
  279. }
  280. // m_log.DebugFormat("[AVFACTORY]: Completed texture check for {0} {1}", sp.Name, sp.UUID);
  281. // If we only found default textures, then the appearance is not cached
  282. return (defonly ? false : true);
  283. }
  284. public int RequestRebake(IScenePresence sp, bool missingTexturesOnly)
  285. {
  286. int texturesRebaked = 0;
  287. for (int i = 0; i < AvatarAppearance.BAKE_INDICES.Length; i++)
  288. {
  289. int idx = AvatarAppearance.BAKE_INDICES[i];
  290. Primitive.TextureEntryFace face = sp.Appearance.Texture.FaceTextures[idx];
  291. // if there is no texture entry, skip it
  292. if (face == null)
  293. continue;
  294. // m_log.DebugFormat(
  295. // "[AVFACTORY]: Looking for texture {0}, id {1} for {2} {3}",
  296. // face.TextureID, idx, client.Name, client.AgentId);
  297. // if the texture is one of the "defaults" then skip it
  298. // this should probably be more intelligent (skirt texture doesnt matter
  299. // if the avatar isnt wearing a skirt) but if any of the main baked
  300. // textures is default then the rest should be as well
  301. if (face.TextureID == UUID.Zero || face.TextureID == AppearanceManager.DEFAULT_AVATAR_TEXTURE)
  302. continue;
  303. if (missingTexturesOnly)
  304. {
  305. if (m_scene.AssetService.Get(face.TextureID.ToString()) != null)
  306. {
  307. continue;
  308. }
  309. else
  310. {
  311. // On inter-simulator teleports, this occurs if baked textures are not being stored by the
  312. // grid asset service (which means that they are not available to the new region and so have
  313. // to be re-requested from the client).
  314. //
  315. // The only available core OpenSimulator behaviour right now
  316. // is not to store these textures, temporarily or otherwise.
  317. m_log.DebugFormat(
  318. "[AVFACTORY]: Missing baked texture {0} ({1}) for {2}, requesting rebake.",
  319. face.TextureID, idx, sp.Name);
  320. }
  321. }
  322. else
  323. {
  324. m_log.DebugFormat(
  325. "[AVFACTORY]: Requesting rebake of {0} ({1}) for {2}.",
  326. face.TextureID, idx, sp.Name);
  327. }
  328. texturesRebaked++;
  329. sp.ControllingClient.SendRebakeAvatarTextures(face.TextureID);
  330. }
  331. return texturesRebaked;
  332. }
  333. #endregion
  334. #region AvatarFactoryModule private methods
  335. private Dictionary<BakeType, Primitive.TextureEntryFace> GetBakedTextureFaces(ScenePresence sp)
  336. {
  337. if (sp.IsChildAgent)
  338. return new Dictionary<BakeType, Primitive.TextureEntryFace>();
  339. Dictionary<BakeType, Primitive.TextureEntryFace> bakedTextures
  340. = new Dictionary<BakeType, Primitive.TextureEntryFace>();
  341. AvatarAppearance appearance = sp.Appearance;
  342. Primitive.TextureEntryFace[] faceTextures = appearance.Texture.FaceTextures;
  343. foreach (int i in Enum.GetValues(typeof(BakeType)))
  344. {
  345. BakeType bakeType = (BakeType)i;
  346. if (bakeType == BakeType.Unknown)
  347. continue;
  348. // m_log.DebugFormat(
  349. // "[AVFACTORY]: NPC avatar {0} has texture id {1} : {2}",
  350. // acd.AgentID, i, acd.Appearance.Texture.FaceTextures[i]);
  351. int ftIndex = (int)AppearanceManager.BakeTypeToAgentTextureIndex(bakeType);
  352. Primitive.TextureEntryFace texture = faceTextures[ftIndex]; // this will be null if there's no such baked texture
  353. bakedTextures[bakeType] = texture;
  354. }
  355. return bakedTextures;
  356. }
  357. private void HandleAppearanceUpdateTimer(object sender, EventArgs ea)
  358. {
  359. long now = DateTime.Now.Ticks;
  360. lock (m_sendqueue)
  361. {
  362. Dictionary<UUID, long> sends = new Dictionary<UUID, long>(m_sendqueue);
  363. foreach (KeyValuePair<UUID, long> kvp in sends)
  364. {
  365. // We have to load the key and value into local parameters to avoid a race condition if we loop
  366. // around and load kvp with a different value before FireAndForget has launched its thread.
  367. UUID avatarID = kvp.Key;
  368. long sendTime = kvp.Value;
  369. // m_log.DebugFormat("[AVFACTORY]: Handling queued appearance updates for {0}, update delta to now is {1}", avatarID, sendTime - now);
  370. if (sendTime < now)
  371. {
  372. Util.FireAndForget(o => SendAppearance(avatarID));
  373. m_sendqueue.Remove(avatarID);
  374. }
  375. }
  376. }
  377. lock (m_savequeue)
  378. {
  379. Dictionary<UUID, long> saves = new Dictionary<UUID, long>(m_savequeue);
  380. foreach (KeyValuePair<UUID, long> kvp in saves)
  381. {
  382. // We have to load the key and value into local parameters to avoid a race condition if we loop
  383. // around and load kvp with a different value before FireAndForget has launched its thread.
  384. UUID avatarID = kvp.Key;
  385. long sendTime = kvp.Value;
  386. if (sendTime < now)
  387. {
  388. Util.FireAndForget(o => SaveAppearance(avatarID));
  389. m_savequeue.Remove(avatarID);
  390. }
  391. }
  392. // We must lock both queues here so that QueueAppearanceSave() or *Send() don't m_updateTimer.Start() on
  393. // another thread inbetween the first count calls and m_updateTimer.Stop() on this thread.
  394. lock (m_sendqueue)
  395. if (m_savequeue.Count == 0 && m_sendqueue.Count == 0)
  396. m_updateTimer.Stop();
  397. }
  398. }
  399. private void SaveAppearance(UUID agentid)
  400. {
  401. // We must set appearance parameters in the en_US culture in order to avoid issues where values are saved
  402. // in a culture where decimal points are commas and then reloaded in a culture which just treats them as
  403. // number seperators.
  404. Culture.SetCurrentCulture();
  405. ScenePresence sp = m_scene.GetScenePresence(agentid);
  406. if (sp == null)
  407. {
  408. // This is expected if the user has gone away.
  409. // m_log.DebugFormat("[AVFACTORY]: Agent {0} no longer in the scene", agentid);
  410. return;
  411. }
  412. // m_log.WarnFormat("[AVFACTORY] avatar {0} save appearance",agentid);
  413. // This could take awhile since it needs to pull inventory
  414. // We need to do it at the point of save so that there is a sufficient delay for any upload of new body part/shape
  415. // assets and item asset id changes to complete.
  416. // I don't think we need to worry about doing this within m_setAppearanceLock since the queueing avoids
  417. // multiple save requests.
  418. SetAppearanceAssets(sp.UUID, sp.Appearance);
  419. m_scene.AvatarService.SetAppearance(agentid, sp.Appearance);
  420. // Trigger this here because it's the final step in the set/queue/save process for appearance setting.
  421. // Everything has been updated and stored. Ensures bakes have been persisted (if option is set to persist bakes).
  422. m_scene.EventManager.TriggerAvatarAppearanceChanged(sp);
  423. }
  424. private void SetAppearanceAssets(UUID userID, AvatarAppearance appearance)
  425. {
  426. IInventoryService invService = m_scene.InventoryService;
  427. if (invService.GetRootFolder(userID) != null)
  428. {
  429. for (int i = 0; i < AvatarWearable.MAX_WEARABLES; i++)
  430. {
  431. for (int j = 0; j < appearance.Wearables[i].Count; j++)
  432. {
  433. if (appearance.Wearables[i][j].ItemID == UUID.Zero)
  434. continue;
  435. // Ignore ruth's assets
  436. if (appearance.Wearables[i][j].ItemID == AvatarWearable.DefaultWearables[i][0].ItemID)
  437. continue;
  438. InventoryItemBase baseItem = new InventoryItemBase(appearance.Wearables[i][j].ItemID, userID);
  439. baseItem = invService.GetItem(baseItem);
  440. if (baseItem != null)
  441. {
  442. appearance.Wearables[i].Add(appearance.Wearables[i][j].ItemID, baseItem.AssetID);
  443. }
  444. else
  445. {
  446. m_log.ErrorFormat(
  447. "[AVFACTORY]: Can't find inventory item {0} for {1}, setting to default",
  448. appearance.Wearables[i][j].ItemID, (WearableType)i);
  449. appearance.Wearables[i].RemoveItem(appearance.Wearables[i][j].ItemID);
  450. }
  451. }
  452. }
  453. }
  454. else
  455. {
  456. m_log.WarnFormat("[AVFACTORY]: user {0} has no inventory, appearance isn't going to work", userID);
  457. }
  458. }
  459. #endregion
  460. #region Client Event Handlers
  461. /// <summary>
  462. /// Tell the client for this scene presence what items it should be wearing now
  463. /// </summary>
  464. /// <param name="client"></param>
  465. private void Client_OnRequestWearables(IClientAPI client)
  466. {
  467. // m_log.DebugFormat("[AVFACTORY]: Client_OnRequestWearables called for {0} ({1})", client.Name, client.AgentId);
  468. ScenePresence sp = m_scene.GetScenePresence(client.AgentId);
  469. if (sp != null)
  470. client.SendWearables(sp.Appearance.Wearables, sp.Appearance.Serial++);
  471. else
  472. m_log.WarnFormat("[AVFACTORY]: Client_OnRequestWearables unable to find presence for {0}", client.AgentId);
  473. }
  474. /// <summary>
  475. /// Set appearance data (texture asset IDs and slider settings) received from a client
  476. /// </summary>
  477. /// <param name="client"></param>
  478. /// <param name="texture"></param>
  479. /// <param name="visualParam"></param>
  480. private void Client_OnSetAppearance(IClientAPI client, Primitive.TextureEntry textureEntry, byte[] visualParams)
  481. {
  482. // m_log.WarnFormat("[AVFACTORY]: Client_OnSetAppearance called for {0} ({1})", client.Name, client.AgentId);
  483. ScenePresence sp = m_scene.GetScenePresence(client.AgentId);
  484. if (sp != null)
  485. SetAppearance(sp, textureEntry, visualParams);
  486. else
  487. m_log.WarnFormat("[AVFACTORY]: Client_OnSetAppearance unable to find presence for {0}", client.AgentId);
  488. }
  489. /// <summary>
  490. /// Update what the avatar is wearing using an item from their inventory.
  491. /// </summary>
  492. /// <param name="client"></param>
  493. /// <param name="e"></param>
  494. private void Client_OnAvatarNowWearing(IClientAPI client, AvatarWearingArgs e)
  495. {
  496. // m_log.WarnFormat("[AVFACTORY]: Client_OnAvatarNowWearing called for {0} ({1})", client.Name, client.AgentId);
  497. ScenePresence sp = m_scene.GetScenePresence(client.AgentId);
  498. if (sp == null)
  499. {
  500. m_log.WarnFormat("[AVFACTORY]: Client_OnAvatarNowWearing unable to find presence for {0}", client.AgentId);
  501. return;
  502. }
  503. // we need to clean out the existing textures
  504. sp.Appearance.ResetAppearance();
  505. // operate on a copy of the appearance so we don't have to lock anything yet
  506. AvatarAppearance avatAppearance = new AvatarAppearance(sp.Appearance, false);
  507. foreach (AvatarWearingArgs.Wearable wear in e.NowWearing)
  508. {
  509. if (wear.Type < AvatarWearable.MAX_WEARABLES)
  510. avatAppearance.Wearables[wear.Type].Add(wear.ItemID, UUID.Zero);
  511. }
  512. avatAppearance.GetAssetsFrom(sp.Appearance);
  513. lock (m_setAppearanceLock)
  514. {
  515. // Update only those fields that we have changed. This is important because the viewer
  516. // often sends AvatarIsWearing and SetAppearance packets at once, and AvatarIsWearing
  517. // shouldn't overwrite the changes made in SetAppearance.
  518. sp.Appearance.Wearables = avatAppearance.Wearables;
  519. sp.Appearance.Texture = avatAppearance.Texture;
  520. // We don't need to send the appearance here since the "iswearing" will trigger a new set
  521. // of visual param and baked texture changes. When those complete, the new appearance will be sent
  522. QueueAppearanceSave(client.AgentId);
  523. }
  524. }
  525. #endregion
  526. public void WriteBakedTexturesReport(IScenePresence sp, ReportOutputAction outputAction)
  527. {
  528. outputAction("For {0} in {1}", sp.Name, m_scene.RegionInfo.RegionName);
  529. outputAction(BAKED_TEXTURES_REPORT_FORMAT, "Bake Type", "UUID");
  530. Dictionary<BakeType, Primitive.TextureEntryFace> bakedTextures = GetBakedTextureFaces(sp.UUID);
  531. foreach (BakeType bt in bakedTextures.Keys)
  532. {
  533. string rawTextureID;
  534. if (bakedTextures[bt] == null)
  535. {
  536. rawTextureID = "not set";
  537. }
  538. else
  539. {
  540. rawTextureID = bakedTextures[bt].TextureID.ToString();
  541. if (m_scene.AssetService.Get(rawTextureID) == null)
  542. rawTextureID += " (not found)";
  543. else
  544. rawTextureID += " (uploaded)";
  545. }
  546. outputAction(BAKED_TEXTURES_REPORT_FORMAT, bt, rawTextureID);
  547. }
  548. bool bakedTextureValid = m_scene.AvatarFactory.ValidateBakedTextureCache(sp);
  549. outputAction("{0} baked appearance texture is {1}", sp.Name, bakedTextureValid ? "OK" : "corrupt");
  550. }
  551. }
  552. }