Exemplo n.º 1
0
        public void WillChangeLastAliasOnTargetSent()
        {
            _player.LastTargetSerial = 0x02;

            AliasCommands.SetDefaultAliases();

            Assert.AreEqual(0x02, AliasCommands.GetAlias("last"));

            OutgoingPacketHandlers.Initialize();
            PacketHandler handler = OutgoingPacketHandlers.GetHandler(0x6C);

            byte[] packet =
            {
                0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };

            handler?.OnReceive(new PacketReader(packet, packet.Length, true));

            Assert.AreEqual(0xAABBCCDD, (uint)AliasCommands.GetAlias("last"));
        }
Exemplo n.º 2
0
        public void WillTargetTileRelativeSelfRunning()
        {
            Engine.Player = new PlayerMobile(0x01)
            {
                X = 100, Y = 100, Direction = (Direction)((int)Direction.East | 0x80)
            };

            AutoResetEvent are = new AutoResetEvent(false);

            void OnInternalPacketSentEvent(byte[] data, int length)
            {
                if (data[0] != 0x6C)
                {
                    Assert.Fail();
                }

                int x = (data[11] << 8) | data[12];

                //int y = ( data[ 13 ] << 8 ) | data[ 14 ];

                if (x != 101)
                {
                    Assert.Fail();
                }

                are.Set();
            }

            Engine.InternalPacketSentEvent += OnInternalPacketSentEvent;

            AliasCommands.SetAlias("self", 0x01);

            TargetCommands.TargetTileRelative("self", 1);

            bool result = are.WaitOne(5000);

            Assert.IsTrue(result);

            Engine.InternalPacketSentEvent -= OnInternalPacketSentEvent;
            Engine.Player = null;
        }
Exemplo n.º 3
0
        public void WillOnlyGetClosestFriend()
        {
            Engine.Player = new PlayerMobile(0x01);

            Mobile mate = new Mobile(0x02)
            {
                X = 5, Y = 5, Notoriety = Notoriety.Murderer
            };
            Mobile random = new Mobile(0x03)
            {
                X = 1, Y = 1, Notoriety = Notoriety.Murderer
            };

            Engine.Mobiles.Add(mate);
            Engine.Mobiles.Add(random);

            Options.CurrentOptions.Friends.Add(new FriendEntry {
                Name = "Mate", Serial = mate.Serial
            });

            bool result = TargetCommands.GetFriend(new[] { "Murderer" }, "Any", "Closest");

            Assert.IsTrue(result);

            Assert.AreEqual(random.Serial, AliasCommands.GetAlias("friend"));

            // Will get mate even though he is further away
            result = TargetCommands.GetFriendListOnly("Closest");

            Assert.IsTrue(result);

            Assert.AreEqual(mate.Serial, AliasCommands.GetAlias("friend"));

            Options.CurrentOptions.Friends.Clear();
            Engine.Mobiles.Clear();
            Engine.Player = null;
        }
Exemplo n.º 4
0
        public void Execute(MacroEntry macro)
        {
            _macro = macro;

            if (Thread != null && Thread.IsAlive)
            {
                Stop();
            }

            _cancellationToken = new CancellationTokenSource();

            if (_importCache == null)
            {
                _importCache = InitializeImports(_engine);
            }

            ScriptSource source = _engine.CreateScriptSourceFromString(_macro.Macro, SourceCodeKind.Statements);

            Dictionary <string, object> importCache = new Dictionary <string, object>(_importCache);

            IsFaulted = false;

            Thread = new Thread(() =>
            {
                Thread = Thread.CurrentThread;

                try
                {
                    StartedEvent?.Invoke();

                    AliasCommands.SetDefaultAliases();

                    ScriptScope macroScope = _engine.CreateScope(importCache);

                    _stopWatch.Reset();
                    _stopWatch.Start();

                    do
                    {
                        _cancellationToken.Token.ThrowIfCancellationRequested();

                        source.Execute(macroScope);

                        _stopWatch.Stop();

                        bool willLoop = _macro.Loop && !IsFaulted && !_cancellationToken.IsCancellationRequested;

                        if (!willLoop)
                        {
                            break;
                        }

                        if (Options.CurrentOptions.Debug)
                        {
                            UO.Commands.SystemMessage(string.Format(Strings.Loop_time___0_, _stopWatch.Elapsed));
                        }

                        int diff = 50 - (int)_stopWatch.ElapsedMilliseconds;

                        if (diff > 0)
                        {
                            Thread.Sleep(diff);
                        }
                    }while (_macro.Loop && !IsFaulted);
                }
                catch (TaskCanceledException)
                {
                    IsFaulted = true;
                }
                catch (ThreadInterruptedException)
                {
                    IsFaulted = true;
                }
                catch (ThreadAbortException)
                {
                    IsFaulted = true;
                }
                catch (Exception e)
                {
                    IsFaulted = true;
                    Exception = e;

                    ExceptionEvent?.Invoke(e);
                }
                finally
                {
                    StoppedEvent?.Invoke();
                }
            })
            {
                IsBackground = true
            };

            try
            {
                Thread.Start();
                Thread.Join();
            }
            catch (ThreadStartException)
            {
                // TODO
            }
        }
Exemplo n.º 5
0
        public void WillKeepLastBetweenCasts()
        {
            Engine.Player            = new PlayerMobile(0x01);
            Engine.PacketWaitEntries = new PacketWaitEntries();

            AutoResetEvent are = new AutoResetEvent(false);

            void OnWaitEntryAddedEvent(PacketWaitEntry entry)
            {
                PacketWriter target = new PacketWriter(0x19);

                target.Write((byte)0x6C);
                target.Fill();

                Engine.SendPacketToClient(target);
                Engine.PacketWaitEntries.CheckWait(target.ToArray(), PacketDirection.Incoming);
            }

            Engine.PacketWaitEntries.WaitEntryAddedEvent += OnWaitEntryAddedEvent;

            void OnInternalPacketSentEvent(byte[] data, int length)
            {
                if (data[0] != 0x6C)
                {
                    return;
                }

                int serial = (data[7] << 24) | (data[8] << 16) | (data[9] << 8) | data[10];

                if (serial != 0x00aabbcc)
                {
                    Assert.Fail();
                }

                are.Set();
            }

            Engine.InternalPacketSentEvent += OnInternalPacketSentEvent;

            Task.Run(() => SpellCommands.Cast("Explosion", 0x00aabbcc));

            bool result = are.WaitOne(60000);

            Assert.AreEqual(0x00aabbcc, AliasCommands.GetAlias("last"));

            Assert.IsTrue(result);

            Task.Run(() => SpellCommands.Cast("Explosion", "last"));

            result = are.WaitOne(60000);

            Assert.AreEqual(0x00aabbcc, AliasCommands.GetAlias("last"));

            Assert.IsTrue(result);

            Assert.AreEqual(0x00aabbcc, Engine.Player.LastTargetSerial);

            Engine.PacketWaitEntries.WaitEntryAddedEvent -= OnWaitEntryAddedEvent;
            Engine.InternalPacketSentEvent -= OnInternalPacketSentEvent;
            Engine.Player = null;
        }
Exemplo n.º 6
0
        public void WillSetDefaultAliases()
        {
            AliasCommands.SetDefaultAliases();

            Assert.AreEqual(_player.Serial, AliasCommands.GetAlias("self"));
        }