29 using System.Collections.Generic;
32 using System.Reflection;
33 using System.Threading;
37 using OpenMetaverse.Assets;
38 using OpenMetaverse.Packets;
40 using OpenSim.Framework;
41 using OpenSim.Framework.Console;
42 using pCampBot.Interfaces;
58 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
62 get {
return m_packetDebugLevel; }
65 if (value == m_packetDebugLevel)
68 m_packetDebugLevel = value;
72 if (m_packetDebugLevel <= 0)
73 Client.Network.UnregisterCallback(PacketType.Default, PacketReceivedDebugHandler);
75 Client.Network.RegisterCallback(PacketType.Default, PacketReceivedDebugHandler,
false);
79 private int m_packetDebugLevel;
100 public Dictionary<string, IBehaviour>
Behaviours {
get;
private set; }
108 public Dictionary<UUID, Primitive>
Objects
113 return new Dictionary<UUID, Primitive>(m_objects);
116 private Dictionary<UUID, Primitive> m_objects =
new Dictionary<UUID, Primitive>();
127 lock (
Client.Network.Simulators)
128 return new List<Simulator>(Client.Network.Simulators);
140 lock (
Client.Network.Simulators)
141 return Client.Network.Simulators.Count;
147 public string Name {
get;
private set; }
161 private Thread m_actionThread;
173 public GridClient
Client {
get;
private set; }
187 string firstName,
string lastName,
string password,
string startLocation,
string loginUri)
201 Behaviours =
new Dictionary<string, IBehaviour>();
206 CreateLibOmvClient();
212 return Behaviours.TryGetValue(abbreviatedName, out behaviour);
217 Dictionary<string, IBehaviour> updatedBehaviours =
new Dictionary<string, IBehaviour>(
Behaviours);
221 behaviour.Initialize(
this);
222 updatedBehaviours.Add(behaviour.AbbreviatedName, behaviour);
236 Dictionary<string, IBehaviour> updatedBehaviours =
new Dictionary<string, IBehaviour>(
Behaviours);
239 if (!updatedBehaviours.TryGetValue(abbreviatedName, out behaviour))
242 updatedBehaviours.Remove(abbreviatedName);
250 private void CreateLibOmvClient()
252 GridClient newClient =
new GridClient();
257 Client.Network.UnregisterCallback(PacketType.Default, PacketReceivedDebugHandler);
259 newClient.Settings.LOGIN_SERVER = Client.Settings.LOGIN_SERVER;
260 newClient.Settings.ALWAYS_DECODE_OBJECTS = Client.Settings.ALWAYS_DECODE_OBJECTS;
261 newClient.Settings.AVATAR_TRACKING = Client.Settings.AVATAR_TRACKING;
262 newClient.Settings.OBJECT_TRACKING = Client.Settings.OBJECT_TRACKING;
263 newClient.Settings.SEND_AGENT_THROTTLE = Client.Settings.SEND_AGENT_THROTTLE;
264 newClient.Settings.SEND_AGENT_UPDATES = Client.Settings.SEND_AGENT_UPDATES;
265 newClient.Settings.SEND_PINGS = Client.Settings.SEND_PINGS;
266 newClient.Settings.STORE_LAND_PATCHES = Client.Settings.STORE_LAND_PATCHES;
267 newClient.Settings.USE_ASSET_CACHE = Client.Settings.USE_ASSET_CACHE;
268 newClient.Settings.MULTIPLE_SIMS = Client.Settings.MULTIPLE_SIMS;
269 newClient.Throttle.Asset = Client.Throttle.Asset;
270 newClient.Throttle.Land = Client.Throttle.Land;
271 newClient.Throttle.Task = Client.Throttle.Task;
272 newClient.Throttle.Texture = Client.Throttle.Texture;
273 newClient.Throttle.Wind = Client.Throttle.Wind;
274 newClient.Throttle.Total = Client.Throttle.Total;
278 newClient.Settings.LOGIN_SERVER =
LoginUri;
279 newClient.Settings.ALWAYS_DECODE_OBJECTS =
false;
280 newClient.Settings.AVATAR_TRACKING =
false;
281 newClient.Settings.OBJECT_TRACKING =
false;
282 newClient.Settings.SEND_AGENT_THROTTLE =
true;
283 newClient.Settings.SEND_PINGS =
true;
284 newClient.Settings.STORE_LAND_PATCHES =
false;
285 newClient.Settings.USE_ASSET_CACHE =
false;
286 newClient.Settings.MULTIPLE_SIMS =
true;
287 newClient.Throttle.Asset = 100000;
288 newClient.Throttle.Land = 100000;
289 newClient.Throttle.Task = 100000;
290 newClient.Throttle.Texture = 100000;
291 newClient.Throttle.Wind = 100000;
292 newClient.Throttle.Total = 400000;
301 if (m_packetDebugLevel > 0)
302 newClient.Network.RegisterCallback(PacketType.Default, PacketReceivedDebugHandler);
309 private void Action()
336 Client.Network.Logout();
342 connectThread.Name =
Name;
343 connectThread.IsBackground =
true;
345 connectThread.Start();
351 private void ConnectInternal()
358 CreateLibOmvClient();
364 Thread.Sleep(Random.Next(1000, 10000));
365 m_actionThread =
new Thread(Action);
366 m_actionThread.Start();
373 else if (
wear !=
"no")
379 Client.Grid.GridRegion += Manager.Grid_GridRegion;
381 Utils.LongToUInts(Client.Network.CurrentSim.Handle, out xUint, out yUint);
382 ushort minX, minY, maxX, maxY;
383 minX = (ushort)Math.Min(0, xUint - 5);
384 minY = (ushort)Math.Min(0, yUint - 5);
385 maxX = (ushort)(xUint + 5);
386 maxY = (ushort)(yUint + 5);
387 Client.Grid.RequestMapBlocks(GridLayerType.Terrain, minX, minY, maxX, maxY,
false);
409 Client.Self.SitOnGround();
420 bool prevUpdatesSetting = Client.Settings.SEND_AGENT_UPDATES;
421 Client.Settings.SEND_AGENT_UPDATES =
true;
423 Client.Settings.SEND_AGENT_UPDATES = prevUpdatesSetting;
430 if (!Directory.Exists(
saveDir))
432 Directory.CreateDirectory(
saveDir);
435 Array wtypes = Enum.GetValues(typeof(WearableType));
436 foreach (WearableType wtype
in wtypes)
438 UUID wearable = Client.Appearance.GetWearableAsset(wtype);
439 if (wearable != UUID.Zero)
455 File.WriteAllBytes(Path.Combine(
saveDir, String.Format(
"{1}.{0}",
456 asset.AssetType.ToString().ToLower(),
457 asset.WearableType)), asset.AssetData);
461 m_log.WarnFormat(
"Failed to decode {0} asset {1}", asset.AssetType, asset.AssetID);
466 m_log.ErrorFormat(
"Exception: {0}{1}", e.Message, e.StackTrace);
473 string type = ((((path.Split(
'/'))[2]).Split(
'.'))[0]).Trim();
477 return WearableType.Eyes;
479 return WearableType.Hair;
481 return WearableType.Pants;
483 return WearableType.Shape;
485 return WearableType.Shirt;
487 return WearableType.Skin;
489 return WearableType.Shape;
500 m_log.DebugFormat(
"Picks a random outfit. Not yet implemented.");
502 else if (wear !=
"save")
506 string[] clothing = Directory.GetFiles(
saveDir,
"*.clothing", SearchOption.TopDirectoryOnly);
507 string[] bodyparts = Directory.GetFiles(
saveDir,
"*.bodypart", SearchOption.TopDirectoryOnly);
509 UUID transid = UUID.Random();
510 List<InventoryBase> listwearables =
new List<InventoryBase>();
512 for (
int i = 0; i < clothing.Length; i++)
514 UUID assetID = UUID.Random();
515 AssetClothing asset =
new AssetClothing(assetID,
File.ReadAllBytes(clothing[i]));
517 asset.Owner = Client.Self.AgentID;
520 transid = Client.Assets.RequestUpload(asset,
true);
521 Client.Inventory.RequestCreateItem(clothfolder.UUID,
"MyClothing" + i.ToString(),
"MyClothing", AssetType.Clothing,
522 transid, InventoryType.Wearable, asset.WearableType, (OpenMetaverse.PermissionMask)PermissionMask.All, delegate(
bool success, InventoryItem item)
526 listwearables.Add(item);
530 m_log.WarnFormat(
"Failed to create item {0}", item.Name);
536 for (
int i = 0; i < bodyparts.Length; i++)
538 UUID assetID = UUID.Random();
539 AssetBodypart asset =
new AssetBodypart(assetID,
File.ReadAllBytes(bodyparts[i]));
541 asset.Owner = Client.Self.AgentID;
544 transid = Client.Assets.RequestUpload(asset,
true);
545 Client.Inventory.RequestCreateItem(clothfolder.UUID,
"MyBodyPart" + i.ToString(),
"MyBodyPart", AssetType.Bodypart,
546 transid, InventoryType.Wearable, asset.WearableType, (OpenMetaverse.PermissionMask)PermissionMask.All, delegate(
bool success, InventoryItem item)
550 listwearables.Add(item);
554 m_log.WarnFormat(
"Failed to create item {0}", item.Name);
562 if (listwearables == null || listwearables.Count == 0)
564 m_log.DebugFormat(
"Nothing to send on this folder!");
568 m_log.DebugFormat(
"Sending {0} wearables...", listwearables.Count);
569 Client.Appearance.WearOutfit(listwearables,
false);
574 Console.WriteLine(ex.ToString());
580 UUID rootfolder = Client.Inventory.Store.RootFolder.UUID;
581 List<InventoryBase> listfolders = Client.Inventory.Store.GetContents(rootfolder);
582 InventoryFolder clothfolder =
new InventoryFolder(UUID.Random());
583 foreach (InventoryBase folder
in listfolders)
585 if (folder.Name ==
"Clothing")
587 clothfolder = (InventoryFolder)folder;
596 m_log.DebugFormat(
"[BOT]: Bot {0} {1} in Network_LoginProcess",
Name, args.Status);
598 if (args.Status == LoginStatus.Success)
610 "[BOT]: Bot {0} connected to region {1} at {2}",
Name, args.Simulator.Name, args.Simulator.IPEndPoint);
616 "[BOT]: Bot {0} disconnected from region {1} at {2}",
Name, args.Simulator.Name, args.Simulator.IPEndPoint);
624 "[BOT]: Bot {0} disconnected from grid, reason {1}, message {2}",
Name, args.Reason, args.Message);
636 (args.Reason == NetworkManager.DisconnectType.ClientInitiated
637 || args.Reason == NetworkManager.DisconnectType.ServerInitiated
638 || args.Reason == NetworkManager.DisconnectType.NetworkTimeout)
656 m_objects[prim.ID] = prim;
658 if (prim.Textures != null)
660 if (prim.Textures.DefaultTexture.TextureID != UUID.Zero)
662 GetTextureOrMesh(prim.Textures.DefaultTexture.TextureID,
true);
665 for (
int i = 0; i < prim.Textures.FaceTextures.Length; i++)
667 Primitive.TextureEntryFace face = prim.Textures.FaceTextures[i];
671 UUID textureID = prim.Textures.FaceTextures[i].TextureID;
673 if (textureID != UUID.Zero)
674 GetTextureOrMesh(textureID,
true);
679 if (prim.Sculpt != null && prim.Sculpt.SculptTexture != UUID.Zero)
681 bool mesh = (prim.Sculpt.Type == SculptType.Mesh);
682 GetTextureOrMesh(prim.Sculpt.SculptTexture, !mesh);
687 private void GetTextureOrMesh(UUID assetID,
bool texture)
695 Manager.AssetsReceived[assetID] =
false;
703 Client.Assets.RequestMesh(assetID, Asset_MeshCallback);
707 m_log.Warn(string.Format(
"Error requesting {0} {1}", texture ?
"texture" :
"mesh", assetID), e);
713 if (state == TextureRequestState.Finished)
716 Manager.AssetsReceived[assetTexture.AssetID] =
true;
720 private void Asset_MeshCallback(
bool success, AssetMesh assetMesh)
723 Manager.AssetsReceived[assetMesh.AssetID] = success;
729 Manager.AssetsReceived[asset.AssetID] =
true;
737 private void PacketReceivedDebugHandler(
object o, PacketReceivedEventArgs args)
739 Packet p = args.Packet;
741 Simulator s = args.Simulator;
744 "[BOT]: Bot {0} received from {1} packet {2} #{3}, rel {4}, res {5}",
745 Name, s.Name, p.Type, h.Sequence, h.Reliable, h.Resent);
Random Random
Random number generator.
void Network_LoginProgress(object sender, LoginProgressEventArgs args)
Dictionary< string, IBehaviour > Behaviours
Behaviours implemented by this bot.
void Asset_ReceivedCallback(AssetDownload transfer, Asset asset)
Bot(BotManager bm, List< IBehaviour > behaviours, string firstName, string lastName, string password, string startLocation, string loginUri)
Constructor
EventType
Event Types from the BOT. Add new events here
List< Simulator > Simulators
delegate void AnEvent(Bot callbot, EventType someevent)
void Asset_TextureCallback_Texture(TextureRequestState state, AssetTexture assetTexture)
void Network_OnDisconnected(object sender, DisconnectedEventArgs args)
void Disconnect()
Tells LibSecondLife to logout and disconnect. Raises the disconnect events once it finishes...
System.Timers.Timer Timer
void Objects_NewPrim(object sender, PrimEventArgs args)
OpenSim.Framework.PermissionMask PermissionMask
Thread/Bot manager for the application
string AbbreviatedName
Abbreviated name of this behaviour.
WearableType GetWearableType(string path)
Random Rng
Random number generator.
ConnectionState ConnectionState
Is this bot connected to the grid?
bool TryGetBehaviour(string abbreviatedName, out IBehaviour behaviour)
BotManager Manager
Bot manager.
void Network_SimConnected(object sender, SimConnectedEventArgs args)
void SaveAsset(AssetWearable asset)
void Stand()
Stand this bot
InventoryFolder FindClothingFolder()
void SitOnGround()
Sit this bot on the ground.
Dictionary< UUID, Primitive > Objects
Objects that the bot has discovered.
System.Timers.Timer Timer
void MakeDefaultAppearance(string wear)
void Network_SimDisconnected(object sender, SimDisconnectedEventArgs args)
int SimulatorsCount
The number of connections that this bot has to different simulators.
bool RemoveBehaviour(string abbreviatedName)
void SaveDefaultAppearance()
bool RequestObjectTextures
Controls whether bots request textures for the object information they receive
Dictionary< UUID, bool > AssetsReceived
Track the assets we have and have not received so we don't endlessly repeat requests.
GridClient Client
New instance of a SecondLife client
bool AddBehaviour(IBehaviour behaviour)