Example #1
0
        private void OnSuccess()
        {
            _networkedPhysics = NetworkedPhyiscs.Create();
            _networkedPhysics.gameObject.AddComponent <ClientNetworkingHandler>();
            if (NetworkUtils.IsServer)
            {
                _networkedPhysics.gameObject.AddComponent <ServerNetworkingHandler>();
                _networkedPhysics.gameObject.AddComponent <NetworkedBotController>();
            }

            NetworkClient.DisconnectHandler = () => { };
            if (NetworkUtils.IsServer)
            {
                NetworkServer.ConnectHandler    = ServerOnClientConnected;
                NetworkServer.DisconnectHandler = ServerOnClientDisconnected;
            }
            else
            {
                NetworkClient.SetTcpHandler(TcpPacketType.Server_State_Joined, buffer => {
                    while (buffer.TotalBitsLeft >= 8)
                    {
                        CreateStructure(buffer.ReadByte());
                    }
                });
                NetworkClient.SetTcpHandler(TcpPacketType.Server_State_Left,
                                            buffer => Destroy(BotCache.Get(buffer.ReadByte()).gameObject));
            }

            CompleteStructure structure = CreateStructure(NetworkUtils.LocalId);

            InitializeLocalStructure(structure);
        }
Example #2
0
        private void ServerFixedUpdate()
        {
            BotCache.ForEachId(id => {
                byte[] packet = BotCache.TakeExtra <byte[]>(id, BotCache.Extra.NetworkedPhysics);
                if (packet == null)
                {
                    return;
                }

                _sharedBuffer.SetContents(packet);
                if (_sharedBuffer.TotalBitsLeft >= BotState.InputSerializedBitsSize)
                {
                    BotState.DeserializePlayerInput(_sharedBuffer, out Vector3 movementInput, out Vector3 trackedPosition);
                    BotCache.Get(id).UpdateInputOnly(movementInput, trackedPosition);
                }
            });
            Simulate(TimestepMillis);

            if (NetworkServer.ClientCount > 0)
            {
                int bitSize = 48 + BotState.SerializedBitsSize * BotCache.Count;
                _sharedBuffer.ClearContents(new byte[(bitSize + 7) / 8]);
                _sharedBuffer.WriteTimestamp(DoubleProtocol.TimeMillis);
                BotCache.ForEach(structure => structure.SerializeState(_sharedBuffer));
                NetworkServer.UdpPayload = _sharedBuffer.Array;
            }
            else
            {
                NetworkServer.UdpPayload = null;
            }
        }
Example #3
0
        private string ReadUnitName(IntPtr activeObject, ulong guid)
        {
            if (BotCache.TryGetName(guid, out string cachedName))
            {
                return(cachedName);
            }

            try
            {
                XMemory.Read(IntPtr.Add(activeObject, 0x964), out uint objName);
                XMemory.Read(IntPtr.Add(new IntPtr(objName), 0x05C), out objName);

                XMemory.ReadString(new IntPtr(objName), Encoding.UTF8, out string name, 32);

                if (name.Length > 0)
                {
                    BotCache.CacheName(guid, name);
                }

                return(name);
            }
            catch
            {
                return("unknown");
            }
        }
Example #4
0
        public WowUnitReaction GetUnitReaction(WowUnit wowUnitA, WowUnit wowUnitB)
        {
            WowUnitReaction reaction = WowUnitReaction.Unknown;

            if (wowUnitA == null || wowUnitB == null)
            {
                return(reaction);
            }

            if (BotCache.TryGetReaction(wowUnitA.FactionTemplate, wowUnitB.FactionTemplate, out WowUnitReaction cachedReaction))
            {
                return(cachedReaction);
            }

            // integer to save the reaction
            XMemory.AllocateMemory(4, out IntPtr memAlloc);
            XMemory.Write(memAlloc, 0);

            string[] asm = new string[]
            {
                $"PUSH {wowUnitA.BaseAddress}",
                $"MOV ECX, {wowUnitB.BaseAddress}",
                $"CALL {OffsetList.FunctionGetUnitReaction}",
                $"MOV [{memAlloc}], EAX",
                "RETN",
            };

            // we need this, to be very accurate, otherwise wow will crash
            if (XMemory.ReadStruct(IntPtr.Add(wowUnitA.DescriptorAddress, OffsetList.DescriptorUnitFlags.ToInt32()), out BitVector32 unitFlagsA) &&
                XMemory.ReadStruct(IntPtr.Add(wowUnitB.DescriptorAddress, OffsetList.DescriptorUnitFlags.ToInt32()), out BitVector32 unitFlagsB))
            {
                wowUnitA.UnitFlags = unitFlagsA;
                wowUnitB.UnitFlags = unitFlagsB;
            }
            else
            {
                return(reaction);
            }

            if (wowUnitA.IsDead || wowUnitB.IsDead)
            {
                return(reaction);
            }

            try
            {
                InjectAndExecute(asm, true);
                XMemory.Read(memAlloc, out reaction);

                BotCache.CacheReaction(wowUnitA.FactionTemplate, wowUnitB.FactionTemplate, reaction);
            }
            finally
            {
                XMemory.FreeMemory(memAlloc);
            }

            return(reaction);
        }
        private void Start()
        {
            NetworkClient.SetTcpHandler(TcpPacketType.Server_Structure_Damage,
                                        buffer => BotCache.Get(buffer.ReadByte()).DamagedClient(buffer));

            NetworkClient.SetTcpHandler(TcpPacketType.Server_System_Execute,
                                        buffer => ((WeaponSystem)BotCache.Get(buffer.ReadByte()).TryGetSystem(BlockPosition.Deserialize(buffer)))
                                        .ClientExecuteWeaponFiring(buffer));
        }
