aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/mass test client/TestClient.cs
blob: cee218c50d7bb4c98adbcf34700ad61f7e02f519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using libsecondlife;
using libsecondlife.Packets;
using libsecondlife.AssetSystem;

namespace libsecondlife.TestClient
{
    public class TestClient : SecondLife
    {
        public delegate void PrimCreatedCallback(Simulator simulator, Primitive prim);

        public event PrimCreatedCallback OnPrimCreated;

        public Dictionary<Simulator, Dictionary<uint, Primitive>> SimPrims;
        public LLUUID GroupID = LLUUID.Zero;
        public Dictionary<LLUUID, GroupMember> GroupMembers;
        public Dictionary<uint, Avatar> AvatarList = new Dictionary<uint,Avatar>();
		public Dictionary<LLUUID, AvatarAppearancePacket> Appearances = new Dictionary<LLUUID, AvatarAppearancePacket>();
		public Dictionary<string, Command> Commands = new Dictionary<string,Command>();
		public bool Running = true;
        public string MasterName = String.Empty;
        public LLUUID MasterKey = LLUUID.Zero;
		public ClientManager ClientManager;
        public int regionX;
        public int regionY;

        //internal libsecondlife.InventorySystem.InventoryFolder currentDirectory;

        private System.Timers.Timer updateTimer;
        

        /// <summary>
        /// 
        /// </summary>
        public TestClient(ClientManager manager)
        {
			ClientManager = manager;

            updateTimer = new System.Timers.Timer(1000);
            updateTimer.Elapsed += new System.Timers.ElapsedEventHandler(updateTimer_Elapsed);

            RegisterAllCommands(Assembly.GetExecutingAssembly());

            Settings.DEBUG = true;
            Settings.STORE_LAND_PATCHES = true;
            Settings.ALWAYS_REQUEST_OBJECTS = true;

            Network.RegisterCallback(PacketType.AgentDataUpdate, new NetworkManager.PacketCallback(AgentDataUpdateHandler));

            Objects.OnNewPrim += new ObjectManager.NewPrimCallback(Objects_OnNewPrim);
            Objects.OnObjectUpdated += new ObjectManager.ObjectUpdatedCallback(Objects_OnObjectUpdated);
            Objects.OnObjectKilled += new ObjectManager.KillObjectCallback(Objects_OnObjectKilled);
			Objects.OnNewAvatar += new ObjectManager.NewAvatarCallback(Objects_OnNewAvatar);
            Self.OnInstantMessage += new MainAvatar.InstantMessageCallback(Self_OnInstantMessage);
            Groups.OnGroupMembers += new GroupManager.GroupMembersCallback(GroupMembersHandler);
            this.OnLogMessage += new LogCallback(TestClient_OnLogMessage);

            Network.RegisterCallback(PacketType.AvatarAppearance, new NetworkManager.PacketCallback(AvatarAppearanceHandler));

            updateTimer.Start();
        }

