Bot.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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(bm.Rng.Next());
  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 += Network_LoginProgress;
  234. newClient.Network.SimConnected += Network_SimConnected;
  235. newClient.Network.SimDisconnected += Network_SimDisconnected;
  236. newClient.Network.Disconnected += Network_OnDisconnected;
  237. newClient.Objects.ObjectUpdate += Objects_NewPrim;
  238. Client = newClient;
  239. }
  240. //We do our actions here. This is where one would
  241. //add additional steps and/or things the bot should do
  242. private void Action()
  243. {
  244. while (ConnectionState == ConnectionState.Connected)
  245. {
  246. lock (Behaviours)
  247. {
  248. foreach (IBehaviour behaviour in Behaviours.Values)
  249. {
  250. // Thread.Sleep(Random.Next(3000, 10000));
  251. // m_log.DebugFormat("[pCAMPBOT]: For {0} performing action {1}", Name, b.GetType());
  252. behaviour.Action();
  253. }
  254. }
  255. // XXX: This is a really shitty way of yielding so that behaviours can be added/removed
  256. Thread.Sleep(100);
  257. }
  258. lock (Behaviours)
  259. foreach (IBehaviour b in Behaviours.Values)
  260. b.Close();
  261. }
  262. /// <summary>
  263. /// Tells LibSecondLife to logout and disconnect. Raises the disconnect events once it finishes.
  264. /// </summary>
  265. public void Disconnect()
  266. {
  267. ConnectionState = ConnectionState.Disconnecting;
  268. // if (m_actionThread != null)
  269. // m_actionThread.Abort();
  270. Client.Network.Logout();
  271. }
  272. public void Connect()
  273. {
  274. Thread connectThread = new Thread(ConnectInternal);
  275. connectThread.Name = Name;
  276. connectThread.IsBackground = true;
  277. connectThread.Start();
  278. }
  279. /// <summary>
  280. /// This is the bot startup loop.
  281. /// </summary>
  282. private void ConnectInternal()
  283. {
  284. ConnectionState = ConnectionState.Connecting;
  285. // Current create a new client on each connect. libomv doesn't seem to process new sim
  286. // information (e.g. EstablishAgentCommunication events) if connecting after a disceonnect with the same
  287. // client
  288. CreateLibOmvClient();
  289. if (Client.Network.Login(FirstName, LastName, Password, "pCampBot", StartLocation, "Your name"))
  290. {
  291. ConnectionState = ConnectionState.Connected;
  292. Thread.Sleep(Random.Next(1000, 10000));
  293. m_actionThread = new Thread(Action);
  294. m_actionThread.Start();
  295. // OnConnected(this, EventType.CONNECTED);
  296. if (wear == "save")
  297. {
  298. Client.Appearance.SetPreviousAppearance();
  299. SaveDefaultAppearance();
  300. }
  301. else if (wear != "no")
  302. {
  303. MakeDefaultAppearance(wear);
  304. }
  305. // Extract nearby region information.
  306. Client.Grid.GridRegion += Manager.Grid_GridRegion;
  307. uint xUint, yUint;
  308. Utils.LongToUInts(Client.Network.CurrentSim.Handle, out xUint, out yUint);
  309. ushort minX, minY, maxX, maxY;
  310. minX = (ushort)Math.Min(0, xUint - 5);
  311. minY = (ushort)Math.Min(0, yUint - 5);
  312. maxX = (ushort)(xUint + 5);
  313. maxY = (ushort)(yUint + 5);
  314. Client.Grid.RequestMapBlocks(GridLayerType.Terrain, minX, minY, maxX, maxY, false);
  315. }
  316. else
  317. {
  318. ConnectionState = ConnectionState.Disconnected;
  319. m_log.ErrorFormat(
  320. "{0} {1} cannot login: {2}", FirstName, LastName, Client.Network.LoginMessage);
  321. if (OnDisconnected != null)
  322. {
  323. OnDisconnected(this, EventType.DISCONNECTED);
  324. }
  325. }
  326. }
  327. /// <summary>
  328. /// Sit this bot on the ground.
  329. /// </summary>
  330. public void SitOnGround()
  331. {
  332. if (ConnectionState == ConnectionState.Connected)
  333. Client.Self.SitOnGround();
  334. }
  335. /// <summary>
  336. /// Stand this bot
  337. /// </summary>
  338. public void Stand()
  339. {
  340. if (ConnectionState == ConnectionState.Connected)
  341. {
  342. // Unlike sit on ground, here libomv checks whether we have SEND_AGENT_UPDATES enabled.
  343. bool prevUpdatesSetting = Client.Settings.SEND_AGENT_UPDATES;
  344. Client.Settings.SEND_AGENT_UPDATES = true;
  345. Client.Self.Stand();
  346. Client.Settings.SEND_AGENT_UPDATES = prevUpdatesSetting;
  347. }
  348. }
  349. public void SaveDefaultAppearance()
  350. {
  351. saveDir = "MyAppearance/" + FirstName + "_" + LastName;
  352. if (!Directory.Exists(saveDir))
  353. {
  354. Directory.CreateDirectory(saveDir);
  355. }
  356. Array wtypes = Enum.GetValues(typeof(WearableType));
  357. foreach (WearableType wtype in wtypes)
  358. {
  359. UUID wearable = Client.Appearance.GetWearableAsset(wtype);
  360. if (wearable != UUID.Zero)
  361. {
  362. Client.Assets.RequestAsset(wearable, AssetType.Clothing, false, Asset_ReceivedCallback);
  363. Client.Assets.RequestAsset(wearable, AssetType.Bodypart, false, Asset_ReceivedCallback);
  364. }
  365. }
  366. }
  367. public void SaveAsset(AssetWearable asset)
  368. {
  369. if (asset != null)
  370. {
  371. try
  372. {
  373. if (asset.Decode())
  374. {
  375. File.WriteAllBytes(Path.Combine(saveDir, String.Format("{1}.{0}",
  376. asset.AssetType.ToString().ToLower(),
  377. asset.WearableType)), asset.AssetData);
  378. }
  379. else
  380. {
  381. m_log.WarnFormat("Failed to decode {0} asset {1}", asset.AssetType, asset.AssetID);
  382. }
  383. }
  384. catch (Exception e)
  385. {
  386. m_log.ErrorFormat("Exception: {0}{1}", e.Message, e.StackTrace);
  387. }
  388. }
  389. }
  390. public WearableType GetWearableType(string path)
  391. {
  392. string type = ((((path.Split('/'))[2]).Split('.'))[0]).Trim();
  393. switch (type)
  394. {
  395. case "Eyes":
  396. return WearableType.Eyes;
  397. case "Hair":
  398. return WearableType.Hair;
  399. case "Pants":
  400. return WearableType.Pants;
  401. case "Shape":
  402. return WearableType.Shape;
  403. case "Shirt":
  404. return WearableType.Shirt;
  405. case "Skin":
  406. return WearableType.Skin;
  407. default:
  408. return WearableType.Shape;
  409. }
  410. }
  411. public void MakeDefaultAppearance(string wear)
  412. {
  413. try
  414. {
  415. if (wear == "yes")
  416. {
  417. //TODO: Implement random outfit picking
  418. m_log.DebugFormat("Picks a random outfit. Not yet implemented.");
  419. }
  420. else if (wear != "save")
  421. saveDir = "MyAppearance/" + wear;
  422. saveDir = saveDir + "/";
  423. string[] clothing = Directory.GetFiles(saveDir, "*.clothing", SearchOption.TopDirectoryOnly);
  424. string[] bodyparts = Directory.GetFiles(saveDir, "*.bodypart", SearchOption.TopDirectoryOnly);
  425. InventoryFolder clothfolder = FindClothingFolder();
  426. UUID transid = UUID.Random();
  427. List<InventoryBase> listwearables = new List<InventoryBase>();
  428. for (int i = 0; i < clothing.Length; i++)
  429. {
  430. UUID assetID = UUID.Random();
  431. AssetClothing asset = new AssetClothing(assetID, File.ReadAllBytes(clothing[i]));
  432. asset.Decode();
  433. asset.Owner = Client.Self.AgentID;
  434. asset.WearableType = GetWearableType(clothing[i]);
  435. asset.Encode();
  436. transid = Client.Assets.RequestUpload(asset,true);
  437. Client.Inventory.RequestCreateItem(clothfolder.UUID, "MyClothing" + i.ToString(), "MyClothing", AssetType.Clothing,
  438. transid, InventoryType.Wearable, asset.WearableType, (OpenMetaverse.PermissionMask)PermissionMask.All, delegate(bool success, InventoryItem item)
  439. {
  440. if (success)
  441. {
  442. listwearables.Add(item);
  443. }
  444. else
  445. {
  446. m_log.WarnFormat("Failed to create item {0}", item.Name);
  447. }
  448. }
  449. );
  450. }
  451. for (int i = 0; i < bodyparts.Length; i++)
  452. {
  453. UUID assetID = UUID.Random();
  454. AssetBodypart asset = new AssetBodypart(assetID, File.ReadAllBytes(bodyparts[i]));
  455. asset.Decode();
  456. asset.Owner = Client.Self.AgentID;
  457. asset.WearableType = GetWearableType(bodyparts[i]);
  458. asset.Encode();
  459. transid = Client.Assets.RequestUpload(asset,true);
  460. Client.Inventory.RequestCreateItem(clothfolder.UUID, "MyBodyPart" + i.ToString(), "MyBodyPart", AssetType.Bodypart,
  461. transid, InventoryType.Wearable, asset.WearableType, (OpenMetaverse.PermissionMask)PermissionMask.All, delegate(bool success, InventoryItem item)
  462. {
  463. if (success)
  464. {
  465. listwearables.Add(item);
  466. }
  467. else
  468. {
  469. m_log.WarnFormat("Failed to create item {0}", item.Name);
  470. }
  471. }
  472. );
  473. }
  474. Thread.Sleep(1000);
  475. if (listwearables == null || listwearables.Count == 0)
  476. {
  477. m_log.DebugFormat("Nothing to send on this folder!");
  478. }
  479. else
  480. {
  481. m_log.DebugFormat("Sending {0} wearables...", listwearables.Count);
  482. Client.Appearance.WearOutfit(listwearables, false);
  483. }
  484. }
  485. catch (Exception ex)
  486. {
  487. Console.WriteLine(ex.ToString());
  488. }
  489. }
  490. public InventoryFolder FindClothingFolder()
  491. {
  492. UUID rootfolder = Client.Inventory.Store.RootFolder.UUID;
  493. List<InventoryBase> listfolders = Client.Inventory.Store.GetContents(rootfolder);
  494. InventoryFolder clothfolder = new InventoryFolder(UUID.Random());
  495. foreach (InventoryBase folder in listfolders)
  496. {
  497. if (folder.Name == "Clothing")
  498. {
  499. clothfolder = (InventoryFolder)folder;
  500. break;
  501. }
  502. }
  503. return clothfolder;
  504. }
  505. public void Network_LoginProgress(object sender, LoginProgressEventArgs args)
  506. {
  507. m_log.DebugFormat("[BOT]: Bot {0} {1} in Network_LoginProcess", Name, args.Status);
  508. if (args.Status == LoginStatus.Success)
  509. {
  510. if (OnConnected != null)
  511. {
  512. OnConnected(this, EventType.CONNECTED);
  513. }
  514. }
  515. }
  516. public void Network_SimConnected(object sender, SimConnectedEventArgs args)
  517. {
  518. m_log.DebugFormat(
  519. "[BOT]: Bot {0} connected to region {1} at {2}", Name, args.Simulator.Name, args.Simulator.IPEndPoint);
  520. }
  521. public void Network_SimDisconnected(object sender, SimDisconnectedEventArgs args)
  522. {
  523. m_log.DebugFormat(
  524. "[BOT]: Bot {0} disconnected from region {1} at {2}", Name, args.Simulator.Name, args.Simulator.IPEndPoint);
  525. }
  526. public void Network_OnDisconnected(object sender, DisconnectedEventArgs args)
  527. {
  528. ConnectionState = ConnectionState.Disconnected;
  529. m_log.DebugFormat(
  530. "[BOT]: Bot {0} disconnected from grid, reason {1}, message {2}", Name, args.Reason, args.Message);
  531. // m_log.ErrorFormat("Fired Network_OnDisconnected");
  532. // if (
  533. // (args.Reason == NetworkManager.DisconnectType.SimShutdown
  534. // || args.Reason == NetworkManager.DisconnectType.NetworkTimeout)
  535. // && OnDisconnected != null)
  536. if (
  537. (args.Reason == NetworkManager.DisconnectType.ClientInitiated
  538. || args.Reason == NetworkManager.DisconnectType.ServerInitiated
  539. || args.Reason == NetworkManager.DisconnectType.NetworkTimeout)
  540. && OnDisconnected != null)
  541. // if (OnDisconnected != null)
  542. {
  543. OnDisconnected(this, EventType.DISCONNECTED);
  544. }
  545. }
  546. public void Objects_NewPrim(object sender, PrimEventArgs args)
  547. {
  548. if (!RequestObjectTextures)
  549. return;
  550. Primitive prim = args.Prim;
  551. if (prim != null)
  552. {
  553. lock (m_objects)
  554. m_objects[prim.ID] = prim;
  555. if (prim.Textures != null)
  556. {
  557. if (prim.Textures.DefaultTexture.TextureID != UUID.Zero)
  558. {
  559. GetTexture(prim.Textures.DefaultTexture.TextureID);
  560. }
  561. for (int i = 0; i < prim.Textures.FaceTextures.Length; i++)
  562. {
  563. Primitive.TextureEntryFace face = prim.Textures.FaceTextures[i];
  564. if (face != null)
  565. {
  566. UUID textureID = prim.Textures.FaceTextures[i].TextureID;
  567. if (textureID != UUID.Zero)
  568. GetTexture(textureID);
  569. }
  570. }
  571. }
  572. if (prim.Sculpt != null && prim.Sculpt.SculptTexture != UUID.Zero)
  573. GetTexture(prim.Sculpt.SculptTexture);
  574. }
  575. }
  576. private void GetTexture(UUID textureID)
  577. {
  578. lock (Manager.AssetsReceived)
  579. {
  580. // Don't request assets more than once.
  581. if (Manager.AssetsReceived.ContainsKey(textureID))
  582. return;
  583. Manager.AssetsReceived[textureID] = false;
  584. Client.Assets.RequestImage(textureID, ImageType.Normal, Asset_TextureCallback_Texture);
  585. }
  586. }
  587. public void Asset_TextureCallback_Texture(TextureRequestState state, AssetTexture assetTexture)
  588. {
  589. //TODO: Implement texture saving and applying
  590. }
  591. public void Asset_ReceivedCallback(AssetDownload transfer, Asset asset)
  592. {
  593. lock (Manager.AssetsReceived)
  594. Manager.AssetsReceived[asset.AssetID] = true;
  595. // if (wear == "save")
  596. // {
  597. // SaveAsset((AssetWearable) asset);
  598. // }
  599. }
  600. }
  601. }