Example #6
0
        /// <summary>
        /// Creates a new GameObject containing this component and also initializes, returns itself.
        /// </summary>
        public static NetworkedPhyiscs Create()
        {
            NetworkedPhyiscs instance = new GameObject("NetworkedPhysics").AddComponent <NetworkedPhyiscs>();

            if (NetworkUtils.IsServer)
            {
                NetworkClient.UdpHandler = buffer => { };
                NetworkServer.UdpHandler = (sender, buffer) => BotCache.SetExtra(sender.Id,
                                                                                 BotCache.Extra.NetworkedPhysics, buffer.Array);
            }
            else
            {
                NetworkClient.UdpHandler = buffer => instance._lastClientUdpPacket = buffer.Array;
            }
            return(instance);
        }
Example #7
0
        private string ReadPlayerName(ulong guid)
        {
            if (BotCache.TryGetName(guid, out string cachedName))
            {
                return(cachedName);
            }

            uint shortGuid;
            uint offset;

            XMemory.Read(IntPtr.Add(OffsetList.NameStore, OffsetList.NameMask.ToInt32()), out uint playerMask);
            XMemory.Read(IntPtr.Add(OffsetList.NameStore, OffsetList.NameBase.ToInt32()), out uint playerBase);

            shortGuid = (uint)guid & 0xfffffff;
            offset    = 12 * (playerMask & shortGuid);

            XMemory.Read(new IntPtr(playerBase + offset + 8), out uint current);
            XMemory.Read(new IntPtr(playerBase + offset), out offset);

            if ((current & 0x1) == 0x1)
            {
                return(string.Empty);
            }

            XMemory.Read(new IntPtr(current), out uint testGuid);

            while (testGuid != shortGuid)
            {
                XMemory.Read(new IntPtr(current + offset + 4), out current);

                if ((current & 0x1) == 0x1)
                {
                    return(string.Empty);
                }

                XMemory.Read(new IntPtr(current), out testGuid);
            }

            XMemory.ReadString(IntPtr.Add(new IntPtr(current), OffsetList.NameString.ToInt32()), Encoding.UTF8, out string name, 16);

            if (name.Length > 0)
            {
                BotCache.CacheName(guid, name);
            }

            return(name);
        }