        public void RegisterAllCommands(Assembly assembly)
        {
            foreach (Type t in assembly.GetTypes())
            {
                try
                {
                    if (t.IsSubclassOf(typeof(Command)))
                    {
                        ConstructorInfo info = t.GetConstructor(new Type[] { typeof(TestClient) });
                        Command command = (Command)info.Invoke(new object[] { this });
                        RegisterCommand(command);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }

        public void RegisterCommand(Command command)
        {
			command.Client = this;
			if (!Commands.ContainsKey(command.Name.ToLower()))
			{
				Commands.Add(command.Name.ToLower(), command);
			}
        }

        //breaks up large responses to deal with the max IM size
        private void SendResponseIM(SecondLife client, LLUUID fromAgentID, string data, LLUUID imSessionID)
        {
            for ( int i = 0 ; i < data.Length ; i += 1024 ) {
                int y;
                if ((i + 1023) > data.Length)
                {
                    y = data.Length - i;
                }
                else
                {
                    y = 1023;
                }
                string message = data.Substring(i, y);
                client.Self.InstantMessage(fromAgentID, message, imSessionID);
            }
        }

		public void DoCommand(string cmd, LLUUID fromAgentID, LLUUID imSessionID)
        {
			string[] tokens = Parsing.ParseArguments(cmd);

            if (tokens.Length == 0)
                return;
				
			string firstToken = tokens[0].ToLower();

            // "all balance" will send the balance command to all currently logged in bots
			if (firstToken == "all" && tokens.Length > 1)
			{
			    cmd = String.Empty;

			    // Reserialize all of the arguments except for "all"
			    for (int i = 1; i < tokens.Length; i++)
			    {
			        cmd += tokens[i] + " ";
			    }

			    ClientManager.DoCommandAll(cmd, fromAgentID, imSessionID);

			    return;
			}

            if (Commands.ContainsKey(firstToken))
            {
                string[] args = new string[tokens.Length - 1];
                Array.Copy(tokens, 1, args, 0, args.Length);
                string response = Commands[firstToken].Execute(args, fromAgentID);

                if (response.Length > 0)
                {
                    Console.WriteLine(response);

                    if (fromAgentID != null && Network.Connected)
                    {
                        // IMs don't like \r\n line endings, clean them up first
                        response = response.Replace("\r", "");
                        SendResponseIM(this, fromAgentID, response, imSessionID);
                    }
                }
            }
        }

        private void updateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (Command c in Commands.Values)
                if (c.Active)
                    c.Think();
        }

        private void AgentDataUpdateHandler(Packet packet, Simulator sim)
        {
            AgentDataUpdatePacket p = (AgentDataUpdatePacket)packet;
            if (p.AgentData.AgentID == sim.Client.Network.AgentID)
            {
                Console.WriteLine("Got the group ID for " + sim.Client.ToString() + ", requesting group members...");
                GroupID = p.AgentData.ActiveGroupID;

                sim.Client.Groups.BeginGetGroupMembers(GroupID);
            }
        }

        private void TestClient_OnLogMessage(string message, Helpers.LogLevel level)
        {
            Console.WriteLine("<" + this.ToString() + "> " + level.ToString() + ": " + message);
        }

        private void GroupMembersHandler(Dictionary<LLUUID, GroupMember> members)
        {
            Console.WriteLine("Got " + members.Count + " group members.");
            GroupMembers = members;
        }

        private void Objects_OnObjectKilled(Simulator simulator, uint objectID)
        {
            lock (SimPrims)
            {
                if (SimPrims.ContainsKey(simulator) && SimPrims[simulator].ContainsKey(objectID))
                    SimPrims[simulator].Remove(objectID);
            }

			lock (AvatarList)
			{
			    if (AvatarList.ContainsKey(objectID))
			        AvatarList.Remove(objectID);
			}
        }

        private void Objects_OnObjectUpdated(Simulator simulator, ObjectUpdate update, ulong regionHandle, ushort timeDilation)
        {
            regionX = (int)(regionHandle >> 32);
            regionY = (int)(regionHandle & 0xFFFFFFFF);

            if (update.Avatar)
            {
                lock (AvatarList)
                {
                    // TODO: We really need a solid avatar and object tracker in Utilities to use here
                    if (AvatarList.ContainsKey(update.LocalID))
                    {
                        AvatarList[update.LocalID].CollisionPlane = update.CollisionPlane;
                        AvatarList[update.LocalID].Position = update.Position;
                        AvatarList[update.LocalID].Velocity = update.Velocity;
                        AvatarList[update.LocalID].Acceleration = update.Acceleration;
                        AvatarList[update.LocalID].Rotation = update.Rotation;
                        AvatarList[update.LocalID].AngularVelocity = update.AngularVelocity;
                        AvatarList[update.LocalID].Textures = update.Textures;
                    }
                }
            }
            else
            {
                lock (SimPrims)
                {
                    if (SimPrims.ContainsKey(simulator) && SimPrims[simulator].ContainsKey(update.LocalID))
                    {
                        SimPrims[simulator][update.LocalID].Position = update.Position;
                        SimPrims[simulator][update.LocalID].Velocity = update.Velocity;
                        SimPrims[simulator][update.LocalID].Acceleration = update.Acceleration;
                        SimPrims[simulator][update.LocalID].Rotation = update.Rotation;
                        SimPrims[simulator][update.LocalID].AngularVelocity = update.AngularVelocity;
                        SimPrims[simulator][update.LocalID].Textures = update.Textures;
                    }
                }
            }
        }

        private void Objects_OnNewPrim(Simulator simulator, Primitive prim, ulong regionHandle, ushort timeDilation)
        {
            lock (SimPrims)
            {
                if (!SimPrims.ContainsKey(simulator))
                {
                    SimPrims[simulator] = new Dictionary<uint, Primitive>(10000);
                }

                SimPrims[simulator][prim.LocalID] = prim;
            }

            if ((prim.Flags & LLObject.ObjectFlags.CreateSelected) != 0 && OnPrimCreated != null)
            {
                OnPrimCreated(simulator, prim);
            }
        }

		private void Objects_OnNewAvatar(Simulator simulator, Avatar avatar, ulong regionHandle, ushort timeDilation)
		{
		    lock (AvatarList)
		    {
		        AvatarList[avatar.LocalID] = avatar;
		    }
		}

        private void AvatarAppearanceHandler(Packet packet, Simulator simulator)
        {
            AvatarAppearancePacket appearance = (AvatarAppearancePacket)packet;

            lock (Appearances) Appearances[appearance.Sender.ID] = appearance;
        }

        private void Self_OnInstantMessage(LLUUID fromAgentID, string fromAgentName, LLUUID toAgentID, 
            uint parentEstateID, LLUUID regionID, LLVector3 position, MainAvatar.InstantMessageDialog dialog, 
            bool groupIM, LLUUID imSessionID, DateTime timestamp, string message, 
            MainAvatar.InstantMessageOnline offline, byte[] binaryBucket)
        {
            if (MasterKey != LLUUID.Zero)
            {
                if (fromAgentID != MasterKey)
                {
                    // Received an IM from someone that is not the bot's master, ignore
                    Console.WriteLine("<IM>" + fromAgentName + " (not master): " + message + "@"  + regionID.ToString() + ":" + position.ToString() );
                    return;
                }
            }
            else
            {
                if (GroupMembers != null && !GroupMembers.ContainsKey(fromAgentID))
                {
                    // Received an IM from someone outside the bot's group, ignore
                    Console.WriteLine("<IM>" + fromAgentName + " (not in group): " + message + "@" + regionID.ToString() + ":" + position.ToString());
                    return;
                }
            }

            Console.WriteLine("<IM>" + fromAgentName + ": " + message);

            if (dialog == MainAvatar.InstantMessageDialog.RequestTeleport)
            {
                Console.WriteLine("Accepting teleport lure.");
                Self.TeleportLureRespond(fromAgentID, true);
            }
            else
            {
                if (dialog == MainAvatar.InstantMessageDialog.InventoryOffered)
                {
                    Console.WriteLine("Accepting inventory offer.");

                    Self.InstantMessage(Self.FirstName + " " + Self.LastName, fromAgentID, String.Empty,
                        imSessionID, MainAvatar.InstantMessageDialog.InventoryAccepted,
                        MainAvatar.InstantMessageOnline.Offline, Self.Position, LLUUID.Zero,
                        Self.InventoryRootFolderUUID.GetBytes());
                }
                else
                {
                    DoCommand(message, fromAgentID, imSessionID);
                }
            }
        }
	}
}