Bot.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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.Text;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Threading;
  33. using System.Timers;
  34. using log4net;
  35. using OpenMetaverse;
  36. using OpenMetaverse.Assets;
  37. using Nini.Config;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Console;
  40. using pCampBot.Interfaces;
  41. using Timer = System.Timers.Timer;
  42. using PermissionMask = OpenSim.Framework.PermissionMask;
  43. namespace pCampBot
  44. {
  45. public enum ConnectionState
  46. {
  47. Disconnected,
  48. Connecting,
  49. Connected,
  50. Disconnecting
  51. }
  52. public class Bot
  53. {
  54. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  55. public delegate void AnEvent(Bot callbot, EventType someevent); // event delegate for bot events
  56. /// <summary>
  57. /// Controls whether bots request textures for the object information they receive
  58. /// </summary>
  59. public bool RequestObjectTextures { get; set; }
  60. /// <summary>
  61. /// Bot manager.
  62. /// </summary>
  63. public BotManager Manager { get; private set; }
  64. /// <summary>
  65. /// Behaviours implemented by this bot.
  66. /// </summary>
  67. /// <remarks>
  68. /// Indexed by abbreviated name. There can only be one instance of a particular behaviour.
  69. /// Lock this structure before manipulating it.
  70. /// </remarks>
  71. public Dictionary<string, IBehaviour> Behaviours { get; private set; }
  72. /// <summary>
  73. /// Objects that the bot has discovered.
  74. /// </summary>
  75. /// <remarks>
  76. /// Returns a list copy. Inserting new objects manually will have no effect.
  77. /// </remarks>
  78. public Dictionary<UUID, Primitive> Objects
  79. {
  80. get
  81. {
  82. lock (m_objects)
  83. return new Dictionary<UUID, Primitive>(m_objects);
  84. }
  85. }
  86. private Dictionary<UUID, Primitive> m_objects = new Dictionary<UUID, Primitive>();
  87. /// <summary>
  88. /// Is this bot connected to the grid?
  89. /// </summary>
  90. public ConnectionState ConnectionState { get; private set; }
  91. public List<Simulator> Simulators
  92. {
  93. get
  94. {
  95. lock (Client.Network.Simulators)
  96. return new List<Simulator>(Client.Network.Simulators);
  97. }
  98. }
  99. /// <summary>
  100. /// The number of connections that this bot has to different simulators.
  101. /// </summary>
  102. /// <value>Includes both root and child connections.</value>
  103. public int SimulatorsCount
  104. {
  105. get
  106. {
  107. lock (Client.Network.Simulators)
  108. return Client.Network.Simulators.Count;
  109. }
  110. }
  111. public string FirstName { get; private set; }
  112. public string LastName { get; private set; }
  113. public string Name { get; private set; }
  114. public string Password { get; private set; }
  115. public string LoginUri { get; private set; }
  116. public string StartLocation { get; private set; }
  117. public string saveDir;
  118. public string wear;
  119. public event AnEvent OnConnected;
  120. public event AnEvent OnDisconnected;
  121. /// <summary>
  122. /// Keep a track of the continuously acting thread so that we can abort it.
  123. /// </summary>
  124. private Thread m_actionThread;
  125. protected List<uint> objectIDs = new List<uint>();
  126. /// <summary>
  127. /// Random number generator.
  128. /// </summary>
  129. public Random Random { get; private set; }
  130. /// <summary>
  131. /// New instance of a SecondLife client
  132. /// </summary>
  133. public GridClient Client { get; private set; }
  134. /// <summary>
  135. /// Constructor
  136. /// </summary>
  137. /// <param name="bm"></param>
  138. /// <param name="behaviours">Behaviours for this bot to perform</param>
  139. /// <param name="firstName"></param>
  140. /// <param name="lastName"></param>
  141. /// <param name="password"></param>
  142. /// <param name="loginUri"></param>
  143. /// <param name="behaviours"></param>
  144. public Bot(
  145. BotManager bm, List<IBehaviour> behaviours,
  146. string firstName, string lastName, string password, string startLocation, string loginUri)
  147. {
  148. ConnectionState = ConnectionState.Disconnected;
  149. Random = new Random(Environment.TickCount);// We do stuff randomly here
  150. FirstName = firstName;
  151. LastName = lastName;
  152. Name = string.Format("{0} {1}", FirstName, LastName);
  153. Password = password;
  154. LoginUri = loginUri;
  155. StartLocation = startLocation;
  156. Manager = bm;
  157. Behaviours = new Dictionary<string, IBehaviour>();
  158. foreach (IBehaviour behaviour in behaviours)
  159. AddBehaviour(behaviour);
  160. // Only calling for use as a template.
  161. CreateLibOmvClient();
  162. }
  163. public bool TryGetBehaviour(string abbreviatedName, out IBehaviour behaviour)
  164. {
  165. lock (Behaviours)
  166. return Behaviours.TryGetValue(abbreviatedName, out behaviour);
  167. }
  168. public bool AddBehaviour(IBehaviour behaviour)
  169. {
  170. lock (Behaviours)
  171. {
  172. if (!Behaviours.ContainsKey(behaviour.AbbreviatedName))
  173. {
  174. behaviour.Initialize(this);
  175. Behaviours.Add(behaviour.AbbreviatedName, behaviour);
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. public bool RemoveBehaviour(string abbreviatedName)
  182. {
  183. lock (Behaviours)
  184. {
  185. IBehaviour behaviour;
  186. if (!Behaviours.TryGetValue(abbreviatedName, out behaviour))
  187. return false;
  188. behaviour.Close();
  189. Behaviours.Remove(abbreviatedName);
  190. return true;
  191. }
  192. }
  193. private void CreateLibOmvClient()
  194. {
  195. GridClient newClient = new GridClient();
  196. if (Client != null)
  197. {
  198. newClient.Settings.LOGIN_SERVER = Client.Settings.LOGIN_SERVER;
  199. newClient.Settings.ALWAYS_DECODE_OBJECTS = Client.Settings.ALWAYS_DECODE_OBJECTS;
  200. newClient.Settings.AVATAR_TRACKING = Client.Settings.AVATAR_TRACKING;
  201. newClient.Settings.OBJECT_TRACKING = Client.Settings.OBJECT_TRACKING;
  202. newClient.Settings.SEND_AGENT_THROTTLE = Client.Settings.SEND_AGENT_THROTTLE;
  203. newClient.Settings.SEND_AGENT_UPDATES = Client.Settings.SEND_AGENT_UPDATES;
  204. newClient.Settings.SEND_PINGS = Client.Settings.SEND_PINGS;
  205. newClient.Settings.STORE_LAND_PATCHES = Client.Settings.STORE_LAND_PATCHES;
  206. newClient.Settings.USE_ASSET_CACHE = Client.Settings.USE_ASSET_CACHE;
  207. newClient.Settings.MULTIPLE_SIMS = Client.Settings.MULTIPLE_SIMS;
  208. newClient.Throttle.Asset = Client.Throttle.Asset;
  209. newClient.Throttle.Land = Client.Throttle.Land;
  210. newClient.Throttle.Task = Client.Throttle.Task;
  211. newClient.Throttle.Texture = Client.Throttle.Texture;
  212. newClient.Throttle.Wind = Client.Throttle.Wind;
  213. newClient.Throttle.Total = Client.Throttle.Total;
  214. }
  215. else
  216. {
  217. newClient.Settings.LOGIN_SERVER = LoginUri;
  218. newClient.Settings.ALWAYS_DECODE_OBJECTS = false;
  219. newClient.Settings.AVATAR_TRACKING = false;
  220. newClient.Settings.OBJECT_TRACKING = false;
  221. newClient.Settings.SEND_AGENT_THROTTLE = true;
  222. newClient.Settings.SEND_PINGS = true;
  223. newClient.Settings.STORE_LAND_PATCHES = false;
  224. newClient.Settings.USE_ASSET_CACHE = false;
  225. newClient.Settings.MULTIPLE_SIMS = true;
  226. newClient.Throttle.Asset = 100000;
  227. newClient.Throttle.Land = 100000;
  228. newClient.Throttle.Task = 100000;
  229. newClient.Throttle.Texture = 100000;
  230. newClient.Throttle.Wind = 100000;
  231. newClient.Throttle.Total = 400000;
  232. }
  233. newClient.Network.LoginProgress += this.Network_LoginProgress;
  234. newClient.Network.SimConnected += this.Network_SimConnected;
  235. newClient.Network.Disconnected += this.Network_OnDisconnected;
  236. newClient.Objects.ObjectUpdate += Objects_NewPrim;
  237. Client = newClient;
  238. }
  239. //We do our actions here. This is where one would
  240. //add additional steps and/or things the bot should do
  241. private void Action()
  242. {
  243. while (ConnectionState != ConnectionState.Disconnecting)
  244. {
  245. lock (Behaviours)
  246. {
  247. foreach (IBehaviour behaviour in Behaviours.Values)
  248. {
  249. // Thread.Sleep(Random.Next(3000, 10000));
  250. // m_log.DebugFormat("[pCAMPBOT]: For {0} performing action {1}", Name, b.GetType());
  251. behaviour.Action();
  252. }
  253. }
  254. // XXX: This is a really shitty way of yielding so that behaviours can be added/removed
  255. Thread.Sleep(100);
  256. }
  257. lock (Behaviours)
  258. foreach (IBehaviour b in Behaviours.Values)
  259. b.Close();
  260. }
  261. /// <summary>
  262. /// Tells LibSecondLife to logout and disconnect. Raises the disconnect events once it finishes.
  263. /// </summary>
  264. public void Disconnect()
  265. {
  266. ConnectionState = ConnectionState.Disconnecting;
  267. // if (m_actionThread != null)
  268. // m_actionThread.Abort();
  269. Client.Network.Logout();
  270. }
  271. public void Connect()
  272. {
  273. Thread connectThread = new Thread(ConnectInternal);
  274. connectThread.Name = Name;
  275. connectThread.IsBackground = true;
  276. connectThread.Start();
  277. }
  278. /// <summary>
  279. /// This is the bot startup loop.
  280. /// </summary>
  281. private void ConnectInternal()
  282. {
  283. ConnectionState = ConnectionState.Connecting;
  284. // Current create a new client on each connect. libomv doesn't seem to process new sim
  285. // information (e.g. EstablishAgentCommunication events) if connecting after a disceonnect with the same
  286. // client
  287. CreateLibOmvClient();
  288. if (Client.Network.Login(FirstName, LastName, Password, "pCampBot", StartLocation, "Your name"))
  289. {
  290. ConnectionState = ConnectionState.Connected;
  291. Thread.Sleep(Random.Next(1000, 10000));
  292. m_actionThread = new Thread(Action);
  293. m_actionThread.Start();
  294. // OnConnected(this, EventType.CONNECTED);
  295. if (wear == "save")
  296. {
  297. Client.Appearance.SetPreviousAppearance();
  298. SaveDefaultAppearance();
  299. }
  300. else if (wear != "no")
  301. {
  302. MakeDefaultAppearance(wear);
  303. }
  304. // Extract nearby region information.
  305. Client.Grid.GridRegion += Manager.Grid_GridRegion;
  306. uint xUint, yUint;
  307. Utils.LongToUInts(Client.Network.CurrentSim.Handle, out xUint, out yUint);
  308. ushort minX, minY, maxX, maxY;
  309. minX = (ushort)Math.Min(0, xUint - 5);
  310. minY = (ushort)Math.Min(0, yUint - 5);
  311. maxX = (ushort)(xUint + 5);
  312. maxY = (ushort)(yUint + 5);
  313. Client.Grid.RequestMapBlocks(GridLayerType.Terrain, minX, minY, maxX, maxY, false);
  314. }
  315. else
  316. {
  317. ConnectionState = ConnectionState.Disconnected;
  318. m_log.ErrorFormat(
  319. "{0} {1} cannot login: {2}", FirstName, LastName, Client.Network.LoginMessage);
  320. if (OnDisconnected != null)
  321. {
  322. OnDisconnected(this, EventType.DISCONNECTED);
  323. }
  324. }
  325. }
  326. /// <summary>
  327. /// Sit this bot on the ground.
  328. /// </summary>
  329. public void SitOnGround()
  330. {
  331. if (ConnectionState == ConnectionState.Connected)
  332. Client.Self.SitOnGround();
  333. }
  334. /// <summary>
  335. /// Stand this bot
  336. /// </summary>
  337. public void Stand()
  338. {
  339. if (ConnectionState == ConnectionState.Connected)
  340. {
  341. // Unlike sit on ground, here libomv checks whether we have SEND_AGENT_UPDATES enabled.
  342. bool prevUpdatesSetting = Client.Settings.SEND_AGENT_UPDATES;
  343. Client.Settings.SEND_AGENT_UPDATES = true;
  344. Client.Self.Stand();
  345. Client.Settings.SEND_AGENT_UPDATES = prevUpdatesSetting;
  346. }
  347. }
  348. public void SaveDefaultAppearance()
  349. {
  350. saveDir = "MyAppearance/" + FirstName + "_" + LastName;
  351. if (!Directory.Exists(saveDir))
  352. {
  353. Directory.CreateDirectory(saveDir);
  354. }
  355. Array wtypes = Enum.GetValues(typeof(WearableType));
  356. foreach (WearableType wtype in wtypes)
  357. {
  358. UUID wearable = Client.Appearance.GetWearableAsset(wtype);
  359. if (wearable != UUID.Zero)
  360. {
  361. Client.Assets.RequestAsset(wearable, AssetType.Clothing, false, Asset_ReceivedCallback);
  362. Client.Assets.RequestAsset(wearable, AssetType.Bodypart, false, Asset_ReceivedCallback);
  363. }
  364. }
  365. }
  366. public void SaveAsset(AssetWearable asset)
  367. {
  368. if (asset != null)
  369. {
  370. try
  371. {
  372. if (asset.Decode())
  373. {
  374. File.WriteAllBytes(Path.Combine(saveDir, String.Format("{1}.{0}",
  375. asset.AssetType.ToString().ToLower(),
  376. asset.WearableType)), asset.AssetData);
  377. }
  378. else
  379. {
  380. m_log.WarnFormat("Failed to decode {0} asset {1}", asset.AssetType, asset.AssetID);
  381. }
  382. }
  383. catch (Exception e)
  384. {
  385. m_log.ErrorFormat("Exception: {0}{1}", e.Message, e.StackTrace);
  386. }
  387. }
  388. }
  389. public WearableType GetWearableType(string path)
  390. {
  391. string type = ((((path.Split('/'))[2]).Split('.'))[0]).Trim();
  392. switch (type)
  393. {
  394. case "Eyes":
  395. return WearableType.Eyes;
  396. case "Hair":
  397. return WearableType.Hair;
  398. case "Pants":
  399. return WearableType.Pants;
  400. case "Shape":
  401. return WearableType.Shape;
  402. case "Shirt":
  403. return WearableType.Shirt;
  404. case "Skin":
  405. return WearableType.Skin;
  406. default:
  407. return WearableType.Shape;
  408. }
  409. }
  410. public void MakeDefaultAppearance(string wear)
  411. {
  412. try
  413. {
  414. if (wear == "yes")
  415. {
  416. //TODO: Implement random outfit picking
  417. m_log.DebugFormat("Picks a random outfit. Not yet implemented.");
  418. }
  419. else if (wear != "save")
  420. saveDir = "MyAppearance/" + wear;
  421. saveDir = saveDir + "/";
  422. string[] clothing = Directory.GetFiles(saveDir, "*.clothing", SearchOption.TopDirectoryOnly);
  423. string[] bodyparts = Directory.GetFiles(saveDir, "*.bodypart", SearchOption.TopDirectoryOnly);
  424. InventoryFolder clothfolder = FindClothingFolder();
  425. UUID transid = UUID.Random();
  426. List<InventoryBase> listwearables = new List<InventoryBase>();
  427. for (int i = 0; i < clothing.Length; i++)
  428. {
  429. UUID assetID = UUID.Random();
  430. AssetClothing asset = new AssetClothing(assetID, File.ReadAllBytes(clothing[i]));
  431. asset.Decode();
  432. asset.Owner = Client.Self.AgentID;
  433. asset.WearableType = GetWearableType(clothing[i]);
  434. asset.Encode();
  435. transid = Client.Assets.RequestUpload(asset,true);
  436. Client.Inventory.RequestCreateItem(clothfolder.UUID, "MyClothing" + i.ToString(), "MyClothing", AssetType.Clothing,
  437. transid, InventoryType.Wearable, asset.WearableType, (OpenMetaverse.PermissionMask)PermissionMask.All, delegate(bool success, InventoryItem item)
  438. {
  439. if (success)
  440. {
  441. listwearables.Add(item);
  442. }
  443. else
  444. {
  445. m_log.WarnFormat("Failed to create item {0}", item.Name);
  446. }
  447. }
  448. );
  449. }
  450. for (int i = 0; i < bodyparts.Length; i++)
  451. {
  452. UUID assetID = UUID.Random();
  453. AssetBodypart asset = new AssetBodypart(assetID, File.ReadAllBytes(bodyparts[i]));
  454. asset.Decode();
  455. asset.Owner = Client.Self.AgentID;
  456. asset.WearableType = GetWearableType(bodyparts[i]);
  457. asset.Encode();
  458. transid = Client.Assets.RequestUpload(asset,true);
  459. Client.Inventory.RequestCreateItem(clothfolder.UUID, "MyBodyPart" + i.ToString(), "MyBodyPart", AssetType.Bodypart,
  460. transid, InventoryType.Wearable, asset.WearableType, (OpenMetaverse.PermissionMask)PermissionMask.All, delegate(bool success, InventoryItem item)
  461. {
  462. if (success)
  463. {
  464. listwearables.Add(item);
  465. }
  466. else
  467. {
  468. m_log.WarnFormat("Failed to create item {0}", item.Name);
  469. }
  470. }
  471. );
  472. }
  473. Thread.Sleep(1000);
  474. if (listwearables == null || listwearables.Count == 0)
  475. {
  476. m_log.DebugFormat("Nothing to send on this folder!");
  477. }
  478. else
  479. {
  480. m_log.DebugFormat("Sending {0} wearables...", listwearables.Count);
  481. Client.Appearance.WearOutfit(listwearables, false);
  482. }
  483. }
  484. catch (Exception ex)
  485. {
  486. Console.WriteLine(ex.ToString());
  487. }
  488. }
  489. public InventoryFolder FindClothingFolder()
  490. {
  491. UUID rootfolder = Client.Inventory.Store.RootFolder.UUID;
  492. List<InventoryBase> listfolders = Client.Inventory.Store.GetContents(rootfolder);
  493. InventoryFolder clothfolder = new InventoryFolder(UUID.Random());
  494. foreach (InventoryBase folder in listfolders)
  495. {
  496. if (folder.Name == "Clothing")
  497. {
  498. clothfolder = (InventoryFolder)folder;
  499. break;
  500. }
  501. }
  502. return clothfolder;
  503. }
  504. public void Network_LoginProgress(object sender, LoginProgressEventArgs args)
  505. {
  506. m_log.DebugFormat("[BOT]: Bot {0} {1} in Network_LoginProcess", Name, args.Status);
  507. if (args.Status == LoginStatus.Success)
  508. {
  509. if (OnConnected != null)
  510. {
  511. OnConnected(this, EventType.CONNECTED);
  512. }
  513. }
  514. }
  515. public void Network_SimConnected(object sender, SimConnectedEventArgs args)
  516. {
  517. m_log.DebugFormat(
  518. "[BOT]: Bot {0} connected to {1} at {2}", Name, args.Simulator.Name, args.Simulator.IPEndPoint);
  519. }
  520. public void Network_OnDisconnected(object sender, DisconnectedEventArgs args)
  521. {
  522. ConnectionState = ConnectionState.Disconnected;
  523. m_log.DebugFormat(
  524. "[BOT]: Bot {0} disconnected reason {1}, message {2}", Name, args.Reason, args.Message);
  525. // m_log.ErrorFormat("Fired Network_OnDisconnected");
  526. // if (
  527. // (args.Reason == NetworkManager.DisconnectType.SimShutdown
  528. // || args.Reason == NetworkManager.DisconnectType.NetworkTimeout)
  529. // && OnDisconnected != null)
  530. if (
  531. (args.Reason == NetworkManager.DisconnectType.ClientInitiated
  532. || args.Reason == NetworkManager.DisconnectType.ServerInitiated
  533. || args.Reason == NetworkManager.DisconnectType.NetworkTimeout)
  534. && OnDisconnected != null)
  535. // if (OnDisconnected != null)
  536. {
  537. OnDisconnected(this, EventType.DISCONNECTED);
  538. }
  539. }
  540. public void Objects_NewPrim(object sender, PrimEventArgs args)
  541. {
  542. if (!RequestObjectTextures)
  543. return;
  544. Primitive prim = args.Prim;
  545. if (prim != null)
  546. {
  547. lock (m_objects)
  548. m_objects[prim.ID] = prim;
  549. if (prim.Textures != null)
  550. {
  551. if (prim.Textures.DefaultTexture.TextureID != UUID.Zero)
  552. {
  553. GetTexture(prim.Textures.DefaultTexture.TextureID);
  554. }
  555. for (int i = 0; i < prim.Textures.FaceTextures.Length; i++)
  556. {
  557. Primitive.TextureEntryFace face = prim.Textures.FaceTextures[i];
  558. if (face != null)
  559. {
  560. UUID textureID = prim.Textures.FaceTextures[i].TextureID;
  561. if (textureID != UUID.Zero)
  562. GetTexture(textureID);
  563. }
  564. }
  565. }
  566. if (prim.Sculpt != null && prim.Sculpt.SculptTexture != UUID.Zero)
  567. GetTexture(prim.Sculpt.SculptTexture);
  568. }
  569. }
  570. private void GetTexture(UUID textureID)
  571. {
  572. lock (Manager.AssetsReceived)
  573. {
  574. // Don't request assets more than once.
  575. if (Manager.AssetsReceived.ContainsKey(textureID))
  576. return;
  577. Manager.AssetsReceived[textureID] = false;
  578. Client.Assets.RequestImage(textureID, ImageType.Normal, Asset_TextureCallback_Texture);
  579. }
  580. }
  581. public void Asset_TextureCallback_Texture(TextureRequestState state, AssetTexture assetTexture)
  582. {
  583. //TODO: Implement texture saving and applying
  584. }
  585. public void Asset_ReceivedCallback(AssetDownload transfer, Asset asset)
  586. {
  587. lock (Manager.AssetsReceived)
  588. Manager.AssetsReceived[asset.AssetID] = true;
  589. // if (wear == "save")
  590. // {
  591. // SaveAsset((AssetWearable) asset);
  592. // }
  593. }
  594. }
  595. }