Example #8
0
        private void ClientOnlyPacketReceived(CompleteStructure localStructure, out int toSimulate, out long lastMillis)
        {
            long currentMillis = DoubleProtocol.TimeMillis;

            lastMillis = _sharedBuffer.ReadTimestamp();
            toSimulate = (int)(currentMillis - lastMillis);
            while (_sharedBuffer.TotalBitsLeft >= BotState.SerializedBitsSize)
            {
                _tempBotState.Update(_sharedBuffer);
                BotCache.Get(_tempBotState.Id).UpdateWholeState(_tempBotState);
            }

            if (toSimulate <= 0)
            {
                toSimulate = 0;
                _silentSkipFastForwardUntil = 0;
            }
            else if (toSimulate >= 500)
            {
                if (currentMillis < _silentSkipFastForwardUntil)
                {
                    Debug.LogWarning($"Skipping {toSimulate}ms of networking fast-forward simulation to avoid delays." +
                                     "Hiding this error for at most 100ms.");
                    _silentSkipFastForwardUntil = currentMillis + 100;
                }
                toSimulate = 0;
            }
            else
            {
                _silentSkipFastForwardUntil = 0;
            }

            while (_guessedInputs.Count > 0)
            {
                KeyValuePair <long, GuessedInput> guessed = _guessedInputs.First();
                if (guessed.Value.MovementInput.Equals(localStructure.MovementInput))
                {
                    _guessedInputs.Remove(guessed.Key);
                }
                else
                {
                    break;
                }
            }
        }
        public async Task FillBotAsync()
        {
            BotCache        botCache = null;
            List <BotCache> inCache  = null;

            var apartments = await _apartmentService.GetAll();

            //var infos = await _infoService.GetAll();
            //var events = await _eventService.GetAll();

            foreach (var apartment in apartments)
            {
                var split = apartment.Utilities.Split('&');

                foreach (var utility in split)
                {
                    var utilitySplit = utility.Split('=');
                    botCache = new BotCache(utilitySplit[0], utilitySplit[1],
                                            apartment.ApartmentId);

                    inCache = await(_botCacheRepository.GetAllWhere(
                                        m => m.ApartmentId == apartment.ApartmentId &&
                                        m.Keyword == utilitySplit[0]));

                    if (!inCache.Any())
                    {
                        _botCacheRepository.Insert(botCache);
                    }
                }

                var inTime  = apartment.CheckIn.TimeOfDay.ToString();
                var outTime = apartment.CheckOut.TimeOfDay.ToString();

                botCache = new BotCache("check", inTime + "&" + outTime, apartment.ApartmentId);

                inCache = await _botCacheRepository.GetAllWhere(m => m.ApartmentId == apartment.ApartmentId &&
                                                                m.Keyword == "check");

                if (!inCache.Any())
                {
                    _botCacheRepository.Insert(botCache);
                }
            }
        }
Example #10
0
        private static void Simulate(int millis)
        {
            int fullSteps = millis / TimestepMillis;

            while (fullSteps-- > 0)
            {
                BotCache.ForEach(structure => structure.SimulatedPhysicsUpdate(1f));
                Physics.Simulate(TimestepSeconds);
            }

            int mod = millis % TimestepMillis;

            if (mod != 0)
            {
                float timestepMultiplier = (float)mod / TimestepMillis;
                BotCache.ForEach(structure => structure.SimulatedPhysicsUpdate(timestepMultiplier));
                Physics.Simulate(mod / 1000f);
            }
        }
Example #11
0
        private void ClientOnlyFixedUpdate()
        {
            CompleteStructure localStructure = BotCache.Get(NetworkUtils.LocalId);
            int  toSimulate;
            long lastMillis;

            if (_lastClientUdpPacket != null)
            {
                _sharedBuffer.SetContents(_lastClientUdpPacket);
                _lastClientUdpPacket = null;
                ClientOnlyPacketReceived(localStructure, out toSimulate, out lastMillis);
                if (toSimulate == 0)
                {
                    return;
                }
            }
            else if (_silentSkipFastForwardUntil != 0)
            {
                return;                 //don't simulate normal steps if skipped the last state update simulation
            }
            else
            {
                toSimulate = TimestepMillis;
                lastMillis = DoubleProtocol.TimeMillis - TimestepMillis;
            }

            while (_guessedInputs.Count > 0)
            {
                KeyValuePair <long, GuessedInput> guessed = _guessedInputs.First();
                if (guessed.Key >= lastMillis)
                {
                    break;
                }

                _guessedInputs.Remove(guessed.Key);
                guessed.Value.RemainingDelay -= (int)(lastMillis - guessed.Key);
                if (guessed.Value.RemainingDelay > 0)
                {
                    _guessedInputs.Remove(lastMillis);
                    _guessedInputs.Add(lastMillis, guessed.Value);
                }
            }

            foreach (KeyValuePair <long, GuessedInput> guessed in _guessedInputs)
            {
                int delta = (int)(guessed.Key - lastMillis);
                if (delta > toSimulate)
                {
                    break;
                }
                else if (delta == 0)
                {
                    localStructure.UpdateInputOnly(guessed.Value.MovementInput, null);
                }
                else
                {
                    Simulate(delta);
                    localStructure.UpdateInputOnly(guessed.Value.MovementInput, null);
                    toSimulate -= delta;
                    lastMillis  = guessed.Key;
                }
            }

            if (toSimulate != 0)
            {
                Simulate(toSimulate);
            }
        }
 public void Update(BotCache botCache)
 {
     _botCacheRepository.Update(botCache);
 }
 public void Add(BotCache botCache)
 {
     _botCacheRepository.Insert(botCache);
 }